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

Commit 926f0716 authored by Salvador Martinez's avatar Salvador Martinez
Browse files

Only query for estimate on battery level change

SysUI is querying for an estimate too much, this change gates
it on being the first estimate or the battery level having
changed so that we don't waste system resources.

Test: atest PowerUITest
Bug: 110259498
Change-Id: I6fb5c08d6388e463d6216de41b3fd49bbc489b81
parent a14e1b39
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -77,6 +77,7 @@ public class PowerUI extends SystemUI {
    private int mPlugType = 0;
    private int mInvalidCharger = 0;
    private EnhancedEstimates mEnhancedEstimates;
    private Estimate mLastEstimate;
    private boolean mLowWarningShownThisChargeCycle;
    private boolean mSevereWarningShownThisChargeCycle;

@@ -247,7 +248,8 @@ public class PowerUI extends SystemUI {

                // Show the correct version of low battery warning if needed
                ThreadUtils.postOnBackgroundThread(() -> {
                    maybeShowBatteryWarning(plugged, oldPlugged, oldBucket, bucket);
                    maybeShowBatteryWarning(
                            oldBatteryLevel, plugged, oldPlugged, oldBucket, bucket);
                });

            } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
@@ -262,14 +264,18 @@ public class PowerUI extends SystemUI {
        }
    }

    protected void maybeShowBatteryWarning(boolean plugged, boolean oldPlugged, int oldBucket,
        int bucket) {
    protected void maybeShowBatteryWarning(int oldBatteryLevel, boolean plugged, boolean oldPlugged,
            int oldBucket, int bucket) {
        boolean isPowerSaver = mPowerManager.isPowerSaveMode();
        // only play SFX when the dialog comes up or the bucket changes
        final boolean playSound = bucket != oldBucket || oldPlugged;
        final boolean hybridEnabled = mEnhancedEstimates.isHybridNotificationEnabled();
        if (hybridEnabled) {
            final Estimate estimate = mEnhancedEstimates.getEstimate();
            Estimate estimate = mLastEstimate;
            if (estimate == null || mBatteryLevel != oldBatteryLevel) {
                estimate = mEnhancedEstimates.getEstimate();
                mLastEstimate = estimate;
            }
            // Turbo is not always booted once SysUI is running so we have ot make sure we actually
            // get data back
            if (estimate != null) {
+32 −2
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import static junit.framework.Assert.assertTrue;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@@ -66,6 +67,8 @@ public class PowerUITest extends SysuiTestCase {
    public static final long BELOW_HYBRID_THRESHOLD = TimeUnit.HOURS.toMillis(2);
    public static final long ABOVE_HYBRID_THRESHOLD = TimeUnit.HOURS.toMillis(4);
    private static final long ABOVE_CHARGE_CYCLE_THRESHOLD = Duration.ofHours(8).toMillis();
    private static final int OLD_BATTERY_LEVEL_NINE = 9;
    private static final int OLD_BATTERY_LEVEL_10 = 10;
    private HardwarePropertiesManager mHardProps;
    private WarningsUI mMockWarnings;
    private PowerUI mPowerUI;
@@ -307,8 +310,8 @@ public class PowerUITest extends SysuiTestCase {
                .thenReturn(new Estimate(BELOW_HYBRID_THRESHOLD, true));
        mPowerUI.mBatteryStatus = BatteryManager.BATTERY_HEALTH_GOOD;

        mPowerUI.maybeShowBatteryWarning(UNPLUGGED, UNPLUGGED, ABOVE_WARNING_BUCKET,
                ABOVE_WARNING_BUCKET);
        mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_NINE, UNPLUGGED, UNPLUGGED,
                ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET);

        // reduce battery level to handle time based trigger -> level trigger interactions
        mPowerUI.mBatteryLevel = 10;
@@ -449,6 +452,33 @@ public class PowerUITest extends SysuiTestCase {
        verify(mMockWarnings, never()).dismissLowBatteryWarning();
    }

    @Test
    public void testMaybeShowBatteryWarning_onlyQueriesEstimateOnBatteryLevelChangeOrNull() {
        mPowerUI.start();
        Estimate estimate = new Estimate(BELOW_HYBRID_THRESHOLD, true);
        when(mEnhancedEstimates.isHybridNotificationEnabled()).thenReturn(true);
        when(mEnhancedEstimates.getLowWarningThreshold()).thenReturn(PowerUI.THREE_HOURS_IN_MILLIS);
        when(mEnhancedEstimates.getSevereWarningThreshold()).thenReturn(ONE_HOUR_MILLIS);
        when(mEnhancedEstimates.getEstimate()).thenReturn(estimate);
        mPowerUI.mBatteryStatus = BatteryManager.BATTERY_HEALTH_GOOD;

        // we expect that the first time it will query even if the level is the same
        mPowerUI.mBatteryLevel = 9;
        mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_NINE, UNPLUGGED, UNPLUGGED,
                ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET);
        verify(mEnhancedEstimates, times(1)).getEstimate();

        // We should NOT query again if the battery level hasn't changed
        mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_NINE, UNPLUGGED, UNPLUGGED,
                ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET);
        verify(mEnhancedEstimates, times(1)).getEstimate();

        // Battery level has changed, so we should query again
        mPowerUI.maybeShowBatteryWarning(OLD_BATTERY_LEVEL_10, UNPLUGGED, UNPLUGGED,
                ABOVE_WARNING_BUCKET, ABOVE_WARNING_BUCKET);
        verify(mEnhancedEstimates, times(2)).getEstimate();
    }

    private void setCurrentTemp(float temp) {
        when(mHardProps.getDeviceTemperatures(DEVICE_TEMPERATURE_SKIN, TEMPERATURE_CURRENT))
                .thenReturn(new float[] { temp });