Update the BatteryTipPolicy

1. Add "testLowBatteryTip"
2. Update default value for low battery threshold

Change-Id: Ifa91ad93f484dc806dfc176ad5ffd912824f5063
Bug: 76113067
Test: RunSettingsRoboTests
This commit is contained in:
Lei Yu
2018-04-20 15:43:45 -07:00
parent b2f089c468
commit df83cf3c29
4 changed files with 27 additions and 6 deletions

View File

@@ -50,6 +50,7 @@ public class BatteryTipPolicy {
private static final String KEY_TEST_BATTERY_SAVER_TIP = "test_battery_saver_tip"; private static final String KEY_TEST_BATTERY_SAVER_TIP = "test_battery_saver_tip";
private static final String KEY_TEST_HIGH_USAGE_TIP = "test_high_usage_tip"; private static final String KEY_TEST_HIGH_USAGE_TIP = "test_high_usage_tip";
private static final String KEY_TEST_SMART_BATTERY_TIP = "test_smart_battery_tip"; private static final String KEY_TEST_SMART_BATTERY_TIP = "test_smart_battery_tip";
private static final String KEY_TEST_LOW_BATTERY_TIP = "test_low_battery_tip";
/** /**
* {@code true} if general battery tip is enabled * {@code true} if general battery tip is enabled
@@ -192,6 +193,14 @@ public class BatteryTipPolicy {
*/ */
public final boolean testSmartBatteryTip; public final boolean testSmartBatteryTip;
/**
* {@code true} if we want to test low battery tip.
*
* @see Settings.Global#BATTERY_TIP_CONSTANTS
* @see #KEY_TEST_LOW_BATTERY_TIP
*/
public final boolean testLowBatteryTip;
private final KeyValueListParser mParser; private final KeyValueListParser mParser;
public BatteryTipPolicy(Context context) { public BatteryTipPolicy(Context context) {
@@ -222,13 +231,14 @@ public class BatteryTipPolicy {
reducedBatteryEnabled = mParser.getBoolean(KEY_REDUCED_BATTERY_ENABLED, false); reducedBatteryEnabled = mParser.getBoolean(KEY_REDUCED_BATTERY_ENABLED, false);
reducedBatteryPercent = mParser.getInt(KEY_REDUCED_BATTERY_PERCENT, 50); reducedBatteryPercent = mParser.getInt(KEY_REDUCED_BATTERY_PERCENT, 50);
lowBatteryEnabled = mParser.getBoolean(KEY_LOW_BATTERY_ENABLED, false); lowBatteryEnabled = mParser.getBoolean(KEY_LOW_BATTERY_ENABLED, false);
lowBatteryHour = mParser.getInt(KEY_LOW_BATTERY_HOUR, 16); lowBatteryHour = mParser.getInt(KEY_LOW_BATTERY_HOUR, 3);
dataHistoryRetainDay = mParser.getInt(KEY_DATA_HISTORY_RETAIN_DAY, 30); dataHistoryRetainDay = mParser.getInt(KEY_DATA_HISTORY_RETAIN_DAY, 30);
excessiveBgDrainPercentage = mParser.getInt(KEY_EXCESSIVE_BG_DRAIN_PERCENTAGE, 10); excessiveBgDrainPercentage = mParser.getInt(KEY_EXCESSIVE_BG_DRAIN_PERCENTAGE, 10);
testBatterySaverTip = mParser.getBoolean(KEY_TEST_BATTERY_SAVER_TIP, false); testBatterySaverTip = mParser.getBoolean(KEY_TEST_BATTERY_SAVER_TIP, false);
testHighUsageTip = mParser.getBoolean(KEY_TEST_HIGH_USAGE_TIP, false); testHighUsageTip = mParser.getBoolean(KEY_TEST_HIGH_USAGE_TIP, false);
testSmartBatteryTip = mParser.getBoolean(KEY_TEST_SMART_BATTERY_TIP, false); testSmartBatteryTip = mParser.getBoolean(KEY_TEST_SMART_BATTERY_TIP, false);
testLowBatteryTip = mParser.getBoolean(KEY_TEST_LOW_BATTERY_TIP, false);
} }
} }

View File

