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 BatterySaverPolicyConfig(Builder in)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 BatterySaverPolicyConfig(Parcel in)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 describeContents()129 public int describeContents() { 130 return 0; 131 } 132 133 @Override writeToParcel(Parcel dest, int flags)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 @NonNull 165 @Override toString()166 public String toString() { 167 StringBuilder sb = new StringBuilder(); 168 for (Map.Entry<String, String> entry : mDeviceSpecificSettings.entrySet()) { 169 sb.append(entry.getKey()).append("=").append(entry.getValue()).append(","); 170 } 171 return "adjust_brightness_disabled=" + !mEnableAdjustBrightness + "," 172 + "adjust_brightness_factor=" + mAdjustBrightnessFactor + "," 173 + "advertise_is_enabled=" + mAdvertiseIsEnabled + "," 174 + "animation_disabled=" + mDisableAnimation + "," 175 + "aod_disabled=" + mDisableAod + "," 176 + "datasaver_disabled=" + !mEnableDataSaver + "," 177 + "enable_night_mode=" + mEnableNightMode + "," 178 + "firewall_disabled=" + !mEnableFirewall + "," 179 + "force_all_apps_standby=" + mForceAllAppsStandby + "," 180 + "force_background_check=" + mForceBackgroundCheck + "," 181 + "fullbackup_deferred=" + mDeferFullBackup + "," 182 + "gps_mode=" + mLocationMode + "," 183 + "keyvaluebackup_deferred=" + mDeferKeyValueBackup + "," 184 + "launch_boost_disabled=" + mDisableLaunchBoost + "," 185 + "optional_sensors_disabled=" + mDisableOptionalSensors + "," 186 + "quick_doze_enabled=" + mEnableQuickDoze + "," 187 + "soundtrigger_disabled=" + mDisableSoundTrigger + "," 188 + "vibration_disabled=" + mDisableVibration + "," 189 + sb.toString(); 190 } 191 192 /** 193 * How much to adjust the screen brightness while in Battery Saver. This will have no effect 194 * if {@link #getEnableAdjustBrightness()} is {@code false}. 195 */ getAdjustBrightnessFactor()196 public float getAdjustBrightnessFactor() { 197 return mAdjustBrightnessFactor; 198 } 199 200 /** 201 * Whether or not to tell the system (and other apps) that Battery Saver is currently enabled. 202 */ getAdvertiseIsEnabled()203 public boolean getAdvertiseIsEnabled() { 204 return mAdvertiseIsEnabled; 205 } 206 207 /** Whether or not to defer full backup while in Battery Saver. */ getDeferFullBackup()208 public boolean getDeferFullBackup() { 209 return mDeferFullBackup; 210 } 211 212 /** Whether or not to defer key-value backup while in Battery Saver. */ getDeferKeyValueBackup()213 public boolean getDeferKeyValueBackup() { 214 return mDeferKeyValueBackup; 215 } 216 217 /** 218 * Returns the device-specific battery saver constants. 219 */ 220 @NonNull getDeviceSpecificSettings()221 public Map<String, String> getDeviceSpecificSettings() { 222 return mDeviceSpecificSettings; 223 } 224 225 /** Whether or not to disable animation while in Battery Saver. */ getDisableAnimation()226 public boolean getDisableAnimation() { 227 return mDisableAnimation; 228 } 229 230 /** Whether or not to disable Always On Display while in Battery Saver. */ getDisableAod()231 public boolean getDisableAod() { 232 return mDisableAod; 233 } 234 235 /** Whether or not to disable launch boost while in Battery Saver. */ getDisableLaunchBoost()236 public boolean getDisableLaunchBoost() { 237 return mDisableLaunchBoost; 238 } 239 240 /** Whether or not to disable optional sensors while in Battery Saver. */ getDisableOptionalSensors()241 public boolean getDisableOptionalSensors() { 242 return mDisableOptionalSensors; 243 } 244 245 /** 246 * Whether or not to disable {@link android.hardware.soundtrigger.SoundTrigger} 247 * while in Battery Saver. 248 */ getDisableSoundTrigger()249 public boolean getDisableSoundTrigger() { 250 return mDisableSoundTrigger; 251 } 252 253 /** Whether or not to disable vibration while in Battery Saver. */ getDisableVibration()254 public boolean getDisableVibration() { 255 return mDisableVibration; 256 } 257 258 /** Whether or not to enable brightness adjustment while in Battery Saver. */ getEnableAdjustBrightness()259 public boolean getEnableAdjustBrightness() { 260 return mEnableAdjustBrightness; 261 } 262 263 /** Whether or not to enable Data Saver while in Battery Saver. */ getEnableDataSaver()264 public boolean getEnableDataSaver() { 265 return mEnableDataSaver; 266 } 267 268 /** 269 * Whether or not to enable network firewall rules to restrict background network use 270 * while in Battery Saver. 271 */ getEnableFirewall()272 public boolean getEnableFirewall() { 273 return mEnableFirewall; 274 } 275 276 /** Whether or not to enable night mode while in Battery Saver. */ getEnableNightMode()277 public boolean getEnableNightMode() { 278 return mEnableNightMode; 279 } 280 281 /** Whether or not to enable Quick Doze while in Battery Saver. */ getEnableQuickDoze()282 public boolean getEnableQuickDoze() { 283 return mEnableQuickDoze; 284 } 285 286 /** Whether or not to force all apps to standby mode while in Battery Saver. */ getForceAllAppsStandby()287 public boolean getForceAllAppsStandby() { 288 return mForceAllAppsStandby; 289 } 290 291 /** 292 * Whether or not to force background check (disallow background services and manifest 293 * broadcast receivers) on all apps (not just apps targeting Android 294 * {@link Build.VERSION_CODES#O} and above) 295 * while in Battery Saver. 296 */ getForceBackgroundCheck()297 public boolean getForceBackgroundCheck() { 298 return mForceBackgroundCheck; 299 } 300 301 /** The location mode while in Battery Saver. */ getLocationMode()302 public int getLocationMode() { 303 return mLocationMode; 304 } 305 306 /** Builder class for constructing {@link BatterySaverPolicyConfig} objects. */ 307 public static final class Builder { 308 private float mAdjustBrightnessFactor = 1f; 309 private boolean mAdvertiseIsEnabled = false; 310 private boolean mDeferFullBackup = false; 311 private boolean mDeferKeyValueBackup = false; 312 @NonNull 313 private final ArrayMap<String, String> mDeviceSpecificSettings = new ArrayMap<>(); 314 private boolean mDisableAnimation = false; 315 private boolean mDisableAod = false; 316 private boolean mDisableLaunchBoost = false; 317 private boolean mDisableOptionalSensors = false; 318 private boolean mDisableSoundTrigger = false; 319 private boolean mDisableVibration = false; 320 private boolean mEnableAdjustBrightness = false; 321 private boolean mEnableDataSaver = false; 322 private boolean mEnableFirewall = false; 323 private boolean mEnableNightMode = false; 324 private boolean mEnableQuickDoze = false; 325 private boolean mForceAllAppsStandby = false; 326 private boolean mForceBackgroundCheck = false; 327 private int mLocationMode = PowerManager.LOCATION_MODE_NO_CHANGE; 328 Builder()329 public Builder() { 330 } 331 332 /** 333 * Set how much to adjust the screen brightness while in Battery Saver. The value should 334 * be in the [0, 1] range, where 1 will not change the brightness. This will have no 335 * effect if {@link #setEnableAdjustBrightness(boolean)} is not called with {@code true}. 336 */ 337 @NonNull setAdjustBrightnessFactor(float adjustBrightnessFactor)338 public Builder setAdjustBrightnessFactor(float adjustBrightnessFactor) { 339 mAdjustBrightnessFactor = adjustBrightnessFactor; 340 return this; 341 } 342 343 /** 344 * Set whether or not to tell the system (and other apps) that Battery Saver is 345 * currently enabled. 346 */ 347 @NonNull setAdvertiseIsEnabled(boolean advertiseIsEnabled)348 public Builder setAdvertiseIsEnabled(boolean advertiseIsEnabled) { 349 mAdvertiseIsEnabled = advertiseIsEnabled; 350 return this; 351 } 352 353 /** Set whether or not to defer full backup while in Battery Saver. */ 354 @NonNull setDeferFullBackup(boolean deferFullBackup)355 public Builder setDeferFullBackup(boolean deferFullBackup) { 356 mDeferFullBackup = deferFullBackup; 357 return this; 358 } 359 360 /** Set whether or not to defer key-value backup while in Battery Saver. */ 361 @NonNull setDeferKeyValueBackup(boolean deferKeyValueBackup)362 public Builder setDeferKeyValueBackup(boolean deferKeyValueBackup) { 363 mDeferKeyValueBackup = deferKeyValueBackup; 364 return this; 365 } 366 367 /** 368 * Adds a key-value pair for device-specific battery saver constants. The supported keys 369 * and values are the same as those in 370 * {@link android.provider.Settings.Global#BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS}. 371 * 372 * @throws IllegalArgumentException if the provided key is invalid (empty, null, or all 373 * whitespace) 374 */ 375 @NonNull addDeviceSpecificSetting(@onNull String key, @NonNull String value)376 public Builder addDeviceSpecificSetting(@NonNull String key, @NonNull String value) { 377 if (key == null) { 378 throw new IllegalArgumentException("Key cannot be null"); 379 } 380 key = key.trim(); 381 if (TextUtils.isEmpty(key)) { 382 throw new IllegalArgumentException("Key cannot be empty"); 383 } 384 mDeviceSpecificSettings.put(key, TextUtils.emptyIfNull(value)); 385 return this; 386 } 387 388 /** Set whether or not to disable animation while in Battery Saver. */ 389 @NonNull setDisableAnimation(boolean disableAnimation)390 public Builder setDisableAnimation(boolean disableAnimation) { 391 mDisableAnimation = disableAnimation; 392 return this; 393 } 394 395 /** Set whether or not to disable Always On Display while in Battery Saver. */ 396 @NonNull setDisableAod(boolean disableAod)397 public Builder setDisableAod(boolean disableAod) { 398 mDisableAod = disableAod; 399 return this; 400 } 401 402 /** Set whether or not to disable launch boost while in Battery Saver. */ 403 @NonNull setDisableLaunchBoost(boolean disableLaunchBoost)404 public Builder setDisableLaunchBoost(boolean disableLaunchBoost) { 405 mDisableLaunchBoost = disableLaunchBoost; 406 return this; 407 } 408 409 /** Set whether or not to disable optional sensors while in Battery Saver. */ 410 @NonNull setDisableOptionalSensors(boolean disableOptionalSensors)411 public Builder setDisableOptionalSensors(boolean disableOptionalSensors) { 412 mDisableOptionalSensors = disableOptionalSensors; 413 return this; 414 } 415 416 /** 417 * Set whether or not to disable {@link android.hardware.soundtrigger.SoundTrigger} 418 * while in Battery Saver. 419 */ 420 @NonNull setDisableSoundTrigger(boolean disableSoundTrigger)421 public Builder setDisableSoundTrigger(boolean disableSoundTrigger) { 422 mDisableSoundTrigger = disableSoundTrigger; 423 return this; 424 } 425 426 /** Set whether or not to disable vibration while in Battery Saver. */ 427 @NonNull setDisableVibration(boolean disableVibration)428 public Builder setDisableVibration(boolean disableVibration) { 429 mDisableVibration = disableVibration; 430 return this; 431 } 432 433 /** Set whether or not to enable brightness adjustment while in Battery Saver. */ 434 @NonNull setEnableAdjustBrightness(boolean enableAdjustBrightness)435 public Builder setEnableAdjustBrightness(boolean enableAdjustBrightness) { 436 mEnableAdjustBrightness = enableAdjustBrightness; 437 return this; 438 } 439 440 /** Set whether or not to enable Data Saver while in Battery Saver. */ 441 @NonNull setEnableDataSaver(boolean enableDataSaver)442 public Builder setEnableDataSaver(boolean enableDataSaver) { 443 mEnableDataSaver = enableDataSaver; 444 return this; 445 } 446 447 /** 448 * Set whether or not to enable network firewall rules to restrict background network use 449 * while in Battery Saver. 450 */ 451 @NonNull setEnableFirewall(boolean enableFirewall)452 public Builder setEnableFirewall(boolean enableFirewall) { 453 mEnableFirewall = enableFirewall; 454 return this; 455 } 456 457 /** Set whether or not to enable night mode while in Battery Saver. */ 458 @NonNull setEnableNightMode(boolean enableNightMode)459 public Builder setEnableNightMode(boolean enableNightMode) { 460 mEnableNightMode = enableNightMode; 461 return this; 462 } 463 464 /** Set whether or not to enable Quick Doze while in Battery Saver. */ 465 @NonNull setEnableQuickDoze(boolean enableQuickDoze)466 public Builder setEnableQuickDoze(boolean enableQuickDoze) { 467 mEnableQuickDoze = enableQuickDoze; 468 return this; 469 } 470 471 /** Set whether or not to force all apps to standby mode while in Battery Saver. */ 472 @NonNull setForceAllAppsStandby(boolean forceAllAppsStandby)473 public Builder setForceAllAppsStandby(boolean forceAllAppsStandby) { 474 mForceAllAppsStandby = forceAllAppsStandby; 475 return this; 476 } 477 478 /** 479 * Set whether or not to force background check (disallow background services and manifest 480 * broadcast receivers) on all apps (not just apps targeting Android 481 * {@link Build.VERSION_CODES#O} and above) 482 * while in Battery Saver. 483 */ 484 @NonNull setForceBackgroundCheck(boolean forceBackgroundCheck)485 public Builder setForceBackgroundCheck(boolean forceBackgroundCheck) { 486 mForceBackgroundCheck = forceBackgroundCheck; 487 return this; 488 } 489 490 /** Set the location mode while in Battery Saver. */ 491 @NonNull setLocationMode(@owerManager.LocationPowerSaveMode int locationMode)492 public Builder setLocationMode(@PowerManager.LocationPowerSaveMode int locationMode) { 493 mLocationMode = locationMode; 494 return this; 495 } 496 497 /** 498 * Build a {@link BatterySaverPolicyConfig} object using the set parameters. This object 499 * is immutable. 500 */ 501 @NonNull build()502 public BatterySaverPolicyConfig build() { 503 return new BatterySaverPolicyConfig(this); 504 } 505 } 506 } 507