Bug 721344: Unregister Connectivity Receiver in only if it has been initialized. [r=mfinkle]
authorSriram Ramasubramanian <sriram@mozilla.com>
Mon, 02 Apr 2012 13:50:03 -0700
changeset 90872 d5100996fcc5ad1ea2a45beed6abd7dbcd44a003
parent 90871 5dfad57e46acee0013a0d093a81fad5b4ba3d155
child 90873 a9cc7ebe75985219945aeb73515e4adaea0893dc
push id7890
push usersramasubramanian@mozilla.com
push dateMon, 02 Apr 2012 20:51:18 +0000
treeherdermozilla-inbound@d5100996fcc5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs721344
milestone14.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 721344: Unregister Connectivity Receiver in only if it has been initialized. [r=mfinkle]
mobile/android/base/GeckoApp.java
mobile/android/base/GeckoConnectivityReceiver.java
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -122,19 +122,17 @@ abstract public class GeckoApp
     public static boolean mDOMFullScreen = false;
     public static Menu sMenu;
     private static GeckoThread sGeckoThread = null;
     public GeckoAppHandler mMainHandler;
     private GeckoProfile mProfile;
     public static boolean sIsGeckoReady = false;
     public static int mOrientation;
 
-    private IntentFilter mConnectivityFilter;
-
-    private BroadcastReceiver mConnectivityReceiver;
+    private GeckoConnectivityReceiver mConnectivityReceiver;
     private BroadcastReceiver mBatteryReceiver;
 
     public static BrowserToolbar mBrowserToolbar;
     public static DoorHangerPopup mDoorHangerPopup;
     public static FormAssistPopup mFormAssistPopup;
     public Favicons mFavicons;
 
     private static LayerController mLayerController;
@@ -1580,18 +1578,16 @@ abstract public class GeckoApp
 
         mBrowserToolbar = new BrowserToolbar(mAppContext);
         mBrowserToolbar.from(actionBar);
 
         // setup gecko layout
         mGeckoLayout = (RelativeLayout) findViewById(R.id.gecko_layout);
         mMainLayout = (LinearLayout) findViewById(R.id.main_layout);
 
-        mConnectivityFilter = new IntentFilter();
-        mConnectivityFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
         mConnectivityReceiver = new GeckoConnectivityReceiver();
     }
 
     private void initialize() {
         mInitialized = true;
 
         Intent intent = getIntent();
         String action = intent.getAction();
@@ -1981,17 +1977,17 @@ abstract public class GeckoApp
         // stop repainting, for example.
 
         // Whatever we do here should be fast, because we're blocking
         // the next activity from showing up until we finish.
 
         // onPause will be followed by either onResume or onStop.
         super.onPause();
 
-        unregisterReceiver(mConnectivityReceiver);
+        mConnectivityReceiver.unregisterFor(mAppContext);
         GeckoNetworkManager.getInstance().stop();
         GeckoScreenOrientationListener.getInstance().stop();
     }
 
     @Override
     public void onResume()
     {
         Log.i(LOGTAG, "resume");
@@ -2016,21 +2012,21 @@ abstract public class GeckoApp
         int newOrientation = getResources().getConfiguration().orientation;
 
         if (mOrientation != newOrientation) {
             mOrientation = newOrientation;
             refreshActionBar();
         }
 
         mMainHandler.post(new Runnable() {
-          public void run() {
-            registerReceiver(mConnectivityReceiver, mConnectivityFilter);
-            GeckoNetworkManager.getInstance().start();
-            GeckoScreenOrientationListener.getInstance().start();
-          }
+            public void run() {
+                mConnectivityReceiver.registerFor(mAppContext);
+                GeckoNetworkManager.getInstance().start();
+                GeckoScreenOrientationListener.getInstance().start();
+            }
         });
 
         if (mOwnActivityDepth > 0)
             mOwnActivityDepth--;
     }
 
     @Override
     public void onStop()
--- a/mobile/android/base/GeckoConnectivityReceiver.java
+++ b/mobile/android/base/GeckoConnectivityReceiver.java
@@ -32,42 +32,65 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 package org.mozilla.gecko;
 
+import android.app.Activity;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 
-public class GeckoConnectivityReceiver
-    extends BroadcastReceiver
-{
+public class GeckoConnectivityReceiver extends BroadcastReceiver {
     /*
      * Keep the below constants in sync with
      * http://mxr.mozilla.org/mozilla-central/source/netwerk/base/public/nsINetworkLinkService.idl
      */
     private static final String LINK_DATA_UP = "up";
     private static final String LINK_DATA_DOWN = "down";
     private static final String LINK_DATA_UNKNOWN = "unknown";
 
+    private IntentFilter mFilter;
+
+    private static boolean isRegistered = false;
+
+    public GeckoConnectivityReceiver() {
+        mFilter = new IntentFilter();
+        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
+    }
+
     @Override
     public void onReceive(Context context, Intent intent) {
         String status;
         ConnectivityManager cm = (ConnectivityManager)
             context.getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo info = cm.getActiveNetworkInfo();
         if (info == null)
             status = LINK_DATA_UNKNOWN;
         else if (!info.isConnected())
             status = LINK_DATA_DOWN;
         else
             status = LINK_DATA_UP;
 
         if (GeckoApp.checkLaunchState(GeckoApp.LaunchState.GeckoRunning))
             GeckoAppShell.onChangeNetworkLinkStatus(status);
     }
+
+    public void registerFor(Activity activity) {
+        if (!isRegistered) {
+            activity.registerReceiver(this, mFilter);
+            isRegistered = true;
+        }
+    }
+
+    public void unregisterFor(Activity activity) {
+        if (isRegistered) {
+            activity.unregisterReceiver(this);
+            isRegistered = false;
+        }
+    }
 }