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