Bug 1350718 - Post: Name the special value -1 used to indicate a new tab should be appended. r?maliu draft
authorTom Klein <twointofive@gmail.com>
Thu, 06 Apr 2017 11:27:42 -0500
changeset 557338 2d664a8df58a3aa7c2f2c9a6db030df7d08ac93a
parent 557337 a190c4e528811d01be4f0d7773adb406d9c86309
child 623025 c974dc7087eafa39d4f2fbdad6d8524eba3d3651
push id52689
push userbmo:twointofive@gmail.com
push dateThu, 06 Apr 2017 17:27:26 +0000
reviewersmaliu
bugs1350718
milestone55.0a1
Bug 1350718 - Post: Name the special value -1 used to indicate a new tab should be appended. r?maliu MozReview-Commit-ID: 7r8cHItW4wG
mobile/android/base/java/org/mozilla/gecko/Tabs.java
mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayout.java
--- a/mobile/android/base/java/org/mozilla/gecko/Tabs.java
+++ b/mobile/android/base/java/org/mozilla/gecko/Tabs.java
@@ -75,16 +75,18 @@ public class Tabs implements BundleEvent
     public static final int LOADURL_EXTERNAL     = 1 << 7;
     /** Indicates the tab is the first shown after Firefox is hidden and restored. */
     public static final int LOADURL_FIRST_AFTER_ACTIVITY_UNHIDDEN = 1 << 8;
     public static final int LOADURL_CUSTOMTAB    = 1 << 9;
 
     private static final long PERSIST_TABS_AFTER_MILLISECONDS = 1000 * 2;
 
     public static final int INVALID_TAB_ID = -1;
+    // Used to indicate a new tab should be appended to the current tabs.
+    public static final int NEW_LAST_INDEX = -1;
 
     private static final AtomicInteger sTabId = new AtomicInteger(0);
     private volatile boolean mInitialTabsAdded;
 
     private Context mAppContext;
     private LayerView mLayerView;
     private ContentObserver mBookmarksContentObserver;
     private PersistTabsRunnable mPersistTabsRunnable;
@@ -254,28 +256,35 @@ public class Tabs implements BundleEvent
         if (mInitialTabsAdded) {
             notifyListeners(tab, TabEvents.ADDED,
                     Integer.toString(getPrivacySpecificTabIndex(tabIndex, isPrivate, type)));
         }
 
         return tab;
     }
 
-    // Return the index, among those tabs of the chosen type whose privacy setting matches
-    // isPrivate, of the tab at position index in mOrder.  Returns -1, for "new last tab",
-    // when index is -1.
+    /**
+     * Return the index, among those tabs of the chosen {@code type} whose privacy setting matches
+     * {@code isPrivate}, of the tab at position {@code index} in {@code mOrder}.  Returns
+     * {@code NEW_LAST_INDEX} when {@code index} is {@code NEW_LAST_INDEX} or no matches were
+     * found.
+     */
     private int getPrivacySpecificTabIndex(int index, boolean isPrivate, TabType type) {
+        if (index == NEW_LAST_INDEX) {
+            return NEW_LAST_INDEX;
+        }
+
         int privacySpecificIndex = -1;
         for (int i = 0; i <= index; i++) {
             final Tab tab = mOrder.get(i);
             if (tab.isPrivate() == isPrivate && tab.getType() == type) {
                 privacySpecificIndex++;
             }
         }
-        return privacySpecificIndex;
+        return privacySpecificIndex > -1 ? privacySpecificIndex : NEW_LAST_INDEX;
     }
 
     public synchronized void removeTab(int id) {
         if (mTabs.containsKey(id)) {
             Tab tab = getTab(id);
             mOrder.remove(tab);
             mTabs.remove(id);
             tabPositionCache.mTabId = INVALID_TAB_ID;
@@ -1014,17 +1023,17 @@ public class Tabs implements BundleEvent
             data.putInt("tabID", tabId);
 
             // The URL is updated for the tab once Gecko responds with the
             // Tab:Added data. We can preliminarily set the tab's URL as
             // long as it's a valid URI.
             String tabUrl = (url != null && Uri.parse(url).getScheme() != null) ? url : null;
 
             // Add the new tab to the end of the tab order.
-            final int tabIndex = -1;
+            final int tabIndex = NEW_LAST_INDEX;
 
             tabToSelect = addTab(tabId, tabUrl, external, parentId, url, isPrivate, tabIndex, type);
             tabToSelect.setDesktopMode(desktopMode);
             tabToSelect.setApplicationId(applicationId);
             if (isFirstShownAfterActivityUnhidden) {
                 // We just opened Firefox so we want to show
                 // the toolbar but not animate it to avoid jank.
                 tabToSelect.setShouldShowToolbarWithoutAnimationOnFirstSelection(true);
--- a/mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayout.java
+++ b/mobile/android/base/java/org/mozilla/gecko/tabs/TabsLayout.java
@@ -101,18 +101,17 @@ public abstract class TabsLayout extends
     public void onTabChanged(Tab tab, Tabs.TabEvents msg, String data) {
         if (msg != Tabs.TabEvents.RESTORED && tab.getType() != type) {
             return;
         }
 
         switch (msg) {
             case ADDED:
                 int tabIndex = Integer.parseInt(data);
-                // A tabIndex of -1 means "add a new last tab".
-                tabIndex = tabIndex == -1 ? tabsAdapter.getItemCount() : tabIndex;
+                tabIndex = tabIndex == Tabs.NEW_LAST_INDEX ? tabsAdapter.getItemCount() : tabIndex;
                 tabsAdapter.notifyTabInserted(tab, tabIndex);
                 if (addAtIndexRequiresScroll(tabIndex)) {
                     // (The SELECTED tab is updated *after* this call to ADDED, so don't just call
                     // updateSelectedPosition().)
                     scrollToPosition(tabIndex);
                 }
                 break;