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

Commit 812ea755 authored by Salvador Martinez's avatar Salvador Martinez
Browse files

Create APIs to interact with DynamicPowerSaver

This creates the PowerManager APIs that allow apps with the
appropriate permissions to interact with Dynamic Power Saver.

Bug: 111450127
Test: WIP
Change-Id: I5b9483fa0fba81a4ade622b1f3dbaec580b68a67
parent 04b98338
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -4109,8 +4109,12 @@ package android.os {
  }

  public final class PowerManager {
    method public int getPowerSaveMode();
    method public boolean setDynamicPowerSavings(boolean, int);
    method public boolean setPowerSaveMode(boolean);
    method public void userActivity(long, int, int);
    field public static final int POWER_SAVER_MODE_DYNAMIC = 1; // 0x1
    field public static final int POWER_SAVER_MODE_PERCENTAGE = 0; // 0x0
    field public static final int USER_ACTIVITY_EVENT_ACCESSIBILITY = 3; // 0x3
    field public static final int USER_ACTIVITY_EVENT_BUTTON = 1; // 0x1
    field public static final int USER_ACTIVITY_EVENT_OTHER = 0; // 0x0
+7 −0
Original line number Diff line number Diff line
@@ -744,7 +744,11 @@ package android.os {
  }

  public final class PowerManager {
    method public int getPowerSaveMode();
    method public boolean setDynamicPowerSavings(boolean, int);
    method public boolean setPowerSaveMode(boolean);
    field public static final int POWER_SAVER_MODE_DYNAMIC = 1; // 0x1
    field public static final int POWER_SAVER_MODE_PERCENTAGE = 0; // 0x0
  }

  public class Process {
@@ -981,6 +985,9 @@ package android.provider {

  public static final class Settings.Global extends android.provider.Settings.NameValueTable {
    field public static final java.lang.String AUTOFILL_COMPAT_MODE_ALLOWED_PACKAGES = "autofill_compat_mode_allowed_packages";
    field public static final java.lang.String AUTOMATIC_POWER_SAVER_MODE = "automatic_power_saver_mode";
    field public static final java.lang.String DYNAMIC_POWER_SAVINGS_DISABLE_THRESHOLD = "dynamic_power_savings_disable_threshold";
    field public static final java.lang.String DYNAMIC_POWER_SAVINGS_ENABLED = "dynamic_power_savings_enabled";
    field public static final java.lang.String HIDDEN_API_BLACKLIST_EXEMPTIONS = "hidden_api_blacklist_exemptions";
    field public static final java.lang.String LOCATION_GLOBAL_KILL_SWITCH = "location_global_kill_switch";
    field public static final java.lang.String LOW_POWER_MODE = "low_power";
+2 −0
Original line number Diff line number Diff line
@@ -48,6 +48,8 @@ interface IPowerManager
    boolean isPowerSaveMode();
    PowerSaveState getPowerSaveState(int serviceType);
    boolean setPowerSaveMode(boolean mode);
    boolean setDynamicPowerSavings(boolean dynamicPowerSavingsEnabled, int disableThreshold);
    int getPowerSaveMode();
    boolean isDeviceIdleMode();
    boolean isLightDeviceIdleMode();

+100 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.os;

import android.Manifest.permission;
import android.annotation.IntDef;
import android.annotation.RequiresPermission;
import android.annotation.SdkConstant;
@@ -1184,6 +1185,105 @@ public final class PowerManager {
        }
    }

    /**
     * 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
     * the user will be in a battery critical situation before being able to plug in.
     * Only apps with the {@link android.Manifest.permission#POWER_SAVER} permission may do this.
     * This is a device global state, not a per user setting.
     *
     * <p>When enabled, the system may enact various measures for reducing power consumption in
     * order to help ensure that the user will make it to their next charging point. The most
     * visible of these will be the automatic enabling of battery saver if the user has set
     * their battery saver mode to "automatic". Note
     * that this is NOT simply an on/off switch for features, but rather a hint for the
     * system to consider enacting these power saving features, some of which have additional
     * logic around when to activate based on this signal.
     *
     * <p>The provided threshold is the percentage the system should consider itself safe at given
     * the current state of the device. The value is an integer representing a battery level.
     *
     * <p>The threshold is meant to set an explicit stopping point for dynamic power savings
     * functionality so that the dynamic power savings itself remains a signal rather than becoming
     * an on/off switch for a subset of features.
     * @hide
     *
     * @param dynamicPowerSavingsEnabled A signal indicating to the system if it believes the
     * dynamic power savings behaviors should be activated.
     * @param disableThreshold When the suggesting app believes it would be safe to disable dynamic
     * power savings behaviors.
     * @return True if the update was allowed and succeeded.
     *
     * @hide
     */
    @SystemApi
    @TestApi
    @RequiresPermission(permission.POWER_SAVER)
    public boolean setDynamicPowerSavings(boolean dynamicPowerSavingsEnabled,
            int disableThreshold) {
        try {
            return mService.setDynamicPowerSavings(dynamicPowerSavingsEnabled, disableThreshold);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Indicates automatic battery saver toggling by the system will be based on percentage.
     *
     * @see PowerManager#getPowerSaveMode()
     *
     *  @hide
     */
    @SystemApi
    @TestApi
    public static final int POWER_SAVER_MODE_PERCENTAGE = 0;

    /**
     * Indicates automatic battery saver toggling by the system will be based on the state
     * of the dynamic power savings signal.
     *
     * @see PowerManager#setDynamicPowerSavings(boolean, int)
     * @see PowerManager#getPowerSaveMode()
     *
     *  @hide
     */
    @SystemApi
    @TestApi
    public static final int POWER_SAVER_MODE_DYNAMIC = 1;

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(value = {
        POWER_SAVER_MODE_PERCENTAGE,
        POWER_SAVER_MODE_DYNAMIC

    })
    public @interface AutoPowerSaverMode{}


    /**
     * Returns the current battery saver control mode. Values it may return are defined in
     * AutoPowerSaverMode. Note that this is a global device state, not a per user setting.
     *
     * @return The current value power saver mode for the system.
     *
     * @see AutoPowerSaverMode
     * @see PowerManager#getPowerSaveMode()
     * @hide
     */
    @AutoPowerSaverMode
    @SystemApi
    @TestApi
    @RequiresPermission(android.Manifest.permission.POWER_SAVER)
    public int getPowerSaveMode() {
        try {
            return mService.getPowerSaveMode();
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Get data about the battery saver mode for a specific service
     * @param serviceType unique key for the service, one of {@link ServiceType}
+16 −53
Original line number Diff line number Diff line
@@ -11813,9 +11813,10 @@ public final class Settings {
         * Battery level [1-100] at which low power mode automatically turns on.
         * Pre-Q If 0, it will not automatically turn on. Q and newer it will only automatically
         * turn on if the {@link #AUTOMATIC_POWER_SAVER_MODE} setting is also set to
         * {@link #AUTOMATIC_POWER_SAVER_MODE_PERCENTAGE}.
         * {@link android.os.PowerManager.AutoPowerSaverMode#POWER_SAVER_MODE_PERCENTAGE}.
         *
         * @see #AUTOMATIC_POWER_SAVER_MODE
         * @see android.os.PowerManager#getPowerSaveMode()
         * @hide
         */
        public static final String LOW_POWER_MODE_TRIGGER_LEVEL = "low_power_trigger_level";
@@ -11824,79 +11825,39 @@ public final class Settings {
        private static final Validator LOW_POWER_MODE_TRIGGER_LEVEL_VALIDATOR =
                new SettingsValidators.InclusiveIntegerRangeValidator(0, 100);
        /**
         * Indicates automatic battery saver toggling by the system will be based on battery level.
         *
         *  @hide
         */
        public static final int AUTOMATIC_POWER_SAVER_MODE_PERCENTAGE = 0;
        /**
         * Indicates automatic battery saver toggling by the system will be based on
         * {@link #DYNAMIC_POWER_SAVINGS_ENABLED} and
         * {@link #DYNAMIC_POWER_SAVINGS_DISABLE_THRESHOLD}.
         *
         * @see #DYNAMIC_POWER_SAVINGS_ENABLED
         *
         *  @hide
         */
        public static final int AUTOMATIC_POWER_SAVER_MODE_DYNAMIC = 1;
        /** @hide */
        @Retention(RetentionPolicy.SOURCE)
        @IntDef(value = {
            AUTOMATIC_POWER_SAVER_MODE_PERCENTAGE,
            AUTOMATIC_POWER_SAVER_MODE_DYNAMIC
        })
        public @interface AutoPowerSaverMode{}
        /**
         * Whether battery saver is currently set to trigger based on percentage, dynamic power
         * savings trigger, or none. See {@link AutoPowerSaverMode} for accepted values.
         * savings trigger, or none. See {@link android.os.PowerManager.AutoPowerSaverMode} for
         * accepted values.
         *
         *  @hide
         */
        @TestApi
        public static final String AUTOMATIC_POWER_SAVER_MODE = "automatic_power_saver_mode";
        private static final Validator AUTOMATIC_POWER_SAVER_MODE_VALIDATOR =
                new SettingsValidators.DiscreteValueValidator(new String[] {"0", "1"});
        /**
         * The percentage the system should consider itself safe at if the dynamic power savings was
         * previously enabled and it enacted measures to reduce power consumption. Value is
         * an integer representing a battery level.
         * The setting that backs the disable threshold for the setPowerSavingsWarning api in
         * PowerManager
         *
         * <p>This value is used to set an explicit stopping point for dynamic power savings
         * functionality so that the {@link #DYNAMIC_POWER_SAVINGS_ENABLED} setting remains a signal
         * for the system rather than becoming an on/off switch itself.
         * @see android.os.PowerManager#setDynamicPowerSavings(boolean, int)
         * @hide
         */
        @TestApi
        public static final String DYNAMIC_POWER_SAVINGS_DISABLE_THRESHOLD =
                "dynamic_power_savings_disable_threshold";
        private static final Validator DYNAMIC_POWER_SAVINGS_VALIDATOR =
                new SettingsValidators.InclusiveIntegerRangeValidator(0, 100);
        /**
         * A signal to the system which an app can update which indicates that
         * the user will be in a battery critical situation in the near future.
         * Only apps with the {@link android.Manifest.permission.POWER_SAVER} permission may modify
         * this setting.
         *
         * <p>When enabled, the system may enact various measures for reducing power consumption in
         * order to help ensure that the user will make it to their next charging point. The most
         * visible of these will be the automatic enabling of battery saver if the user has set
         * {@link #AUTOMATIC_POWER_SAVER_MODE} to {@link #AUTOMATIC_POWER_SAVER_MODE_DYNAMIC}. Note
         * that this is NOT an on/off switch for all these features, but rather a hint for the
         * system to consider enacting these power saving features, some of which have additional
         * logic around when to activate based on this signal.
         * The setting which backs the setDynamicPowerSavings api in PowerManager.
         *
         * <p>Supported values:
         * <ul>
         * <li>0 = Disabled
         * <li>1 = Enabled
         * </ul>
         * @see #DYNAMIC_POWER_SAVINGS_DISABLE_THRESHOLD
         * @see android.os.PowerManager#setDynamicPowerSavings(boolean, int)
         * @hide
         */
        @TestApi
        public static final String DYNAMIC_POWER_SAVINGS_ENABLED = "dynamic_power_savings_enabled";
        /**
@@ -12822,6 +12783,8 @@ public final class Settings {
            VALIDATORS.put(LOW_POWER_MODE_TRIGGER_LEVEL_MAX,
                    LOW_POWER_MODE_TRIGGER_LEVEL_VALIDATOR);
            VALIDATORS.put(AUTOMATIC_POWER_SAVER_MODE, AUTOMATIC_POWER_SAVER_MODE_VALIDATOR);
            VALIDATORS.put(DYNAMIC_POWER_SAVINGS_DISABLE_THRESHOLD,
                    DYNAMIC_POWER_SAVINGS_VALIDATOR);
            VALIDATORS.put(BLUETOOTH_ON, BLUETOOTH_ON_VALIDATOR);
            VALIDATORS.put(PRIVATE_DNS_MODE, PRIVATE_DNS_MODE_VALIDATOR);
            VALIDATORS.put(PRIVATE_DNS_SPECIFIER, PRIVATE_DNS_SPECIFIER_VALIDATOR);
Loading