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

Commit 91e395cc authored by mayankkk's avatar mayankkk Committed by Mayank Dandwani
Browse files

Rate limit battery changed for max charging current extra.

Bug: 362337621
Test: atest BatteryServiceTest
Flag: com.android.server.flags.rate_limit_battery_changed_broadcast
Change-Id: Ia9bccaa86e9b854ad939d2e70bdebd486b5db6ab
parent a457c6a3
Loading
Loading
Loading
Loading
+49 −5
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@ import android.service.battery.BatteryServiceDumpProto;
import android.sysprop.PowerProperties;
import android.util.EventLog;
import android.util.Slog;
import android.util.TimeUtils;
import android.util.proto.ProtoOutputStream;

import com.android.internal.annotations.VisibleForTesting;
@@ -303,6 +304,17 @@ public final class BatteryService extends SystemService {
     */
    @VisibleForTesting
    public long mLastBroadcastVoltageUpdateTime;
    /**
     * Time when the max charging current was updated last by HAL and we sent the
     * {@link Intent#ACTION_BATTERY_CHANGED} broadcast.
     * Note: This value is used to rate limit the {@link Intent#ACTION_BATTERY_CHANGED} broadcast
     * so it is possible that max current was updated but we did not send the broadcast so in that
     * case we do not update the time.
     */
    @VisibleForTesting
    public long mLastBroadcastMaxChargingCurrentUpdateTime;

    private boolean mIsFirstBatteryChangedUpdate = true;

    private Led mLed;

@@ -350,16 +362,21 @@ public final class BatteryService extends SystemService {
    private static final int ABSOLUTE_DECI_CELSIUS_DIFF_FOR_TEMP_UPDATE = 10;
    /**
     * This value is used to rate limit the {@link Intent#ACTION_BATTERY_CHANGED} broadcast. We
     * only send the broadcast if the last voltage was updated at least 20s seconds back and has a
     * only send the broadcast if the last voltage was updated at least 20 seconds back and has a
     * fluctuation of at least 1%.
     */
    private static final int TIME_DIFF_FOR_VOLTAGE_UPDATE_MS = 20000;
    /**
     * The value is used to rate limit the {@link Intent#ACTION_BATTERY_CHANGED} broadcast. We
     * only send the broadcast if the last voltage was updated at least 20s seconds back and has a
     * only send the broadcast if the last voltage was updated at least 20 seconds back and has a
     * fluctuation of at least 1%.
     */
    private static final float BASE_POINT_DIFF_FOR_VOLTAGE_UPDATE = 0.01f;
    /**
     * This value is used to rate limit the {@link Intent#ACTION_BATTERY_CHANGED} broadcast. We
     * only send the broadcast if the last max charging current was updated at least 5 seconds back.
     */
    private static final int TIME_DIFF_FOR_MAX_CHARGING_CURRENT_UPDATE_MS = 5000;

    private final Handler.Callback mLocalCallback = msg -> {
        switch (msg.what) {
@@ -1252,8 +1269,10 @@ public final class BatteryService extends SystemService {
        if (!com.android.server.flags.Flags.rateLimitBatteryChangedBroadcast()) {
            return false;
        }
        if (mLastBroadcastBatteryVoltage == 0 || mLastBroadcastBatteryTemperature == 0) {
        if (mIsFirstBatteryChangedUpdate) {
            mLastBroadcastVoltageUpdateTime = SystemClock.elapsedRealtime();
            mLastBroadcastMaxChargingCurrentUpdateTime = SystemClock.elapsedRealtime();
            mIsFirstBatteryChangedUpdate = false;
            return false;
        }

@@ -1261,13 +1280,14 @@ public final class BatteryService extends SystemService {
                mLastBroadcastBatteryVoltage != mHealthInfo.batteryVoltageMillivolts;
        final boolean temperatureUpdated =
                mLastBroadcastBatteryTemperature != mHealthInfo.batteryTemperatureTenthsCelsius;
        final boolean maxChargingCurrentUpdated =
                mLastBroadcastMaxChargingCurrent != mHealthInfo.maxChargingCurrentMicroamps;
        final boolean otherStatesUpdated = forceUpdate
                || mHealthInfo.batteryStatus != mLastBroadcastBatteryStatus
                || mHealthInfo.batteryHealth != mLastBroadcastBatteryHealth
                || mHealthInfo.batteryPresent != mLastBroadcastBatteryPresent
                || mHealthInfo.batteryLevel != mLastBroadcastBatteryLevel
                || mPlugType != mLastBroadcastPlugType
                || mHealthInfo.maxChargingCurrentMicroamps != mLastBroadcastMaxChargingCurrent
                || mHealthInfo.maxChargingVoltageMicrovolts != mLastBroadcastMaxChargingVoltage
                || mInvalidCharger != mLastBroadcastInvalidCharger
                || mHealthInfo.batteryCycleCount != mLastBroadcastBatteryCycleCount
@@ -1280,6 +1300,9 @@ public final class BatteryService extends SystemService {
            if (voltageUpdated) {
                mLastBroadcastVoltageUpdateTime = SystemClock.elapsedRealtime();
            }
            if (maxChargingCurrentUpdated) {
                mLastBroadcastMaxChargingCurrentUpdateTime = SystemClock.elapsedRealtime();
            }
            return false;
        }

@@ -1295,6 +1318,9 @@ public final class BatteryService extends SystemService {
                        >= TIME_DIFF_FOR_VOLTAGE_UPDATE_MS) {
            mLastBroadcastVoltageUpdateTime = SystemClock.elapsedRealtime();

            if (maxChargingCurrentUpdated) {
                mLastBroadcastMaxChargingCurrentUpdateTime = SystemClock.elapsedRealtime();
            }
            return false;
        }

@@ -1304,6 +1330,20 @@ public final class BatteryService extends SystemService {
                mLastBroadcastBatteryTemperature - mHealthInfo.batteryTemperatureTenthsCelsius)
                        >= ABSOLUTE_DECI_CELSIUS_DIFF_FOR_TEMP_UPDATE) {

            if (voltageUpdated) {
                mLastBroadcastVoltageUpdateTime = SystemClock.elapsedRealtime();
            }
            if (maxChargingCurrentUpdated) {
                mLastBroadcastMaxChargingCurrentUpdateTime = SystemClock.elapsedRealtime();
            }
            return false;
        }

        if (maxChargingCurrentUpdated
                && SystemClock.elapsedRealtime() - mLastBroadcastMaxChargingCurrentUpdateTime
                >= TIME_DIFF_FOR_MAX_CHARGING_CURRENT_UPDATE_MS) {
            mLastBroadcastMaxChargingCurrentUpdateTime = SystemClock.elapsedRealtime();

            if (voltageUpdated) {
                mLastBroadcastVoltageUpdateTime = SystemClock.elapsedRealtime();
            }
@@ -1615,6 +1655,9 @@ public final class BatteryService extends SystemService {
                pw.println("  Wireless powered: " + mHealthInfo.chargerWirelessOnline);
                pw.println("  Dock powered: " + mHealthInfo.chargerDockOnline);
                pw.println("  Max charging current: " + mHealthInfo.maxChargingCurrentMicroamps);
                pw.println(" Time when the latest updated value of the Max charging current was"
                        + " sent via battery changed broadcast: "
                        + TimeUtils.formatDuration(mLastBroadcastMaxChargingCurrentUpdateTime));
                pw.println("  Max charging voltage: " + mHealthInfo.maxChargingVoltageMicrovolts);
                pw.println("  Charge counter: " + mHealthInfo.batteryChargeCounterUah);
                pw.println("  status: " + mHealthInfo.batteryStatus);
@@ -1624,7 +1667,8 @@ public final class BatteryService extends SystemService {
                pw.println("  scale: " + BATTERY_SCALE);
                pw.println("  voltage: " + mHealthInfo.batteryVoltageMillivolts);
                pw.println(" Time when the latest updated value of the voltage was sent via "
                        + "battery changed broadcast: " + mLastBroadcastVoltageUpdateTime);
                        + "battery changed broadcast: "
                        + TimeUtils.formatDuration(mLastBroadcastVoltageUpdateTime));
                pw.println(" The last voltage value sent via the battery changed broadcast: "
                        + mLastBroadcastBatteryVoltage);
                pw.println("  temperature: " + mHealthInfo.batteryTemperatureTenthsCelsius);
+4 −0
Original line number Diff line number Diff line
@@ -176,6 +176,10 @@
                    "include-filter": "com.android.server.wm.BackgroundActivityStart*"
                }
            ]
        },
        {
            "name": "FrameworksMockingServicesTests_service_batteryServiceTest",
            "file_patterns": ["BatteryService\\.java"]
        }
   ]
}
+7 −0
Original line number Diff line number Diff line
@@ -392,3 +392,10 @@ test_module_config {
    ],
    include_filters: ["com.android.server.StorageManagerServiceTest"],
}

test_module_config {
    name: "FrameworksMockingServicesTests_service_batteryServiceTest",
    base: "FrameworksMockingServicesTests",
    test_suites: ["device-tests"],
    include_filters: ["com.android.server.BatteryServiceTest"],
}
+70 −17
Original line number Diff line number Diff line
@@ -79,6 +79,8 @@ public class BatteryServiceTest {
    private static final int UPDATED_BATTERY_HEALTH = 3;
    private static final int CURRENT_CHARGE_COUNTER = 4680000;
    private static final int UPDATED_CHARGE_COUNTER = 4218000;
    private static final int CURRENT_MAX_CHARGING_CURRENT = 298125;
    private static final int UPDATED_MAX_CHARGING_CURRENT = 398125;
    private static final int HANDLER_IDLE_TIME_MS = 5000;
    @Rule
    public final ExtendedMockitoRule mExtendedMockitoRule = new ExtendedMockitoRule.Builder(this)
@@ -143,7 +145,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_CHARGE_COUNTER, CURRENT_BATTERY_HEALTH));
                CURRENT_CHARGE_COUNTER, CURRENT_BATTERY_HEALTH, CURRENT_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();

@@ -156,7 +158,8 @@ public class BatteryServiceTest {
        mBatteryService.update(
                createHealthInfo(VOLTAGE_MORE_THEN_ONE_PERCENT, CURRENT_BATTERY_TEMP,
                        CURRENT_CHARGE_COUNTER,
                        CURRENT_BATTERY_HEALTH));
                        CURRENT_BATTERY_HEALTH,
                        CURRENT_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();

@@ -165,13 +168,17 @@ public class BatteryServiceTest {

    @Test
    @EnableFlags(Flags.FLAG_RATE_LIMIT_BATTERY_CHANGED_BROADCAST)
    public void onlyVoltageUpdated_broadcastSent() {
    public void voltageUpdated_withUpdateInChargingCurrent_broadcastSent() {
        mBatteryService.mLastBroadcastVoltageUpdateTime = SystemClock.elapsedRealtime() - 20000;
        long lastChargingCurrentUpdateTime =
                mBatteryService.mLastBroadcastMaxChargingCurrentUpdateTime;
        mBatteryService.update(createHealthInfo(VOLTAGE_MORE_THEN_ONE_PERCENT, CURRENT_BATTERY_TEMP,
                CURRENT_CHARGE_COUNTER, CURRENT_BATTERY_HEALTH));
                CURRENT_CHARGE_COUNTER, CURRENT_BATTERY_HEALTH, UPDATED_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();

        assertTrue(lastChargingCurrentUpdateTime
                < mBatteryService.mLastBroadcastMaxChargingCurrentUpdateTime);
        verifyNumberOfTimesBroadcastSent(1);
    }

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

        waitForHandlerToExecute();

@@ -191,23 +199,31 @@ public class BatteryServiceTest {
    @EnableFlags(Flags.FLAG_RATE_LIMIT_BATTERY_CHANGED_BROADCAST)
    public void tempUpdated_broadcastSent() {
        long lastVoltageUpdateTime = mBatteryService.mLastBroadcastVoltageUpdateTime;
        long lastChargingCurrentUpdateTime =
                mBatteryService.mLastBroadcastMaxChargingCurrentUpdateTime;
        mBatteryService.update(
                createHealthInfo(VOLTAGE_LESS_THEN_ONE_PERCENT, TEMP_MORE_THEN_ONE_DEGREE_CELSIUS,
                        UPDATED_CHARGE_COUNTER, CURRENT_BATTERY_HEALTH));
                        UPDATED_CHARGE_COUNTER, CURRENT_BATTERY_HEALTH,
                        UPDATED_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();

        assertTrue(lastVoltageUpdateTime < mBatteryService.mLastBroadcastVoltageUpdateTime);
        assertTrue(lastChargingCurrentUpdateTime
                < mBatteryService.mLastBroadcastMaxChargingCurrentUpdateTime);
        verifyNumberOfTimesBroadcastSent(1);
    }

    @Test
    @EnableFlags(Flags.FLAG_RATE_LIMIT_BATTERY_CHANGED_BROADCAST)
    public void batteryHealthUpdated_voltageAndTempConst_broadcastSent() {
    public void batteryHealthUpdated_withOtherExtrasConstant_broadcastSent() {
        long lastVoltageUpdateTime = mBatteryService.mLastBroadcastVoltageUpdateTime;
        long lastChargingCurrentUpdateTime =
                mBatteryService.mLastBroadcastMaxChargingCurrentUpdateTime;
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, CURRENT_BATTERY_TEMP,
                createHealthInfo(VOLTAGE_LESS_THEN_ONE_PERCENT, CURRENT_BATTERY_TEMP,
                        CURRENT_CHARGE_COUNTER,
                        UPDATED_BATTERY_HEALTH));
                        UPDATED_BATTERY_HEALTH, UPDATED_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();

@@ -217,10 +233,13 @@ public class BatteryServiceTest {
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, CURRENT_BATTERY_TEMP,
                        UPDATED_CHARGE_COUNTER,
                        UPDATED_BATTERY_HEALTH));
                        UPDATED_BATTERY_HEALTH, CURRENT_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();

        assertTrue(lastVoltageUpdateTime < mBatteryService.mLastBroadcastVoltageUpdateTime);
        assertTrue(lastChargingCurrentUpdateTime
                < mBatteryService.mLastBroadcastMaxChargingCurrentUpdateTime);
        verifyNumberOfTimesBroadcastSent(1);
    }

@@ -228,7 +247,7 @@ 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));
                CURRENT_CHARGE_COUNTER, CURRENT_BATTERY_HEALTH, CURRENT_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();

@@ -241,7 +260,7 @@ public class BatteryServiceTest {
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, CURRENT_BATTERY_TEMP,
                        UPDATED_CHARGE_COUNTER,
                        CURRENT_BATTERY_HEALTH));
                        CURRENT_BATTERY_HEALTH, CURRENT_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();

@@ -254,7 +273,7 @@ public class BatteryServiceTest {
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, TEMP_LESS_THEN_ONE_DEGREE_CELSIUS,
                        UPDATED_CHARGE_COUNTER,
                        CURRENT_BATTERY_HEALTH));
                        CURRENT_BATTERY_HEALTH, CURRENT_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();

