Bug 1350718 - Post: Name the special value -1 used to indicate a new tab should be appended. r=maliu
authorTom Klein <twointofive@gmail.com>
Thu, 06 Apr 2017 11:27:42 -0500
changeset 558334 d544c4b2b56bc52233f263ca8a0b3b6723a81dc5
parent 558333 63b5915ed9bcd57c128f2424ae68f2226e4cdf15
child 558335 28d99fb2332752e1685deed31cd8bc6cfa01eb6b
push id52860
push userbmo:walkingice0204@gmail.com
push dateFri, 07 Apr 2017 13:29: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
@@ -76,16 +76,18 @@ public class Tabs implements BundleEvent
     /** 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;
     public static final int LOADURL_WEBAPP = 1 << 10;
 
     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;
@@ -255,28 +257,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;
@@ -1017,17 +1026,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;