Debian Codegraph @

core/java/android/os/BatterySaverPolicyConfig.java

raw symbols SCIP
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 mDisableVibration;
46 private final boolean mEnableAdjustBrightness;
47 private final boolean mEnableDataSaver;
48 private final boolean mEnableFirewall;
49 private final boolean mEnableNightMode;
50 private final boolean mEnableQuickDoze;
51 private final boolean mForceAllAppsStandby;
52 private final boolean mForceBackgroundCheck;
53 private final int mLocationMode;
54 private final int mSoundTriggerMode;
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 mDisableVibration = in.mDisableVibration;
68 mEnableAdjustBrightness = in.mEnableAdjustBrightness;
69 mEnableDataSaver = in.mEnableDataSaver;
70 mEnableFirewall = in.mEnableFirewall;
71 mEnableNightMode = in.mEnableNightMode;
72 mEnableQuickDoze = in.mEnableQuickDoze;
73 mForceAllAppsStandby = in.mForceAllAppsStandby;
74 mForceBackgroundCheck = in.mForceBackgroundCheck;
75 mLocationMode = Math.max(PowerManager.MIN_LOCATION_MODE,
76 Math.min(in.mLocationMode, PowerManager.MAX_LOCATION_MODE));
77 mSoundTriggerMode = Math.max(PowerManager.MIN_SOUND_TRIGGER_MODE,
78 Math.min(in.mSoundTriggerMode, PowerManager.MAX_SOUND_TRIGGER_MODE));
79 }
80
81 private BatterySaverPolicyConfig(Parcel in) {
82 mAdjustBrightnessFactor = Math.max(0, Math.min(in.readFloat(), 1f));
83 mAdvertiseIsEnabled = in.readBoolean();
84 mDeferFullBackup = in.readBoolean();
85 mDeferKeyValueBackup = in.readBoolean();
86
87 final int size = in.readInt();
88 Map<String, String> deviceSpecificSettings = new ArrayMap<>(size);
89 for (int i = 0; i < size; ++i) {
90 String key = TextUtils.emptyIfNull(in.readString());
91 String val = TextUtils.emptyIfNull(in.readString());
92 if (key.trim().isEmpty()) {
93 continue;
94 }
95 deviceSpecificSettings.put(key, val);
96 }
97 mDeviceSpecificSettings = Collections.unmodifiableMap(deviceSpecificSettings);
98
99 mDisableAnimation = in.readBoolean();
100 mDisableAod = in.readBoolean();
101 mDisableLaunchBoost = in.readBoolean();
102 mDisableOptionalSensors = 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 mSoundTriggerMode = Math.max(PowerManager.MIN_SOUND_TRIGGER_MODE,
114 Math.min(in.readInt(), PowerManager.MAX_SOUND_TRIGGER_MODE));
115 }
116
117 public static final @android.annotation.NonNull Creator<BatterySaverPolicyConfig> CREATOR =
118 new Creator<BatterySaverPolicyConfig>() {
119 @Override
120 public BatterySaverPolicyConfig createFromParcel(Parcel in) {
121 return new BatterySaverPolicyConfig(in);
122 }
123
124 @Override
125 public BatterySaverPolicyConfig[] newArray(int size) {
126 return new BatterySaverPolicyConfig[size];
127 }
128 };
129
130 @Override
131 public int describeContents() {
132 return 0;
133 }
134
135 @Override
136 public void writeToParcel(Parcel dest, int flags) {
137 dest.writeFloat(mAdjustBrightnessFactor);
138 dest.writeBoolean(mAdvertiseIsEnabled);
139 dest.writeBoolean(mDeferFullBackup);
140 dest.writeBoolean(mDeferKeyValueBackup);
141
142 final Set<Map.Entry<String, String>> entries = mDeviceSpecificSettings.entrySet();
143 final int size = entries.size();
144 dest.writeInt(size);
145 for (Map.Entry<String, String> entry : entries) {
146 dest.writeString(entry.getKey());
147 dest.writeString(entry.getValue());
148 }
149
150 dest.writeBoolean(mDisableAnimation);
151 dest.writeBoolean(mDisableAod);
152 dest.writeBoolean(mDisableLaunchBoost);
153 dest.writeBoolean(mDisableOptionalSensors);
154 dest.writeBoolean(mDisableVibration);
155 dest.writeBoolean(mEnableAdjustBrightness);
156 dest.writeBoolean(mEnableDataSaver);
157 dest.writeBoolean(mEnableFirewall);
158 dest.writeBoolean(mEnableNightMode);
159 dest.writeBoolean(mEnableQuickDoze);
160 dest.writeBoolean(mForceAllAppsStandby);
161 dest.writeBoolean(mForceBackgroundCheck);
162 dest.writeInt(mLocationMode);
163 dest.writeInt(mSoundTriggerMode);
164 }
165
166 @NonNull
167 @Override
168 public String toString() {
169 StringBuilder sb = new StringBuilder();
170 for (Map.Entry<String, String> entry : mDeviceSpecificSettings.entrySet()) {
171 sb.append(entry.getKey()).append("=").append(entry.getValue()).append(",");
172 }
173 return "adjust_brightness_disabled=" + !mEnableAdjustBrightness + ","
174 + "adjust_brightness_factor=" + mAdjustBrightnessFactor + ","
175 + "advertise_is_enabled=" + mAdvertiseIsEnabled + ","
176 + "animation_disabled=" + mDisableAnimation + ","
177 + "aod_disabled=" + mDisableAod + ","
178 + "datasaver_disabled=" + !mEnableDataSaver + ","
179 + "enable_night_mode=" + mEnableNightMode + ","
180 + "firewall_disabled=" + !mEnableFirewall + ","
181 + "force_all_apps_standby=" + mForceAllAppsStandby + ","
182 + "force_background_check=" + mForceBackgroundCheck + ","
183 + "fullbackup_deferred=" + mDeferFullBackup + ","
184 + "gps_mode=" + mLocationMode + ","
185 + "keyvaluebackup_deferred=" + mDeferKeyValueBackup + ","
186 + "launch_boost_disabled=" + mDisableLaunchBoost + ","
187 + "optional_sensors_disabled=" + mDisableOptionalSensors + ","
188 + "quick_doze_enabled=" + mEnableQuickDoze + ","
189 + "soundtrigger_mode=" + mSoundTriggerMode + ","
190 + "vibration_disabled=" + mDisableVibration + ","
191 + sb.toString();
192 }
193
194 /**
195 * How much to adjust the screen brightness while in Battery Saver. This will have no effect
196 * if {@link #getEnableAdjustBrightness()} is {@code false}.
197 */
198 public float getAdjustBrightnessFactor() {
199 return mAdjustBrightnessFactor;
200 }
201
202 /**
203 * Whether or not to tell the system (and other apps) that Battery Saver is currently enabled.
204 */
205 public boolean getAdvertiseIsEnabled() {
206 return mAdvertiseIsEnabled;
207 }
208
209 /** Whether or not to defer full backup while in Battery Saver. */
210 public boolean getDeferFullBackup() {
211 return mDeferFullBackup;
212 }
213
214 /** Whether or not to defer key-value backup while in Battery Saver. */
215 public boolean getDeferKeyValueBackup() {
216 return mDeferKeyValueBackup;
217 }
218
219 /**
220 * Returns the device-specific battery saver constants.
221 */
222 @NonNull
223 public Map<String, String> getDeviceSpecificSettings() {
224 return mDeviceSpecificSettings;
225 }
226
227 /** Whether or not to disable animation while in Battery Saver. */
228 public boolean getDisableAnimation() {
229 return mDisableAnimation;
230 }
231
232 /** Whether or not to disable Always On Display while in Battery Saver. */
233 public boolean getDisableAod() {
234 return mDisableAod;
235 }
236
237 /** Whether or not to disable launch boost while in Battery Saver. */
238 public boolean getDisableLaunchBoost() {
239 return mDisableLaunchBoost;
240 }
241
242 /** Whether or not to disable optional sensors while in Battery Saver. */
243 public boolean getDisableOptionalSensors() {
244 return mDisableOptionalSensors;
245 }
246
247 /**
248 * Get the SoundTrigger mode while in Battery Saver.
249 */
250 @PowerManager.SoundTriggerPowerSaveMode
251 public int getSoundTriggerMode() {
252 return mSoundTriggerMode;
253 }
254
255 /**
256 * Whether or not to disable {@link android.hardware.soundtrigger.SoundTrigger}
257 * while in Battery Saver.
258 * @deprecated Use {@link #getSoundTriggerMode()} instead.
259 */
260 @Deprecated
261 public boolean getDisableSoundTrigger() {
262 return mSoundTriggerMode == PowerManager.SOUND_TRIGGER_MODE_ALL_DISABLED;
263 }
264
265 /** Whether or not to disable vibration while in Battery Saver. */
266 public boolean getDisableVibration() {
267 return mDisableVibration;
268 }
269
270 /** Whether or not to enable brightness adjustment while in Battery Saver. */
271 public boolean getEnableAdjustBrightness() {
272 return mEnableAdjustBrightness;
273 }
274
275 /** Whether or not to enable Data Saver while in Battery Saver. */
276 public boolean getEnableDataSaver() {
277 return mEnableDataSaver;
278 }
279
280 /**
281 * Whether or not to enable network firewall rules to restrict background network use
282 * while in Battery Saver.
283 */
284 public boolean getEnableFirewall() {
285 return mEnableFirewall;
286 }
287
288 /** Whether or not to enable night mode while in Battery Saver. */
289 public boolean getEnableNightMode() {
290 return mEnableNightMode;
291 }
292
293 /** Whether or not to enable Quick Doze while in Battery Saver. */
294 public boolean getEnableQuickDoze() {
295 return mEnableQuickDoze;
296 }
297
298 /** Whether or not to force all apps to standby mode while in Battery Saver. */
299 public boolean getForceAllAppsStandby() {
300 return mForceAllAppsStandby;
301 }
302
303 /**
304 * Whether or not to force background check (disallow background services and manifest
305 * broadcast receivers) on all apps (not just apps targeting Android
306 * {@link Build.VERSION_CODES#O} and above)
307 * while in Battery Saver.
308 */
309 public boolean getForceBackgroundCheck() {
310 return mForceBackgroundCheck;
311 }
312
313 /** The location mode while in Battery Saver. */
314 public int getLocationMode() {
315 return mLocationMode;
316 }
317
318 /** Builder class for constructing {@link BatterySaverPolicyConfig} objects. */
319 public static final class Builder {
320 private float mAdjustBrightnessFactor = 1f;
321 private boolean mAdvertiseIsEnabled = false;
322 private boolean mDeferFullBackup = false;
323 private boolean mDeferKeyValueBackup = false;
324 @NonNull
325 private final ArrayMap<String, String> mDeviceSpecificSettings = new ArrayMap<>();
326 private boolean mDisableAnimation = false;
327 private boolean mDisableAod = false;
328 private boolean mDisableLaunchBoost = false;
329 private boolean mDisableOptionalSensors = false;
330 private boolean mDisableVibration = false;
331 private boolean mEnableAdjustBrightness = false;
332 private boolean mEnableDataSaver = false;
333 private boolean mEnableFirewall = false;
334 private boolean mEnableNightMode = false;
335 private boolean mEnableQuickDoze = false;
336 private boolean mForceAllAppsStandby = false;
337 private boolean mForceBackgroundCheck = false;
338 private int mLocationMode = PowerManager.LOCATION_MODE_NO_CHANGE;
339 private int mSoundTriggerMode = PowerManager.SOUND_TRIGGER_MODE_ALL_ENABLED;
340
341 public Builder() {
342 }
343
344 /**
345 * Creates a Builder prepopulated with the values from the passed in
346 * {@link BatterySaverPolicyConfig}.
347 */
348 public Builder(@NonNull BatterySaverPolicyConfig batterySaverPolicyConfig) {
349 mAdjustBrightnessFactor = batterySaverPolicyConfig.getAdjustBrightnessFactor();
350 mAdvertiseIsEnabled = batterySaverPolicyConfig.getAdvertiseIsEnabled();
351 mDeferFullBackup = batterySaverPolicyConfig.getDeferFullBackup();
352 mDeferKeyValueBackup = batterySaverPolicyConfig.getDeferKeyValueBackup();
353
354 for (String key :
355 batterySaverPolicyConfig.getDeviceSpecificSettings().keySet()) {
356 mDeviceSpecificSettings.put(key,
357 batterySaverPolicyConfig.getDeviceSpecificSettings().get(key));
358 }
359
360 mDisableAnimation = batterySaverPolicyConfig.getDisableAnimation();
361 mDisableAod = batterySaverPolicyConfig.getDisableAod();
362 mDisableLaunchBoost = batterySaverPolicyConfig.getDisableLaunchBoost();
363 mDisableOptionalSensors = batterySaverPolicyConfig.getDisableOptionalSensors();
364 mDisableVibration = batterySaverPolicyConfig.getDisableVibration();
365 mEnableAdjustBrightness = batterySaverPolicyConfig.getEnableAdjustBrightness();
366 mEnableDataSaver = batterySaverPolicyConfig.getEnableDataSaver();
367 mEnableFirewall = batterySaverPolicyConfig.getEnableFirewall();
368 mEnableNightMode = batterySaverPolicyConfig.getEnableNightMode();
369 mEnableQuickDoze = batterySaverPolicyConfig.getEnableQuickDoze();
370 mForceAllAppsStandby = batterySaverPolicyConfig.getForceAllAppsStandby();
371 mForceBackgroundCheck = batterySaverPolicyConfig.getForceBackgroundCheck();
372 mLocationMode = batterySaverPolicyConfig.getLocationMode();
373 mSoundTriggerMode = batterySaverPolicyConfig.getSoundTriggerMode();
374 }
375
376 /**
377 * Set how much to adjust the screen brightness while in Battery Saver. The value should
378 * be in the [0, 1] range, where 1 will not change the brightness. This will have no
379 * effect if {@link #setEnableAdjustBrightness(boolean)} is not called with {@code true}.
380 */
381 @NonNull
382 public Builder setAdjustBrightnessFactor(float adjustBrightnessFactor) {
383 mAdjustBrightnessFactor = adjustBrightnessFactor;
384 return this;
385 }
386
387 /**
388 * Set whether or not to tell the system (and other apps) that Battery Saver is
389 * currently enabled.
390 */
391 @NonNull
392 public Builder setAdvertiseIsEnabled(boolean advertiseIsEnabled) {
393 mAdvertiseIsEnabled = advertiseIsEnabled;
394 return this;
395 }
396
397 /** Set whether or not to defer full backup while in Battery Saver. */
398 @NonNull
399 public Builder setDeferFullBackup(boolean deferFullBackup) {
400 mDeferFullBackup = deferFullBackup;
401 return this;
402 }
403
404 /** Set whether or not to defer key-value backup while in Battery Saver. */
405 @NonNull
406 public Builder setDeferKeyValueBackup(boolean deferKeyValueBackup) {
407 mDeferKeyValueBackup = deferKeyValueBackup;
408 return this;
409 }
410
411 /**
412 * Adds a key-value pair for device-specific battery saver constants. The supported keys
413 * and values are the same as those in
414 * {@link android.provider.Settings.Global#BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS}.
415 *
416 * @throws IllegalArgumentException if the provided key is invalid (empty, null, or all
417 * whitespace)
418 */
419 @NonNull
420 public Builder addDeviceSpecificSetting(@NonNull String key, @NonNull String value) {
421 if (key == null) {
422 throw new IllegalArgumentException("Key cannot be null");
423 }
424 key = key.trim();
425 if (TextUtils.isEmpty(key)) {
426 throw new IllegalArgumentException("Key cannot be empty");
427 }
428 mDeviceSpecificSettings.put(key, TextUtils.emptyIfNull(value));
429 return this;
430 }
431
432 /** Set whether or not to disable animation while in Battery Saver. */
433 @NonNull
434 public Builder setDisableAnimation(boolean disableAnimation) {
435 mDisableAnimation = disableAnimation;
436 return this;
437 }
438
439 /** Set whether or not to disable Always On Display while in Battery Saver. */
440 @NonNull
441 public Builder setDisableAod(boolean disableAod) {
442 mDisableAod = disableAod;
443 return this;
444 }
445
446 /** Set whether or not to disable launch boost while in Battery Saver. */
447 @NonNull
448 public Builder setDisableLaunchBoost(boolean disableLaunchBoost) {
449 mDisableLaunchBoost = disableLaunchBoost;
450 return this;
451 }
452
453 /** Set whether or not to disable optional sensors while in Battery Saver. */
454 @NonNull
455 public Builder setDisableOptionalSensors(boolean disableOptionalSensors) {
456 mDisableOptionalSensors = disableOptionalSensors;
457 return this;
458 }
459
460 /**
461 * Set whether or not to disable {@link android.hardware.soundtrigger.SoundTrigger}
462 * while in Battery Saver.
463 * @deprecated Use {@link #setSoundTriggerMode(int)} instead.
464 */
465 @Deprecated
466 @NonNull
467 public Builder setDisableSoundTrigger(boolean disableSoundTrigger) {
468 if (disableSoundTrigger) {
469 mSoundTriggerMode = PowerManager.SOUND_TRIGGER_MODE_ALL_DISABLED;
470 } else {
471 mSoundTriggerMode = PowerManager.SOUND_TRIGGER_MODE_ALL_ENABLED;
472 }
473 return this;
474 }
475
476 /**
477 * Set the SoundTrigger mode while in Battery Saver.
478 */
479 @NonNull
480 public Builder setSoundTriggerMode(
481 @PowerManager.SoundTriggerPowerSaveMode int soundTriggerMode) {
482 mSoundTriggerMode = soundTriggerMode;
483 return this;
484 }
485
486 /** Set whether or not to disable vibration while in Battery Saver. */
487 @NonNull
488 public Builder setDisableVibration(boolean disableVibration) {
489 mDisableVibration = disableVibration;
490 return this;
491 }
492
493 /** Set whether or not to enable brightness adjustment while in Battery Saver. */
494 @NonNull
495 public Builder setEnableAdjustBrightness(boolean enableAdjustBrightness) {
496 mEnableAdjustBrightness = enableAdjustBrightness;
497 return this;
498 }
499
500 /** Set whether or not to enable Data Saver while in Battery Saver. */
501 @NonNull
502 public Builder setEnableDataSaver(boolean enableDataSaver) {
503 mEnableDataSaver = enableDataSaver;
504 return this;
505 }
506
507 /**
508 * Set whether or not to enable network firewall rules to restrict background network use
509 * while in Battery Saver.
510 */
511 @NonNull
512 public Builder setEnableFirewall(boolean enableFirewall) {
513 mEnableFirewall = enableFirewall;
514 return this;
515 }
516
517 /** Set whether or not to enable night mode while in Battery Saver. */
518 @NonNull
519 public Builder setEnableNightMode(boolean enableNightMode) {
520 mEnableNightMode = enableNightMode;
521 return this;
522 }
523
524 /** Set whether or not to enable Quick Doze while in Battery Saver. */
525 @NonNull
526 public Builder setEnableQuickDoze(boolean enableQuickDoze) {
527 mEnableQuickDoze = enableQuickDoze;
528 return this;
529 }
530
531 /** Set whether or not to force all apps to standby mode while in Battery Saver. */
532 @NonNull
533 public Builder setForceAllAppsStandby(boolean forceAllAppsStandby) {
534 mForceAllAppsStandby = forceAllAppsStandby;
535 return this;
536 }
537
538 /**
539 * Set whether or not to force background check (disallow background services and manifest
540 * broadcast receivers) on all apps (not just apps targeting Android
541 * {@link Build.VERSION_CODES#O} and above)
542 * while in Battery Saver.
543 */
544 @NonNull
545 public Builder setForceBackgroundCheck(boolean forceBackgroundCheck) {
546 mForceBackgroundCheck = forceBackgroundCheck;
547 return this;
548 }
549
550 /** Set the location mode while in Battery Saver. */
551 @NonNull
552 public Builder setLocationMode(@PowerManager.LocationPowerSaveMode int locationMode) {
553 mLocationMode = locationMode;
554 return this;
555 }
556
557 /**
558 * Build a {@link BatterySaverPolicyConfig} object using the set parameters. This object
559 * is immutable.
560 */
561 @NonNull
562 public BatterySaverPolicyConfig build() {
563 return new BatterySaverPolicyConfig(this);
564 }
565 }
566 }