@@ -267,10 +286,42 @@ public class BatteryServiceTest {
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, CURRENT_BATTERY_TEMP,
                        UPDATED_CHARGE_COUNTER,
                        CURRENT_BATTERY_HEALTH));
                        CURRENT_BATTERY_HEALTH, CURRENT_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();

        verifyNumberOfTimesBroadcastSent(1);
    }

    @Test
    @EnableFlags(Flags.FLAG_RATE_LIMIT_BATTERY_CHANGED_BROADCAST)
    public void onlyMaxChargingCurrentUpdated_beforeFiveSeconds_broadcastNotSent() {
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, CURRENT_BATTERY_TEMP,
                        CURRENT_CHARGE_COUNTER,
                        CURRENT_BATTERY_HEALTH,
                        UPDATED_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();

        verifyNumberOfTimesBroadcastSent(0);
    }

    @Test
    @EnableFlags(Flags.FLAG_RATE_LIMIT_BATTERY_CHANGED_BROADCAST)
    public void maxChargingCurrentUpdated_afterFiveSeconds_broadcastSent() {
        mBatteryService.mLastBroadcastMaxChargingCurrentUpdateTime =
                SystemClock.elapsedRealtime() - 5000;
        long lastVoltageUpdateTime = mBatteryService.mLastBroadcastVoltageUpdateTime;
        mBatteryService.update(
                createHealthInfo(VOLTAGE_MORE_THEN_ONE_PERCENT, CURRENT_BATTERY_TEMP,
                        CURRENT_CHARGE_COUNTER,
                        CURRENT_BATTERY_HEALTH,
                        UPDATED_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();

        assertTrue(lastVoltageUpdateTime < mBatteryService.mLastBroadcastVoltageUpdateTime);
        verifyNumberOfTimesBroadcastSent(1);
    }

@@ -278,7 +329,8 @@ public class BatteryServiceTest {
            int batteryVoltage,
            int batteryTemperature,
            int batteryChargeCounter,
            int batteryHealth) {
            int batteryHealth,
            int maxChargingCurrent) {
        HealthInfo h = new HealthInfo();
        h.batteryVoltageMillivolts = batteryVoltage;
        h.batteryTemperatureTenthsCelsius = batteryTemperature;
@@ -287,7 +339,7 @@ public class BatteryServiceTest {
        h.batteryHealth = batteryHealth;
        h.batteryPresent = true;
        h.batteryLevel = 100;
        h.maxChargingCurrentMicroamps = 298125;
        h.maxChargingCurrentMicroamps = maxChargingCurrent;
        h.batteryCurrentAverageMicroamps = -2812;
        h.batteryCurrentMicroamps = 298125;
        h.maxChargingVoltageMicrovolts = 3000;
@@ -308,7 +360,8 @@ public class BatteryServiceTest {
        mBatteryService.update(
                createHealthInfo(CURRENT_BATTERY_VOLTAGE, CURRENT_BATTERY_TEMP,
                        CURRENT_CHARGE_COUNTER,
                        CURRENT_BATTERY_HEALTH));
                        CURRENT_BATTERY_HEALTH,
                        CURRENT_MAX_CHARGING_CURRENT));

        waitForHandlerToExecute();
    }