@@ -46,17 +46,17 @@ public class LowBatteryDetector implements BatteryTipDetector {
@Override @Override
public BatteryTip detect() { public BatteryTip detect() {
final boolean powerSaveModeOn = mPowerManager.isPowerSaveMode(); final boolean powerSaveModeOn = mPowerManager.isPowerSaveMode();
//TODO(jackqdyulei): hook up this 3 hours to server side
final boolean lowBattery = mBatteryInfo.batteryLevel <= mWarningLevel final boolean lowBattery = mBatteryInfo.batteryLevel <= mWarningLevel
|| (mBatteryInfo.discharging || (mBatteryInfo.discharging
&& mBatteryInfo.remainingTimeUs < TimeUnit.HOURS.toMicros(3)); && mBatteryInfo.remainingTimeUs < TimeUnit.HOURS.toMicros(mPolicy.lowBatteryHour));
int state = BatteryTip.StateType.INVISIBLE; int state = BatteryTip.StateType.INVISIBLE;
if (mPolicy.lowBatteryEnabled) { if (mPolicy.lowBatteryEnabled) {
if (powerSaveModeOn) { if (powerSaveModeOn) {
// Show it is handled if battery saver is on // Show it is handled if battery saver is on
state = BatteryTip.StateType.HANDLED; state = BatteryTip.StateType.HANDLED;
} else if (mBatteryInfo.discharging && lowBattery) { } else if (mPolicy.testLowBatteryTip || (mBatteryInfo.discharging && lowBattery)) {
// Show it is new if in test or in discharging low battery state
state = BatteryTip.StateType.NEW; state = BatteryTip.StateType.NEW;
} }
} }

View File

@@ -48,7 +48,8 @@ public class BatteryTipPolicyTest {
+ ",excessive_bg_drain_percentage=25" + ",excessive_bg_drain_percentage=25"
+ ",test_battery_saver_tip=true" + ",test_battery_saver_tip=true"
+ ",test_high_usage_tip=false" + ",test_high_usage_tip=false"
+ ",test_smart_battery_tip=true"; + ",test_smart_battery_tip=true"
+ ",test_low_battery_tip=true";
private Context mContext; private Context mContext;
@Before @Before
@@ -80,6 +81,7 @@ public class BatteryTipPolicyTest {
assertThat(batteryTipPolicy.testBatterySaverTip).isTrue(); assertThat(batteryTipPolicy.testBatterySaverTip).isTrue();
assertThat(batteryTipPolicy.testHighUsageTip).isFalse(); assertThat(batteryTipPolicy.testHighUsageTip).isFalse();
assertThat(batteryTipPolicy.testSmartBatteryTip).isTrue(); assertThat(batteryTipPolicy.testSmartBatteryTip).isTrue();
assertThat(batteryTipPolicy.testLowBatteryTip).isTrue();
} }
@Test @Test
@@ -100,11 +102,12 @@ public class BatteryTipPolicyTest {
assertThat(batteryTipPolicy.reducedBatteryEnabled).isFalse(); assertThat(batteryTipPolicy.reducedBatteryEnabled).isFalse();
assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(50); assertThat(batteryTipPolicy.reducedBatteryPercent).isEqualTo(50);
assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse(); assertThat(batteryTipPolicy.lowBatteryEnabled).isFalse();
assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(16); assertThat(batteryTipPolicy.lowBatteryHour).isEqualTo(3);
assertThat(batteryTipPolicy.dataHistoryRetainDay).isEqualTo(30); assertThat(batteryTipPolicy.dataHistoryRetainDay).isEqualTo(30);
assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(10); assertThat(batteryTipPolicy.excessiveBgDrainPercentage).isEqualTo(10);
assertThat(batteryTipPolicy.testBatterySaverTip).isFalse(); assertThat(batteryTipPolicy.testBatterySaverTip).isFalse();
assertThat(batteryTipPolicy.testHighUsageTip).isFalse(); assertThat(batteryTipPolicy.testHighUsageTip).isFalse();
assertThat(batteryTipPolicy.testSmartBatteryTip).isFalse(); assertThat(batteryTipPolicy.testSmartBatteryTip).isFalse();
assertThat(batteryTipPolicy.testLowBatteryTip).isFalse();
} }
} }

View File

@@ -58,6 +58,7 @@ public class LowBatteryDetectorTest {
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mShadowPowerManager = Shadows.shadowOf(mContext.getSystemService(PowerManager.class)); mShadowPowerManager = Shadows.shadowOf(mContext.getSystemService(PowerManager.class));
ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", true); ReflectionHelpers.setField(mPolicy, "lowBatteryEnabled", true);
ReflectionHelpers.setField(mPolicy, "lowBatteryHour", 3);
mBatteryInfo.discharging = true; mBatteryInfo.discharging = true;
mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo); mLowBatteryDetector = new LowBatteryDetector(mContext, mPolicy, mBatteryInfo);
@@ -71,6 +72,13 @@ public class LowBatteryDetectorTest {
assertThat(mLowBatteryDetector.detect().isVisible()).isFalse(); assertThat(mLowBatteryDetector.detect().isVisible()).isFalse();
} }
@Test
public void testDetect_enabledByTest_tipNew() {
ReflectionHelpers.setField(mPolicy, "testLowBatteryTip", true);
assertThat(mLowBatteryDetector.detect().getState()).isEqualTo(BatteryTip.StateType.NEW);
}
@Test @Test
public void testDetect_lowBattery_tipNew() { public void testDetect_lowBattery_tipNew() {
mBatteryInfo.batteryLevel = 3; mBatteryInfo.batteryLevel = 3;