Kweku Adams | 9f488e2 | 2019-01-14 16:25:08 -0800 | [diff] [blame] | 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 mEnableQuickDoze; |
| 51 | private final boolean mForceAllAppsStandby; |
| 52 | private final boolean mForceBackgroundCheck; |
| 53 | private final int mGpsMode; |
| 54 | |
| 55 | private BatterySaverPolicyConfig(Builder in) { |
| 56 | mAdjustBrightnessFactor = Math.max(0, Math.min(in.mAdjustBrightnessFactor, 1f)); |
| 57 | mAdvertiseIsEnabled = in.mAdvertiseIsEnabled; |
| 58 | mDeferFullBackup = in.mDeferFullBackup; |
| 59 | mDeferKeyValueBackup = in.mDeferKeyValueBackup; |
| 60 | mDeviceSpecificSettings = Collections.unmodifiableMap(in.mDeviceSpecificSettings); |
| 61 | mDisableAnimation = in.mDisableAnimation; |
| 62 | mDisableAod = in.mDisableAod; |
| 63 | mDisableLaunchBoost = in.mDisableLaunchBoost; |
| 64 | mDisableOptionalSensors = in.mDisableOptionalSensors; |
| 65 | mDisableSoundTrigger = in.mDisableSoundTrigger; |
| 66 | mDisableVibration = in.mDisableVibration; |
| 67 | mEnableAdjustBrightness = in.mEnableAdjustBrightness; |
| 68 | mEnableDataSaver = in.mEnableDataSaver; |
| 69 | mEnableFirewall = in.mEnableFirewall; |
| 70 | mEnableQuickDoze = in.mEnableQuickDoze; |
| 71 | mForceAllAppsStandby = in.mForceAllAppsStandby; |
| 72 | mForceBackgroundCheck = in.mForceBackgroundCheck; |
| 73 | mGpsMode = Math.max(PowerManager.MIN_LOCATION_MODE, |
| 74 | Math.min(in.mGpsMode, PowerManager.MAX_LOCATION_MODE)); |
| 75 | } |
| 76 | |
| 77 | private BatterySaverPolicyConfig(Parcel in) { |
| 78 | mAdjustBrightnessFactor = Math.max(0, Math.min(in.readFloat(), 1f)); |
| 79 | mAdvertiseIsEnabled = in.readBoolean(); |
| 80 | mDeferFullBackup = in.readBoolean(); |
| 81 | mDeferKeyValueBackup = in.readBoolean(); |
| 82 | |
| 83 | final int size = in.readInt(); |
| 84 | Map<String, String> deviceSpecificSettings = new ArrayMap<>(size); |
| 85 | for (int i = 0; i < size; ++i) { |
| 86 | String key = TextUtils.emptyIfNull(in.readString()); |
| 87 | String val = TextUtils.emptyIfNull(in.readString()); |
| 88 | if (key.trim().isEmpty()) { |
| 89 | continue; |
| 90 | } |
| 91 | deviceSpecificSettings.put(key, val); |
| 92 | } |
| 93 | mDeviceSpecificSettings = Collections.unmodifiableMap(deviceSpecificSettings); |
| 94 | |
| 95 | mDisableAnimation = in.readBoolean(); |
| 96 | mDisableAod = in.readBoolean(); |
| 97 | mDisableLaunchBoost = in.readBoolean(); |
| 98 | mDisableOptionalSensors = in.readBoolean(); |
| 99 | mDisableSoundTrigger = in.readBoolean(); |
| 100 | mDisableVibration = in.readBoolean(); |
| 101 | mEnableAdjustBrightness = in.readBoolean(); |
| 102 | mEnableDataSaver = in.readBoolean(); |
| 103 | mEnableFirewall = in.readBoolean(); |
| 104 | mEnableQuickDoze = in.readBoolean(); |
| 105 | mForceAllAppsStandby = in.readBoolean(); |
| 106 | mForceBackgroundCheck = in.readBoolean(); |
| 107 | mGpsMode = Math.max(PowerManager.MIN_LOCATION_MODE, |
| 108 | Math.min(in.readInt(), PowerManager.MAX_LOCATION_MODE)); |
| 109 | } |
| 110 | |
| 111 | public static final Creator<BatterySaverPolicyConfig> CREATOR = |
| 112 | new Creator<BatterySaverPolicyConfig>() { |
| 113 | @Override |
| 114 | public BatterySaverPolicyConfig createFromParcel(Parcel in) { |
| 115 | return new BatterySaverPolicyConfig(in); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public BatterySaverPolicyConfig[] newArray(int size) { |
| 120 | return new BatterySaverPolicyConfig[size]; |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | @Override |
| 125 | public int describeContents() { |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void writeToParcel(Parcel dest, int flags) { |
| 131 | dest.writeFloat(mAdjustBrightnessFactor); |
| 132 | dest.writeBoolean(mAdvertiseIsEnabled); |
| 133 | dest.writeBoolean(mDeferFullBackup); |
| 134 | dest.writeBoolean(mDeferKeyValueBackup); |
| 135 | |
| 136 | final Set<Map.Entry<String, String>> entries = mDeviceSpecificSettings.entrySet(); |
| 137 | final int size = entries.size(); |
| 138 | dest.writeInt(size); |
| 139 | for (Map.Entry<String, String> entry : entries) { |
| 140 | dest.writeString(entry.getKey()); |
| 141 | dest.writeString(entry.getValue()); |
| 142 | } |
| 143 | |
| 144 | dest.writeBoolean(mDisableAnimation); |
| 145 | dest.writeBoolean(mDisableAod); |
| 146 | dest.writeBoolean(mDisableLaunchBoost); |
| 147 | dest.writeBoolean(mDisableOptionalSensors); |
| 148 | dest.writeBoolean(mDisableSoundTrigger); |
| 149 | dest.writeBoolean(mDisableVibration); |
| 150 | dest.writeBoolean(mEnableAdjustBrightness); |
| 151 | dest.writeBoolean(mEnableDataSaver); |
| 152 | dest.writeBoolean(mEnableFirewall); |
| 153 | dest.writeBoolean(mEnableQuickDoze); |
| 154 | dest.writeBoolean(mForceAllAppsStandby); |
| 155 | dest.writeBoolean(mForceBackgroundCheck); |
| 156 | dest.writeInt(mGpsMode); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public String toString() { |
| 161 | StringBuilder sb = new StringBuilder(); |
| 162 | for (Map.Entry<String, String> entry : mDeviceSpecificSettings.entrySet()) { |
| 163 | sb.append(entry.getKey()).append("=").append(entry.getValue()).append(","); |
| 164 | } |
| 165 | return "adjust_brightness_disabled=" + !mEnableAdjustBrightness + "," |
| 166 | + "adjust_brightness_factor=" + mAdjustBrightnessFactor + "," |
| 167 | + "advertise_is_enabled=" + mAdvertiseIsEnabled + "," |
| 168 | + "animation_disabled=" + mDisableAnimation + "," |
| 169 | + "aod_disabled=" + mDisableAod + "," |
| 170 | + "datasaver_disabled=" + !mEnableDataSaver + "," |
| 171 | + "firewall_disabled=" + !mEnableFirewall + "," |
| 172 | + "force_all_apps_standby=" + mForceAllAppsStandby + "," |
| 173 | + "force_background_check=" + mForceBackgroundCheck + "," |
| 174 | + "fullbackup_deferred=" + mDeferFullBackup + "," |
| 175 | + "gps_mode=" + mGpsMode + "," |
| 176 | + "keyvaluebackup_deferred=" + mDeferKeyValueBackup + "," |
| 177 | + "launch_boost_disabled=" + mDisableLaunchBoost + "," |
| 178 | + "optional_sensors_disabled=" + mDisableOptionalSensors + "," |
| 179 | + "quick_doze_enabled=" + mEnableQuickDoze + "," |
| 180 | + "soundtrigger_disabled=" + mDisableSoundTrigger + "," |
| 181 | + "vibration_disabled=" + mDisableVibration + "," |
| 182 | + sb.toString(); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * How much to adjust the screen brightness while in Battery Saver. This will have no effect |
| 187 | * if {@link #getEnableAdjustBrightness()} is {@code false}. |
| 188 | */ |
| 189 | public float getAdjustBrightnessFactor() { |
| 190 | return mAdjustBrightnessFactor; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Whether or not to tell the system (and other apps) that Battery Saver is currently enabled. |
| 195 | */ |
| 196 | public boolean getAdvertiseIsEnabled() { |
| 197 | return mAdvertiseIsEnabled; |
| 198 | } |
| 199 | |
| 200 | /** Whether or not to defer full backup while in Battery Saver. */ |
| 201 | public boolean getDeferFullBackup() { |
| 202 | return mDeferFullBackup; |
| 203 | } |
| 204 | |
| 205 | /** Whether or not to defer key-value backup while in Battery Saver. */ |
| 206 | public boolean getDeferKeyValueBackup() { |
| 207 | return mDeferKeyValueBackup; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Returns the device-specific battery saver constants. |
| 212 | */ |
| 213 | @NonNull |
| 214 | public Map<String, String> getDeviceSpecificSettings() { |
| 215 | return mDeviceSpecificSettings; |
| 216 | } |
| 217 | |
| 218 | /** Whether or not to disable animation while in Battery Saver. */ |
| 219 | public boolean getDisableAnimation() { |
| 220 | return mDisableAnimation; |
| 221 | } |
| 222 | |
| 223 | /** Whether or not to disable Always On Display while in Battery Saver. */ |
| 224 | public boolean getDisableAod() { |
| 225 | return mDisableAod; |
| 226 | } |
| 227 | |
| 228 | /** Whether or not to disable launch boost while in Battery Saver. */ |
| 229 | public boolean getDisableLaunchBoost() { |
| 230 | return mDisableLaunchBoost; |
| 231 | } |
| 232 | |
| 233 | /** Whether or not to disable optional sensors while in Battery Saver. */ |
| 234 | public boolean getDisableOptionalSensors() { |
| 235 | return mDisableOptionalSensors; |
| 236 | } |
| 237 | |
| 238 | /** Whether or not to disable sound trigger while in Battery Saver. */ |
| 239 | public boolean getDisableSoundTrigger() { |
| 240 | return mDisableSoundTrigger; |
| 241 | } |
| 242 | |
| 243 | /** Whether or not to disable vibration while in Battery Saver. */ |
| 244 | public boolean getDisableVibration() { |
| 245 | return mDisableVibration; |
| 246 | } |
| 247 | |
| 248 | /** Whether or not to enable brightness adjustment while in Battery Saver. */ |
| 249 | public boolean getEnableAdjustBrightness() { |
| 250 | return mEnableAdjustBrightness; |
| 251 | } |
| 252 | |
| 253 | /** Whether or not to enable Data Saver while in Battery Saver. */ |
| 254 | public boolean getEnableDataSaver() { |
| 255 | return mEnableDataSaver; |
| 256 | } |
| 257 | |
| 258 | /** Whether or not to enable the network firewall while in Battery Saver. */ |
| 259 | public boolean getEnableFirewall() { |
| 260 | return mEnableFirewall; |
| 261 | } |
| 262 | |
| 263 | /** Whether or not to enable Quick Doze while in Battery Saver. */ |
| 264 | public boolean getEnableQuickDoze() { |
| 265 | return mEnableQuickDoze; |
| 266 | } |
| 267 | |
| 268 | /** Whether or not to force all apps to standby mode while in Battery Saver. */ |
| 269 | public boolean getForceAllAppsStandby() { |
| 270 | return mForceAllAppsStandby; |
| 271 | } |
| 272 | |
| 273 | /** Whether or not to force background check while in Battery Saver. */ |
| 274 | public boolean getForceBackgroundCheck() { |
| 275 | return mForceBackgroundCheck; |
| 276 | } |
| 277 | |
| 278 | /** The GPS mode while in Battery Saver. */ |
| 279 | public int getGpsMode() { |
| 280 | return mGpsMode; |
| 281 | } |
| 282 | |
| 283 | /** Builder class for constructing {@link BatterySaverPolicyConfig} objects. */ |
| 284 | public static final class Builder { |
| 285 | private float mAdjustBrightnessFactor = 1f; |
| 286 | private boolean mAdvertiseIsEnabled = false; |
| 287 | private boolean mDeferFullBackup = false; |
| 288 | private boolean mDeferKeyValueBackup = false; |
| 289 | @NonNull |
| 290 | private final ArrayMap<String, String> mDeviceSpecificSettings = new ArrayMap<>(); |
| 291 | private boolean mDisableAnimation = false; |
| 292 | private boolean mDisableAod = false; |
| 293 | private boolean mDisableLaunchBoost = false; |
| 294 | private boolean mDisableOptionalSensors = false; |
| 295 | private boolean mDisableSoundTrigger = false; |
| 296 | private boolean mDisableVibration = false; |
| 297 | private boolean mEnableAdjustBrightness = false; |
| 298 | private boolean mEnableDataSaver = false; |
| 299 | private boolean mEnableFirewall = false; |
| 300 | private boolean mEnableQuickDoze = false; |
| 301 | private boolean mForceAllAppsStandby = false; |
| 302 | private boolean mForceBackgroundCheck = false; |
| 303 | private int mGpsMode = PowerManager.LOCATION_MODE_NO_CHANGE; |
| 304 | |
| 305 | public Builder() { |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Set how much to adjust the screen brightness while in Battery Saver. The value should |
| 310 | * be in the [0, 1] range, where 1 will not change the brightness. This will have no |
| 311 | * effect if {@link #setEnableAdjustBrightness(boolean)} is not called with {@code true}. |
| 312 | */ |
| 313 | @NonNull |
| 314 | public Builder setAdjustBrightnessFactor(float adjustBrightnessFactor) { |
| 315 | mAdjustBrightnessFactor = adjustBrightnessFactor; |
| 316 | return this; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Set whether or not to tell the system (and other apps) that Battery Saver is |
| 321 | * currently enabled. |
| 322 | */ |
| 323 | @NonNull |
| 324 | public Builder setAdvertiseIsEnabled(boolean advertiseIsEnabled) { |
| 325 | mAdvertiseIsEnabled = advertiseIsEnabled; |
| 326 | return this; |
| 327 | } |
| 328 | |
| 329 | /** Set whether or not to defer full backup while in Battery Saver. */ |
| 330 | @NonNull |
| 331 | public Builder setDeferFullBackup(boolean deferFullBackup) { |
| 332 | mDeferFullBackup = deferFullBackup; |
| 333 | return this; |
| 334 | } |
| 335 | |
| 336 | /** Set whether or not to defer key-value backup while in Battery Saver. */ |
| 337 | @NonNull |
| 338 | public Builder setDeferKeyValueBackup(boolean deferKeyValueBackup) { |
| 339 | mDeferKeyValueBackup = deferKeyValueBackup; |
| 340 | return this; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Adds a key-value pair for device-specific battery saver constants. The supported keys |
| 345 | * and values are the same as those in |
| 346 | * {@link android.provider.Settings.Global#BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS}. |
| 347 | * |
| 348 | * @throws IllegalArgumentException if the provided key is invalid (empty, null, or all |
| 349 | * whitespace) |
| 350 | */ |
| 351 | @NonNull |
| 352 | public Builder addDeviceSpecificSetting(@NonNull String key, @NonNull String value) { |
| 353 | if (key == null) { |
| 354 | throw new IllegalArgumentException("Key cannot be null"); |
| 355 | } |
| 356 | key = key.trim(); |
| 357 | if (TextUtils.isEmpty(key)) { |
| 358 | throw new IllegalArgumentException("Key cannot be empty"); |
| 359 | } |
| 360 | mDeviceSpecificSettings.put(key, TextUtils.emptyIfNull(value)); |
| 361 | return this; |
| 362 | } |
| 363 | |
| 364 | /** Set whether or not to disable animation while in Battery Saver. */ |
| 365 | @NonNull |
| 366 | public Builder setDisableAnimation(boolean disableAnimation) { |
| 367 | mDisableAnimation = disableAnimation; |
| 368 | return this; |
| 369 | } |
| 370 | |
| 371 | /** Set whether or not to disable Always On Display while in Battery Saver. */ |
| 372 | @NonNull |
| 373 | public Builder setDisableAod(boolean disableAod) { |
| 374 | mDisableAod = disableAod; |
| 375 | return this; |
| 376 | } |
| 377 | |
| 378 | /** Set whether or not to disable launch boost while in Battery Saver. */ |
| 379 | @NonNull |
| 380 | public Builder setDisableLaunchBoost(boolean disableLaunchBoost) { |
| 381 | mDisableLaunchBoost = disableLaunchBoost; |
| 382 | return this; |
| 383 | } |
| 384 | |
| 385 | /** Set whether or not to disable optional sensors while in Battery Saver. */ |
| 386 | @NonNull |
| 387 | public Builder setDisableOptionalSensors(boolean disableOptionalSensors) { |
| 388 | mDisableOptionalSensors = disableOptionalSensors; |
| 389 | return this; |
| 390 | } |
| 391 | |
| 392 | /** Set whether or not to disable sound trigger while in Battery Saver. */ |
| 393 | @NonNull |
| 394 | public Builder setDisableSoundTrigger(boolean disableSoundTrigger) { |
| 395 | mDisableSoundTrigger = disableSoundTrigger; |
| 396 | return this; |
| 397 | } |
| 398 | |
| 399 | /** Set whether or not to disable vibration while in Battery Saver. */ |
| 400 | @NonNull |
| 401 | public Builder setDisableVibration(boolean disableVibration) { |
| 402 | mDisableVibration = disableVibration; |
| 403 | return this; |
| 404 | } |
| 405 | |
| 406 | /** Set whether or not to enable brightness adjustment while in Battery Saver. */ |
| 407 | @NonNull |
| 408 | public Builder setEnableAdjustBrightness(boolean enableAdjustBrightness) { |
| 409 | mEnableAdjustBrightness = enableAdjustBrightness; |
| 410 | return this; |
| 411 | } |
| 412 | |
| 413 | /** Set whether or not to enable Data Saver while in Battery Saver. */ |
| 414 | @NonNull |
| 415 | public Builder setEnableDataSaver(boolean enableDataSaver) { |
| 416 | mEnableDataSaver = enableDataSaver; |
| 417 | return this; |
| 418 | } |
| 419 | |
| 420 | /** Set whether or not to enable the network firewall while in Battery Saver. */ |
| 421 | @NonNull |
| 422 | public Builder setEnableFirewall(boolean enableFirewall) { |
| 423 | mEnableFirewall = enableFirewall; |
| 424 | return this; |
| 425 | } |
| 426 | |
| 427 | /** Set whether or not to enable Quick Doze while in Battery Saver. */ |
| 428 | @NonNull |
| 429 | public Builder setEnableQuickDoze(boolean enableQuickDoze) { |
| 430 | mEnableQuickDoze = enableQuickDoze; |
| 431 | return this; |
| 432 | } |
| 433 | |
| 434 | /** Set whether or not to force all apps to standby mode while in Battery Saver. */ |
| 435 | @NonNull |
| 436 | public Builder setForceAllAppsStandby(boolean forceAllAppsStandby) { |
| 437 | mForceAllAppsStandby = forceAllAppsStandby; |
| 438 | return this; |
| 439 | } |
| 440 | |
| 441 | /** Set whether or not to force background check while in Battery Saver. */ |
| 442 | @NonNull |
| 443 | public Builder setForceBackgroundCheck(boolean forceBackgroundCheck) { |
| 444 | mForceBackgroundCheck = forceBackgroundCheck; |
| 445 | return this; |
| 446 | } |
| 447 | |
| 448 | /** Set the GPS mode while in Battery Saver. */ |
| 449 | @NonNull |
| 450 | public Builder setGpsMode(@PowerManager.LocationPowerSaveMode int gpsMode) { |
| 451 | mGpsMode = gpsMode; |
| 452 | return this; |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Build a {@link BatterySaverPolicyConfig} object using the set parameters. This object |
| 457 | * is immutable. |
| 458 | */ |
| 459 | @NonNull |
| 460 | public BatterySaverPolicyConfig build() { |
| 461 | if (!mEnableAdjustBrightness && Float.compare(1f, mAdjustBrightnessFactor) != 0) { |
| 462 | throw new IllegalArgumentException("Brightness adjustment factor changed without " |
| 463 | + "enabling brightness adjustment"); |
| 464 | } |
| 465 | return new BatterySaverPolicyConfig(this); |
| 466 | } |
| 467 | } |
| 468 | } |