diff --git a/embedding/android/GeckoApp.java b/embedding/android/GeckoApp.java index 90062ebbbc94..274012dbccae 100644 --- a/embedding/android/GeckoApp.java +++ b/embedding/android/GeckoApp.java @@ -88,8 +88,6 @@ abstract public class GeckoApp private IntentFilter mConnectivityFilter; private BroadcastReceiver mConnectivityReceiver; private BroadcastReceiver mBatteryReceiver; - private IntentFilter mNetworkFilter; - private GeckoNetworkManager mNetworkReceiver; enum LaunchState {PreLaunch, Launching, WaitForDebugger, Launched, GeckoRunning, GeckoExiting}; @@ -419,10 +417,7 @@ abstract public class GeckoApp SmsManager.getInstance().init(); } - mNetworkFilter = new IntentFilter(); - mNetworkFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); - mNetworkReceiver = new GeckoNetworkManager(); - mNetworkReceiver.initialize(); + GeckoNetworkManager.getInstance().init(); if (!checkAndSetLaunchState(LaunchState.PreLaunch, LaunchState.Launching)) @@ -516,7 +511,7 @@ abstract public class GeckoApp super.onPause(); unregisterReceiver(mConnectivityReceiver); - unregisterReceiver(mNetworkReceiver); + GeckoNetworkManager.getInstance().stop(); } @Override @@ -535,8 +530,7 @@ abstract public class GeckoApp onNewIntent(getIntent()); registerReceiver(mConnectivityReceiver, mConnectivityFilter); - mNetworkReceiver.resume(); - registerReceiver(mNetworkReceiver, mNetworkFilter); + GeckoNetworkManager.getInstance().start(); } @Override @@ -589,10 +583,11 @@ abstract public class GeckoApp SmsManager.getInstance().shutdown(); } + GeckoNetworkManager.getInstance().stop(); + super.onDestroy(); unregisterReceiver(mBatteryReceiver); - unregisterReceiver(mNetworkReceiver); } @Override diff --git a/embedding/android/GeckoAppShell.java b/embedding/android/GeckoAppShell.java index 34d512e227f6..60650758da3c 100644 --- a/embedding/android/GeckoAppShell.java +++ b/embedding/android/GeckoAppShell.java @@ -1778,6 +1778,6 @@ public class GeckoAppShell } public static double[] getCurrentNetworkInformation() { - return GeckoNetworkManager.getCurrentInformation(); + return GeckoNetworkManager.getInstance().getCurrentInformation(); } } diff --git a/embedding/android/GeckoNetworkManager.java b/embedding/android/GeckoNetworkManager.java index ac2c1fdd9df6..8e4883cee4bb 100644 --- a/embedding/android/GeckoNetworkManager.java +++ b/embedding/android/GeckoNetworkManager.java @@ -44,6 +44,7 @@ import android.util.Log; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; @@ -96,6 +97,8 @@ import android.telephony.TelephonyManager; public class GeckoNetworkManager extends BroadcastReceiver { + static private final GeckoNetworkManager sInstance = new GeckoNetworkManager(); + static private final double kDefaultBandwidth = -1.0; static private final boolean kDefaultCanBeMetered = false; @@ -127,36 +130,49 @@ public class GeckoNetworkManager NETWORK_UNKNOWN } - static private NetworkType sNetworkType = NetworkType.NETWORK_NONE; + private NetworkType mNetworkType = NetworkType.NETWORK_NONE; + private IntentFilter mNetworkFilter = new IntentFilter(); + + public static GeckoNetworkManager getInstance() { + return sInstance; + } @Override public void onReceive(Context aContext, Intent aIntent) { updateNetworkType(); } - public static void initialize() { - sNetworkType = getNetworkType(); + public void init() { + mNetworkFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); + + mNetworkType = getNetworkType(); } - public static void resume() { + public void start() { updateNetworkType(); + + GeckoApp.mAppContext.registerReceiver(sInstance, mNetworkFilter); } - private static void updateNetworkType() { - NetworkType previousNetworkType = sNetworkType; - sNetworkType = getNetworkType(); + public void stop() { + GeckoApp.mAppContext.unregisterReceiver(sInstance); + } - if (sNetworkType == previousNetworkType) { + private void updateNetworkType() { + NetworkType previousNetworkType = mNetworkType; + mNetworkType = getNetworkType(); + + if (mNetworkType == previousNetworkType) { return; } - GeckoAppShell.sendEventToGecko(new GeckoEvent(getNetworkSpeed(sNetworkType), - isNetworkUsuallyMetered(sNetworkType))); + GeckoAppShell.sendEventToGecko(new GeckoEvent(getNetworkSpeed(mNetworkType), + isNetworkUsuallyMetered(mNetworkType))); } - public static double[] getCurrentInformation() { - return new double[] { getNetworkSpeed(sNetworkType), - isNetworkUsuallyMetered(sNetworkType) ? 1.0 : 0.0 }; + public double[] getCurrentInformation() { + return new double[] { getNetworkSpeed(mNetworkType), + isNetworkUsuallyMetered(mNetworkType) ? 1.0 : 0.0 }; } private static NetworkType getNetworkType() { diff --git a/mobile/android/base/GeckoApp.java b/mobile/android/base/GeckoApp.java index 5fdeaac4ea00..46beb4c820f5 100644 --- a/mobile/android/base/GeckoApp.java +++ b/mobile/android/base/GeckoApp.java @@ -119,11 +119,9 @@ abstract public class GeckoApp private IntentFilter mConnectivityFilter; private IntentFilter mBatteryFilter; - private IntentFilter mNetworkFilter; private BroadcastReceiver mConnectivityReceiver; private BroadcastReceiver mBatteryReceiver; - private GeckoNetworkManager mNetworkReceiver; public static BrowserToolbar mBrowserToolbar; public static DoorHangerPopup mDoorHangerPopup; @@ -1576,10 +1574,7 @@ abstract public class GeckoApp SmsManager.getInstance().init(); } - mNetworkFilter = new IntentFilter(); - mNetworkFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); - mNetworkReceiver = new GeckoNetworkManager(); - mNetworkReceiver.initialize(); + GeckoNetworkManager.getInstance().init(); final GeckoApp self = this; @@ -1711,7 +1706,7 @@ abstract public class GeckoApp super.onPause(); unregisterReceiver(mConnectivityReceiver); - unregisterReceiver(mNetworkReceiver); + GeckoNetworkManager.getInstance().stop(); } @Override @@ -1729,8 +1724,7 @@ abstract public class GeckoApp onNewIntent(getIntent()); registerReceiver(mConnectivityReceiver, mConnectivityFilter); - mNetworkReceiver.resume(); - registerReceiver(mNetworkReceiver, mNetworkFilter); + GeckoNetworkManager.getInstance().start(); if (mOwnActivityDepth > 0) mOwnActivityDepth--; @@ -1813,10 +1807,11 @@ abstract public class GeckoApp SmsManager.getInstance().shutdown(); } + GeckoNetworkManager.getInstance().stop(); + super.onDestroy(); unregisterReceiver(mBatteryReceiver); - unregisterReceiver(mNetworkReceiver); } @Override diff --git a/mobile/android/base/GeckoAppShell.java b/mobile/android/base/GeckoAppShell.java index 58c712e51788..3111af2c2e8f 100644 --- a/mobile/android/base/GeckoAppShell.java +++ b/mobile/android/base/GeckoAppShell.java @@ -1749,6 +1749,6 @@ public class GeckoAppShell } public static double[] getCurrentNetworkInformation() { - return GeckoNetworkManager.getCurrentInformation(); + return GeckoNetworkManager.getInstance().getCurrentInformation(); } } diff --git a/mobile/android/base/GeckoNetworkManager.java b/mobile/android/base/GeckoNetworkManager.java index ac2c1fdd9df6..8e4883cee4bb 100644 --- a/mobile/android/base/GeckoNetworkManager.java +++ b/mobile/android/base/GeckoNetworkManager.java @@ -44,6 +44,7 @@ import android.util.Log; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; @@ -96,6 +97,8 @@ import android.telephony.TelephonyManager; public class GeckoNetworkManager extends BroadcastReceiver { + static private final GeckoNetworkManager sInstance = new GeckoNetworkManager(); + static private final double kDefaultBandwidth = -1.0; static private final boolean kDefaultCanBeMetered = false; @@ -127,36 +130,49 @@ public class GeckoNetworkManager NETWORK_UNKNOWN } - static private NetworkType sNetworkType = NetworkType.NETWORK_NONE; + private NetworkType mNetworkType = NetworkType.NETWORK_NONE; + private IntentFilter mNetworkFilter = new IntentFilter(); + + public static GeckoNetworkManager getInstance() { + return sInstance; + } @Override public void onReceive(Context aContext, Intent aIntent) { updateNetworkType(); } - public static void initialize() { - sNetworkType = getNetworkType(); + public void init() { + mNetworkFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); + + mNetworkType = getNetworkType(); } - public static void resume() { + public void start() { updateNetworkType(); + + GeckoApp.mAppContext.registerReceiver(sInstance, mNetworkFilter); } - private static void updateNetworkType() { - NetworkType previousNetworkType = sNetworkType; - sNetworkType = getNetworkType(); + public void stop() { + GeckoApp.mAppContext.unregisterReceiver(sInstance); + } - if (sNetworkType == previousNetworkType) { + private void updateNetworkType() { + NetworkType previousNetworkType = mNetworkType; + mNetworkType = getNetworkType(); + + if (mNetworkType == previousNetworkType) { return; } - GeckoAppShell.sendEventToGecko(new GeckoEvent(getNetworkSpeed(sNetworkType), - isNetworkUsuallyMetered(sNetworkType))); + GeckoAppShell.sendEventToGecko(new GeckoEvent(getNetworkSpeed(mNetworkType), + isNetworkUsuallyMetered(mNetworkType))); } - public static double[] getCurrentInformation() { - return new double[] { getNetworkSpeed(sNetworkType), - isNetworkUsuallyMetered(sNetworkType) ? 1.0 : 0.0 }; + public double[] getCurrentInformation() { + return new double[] { getNetworkSpeed(mNetworkType), + isNetworkUsuallyMetered(mNetworkType) ? 1.0 : 0.0 }; } private static NetworkType getNetworkType() {