Bug 761706 - Bind GeckoConnectivityReceiver to the global application context. r=mfinkle
authorKartikaya Gupta <kgupta@mozilla.com>
Fri, 14 Sep 2012 11:19:40 -0400
changeset 107204 29466c92fe4439f3cb465c14298d229fc40c0214
parent 107203 338823aa94a63d28394001d7a36293d85365a238
child 107205 0a5e2949c8f382f209d67aba4550fcbe2b249a72
push id74
push usershu@rfrn.org
push dateTue, 18 Sep 2012 19:23:47 +0000
reviewersmfinkle
bugs761706
milestone18.0a1
Bug 761706 - Bind GeckoConnectivityReceiver to the global application context. r=mfinkle
mobile/android/base/GeckoApp.java
mobile/android/base/GeckoConnectivityReceiver.java
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -149,17 +149,16 @@ abstract public class GeckoApp
     private static GeckoThread sGeckoThread;
     public Handler mMainHandler;
     private GeckoProfile mProfile;
     public static boolean sIsGeckoReady = false;
     public static int mOrientation;
     private boolean mIsRestoringActivity;
     private String mCurrentResponse = "";
 
-    private GeckoConnectivityReceiver mConnectivityReceiver;
     private GeckoBatteryManager mBatteryReceiver;
     private PromptService mPromptService;
     private Favicons mFavicons;
     private TextSelection mTextSelection;
 
     protected DoorHangerPopup mDoorHangerPopup;
     protected FormAssistPopup mFormAssistPopup;
     protected TabsPanel mTabsPanel;
@@ -1602,18 +1601,18 @@ abstract public class GeckoApp
 
         if (SmsManager.getInstance() != null) {
           SmsManager.getInstance().start();
         }
 
         mBatteryReceiver = new GeckoBatteryManager();
         mBatteryReceiver.registerFor(mAppContext);
 
-        mConnectivityReceiver = new GeckoConnectivityReceiver();
-        mConnectivityReceiver.registerFor(mAppContext);
+        GeckoConnectivityReceiver.getInstance().init(this);
+        GeckoConnectivityReceiver.getInstance().start();
 
         mPromptService = new PromptService();
 
         mTextSelection = new TextSelection((TextSelectionHandle) findViewById(R.id.start_handle),
                                            (TextSelectionHandle) findViewById(R.id.end_handle),
                                            GeckoAppShell.getEventDispatcher());
 
         GeckoNetworkManager.getInstance().init(this);
@@ -2135,30 +2134,28 @@ abstract public class GeckoApp
         GeckoAppShell.geckoEventSync();
     }
 
     @Override
     public void onApplicationPause() {
         Log.i(LOGTAG, "application paused");
         GeckoAppShell.sendEventToGecko(GeckoEvent.createPauseEvent(true));
 
-        if (mConnectivityReceiver != null)
-            mConnectivityReceiver.unregisterFor(mAppContext);
+        GeckoConnectivityReceiver.getInstance().stop();
         GeckoNetworkManager.getInstance().stop();
         GeckoScreenOrientationListener.getInstance().stop();
     }
 
     @Override
     public void onApplicationResume() {
         Log.i(LOGTAG, "application resumed");
         if (checkLaunchState(LaunchState.GeckoRunning))
             GeckoAppShell.sendEventToGecko(GeckoEvent.createResumeEvent(true));
 
-        if (mConnectivityReceiver != null)
-            mConnectivityReceiver.registerFor(mAppContext);
+        GeckoConnectivityReceiver.getInstance().start();
         GeckoNetworkManager.getInstance().start();
         GeckoScreenOrientationListener.getInstance().start();
     }
 
     @Override
     public Object onRetainNonConfigurationInstance() {
         // Send a non-null value so that we can restart the application, 
         // when activity restarts due to configuration change.
--- a/mobile/android/base/GeckoConnectivityReceiver.java
+++ b/mobile/android/base/GeckoConnectivityReceiver.java
@@ -1,16 +1,15 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * 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 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;
 import android.util.Log;
 
@@ -20,54 +19,64 @@ public class GeckoConnectivityReceiver e
      * 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 static final String LOGTAG = "GeckoConnectivityReceiver";
 
+    private static GeckoConnectivityReceiver sInstance = new GeckoConnectivityReceiver();
+
     private IntentFilter mFilter;
+    private Context mApplicationContext;
+    private boolean mIsEnabled;
 
-    private static boolean isRegistered = false;
+    public static GeckoConnectivityReceiver getInstance() {
+        return sInstance;
+    }
 
-    public GeckoConnectivityReceiver() {
+    private GeckoConnectivityReceiver() {
         mFilter = new IntentFilter();
         mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
     }
 
+    public void init(Context context) {
+        mApplicationContext = context.getApplicationContext();
+    }
+
+    public synchronized void start() {
+        if (!mIsEnabled) {
+            // registerReceiver will return null if registering fails
+            if (mApplicationContext.registerReceiver(this, mFilter) == null) {
+                Log.e(LOGTAG, "Registering receiver failed");
+            } else {
+                mIsEnabled = true;
+            }
+        }
+    }
+
+    public synchronized void stop() {
+        if (mIsEnabled) {
+            mApplicationContext.unregisterReceiver(this);
+            mIsEnabled = false;
+        }
+    }
+
     @Override
     public void onReceive(Context context, Intent intent) {
-        String status;
-        ConnectivityManager cm = (ConnectivityManager)
-            context.getSystemService(Context.CONNECTIVITY_SERVICE);
+        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) {
-            // registerReciever will return null if registering fails
-            isRegistered = activity.registerReceiver(this, mFilter) != null;
-            if (!isRegistered)
-                Log.e(LOGTAG, "Registering receiver failed");
+        String status;
+        if (info == null) {
+            status = LINK_DATA_UNKNOWN;
+        } else if (!info.isConnected()) {
+            status = LINK_DATA_DOWN;
+        } else {
+            status = LINK_DATA_UP;
         }
-    }
 
-    public void unregisterFor(Activity activity) {
-        if (isRegistered) {
-            try {
-                activity.unregisterReceiver(this);
-            } catch (IllegalArgumentException iae) {
-                Log.e(LOGTAG, "Unregistering receiver failed", iae);
-            }
-            isRegistered = false;
+        if (GeckoApp.checkLaunchState(GeckoApp.LaunchState.GeckoRunning)) {
+            GeckoAppShell.onChangeNetworkLinkStatus(status);
         }
     }
 }