Home | History | Annotate | Download | only in os
      1 /*
      2  * Copyright (C) 2019 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.os;
     18 
     19 import android.annotation.NonNull;
     20 import android.annotation.SystemApi;
     21 import android.text.TextUtils;
     22 import android.util.ArrayMap;
     23 
     24 import java.util.Collections;
     25 import java.util.Map;
     26 import java.util.Set;
     27 
     28 /**
     29  * Config to set Battery Saver policy flags.
     30  *
     31  * @hide
     32  */
     33 @SystemApi
     34 public final class BatterySaverPolicyConfig implements Parcelable {
     35     private final float mAdjustBrightnessFactor;
     36     private final boolean mAdvertiseIsEnabled;
     37     private final boolean mDeferFullBackup;
     38     private final boolean mDeferKeyValueBackup;
     39     @NonNull
     40     private final Map<String, String> mDeviceSpecificSettings;
     41     private final boolean mDisableAnimation;
     42     private final boolean mDisableAod;
     43     private final boolean mDisableLaunchBoost;
     44     private final boolean mDisableOptionalSensors;
     45     private final boolean mDisableSoundTrigger;
     46     private final boolean mDisableVibration;
     47     private final boolean mEnableAdjustBrightness;
     48     private final boolean mEnableDataSaver;
     49     private final boolean mEnableFirewall;
     50     private final boolean mEnableNightMode;
     51     private final boolean mEnableQuickDoze;
     52     private final boolean mForceAllAppsStandby;
     53     private final boolean mForceBackgroundCheck;
     54     private final int mLocationMode;
     55 
     56     private BatterySaverPolicyConfig(Builder in) {
     57         mAdjustBrightnessFactor = Math.max(0, Math.min(in.mAdjustBrightnessFactor, 1f));
     58         mAdvertiseIsEnabled = in.mAdvertiseIsEnabled;
     59         mDeferFullBackup = in.mDeferFullBackup;
     60         mDeferKeyValueBackup = in.mDeferKeyValueBackup;
     61         mDeviceSpecificSettings = Collections.unmodifiableMap(
     62                 new ArrayMap<>(in.mDeviceSpecificSettings));
     63         mDisableAnimation = in.mDisableAnimation;
     64         mDisableAod = in.mDisableAod;
     65         mDisableLaunchBoost = in.mDisableLaunchBoost;
     66         mDisableOptionalSensors = in.mDisableOptionalSensors;
     67         mDisableSoundTrigger = in.mDisableSoundTrigger;
     68         mDisableVibration = in.mDisableVibration;
     69         mEnableAdjustBrightness = in.mEnableAdjustBrightness;
     70         mEnableDataSaver = in.mEnableDataSaver;
     71         mEnableFirewall = in.mEnableFirewall;
     72         mEnableNightMode = in.mEnableNightMode;
     73         mEnableQuickDoze = in.mEnableQuickDoze;
     74         mForceAllAppsStandby = in.mForceAllAppsStandby;
     75         mForceBackgroundCheck = in.mForceBackgroundCheck;
     76         mLocationMode = Math.max(PowerManager.MIN_LOCATION_MODE,
     77                 Math.min(in.mLocationMode, PowerManager.MAX_LOCATION_MODE));
     78     }
     79 
     80     private BatterySaverPolicyConfig(Parcel in) {
     81         mAdjustBrightnessFactor = Math.max(0, Math.min(in.readFloat(), 1f));
     82         mAdvertiseIsEnabled = in.readBoolean();
     83         mDeferFullBackup = in.readBoolean();
     84         mDeferKeyValueBackup = in.readBoolean();
     85 
     86         final int size = in.readInt();
     87         Map<String, String> deviceSpecificSettings = new ArrayMap<>(size);
     88         for (int i = 0; i < size; ++i) {
     89             String key = TextUtils.emptyIfNull(in.readString());
     90             String val = TextUtils.emptyIfNull(in.readString());
     91             if (key.trim().isEmpty()) {
     92                 continue;
     93             }
     94             deviceSpecificSettings.put(key, val);
     95         }
     96         mDeviceSpecificSettings = Collections.unmodifiableMap(deviceSpecificSettings);
     97 
     98         mDisableAnimation = in.readBoolean();
     99         mDisableAod = in.readBoolean();
    100         mDisableLaunchBoost = in.readBoolean();
    101         mDisableOptionalSensors = in.readBoolean();
    102         mDisableSoundTrigger = in.readBoolean();
    103         mDisableVibration = in.readBoolean();
    104         mEnableAdjustBrightness = in.readBoolean();
    105         mEnableDataSaver = in.readBoolean();
    106         mEnableFirewall = in.readBoolean();
    107         mEnableNightMode = in.readBoolean();
    108         mEnableQuickDoze = in.readBoolean();
    109         mForceAllAppsStandby = in.readBoolean();
    110         mForceBackgroundCheck = in.readBoolean();
    111         mLocationMode = Math.max(PowerManager.MIN_LOCATION_MODE,
    112                 Math.min(in.readInt(), PowerManager.MAX_LOCATION_MODE));
    113     }
    114 
    115     public static final @android.annotation.NonNull Creator<BatterySaverPolicyConfig> CREATOR =
    116             new Creator<BatterySaverPolicyConfig>() {
    117                 @Override
    118                 public BatterySaverPolicyConfig createFromParcel(Parcel in) {
    119                     return new BatterySaverPolicyConfig(in);
    120                 }
    121 
    122                 @Override
    123                 public BatterySaverPolicyConfig[] newArray(int size) {
    124                     return new BatterySaverPolicyConfig[size];
    125                 }
    126             };
    127 
    128     @Override
    129     public int describeContents() {
    130         return 0;
    131     }
    132 
    133     @Override
    134     public void writeToParcel(Parcel dest, int flags) {
    135         dest.writeFloat(mAdjustBrightnessFactor);
    136         dest.writeBoolean(mAdvertiseIsEnabled);
    137         dest.writeBoolean(mDeferFullBackup);
    138         dest.writeBoolean(mDeferKeyValueBackup);
    139 
    140         final Set<Map.Entry<String, String>> entries = mDeviceSpecificSettings.entrySet();
    141         final int size = entries.size();
    142         dest.writeInt(size);
    143         for (Map.Entry<String, String> entry : entries) {
    144             dest.writeString(entry.getKey());
    145             dest.writeString(entry.getValue());
    146         }
    147 
    148         dest.writeBoolean(mDisableAnimation);
    149         dest.writeBoolean(mDisableAod);
    150         dest.writeBoolean(mDisableLaunchBoost);
    151         dest.writeBoolean(mDisableOptionalSensors);
    152         dest.writeBoolean(mDisableSoundTrigger);
    153         dest.writeBoolean(mDisableVibration);
    154         dest.writeBoolean(mEnableAdjustBrightness);
    155         dest.writeBoolean(mEnableDataSaver);
    156         dest.writeBoolean(mEnableFirewall);
    157         dest.writeBoolean(mEnableNightMode);
    158         dest.writeBoolean(mEnableQuickDoze);
    159         dest.writeBoolean(mForceAllAppsStandby);
    160         dest.writeBoolean(mForceBackgroundCheck);
    161         dest.writeInt(mLocationMode);
    162     }
    163 
    164     @Override
    165     public String toString() {
    166         StringBuilder sb = new StringBuilder();
    167         for (Map.Entry<String, String> entry : mDeviceSpecificSettings.entrySet()) {
    168             sb.append(entry.getKey()).append("=").append(entry.getValue()).append(",");
    169         }
    170         return "adjust_brightness_disabled=" + !mEnableAdjustBrightness + ","
    171                 + "adjust_brightness_factor=" + mAdjustBrightnessFactor + ","
    172                 + "advertise_is_enabled=" + mAdvertiseIsEnabled + ","
    173                 + "animation_disabled=" + mDisableAnimation + ","
    174                 + "aod_disabled=" + mDisableAod + ","
    175                 + "datasaver_disabled=" + !mEnableDataSaver + ","
    176                 + "enable_night_mode=" + mEnableNightMode + ","
    177                 + "firewall_disabled=" + !mEnableFirewall + ","
    178                 + "force_all_apps_standby=" + mForceAllAppsStandby + ","
    179                 + "force_background_check=" + mForceBackgroundCheck + ","
    180                 + "fullbackup_deferred=" + mDeferFullBackup + ","
    181                 + "gps_mode=" + mLocationMode + ","
    182                 + "keyvaluebackup_deferred=" + mDeferKeyValueBackup + ","
    183                 + "launch_boost_disabled=" + mDisableLaunchBoost + ","
    184                 + "optional_sensors_disabled=" + mDisableOptionalSensors + ","
    185                 + "quick_doze_enabled=" + mEnableQuickDoze + ","
    186                 + "soundtrigger_disabled=" + mDisableSoundTrigger + ","
    187                 + "vibration_disabled=" + mDisableVibration + ","
    188                 + sb.toString();
    189     }
    190 
    191     /**
    192      * How much to adjust the screen brightness while in Battery Saver. This will have no effect
    193      * if {@link #getEnableAdjustBrightness()} is {@code false}.
    194      */
    195     public float getAdjustBrightnessFactor() {
    196         return mAdjustBrightnessFactor;
    197     }
    198 
    199     /**
    200      * Whether or not to tell the system (and other apps) that Battery Saver is currently enabled.
    201      */
    202     public boolean getAdvertiseIsEnabled() {
    203         return mAdvertiseIsEnabled;
    204     }
    205 
    206     /** Whether or not to defer full backup while in Battery Saver. */
    207     public boolean getDeferFullBackup() {
    208         return mDeferFullBackup;
    209     }
    210 
    211     /** Whether or not to defer key-value backup while in Battery Saver. */
    212     public boolean getDeferKeyValueBackup() {
    213         return mDeferKeyValueBackup;
    214     }
    215 
    216     /**
    217      * Returns the device-specific battery saver constants.
    218      */
    219     @NonNull
    220     public Map<String, String> getDeviceSpecificSettings() {
    221         return mDeviceSpecificSettings;
    222     }
    223 
    224     /** Whether or not to disable animation while in Battery Saver. */
    225     public boolean getDisableAnimation() {
    226         return mDisableAnimation;
    227     }
    228 
    229     /** Whether or not to disable Always On Display while in Battery Saver. */
    230     public boolean getDisableAod() {
    231         return mDisableAod;
    232     }
    233 
    234     /** Whether or not to disable launch boost while in Battery Saver. */
    235     public boolean getDisableLaunchBoost() {
    236         return mDisableLaunchBoost;
    237     }
    238 
    239     /** Whether or not to disable optional sensors while in Battery Saver. */
    240     public boolean getDisableOptionalSensors() {
    241         return mDisableOptionalSensors;
    242     }
    243 
    244     /**
    245      * Whether or not to disable {@link android.hardware.soundtrigger.SoundTrigger}
    246      * while in Battery Saver.
    247      */
    248     public boolean getDisableSoundTrigger() {
    249         return mDisableSoundTrigger;
    250     }
    251 
    252     /** Whether or not to disable vibration while in Battery Saver. */
    253     public boolean getDisableVibration() {
    254         return mDisableVibration;
    255     }
    256 
    257     /** Whether or not to enable brightness adjustment while in Battery Saver. */
    258     public boolean getEnableAdjustBrightness() {
    259         return mEnableAdjustBrightness;
    260     }
    261 
    262     /** Whether or not to enable Data Saver while in Battery Saver. */
    263     public boolean getEnableDataSaver() {
    264         return mEnableDataSaver;
    265     }
    266 
    267     /**
    268      * Whether or not to enable network firewall rules to restrict background network use
    269      * while in Battery Saver.
    270      */
    271     public boolean getEnableFirewall() {
    272         return mEnableFirewall;
    273     }
    274 
    275     /** Whether or not to enable night mode while in Battery Saver. */
    276     public boolean getEnableNightMode() {
    277         return mEnableNightMode;
    278     }
    279 
    280     /** Whether or not to enable Quick Doze while in Battery Saver. */
    281     public boolean getEnableQuickDoze() {
    282         return mEnableQuickDoze;
    283     }
    284 
    285     /** Whether or not to force all apps to standby mode while in Battery Saver. */
    286     public boolean getForceAllAppsStandby() {
    287         return mForceAllAppsStandby;
    288     }
    289 
    290     /**
    291      * Whether or not to force background check (disallow background services and manifest
    292      * broadcast receivers) on all apps (not just apps targeting Android
    293      * {@link Build.VERSION_CODES#O} and above)
    294      * while in Battery Saver.
    295      */
    296     public boolean getForceBackgroundCheck() {
    297         return mForceBackgroundCheck;
    298     }
    299 
    300     /** The location mode while in Battery Saver. */
    301     public int getLocationMode() {
    302         return mLocationMode;
    303     }
    304 
    305     /** Builder class for constructing {@link BatterySaverPolicyConfig} objects. */
    306     public static final class Builder {
    307         private float mAdjustBrightnessFactor = 1f;
    308         private boolean mAdvertiseIsEnabled = false;
    309         private boolean mDeferFullBackup = false;
    310         private boolean mDeferKeyValueBackup = false;
    311         @NonNull
    312         private final ArrayMap<String, String> mDeviceSpecificSettings = new ArrayMap<>();
    313         private boolean mDisableAnimation = false;
    314         private boolean mDisableAod = false;
    315         private boolean mDisableLaunchBoost = false;
    316         private boolean mDisableOptionalSensors = false;
    317         private boolean mDisableSoundTrigger = false;
    318         private boolean mDisableVibration = false;
    319         private boolean mEnableAdjustBrightness = false;
    320         private boolean mEnableDataSaver = false;
    321         private boolean mEnableFirewall = false;
    322         private boolean mEnableNightMode = false;
    323         private boolean mEnableQuickDoze = false;
    324         private boolean mForceAllAppsStandby = false;
    325         private boolean mForceBackgroundCheck = false;
    326         private int mLocationMode = PowerManager.LOCATION_MODE_NO_CHANGE;
    327 
    328         public Builder() {
    329         }
    330 
    331         /**
    332          * Set how much to adjust the screen brightness while in Battery Saver. The value should
    333          * be in the [0, 1] range, where 1 will not change the brightness. This will have no
    334          * effect if {@link #setEnableAdjustBrightness(boolean)} is not called with {@code true}.
    335          */
    336         @NonNull
    337         public Builder setAdjustBrightnessFactor(float adjustBrightnessFactor) {
    338             mAdjustBrightnessFactor = adjustBrightnessFactor;
    339             return this;
    340         }
    341 
    342         /**
    343          * Set whether or not to tell the system (and other apps) that Battery Saver is
    344          * currently enabled.
    345          */
    346         @NonNull
    347         public Builder setAdvertiseIsEnabled(boolean advertiseIsEnabled) {
    348             mAdvertiseIsEnabled = advertiseIsEnabled;
    349             return this;
    350         }
    351 
    352         /** Set whether or not to defer full backup while in Battery Saver. */
    353         @NonNull
    354         public Builder setDeferFullBackup(boolean deferFullBackup) {
    355             mDeferFullBackup = deferFullBackup;
    356             return this;
    357         }
    358 
    359         /** Set whether or not to defer key-value backup while in Battery Saver. */
    360         @NonNull
    361         public Builder setDeferKeyValueBackup(boolean deferKeyValueBackup) {
    362             mDeferKeyValueBackup = deferKeyValueBackup;
    363             return this;
    364         }
    365 
    366         /**
    367          * Adds a key-value pair for device-specific battery saver constants. The supported keys
    368          * and values are the same as those in
    369          * {@link android.provider.Settings.Global#BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS}.
    370          *
    371          * @throws IllegalArgumentException if the provided key is invalid (empty, null, or all
    372          * whitespace)
    373          */
    374         @NonNull
    375         public Builder addDeviceSpecificSetting(@NonNull String key, @NonNull String value) {
    376             if (key == null) {
    377                 throw new IllegalArgumentException("Key cannot be null");
    378             }
    379             key = key.trim();
    380             if (TextUtils.isEmpty(key)) {
    381                 throw new IllegalArgumentException("Key cannot be empty");
    382             }
    383             mDeviceSpecificSettings.put(key, TextUtils.emptyIfNull(value));
    384             return this;
    385         }
    386 
    387         /** Set whether or not to disable animation while in Battery Saver. */
    388         @NonNull
    389         public Builder setDisableAnimation(boolean disableAnimation) {
    390             mDisableAnimation = disableAnimation;
    391             return this;
    392         }
    393 
    394         /** Set whether or not to disable Always On Display while in Battery Saver. */
    395         @NonNull
    396         public Builder setDisableAod(boolean disableAod) {
    397             mDisableAod = disableAod;
    398             return this;
    399         }
    400 
    401         /** Set whether or not to disable launch boost while in Battery Saver. */
    402         @NonNull
    403         public Builder setDisableLaunchBoost(boolean disableLaunchBoost) {
    404             mDisableLaunchBoost = disableLaunchBoost;
    405             return this;
    406         }
    407 
    408         /** Set whether or not to disable optional sensors while in Battery Saver. */
    409         @NonNull
    410         public Builder setDisableOptionalSensors(boolean disableOptionalSensors) {
    411             mDisableOptionalSensors = disableOptionalSensors;
    412             return this;
    413         }
    414 
    415         /**
    416          * Set whether or not to disable  {@link android.hardware.soundtrigger.SoundTrigger}
    417          * while in Battery Saver.
    418          */
    419         @NonNull
    420         public Builder setDisableSoundTrigger(boolean disableSoundTrigger) {
    421             mDisableSoundTrigger = disableSoundTrigger;
    422             return this;
    423         }
    424 
    425         /** Set whether or not to disable vibration while in Battery Saver. */
    426         @NonNull
    427         public Builder setDisableVibration(boolean disableVibration) {
    428             mDisableVibration = disableVibration;
    429             return this;
    430         }
    431 
    432         /** Set whether or not to enable brightness adjustment while in Battery Saver. */
    433         @NonNull
    434         public Builder setEnableAdjustBrightness(boolean enableAdjustBrightness) {
    435             mEnableAdjustBrightness = enableAdjustBrightness;
    436             return this;
    437         }
    438 
    439         /** Set whether or not to enable Data Saver while in Battery Saver. */
    440         @NonNull
    441         public Builder setEnableDataSaver(boolean enableDataSaver) {
    442             mEnableDataSaver = enableDataSaver;
    443             return this;
    444         }
    445 
    446         /**
    447          * Set whether or not to enable network firewall rules to restrict background network use
    448          * while in Battery Saver.
    449          */
    450         @NonNull
    451         public Builder setEnableFirewall(boolean enableFirewall) {
    452             mEnableFirewall = enableFirewall;
    453             return this;
    454         }
    455 
    456         /** Set whether or not to enable night mode while in Battery Saver. */
    457         @NonNull
    458         public Builder setEnableNightMode(boolean enableNightMode) {
    459             mEnableNightMode = enableNightMode;
    460             return this;
    461         }
    462 
    463         /** Set whether or not to enable Quick Doze while in Battery Saver. */
    464         @NonNull
    465         public Builder setEnableQuickDoze(boolean enableQuickDoze) {
    466             mEnableQuickDoze = enableQuickDoze;
    467             return this;
    468         }
    469 
    470         /** Set whether or not to force all apps to standby mode while in Battery Saver. */
    471         @NonNull
    472         public Builder setForceAllAppsStandby(boolean forceAllAppsStandby) {
    473             mForceAllAppsStandby = forceAllAppsStandby;
    474             return this;
    475         }
    476 
    477         /**
    478          * Set whether or not to force background check (disallow background services and manifest
    479          * broadcast receivers) on all apps (not just apps targeting Android
    480          * {@link Build.VERSION_CODES#O} and above)
    481          * while in Battery Saver.
    482          */
    483         @NonNull
    484         public Builder setForceBackgroundCheck(boolean forceBackgroundCheck) {
    485             mForceBackgroundCheck = forceBackgroundCheck;
    486             return this;
    487         }
    488 
    489         /** Set the location mode while in Battery Saver. */
    490         @NonNull
    491         public Builder setLocationMode(@PowerManager.LocationPowerSaveMode int locationMode) {
    492             mLocationMode = locationMode;
    493             return this;
    494         }
    495 
    496         /**
    497          * Build a {@link BatterySaverPolicyConfig} object using the set parameters. This object
    498          * is immutable.
    499          */
    500         @NonNull
    501         public BatterySaverPolicyConfig build() {
    502             return new BatterySaverPolicyConfig(this);
    503         }
    504     }
    505 }
    506