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 559319 547894d33f6da1ffb0ab46079f907f618848dc61
parent 559317 51cdc417d448d6c9a485ac243f1ea60e4185912c
child 559320 2fc77aba52828c165a8b41fb05ad280391619e18
push id53055
push usermozilla@buttercookie.de
push dateSun, 09 Apr 2017 20:38:11 +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) {