| Index: device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java
|
| diff --git a/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java b/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java
|
| index 90c0f38f68bd9e590fa898cf3d53347fa00ef4ea..da1b55aabd104598cade25d8314e435396ba69bf 100644
|
| --- a/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java
|
| +++ b/device/battery/android/java/src/org/chromium/device/battery/BatteryStatusManager.java
|
| @@ -12,20 +12,25 @@ import android.os.BatteryManager;
|
| import android.os.Build;
|
| import android.util.Log;
|
|
|
| -import org.chromium.base.CalledByNative;
|
| -import org.chromium.base.JNINamespace;
|
| import org.chromium.base.VisibleForTesting;
|
|
|
| /**
|
| - * Android implementation of the battery status APIs.
|
| + * Data source for battery status information. This class registers for battery status notifications
|
| + * from the system and calls the callback passed on construction whenever a notification is
|
| + * received.
|
| */
|
| -@JNINamespace("device")
|
| class BatteryStatusManager {
|
|
|
| private static final String TAG = "BatteryStatusManager";
|
|
|
| + interface BatteryStatusCallback {
|
| + void onBatteryStatusChanged(boolean charging, double chargingTime, double dischargingTime,
|
| + double level);
|
| + }
|
| +
|
| // A reference to the application context in order to acquire the SensorService.
|
| private final Context mAppContext;
|
| + private final BatteryStatusCallback mCallback;
|
| private final IntentFilter mFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
|
| private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
| @Override
|
| @@ -34,50 +39,52 @@ class BatteryStatusManager {
|
| }
|
| };
|
|
|
| - // Non-zero if and only if we're listening for events.
|
| - // To avoid race conditions on the C++ side, access must be synchronized.
|
| - private long mNativePtr;
|
| - // The lock to access the mNativePtr.
|
| - private final Object mNativePtrLock = new Object();
|
| + // This is to workaround a Galaxy Nexus bug, see the comment in the constructor.
|
| + private final boolean mIgnoreBatteryPresentState;
|
|
|
| private boolean mEnabled = false;
|
|
|
| - protected BatteryStatusManager(Context context) {
|
| + private BatteryStatusManager(Context context, BatteryStatusCallback callback,
|
| + boolean ignoreBatteryPresentState) {
|
| mAppContext = context.getApplicationContext();
|
| + mCallback = callback;
|
| + mIgnoreBatteryPresentState = ignoreBatteryPresentState;
|
| }
|
|
|
| - @CalledByNative
|
| - static BatteryStatusManager getInstance(Context appContext) {
|
| - return new BatteryStatusManager(appContext);
|
| + BatteryStatusManager(Context context, BatteryStatusCallback callback) {
|
| + // BatteryManager.EXTRA_PRESENT appears to be unreliable on Galaxy Nexus,
|
| + // Android 4.2.1, it always reports false. See http://crbug.com/384348.
|
| + this(context, callback, Build.MODEL.equals("Galaxy Nexus"));
|
| }
|
|
|
| /**
|
| - * Start listening for intents
|
| + * Creates a BatteryStatusManager without the Galaxy Nexus workaround for consistency in
|
| + * testing.
|
| + */
|
| + static BatteryStatusManager createBatteryStatusManagerForTesting(Context context,
|
| + BatteryStatusCallback callback) {
|
| + return new BatteryStatusManager(context, callback, false);
|
| + }
|
| +
|
| + /**
|
| + * Starts listening for intents.
|
| * @return True on success.
|
| */
|
| - @CalledByNative
|
| - boolean start(long nativePtr) {
|
| - synchronized (mNativePtrLock) {
|
| - if (!mEnabled && mAppContext.registerReceiver(mReceiver, mFilter) != null) {
|
| - // success
|
| - mNativePtr = nativePtr;
|
| - mEnabled = true;
|
| - }
|
| + boolean start() {
|
| + if (!mEnabled && mAppContext.registerReceiver(mReceiver, mFilter) != null) {
|
| + // success
|
| + mEnabled = true;
|
| }
|
| return mEnabled;
|
| }
|
|
|
| /**
|
| - * Stop listening to intents.
|
| + * Stops listening to intents.
|
| */
|
| - @CalledByNative
|
| void stop() {
|
| - synchronized (mNativePtrLock) {
|
| - if (mEnabled) {
|
| - mAppContext.unregisterReceiver(mReceiver);
|
| - mNativePtr = 0;
|
| - mEnabled = false;
|
| - }
|
| + if (mEnabled) {
|
| + mAppContext.unregisterReceiver(mReceiver);
|
| + mEnabled = false;
|
| }
|
| }
|
|
|
| @@ -88,13 +95,13 @@ class BatteryStatusManager {
|
| return;
|
| }
|
|
|
| - boolean present = ignoreBatteryPresentState()
|
| + boolean present = mIgnoreBatteryPresentState
|
| ? true : intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false);
|
| int pluggedStatus = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
|
|
|
| if (!present || pluggedStatus == -1) {
|
| // No battery or no plugged status: return default values.
|
| - gotBatteryStatus(true, 0, Double.POSITIVE_INFINITY, 1);
|
| + mCallback.onBatteryStatusChanged(true, 0, Double.POSITIVE_INFINITY, 1);
|
| return;
|
| }
|
|
|
| @@ -117,32 +124,6 @@ class BatteryStatusManager {
|
| double chargingTime = (charging & batteryFull) ? 0 : Double.POSITIVE_INFINITY;
|
| double dischargingTime = Double.POSITIVE_INFINITY;
|
|
|
| - gotBatteryStatus(charging, chargingTime, dischargingTime, level);
|
| - }
|
| -
|
| - /**
|
| - * Returns whether the BatteryStatusManager should ignore the battery present state.
|
| - * It is required for some devices that incorrectly set the EXTRA_PRESENT property.
|
| - */
|
| - protected boolean ignoreBatteryPresentState() {
|
| - // BatteryManager.EXTRA_PRESENT appears to be unreliable on Galaxy Nexus,
|
| - // Android 4.2.1, it always reports false. See crbug.com/384348.
|
| - return Build.MODEL.equals("Galaxy Nexus");
|
| + mCallback.onBatteryStatusChanged(charging, chargingTime, dischargingTime, level);
|
| }
|
| -
|
| - protected void gotBatteryStatus(boolean charging, double chargingTime,
|
| - double dischargingTime, double level) {
|
| - synchronized (mNativePtrLock) {
|
| - if (mNativePtr != 0) {
|
| - nativeGotBatteryStatus(mNativePtr, charging, chargingTime, dischargingTime, level);
|
| - }
|
| - }
|
| - }
|
| -
|
| - /**
|
| - * Native JNI call
|
| - * see device/battery/battery_status_manager_android.cc
|
| - */
|
| - private native void nativeGotBatteryStatus(long nativeBatteryStatusManagerAndroid,
|
| - boolean charging, double chargingTime, double dischargingTime, double level);
|
| }
|
|
|