Bug 1363457 - 2. Remove GeckoApp.getEventDispatcher(); r=droeh
authorJim Chen <nchen@mozilla.com>
Thu, 18 May 2017 17:40:32 -0400
changeset 409683 623036d074b415a010a734fcb5c53c88fd0ed143
parent 409682 a8ba0313fbffe131069b461dc6495c8a9f8d5729
child 409684 f13e13645ca1acb6ed957f778252100b3f713fb8
push id1490
push usermtabara@mozilla.com
push dateMon, 31 Jul 2017 14:08:16 +0000
treeherdermozilla-release@70e32e6bf15e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdroeh
bugs1363457
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1363457 - 2. Remove GeckoApp.getEventDispatcher(); r=droeh Use the current Activity to derive GeckoApp instances in tests, so we no longer need GeckoApp.getEventDispatcher() at all. Also refactor some code for getting an Activity from Context in FindInPageBar and FormAssistPopup. MozReview-Commit-ID: H1QNbtt9dB1
mobile/android/base/java/org/mozilla/gecko/FindInPageBar.java
mobile/android/base/java/org/mozilla/gecko/FormAssistPopup.java
mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/FennecNativeActions.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testEventDispatcher.java
mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testRuntimePermissionsAPI.java
--- a/mobile/android/base/java/org/mozilla/gecko/FindInPageBar.java
+++ b/mobile/android/base/java/org/mozilla/gecko/FindInPageBar.java
@@ -1,21 +1,21 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
+import org.mozilla.gecko.util.ActivityUtils;
 import org.mozilla.gecko.util.BundleEventListener;
 import org.mozilla.gecko.util.EventCallback;
 import org.mozilla.gecko.util.GeckoBundle;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import android.content.Context;
-import android.content.ContextWrapper;
 import android.text.Editable;
 import android.text.TextUtils;
 import android.text.TextWatcher;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -23,37 +23,30 @@ import android.view.inputmethod.InputMet
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
 public class FindInPageBar extends LinearLayout
         implements TextWatcher, View.OnClickListener, BundleEventListener {
     private static final String LOGTAG = "GeckoFindInPageBar";
     private static final String REQUEST_ID = "FindInPageBar";
 
-    private final Context mContext;
     /* package */ CustomEditText mFindText;
     private TextView mStatusText;
     private boolean mInflated;
-    private GeckoApp geckoApp;
+    private final GeckoApp geckoApp;
 
     public FindInPageBar(Context context, AttributeSet attrs) {
         super(context, attrs);
-        mContext = context;
         setFocusable(true);
 
-        while (context instanceof ContextWrapper) {
-            if (context instanceof GeckoApp) {
-                geckoApp = (GeckoApp) context;
-            }
-            context = ((ContextWrapper) context).getBaseContext();
-        }
+        geckoApp = (GeckoApp) ActivityUtils.getActivityFromContext(context);
     }
 
     public void inflateContent() {
-        LayoutInflater inflater = LayoutInflater.from(mContext);
+        LayoutInflater inflater = LayoutInflater.from(getContext());
         View content = inflater.inflate(R.layout.find_in_page_content, this);
 
         content.findViewById(R.id.find_prev).setOnClickListener(this);
         content.findViewById(R.id.find_next).setOnClickListener(this);
         content.findViewById(R.id.find_close).setOnClickListener(this);
 
         // Capture clicks on the rest of the view to prevent them from
         // leaking into other views positioned below.
--- a/mobile/android/base/java/org/mozilla/gecko/FormAssistPopup.java
+++ b/mobile/android/base/java/org/mozilla/gecko/FormAssistPopup.java
@@ -3,25 +3,25 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
 import org.mozilla.gecko.animation.ViewHelper;
 import org.mozilla.gecko.gfx.FloatSize;
 import org.mozilla.gecko.gfx.ImmutableViewportMetrics;
+import org.mozilla.gecko.util.ActivityUtils;
 import org.mozilla.gecko.util.BundleEventListener;
 import org.mozilla.gecko.util.EventCallback;
 import org.mozilla.gecko.util.GeckoBundle;
 import org.mozilla.gecko.util.ThreadUtils;
 import org.mozilla.gecko.widget.SwipeDismissListViewTouchListener;
 import org.mozilla.gecko.widget.SwipeDismissListViewTouchListener.OnDismissCallback;
 
 import android.content.Context;
-import android.content.ContextWrapper;
 import android.content.res.Resources;
 import android.graphics.PointF;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.util.Pair;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -36,25 +36,24 @@ import android.widget.ListView;
 import android.widget.RelativeLayout;
 import android.widget.RelativeLayout.LayoutParams;
 import android.widget.TextView;
 
 import java.util.Arrays;
 import java.util.Collection;
 
 public class FormAssistPopup extends RelativeLayout implements BundleEventListener {
-    private final Context mContext;
     private final Animation mAnimation;
 
     private ListView mAutoCompleteList;
     private RelativeLayout mValidationMessage;
     private TextView mValidationMessageText;
     private ImageView mValidationMessageArrow;
     private ImageView mValidationMessageArrowInverted;
-    private GeckoApp geckoApp;
+    private final GeckoApp geckoApp;
 
     private double mX;
     private double mY;
     private double mW;
     private double mH;
 
     private enum PopupType {
         AUTOCOMPLETE,
@@ -79,29 +78,23 @@ public class FormAssistPopup extends Rel
                                             InputMethods.METHOD_OPENWNN_PLUS,          // bug 768108
                                             InputMethods.METHOD_SIMEJI,                // bug 768108
                                             InputMethods.METHOD_SWYPE,                 // bug 755909
                                             InputMethods.METHOD_SWYPE_BETA            // bug 755909
                                             );
 
     public FormAssistPopup(Context context, AttributeSet attrs) {
         super(context, attrs);
-        mContext = context;
 
         mAnimation = AnimationUtils.loadAnimation(context, R.anim.grow_fade_in);
         mAnimation.setDuration(75);
 
         setFocusable(false);
 
-        while (context instanceof ContextWrapper) {
-            if (context instanceof GeckoApp) {
-                geckoApp = (GeckoApp) context;
-            }
-            context = ((ContextWrapper) context).getBaseContext();
-        }
+        geckoApp = (GeckoApp) ActivityUtils.getActivityFromContext(context);
     }
 
     @Override
     public void onAttachedToWindow() {
         super.onAttachedToWindow();
 
         geckoApp.getAppEventDispatcher().registerUiThreadListener(this,
             "FormAssist:AutoCompleteResult",
@@ -137,26 +130,26 @@ public class FormAssistPopup extends Rel
         } else if ("FormAssist:Hide".equals(event)) {
             hide();
         }
     }
 
     private void showAutoCompleteSuggestions(final GeckoBundle[] suggestions,
                                              final GeckoBundle rect,
                                              final boolean isEmpty) {
-        final String inputMethod = InputMethods.getCurrentInputMethod(mContext);
+        final String inputMethod = InputMethods.getCurrentInputMethod(getContext());
         if (!isEmpty && sInputMethodBlocklist.contains(inputMethod)) {
             // Don't display the form auto-complete popup after the user starts typing
             // to avoid confusing somes IME. See bug 758820 and bug 632744.
             hide();
             return;
         }
 
         if (mAutoCompleteList == null) {
-            LayoutInflater inflater = LayoutInflater.from(mContext);
+            LayoutInflater inflater = LayoutInflater.from(getContext());
             mAutoCompleteList = (ListView) inflater.inflate(R.layout.autocomplete_list, null);
 
             mAutoCompleteList.setOnItemClickListener(new OnItemClickListener() {
                 @Override
                 public void onItemClick(AdapterView<?> parentView, View view, int position, long id) {
                     // Use the value stored with the autocomplete view, not the label text,
                     // since they can be different.
                     final TextView textView = (TextView) view;
@@ -196,35 +189,37 @@ public class FormAssistPopup extends Rel
             mAutoCompleteList.setOnScrollListener(touchListener.makeScrollListener());
 
             // Setting this recycler listener is required to make sure animated views are reset.
             mAutoCompleteList.setRecyclerListener(touchListener.makeRecyclerListener());
 
             addView(mAutoCompleteList);
         }
 
-        AutoCompleteListAdapter adapter = new AutoCompleteListAdapter(mContext, R.layout.autocomplete_list_item);
+        AutoCompleteListAdapter adapter = new AutoCompleteListAdapter(
+                getContext(), R.layout.autocomplete_list_item);
         adapter.populateSuggestionsList(suggestions);
         mAutoCompleteList.setAdapter(adapter);
 
         if (setGeckoPositionData(rect, true)) {
             positionAndShowPopup();
         }
     }
 
     private void showValidationMessage(final String validationMessage,
                                        final GeckoBundle rect) {
         if (mValidationMessage == null) {
-            LayoutInflater inflater = LayoutInflater.from(mContext);
+            LayoutInflater inflater = LayoutInflater.from(getContext());
             mValidationMessage = (RelativeLayout) inflater.inflate(R.layout.validation_message, null);
 
             addView(mValidationMessage);
             mValidationMessageText = (TextView) mValidationMessage.findViewById(R.id.validation_message_text);
 
-            sValidationTextMarginTop = (int) (mContext.getResources().getDimension(R.dimen.validation_message_margin_top));
+            sValidationTextMarginTop = (int) (getContext().getResources().getDimension(
+                    R.dimen.validation_message_margin_top));
 
             sValidationTextLayoutNormal = new LayoutParams(mValidationMessageText.getLayoutParams());
             sValidationTextLayoutNormal.setMargins(0, sValidationTextMarginTop, 0, 0);
 
             sValidationTextLayoutInverted = new LayoutParams((ViewGroup.MarginLayoutParams) sValidationTextLayoutNormal);
             sValidationTextLayoutInverted.setMargins(0, 0, 0, 0);
 
             mValidationMessageArrow = (ImageView) mValidationMessage.findViewById(R.id.validation_message_arrow);
@@ -255,32 +250,32 @@ public class FormAssistPopup extends Rel
     private void positionAndShowPopup() {
         positionAndShowPopup(GeckoAppShell.getLayerView().getViewportMetrics());
     }
 
     private void positionAndShowPopup(ImmutableViewportMetrics aMetrics) {
         ThreadUtils.assertOnUiThread();
 
         // Don't show the form assist popup when using fullscreen VKB
-        InputMethodManager imm =
-                (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
+        InputMethodManager imm = (InputMethodManager)
+                getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
         if (imm.isFullscreenMode()) {
             return;
         }
 
         // Hide/show the appropriate popup contents
         if (mAutoCompleteList != null) {
             mAutoCompleteList.setVisibility((mPopupType == PopupType.AUTOCOMPLETE) ? VISIBLE : GONE);
         }
         if (mValidationMessage != null) {
             mValidationMessage.setVisibility((mPopupType == PopupType.AUTOCOMPLETE) ? GONE : VISIBLE);
         }
 
         if (sAutoCompleteMinWidth == 0) {
-            Resources res = mContext.getResources();
+            Resources res = getContext().getResources();
             sAutoCompleteMinWidth = (int) (res.getDimension(R.dimen.autocomplete_min_width));
             sAutoCompleteRowHeight = (int) (res.getDimension(R.dimen.autocomplete_row_height));
             sValidationMessageHeight = (int) (res.getDimension(R.dimen.validation_message_height));
         }
 
         float zoom = aMetrics.zoomFactor;
 
         // These values correspond to the input box for which we want to
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApp.java
@@ -2005,21 +2005,16 @@ public abstract class GeckoApp extends G
      * Activities that don't implement a normal tabbed browsing UI and overwrite the tab selection
      * made by session restoring should probably override this and return true.
      */
     protected boolean saveSelectedStartupTab() {
         return false;
     }
 
     @RobocopTarget
-    public static @NonNull EventDispatcher getEventDispatcher() {
-        final GeckoApp geckoApp = (GeckoApp) GeckoAppShell.getGeckoInterface();
-        return geckoApp.getAppEventDispatcher();
-    }
-
     public @NonNull EventDispatcher getAppEventDispatcher() {
         if (mLayerView == null) {
             throw new IllegalStateException("Must not call getAppEventDispatcher() until after onCreate()");
         }
 
         return mLayerView.getEventDispatcher();
     }
 
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/FennecNativeActions.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/FennecNativeActions.java
@@ -219,25 +219,27 @@ public class FennecNativeActions impleme
 
     public RepeatedEventExpecter expectGlobalEvent(final EventType type, final String geckoEvent) {
         FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, "waiting for " + geckoEvent);
         return new GeckoEventExpecter(EventDispatcher.getInstance(), type, geckoEvent);
     }
 
     public RepeatedEventExpecter expectWindowEvent(final EventType type, final String geckoEvent) {
         FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, "waiting for " + geckoEvent);
-        return new GeckoEventExpecter(GeckoApp.getEventDispatcher(), type, geckoEvent);
+        return new GeckoEventExpecter(
+                ((GeckoApp) mSolo.getCurrentActivity()).getAppEventDispatcher(),
+                type, geckoEvent);
     }
 
     public void sendGlobalEvent(final String event, final GeckoBundle data) {
         EventDispatcher.getInstance().dispatch(event, data);
     }
 
     public void sendWindowEvent(final String event, final GeckoBundle data) {
-        GeckoApp.getEventDispatcher().dispatch(event, data);
+        ((GeckoApp) mSolo.getCurrentActivity()).getAppEventDispatcher().dispatch(event, data);
     }
 
     public static final class PrefProxy implements PrefsHelper.PrefHandler, PrefWaiter {
         public static final int MAX_WAIT_MS = 180000;
 
         /* package */ final PrefHandlerBase target;
         private final String[] expectedPrefs;
         private final ArrayList<String> seenPrefs = new ArrayList<>();
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testEventDispatcher.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testEventDispatcher.java
@@ -60,22 +60,22 @@ public class testEventDispatcher extends
         testScope("global");
 
         // Test GeckoView-specific EventDispatcher.
         testScope("window");
 
         getJS().syncCall("finish_test");
     }
 
-    private static EventDispatcher getDispatcher(final String scope) {
+    private EventDispatcher getDispatcher(final String scope) {
         if ("global".equals(scope)) {
             return EventDispatcher.getInstance();
         }
         if ("window".equals(scope)) {
-            return GeckoApp.getEventDispatcher();
+            return ((GeckoApp) getActivity()).getAppEventDispatcher();
         }
         fFail("scope argument should be valid string");
         return null;
     }
 
     private void testScope(final String scope) {
         // Test Gecko thread events.
         getDispatcher(scope).registerGeckoThreadListener(
--- a/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testRuntimePermissionsAPI.java
+++ b/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/testRuntimePermissionsAPI.java
@@ -16,24 +16,27 @@ public class testRuntimePermissionsAPI e
     public testRuntimePermissionsAPI() {
         super("testRuntimePermissionsAPI.js");
     }
 
     @Override
     public void setUp() throws Exception {
         super.setUp();
 
-        GeckoApp.getEventDispatcher().registerUiThreadListener(this, "RuntimePermissions:Prompt");
+        ((GeckoApp) getActivity()).getAppEventDispatcher()
+                                  .registerUiThreadListener(this, "RuntimePermissions:Prompt");
     }
 
     @Override
     public void tearDown() throws Exception {
         super.tearDown();
 
-        GeckoApp.getEventDispatcher().unregisterUiThreadListener(this, "RuntimePermissions:Prompt");
+        ((GeckoApp) getActivity()).getAppEventDispatcher()
+                                  .unregisterUiThreadListener(this,
+                                                              "RuntimePermissions:Prompt");
     }
 
     @Override
     public void handleMessage(String event, GeckoBundle message, EventCallback callback) {
         mAsserter.is(event, "RuntimePermissions:Prompt", "Received RuntimePermissions:Prompt event");
 
         try {
             String[] permissions = message.getStringArray("permissions");