blob: 3f6ce4fa807c9d61c3982ee613ca093fcdfabd7c [file] [log] [blame]
Kweku Adams9f488e22019-01-14 16:25:08 -08001/*
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
17package android.os;
18
19import android.annotation.NonNull;
20import android.annotation.SystemApi;
21import android.text.TextUtils;
22import android.util.ArrayMap;
23
24import java.util.Collections;
25import java.util.Map;
26import java.util.Set;
27
28/**
29 * Config to set Battery Saver policy flags.
30 *
31 * @hide
32 */
33@SystemApi
34public 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;
Kweku Adams41323842019-02-05 12:16:09 -080050 private final boolean mEnableNightMode;
Kweku Adams9f488e22019-01-14 16:25:08 -080051 private final boolean mEnableQuickDoze;
52 private final boolean mForceAllAppsStandby;
53 private final boolean mForceBackgroundCheck;
Kweku Adams41323842019-02-05 12:16:09 -080054 private final int mLocationMode;
Kweku Adams9f488e22019-01-14 16:25:08 -080055
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;
Kweku Adamsc1d844a2019-03-28 16:05:00 -070061 mDeviceSpecificSettings = Collections.unmodifiableMap(
62 new ArrayMap<>(in.mDeviceSpecificSettings));
Kweku Adams9f488e22019-01-14 16:25:08 -080063 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;
Kweku Adams41323842019-02-05 12:16:09 -080072 mEnableNightMode = in.mEnableNightMode;
Kweku Adams9f488e22019-01-14 16:25:08 -080073 mEnableQuickDoze = in.mEnableQuickDoze;
74 mForceAllAppsStandby = in.mForceAllAppsStandby;
75 mForceBackgroundCheck = in.mForceBackgroundCheck;
Kweku Adams41323842019-02-05 12:16:09 -080076 mLocationMode = Math.max(PowerManager.MIN_LOCATION_MODE,
77 Math.min(in.mLocationMode, PowerManager.MAX_LOCATION_MODE));
Kweku Adams9f488e22019-01-14 16:25:08 -080078 }
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();
Kweku Adams41323842019-02-05 12:16:09 -0800107 mEnableNightMode = in.readBoolean();
Kweku Adams9f488e22019-01-14 16:25:08 -0800108 mEnableQuickDoze = in.readBoolean();
109 mForceAllAppsStandby = in.readBoolean();
110 mForceBackgroundCheck = in.readBoolean();
Kweku Adams41323842019-02-05 12:16:09 -0800111 mLocationMode = Math.max(PowerManager.MIN_LOCATION_MODE,
Kweku Adams9f488e22019-01-14 16:25:08 -0800112 Math.min(in.readInt(), PowerManager.MAX_LOCATION_MODE));
113 }
114
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700115 public static final @android.annotation.NonNull Creator<BatterySaverPolicyConfig> CREATOR =
Kweku Adams9f488e22019-01-14 16:25:08 -0800116 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);
Kweku Adams41323842019-02-05 12:16:09 -0800157 dest.writeBoolean(mEnableNightMode);
Kweku Adams9f488e22019-01-14 16:25:08 -0800158 dest.writeBoolean(mEnableQuickDoze);
159 dest.writeBoolean(mForceAllAppsStandby);
160 dest.writeBoolean(mForceBackgroundCheck);
Kweku Adams41323842019-02-05 12:16:09 -0800161 dest.writeInt(mLocationMode);
Kweku Adams9f488e22019-01-14 16:25:08 -0800162 }
163
Aurimas Liutikas4d1699d2019-08-28 13:01:05 -0700164 @NonNull
Kweku Adams9f488e22019-01-14 16:25:08 -0800165 @Override
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 + ","
Kweku Adams41323842019-02-05 12:16:09 -0800177 + "enable_night_mode=" + mEnableNightMode + ","
Kweku Adams9f488e22019-01-14 16:25:08 -0800178 + "firewall_disabled=" + !mEnableFirewall + ","
179 + "force_all_apps_standby=" + mForceAllAppsStandby + ","
180 + "force_background_check=" + mForceBackgroundCheck + ","
181 + "fullbackup_deferred=" + mDeferFullBackup + ","
Kweku Adams41323842019-02-05 12:16:09 -0800182 + "gps_mode=" + mLocationMode + ","
Kweku Adams9f488e22019-01-14 16:25:08 -0800183 + "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 */
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 */
203 public boolean getAdvertiseIsEnabled() {
204 return mAdvertiseIsEnabled;
205 }
206
207 /** Whether or not to defer full backup while in Battery Saver. */
208 public boolean getDeferFullBackup() {
209 return mDeferFullBackup;
210 }
211
212 /** Whether or not to defer key-value backup while in Battery Saver. */
213 public boolean getDeferKeyValueBackup() {
214 return mDeferKeyValueBackup;
215 }
216
217 /**
218 * Returns the device-specific battery saver constants.
219 */
220 @NonNull
221 public Map<String, String> getDeviceSpecificSettings() {
222 return mDeviceSpecificSettings;
223 }
224
225 /** Whether or not to disable animation while in Battery Saver. */
226 public boolean getDisableAnimation() {
227 return mDisableAnimation;
228 }
229
230 /** Whether or not to disable Always On Display while in Battery Saver. */
231 public boolean getDisableAod() {
232 return mDisableAod;
233 }
234
235 /** Whether or not to disable launch boost while in Battery Saver. */
236 public boolean getDisableLaunchBoost() {
237 return mDisableLaunchBoost;
238 }
239
240 /** Whether or not to disable optional sensors while in Battery Saver. */
241 public boolean getDisableOptionalSensors() {
242 return mDisableOptionalSensors;
243 }
244
Makoto Onukic458c072019-04-01 12:56:27 -0700245 /**
246 * Whether or not to disable {@link android.hardware.soundtrigger.SoundTrigger}
247 * while in Battery Saver.
248 */
Kweku Adams9f488e22019-01-14 16:25:08 -0800249 public boolean getDisableSoundTrigger() {
250 return mDisableSoundTrigger;
251 }
252
253 /** Whether or not to disable vibration while in Battery Saver. */
254 public boolean getDisableVibration() {
255 return mDisableVibration;
256 }
257
258 /** Whether or not to enable brightness adjustment while in Battery Saver. */
259 public boolean getEnableAdjustBrightness() {
260 return mEnableAdjustBrightness;
261 }
262
263 /** Whether or not to enable Data Saver while in Battery Saver. */
264 public boolean getEnableDataSaver() {
265 return mEnableDataSaver;
266 }
267
Makoto Onukic458c072019-04-01 12:56:27 -0700268 /**
269 * Whether or not to enable network firewall rules to restrict background network use
270 * while in Battery Saver.
271 */
Kweku Adams9f488e22019-01-14 16:25:08 -0800272 public boolean getEnableFirewall() {
273 return mEnableFirewall;
274 }
275
Kweku Adams41323842019-02-05 12:16:09 -0800276 /** Whether or not to enable night mode while in Battery Saver. */
277 public boolean getEnableNightMode() {
278 return mEnableNightMode;
279 }
280
Kweku Adams9f488e22019-01-14 16:25:08 -0800281 /** Whether or not to enable Quick Doze while in Battery Saver. */
282 public boolean getEnableQuickDoze() {
283 return mEnableQuickDoze;
284 }
285
286 /** Whether or not to force all apps to standby mode while in Battery Saver. */
287 public boolean getForceAllAppsStandby() {
288 return mForceAllAppsStandby;
289 }
290
Makoto Onukic458c072019-04-01 12:56:27 -0700291 /**
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 */
Kweku Adams9f488e22019-01-14 16:25:08 -0800297 public boolean getForceBackgroundCheck() {
298 return mForceBackgroundCheck;
299 }
300
Kweku Adams41323842019-02-05 12:16:09 -0800301 /** The location mode while in Battery Saver. */
302 public int getLocationMode() {
303 return mLocationMode;
Kweku Adams9f488e22019-01-14 16:25:08 -0800304 }
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;
Kweku Adams41323842019-02-05 12:16:09 -0800323 private boolean mEnableNightMode = false;
Kweku Adams9f488e22019-01-14 16:25:08 -0800324 private boolean mEnableQuickDoze = false;
325 private boolean mForceAllAppsStandby = false;
326 private boolean mForceBackgroundCheck = false;
Kweku Adams41323842019-02-05 12:16:09 -0800327 private int mLocationMode = PowerManager.LOCATION_MODE_NO_CHANGE;
Kweku Adams9f488e22019-01-14 16:25:08 -0800328
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
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
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
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
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
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
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
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
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
411 public Builder setDisableOptionalSensors(boolean disableOptionalSensors) {
412 mDisableOptionalSensors = disableOptionalSensors;
413 return this;
414 }
415
Makoto Onukic458c072019-04-01 12:56:27 -0700416 /**
417 * Set whether or not to disable {@link android.hardware.soundtrigger.SoundTrigger}
418 * while in Battery Saver.
419 */
Kweku Adams9f488e22019-01-14 16:25:08 -0800420 @NonNull
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
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
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
442 public Builder setEnableDataSaver(boolean enableDataSaver) {
443 mEnableDataSaver = enableDataSaver;
444 return this;
445 }
446
Makoto Onukic458c072019-04-01 12:56:27 -0700447 /**
448 * Set whether or not to enable network firewall rules to restrict background network use
449 * while in Battery Saver.
450 */
Kweku Adams9f488e22019-01-14 16:25:08 -0800451 @NonNull
452 public Builder setEnableFirewall(boolean enableFirewall) {
453 mEnableFirewall = enableFirewall;
454 return this;
455 }
456
Kweku Adams41323842019-02-05 12:16:09 -0800457 /** Set whether or not to enable night mode while in Battery Saver. */
458 @NonNull
459 public Builder setEnableNightMode(boolean enableNightMode) {
460 mEnableNightMode = enableNightMode;
461 return this;
462 }
463
Kweku Adams9f488e22019-01-14 16:25:08 -0800464 /** Set whether or not to enable Quick Doze while in Battery Saver. */
465 @NonNull
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
473 public Builder setForceAllAppsStandby(boolean forceAllAppsStandby) {
474 mForceAllAppsStandby = forceAllAppsStandby;
475 return this;
476 }
477
Makoto Onukic458c072019-04-01 12:56:27 -0700478 /**
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 */
Kweku Adams9f488e22019-01-14 16:25:08 -0800484 @NonNull
485 public Builder setForceBackgroundCheck(boolean forceBackgroundCheck) {
486 mForceBackgroundCheck = forceBackgroundCheck;
487 return this;
488 }
489
Kweku Adams41323842019-02-05 12:16:09 -0800490 /** Set the location mode while in Battery Saver. */
Kweku Adams9f488e22019-01-14 16:25:08 -0800491 @NonNull
Kweku Adams41323842019-02-05 12:16:09 -0800492 public Builder setLocationMode(@PowerManager.LocationPowerSaveMode int locationMode) {
493 mLocationMode = locationMode;
Kweku Adams9f488e22019-01-14 16:25:08 -0800494 return this;
495 }
496
497 /**
498 * Build a {@link BatterySaverPolicyConfig} object using the set parameters. This object
499 * is immutable.
500 */
501 @NonNull
502 public BatterySaverPolicyConfig build() {
Kweku Adams9f488e22019-01-14 16:25:08 -0800503 return new BatterySaverPolicyConfig(this);
504 }
505 }
506}