1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.os;
import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.text.TextUtils;
import android.util.ArrayMap;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
/**
* Config to set Battery Saver policy flags.
*
* @hide
*/
@SystemApi
public final class BatterySaverPolicyConfig implements Parcelable {
private final float mAdjustBrightnessFactor;
private final boolean mAdvertiseIsEnabled;
private final boolean mDeferFullBackup;
private final boolean mDeferKeyValueBackup;
@NonNull
private final Map<String, String> mDeviceSpecificSettings;
private final boolean mDisableAnimation;
private final boolean mDisableAod;
private final boolean mDisableLaunchBoost;
private final boolean mDisableOptionalSensors;
private final boolean mDisableSoundTrigger;
private final boolean mDisableVibration;
private final boolean mEnableAdjustBrightness;
private final boolean mEnableDataSaver;
private final boolean mEnableFirewall;
private final boolean mEnableNightMode;
private final boolean mEnableQuickDoze;
private final boolean mForceAllAppsStandby;
private final boolean mForceBackgroundCheck;
private final int mLocationMode;
private BatterySaverPolicyConfig(Builder in) {
mAdjustBrightnessFactor = Math.max(0, Math.min(in.mAdjustBrightnessFactor, 1f));
mAdvertiseIsEnabled = in.mAdvertiseIsEnabled;
mDeferFullBackup = in.mDeferFullBackup;
mDeferKeyValueBackup = in.mDeferKeyValueBackup;
mDeviceSpecificSettings = Collections.unmodifiableMap(
new ArrayMap<>(in.mDeviceSpecificSettings));
mDisableAnimation = in.mDisableAnimation;
mDisableAod = in.mDisableAod;
mDisableLaunchBoost = in.mDisableLaunchBoost;
mDisableOptionalSensors = in.mDisableOptionalSensors;
mDisableSoundTrigger = in.mDisableSoundTrigger;
mDisableVibration = in.mDisableVibration;
mEnableAdjustBrightness = in.mEnableAdjustBrightness;
mEnableDataSaver = in.mEnableDataSaver;
mEnableFirewall = in.mEnableFirewall;
mEnableNightMode = in.mEnableNightMode;
mEnableQuickDoze = in.mEnableQuickDoze;
mForceAllAppsStandby = in.mForceAllAppsStandby;
mForceBackgroundCheck = in.mForceBackgroundCheck;
mLocationMode = Math.max(PowerManager.MIN_LOCATION_MODE,
Math.min(in.mLocationMode, PowerManager.MAX_LOCATION_MODE));
}
private BatterySaverPolicyConfig(Parcel in) {
mAdjustBrightnessFactor = Math.max(0, Math.min(in.readFloat(), 1f));
mAdvertiseIsEnabled = in.readBoolean();
mDeferFullBackup = in.readBoolean();
mDeferKeyValueBackup = in.readBoolean();
final int size = in.readInt();
Map<String, String> deviceSpecificSettings = new ArrayMap<>(size);
for (int i = 0; i < size; ++i) {
String key = TextUtils.emptyIfNull(in.readString());
String val = TextUtils.emptyIfNull(in.readString());
if (key.trim().isEmpty()) {
continue;
}
deviceSpecificSettings.put(key, val);
}
mDeviceSpecificSettings = Collections.unmodifiableMap(deviceSpecificSettings);
mDisableAnimation = in.readBoolean();
mDisableAod = in.readBoolean();
mDisableLaunchBoost = in.readBoolean();
mDisableOptionalSensors = in.readBoolean();
mDisableSoundTrigger = in.readBoolean();
mDisableVibration = in.readBoolean();
mEnableAdjustBrightness = in.readBoolean();
mEnableDataSaver = in.readBoolean();
mEnableFirewall = in.readBoolean();
mEnableNightMode = in.readBoolean();
mEnableQuickDoze = in.readBoolean();
mForceAllAppsStandby = in.readBoolean();
mForceBackgroundCheck = in.readBoolean();
mLocationMode = Math.max(PowerManager.MIN_LOCATION_MODE,
Math.min(in.readInt(), PowerManager.MAX_LOCATION_MODE));
}
public static final @android.annotation.NonNull Creator<BatterySaverPolicyConfig> CREATOR =
new Creator<BatterySaverPolicyConfig>() {
@Override
public BatterySaverPolicyConfig createFromParcel(Parcel in) {
return new BatterySaverPolicyConfig(in);
}
@Override
public BatterySaverPolicyConfig[] newArray(int size) {
return new BatterySaverPolicyConfig[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeFloat(mAdjustBrightnessFactor);
dest.writeBoolean(mAdvertiseIsEnabled);
dest.writeBoolean(mDeferFullBackup);
dest.writeBoolean(mDeferKeyValueBackup);
final Set<Map.Entry<String, String>> entries = mDeviceSpecificSettings.entrySet();
final int size = entries.size();
dest.writeInt(size);
for (Map.Entry<String, String> entry : entries) {
dest.writeString(entry.getKey());
dest.writeString(entry.getValue());
}
dest.writeBoolean(mDisableAnimation);
dest.writeBoolean(mDisableAod);
dest.writeBoolean(mDisableLaunchBoost);
dest.writeBoolean(mDisableOptionalSensors);
dest.writeBoolean(mDisableSoundTrigger);
dest.writeBoolean(mDisableVibration);
dest.writeBoolean(mEnableAdjustBrightness);
dest.writeBoolean(mEnableDataSaver);
dest.writeBoolean(mEnableFirewall);
dest.writeBoolean(mEnableNightMode);
dest.writeBoolean(mEnableQuickDoze);
dest.writeBoolean(mForceAllAppsStandby);
dest.writeBoolean(mForceBackgroundCheck);
dest.writeInt(mLocationMode);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : mDeviceSpecificSettings.entrySet()) {
sb.append(entry.getKey()).append("=").append(entry.getValue()).append(",");
}
return "adjust_brightness_disabled=" + !mEnableAdjustBrightness + ","
+ "adjust_brightness_factor=" + mAdjustBrightnessFactor + ","
+ "advertise_is_enabled=" + mAdvertiseIsEnabled + ","
+ "animation_disabled=" + mDisableAnimation + ","
+ "aod_disabled=" + mDisableAod + ","
+ "datasaver_disabled=" + !mEnableDataSaver + ","
+ "enable_night_mode=" + mEnableNightMode + ","
+ "firewall_disabled=" + !mEnableFirewall + ","
+ "force_all_apps_standby=" + mForceAllAppsStandby + ","
+ "force_background_check=" + mForceBackgroundCheck + ","
+ "fullbackup_deferred=" + mDeferFullBackup + ","
+ "gps_mode=" + mLocationMode + ","
+ "keyvaluebackup_deferred=" + mDeferKeyValueBackup + ","
+ "launch_boost_disabled=" + mDisableLaunchBoost + ","
+ "optional_sensors_disabled=" + mDisableOptionalSensors + ","
+ "quick_doze_enabled=" + mEnableQuickDoze + ","
+ "soundtrigger_disabled=" + mDisableSoundTrigger + ","
+ "vibration_disabled=" + mDisableVibration + ","
+ sb.toString();
}
/**
* How much to adjust the screen brightness while in Battery Saver. This will have no effect
* if {@link #getEnableAdjustBrightness()} is {@code false}.
*/
public float getAdjustBrightnessFactor() {
return mAdjustBrightnessFactor;
}
/**
* Whether or not to tell the system (and other apps) that Battery Saver is currently enabled.
*/
public boolean getAdvertiseIsEnabled() {
return mAdvertiseIsEnabled;
}
/** Whether or not to defer full backup while in Battery Saver. */
public boolean getDeferFullBackup() {
return mDeferFullBackup;
}
/** Whether or not to defer key-value backup while in Battery Saver. */
public boolean getDeferKeyValueBackup() {
return mDeferKeyValueBackup;
}
/**
* Returns the device-specific battery saver constants.
*/
@NonNull
public Map<String, String> getDeviceSpecificSettings() {
return mDeviceSpecificSettings;
}
/** Whether or not to disable animation while in Battery Saver. */
public boolean getDisableAnimation() {
return mDisableAnimation;
}
/** Whether or not to disable Always On Display while in Battery Saver. */
public boolean getDisableAod() {
return mDisableAod;
}
/** Whether or not to disable launch boost while in Battery Saver. */
public boolean getDisableLaunchBoost() {
return mDisableLaunchBoost;
}
/** Whether or not to disable optional sensors while in Battery Saver. */
public boolean getDisableOptionalSensors() {
return mDisableOptionalSensors;
}
/**
* Whether or not to disable {@link android.hardware.soundtrigger.SoundTrigger}
* while in Battery Saver.
*/
public boolean getDisableSoundTrigger() {
return mDisableSoundTrigger;
}
/** Whether or not to disable vibration while in Battery Saver. */
public boolean getDisableVibration() {
return mDisableVibration;
}
/** Whether or not to enable brightness adjustment while in Battery Saver. */
public boolean getEnableAdjustBrightness() {
return mEnableAdjustBrightness;
}
/** Whether or not to enable Data Saver while in Battery Saver. */
public boolean getEnableDataSaver() {
return mEnableDataSaver;
}
/**
* Whether or not to enable network firewall rules to restrict background network use
* while in Battery Saver.
*/
public boolean getEnableFirewall() {
return mEnableFirewall;
}
/** Whether or not to enable night mode while in Battery Saver. */
public boolean getEnableNightMode() {
return mEnableNightMode;
}
/** Whether or not to enable Quick Doze while in Battery Saver. */
public boolean getEnableQuickDoze() {
return mEnableQuickDoze;
}
/** Whether or not to force all apps to standby mode while in Battery Saver. */
public boolean getForceAllAppsStandby() {
return mForceAllAppsStandby;
}
/**
* Whether or not to force background check (disallow background services and manifest
* broadcast receivers) on all apps (not just apps targeting Android
* {@link Build.VERSION_CODES#O} and above)
* while in Battery Saver.
*/
public boolean getForceBackgroundCheck() {
return mForceBackgroundCheck;
}
/** The location mode while in Battery Saver. */
public int getLocationMode() {
return mLocationMode;
}
/** Builder class for constructing {@link BatterySaverPolicyConfig} objects. */
public static final class Builder {
private float mAdjustBrightnessFactor = 1f;
private boolean mAdvertiseIsEnabled = false;
private boolean mDeferFullBackup = false;
private boolean mDeferKeyValueBackup = false;
@NonNull
private final ArrayMap<String, String> mDeviceSpecificSettings = new ArrayMap<>();
private boolean mDisableAnimation = false;
private boolean mDisableAod = false;
private boolean mDisableLaunchBoost = false;
private boolean mDisableOptionalSensors = false;
private boolean mDisableSoundTrigger = false;
private boolean mDisableVibration = false;
private boolean mEnableAdjustBrightness = false;
private boolean mEnableDataSaver = false;
private boolean mEnableFirewall = false;
private boolean mEnableNightMode = false;
private boolean mEnableQuickDoze = false;
private boolean mForceAllAppsStandby = false;
private boolean mForceBackgroundCheck = false;
private int mLocationMode = PowerManager.LOCATION_MODE_NO_CHANGE;
public Builder() {
}
/**
* Set how much to adjust the screen brightness while in Battery Saver. The value should
* be in the [0, 1] range, where 1 will not change the brightness. This will have no
* effect if {@link #setEnableAdjustBrightness(boolean)} is not called with {@code true}.
*/
@NonNull
public Builder setAdjustBrightnessFactor(float adjustBrightnessFactor) {
mAdjustBrightnessFactor = adjustBrightnessFactor;
return this;
}
/**
* Set whether or not to tell the system (and other apps) that Battery Saver is
* currently enabled.
*/
@NonNull
public Builder setAdvertiseIsEnabled(boolean advertiseIsEnabled) {
mAdvertiseIsEnabled = advertiseIsEnabled;
return this;
}
/** Set whether or not to defer full backup while in Battery Saver. */
@NonNull
public Builder setDeferFullBackup(boolean deferFullBackup) {
mDeferFullBackup = deferFullBackup;
return this;
}
/** Set whether or not to defer key-value backup while in Battery Saver. */
@NonNull
public Builder setDeferKeyValueBackup(boolean deferKeyValueBackup) {
mDeferKeyValueBackup = deferKeyValueBackup;
return this;
}
/**
* Adds a key-value pair for device-specific battery saver constants. The supported keys
* and values are the same as those in
* {@link android.provider.Settings.Global#BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS}.
*
* @throws IllegalArgumentException if the provided key is invalid (empty, null, or all
* whitespace)
*/
@NonNull
public Builder addDeviceSpecificSetting(@NonNull String key, @NonNull String value) {
if (key == null) {
throw new IllegalArgumentException("Key cannot be null");
}
key = key.trim();
if (TextUtils.isEmpty(key)) {
throw new IllegalArgumentException("Key cannot be empty");
}
mDeviceSpecificSettings.put(key, TextUtils.emptyIfNull(value));
return this;
}
/** Set whether or not to disable animation while in Battery Saver. */
@NonNull
public Builder setDisableAnimation(boolean disableAnimation) {
mDisableAnimation = disableAnimation;
return this;
}
/** Set whether or not to disable Always On Display while in Battery Saver. */
@NonNull
public Builder setDisableAod(boolean disableAod) {
mDisableAod = disableAod;
return this;
}
/** Set whether or not to disable launch boost while in Battery Saver. */
@NonNull
public Builder setDisableLaunchBoost(boolean disableLaunchBoost) {
mDisableLaunchBoost = disableLaunchBoost;
return this;
}
/** Set whether or not to disable optional sensors while in Battery Saver. */
@NonNull
public Builder setDisableOptionalSensors(boolean disableOptionalSensors) {
mDisableOptionalSensors = disableOptionalSensors;
return this;
}
/**
* Set whether or not to disable {@link android.hardware.soundtrigger.SoundTrigger}
* while in Battery Saver.
*/
@NonNull
public Builder setDisableSoundTrigger(boolean disableSoundTrigger) {
mDisableSoundTrigger = disableSoundTrigger;
return this;
}
/** Set whether or not to disable vibration while in Battery Saver. */
@NonNull
public Builder setDisableVibration(boolean disableVibration) {
mDisableVibration = disableVibration;
return this;
}
/** Set whether or not to enable brightness adjustment while in Battery Saver. */
@NonNull
public Builder setEnableAdjustBrightness(boolean enableAdjustBrightness) {
mEnableAdjustBrightness = enableAdjustBrightness;
return this;
}
/** Set whether or not to enable Data Saver while in Battery Saver. */
@NonNull
public Builder setEnableDataSaver(boolean enableDataSaver) {
mEnableDataSaver = enableDataSaver;
return this;
}
/**
* Set whether or not to enable network firewall rules to restrict background network use
* while in Battery Saver.
*/
@NonNull
public Builder setEnableFirewall(boolean enableFirewall) {
mEnableFirewall = enableFirewall;
return this;
}
/** Set whether or not to enable night mode while in Battery Saver. */
@NonNull
public Builder setEnableNightMode(boolean enableNightMode) {
mEnableNightMode = enableNightMode;
return this;
}
/** Set whether or not to enable Quick Doze while in Battery Saver. */
@NonNull
public Builder setEnableQuickDoze(boolean enableQuickDoze) {
mEnableQuickDoze = enableQuickDoze;
return this;
}
/** Set whether or not to force all apps to standby mode while in Battery Saver. */
@NonNull
public Builder setForceAllAppsStandby(boolean forceAllAppsStandby) {
mForceAllAppsStandby = forceAllAppsStandby;
return this;
}
/**
* Set whether or not to force background check (disallow background services and manifest
* broadcast receivers) on all apps (not just apps targeting Android
* {@link Build.VERSION_CODES#O} and above)
* while in Battery Saver.
*/
@NonNull
public Builder setForceBackgroundCheck(boolean forceBackgroundCheck) {
mForceBackgroundCheck = forceBackgroundCheck;
return this;
}
/** Set the location mode while in Battery Saver. */
@NonNull
public Builder setLocationMode(@PowerManager.LocationPowerSaveMode int locationMode) {
mLocationMode = locationMode;
return this;
}
/**
* Build a {@link BatterySaverPolicyConfig} object using the set parameters. This object
* is immutable.
*/
@NonNull
public BatterySaverPolicyConfig build() {
return new BatterySaverPolicyConfig(this);
}
}
}
|