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

Commit 9b26e51d authored by mayankkk's avatar mayankkk Committed by Mayank Dandwani
Browse files

Don't trigger batterychanged broadcast when ChargeCounter is updated.

- CHARGE_COUNTER is an UnsupportedAppUsage from Android R and it is not
  used internally as well.

BUG: 376706419
Test: atest BatteryServiceTest
Flag: com.android.server.flags.rate_limit_battery_changed_broadcast
Change-Id: I87ad8bfeb76b735b1604c5212781c5da4a86de10
parent 431ae3b8
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -733,6 +733,10 @@ public final class BatteryService extends SystemService {
            mHandler.post(this::notifyChargingPolicyChanged);
        }

        final boolean includeChargeCounter =
                !com.android.server.flags.Flags.rateLimitBatteryChangedBroadcast()
                        && mHealthInfo.batteryChargeCounterUah != mLastBroadcastChargeCounter;

        if (force
                || (mHealthInfo.batteryStatus != mLastBroadcastBatteryStatus
                || mHealthInfo.batteryHealth != mLastBroadcastBatteryHealth
@@ -743,7 +747,7 @@ public final class BatteryService extends SystemService {
                || mHealthInfo.batteryTemperatureTenthsCelsius != mLastBroadcastBatteryTemperature
                || mHealthInfo.maxChargingCurrentMicroamps != mLastBroadcastMaxChargingCurrent
                || mHealthInfo.maxChargingVoltageMicrovolts != mLastBroadcastMaxChargingVoltage
                || mHealthInfo.batteryChargeCounterUah != mLastBroadcastChargeCounter
                || includeChargeCounter
                || mInvalidCharger != mLastBroadcastInvalidCharger
                || mHealthInfo.batteryCycleCount != mLastBroadcastBatteryCycleCount
                || mHealthInfo.chargingState != mLastBroadcastChargingState
@@ -1268,8 +1272,7 @@ public final class BatteryService extends SystemService {
                || mInvalidCharger != mLastBroadcastInvalidCharger
                || mHealthInfo.batteryCycleCount != mLastBroadcastBatteryCycleCount
                || mHealthInfo.chargingState != mLastBroadcastChargingState
                || mHealthInfo.batteryCapacityLevel != mLastBroadcastBatteryCapacityLevel
                || mLastBroadcastChargeCounter != mHealthInfo.batteryChargeCounterUah;
                || mHealthInfo.batteryCapacityLevel != mLastBroadcastBatteryCapacityLevel;

        // We only rate limit based on changes in the temp, voltage.
        if (otherStatesUpdated) {
+56 −11
Original line number Diff line number Diff line
@@ -77,7 +77,9 @@ public class BatteryServiceTest {
    private static final int TEMP_MORE_THEN_ONE_DEGREE_CELSIUS = 310;
    private static final int CURRENT_BATTERY_HEALTH = 2;
    private static final int UPDATED_BATTERY_HEALTH = 3;
    private static final int HANDLER_IDLE_TIME_MS = 1000;
    private static final int CURRENT_CHARGE_COUNTER = 4680000;
    private static final int UPDATED_CHARGE_COUNTER = 4218000;
    private static final int HANDLER_IDLE_TIME_MS = 5000;
    @Rule
    public final ExtendedMockitoRule mExtendedMockitoRule = new ExtendedMockitoRule.Builder(this)
            .mockStatic(SystemProperties.class)
@@ -141,7 +143,7 @@ public class BatteryServiceTest {
    @EnableFlags(Flags.FLAG_RATE_LIMIT_BATTERY_CHANGED_BROADCAST)
    public void onlyVoltageUpdated_lessThenOnePercent_broadcastNotSent() {
        mBatteryService.update(createHealthInfo(VOLTAGE_LESS_THEN_ONE_PERCENT, CURRENT_BATTERY_TEMP,
                CURRENT_BATTERY_HEALTH));
                CURRENT_CHARGE_COUNTER, CURRENT_BATTERY_HEALTH));

        waitForHandlerToExecute();

@@ -153,6 +155,7 @@ public class BatteryServiceTest {
    public void onlyVoltageUpdated_beforeTwentySeconds_broadcastNotSent() {
        mBatteryService.update(
                createHealthInfo(VOLTAGE_MORE_THEN_ONE_PERCENT, CURRENT_BATTERY_TEMP,
                        CURRENT_CHARGE_COUNTER,
                        CURRENT_BATTERY_HEALTH));

        waitForHandlerToExecute();
@@ -165,7 +168,7 @@ public class BatteryServiceTest {
    public void onlyVoltageUpdated_broadcastSent() {
        mBatteryService.mLastBroadcastVoltageUpdateTime = SystemClock.elapsedRealtime() - 20000;
        mBatteryService.update(createHealthInfo(VOLTAGE_MORE_THEN_ONE_PERCENT, CURRENT_BATTERY_TEMP,
                CURRENT_BATTERY_HEALTH));
                CURRENT_CHARGE_COUNTER, CURRENT_BATTERY_HEALTH));

        waitForHandlerToExecute();

@@ -177,7 +180,7 @@ public class BatteryServiceTest {
    public void onlyTempUpdated_lessThenOneDegreeCelsius_broadcastNotSent() {
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, TEMP_LESS_THEN_ONE_DEGREE_CELSIUS,
                        CURRENT_BATTERY_HEALTH));
                        CURRENT_CHARGE_COUNTER, CURRENT_BATTERY_HEALTH));

        waitForHandlerToExecute();

@@ -190,7 +193,7 @@ public class BatteryServiceTest {
        long lastVoltageUpdateTime = mBatteryService.mLastBroadcastVoltageUpdateTime;
        mBatteryService.update(
                createHealthInfo(VOLTAGE_LESS_THEN_ONE_PERCENT, TEMP_MORE_THEN_ONE_DEGREE_CELSIUS,
                        CURRENT_BATTERY_HEALTH));
                        UPDATED_CHARGE_COUNTER, CURRENT_BATTERY_HEALTH));

        waitForHandlerToExecute();

@@ -203,15 +206,17 @@ public class BatteryServiceTest {
    public void batteryHealthUpdated_voltageAndTempConst_broadcastSent() {
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, CURRENT_BATTERY_TEMP,
                        CURRENT_CHARGE_COUNTER,
                        UPDATED_BATTERY_HEALTH));

        waitForHandlerToExecute();

        verifyNumberOfTimesBroadcastSent(1);

        // updating the voltage just after the health update does not triggers the broadcast.
        // updating counter just after the health update does not triggers broadcast.
        mBatteryService.update(
                createHealthInfo(VOLTAGE_MORE_THEN_ONE_PERCENT, CURRENT_BATTERY_TEMP,
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, CURRENT_BATTERY_TEMP,
                        UPDATED_CHARGE_COUNTER,
                        UPDATED_BATTERY_HEALTH));

        waitForHandlerToExecute();
@@ -223,6 +228,45 @@ public class BatteryServiceTest {
    @DisableFlags(Flags.FLAG_RATE_LIMIT_BATTERY_CHANGED_BROADCAST)
    public void voltageUpdated_lessThanOnePercent_flagDisabled_broadcastSent() {
        mBatteryService.update(createHealthInfo(VOLTAGE_LESS_THEN_ONE_PERCENT, CURRENT_BATTERY_TEMP,
                CURRENT_CHARGE_COUNTER, CURRENT_BATTERY_HEALTH));

        waitForHandlerToExecute();

        verifyNumberOfTimesBroadcastSent(1);
    }

    @Test
    @EnableFlags(Flags.FLAG_RATE_LIMIT_BATTERY_CHANGED_BROADCAST)
    public void onlyChargeCounterUpdated_broadcastNotSent() {
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, CURRENT_BATTERY_TEMP,
                        UPDATED_CHARGE_COUNTER,
                        CURRENT_BATTERY_HEALTH));

        waitForHandlerToExecute();

        verifyNumberOfTimesBroadcastSent(0);
    }

    @Test
    @EnableFlags(Flags.FLAG_RATE_LIMIT_BATTERY_CHANGED_BROADCAST)
    public void chargeCounterUpdated_tempUpdatedLessThanOneDegree_broadcastNotSent() {
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, TEMP_LESS_THEN_ONE_DEGREE_CELSIUS,
                        UPDATED_CHARGE_COUNTER,
                        CURRENT_BATTERY_HEALTH));

        waitForHandlerToExecute();

        verifyNumberOfTimesBroadcastSent(0);
    }

    @Test
    @DisableFlags(Flags.FLAG_RATE_LIMIT_BATTERY_CHANGED_BROADCAST)
    public void onlyChargeCounterUpdated_broadcastSent() {
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, CURRENT_BATTERY_TEMP,
                        UPDATED_CHARGE_COUNTER,
                        CURRENT_BATTERY_HEALTH));

        waitForHandlerToExecute();
@@ -233,11 +277,12 @@ public class BatteryServiceTest {
    private HealthInfo createHealthInfo(
            int batteryVoltage,
            int batteryTemperature,
            int batteryChargeCounter,
            int batteryHealth) {
        HealthInfo h = new HealthInfo();
        h.batteryVoltageMillivolts = batteryVoltage;
        h.batteryTemperatureTenthsCelsius = batteryTemperature;
        h.batteryChargeCounterUah = 4680000;
        h.batteryChargeCounterUah = batteryChargeCounter;
        h.batteryStatus = 5;
        h.batteryHealth = batteryHealth;
        h.batteryPresent = true;
@@ -253,7 +298,7 @@ public class BatteryServiceTest {
    }

    // Creates a new battery service objects and sets the initial values.
    private void createBatteryService() {
    private void createBatteryService() throws InterruptedException {
        final HandlerThread handlerThread = new HandlerThread("BatteryServiceTest");
        handlerThread.start();

@@ -262,6 +307,7 @@ public class BatteryServiceTest {
        // trigger the update to set the initial values.
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, CURRENT_BATTERY_TEMP,
                        CURRENT_CHARGE_COUNTER,
                        CURRENT_BATTERY_HEALTH));

        waitForHandlerToExecute();
@@ -289,5 +335,4 @@ public class BatteryServiceTest {
                        eq(BatteryService.BATTERY_CHANGED_OPTIONS), eq(UserHandle.USER_ALL)),
                times(++numberOfTimes));
    }

}