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

Commit 4838c005 authored by Nicholas Ambur's avatar Nicholas Ambur
Browse files

add full battery saver runtime modification API

New system API added for privledged clients to modify the full battery
saver mode policy. This API is used to override static settings at
runtime, and any overridden settings are cleared when exiting full
battery saver mode.

Bug: 172294448
Test: atest PowerManagerTest
Test: atest PowerManagerServiceTest
Test: atest BatterySaverPolicyTest
Test: build and boot ensuring SoundTrigger service behavior is not
changed

Change-Id: I41f968799b184a5dac702553379294795614be0a
parent 8b317b15
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -7854,6 +7854,7 @@ package android.os {
  public static final class BatterySaverPolicyConfig.Builder {
    ctor public BatterySaverPolicyConfig.Builder();
    ctor public BatterySaverPolicyConfig.Builder(@NonNull android.os.BatterySaverPolicyConfig);
    method @NonNull public android.os.BatterySaverPolicyConfig.Builder addDeviceSpecificSetting(@NonNull String, @NonNull String);
    method @NonNull public android.os.BatterySaverPolicyConfig build();
    method @NonNull public android.os.BatterySaverPolicyConfig.Builder setAdjustBrightnessFactor(float);
@@ -8221,6 +8222,7 @@ package android.os {
  public final class PowerManager {
    method @RequiresPermission(allOf={android.Manifest.permission.READ_DREAM_STATE, android.Manifest.permission.WRITE_DREAM_STATE}) public void dream(long);
    method @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public boolean forceSuspend();
    method @NonNull public android.os.BatterySaverPolicyConfig getFullPowerSavePolicy();
    method @RequiresPermission(android.Manifest.permission.POWER_SAVER) public int getPowerSaveModeTrigger();
    method @RequiresPermission(android.Manifest.permission.READ_DREAM_STATE) public boolean isAmbientDisplayAvailable();
    method @RequiresPermission(android.Manifest.permission.READ_DREAM_STATE) public boolean isAmbientDisplaySuppressed();
@@ -8229,6 +8231,7 @@ package android.os {
    method @RequiresPermission(anyOf={android.Manifest.permission.DEVICE_POWER, android.Manifest.permission.POWER_SAVER}) public boolean setAdaptivePowerSavePolicy(@NonNull android.os.BatterySaverPolicyConfig);
    method @RequiresPermission(android.Manifest.permission.DEVICE_POWER) public void setBatteryDischargePrediction(@NonNull java.time.Duration, boolean);
    method @RequiresPermission(android.Manifest.permission.POWER_SAVER) public boolean setDynamicPowerSaveHint(boolean, int);
    method @RequiresPermission(anyOf={android.Manifest.permission.DEVICE_POWER, android.Manifest.permission.POWER_SAVER}) public boolean setFullPowerSavePolicy(@NonNull android.os.BatterySaverPolicyConfig);
    method @RequiresPermission(anyOf={android.Manifest.permission.DEVICE_POWER, android.Manifest.permission.POWER_SAVER}) public boolean setPowerSaveModeEnabled(boolean);
    method @RequiresPermission(android.Manifest.permission.WRITE_DREAM_STATE) public void suppressAmbientDisplay(@NonNull String, boolean);
    method @RequiresPermission(anyOf={android.Manifest.permission.DEVICE_POWER, android.Manifest.permission.USER_ACTIVITY}) public void userActivity(long, int, int);
+32 −0
Original line number Diff line number Diff line
@@ -340,6 +340,38 @@ public final class BatterySaverPolicyConfig implements Parcelable {
        public Builder() {
        }

        /**
         * Creates a Builder prepopulated with the values from the passed in
         * {@link BatterySaverPolicyConfig}.
         */
        public Builder(@NonNull BatterySaverPolicyConfig batterySaverPolicyConfig) {
            mAdjustBrightnessFactor = batterySaverPolicyConfig.getAdjustBrightnessFactor();
            mAdvertiseIsEnabled = batterySaverPolicyConfig.getAdvertiseIsEnabled();
            mDeferFullBackup = batterySaverPolicyConfig.getDeferFullBackup();
            mDeferKeyValueBackup = batterySaverPolicyConfig.getDeferKeyValueBackup();

            for (String key :
                    batterySaverPolicyConfig.getDeviceSpecificSettings().keySet()) {
                mDeviceSpecificSettings.put(key,
                        batterySaverPolicyConfig.getDeviceSpecificSettings().get(key));
            }

            mDisableAnimation = batterySaverPolicyConfig.getDisableAnimation();
            mDisableAod = batterySaverPolicyConfig.getDisableAod();
            mDisableLaunchBoost = batterySaverPolicyConfig.getDisableLaunchBoost();
            mDisableOptionalSensors = batterySaverPolicyConfig.getDisableOptionalSensors();
            mDisableVibration = batterySaverPolicyConfig.getDisableVibration();
            mEnableAdjustBrightness = batterySaverPolicyConfig.getEnableAdjustBrightness();
            mEnableDataSaver = batterySaverPolicyConfig.getEnableDataSaver();
            mEnableFirewall = batterySaverPolicyConfig.getEnableFirewall();
            mEnableNightMode = batterySaverPolicyConfig.getEnableNightMode();
            mEnableQuickDoze = batterySaverPolicyConfig.getEnableQuickDoze();
            mForceAllAppsStandby = batterySaverPolicyConfig.getForceAllAppsStandby();
            mForceBackgroundCheck = batterySaverPolicyConfig.getForceBackgroundCheck();
            mLocationMode = batterySaverPolicyConfig.getLocationMode();
            mSoundTriggerMode = batterySaverPolicyConfig.getSoundTriggerMode();
        }

        /**
         * Set how much to adjust the screen brightness while in Battery Saver. The value should
         * be in the [0, 1] range, where 1 will not change the brightness. This will have no
+2 −0
Original line number Diff line number Diff line
@@ -55,6 +55,8 @@ interface IPowerManager
    boolean isPowerSaveMode();
    PowerSaveState getPowerSaveState(int serviceType);
    boolean setPowerSaveModeEnabled(boolean mode);
    BatterySaverPolicyConfig getFullPowerSavePolicy();
    boolean setFullPowerSavePolicy(in BatterySaverPolicyConfig config);
    boolean setDynamicPowerSaveHint(boolean powerSaveHint, int disableThreshold);
    boolean setAdaptivePowerSavePolicy(in BatterySaverPolicyConfig config);
    boolean setAdaptivePowerSaveEnabled(boolean enabled);
+59 −0
Original line number Diff line number Diff line
@@ -1637,6 +1637,65 @@ public final class PowerManager {
        }
    }

    /**
     * Gets the current policy for full power save mode.
     *
     * @return The {@link BatterySaverPolicyConfig} which is currently set for the full power save
     *          policy level.
     *
     * @hide
     */
    @SystemApi
    @NonNull
    public BatterySaverPolicyConfig getFullPowerSavePolicy() {
        try {
            return mService.getFullPowerSavePolicy();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Sets the policy for full power save mode.
     *
     * Any settings set by this API will persist for only one session of full battery saver mode.
     * The settings set by this API are cleared upon exit of full battery saver mode, and the
     * caller is expected to set the desired values again for the next full battery saver mode
     * session if desired.
     *
     * Use-cases:
     * 1. Set policy outside of full battery saver mode
     *     - full policy set -> enter BS -> policy setting applied -> exit BS -> setting cleared
     * 2. Set policy inside of full battery saver mode
     *     - enter BS -> full policy set -> policy setting applied -> exit BS -> setting cleared
     *
     * This API is intended to be used with {@link #getFullPowerSavePolicy()} API when a client only
     * wants to modify a specific setting(s) and leave the remaining policy attributes the same.
     * Example:
     * BatterySaverPolicyConfig newFullPolicyConfig =
     *     new BatterySaverPolicyConfig.Builder(powerManager.getFullPowerSavePolicy())
     *         .setSoundTriggerMode(PowerManager.SOUND_TRIGGER_MODE_ALL_DISABLED)
     *         .build();
     * powerManager.setFullPowerSavePolicy(newFullPolicyConfig);
     *
     * @return true if there was an effectual change. If full battery saver is enabled, then this
     * will return true.
     *
     * @hide
     */
    @SystemApi
    @RequiresPermission(anyOf = {
            android.Manifest.permission.DEVICE_POWER,
            android.Manifest.permission.POWER_SAVER
    })
    public boolean setFullPowerSavePolicy(@NonNull BatterySaverPolicyConfig config) {
        try {
            return mService.setFullPowerSavePolicy(config);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Updates the current state of dynamic power savings and disable threshold. This is
     * a signal to the system which an app can update to serve as an indicator that
+25 −0
Original line number Diff line number Diff line
@@ -5031,6 +5031,31 @@ public final class PowerManagerService extends SystemService
            }
        }

        @Override // Binder call
        public BatterySaverPolicyConfig getFullPowerSavePolicy() {
            final long ident = Binder.clearCallingIdentity();
            try {
                return mBatterySaverStateMachine.getFullBatterySaverPolicy();
            } finally {
                Binder.restoreCallingIdentity(ident);
            }
        }

        @Override // Binder call
        public boolean setFullPowerSavePolicy(@NonNull BatterySaverPolicyConfig config) {
            if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER)
                    != PackageManager.PERMISSION_GRANTED) {
                mContext.enforceCallingOrSelfPermission(
                        android.Manifest.permission.DEVICE_POWER, "setFullPowerSavePolicy");
            }
            final long ident = Binder.clearCallingIdentity();
            try {
                return mBatterySaverStateMachine.setFullBatterySaverPolicy(config);
            } finally {
                Binder.restoreCallingIdentity(ident);
            }
        }

        @Override // Binder call
        public boolean setDynamicPowerSaveHint(boolean powerSaveHint, int disableThreshold) {
            mContext.enforceCallingOrSelfPermission(android.Manifest.permission.POWER_SAVER,
Loading