Merge "Add showChargingMethod in BatteryInfo" into oc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
5883a40931
@@ -781,8 +781,10 @@
|
|||||||
<!-- Summary shown for color space correction preference when its value is overridden by another preference [CHAR LIMIT=35] -->
|
<!-- Summary shown for color space correction preference when its value is overridden by another preference [CHAR LIMIT=35] -->
|
||||||
<string name="daltonizer_type_overridden">Overridden by <xliff:g id="title" example="Simulate color space">%1$s</xliff:g></string>
|
<string name="daltonizer_type_overridden">Overridden by <xliff:g id="title" example="Simulate color space">%1$s</xliff:g></string>
|
||||||
|
|
||||||
<!-- [CHAR_LIMIT=40] Label for estimated remaining duration of battery charging/discharging -->
|
<!-- [CHAR_LIMIT=40] Label for estimated remaining duration of battery discharging -->
|
||||||
<string name="power_remaining_duration_only">Approx. <xliff:g id="time">%1$s</xliff:g> left</string>
|
<string name="power_remaining_duration_only">About <xliff:g id="time">%1$s</xliff:g> left</string>
|
||||||
|
<!-- [CHAR_LIMIT=40] Label for estimated remaining duration of battery charging -->
|
||||||
|
<string name="power_remaining_charging_duration_only"><xliff:g id="time">%1$s</xliff:g> left until fully charged</string>
|
||||||
|
|
||||||
<!-- [CHAR_LIMIT=40] Short label for estimated remaining duration of battery charging/discharging -->
|
<!-- [CHAR_LIMIT=40] Short label for estimated remaining duration of battery charging/discharging -->
|
||||||
<string name="power_remaining_duration_only_short"><xliff:g id="time">%1$s</xliff:g> left</string>
|
<string name="power_remaining_duration_only_short"><xliff:g id="time">%1$s</xliff:g> left</string>
|
||||||
@@ -804,41 +806,11 @@
|
|||||||
<!-- [CHAR_LIMIT=40] Short label for battery level chart when charging with duration -->
|
<!-- [CHAR_LIMIT=40] Short label for battery level chart when charging with duration -->
|
||||||
<string name="power_charging_duration_short"><xliff:g id="level">%1$s</xliff:g> -
|
<string name="power_charging_duration_short"><xliff:g id="level">%1$s</xliff:g> -
|
||||||
<xliff:g id="time">%2$s</xliff:g></string>
|
<xliff:g id="time">%2$s</xliff:g></string>
|
||||||
<!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
|
|
||||||
<string name="power_charging_duration_ac"><xliff:g id="level">%1$s</xliff:g> -
|
|
||||||
<xliff:g id="time">%2$s</xliff:g> until fully charged on AC</string>
|
|
||||||
<!-- [CHAR_LIMIT=40] Short label for battery level chart when charging with duration -->
|
|
||||||
<string name="power_charging_duration_ac_short"><xliff:g id="level">%1$s</xliff:g> -
|
|
||||||
<xliff:g id="time">%2$s</xliff:g></string>
|
|
||||||
<!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
|
|
||||||
<string name="power_charging_duration_usb"><xliff:g id="level">%1$s</xliff:g> -
|
|
||||||
<xliff:g id="time">%2$s</xliff:g> until fully charged over USB</string>
|
|
||||||
<!-- [CHAR_LIMIT=40] Short label for battery level chart when charging with duration -->
|
|
||||||
<string name="power_charging_duration_usb_short"><xliff:g id="level">%1$s</xliff:g> -
|
|
||||||
<xliff:g id="time">%2$s</xliff:g></string>
|
|
||||||
<!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
|
|
||||||
<string name="power_charging_duration_wireless"><xliff:g id="level">%1$s</xliff:g> -
|
|
||||||
<xliff:g id="time">%2$s</xliff:g> until fully charged from wireless</string>
|
|
||||||
<!-- [CHAR_LIMIT=40] Label for battery level chart when charging with duration -->
|
|
||||||
<string name="power_charging_duration_wireless_short"><xliff:g id="level">%1$s</xliff:g> -
|
|
||||||
<xliff:g id="time">%2$s</xliff:g></string>
|
|
||||||
|
|
||||||
<!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed -->
|
<!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed -->
|
||||||
<string name="battery_info_status_unknown">Unknown</string>
|
<string name="battery_info_status_unknown">Unknown</string>
|
||||||
<!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging from an unknown source. -->
|
<!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging from an unknown source. -->
|
||||||
<string name="battery_info_status_charging">Charging</string>
|
<string name="battery_info_status_charging">Charging</string>
|
||||||
<!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging on AC. -->
|
|
||||||
<string name="battery_info_status_charging_ac">Charging on AC</string>
|
|
||||||
<!-- [CHAR_LIMIT=20] Battery short status label when charing on AC -->
|
|
||||||
<string name="battery_info_status_charging_ac_short">Charging</string>
|
|
||||||
<!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging over USB. -->
|
|
||||||
<string name="battery_info_status_charging_usb">Charging over USB</string>
|
|
||||||
<!-- [CHAR_LIMIT=20] Battery short status label when charging over USB. -->
|
|
||||||
<string name="battery_info_status_charging_usb_short">Charging</string>
|
|
||||||
<!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging over a wireless connection. -->
|
|
||||||
<string name="battery_info_status_charging_wireless">Charging wirelessly</string>
|
|
||||||
<!-- [CHAR_LIMIT=20] Battery short status label when charging wirelessly. -->
|
|
||||||
<string name="battery_info_status_charging_wireless_short">Charging</string>
|
|
||||||
<!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed -->
|
<!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed -->
|
||||||
<string name="battery_info_status_discharging">Not charging</string>
|
<string name="battery_info_status_discharging">Not charging</string>
|
||||||
<!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed -->
|
<!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed -->
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ public class BatteryInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void getBatteryInfo(final Context context, final Callback callback) {
|
public static void getBatteryInfo(final Context context, final Callback callback) {
|
||||||
BatteryInfo.getBatteryInfo(context, callback, false);
|
BatteryInfo.getBatteryInfo(context, callback, false /* shortString */);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void getBatteryInfo(final Context context, final Callback callback,
|
public static void getBatteryInfo(final Context context, final Callback callback,
|
||||||
@@ -115,8 +115,8 @@ public class BatteryInfo {
|
|||||||
final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
|
final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
|
||||||
Intent batteryBroadcast = context.registerReceiver(null,
|
Intent batteryBroadcast = context.registerReceiver(null,
|
||||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||||
BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(context,
|
BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(context, batteryBroadcast,
|
||||||
batteryBroadcast, batteryStats, elapsedRealtimeUs, shortString);
|
batteryStats, elapsedRealtimeUs, shortString);
|
||||||
callback.onBatteryInfoLoaded(batteryInfo);
|
callback.onBatteryInfoLoaded(batteryInfo);
|
||||||
}
|
}
|
||||||
}.execute();
|
}.execute();
|
||||||
@@ -125,7 +125,7 @@ public class BatteryInfo {
|
|||||||
public static BatteryInfo getBatteryInfo(Context context, Intent batteryBroadcast,
|
public static BatteryInfo getBatteryInfo(Context context, Intent batteryBroadcast,
|
||||||
BatteryStats stats, long elapsedRealtimeUs) {
|
BatteryStats stats, long elapsedRealtimeUs) {
|
||||||
return BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats, elapsedRealtimeUs,
|
return BatteryInfo.getBatteryInfo(context, batteryBroadcast, stats, elapsedRealtimeUs,
|
||||||
false);
|
false /* shortString */);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BatteryInfo getBatteryInfo(Context context, Intent batteryBroadcast,
|
public static BatteryInfo getBatteryInfo(Context context, Intent batteryBroadcast,
|
||||||
@@ -136,7 +136,7 @@ public class BatteryInfo {
|
|||||||
info.batteryPercentString = Utils.formatPercentage(info.mBatteryLevel);
|
info.batteryPercentString = Utils.formatPercentage(info.mBatteryLevel);
|
||||||
info.mCharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
|
info.mCharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
|
||||||
final Resources resources = context.getResources();
|
final Resources resources = context.getResources();
|
||||||
info.statusLabel = Utils.getBatteryStatus(resources, batteryBroadcast, shortString);
|
info.statusLabel = Utils.getBatteryStatus(resources, batteryBroadcast);
|
||||||
if (!info.mCharging) {
|
if (!info.mCharging) {
|
||||||
final long drainTime = stats.computeBatteryTimeRemaining(elapsedRealtimeUs);
|
final long drainTime = stats.computeBatteryTimeRemaining(elapsedRealtimeUs);
|
||||||
if (drainTime > 0) {
|
if (drainTime > 0) {
|
||||||
@@ -164,25 +164,12 @@ public class BatteryInfo {
|
|||||||
info.remainingTimeUs = chargeTime;
|
info.remainingTimeUs = chargeTime;
|
||||||
String timeString = Formatter.formatShortElapsedTime(context,
|
String timeString = Formatter.formatShortElapsedTime(context,
|
||||||
chargeTime / 1000);
|
chargeTime / 1000);
|
||||||
int plugType = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
|
int resId = shortString ? R.string.power_charging_duration_short
|
||||||
int resId;
|
: R.string.power_charging_duration;
|
||||||
if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {
|
info.remainingLabel = resources.getString(
|
||||||
resId = shortString ? R.string.power_charging_duration_ac_short
|
R.string.power_remaining_charging_duration_only, timeString);
|
||||||
: R.string.power_charging_duration_ac;
|
info.mChargeLabelString = resources.getString(resId, info.batteryPercentString,
|
||||||
} else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
|
|
||||||
resId = shortString ? R.string.power_charging_duration_usb_short
|
|
||||||
: R.string.power_charging_duration_usb;
|
|
||||||
} else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
|
|
||||||
resId = shortString ? R.string.power_charging_duration_wireless_short
|
|
||||||
: R.string.power_charging_duration_wireless;
|
|
||||||
} else {
|
|
||||||
resId = shortString ? R.string.power_charging_duration_short
|
|
||||||
: R.string.power_charging_duration;
|
|
||||||
}
|
|
||||||
info.remainingLabel = resources.getString(R.string.power_remaining_duration_only,
|
|
||||||
timeString);
|
timeString);
|
||||||
info.mChargeLabelString = resources.getString(
|
|
||||||
resId, info.batteryPercentString, timeString);
|
|
||||||
} else {
|
} else {
|
||||||
info.remainingLabel = null;
|
info.remainingLabel = null;
|
||||||
info.mChargeLabelString = resources.getString(
|
info.mChargeLabelString = resources.getString(
|
||||||
|
|||||||
@@ -140,30 +140,11 @@ public class Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
|
public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) {
|
||||||
return Utils.getBatteryStatus(res, batteryChangedIntent, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getBatteryStatus(Resources res, Intent batteryChangedIntent,
|
|
||||||
boolean shortString) {
|
|
||||||
int plugType = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
|
|
||||||
int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
|
int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
|
||||||
BatteryManager.BATTERY_STATUS_UNKNOWN);
|
BatteryManager.BATTERY_STATUS_UNKNOWN);
|
||||||
String statusString;
|
String statusString;
|
||||||
if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
|
if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
|
||||||
int resId;
|
statusString = res.getString(R.string.battery_info_status_charging);
|
||||||
if (plugType == BatteryManager.BATTERY_PLUGGED_AC) {
|
|
||||||
resId = shortString ? R.string.battery_info_status_charging_ac_short
|
|
||||||
: R.string.battery_info_status_charging_ac;
|
|
||||||
} else if (plugType == BatteryManager.BATTERY_PLUGGED_USB) {
|
|
||||||
resId = shortString ? R.string.battery_info_status_charging_usb_short
|
|
||||||
: R.string.battery_info_status_charging_usb;
|
|
||||||
} else if (plugType == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
|
|
||||||
resId = shortString ? R.string.battery_info_status_charging_wireless_short
|
|
||||||
: R.string.battery_info_status_charging_wireless;
|
|
||||||
} else {
|
|
||||||
resId = R.string.battery_info_status_charging;
|
|
||||||
}
|
|
||||||
statusString = res.getString(resId);
|
|
||||||
} else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
|
} else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
|
||||||
statusString = res.getString(R.string.battery_info_status_discharging);
|
statusString = res.getString(R.string.battery_info_status_discharging);
|
||||||
} else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
|
} else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import android.content.Intent;
|
|||||||
import android.os.BatteryManager;
|
import android.os.BatteryManager;
|
||||||
import android.os.BatteryStats;
|
import android.os.BatteryStats;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -31,14 +32,23 @@ import org.robolectric.RobolectricTestRunner;
|
|||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.anyLong;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
public class BatteryInfoTest {
|
public class BatteryInfoTest {
|
||||||
private static final String STATUS_FULL = "Full";
|
private static final String STATUS_FULL = "Full";
|
||||||
private Intent mBatteryBroadcast;
|
private static final String STATUS_CHARGING_NO_TIME = "Charging";
|
||||||
@Mock
|
private static final String STATUS_CHARGING_TIME = "Charging - 2h left";
|
||||||
|
private static final long REMAINING_TIME_NULL = -1;
|
||||||
|
private static final long REMAINING_TIME = 2;
|
||||||
|
private Intent mDisChargingBatteryBroadcast;
|
||||||
|
private Intent mChargingBatteryBroadcast;
|
||||||
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private BatteryStats mBatteryStats;
|
private BatteryStats mBatteryStats;
|
||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
@@ -47,21 +57,53 @@ public class BatteryInfoTest {
|
|||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
mBatteryBroadcast = new Intent();
|
mDisChargingBatteryBroadcast = new Intent();
|
||||||
mBatteryBroadcast.putExtra(BatteryManager.EXTRA_PLUGGED, 0);
|
mDisChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_PLUGGED, 0);
|
||||||
mBatteryBroadcast.putExtra(BatteryManager.EXTRA_LEVEL, 0);
|
mDisChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_LEVEL, 0);
|
||||||
mBatteryBroadcast.putExtra(BatteryManager.EXTRA_SCALE, 100);
|
mDisChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_SCALE, 100);
|
||||||
mBatteryBroadcast.putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_FULL);
|
mDisChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_STATUS,
|
||||||
|
BatteryManager.BATTERY_STATUS_FULL);
|
||||||
|
|
||||||
|
mChargingBatteryBroadcast = new Intent();
|
||||||
|
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_PLUGGED,
|
||||||
|
BatteryManager.BATTERY_PLUGGED_AC);
|
||||||
|
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_LEVEL, 50);
|
||||||
|
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_SCALE, 100);
|
||||||
|
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_STATUS,
|
||||||
|
BatteryManager.BATTERY_STATUS_UNKNOWN);
|
||||||
|
|
||||||
when(mContext.getResources().getString(R.string.battery_info_status_full))
|
when(mContext.getResources().getString(R.string.battery_info_status_full))
|
||||||
.thenReturn(STATUS_FULL);
|
.thenReturn(STATUS_FULL);
|
||||||
|
when(mContext.getResources().getString(eq(R.string.power_charging), any(),
|
||||||
|
any())).thenReturn(STATUS_CHARGING_NO_TIME);
|
||||||
|
when(mContext.getResources().getString(eq(R.string.power_charging_duration), any(),
|
||||||
|
any())).thenReturn(STATUS_CHARGING_TIME);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetBatteryInfo_HasStatusLabel() {
|
public void testGetBatteryInfo_hasStatusLabel() {
|
||||||
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mBatteryBroadcast, mBatteryStats,
|
doReturn(REMAINING_TIME_NULL).when(mBatteryStats).computeBatteryTimeRemaining(anyLong());
|
||||||
SystemClock.elapsedRealtime() * 1000, true);
|
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mDisChargingBatteryBroadcast,
|
||||||
|
mBatteryStats, SystemClock.elapsedRealtime() * 1000, true);
|
||||||
|
|
||||||
assertThat(info.statusLabel).isEqualTo(STATUS_FULL);
|
assertThat(info.statusLabel).isEqualTo(STATUS_FULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBatteryInfo_doNotShowChargingMethod_hasRemainingTime() {
|
||||||
|
doReturn(REMAINING_TIME).when(mBatteryStats).computeChargeTimeRemaining(anyLong());
|
||||||
|
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mChargingBatteryBroadcast,
|
||||||
|
mBatteryStats, SystemClock.elapsedRealtime() * 1000, false);
|
||||||
|
|
||||||
|
assertThat(info.mChargeLabelString).isEqualTo(STATUS_CHARGING_TIME);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetBatteryInfo_doNotShowChargingMethod_noRemainingTime() {
|
||||||
|
doReturn(REMAINING_TIME_NULL).when(mBatteryStats).computeChargeTimeRemaining(anyLong());
|
||||||
|
BatteryInfo info = BatteryInfo.getBatteryInfo(mContext, mChargingBatteryBroadcast,
|
||||||
|
mBatteryStats, SystemClock.elapsedRealtime() * 1000, false);
|
||||||
|
|
||||||
|
assertThat(info.mChargeLabelString).isEqualTo(STATUS_CHARGING_NO_TIME);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user