From 926f071688d91135575fbfc708e4bb884c86e780 Mon Sep 17 00:00:00 2001 From: Salvador Martinez Date: Tue, 3 Jul 2018 18:07:07 -0700 Subject: 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 --- .../src/com/android/systemui/power/PowerUI.java | 14 ++++++--- .../com/android/systemui/power/PowerUITest.java | 34 ++++++++++++++++++++-- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/power/PowerUI.java b/packages/SystemUI/src/com/android/systemui/power/PowerUI.java index 6801e6917d3d..9a648d17c4ff 100644 --- a/packages/SystemUI/src/com/android/systemui/power/PowerUI.java +++ b/packages/SystemUI/src/com/android/systemui/power/PowerUI.java @@ -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) { diff --git a/packages/SystemUI/tests/src/com/android/systemui/power/PowerUITest.java b/packages/SystemUI/tests/src/com/android/systemui/power/PowerUITest.java index d19715d5c5cc..5ecf0c04aeb1 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/power/PowerUITest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/power/PowerUITest.java @@ -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 }); -- cgit v1.2.3-59-g8ed1b