Bug 761706 - Bind GeckoConnectivityReceiver to the global application context. r=mfinkle

This commit is contained in:
Kartikaya Gupta 2012-09-14 11:19:40 -04:00
parent 4d76e23db3
commit 6aef15e675
2 changed files with 44 additions and 38 deletions

View File

@ -154,7 +154,6 @@ abstract public class GeckoApp
private boolean mIsRestoringActivity;
private String mCurrentResponse = "";
private GeckoConnectivityReceiver mConnectivityReceiver;
private GeckoBatteryManager mBatteryReceiver;
private PromptService mPromptService;
private Favicons mFavicons;
@ -1607,8 +1606,8 @@ abstract public class GeckoApp
mBatteryReceiver = new GeckoBatteryManager();
mBatteryReceiver.registerFor(mAppContext);
mConnectivityReceiver = new GeckoConnectivityReceiver();
mConnectivityReceiver.registerFor(mAppContext);
GeckoConnectivityReceiver.getInstance().init(this);
GeckoConnectivityReceiver.getInstance().start();
mPromptService = new PromptService();
@ -2140,8 +2139,7 @@ abstract public class GeckoApp
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();
}
@ -2152,8 +2150,7 @@ abstract public class GeckoApp
if (checkLaunchState(LaunchState.GeckoRunning))
GeckoAppShell.sendEventToGecko(GeckoEvent.createResumeEvent(true));
if (mConnectivityReceiver != null)
mConnectivityReceiver.registerFor(mAppContext);
GeckoConnectivityReceiver.getInstance().start();
GeckoNetworkManager.getInstance().start();
GeckoScreenOrientationListener.getInstance().start();
}

View File

@ -5,7 +5,6 @@
package org.mozilla.gecko;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@ -25,49 +24,59 @@ public class GeckoConnectivityReceiver extends BroadcastReceiver {
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);
}
@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 init(Context context) {
mApplicationContext = context.getApplicationContext();
}
public void registerFor(Activity activity) {
if (!isRegistered) {
// registerReciever will return null if registering fails
isRegistered = activity.registerReceiver(this, mFilter) != null;
if (!isRegistered)
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 void unregisterFor(Activity activity) {
if (isRegistered) {
try {
activity.unregisterReceiver(this);
} catch (IllegalArgumentException iae) {
Log.e(LOGTAG, "Unregistering receiver failed", iae);
}
isRegistered = false;
public synchronized void stop() {
if (mIsEnabled) {
mApplicationContext.unregisterReceiver(this);
mIsEnabled = false;
}
}
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
String status;
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);
}
}
}