Bug 713687 - Part 3 - Network API Android backend: make GeckoNetworkManager a singleton. r=dougt
authorMounir Lamouri <mounir.lamouri@gmail.com>
Mon, 16 Jan 2012 18:17:34 +0100
changeset 84751 9ee757bbbcd0031fe56d1cf394e0e7d9bff9bcee
parent 84750 b92399819ec4dc53371f336283ed58c21884b9bd
child 84752 9f68bafdd6852e727f4af1a303ca20a9bab0466a
push id443
push usertim.taubert@gmx.de
push dateWed, 18 Jan 2012 12:23:22 +0000
treeherderfx-team@f93675b9e0ac [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdougt
bugs713687
milestone12.0a1
Bug 713687 - Part 3 - Network API Android backend: make GeckoNetworkManager a singleton. r=dougt
embedding/android/GeckoApp.java
embedding/android/GeckoAppShell.java
embedding/android/GeckoNetworkManager.java
mobile/android/base/GeckoApp.java
mobile/android/base/GeckoAppShell.java
mobile/android/base/GeckoNetworkManager.java
--- a/embedding/android/GeckoApp.java
+++ b/embedding/android/GeckoApp.java
@@ -83,18 +83,16 @@ abstract public class GeckoApp
     public static GeckoApp mAppContext;
     public static boolean mFullscreen = false;
     public static File sGREDir = null;
     static Thread mLibLoadThread = null;
     public Handler mMainHandler;
     private IntentFilter mConnectivityFilter;
     private BroadcastReceiver mConnectivityReceiver;
     private BroadcastReceiver mBatteryReceiver;
-    private IntentFilter mNetworkFilter;
-    private GeckoNetworkManager mNetworkReceiver;
 
     enum LaunchState {PreLaunch, Launching, WaitForDebugger,
                       Launched, GeckoRunning, GeckoExiting};
     private static LaunchState sLaunchState = LaunchState.PreLaunch;
     private static boolean sTryCatchAttached = false;
 
 
     static boolean checkLaunchState(LaunchState checkState) {
@@ -414,20 +412,17 @@ abstract public class GeckoApp
         batteryFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
         mBatteryReceiver = new GeckoBatteryManager();
         registerReceiver(mBatteryReceiver, batteryFilter);
 
         if (SmsManager.getInstance() != null) {
             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))
             return;
 
         checkAndLaunchUpdate();
         mLibLoadThread = new Thread(new Runnable() {
             public void run() {
@@ -511,17 +506,17 @@ abstract public class GeckoApp
 
         // 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);
-        unregisterReceiver(mNetworkReceiver);
+        GeckoNetworkManager.getInstance().stop();
     }
 
     @Override
     public void onResume()
     {
         Log.i(LOG_FILE_NAME, "resume");
         if (checkLaunchState(LaunchState.GeckoRunning))
             GeckoAppShell.onResume();
@@ -530,18 +525,17 @@ abstract public class GeckoApp
         super.onResume();
 
         // Just in case. Normally we start in onNewIntent
         if (checkLaunchState(LaunchState.PreLaunch) ||
             checkLaunchState(LaunchState.Launching))
             onNewIntent(getIntent());
 
         registerReceiver(mConnectivityReceiver, mConnectivityFilter);
-        mNetworkReceiver.resume();
-        registerReceiver(mNetworkReceiver, mNetworkFilter);
+        GeckoNetworkManager.getInstance().start();
     }
 
     @Override
     public void onStop()
     {
         Log.i(LOG_FILE_NAME, "stop");
         // We're about to be stopped, potentially in preparation for
         // being destroyed.  We're killable after this point -- as I
@@ -584,20 +578,21 @@ abstract public class GeckoApp
         // in onXreExit.
         if (isFinishing())
             GeckoAppShell.sendEventToGecko(new GeckoEvent(GeckoEvent.ACTIVITY_SHUTDOWN));
 
         if (SmsManager.getInstance() != null) {
             SmsManager.getInstance().shutdown();
         }
 
+        GeckoNetworkManager.getInstance().stop();
+
         super.onDestroy();
 
         unregisterReceiver(mBatteryReceiver);
-        unregisterReceiver(mNetworkReceiver);
     }
 
     @Override
     public void onConfigurationChanged(android.content.res.Configuration newConfig)
     {
         Log.i(LOG_FILE_NAME, "configuration changed");
         // nothing, just ignore
         super.onConfigurationChanged(newConfig);
--- a/embedding/android/GeckoAppShell.java
+++ b/embedding/android/GeckoAppShell.java
@@ -1773,11 +1773,11 @@ public class GeckoAppShell
             if ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE) {
                 return true;
             }
         }
         return false;
     }
 
     public static double[] getCurrentNetworkInformation() {
-        return GeckoNetworkManager.getCurrentInformation();
+        return GeckoNetworkManager.getInstance().getCurrentInformation();
     }
 }
