Bug 742216: Battery sensor cleanup in the similar lines of GeckoConnectivityReceiver. [r=mfinkle]

This commit is contained in:
Sriram Ramasubramanian 2012-04-04 17:31:19 -07:00
Родитель 499282017e
Коммит 0bef526bac
2 изменённых файлов: 25 добавлений и 8 удалений

Просмотреть файл

@ -129,7 +129,7 @@ abstract public class GeckoApp
public static int mOrientation;
private GeckoConnectivityReceiver mConnectivityReceiver;
private BroadcastReceiver mBatteryReceiver;
private GeckoBatteryManager mBatteryReceiver;
public static BrowserToolbar mBrowserToolbar;
public static DoorHangerPopup mDoorHangerPopup;
@ -1731,15 +1731,13 @@ abstract public class GeckoApp
GeckoAppShell.registerGeckoEventListener("Session:StatePurged", GeckoApp.mAppContext);
GeckoAppShell.registerGeckoEventListener("Bookmark:Insert", GeckoApp.mAppContext);
IntentFilter batteryFilter = new IntentFilter();
batteryFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
mBatteryReceiver = new GeckoBatteryManager();
registerReceiver(mBatteryReceiver, batteryFilter);
if (SmsManager.getInstance() != null) {
SmsManager.getInstance().start();
}
mBatteryReceiver = new GeckoBatteryManager();
mBatteryReceiver.registerFor(mAppContext);
mConnectivityReceiver = new GeckoConnectivityReceiver();
mConnectivityReceiver.registerFor(mAppContext);
@ -2088,7 +2086,7 @@ abstract public class GeckoApp
super.onDestroy();
unregisterReceiver(mBatteryReceiver);
mBatteryReceiver.unregisterFor(mAppContext);
if (mAboutHomeContent != null) {
mAboutHomeContent.onDestroy();

Просмотреть файл

@ -42,10 +42,11 @@ import java.util.Date;
import android.util.Log;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;
import android.os.SystemClock;
@ -68,6 +69,24 @@ public class GeckoBatteryManager
private static boolean sCharging = kDefaultCharging;
private static double sRemainingTime = kDefaultRemainingTime;;
private static boolean isRegistered = false;
public void registerFor(Activity activity) {
if (!isRegistered) {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
activity.registerReceiver(this, filter);
isRegistered = true;
}
}
public void unregisterFor(Activity activity) {
if (isRegistered) {
activity.unregisterReceiver(this);
isRegistered = false;
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {