Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit df83cf3c authored by Lei Yu's avatar Lei Yu
Browse files

Update the BatteryTipPolicy

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

Change-Id: Ifa91ad93f484dc806dfc176ad5ffd912824f5063
Bug: 76113067
Test: RunSettingsRoboTests
parent b2f089c4
Loading
Loading
Loading
Loading
+11 −1
Original line number Original line Diff line number Diff line
@@ -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);
    }
    }


}
}
+3 −3
Original line number Original line Diff line number Diff line
@@ -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;
            }
            }
        }
        }
+5 −2
Original line number Original line Diff line number Diff line
@@ -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();
    }
    }
}
}
+8 −0
Original line number Original line Diff line number Diff line
@@ -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;