--- a/embedding/android/GeckoNetworkManager.java
+++ b/embedding/android/GeckoNetworkManager.java
@@ -39,16 +39,17 @@ package org.mozilla.gecko;
 
 import java.lang.Math;
 
 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;
 
 import android.telephony.TelephonyManager;
 
 /*
  * A part of the work of GeckoNetworkManager is to give an estimation of the
@@ -91,16 +92,18 @@ import android.telephony.TelephonyManage
  *
  * 3.9G (50 Mb/s)
  * int NETWORK_TYPE_LTE      Current network is LTE
  */
 
 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;
 
   static private final double  kMaxBandwidth = 20.0;
 
   static private final double  kNetworkSpeedEthernet = 20.0;           // 20 Mb/s
   static private final double  kNetworkSpeedWifi     = 20.0;           // 20 Mb/s
   static private final double  kNetworkSpeedWiMax    = 40.0;           // 40 Mb/s
@@ -122,46 +125,59 @@ public class GeckoNetworkManager
     NETWORK_2_75_G, // 2.75G
     NETWORK_3_G,    // 3G
     NETWORK_3_5_G,  // 3.5G
     NETWORK_3_75_G, // 3.75G
     NETWORK_3_9_G,  // 3.9G
     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() {
     ConnectivityManager cm =
       (ConnectivityManager)GeckoApp.mAppContext.getSystemService(Context.CONNECTIVITY_SERVICE);
 
     if (cm.getActiveNetworkInfo() == null) {
       return NetworkType.NETWORK_NONE;
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -114,21 +114,19 @@ abstract public class GeckoApp
     private static GeckoThread sGeckoThread = null;
     public Handler mMainHandler;
     private File mProfileDir;
     private static boolean sIsGeckoReady = false;
     private static int mOrientation;
 
     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;
     public static AutoCompletePopup mAutoCompletePopup;
     public Favicons mFavicons;
 
     private Geocoder mGeocoder;
     private Address  mLastGeoAddress;
@@ -1571,20 +1569,17 @@ abstract public class GeckoApp
         batteryFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
         mBatteryReceiver = new GeckoBatteryManager();
         registerReceiver(mBatteryReceiver, batteryFilter);
 
         if (SmsManager.getInstance() != null) {
           SmsManager.getInstance().init();
         }
 
-        mNetworkFilter = new IntentFilter();
-        mNetworkFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
-        mNetworkReceiver = new GeckoNetworkManager();
-        mNetworkReceiver.initialize();
+        GeckoNetworkManager.getInstance().init();
 
         final GeckoApp self = this;
  
         GeckoAppShell.getHandler().postDelayed(new Runnable() {
             public void run() {
                 
                 Log.w(LOGTAG, "zerdatime " + new Date().getTime() + " - pre checkLaunchState");
 
@@ -1706,17 +1701,17 @@ abstract public class GeckoApp
 
         // 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);
-        unregisterReceiver(mNetworkReceiver);
+        GeckoNetworkManager.getInstance().stop();
     }
 
     @Override
     public void onResume()
     {
         Log.i(LOGTAG, "resume");
         if (checkLaunchState(LaunchState.GeckoRunning))
             GeckoAppShell.onResume();
@@ -1724,18 +1719,17 @@ abstract public class GeckoApp
         // Undo whatever we did in onPause.
         super.onResume();
 
         // Just in case. Normally we start in onNewIntent
         if (checkLaunchState(LaunchState.Launching))
             onNewIntent(getIntent());
 
         registerReceiver(mConnectivityReceiver, mConnectivityFilter);
-        mNetworkReceiver.resume();
-        registerReceiver(mNetworkReceiver, mNetworkFilter);
+        GeckoNetworkManager.getInstance().start();
 
         if (mOwnActivityDepth > 0)
             mOwnActivityDepth--;
     }
 
     @Override
     public void onStop()
     {
@@ -1808,20 +1802,21 @@ abstract public class GeckoApp
         GeckoAppShell.unregisterGeckoEventListener("Downloads:Done", GeckoApp.mAppContext);
 
         mFavicons.close();
 
         if (SmsManager.getInstance() != null) {
           SmsManager.getInstance().shutdown();
         }
 
+        GeckoNetworkManager.getInstance().stop();
+
         super.onDestroy();
 
         unregisterReceiver(mBatteryReceiver);
-        unregisterReceiver(mNetworkReceiver);
     }
 
     @Override
     public void onContentChanged() {
         super.onContentChanged();
         if (mAboutHomeContent != null)
             mAboutHomeContent.onActivityContentChanged(this);
     }
--- a/mobile/android/base/GeckoAppShell.java
+++ b/mobile/android/base/GeckoAppShell.java
@@ -1744,11 +1744,11 @@ public class GeckoAppShell
 
     public static void viewSizeChanged() {
         if (mInputConnection != null && mInputConnection.isIMEEnabled()) {
             sendEventToGecko(new GeckoEvent("ScrollTo:FocusedInput", ""));
         }
     }
 
     public static double[] getCurrentNetworkInformation() {
-        return GeckoNetworkManager.getCurrentInformation();
+        return GeckoNetworkManager.getInstance().getCurrentInformation();
     }
 }
--- a/mobile/android/base/GeckoNetworkManager.java
+++ b/mobile/android/base/GeckoNetworkManager.java
@@ -39,16 +39,17 @@ package org.mozilla.gecko;
 
 import java.lang.Math;
 
 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;
 
 import android.telephony.TelephonyManager;
 
 /*
  * A part of the work of GeckoNetworkManager is to give an estimation of the
@@ -91,16 +92,18 @@ import android.telephony.TelephonyManage
  *
  * 3.9G (50 Mb/s)
  * int NETWORK_TYPE_LTE      Current network is LTE
  */
 
 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;
 
   static private final double  kMaxBandwidth = 20.0;
 
   static private final double  kNetworkSpeedEthernet = 20.0;           // 20 Mb/s
   static private final double  kNetworkSpeedWifi     = 20.0;           // 20 Mb/s
   static private final double  kNetworkSpeedWiMax    = 40.0;           // 40 Mb/s
@@ -122,46 +125,59 @@ public class GeckoNetworkManager
     NETWORK_2_75_G, // 2.75G
     NETWORK_3_G,    // 3G
     NETWORK_3_5_G,  // 3.5G
     NETWORK_3_75_G, // 3.75G
     NETWORK_3_9_G,  // 3.9G
     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() {
     ConnectivityManager cm =
       (ConnectivityManager)GeckoApp.mAppContext.getSystemService(Context.CONNECTIVITY_SERVICE);
 
     if (cm.getActiveNetworkInfo() == null) {
       return NetworkType.NETWORK_NONE;