diff --git a/mobile/android/base/GeckoApp.java b/mobile/android/base/GeckoApp.java index 1e7184f06006..23356c9303ec 100644 --- a/mobile/android/base/GeckoApp.java +++ b/mobile/android/base/GeckoApp.java @@ -127,9 +127,7 @@ abstract public class GeckoApp 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; @@ -1585,8 +1583,6 @@ abstract public class GeckoApp 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(); } @@ -1986,7 +1982,7 @@ abstract public class GeckoApp // onPause will be followed by either onResume or onStop. super.onPause(); - unregisterReceiver(mConnectivityReceiver); + mConnectivityReceiver.unregisterFor(mAppContext); GeckoNetworkManager.getInstance().stop(); GeckoScreenOrientationListener.getInstance().stop(); } @@ -2021,11 +2017,11 @@ abstract public class GeckoApp } 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) diff --git a/mobile/android/base/GeckoConnectivityReceiver.java b/mobile/android/base/GeckoConnectivityReceiver.java index 2cf20d7436b4..4468fb0b975c 100644 --- a/mobile/android/base/GeckoConnectivityReceiver.java +++ b/mobile/android/base/GeckoConnectivityReceiver.java @@ -37,15 +37,15 @@ 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 @@ -54,6 +54,15 @@ public class GeckoConnectivityReceiver 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; @@ -70,4 +79,18 @@ public class GeckoConnectivityReceiver 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; + } + } }