Send current battery present state on add callback

BatteryControllerImpl won't send updates to the battery unknown state if
it hasn't changed, but BatteryMeterView can get reinflated on theme
change. This change makes sure we send the current state when callbacks
are added.

Test: atest BatteryControllerTest
Bug: 174959955
Change-Id: I30b4a7b7f55a1f26b6451f23721be3a709ddf894
This commit is contained in:
Evan Laird
2020-12-09 16:10:30 -05:00
parent 8e3ec4343d
commit 89f2b74245
2 changed files with 40 additions and 0 deletions

View File

@@ -155,8 +155,11 @@ public class BatteryControllerImpl extends BroadcastReceiver implements BatteryC
mChangeCallbacks.add(cb);
}
if (!mHasReceivedBattery) return;
// Make sure new callbacks get the correct initial state
cb.onBatteryLevelChanged(mLevel, mPluggedIn, mCharging);
cb.onPowerSaveChanged(mPowerSave);
cb.onBatteryUnknownStateChanged(mStateUnknown);
}
@Override

View File

@@ -16,7 +16,11 @@
package com.android.systemui.statusbar.policy;
import static android.os.BatteryManager.EXTRA_PRESENT;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Intent;
@@ -31,6 +35,7 @@ import com.android.systemui.SysuiTestCase;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.demomode.DemoModeController;
import com.android.systemui.power.EnhancedEstimates;
import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
import org.junit.Assert;
import org.junit.Before;
@@ -98,4 +103,36 @@ public class BatteryControllerTest extends SysuiTestCase {
Assert.assertFalse(mBatteryController.isAodPowerSave());
}
@Test
public void testBatteryPresentState_notPresent() {
// GIVEN a battery state callback listening for changes
BatteryStateChangeCallback cb = mock(BatteryStateChangeCallback.class);
mBatteryController.addCallback(cb);
// WHEN the state of the battery becomes unknown
Intent i = new Intent(Intent.ACTION_BATTERY_CHANGED);
i.putExtra(EXTRA_PRESENT, false);
mBatteryController.onReceive(getContext(), i);
// THEN the callback is notified
verify(cb, atLeastOnce()).onBatteryUnknownStateChanged(true);
}
@Test
public void testBatteryPresentState_callbackAddedAfterStateChange() {
// GIVEN a battery state callback
BatteryController.BatteryStateChangeCallback cb =
mock(BatteryController.BatteryStateChangeCallback.class);
// GIVEN the state has changed before adding a new callback
Intent i = new Intent(Intent.ACTION_BATTERY_CHANGED);
i.putExtra(EXTRA_PRESENT, false);
mBatteryController.onReceive(getContext(), i);
// WHEN a callback is added
mBatteryController.addCallback(cb);
// THEN it is informed about the battery state
verify(cb, atLeastOnce()).onBatteryUnknownStateChanged(true);
}
}