blob: f23b68e28c3464fef78a9899c945f6cb0143dd47 [file] [log] [blame]
jackqdyulei455e90a2017-02-09 15:29:16 -08001/*
2 * Copyright (C) 2017 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 */
Kweku Adams7fb72a42019-01-08 16:08:49 -080016package com.android.server.power.batterysaver;
jackqdyulei455e90a2017-02-09 15:29:16 -080017
Kweku Adams9f488e22019-01-14 16:25:08 -080018import android.annotation.IntDef;
jackqdyulei455e90a2017-02-09 15:29:16 -080019import android.content.ContentResolver;
Makoto Onuki66a78122017-11-14 15:03:21 -080020import android.content.Context;
jackqdyulei455e90a2017-02-09 15:29:16 -080021import android.database.ContentObserver;
22import android.net.Uri;
Kweku Adams9f488e22019-01-14 16:25:08 -080023import android.os.BatterySaverPolicyConfig;
jackqdyulei455e90a2017-02-09 15:29:16 -080024import android.os.Handler;
Makoto Onuki57f0f552017-12-11 12:22:18 -080025import android.os.PowerManager;
Makoto Onuki2eccd022017-11-01 13:44:23 -070026import android.os.PowerManager.ServiceType;
Makoto Onuki66a78122017-11-14 15:03:21 -080027import android.os.PowerSaveState;
jackqdyulei455e90a2017-02-09 15:29:16 -080028import android.provider.Settings;
Makoto Onuki66a78122017-11-14 15:03:21 -080029import android.text.TextUtils;
30import android.util.ArrayMap;
jackqdyulei455e90a2017-02-09 15:29:16 -080031import android.util.KeyValueListParser;
32import android.util.Slog;
Makoto Onukiebd24212018-05-07 12:29:51 -070033import android.view.accessibility.AccessibilityManager;
Makoto Onuki66a78122017-11-14 15:03:21 -080034
Makoto Onuki9be01402017-11-10 13:22:26 -080035import com.android.internal.R;
Makoto Onuki66a78122017-11-14 15:03:21 -080036import com.android.internal.annotations.GuardedBy;
jackqdyulei455e90a2017-02-09 15:29:16 -080037import com.android.internal.annotations.VisibleForTesting;
Makoto Onukibd7a6252018-05-10 13:41:39 -070038import com.android.internal.os.BackgroundThread;
39import com.android.internal.util.ConcurrentUtils;
Kweku Adams7fb72a42019-01-08 16:08:49 -080040import com.android.server.power.PowerManagerService;
jackqdyulei455e90a2017-02-09 15:29:16 -080041
42import java.io.PrintWriter;
Kweku Adams9f488e22019-01-14 16:25:08 -080043import java.lang.annotation.Retention;
44import java.lang.annotation.RetentionPolicy;
Makoto Onuki66a78122017-11-14 15:03:21 -080045import java.util.ArrayList;
Makoto Onuki3aaed292017-11-15 16:31:24 -080046import java.util.List;
Kweku Adams9f488e22019-01-14 16:25:08 -080047import java.util.Map;
48import java.util.Objects;
jackqdyulei455e90a2017-02-09 15:29:16 -080049
50/**
Kweku Adams7fb72a42019-01-08 16:08:49 -080051 * Class to decide whether to turn on battery saver mode for specific services.
Makoto Onuki66a78122017-11-14 15:03:21 -080052 *
Makoto Onukibd7a6252018-05-10 13:41:39 -070053 * IMPORTANT: This class shares the power manager lock, which is very low in the lock hierarchy.
54 * Do not call out with the lock held, such as AccessibilityManager. (Settings provider is okay.)
55 *
Kweku Adams9f488e22019-01-14 16:25:08 -080056 * Test: atest com.android.server.power.batterysaver.BatterySaverPolicyTest
jackqdyulei455e90a2017-02-09 15:29:16 -080057 */
58public class BatterySaverPolicy extends ContentObserver {
jackqdyulei455e90a2017-02-09 15:29:16 -080059 private static final String TAG = "BatterySaverPolicy";
60
Kweku Adams9f488e22019-01-14 16:25:08 -080061 static final boolean DEBUG = false; // DO NOT SUBMIT WITH TRUE.
Makoto Onukice643a32017-11-20 14:31:06 -080062
jackqdyulei455e90a2017-02-09 15:29:16 -080063 private static final String KEY_GPS_MODE = "gps_mode";
64 private static final String KEY_VIBRATION_DISABLED = "vibration_disabled";
65 private static final String KEY_ANIMATION_DISABLED = "animation_disabled";
66 private static final String KEY_SOUNDTRIGGER_DISABLED = "soundtrigger_disabled";
Kweku Adams7e5c7012018-11-03 16:50:06 -070067
68 /**
69 * Disable turning on the network firewall when Battery Saver is turned on.
70 * If set to false, the firewall WILL be turned on when Battery Saver is turned on.
71 * If set to true, the firewall WILL NOT be turned on when Battery Saver is turned on.
72 */
73 private static final String KEY_ACTIVATE_FIREWALL_DISABLED = "firewall_disabled";
74
75 /**
76 * Disable turning on the special low power screen brightness dimming when Battery Saver is
77 * turned on.
78 * If set to false, the screen brightness dimming WILL be turned on by Battery Saver.
79 * If set to true, the screen brightness WILL NOT be turned on by Battery Saver.
80 */
jackqdyulei455e90a2017-02-09 15:29:16 -080081 private static final String KEY_ADJUST_BRIGHTNESS_DISABLED = "adjust_brightness_disabled";
Kweku Adams7e5c7012018-11-03 16:50:06 -070082
83 /**
84 * Disable turning on Data Saver when Battery Saver is turned on.
85 * If set to false, Data Saver WILL be turned on when Battery Saver is turned on.
86 * If set to true, Data Saver WILL NOT be turned on when Battery Saver is turned on.
87 */
88 private static final String KEY_ACTIVATE_DATASAVER_DISABLED = "datasaver_disabled";
Kweku Adams9f488e22019-01-14 16:25:08 -080089
90 /**
91 * {@code true} if the Policy should advertise to the rest of the system that battery saver
92 * is enabled. This advertising could cause other system components to change their
93 * behavior. This will not affect other policy flags and what they change.
94 */
95 private static final String KEY_ADVERTISE_IS_ENABLED = "advertise_is_enabled";
96
Makoto Onukia7d8c4d2017-11-20 15:20:16 -080097 private static final String KEY_LAUNCH_BOOST_DISABLED = "launch_boost_disabled";
jackqdyulei455e90a2017-02-09 15:29:16 -080098 private static final String KEY_ADJUST_BRIGHTNESS_FACTOR = "adjust_brightness_factor";
99 private static final String KEY_FULLBACKUP_DEFERRED = "fullbackup_deferred";
100 private static final String KEY_KEYVALUE_DEFERRED = "keyvaluebackup_deferred";
Makoto Onuki9be01402017-11-10 13:22:26 -0800101 private static final String KEY_FORCE_ALL_APPS_STANDBY = "force_all_apps_standby";
Makoto Onukie7ec72a2017-11-22 11:16:30 -0800102 private static final String KEY_FORCE_BACKGROUND_CHECK = "force_background_check";
Makoto Onukiaae89532017-11-08 14:32:03 -0800103 private static final String KEY_OPTIONAL_SENSORS_DISABLED = "optional_sensors_disabled";
Lucas Dupin92a62e52018-01-30 17:22:20 -0800104 private static final String KEY_AOD_DISABLED = "aod_disabled";
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700105 // Go into deep Doze as soon as the screen turns off.
106 private static final String KEY_QUICK_DOZE_ENABLED = "quick_doze_enabled";
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800107 private static final String KEY_ENABLE_NIGHT_MODE = "enable_night_mode";
jackqdyulei455e90a2017-02-09 15:29:16 -0800108
Makoto Onukice643a32017-11-20 14:31:06 -0800109 private static final String KEY_CPU_FREQ_INTERACTIVE = "cpufreq-i";
110 private static final String KEY_CPU_FREQ_NONINTERACTIVE = "cpufreq-n";
Makoto Onuki66a78122017-11-14 15:03:21 -0800111
Kweku Adams9f488e22019-01-14 16:25:08 -0800112 @VisibleForTesting
113 static final Policy OFF_POLICY = new Policy(
114 1f, /* adjustBrightnessFactor */
115 false, /* advertiseIsEnabled */
116 false, /* deferFullBackup */
117 false, /* deferKeyValueBackup */
118 false, /* disableAnimation */
119 false, /* disableAod */
120 false, /* disableLaunchBoost */
121 false, /* disableOptionalSensors */
122 false, /* disableSoundTrigger */
123 false, /* disableVibration */
124 false, /* enableAdjustBrightness */
125 false, /* enableDataSaver */
126 false, /* enableFireWall */
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800127 false, /* enableNightMode */
Kweku Adams9f488e22019-01-14 16:25:08 -0800128 false, /* enableQuickDoze */
129 new ArrayMap<>(), /* filesForInteractive */
130 new ArrayMap<>(), /* filesForNoninteractive */
131 false, /* forceAllAppsStandby */
132 false, /* forceBackgroundCheck */
Kweku Adams731a1032019-02-04 14:05:41 -0800133 PowerManager.LOCATION_MODE_NO_CHANGE /* locationMode */
Kweku Adams9f488e22019-01-14 16:25:08 -0800134 );
135
136 private static final Policy DEFAULT_ADAPTIVE_POLICY = OFF_POLICY;
137
138 private static final Policy DEFAULT_FULL_POLICY = new Policy(
Kweku Adams87b9ce92018-10-02 10:38:00 -0700139 0.5f, /* adjustBrightnessFactor */
Kweku Adams9f488e22019-01-14 16:25:08 -0800140 true, /* advertiseIsEnabled */
Kweku Adams87b9ce92018-10-02 10:38:00 -0700141 true, /* deferFullBackup */
142 true, /* deferKeyValueBackup */
143 false, /* disableAnimation */
144 true, /* disableAod */
145 true, /* disableLaunchBoost */
146 true, /* disableOptionalSensors */
147 true, /* disableSoundTrigger */
148 true, /* disableVibration */
149 false, /* enableAdjustBrightness */
150 false, /* enableDataSaver */
151 true, /* enableFirewall */
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800152 true, /* enableNightMode */
Kweku Adams64cdc282018-12-11 16:17:24 -0800153 true, /* enableQuickDoze */
Kweku Adams87b9ce92018-10-02 10:38:00 -0700154 new ArrayMap<>(), /* filesForInteractive */
155 new ArrayMap<>(), /* filesForNoninteractive */
156 true, /* forceAllAppsStandby */
157 true, /* forceBackgroundCheck */
Kweku Adams731a1032019-02-04 14:05:41 -0800158 PowerManager.LOCATION_MODE_ALL_DISABLED_WHEN_SCREEN_OFF /* locationMode */
Kweku Adams87b9ce92018-10-02 10:38:00 -0700159 );
160
Makoto Onukibd7a6252018-05-10 13:41:39 -0700161 private final Object mLock;
162 private final Handler mHandler;
Makoto Onukia7d8c4d2017-11-20 15:20:16 -0800163
164 @GuardedBy("mLock")
165 private String mSettings;
166
167 @GuardedBy("mLock")
168 private String mDeviceSpecificSettings;
169
170 @GuardedBy("mLock")
171 private String mDeviceSpecificSettingsSource; // For dump() only.
jackqdyulei455e90a2017-02-09 15:29:16 -0800172
Kweku Adams9f488e22019-01-14 16:25:08 -0800173 @GuardedBy("mLock")
174 private String mAdaptiveSettings;
175
176 @GuardedBy("mLock")
177 private String mAdaptiveDeviceSpecificSettings;
178
jackqdyulei455e90a2017-02-09 15:29:16 -0800179 /**
Makoto Onuki85dd0852017-12-01 09:16:48 -0800180 * A short string describing which battery saver is now enabled, which we dump in the eventlog.
181 */
182 @GuardedBy("mLock")
183 private String mEventLogKeys;
184
185 /**
Kweku Adams87b9ce92018-10-02 10:38:00 -0700186 * Whether vibration should *really* be disabled -- i.e. {@link Policy#disableVibration}
Makoto Onukiebd24212018-05-07 12:29:51 -0700187 * is true *and* {@link #mAccessibilityEnabled} is false.
188 */
189 @GuardedBy("mLock")
Kweku Adams87b9ce92018-10-02 10:38:00 -0700190 private boolean mDisableVibrationEffective;
jackqdyulei455e90a2017-02-09 15:29:16 -0800191
192 /**
Kweku Adams87b9ce92018-10-02 10:38:00 -0700193 * Whether accessibility is currently enabled or not.
jackqdyulei455e90a2017-02-09 15:29:16 -0800194 */
Makoto Onukia7d8c4d2017-11-20 15:20:16 -0800195 @GuardedBy("mLock")
Kweku Adams87b9ce92018-10-02 10:38:00 -0700196 private boolean mAccessibilityEnabled;
jackqdyulei455e90a2017-02-09 15:29:16 -0800197
Kweku Adams9f488e22019-01-14 16:25:08 -0800198 /** The current default adaptive policy. */
Makoto Onukia7d8c4d2017-11-20 15:20:16 -0800199 @GuardedBy("mLock")
Kweku Adams9f488e22019-01-14 16:25:08 -0800200 private Policy mDefaultAdaptivePolicy = DEFAULT_ADAPTIVE_POLICY;
201
202 /** The policy that will be used for adaptive battery saver. */
203 @GuardedBy("mLock")
204 private Policy mAdaptivePolicy = DEFAULT_ADAPTIVE_POLICY;
205
206 /** The policy to be used for full battery saver. */
207 @GuardedBy("mLock")
208 private Policy mFullPolicy = DEFAULT_FULL_POLICY;
209
210 @IntDef(prefix = {"POLICY_LEVEL_"}, value = {
211 POLICY_LEVEL_OFF,
212 POLICY_LEVEL_ADAPTIVE,
213 POLICY_LEVEL_FULL,
214 })
215 @Retention(RetentionPolicy.SOURCE)
216 @interface PolicyLevel {}
217
218 static final int POLICY_LEVEL_OFF = 0;
219 static final int POLICY_LEVEL_ADAPTIVE = 1;
220 static final int POLICY_LEVEL_FULL = 2;
221
222 @GuardedBy("mLock")
223 private int mPolicyLevel = POLICY_LEVEL_OFF;
Makoto Onukic7335002018-03-15 17:04:34 -0700224
Makoto Onukibd7a6252018-05-10 13:41:39 -0700225 private final Context mContext;
226 private final ContentResolver mContentResolver;
227 private final BatterySavingStats mBatterySavingStats;
Makoto Onukiebd24212018-05-07 12:29:51 -0700228
229 @GuardedBy("mLock")
Makoto Onuki3aaed292017-11-15 16:31:24 -0800230 private final List<BatterySaverPolicyListener> mListeners = new ArrayList<>();
Makoto Onuki66a78122017-11-14 15:03:21 -0800231
Makoto Onuki66a78122017-11-14 15:03:21 -0800232 public interface BatterySaverPolicyListener {
233 void onBatterySaverPolicyChanged(BatterySaverPolicy policy);
234 }
235
Makoto Onukibd7a6252018-05-10 13:41:39 -0700236 public BatterySaverPolicy(Object lock, Context context, BatterySavingStats batterySavingStats) {
237 super(BackgroundThread.getHandler());
238 mLock = lock;
239 mHandler = BackgroundThread.getHandler();
240 mContext = context;
241 mContentResolver = context.getContentResolver();
242 mBatterySavingStats = batterySavingStats;
jackqdyulei455e90a2017-02-09 15:29:16 -0800243 }
244
Makoto Onukibd7a6252018-05-10 13:41:39 -0700245 /**
246 * Called by {@link PowerManagerService#systemReady}, *with no lock held.*
247 */
248 public void systemReady() {
249 ConcurrentUtils.wtfIfLockHeld(TAG, mLock);
250
251 mContentResolver.registerContentObserver(Settings.Global.getUriFor(
252 Settings.Global.BATTERY_SAVER_CONSTANTS), false, this);
253 mContentResolver.registerContentObserver(Settings.Global.getUriFor(
Kweku Adams9f488e22019-01-14 16:25:08 -0800254 Settings.Global.BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS), false, this);
255 mContentResolver.registerContentObserver(Settings.Global.getUriFor(
256 Settings.Global.BATTERY_SAVER_ADAPTIVE_CONSTANTS), false, this);
257 mContentResolver.registerContentObserver(Settings.Global.getUriFor(
258 Settings.Global.BATTERY_SAVER_ADAPTIVE_DEVICE_SPECIFIC_CONSTANTS), false, this);
Makoto Onukibd7a6252018-05-10 13:41:39 -0700259
260 final AccessibilityManager acm = mContext.getSystemService(AccessibilityManager.class);
261
262 acm.addAccessibilityStateChangeListener((enabled) -> {
263 synchronized (mLock) {
264 mAccessibilityEnabled = enabled;
265 }
266 refreshSettings();
267 });
268 final boolean enabled = acm.isEnabled();
Makoto Onuki66a78122017-11-14 15:03:21 -0800269 synchronized (mLock) {
Makoto Onukibd7a6252018-05-10 13:41:39 -0700270 mAccessibilityEnabled = enabled;
Makoto Onuki66a78122017-11-14 15:03:21 -0800271 }
272 onChange(true, null);
273 }
274
Kweku Adams7fb72a42019-01-08 16:08:49 -0800275 @VisibleForTesting
Makoto Onuki66a78122017-11-14 15:03:21 -0800276 public void addListener(BatterySaverPolicyListener listener) {
277 synchronized (mLock) {
Kweku Adams7fb72a42019-01-08 16:08:49 -0800278 // TODO: set this in the constructor instead
Makoto Onuki66a78122017-11-14 15:03:21 -0800279 mListeners.add(listener);
280 }
281 }
282
283 @VisibleForTesting
284 String getGlobalSetting(String key) {
Makoto Onukibd7a6252018-05-10 13:41:39 -0700285 return Settings.Global.getString(mContentResolver, key);
Makoto Onuki66a78122017-11-14 15:03:21 -0800286 }
287
288 @VisibleForTesting
289 int getDeviceSpecificConfigResId() {
290 return R.string.config_batterySaverDeviceSpecificConfig;
291 }
292
jackqdyulei455e90a2017-02-09 15:29:16 -0800293 @Override
294 public void onChange(boolean selfChange, Uri uri) {
Makoto Onukiebd24212018-05-07 12:29:51 -0700295 refreshSettings();
296 }
297
298 private void refreshSettings() {
Makoto Onuki66a78122017-11-14 15:03:21 -0800299 final BatterySaverPolicyListener[] listeners;
300 synchronized (mLock) {
301 // Load the non-device-specific setting.
302 final String setting = getGlobalSetting(Settings.Global.BATTERY_SAVER_CONSTANTS);
303
304 // Load the device specific setting.
305 // We first check the global setting, and if it's empty or the string "null" is set,
306 // use the default value from config.xml.
307 String deviceSpecificSetting = getGlobalSetting(
308 Settings.Global.BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS);
309 mDeviceSpecificSettingsSource =
310 Settings.Global.BATTERY_SAVER_DEVICE_SPECIFIC_CONSTANTS;
311
312 if (TextUtils.isEmpty(deviceSpecificSetting) || "null".equals(deviceSpecificSetting)) {
313 deviceSpecificSetting =
314 mContext.getString(getDeviceSpecificConfigResId());
315 mDeviceSpecificSettingsSource = "(overlay)";
316 }
317
Kweku Adams9f488e22019-01-14 16:25:08 -0800318 final String adaptiveSetting =
319 getGlobalSetting(Settings.Global.BATTERY_SAVER_ADAPTIVE_CONSTANTS);
320 final String adaptiveDeviceSpecificSetting = getGlobalSetting(
321 Settings.Global.BATTERY_SAVER_ADAPTIVE_DEVICE_SPECIFIC_CONSTANTS);
322
323 if (!updateConstantsLocked(setting, deviceSpecificSetting,
324 adaptiveSetting, adaptiveDeviceSpecificSetting)) {
325 // Nothing of note changed.
326 return;
327 }
Makoto Onuki66a78122017-11-14 15:03:21 -0800328
Kweku Adams7fb72a42019-01-08 16:08:49 -0800329 listeners = mListeners.toArray(new BatterySaverPolicyListener[0]);
Makoto Onuki66a78122017-11-14 15:03:21 -0800330 }
331
332 // Notify the listeners.
Makoto Onukibd7a6252018-05-10 13:41:39 -0700333 mHandler.post(() -> {
334 for (BatterySaverPolicyListener listener : listeners) {
335 listener.onBatterySaverPolicyChanged(this);
336 }
337 });
jackqdyulei455e90a2017-02-09 15:29:16 -0800338 }
339
Andreas Gampea36dc622018-02-05 17:19:22 -0800340 @GuardedBy("mLock")
jackqdyulei455e90a2017-02-09 15:29:16 -0800341 @VisibleForTesting
Makoto Onuki66a78122017-11-14 15:03:21 -0800342 void updateConstantsLocked(final String setting, final String deviceSpecificSetting) {
Kweku Adams9f488e22019-01-14 16:25:08 -0800343 updateConstantsLocked(setting, deviceSpecificSetting, "", "");
344 }
345
346 /** @return true if the currently active policy changed. */
347 private boolean updateConstantsLocked(String setting, String deviceSpecificSetting,
348 String adaptiveSetting, String adaptiveDeviceSpecificSetting) {
349 setting = TextUtils.emptyIfNull(setting);
350 deviceSpecificSetting = TextUtils.emptyIfNull(deviceSpecificSetting);
351 adaptiveSetting = TextUtils.emptyIfNull(adaptiveSetting);
352 adaptiveDeviceSpecificSetting = TextUtils.emptyIfNull(adaptiveDeviceSpecificSetting);
353
354 if (setting.equals(mSettings)
355 && deviceSpecificSetting.equals(mDeviceSpecificSettings)
356 && adaptiveSetting.equals(mAdaptiveSettings)
357 && adaptiveDeviceSpecificSetting.equals(mAdaptiveDeviceSpecificSettings)) {
358 return false;
359 }
360
Makoto Onuki66a78122017-11-14 15:03:21 -0800361 mSettings = setting;
362 mDeviceSpecificSettings = deviceSpecificSetting;
Kweku Adams9f488e22019-01-14 16:25:08 -0800363 mAdaptiveSettings = adaptiveSetting;
364 mAdaptiveDeviceSpecificSettings = adaptiveDeviceSpecificSetting;
Makoto Onuki66a78122017-11-14 15:03:21 -0800365
Makoto Onukice643a32017-11-20 14:31:06 -0800366 if (DEBUG) {
367 Slog.i(TAG, "mSettings=" + mSettings);
368 Slog.i(TAG, "mDeviceSpecificSettings=" + mDeviceSpecificSettings);
Kweku Adams9f488e22019-01-14 16:25:08 -0800369 Slog.i(TAG, "mAdaptiveSettings=" + mAdaptiveSettings);
370 Slog.i(TAG, "mAdaptiveDeviceSpecificSettings=" + mAdaptiveDeviceSpecificSettings);
Makoto Onukice643a32017-11-20 14:31:06 -0800371 }
372
Kweku Adams9f488e22019-01-14 16:25:08 -0800373 boolean changed = false;
374 Policy newFullPolicy = Policy.fromSettings(setting, deviceSpecificSetting,
375 DEFAULT_FULL_POLICY);
376 if (mPolicyLevel == POLICY_LEVEL_FULL && !mFullPolicy.equals(newFullPolicy)) {
377 changed = true;
Makoto Onuki66a78122017-11-14 15:03:21 -0800378 }
Kweku Adams9f488e22019-01-14 16:25:08 -0800379 mFullPolicy = newFullPolicy;
Makoto Onuki66a78122017-11-14 15:03:21 -0800380
Kweku Adams9f488e22019-01-14 16:25:08 -0800381 mDefaultAdaptivePolicy = Policy.fromSettings(adaptiveSetting, adaptiveDeviceSpecificSetting,
382 DEFAULT_ADAPTIVE_POLICY);
383 if (mPolicyLevel == POLICY_LEVEL_ADAPTIVE
384 && !mAdaptivePolicy.equals(mDefaultAdaptivePolicy)) {
385 changed = true;
Kweku Adams87b9ce92018-10-02 10:38:00 -0700386 }
Kweku Adams9f488e22019-01-14 16:25:08 -0800387 // This will override any config set by an external source. This should be fine for now.
388 // TODO: make sure it doesn't override what's set externally
389 mAdaptivePolicy = mDefaultAdaptivePolicy;
Kweku Adams87b9ce92018-10-02 10:38:00 -0700390
Kweku Adams9f488e22019-01-14 16:25:08 -0800391 updatePolicyDependenciesLocked();
Kweku Adams87b9ce92018-10-02 10:38:00 -0700392
Kweku Adams9f488e22019-01-14 16:25:08 -0800393 return changed;
394 }
Makoto Onuki85dd0852017-12-01 09:16:48 -0800395
Kweku Adams9f488e22019-01-14 16:25:08 -0800396 @GuardedBy("mLock")
397 private void updatePolicyDependenciesLocked() {
398 final Policy currPolicy = getCurrentPolicyLocked();
399 // Update the effective vibration policy.
400 mDisableVibrationEffective = currPolicy.disableVibration
Makoto Onukiebd24212018-05-07 12:29:51 -0700401 && !mAccessibilityEnabled; // Don't disable vibration when accessibility is on.
402
Makoto Onuki85dd0852017-12-01 09:16:48 -0800403 final StringBuilder sb = new StringBuilder();
404
Kweku Adams9f488e22019-01-14 16:25:08 -0800405 if (currPolicy.forceAllAppsStandby) sb.append("A");
406 if (currPolicy.forceBackgroundCheck) sb.append("B");
Makoto Onuki85dd0852017-12-01 09:16:48 -0800407
Kweku Adams87b9ce92018-10-02 10:38:00 -0700408 if (mDisableVibrationEffective) sb.append("v");
Kweku Adams9f488e22019-01-14 16:25:08 -0800409 if (currPolicy.disableAnimation) sb.append("a");
410 if (currPolicy.disableSoundTrigger) sb.append("s");
411 if (currPolicy.deferFullBackup) sb.append("F");
412 if (currPolicy.deferKeyValueBackup) sb.append("K");
413 if (currPolicy.enableFirewall) sb.append("f");
414 if (currPolicy.enableDataSaver) sb.append("d");
415 if (currPolicy.enableAdjustBrightness) sb.append("b");
Makoto Onuki85dd0852017-12-01 09:16:48 -0800416
Kweku Adams9f488e22019-01-14 16:25:08 -0800417 if (currPolicy.disableLaunchBoost) sb.append("l");
418 if (currPolicy.disableOptionalSensors) sb.append("S");
419 if (currPolicy.disableAod) sb.append("o");
420 if (currPolicy.enableQuickDoze) sb.append("q");
Makoto Onuki85dd0852017-12-01 09:16:48 -0800421
Kweku Adams731a1032019-02-04 14:05:41 -0800422 sb.append(currPolicy.locationMode);
Makoto Onuki85dd0852017-12-01 09:16:48 -0800423
424 mEventLogKeys = sb.toString();
Kweku Adams87b9ce92018-10-02 10:38:00 -0700425 }
426
Kweku Adams9f488e22019-01-14 16:25:08 -0800427 static class Policy {
Kweku Adams87b9ce92018-10-02 10:38:00 -0700428 /**
429 * This is the flag to decide the how much to adjust the screen brightness. This is
430 * the float value from 0 to 1 where 1 means don't change brightness.
431 *
432 * @see Settings.Global#BATTERY_SAVER_CONSTANTS
433 * @see #KEY_ADJUST_BRIGHTNESS_FACTOR
434 */
435 public final float adjustBrightnessFactor;
436
437 /**
Kweku Adams9f488e22019-01-14 16:25:08 -0800438 * {@code true} if the Policy should advertise to the rest of the system that battery saver
439 * is enabled. This advertising could cause other system components to change their
440 * behavior. This will not affect other policy flags and what they change.
441 *
442 * @see Settings.Global#BATTERY_SAVER_CONSTANTS
443 * @see #KEY_ADVERTISE_IS_ENABLED
444 */
445 public final boolean advertiseIsEnabled;
446
447 /**
Kweku Adams87b9ce92018-10-02 10:38:00 -0700448 * {@code true} if full backup is deferred in battery saver mode.
449 *
450 * @see Settings.Global#BATTERY_SAVER_CONSTANTS
451 * @see #KEY_FULLBACKUP_DEFERRED
452 */
453 public final boolean deferFullBackup;
454
455 /**
456 * {@code true} if key value backup is deferred in battery saver mode.
457 *
458 * @see Settings.Global#BATTERY_SAVER_CONSTANTS
459 * @see #KEY_KEYVALUE_DEFERRED
460 */
461 public final boolean deferKeyValueBackup;
462
463 /**
464 * {@code true} if animation is disabled in battery saver mode.
465 *
466 * @see Settings.Global#BATTERY_SAVER_CONSTANTS
467 * @see #KEY_ANIMATION_DISABLED
468 */
469 public final boolean disableAnimation;
470
471 /**
472 * {@code true} if AOD is disabled in battery saver mode.
473 */
474 public final boolean disableAod;
475
476 /**
477 * {@code true} if launch boost should be disabled on battery saver.
478 */
479 public final boolean disableLaunchBoost;
480
481 /**
482 * Whether to show non-essential sensors (e.g. edge sensors) or not.
483 */
484 public final boolean disableOptionalSensors;
485
486 /**
487 * {@code true} if sound trigger is disabled in battery saver mode
488 * in battery saver mode.
489 *
490 * @see Settings.Global#BATTERY_SAVER_CONSTANTS
491 * @see #KEY_SOUNDTRIGGER_DISABLED
492 */
493 public final boolean disableSoundTrigger;
494
495 /**
496 * {@code true} if vibration is disabled in battery saver mode.
497 *
498 * @see Settings.Global#BATTERY_SAVER_CONSTANTS
499 * @see #KEY_VIBRATION_DISABLED
500 */
501 public final boolean disableVibration;
502
503 /**
504 * {@code true} if low power mode brightness adjustment should be turned on in battery saver
505 * mode.
506 *
507 * @see Settings.Global#BATTERY_SAVER_CONSTANTS
508 * @see #KEY_ADJUST_BRIGHTNESS_DISABLED
509 */
510 public final boolean enableAdjustBrightness;
511
512 /**
513 * {@code true} if data saver should be turned on in battery saver mode.
514 *
515 * @see Settings.Global#BATTERY_SAVER_CONSTANTS
516 * @see #KEY_ACTIVATE_DATASAVER_DISABLED
517 */
518 public final boolean enableDataSaver;
519
520 /**
521 * {@code true} if network policy firewall should be turned on in battery saver mode.
522 *
523 * @see Settings.Global#BATTERY_SAVER_CONSTANTS
524 * @see #KEY_ACTIVATE_FIREWALL_DISABLED
525 */
526 public final boolean enableFirewall;
527
528 /**
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800529 * Whether to enable night mode or not.
530 */
531 public final boolean enableNightMode;
532
533 /**
Kweku Adams87b9ce92018-10-02 10:38:00 -0700534 * Whether Quick Doze is enabled or not.
535 */
536 public final boolean enableQuickDoze;
537
538 /**
539 * List of [Filename -> content] that should be written when battery saver is activated
540 * and the device is interactive.
541 *
542 * We use this to change the max CPU frequencies.
543 */
544 public final ArrayMap<String, String> filesForInteractive;
545
546 /**
547 * List of [Filename -> content] that should be written when battery saver is activated
548 * and the device is non-interactive.
549 *
550 * We use this to change the max CPU frequencies.
551 */
552 public final ArrayMap<String, String> filesForNoninteractive;
553
554 /**
555 * Whether to put all apps in the stand-by mode.
556 */
557 public final boolean forceAllAppsStandby;
558
559 /**
Wei Wang8e0ad132018-11-14 21:50:17 -0800560 * Whether to force background check.
Kweku Adams87b9ce92018-10-02 10:38:00 -0700561 */
562 public final boolean forceBackgroundCheck;
563
564 /**
Kweku Adams731a1032019-02-04 14:05:41 -0800565 * This is the flag to decide the location mode in battery saver mode. This was
566 * previously called gpsMode.
Kweku Adams87b9ce92018-10-02 10:38:00 -0700567 *
568 * @see Settings.Global#BATTERY_SAVER_CONSTANTS
569 * @see #KEY_GPS_MODE
570 */
Kweku Adams731a1032019-02-04 14:05:41 -0800571 public final int locationMode;
Kweku Adams87b9ce92018-10-02 10:38:00 -0700572
Kweku Adams9f488e22019-01-14 16:25:08 -0800573 private final int mHashCode;
574
Kweku Adams87b9ce92018-10-02 10:38:00 -0700575 Policy(
576 float adjustBrightnessFactor,
Kweku Adams9f488e22019-01-14 16:25:08 -0800577 boolean advertiseIsEnabled,
Kweku Adams87b9ce92018-10-02 10:38:00 -0700578 boolean deferFullBackup,
579 boolean deferKeyValueBackup,
580 boolean disableAnimation,
581 boolean disableAod,
582 boolean disableLaunchBoost,
583 boolean disableOptionalSensors,
584 boolean disableSoundTrigger,
585 boolean disableVibration,
586 boolean enableAdjustBrightness,
587 boolean enableDataSaver,
588 boolean enableFirewall,
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800589 boolean enableNightMode,
Kweku Adams87b9ce92018-10-02 10:38:00 -0700590 boolean enableQuickDoze,
591 ArrayMap<String, String> filesForInteractive,
592 ArrayMap<String, String> filesForNoninteractive,
593 boolean forceAllAppsStandby,
594 boolean forceBackgroundCheck,
Kweku Adams731a1032019-02-04 14:05:41 -0800595 int locationMode) {
Kweku Adams87b9ce92018-10-02 10:38:00 -0700596
597 this.adjustBrightnessFactor = adjustBrightnessFactor;
Kweku Adams9f488e22019-01-14 16:25:08 -0800598 this.advertiseIsEnabled = advertiseIsEnabled;
Kweku Adams87b9ce92018-10-02 10:38:00 -0700599 this.deferFullBackup = deferFullBackup;
600 this.deferKeyValueBackup = deferKeyValueBackup;
601 this.disableAnimation = disableAnimation;
602 this.disableAod = disableAod;
603 this.disableLaunchBoost = disableLaunchBoost;
604 this.disableOptionalSensors = disableOptionalSensors;
605 this.disableSoundTrigger = disableSoundTrigger;
606 this.disableVibration = disableVibration;
607 this.enableAdjustBrightness = enableAdjustBrightness;
608 this.enableDataSaver = enableDataSaver;
609 this.enableFirewall = enableFirewall;
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800610 this.enableNightMode = enableNightMode;
Kweku Adams87b9ce92018-10-02 10:38:00 -0700611 this.enableQuickDoze = enableQuickDoze;
612 this.filesForInteractive = filesForInteractive;
613 this.filesForNoninteractive = filesForNoninteractive;
614 this.forceAllAppsStandby = forceAllAppsStandby;
615 this.forceBackgroundCheck = forceBackgroundCheck;
Kweku Adams731a1032019-02-04 14:05:41 -0800616 this.locationMode = locationMode;
Kweku Adams9f488e22019-01-14 16:25:08 -0800617
618 mHashCode = Objects.hash(
619 adjustBrightnessFactor,
620 advertiseIsEnabled,
621 deferFullBackup,
622 deferKeyValueBackup,
623 disableAnimation,
624 disableAod,
625 disableLaunchBoost,
626 disableOptionalSensors,
627 disableSoundTrigger,
628 disableVibration,
629 enableAdjustBrightness,
630 enableDataSaver,
631 enableFirewall,
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800632 enableNightMode,
Kweku Adams9f488e22019-01-14 16:25:08 -0800633 enableQuickDoze,
634 filesForInteractive,
635 filesForNoninteractive,
636 forceAllAppsStandby,
637 forceBackgroundCheck,
Kweku Adams731a1032019-02-04 14:05:41 -0800638 locationMode);
Kweku Adams9f488e22019-01-14 16:25:08 -0800639 }
640
641 static Policy fromConfig(BatterySaverPolicyConfig config) {
642 if (config == null) {
643 Slog.e(TAG, "Null config passed down to BatterySaverPolicy");
644 return OFF_POLICY;
645 }
646
647 // Device-specific parameters.
648 Map<String, String> deviceSpecificSettings = config.getDeviceSpecificSettings();
649 final String cpuFreqInteractive =
650 deviceSpecificSettings.getOrDefault(KEY_CPU_FREQ_INTERACTIVE, "");
651 final String cpuFreqNoninteractive =
652 deviceSpecificSettings.getOrDefault(KEY_CPU_FREQ_NONINTERACTIVE, "");
653
654 return new Policy(
655 config.getAdjustBrightnessFactor(),
656 config.getAdvertiseIsEnabled(),
657 config.getDeferFullBackup(),
658 config.getDeferKeyValueBackup(),
659 config.getDisableAnimation(),
660 config.getDisableAod(),
661 config.getDisableLaunchBoost(),
662 config.getDisableOptionalSensors(),
663 config.getDisableSoundTrigger(),
664 config.getDisableVibration(),
665 config.getEnableAdjustBrightness(),
666 config.getEnableDataSaver(),
667 config.getEnableFirewall(),
Kweku Adams41323842019-02-05 12:16:09 -0800668 config.getEnableNightMode(),
Kweku Adams9f488e22019-01-14 16:25:08 -0800669 config.getEnableQuickDoze(),
670 /* filesForInteractive */
671 (new CpuFrequencies()).parseString(cpuFreqInteractive).toSysFileMap(),
672 /* filesForNoninteractive */
673 (new CpuFrequencies()).parseString(cpuFreqNoninteractive).toSysFileMap(),
674 config.getForceAllAppsStandby(),
675 config.getForceBackgroundCheck(),
Kweku Adams41323842019-02-05 12:16:09 -0800676 config.getLocationMode()
Kweku Adams9f488e22019-01-14 16:25:08 -0800677 );
678 }
679
680 static Policy fromSettings(String settings, String deviceSpecificSettings) {
681 return fromSettings(settings, deviceSpecificSettings, OFF_POLICY);
682 }
683
684 static Policy fromSettings(String settings, String deviceSpecificSettings,
685 Policy defaultPolicy) {
686 final KeyValueListParser parser = new KeyValueListParser(',');
687
688 // Device-specific parameters.
689 try {
690 parser.setString(deviceSpecificSettings == null ? "" : deviceSpecificSettings);
691 } catch (IllegalArgumentException e) {
692 Slog.wtf(TAG, "Bad device specific battery saver constants: "
693 + deviceSpecificSettings);
694 }
695
696 final String cpuFreqInteractive = parser.getString(KEY_CPU_FREQ_INTERACTIVE, "");
697 final String cpuFreqNoninteractive = parser.getString(KEY_CPU_FREQ_NONINTERACTIVE, "");
698
699 // Non-device-specific parameters.
700 try {
701 parser.setString(settings == null ? "" : settings);
702 } catch (IllegalArgumentException e) {
703 Slog.wtf(TAG, "Bad battery saver constants: " + settings);
704 }
705
706 float adjustBrightnessFactor = parser.getFloat(KEY_ADJUST_BRIGHTNESS_FACTOR,
707 defaultPolicy.adjustBrightnessFactor);
708 boolean advertiseIsEnabled = parser.getBoolean(KEY_ADVERTISE_IS_ENABLED,
709 defaultPolicy.advertiseIsEnabled);
710 boolean deferFullBackup = parser.getBoolean(KEY_FULLBACKUP_DEFERRED,
711 defaultPolicy.deferFullBackup);
712 boolean deferKeyValueBackup = parser.getBoolean(KEY_KEYVALUE_DEFERRED,
713 defaultPolicy.deferKeyValueBackup);
714 boolean disableAnimation = parser.getBoolean(KEY_ANIMATION_DISABLED,
715 defaultPolicy.disableAnimation);
716 boolean disableAod = parser.getBoolean(KEY_AOD_DISABLED, defaultPolicy.disableAod);
717 boolean disableLaunchBoost = parser.getBoolean(KEY_LAUNCH_BOOST_DISABLED,
718 defaultPolicy.disableLaunchBoost);
719 boolean disableOptionalSensors = parser.getBoolean(KEY_OPTIONAL_SENSORS_DISABLED,
720 defaultPolicy.disableOptionalSensors);
721 boolean disableSoundTrigger = parser.getBoolean(KEY_SOUNDTRIGGER_DISABLED,
722 defaultPolicy.disableSoundTrigger);
723 boolean disableVibrationConfig = parser.getBoolean(KEY_VIBRATION_DISABLED,
724 defaultPolicy.disableVibration);
725 boolean enableAdjustBrightness = !parser.getBoolean(KEY_ADJUST_BRIGHTNESS_DISABLED,
726 !defaultPolicy.enableAdjustBrightness);
727 boolean enableDataSaver = !parser.getBoolean(KEY_ACTIVATE_DATASAVER_DISABLED,
728 !defaultPolicy.enableDataSaver);
729 boolean enableFirewall = !parser.getBoolean(KEY_ACTIVATE_FIREWALL_DISABLED,
730 !defaultPolicy.enableFirewall);
Kweku Adams44e62802019-03-04 17:16:02 -0800731 boolean enableNightMode = parser.getBoolean(KEY_ENABLE_NIGHT_MODE,
732 defaultPolicy.enableNightMode);
Kweku Adams9f488e22019-01-14 16:25:08 -0800733 boolean enableQuickDoze = parser.getBoolean(KEY_QUICK_DOZE_ENABLED,
734 defaultPolicy.enableQuickDoze);
735 boolean forceAllAppsStandby = parser.getBoolean(KEY_FORCE_ALL_APPS_STANDBY,
736 defaultPolicy.forceAllAppsStandby);
737 boolean forceBackgroundCheck = parser.getBoolean(KEY_FORCE_BACKGROUND_CHECK,
738 defaultPolicy.forceBackgroundCheck);
Kweku Adams731a1032019-02-04 14:05:41 -0800739 int locationMode = parser.getInt(KEY_GPS_MODE, defaultPolicy.locationMode);
Kweku Adams9f488e22019-01-14 16:25:08 -0800740
741 return new Policy(
742 adjustBrightnessFactor,
743 advertiseIsEnabled,
744 deferFullBackup,
745 deferKeyValueBackup,
746 disableAnimation,
747 disableAod,
748 disableLaunchBoost,
749 disableOptionalSensors,
750 disableSoundTrigger,
751 /* disableVibration */
752 disableVibrationConfig,
753 enableAdjustBrightness,
754 enableDataSaver,
755 enableFirewall,
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800756 enableNightMode,
Kweku Adams9f488e22019-01-14 16:25:08 -0800757 enableQuickDoze,
758 /* filesForInteractive */
759 (new CpuFrequencies()).parseString(cpuFreqInteractive).toSysFileMap(),
760 /* filesForNoninteractive */
761 (new CpuFrequencies()).parseString(cpuFreqNoninteractive).toSysFileMap(),
762 forceAllAppsStandby,
763 forceBackgroundCheck,
Kweku Adams731a1032019-02-04 14:05:41 -0800764 locationMode
Kweku Adams9f488e22019-01-14 16:25:08 -0800765 );
766 }
767
768 @Override
769 public boolean equals(Object obj) {
770 if (this == obj) return true;
771 if (!(obj instanceof Policy)) return false;
772 Policy other = (Policy) obj;
773 return Float.compare(other.adjustBrightnessFactor, adjustBrightnessFactor) == 0
774 && advertiseIsEnabled == other.advertiseIsEnabled
775 && deferFullBackup == other.deferFullBackup
776 && deferKeyValueBackup == other.deferKeyValueBackup
777 && disableAnimation == other.disableAnimation
778 && disableAod == other.disableAod
779 && disableLaunchBoost == other.disableLaunchBoost
780 && disableOptionalSensors == other.disableOptionalSensors
781 && disableSoundTrigger == other.disableSoundTrigger
782 && disableVibration == other.disableVibration
783 && enableAdjustBrightness == other.enableAdjustBrightness
784 && enableDataSaver == other.enableDataSaver
785 && enableFirewall == other.enableFirewall
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800786 && enableNightMode == other.enableNightMode
Kweku Adams9f488e22019-01-14 16:25:08 -0800787 && enableQuickDoze == other.enableQuickDoze
788 && forceAllAppsStandby == other.forceAllAppsStandby
789 && forceBackgroundCheck == other.forceBackgroundCheck
Kweku Adams731a1032019-02-04 14:05:41 -0800790 && locationMode == other.locationMode
Kweku Adams9f488e22019-01-14 16:25:08 -0800791 && filesForInteractive.equals(other.filesForInteractive)
792 && filesForNoninteractive.equals(other.filesForNoninteractive);
793 }
794
795 @Override
796 public int hashCode() {
797 return mHashCode;
Kweku Adams87b9ce92018-10-02 10:38:00 -0700798 }
jackqdyulei455e90a2017-02-09 15:29:16 -0800799 }
800
801 /**
Kweku Adams9f488e22019-01-14 16:25:08 -0800802 * Get the {@link PowerSaveState} based on the current policy level.
jackqdyulei455e90a2017-02-09 15:29:16 -0800803 * The result will have {@link PowerSaveState#batterySaverEnabled} and some other
804 * parameters when necessary.
805 *
Kweku Adams9f488e22019-01-14 16:25:08 -0800806 * @param type type of the service, one of {@link ServiceType}
jackqdyulei455e90a2017-02-09 15:29:16 -0800807 * @return State data that contains battery saver data
808 */
Kweku Adams9f488e22019-01-14 16:25:08 -0800809 public PowerSaveState getBatterySaverPolicy(@ServiceType int type) {
Makoto Onuki66a78122017-11-14 15:03:21 -0800810 synchronized (mLock) {
Kweku Adams9f488e22019-01-14 16:25:08 -0800811 final Policy currPolicy = getCurrentPolicyLocked();
jackqdyulei29c82ab2017-03-10 14:09:16 -0800812 final PowerSaveState.Builder builder = new PowerSaveState.Builder()
Kweku Adams9f488e22019-01-14 16:25:08 -0800813 .setGlobalBatterySaverEnabled(currPolicy.advertiseIsEnabled);
jackqdyulei455e90a2017-02-09 15:29:16 -0800814 switch (type) {
Kweku Adams731a1032019-02-04 14:05:41 -0800815 case ServiceType.LOCATION:
Kweku Adams9f488e22019-01-14 16:25:08 -0800816 boolean isEnabled = currPolicy.advertiseIsEnabled
Kweku Adams731a1032019-02-04 14:05:41 -0800817 || currPolicy.locationMode != PowerManager.LOCATION_MODE_NO_CHANGE;
Kweku Adams9f488e22019-01-14 16:25:08 -0800818 return builder.setBatterySaverEnabled(isEnabled)
Kweku Adams731a1032019-02-04 14:05:41 -0800819 .setLocationMode(currPolicy.locationMode)
jackqdyulei455e90a2017-02-09 15:29:16 -0800820 .build();
821 case ServiceType.ANIMATION:
Kweku Adams9f488e22019-01-14 16:25:08 -0800822 return builder.setBatterySaverEnabled(currPolicy.disableAnimation)
jackqdyulei455e90a2017-02-09 15:29:16 -0800823 .build();
824 case ServiceType.FULL_BACKUP:
Kweku Adams9f488e22019-01-14 16:25:08 -0800825 return builder.setBatterySaverEnabled(currPolicy.deferFullBackup)
jackqdyulei455e90a2017-02-09 15:29:16 -0800826 .build();
827 case ServiceType.KEYVALUE_BACKUP:
Kweku Adams9f488e22019-01-14 16:25:08 -0800828 return builder.setBatterySaverEnabled(currPolicy.deferKeyValueBackup)
jackqdyulei455e90a2017-02-09 15:29:16 -0800829 .build();
830 case ServiceType.NETWORK_FIREWALL:
Kweku Adams9f488e22019-01-14 16:25:08 -0800831 return builder.setBatterySaverEnabled(currPolicy.enableFirewall)
jackqdyulei455e90a2017-02-09 15:29:16 -0800832 .build();
833 case ServiceType.SCREEN_BRIGHTNESS:
Kweku Adams9f488e22019-01-14 16:25:08 -0800834 return builder.setBatterySaverEnabled(currPolicy.enableAdjustBrightness)
835 .setBrightnessFactor(currPolicy.adjustBrightnessFactor)
jackqdyulei455e90a2017-02-09 15:29:16 -0800836 .build();
jackqdyulei29c82ab2017-03-10 14:09:16 -0800837 case ServiceType.DATA_SAVER:
Kweku Adams9f488e22019-01-14 16:25:08 -0800838 return builder.setBatterySaverEnabled(currPolicy.enableDataSaver)
jackqdyulei29c82ab2017-03-10 14:09:16 -0800839 .build();
jackqdyulei455e90a2017-02-09 15:29:16 -0800840 case ServiceType.SOUND:
Kweku Adams9f488e22019-01-14 16:25:08 -0800841 return builder.setBatterySaverEnabled(currPolicy.disableSoundTrigger)
jackqdyulei455e90a2017-02-09 15:29:16 -0800842 .build();
843 case ServiceType.VIBRATION:
Kweku Adams87b9ce92018-10-02 10:38:00 -0700844 return builder.setBatterySaverEnabled(mDisableVibrationEffective)
jackqdyulei455e90a2017-02-09 15:29:16 -0800845 .build();
Makoto Onukie7ec72a2017-11-22 11:16:30 -0800846 case ServiceType.FORCE_ALL_APPS_STANDBY:
Kweku Adams9f488e22019-01-14 16:25:08 -0800847 return builder.setBatterySaverEnabled(currPolicy.forceAllAppsStandby)
Makoto Onukie7ec72a2017-11-22 11:16:30 -0800848 .build();
849 case ServiceType.FORCE_BACKGROUND_CHECK:
Kweku Adams9f488e22019-01-14 16:25:08 -0800850 return builder.setBatterySaverEnabled(currPolicy.forceBackgroundCheck)
Makoto Onukie7ec72a2017-11-22 11:16:30 -0800851 .build();
Kweku Adams4db6a3c2019-02-04 16:06:13 -0800852 case ServiceType.NIGHT_MODE:
853 return builder.setBatterySaverEnabled(currPolicy.enableNightMode)
854 .build();
Makoto Onukiaae89532017-11-08 14:32:03 -0800855 case ServiceType.OPTIONAL_SENSORS:
Kweku Adams9f488e22019-01-14 16:25:08 -0800856 return builder.setBatterySaverEnabled(currPolicy.disableOptionalSensors)
Makoto Onukiaae89532017-11-08 14:32:03 -0800857 .build();
Lucas Dupin92a62e52018-01-30 17:22:20 -0800858 case ServiceType.AOD:
Kweku Adams9f488e22019-01-14 16:25:08 -0800859 return builder.setBatterySaverEnabled(currPolicy.disableAod)
Lucas Dupin92a62e52018-01-30 17:22:20 -0800860 .build();
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700861 case ServiceType.QUICK_DOZE:
Kweku Adams9f488e22019-01-14 16:25:08 -0800862 return builder.setBatterySaverEnabled(currPolicy.enableQuickDoze)
Kweku Adamsb396ccf2018-09-17 16:37:15 -0700863 .build();
jackqdyulei455e90a2017-02-09 15:29:16 -0800864 default:
Kweku Adams9f488e22019-01-14 16:25:08 -0800865 return builder.setBatterySaverEnabled(currPolicy.advertiseIsEnabled)
jackqdyulei455e90a2017-02-09 15:29:16 -0800866 .build();
867 }
868 }
869 }
870
Kweku Adams9f488e22019-01-14 16:25:08 -0800871 /**
872 * Sets the current policy.
873 *
874 * @return true if the policy level was changed.
875 */
876 boolean setPolicyLevel(@PolicyLevel int level) {
877 synchronized (mLock) {
878 if (mPolicyLevel == level) {
879 return false;
880 }
881 switch (level) {
882 case POLICY_LEVEL_FULL:
883 case POLICY_LEVEL_ADAPTIVE:
884 case POLICY_LEVEL_OFF:
885 mPolicyLevel = level;
886 break;
887 default:
888 Slog.wtf(TAG, "setPolicyLevel invalid level given: " + level);
889 return false;
890 }
891 updatePolicyDependenciesLocked();
892 return true;
893 }
894 }
895
896 /** @return true if the current policy changed and the policy level is ADAPTIVE. */
897 boolean setAdaptivePolicyLocked(Policy p) {
898 if (p == null) {
899 Slog.wtf(TAG, "setAdaptivePolicy given null policy");
900 return false;
901 }
902 if (mAdaptivePolicy.equals(p)) {
903 return false;
904 }
905
906 mAdaptivePolicy = p;
907 if (mPolicyLevel == POLICY_LEVEL_ADAPTIVE) {
908 updatePolicyDependenciesLocked();
909 return true;
910 }
911 return false;
912 }
913
914 /** @return true if the current policy changed and the policy level is ADAPTIVE. */
915 boolean resetAdaptivePolicyLocked() {
916 return setAdaptivePolicyLocked(mDefaultAdaptivePolicy);
917 }
918
919 private Policy getCurrentPolicyLocked() {
920 switch (mPolicyLevel) {
921 case POLICY_LEVEL_FULL:
922 return mFullPolicy;
923 case POLICY_LEVEL_ADAPTIVE:
924 return mAdaptivePolicy;
925 case POLICY_LEVEL_OFF:
926 default:
927 return OFF_POLICY;
928 }
929 }
930
Makoto Onuki2b186fa2017-11-29 13:51:01 -0800931 public int getGpsMode() {
932 synchronized (mLock) {
Kweku Adams731a1032019-02-04 14:05:41 -0800933 return getCurrentPolicyLocked().locationMode;
Makoto Onuki2b186fa2017-11-29 13:51:01 -0800934 }
935 }
936
Makoto Onuki3aaed292017-11-15 16:31:24 -0800937 public ArrayMap<String, String> getFileValues(boolean interactive) {
Makoto Onuki66a78122017-11-14 15:03:21 -0800938 synchronized (mLock) {
Kweku Adams9f488e22019-01-14 16:25:08 -0800939 return interactive ? getCurrentPolicyLocked().filesForInteractive
940 : getCurrentPolicyLocked().filesForNoninteractive;
Makoto Onuki66a78122017-11-14 15:03:21 -0800941 }
942 }
jackqdyulei455e90a2017-02-09 15:29:16 -0800943
Makoto Onukia7d8c4d2017-11-20 15:20:16 -0800944 public boolean isLaunchBoostDisabled() {
945 synchronized (mLock) {
Kweku Adams9f488e22019-01-14 16:25:08 -0800946 return getCurrentPolicyLocked().disableLaunchBoost;
947 }
948 }
949
950 boolean shouldAdvertiseIsEnabled() {
951 synchronized (mLock) {
952 return getCurrentPolicyLocked().advertiseIsEnabled;
Makoto Onukia7d8c4d2017-11-20 15:20:16 -0800953 }
954 }
955
Makoto Onuki85dd0852017-12-01 09:16:48 -0800956 public String toEventLogString() {
957 synchronized (mLock) {
958 return mEventLogKeys;
959 }
960 }
961
Makoto Onuki66a78122017-11-14 15:03:21 -0800962 public void dump(PrintWriter pw) {
963 synchronized (mLock) {
964 pw.println();
Makoto Onukibd7a6252018-05-10 13:41:39 -0700965 mBatterySavingStats.dump(pw, "");
Makoto Onukic7335002018-03-15 17:04:34 -0700966
967 pw.println();
968 pw.println("Battery saver policy (*NOTE* they only apply when battery saver is ON):");
Makoto Onukice643a32017-11-20 14:31:06 -0800969 pw.println(" Settings: " + Settings.Global.BATTERY_SAVER_CONSTANTS);
970 pw.println(" value: " + mSettings);
971 pw.println(" Settings: " + mDeviceSpecificSettingsSource);
972 pw.println(" value: " + mDeviceSpecificSettings);
Makoto Onuki66a78122017-11-14 15:03:21 -0800973
Kweku Adams9f488e22019-01-14 16:25:08 -0800974 pw.println(" Adaptive Settings: " + Settings.Global.BATTERY_SAVER_ADAPTIVE_CONSTANTS);
975 pw.println(" value: " + mAdaptiveSettings);
976 pw.println(" Adaptive Device Specific Settings: "
977 + Settings.Global.BATTERY_SAVER_ADAPTIVE_DEVICE_SPECIFIC_CONSTANTS);
978 pw.println(" value: " + mAdaptiveDeviceSpecificSettings);
979
Makoto Onukiebd24212018-05-07 12:29:51 -0700980 pw.println(" mAccessibilityEnabled=" + mAccessibilityEnabled);
Kweku Adams9f488e22019-01-14 16:25:08 -0800981 pw.println(" mPolicyLevel=" + mPolicyLevel);
Makoto Onuki66a78122017-11-14 15:03:21 -0800982
Kweku Adams9f488e22019-01-14 16:25:08 -0800983 dumpPolicyLocked(pw, " ", "full", mFullPolicy);
984 dumpPolicyLocked(pw, " ", "default adaptive", mDefaultAdaptivePolicy);
985 dumpPolicyLocked(pw, " ", "current adaptive", mAdaptivePolicy);
Makoto Onuki66a78122017-11-14 15:03:21 -0800986 }
987 }
988
Kweku Adams9f488e22019-01-14 16:25:08 -0800989 private void dumpPolicyLocked(PrintWriter pw, String indent, String label, Policy p) {
990 pw.println();
991 pw.print(indent);
992 pw.println("Policy '" + label + "'");
993 pw.print(indent);
994 pw.println(" " + KEY_ADVERTISE_IS_ENABLED + "=" + p.advertiseIsEnabled);
995 pw.print(indent);
996 pw.println(" " + KEY_VIBRATION_DISABLED + ":config=" + p.disableVibration);
997 // mDisableVibrationEffective is based on the currently selected policy
998 pw.print(indent);
999 pw.println(" " + KEY_VIBRATION_DISABLED + ":effective=" + (p.disableVibration
1000 && !mAccessibilityEnabled));
1001 pw.print(indent);
1002 pw.println(" " + KEY_ANIMATION_DISABLED + "=" + p.disableAnimation);
1003 pw.print(indent);
1004 pw.println(" " + KEY_FULLBACKUP_DEFERRED + "=" + p.deferFullBackup);
1005 pw.print(indent);
1006 pw.println(" " + KEY_KEYVALUE_DEFERRED + "=" + p.deferKeyValueBackup);
1007 pw.print(indent);
1008 pw.println(" " + KEY_ACTIVATE_FIREWALL_DISABLED + "=" + !p.enableFirewall);
1009 pw.print(indent);
1010 pw.println(" " + KEY_ACTIVATE_DATASAVER_DISABLED + "=" + !p.enableDataSaver);
1011 pw.print(indent);
1012 pw.println(" " + KEY_LAUNCH_BOOST_DISABLED + "=" + p.disableLaunchBoost);
1013 pw.println(
1014 " " + KEY_ADJUST_BRIGHTNESS_DISABLED + "=" + !p.enableAdjustBrightness);
1015 pw.print(indent);
1016 pw.println(" " + KEY_ADJUST_BRIGHTNESS_FACTOR + "=" + p.adjustBrightnessFactor);
1017 pw.print(indent);
Kweku Adams731a1032019-02-04 14:05:41 -08001018 pw.println(" " + KEY_GPS_MODE + "=" + p.locationMode);
Kweku Adams9f488e22019-01-14 16:25:08 -08001019 pw.print(indent);
1020 pw.println(" " + KEY_FORCE_ALL_APPS_STANDBY + "=" + p.forceAllAppsStandby);
1021 pw.print(indent);
1022 pw.println(" " + KEY_FORCE_BACKGROUND_CHECK + "=" + p.forceBackgroundCheck);
1023 pw.println(
1024 " " + KEY_OPTIONAL_SENSORS_DISABLED + "=" + p.disableOptionalSensors);
1025 pw.print(indent);
1026 pw.println(" " + KEY_AOD_DISABLED + "=" + p.disableAod);
1027 pw.print(indent);
1028 pw.println(" " + KEY_SOUNDTRIGGER_DISABLED + "=" + p.disableSoundTrigger);
1029 pw.print(indent);
1030 pw.println(" " + KEY_QUICK_DOZE_ENABLED + "=" + p.enableQuickDoze);
Kweku Adams4db6a3c2019-02-04 16:06:13 -08001031 pw.print(indent);
1032 pw.println(" " + KEY_ENABLE_NIGHT_MODE + "=" + p.enableNightMode);
Kweku Adams9f488e22019-01-14 16:25:08 -08001033
1034 pw.print(" Interactive File values:\n");
1035 dumpMap(pw, " ", p.filesForInteractive);
1036 pw.println();
1037
1038 pw.print(" Noninteractive File values:\n");
1039 dumpMap(pw, " ", p.filesForNoninteractive);
1040 }
1041
Makoto Onuki66a78122017-11-14 15:03:21 -08001042 private void dumpMap(PrintWriter pw, String prefix, ArrayMap<String, String> map) {
1043 if (map == null) {
1044 return;
1045 }
1046 final int size = map.size();
1047 for (int i = 0; i < size; i++) {
1048 pw.print(prefix);
1049 pw.print(map.keyAt(i));
1050 pw.print(": '");
1051 pw.print(map.valueAt(i));
1052 pw.println("'");
1053 }
jackqdyulei455e90a2017-02-09 15:29:16 -08001054 }
Makoto Onukibd7a6252018-05-10 13:41:39 -07001055
1056 @VisibleForTesting
1057 public void setAccessibilityEnabledForTest(boolean enabled) {
1058 synchronized (mLock) {
1059 mAccessibilityEnabled = enabled;
Kweku Adams9f488e22019-01-14 16:25:08 -08001060 updatePolicyDependenciesLocked();
Makoto Onukibd7a6252018-05-10 13:41:39 -07001061 }
1062 }
jackqdyulei455e90a2017-02-09 15:29:16 -08001063}