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..0601be1eb49afa5a3cb099f6e5e4f7ec1c4d5b51 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; |
+import org.chromium.mojom.device.BatteryStatus; |
/** |
- * 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(BatteryStatus batteryStatus); |
+ } |
+ |
// 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")); |
+ } |
+ |
+ /** |
+ * 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); |
} |
/** |
- * Start listening for intents |
+ * 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,20 +95,17 @@ class BatteryStatusManager { |
return; |
} |
- boolean present = ignoreBatteryPresentState() |
- ? true : intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false); |
+ 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(new BatteryStatus()); |
return; |
} |
- boolean charging = pluggedStatus != 0; |
- int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); |
- boolean batteryFull = status == BatteryManager.BATTERY_STATUS_FULL; |
- |
int current = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); |
int max = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); |
double level = (double) current / (double) max; |
@@ -114,35 +118,17 @@ class BatteryStatusManager { |
// we could compute it manually based on level delta. |
// TODO(timvolodine): add proper projection for chargingTime, dischargingTime |
// (see crbug.com/401553). |
+ boolean charging = pluggedStatus != 0; |
+ int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); |
+ boolean batteryFull = status == BatteryManager.BATTERY_STATUS_FULL; |
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"); |
- } |
- |
- protected void gotBatteryStatus(boolean charging, double chargingTime, |
- double dischargingTime, double level) { |
- synchronized (mNativePtrLock) { |
- if (mNativePtr != 0) { |
- nativeGotBatteryStatus(mNativePtr, charging, chargingTime, dischargingTime, level); |
- } |
- } |
+ BatteryStatus batteryStatus = new BatteryStatus(); |
+ batteryStatus.charging = charging; |
+ batteryStatus.chargingTime = chargingTime; |
+ batteryStatus.dischargingTime = dischargingTime; |
+ batteryStatus.level = level; |
+ mCallback.onBatteryStatusChanged(batteryStatus); |
} |
- |
- /** |
- * 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); |
} |