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

Commit f57f99ed authored by Kweku Adams's avatar Kweku Adams
Browse files

Formalizing states in BatterySaverStateMachine.

Formalizing the states and transitions makes it easier to add new
functionality and guarantee correctness.

This also fixes the issue where automatically turning off sticky
accidentally turns off battery saver since the transition is only
done when in the correct state. The problem was that when the battery
level changed (above the sticky auto disable threshold),
doAutoBatterySaver would be called. stickyEnabled would be true,
and the level would be above the threshold, so it would disable sticky.
Then at the next level drop, doAutoBatterySaver would be called again,
but this time, sticky would be false, so it would go to automatic check
and see that the level is above the threshold and then turn off battery saver.

Bug: 127659938
Bug: 119764865
Bug: 112232746
Bug: 79580230
Test: atest com.android.server.power.batterysaver.BatterySaverStateMachineTest
Test: atest CtsBatterySavingTestCases
Change-Id: Ia311d8bdc593a1680eda82d4d06fee21ea45c0ba
parent 0718f9d0
Loading
Loading
Loading
Loading
+12 −3
Original line number Diff line number Diff line
@@ -318,6 +318,16 @@ message BatterySaverStateMachineProto {
    // Whether battery saver is enabled.
    optional bool enabled = 1;

    enum StateEnum {
        STATE_UNKNOWN = 0;
        STATE_OFF = 1;
        STATE_MANUAL_ON = 2;
        STATE_AUTOMATIC_ON = 3;
        STATE_OFF_AUTOMATIC_SNOOZED = 4;
        STATE_PENDING_STICKY_ON = 5;
    }
    optional StateEnum state = 18;

    // Whether full battery saver is enabled.
    optional bool is_full_enabled = 14;

@@ -337,8 +347,7 @@ message BatterySaverStateMachineProto {
    // Whether battery status has been set at least once.
    optional bool battery_status_set = 4;

    // Whether automatic battery saver has been canceled by the user.
    optional bool battery_saver_snoozing = 5;
    reserved 5; // battery_saver_snoozing

    // Whether the device is connected to any power source.
    optional bool is_powered = 6;
@@ -373,5 +382,5 @@ message BatterySaverStateMachineProto {
    // using elapsed realtime as the timebase.
    optional int64 last_adaptive_battery_saver_changed_externally_elapsed = 17;

    // Next tag: 18
    // Next tag: 19
}
+35 −3
Original line number Diff line number Diff line
@@ -115,9 +115,41 @@ public class BatterySaverController implements BatterySaverPolicyListener {
    public static final int REASON_SETTING_CHANGED = 8;
    public static final int REASON_DYNAMIC_POWER_SAVINGS_AUTOMATIC_ON = 9;
    public static final int REASON_DYNAMIC_POWER_SAVINGS_AUTOMATIC_OFF = 10;
    public static final int REASON_STICKY_RESTORE_OFF = 11;
    public static final int REASON_ADAPTIVE_DYNAMIC_POWER_SAVINGS_CHANGED = 12;
    public static final int REASON_TIMEOUT = 13;
    public static final int REASON_ADAPTIVE_DYNAMIC_POWER_SAVINGS_CHANGED = 11;
    public static final int REASON_TIMEOUT = 12;

    static String reasonToString(int reason) {
        switch (reason) {
            case BatterySaverController.REASON_PERCENTAGE_AUTOMATIC_ON:
                return "Percentage Auto ON";
            case BatterySaverController.REASON_PERCENTAGE_AUTOMATIC_OFF:
                return "Percentage Auto OFF";
            case BatterySaverController.REASON_MANUAL_ON:
                return "Manual ON";
            case BatterySaverController.REASON_MANUAL_OFF:
                return "Manual OFF";
            case BatterySaverController.REASON_STICKY_RESTORE:
                return "Sticky restore";
            case BatterySaverController.REASON_INTERACTIVE_CHANGED:
                return "Interactivity changed";
            case BatterySaverController.REASON_POLICY_CHANGED:
                return "Policy changed";
            case BatterySaverController.REASON_PLUGGED_IN:
                return "Plugged in";
            case BatterySaverController.REASON_SETTING_CHANGED:
                return "Setting changed";
            case BatterySaverController.REASON_DYNAMIC_POWER_SAVINGS_AUTOMATIC_ON:
                return "Dynamic Warning Auto ON";
            case BatterySaverController.REASON_DYNAMIC_POWER_SAVINGS_AUTOMATIC_OFF:
                return "Dynamic Warning Auto OFF";
            case BatterySaverController.REASON_ADAPTIVE_DYNAMIC_POWER_SAVINGS_CHANGED:
                return "Adaptive Power Savings changed";
            case BatterySaverController.REASON_TIMEOUT:
                return "timeout";
            default:
                return "Unknown reason: " + reason;
        }
    }

    /**
     * Plugin interface. All methods are guaranteed to be called on the same (handler) thread.
+254 −96

File changed.

Preview size limit exceeded, changes collapsed.

+290 −8

File changed.

Preview size limit exceeded, changes collapsed.