Bug 1352997 - Part 1 - Register GeckoApp's onTabsChangedListener earlier. r?sebastian draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Sat, 08 Apr 2017 11:16:13 +0200
changeset 559305 2bdf7c7b5eef97ed213a8b0c71337300a8cd6e80
parent 559304 6e4780dcca94a79ea4019c42bd85a41dcab66c76
child 559306 b0870e702458e3cca07db16dc49efd0d49bc8c88
push id53051
push usermozilla@buttercookie.de
push dateSun, 09 Apr 2017 17:35:56 +0000
reviewerssebastian
bugs1352997
milestone55.0a1
Bug 1352997 - Part 1 - Register GeckoApp's onTabsChangedListener earlier. r?sebastian For BrowserApp we want to switch the last selected tab tracking to use tab selection events instead, so we need to register the listener earlier in order to catch the initial selection of the startup tab as well. MozReview-Commit-ID: F7luIE6oNK
mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
--- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
@@ -335,16 +335,21 @@ public class BrowserApp extends GeckoApp
         } else {
             view = super.onCreateView(name, context, attrs);
         }
         return view;
     }
 
     @Override
     public void onTabChanged(Tab tab, TabEvents msg, String data) {
+        if (!mInitialized) {
+            super.onTabChanged(tab, msg, data);
+            return;
+        }
+
         if (tab == null) {
             // Only RESTORED is allowed a null tab: it's the only event that
             // isn't tied to a specific tab.
             if (msg != Tabs.TabEvents.RESTORED) {
                 throw new IllegalArgumentException("onTabChanged:" + msg + " must specify a tab.");
             }
 
             final Tab selectedTab = Tabs.getInstance().getSelectedTab();
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
@@ -405,32 +405,41 @@ public abstract class GeckoApp
         // When a tab is unselected, another tab is always selected first.
         // When we're switching activites because of differing tab types, the above is not true.
         switch (msg) {
             case UNSELECTED:
                 break;
 
             case LOCATION_CHANGE:
                 // We only care about location change for the selected tab.
-                if (!Tabs.getInstance().isSelectedTab(tab))
-                    break;
-                // Fall through...
+                if (Tabs.getInstance().isSelectedTab(tab)) {
+                    resetOptionsMenu(true);
+                }
+                break;
+
             case SELECTED:
-                invalidateOptionsMenu();
-                if (mFormAssistPopup != null)
-                    mFormAssistPopup.hide();
+                resetOptionsMenu(true);
                 break;
 
             case DESKTOP_MODE_CHANGE:
                 if (Tabs.getInstance().isSelectedTab(tab))
-                    invalidateOptionsMenu();
+                    resetOptionsMenu(false);
                 break;
         }
     }
 
+    private void resetOptionsMenu(boolean resetFormAssist) {
+        if (mInitialized) {
+            invalidateOptionsMenu();
+            if (resetFormAssist && mFormAssistPopup != null) {
+                mFormAssistPopup.hide();
+            }
+        }
+    }
+
     public void refreshChrome() { }
 
     @Override
     public void invalidateOptionsMenu() {
         if (mMenu == null) {
             return;
         }
 
@@ -1318,16 +1327,17 @@ public abstract class GeckoApp
             "Share:Text",
             "SystemUI:Visibility",
             "ToggleChrome:Focus",
             "ToggleChrome:Hide",
             "ToggleChrome:Show",
             null);
 
         Tabs.getInstance().attachToContext(this, mLayerView);
+        Tabs.registerOnTabsChangedListener(this);
 
         // Use global layout state change to kick off additional initialization
         mMainLayout.getViewTreeObserver().addOnGlobalLayoutListener(this);
 
         if (Versions.preMarshmallow) {
             mTextSelection = new ActionBarTextSelection(this);
         } else {
             mTextSelection = new FloatingToolbarTextSelection(this, mLayerView);
@@ -1640,35 +1650,33 @@ public abstract class GeckoApp
         if (isFirstTab) {
             flags |= Tabs.LOADURL_FIRST_AFTER_ACTIVITY_UNHIDDEN;
         }
 
         return flags;
     }
 
     private void initialize() {
-        mInitialized = true;
-
         mWasFirstTabShownAfterActivityUnhidden = true; // Reset since we'll be loading a tab.
 
         final SafeIntent intent = new SafeIntent(getIntent());
         final String action = intent.getAction();
 
         final String passedUri = getIntentURI(intent);
 
         final boolean isExternalURL = invokedWithExternalURL(passedUri);
 
         // Start migrating as early as possible, can do this in
         // parallel with Gecko load.
         checkMigrateProfile();
 
-        Tabs.registerOnTabsChangedListener(this);
-
         initializeChrome();
 
+        mInitialized = true;
+
         // We need to wait here because mShouldRestore can revert back to
         // false if a parsing error occurs and the startup tab we load
         // depends on whether we restore tabs or not.
         synchronized (this) {
             while (!mSessionRestoreParsingFinished) {
                 try {
                     wait();
                 } catch (final InterruptedException e) {