[Android] Battery Status API: some tweaks to the BatteryStatusManager.java.

Make sure battery status works with proper default values
on Android. Add an exception for battery present check for
Galaxy Nexus because it wrongly reports that the battery
is not present (see http://crbug.com/384348).

BUG=122593, 384348, 360068
TEST=http://jsbin.com/battery-status-diagnostics (manual)

Review URL: https://codereview.chromium.org/330333003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278829 0039d316-1c4b-4281-b951-d872f2087c98
parent 7a1da46c
......@@ -9,6 +9,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;
import android.util.Log;
import com.google.common.annotations.VisibleForTesting;
......@@ -88,24 +89,44 @@ class BatteryStatusManager {
return;
}
boolean present = ignoreBatteryPresentState() ?
true : intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false);
if (!present) {
// No battery, return default values.
gotBatteryStatus(true, 0, Double.POSITIVE_INFINITY, 1);
return;
}
int current = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int max = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
double level = (double)current / (double)max;
if (level < 0 || level > 1) {
// Sanity check, assume default value in this case.
level = 1.0;
}
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
// by default assume a battery is present
boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
boolean charging = (present && status == BatteryManager.BATTERY_STATUS_DISCHARGING)
? false : true;
//TODO(timvolodine) : add proper projection for chargingTime, dischargingTime.
double chargingTime = (!present || status == BatteryManager.BATTERY_STATUS_FULL)
? 0 : Double.POSITIVE_INFINITY;
boolean charging = !(status == BatteryManager.BATTERY_STATUS_DISCHARGING);
// TODO(timvolodine) : add proper projection for chargingTime, dischargingTime.
double chargingTime = (status == BatteryManager.BATTERY_STATUS_FULL) ?
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) {
......
......@@ -28,6 +28,7 @@ public class BatteryStatusManagerTest extends AndroidTestCase {
@SmallTest
public void testOnReceiveBatteryDischarging() {
Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
intent.putExtra(BatteryManager.EXTRA_PRESENT, true);
intent.putExtra(BatteryManager.EXTRA_LEVEL, 10);
intent.putExtra(BatteryManager.EXTRA_SCALE, 100);
intent.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_DISCHARGING);
......@@ -42,6 +43,7 @@ public class BatteryStatusManagerTest extends AndroidTestCase {
@SmallTest
public void testOnReceiveBatteryCharging() {
Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
intent.putExtra(BatteryManager.EXTRA_PRESENT, true);
intent.putExtra(BatteryManager.EXTRA_LEVEL, 50);
intent.putExtra(BatteryManager.EXTRA_SCALE, 100);
intent.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING);
......@@ -56,6 +58,7 @@ public class BatteryStatusManagerTest extends AndroidTestCase {
@SmallTest
public void testOnReceiveBatteryFull() {
Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
intent.putExtra(BatteryManager.EXTRA_PRESENT, true);
intent.putExtra(BatteryManager.EXTRA_LEVEL, 100);
intent.putExtra(BatteryManager.EXTRA_SCALE, 100);
intent.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_FULL);
......@@ -113,6 +116,11 @@ public class BatteryStatusManagerTest extends AndroidTestCase {
assertEquals(mCalls, names);
}
@Override
protected boolean ignoreBatteryPresentState() {
return false;
}
@Override
protected void gotBatteryStatus(boolean charging, double chargingTime,
double dischargingTime, double level) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment