1 /* 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 */ 16 package com.android.server.power; 17 18 import android.annotation.IntDef; 19 import android.content.ContentResolver; 20 import android.database.ContentObserver; 21 import android.net.Uri; 22 import android.os.Handler; 23 import android.provider.Settings; 24 import android.util.KeyValueListParser; 25 import android.util.Slog; 26 import android.os.PowerSaveState; 27 import com.android.internal.annotations.VisibleForTesting; 28 29 import java.io.PrintWriter; 30 import java.lang.annotation.Retention; 31 import java.lang.annotation.RetentionPolicy; 32 33 /** 34 * Class to decide whether to turn on battery saver mode for specific service 35 */ 36 public class BatterySaverPolicy extends ContentObserver { 37 @Retention(RetentionPolicy.SOURCE) 38 @IntDef({ServiceType.GPS, 39 ServiceType.VIBRATION, 40 ServiceType.ANIMATION, 41 ServiceType.FULL_BACKUP, 42 ServiceType.KEYVALUE_BACKUP, 43 ServiceType.NETWORK_FIREWALL, 44 ServiceType.SCREEN_BRIGHTNESS, 45 ServiceType.SOUND, 46 ServiceType.BATTERY_STATS, 47 ServiceType.DATA_SAVER}) 48 public @interface ServiceType { 49 int NULL = 0; 50 int GPS = 1; 51 int VIBRATION = 2; 52 int ANIMATION = 3; 53 int FULL_BACKUP = 4; 54 int KEYVALUE_BACKUP = 5; 55 int NETWORK_FIREWALL = 6; 56 int SCREEN_BRIGHTNESS = 7; 57 int SOUND = 8; 58 int BATTERY_STATS = 9; 59 int DATA_SAVER = 10; 60 } 61 62 private static final String TAG = "BatterySaverPolicy"; 63 64 // Value of batterySaverGpsMode such that GPS isn't affected by battery saver mode. 65 public static final int GPS_MODE_NO_CHANGE = 0; 66 // Value of batterySaverGpsMode such that GPS is disabled when battery saver mode 67 // is enabled and the screen is off. 68 public static final int GPS_MODE_DISABLED_WHEN_SCREEN_OFF = 1; 69 // Secure setting for GPS behavior when battery saver mode is on. 70 public static final String SECURE_KEY_GPS_MODE = "batterySaverGpsMode"; 71 72 private static final String KEY_GPS_MODE = "gps_mode"; 73 private static final String KEY_VIBRATION_DISABLED = "vibration_disabled"; 74 private static final String KEY_ANIMATION_DISABLED = "animation_disabled"; 75 private static final String KEY_SOUNDTRIGGER_DISABLED = "soundtrigger_disabled"; 76 private static final String KEY_FIREWALL_DISABLED = "firewall_disabled"; 77 private static final String KEY_ADJUST_BRIGHTNESS_DISABLED = "adjust_brightness_disabled"; 78 private static final String KEY_DATASAVER_DISABLED = "datasaver_disabled"; 79 private static final String KEY_ADJUST_BRIGHTNESS_FACTOR = "adjust_brightness_factor"; 80 private static final String KEY_FULLBACKUP_DEFERRED = "fullbackup_deferred"; 81 private static final String KEY_KEYVALUE_DEFERRED = "keyvaluebackup_deferred"; 82 83 private final KeyValueListParser mParser = new KeyValueListParser(','); 84 85 /** 86 * {@code true} if vibration is disabled in battery saver mode. 87 * 88 * @see Settings.Global#BATTERY_SAVER_CONSTANTS 89 * @see #KEY_VIBRATION_DISABLED 90 */ 91 private boolean mVibrationDisabled; 92 93 /** 94 * {@code true} if animation is disabled in battery saver mode. 95 * 96 * @see Settings.Global#BATTERY_SAVER_CONSTANTS 97 * @see #KEY_ANIMATION_DISABLED 98 */ 99 private boolean mAnimationDisabled; 100 101 /** 102 * {@code true} if sound trigger is disabled in battery saver mode 103 * in battery saver mode. 104 * 105 * @see Settings.Global#BATTERY_SAVER_CONSTANTS 106 * @see #KEY_SOUNDTRIGGER_DISABLED 107 */ 108 private boolean mSoundTriggerDisabled; 109 110 /** 111 * {@code true} if full backup is deferred in battery saver mode. 112 * 113 * @see Settings.Global#BATTERY_SAVER_CONSTANTS 114 * @see #KEY_FULLBACKUP_DEFERRED 115 */ 116 private boolean mFullBackupDeferred; 117 118 /** 119 * {@code true} if key value backup is deferred in battery saver mode. 120 * 121 * @see Settings.Global#BATTERY_SAVER_CONSTANTS 122 * @see #KEY_KEYVALUE_DEFERRED 123 */ 124 private boolean mKeyValueBackupDeferred; 125 126 /** 127 * {@code true} if network policy firewall is disabled in battery saver mode. 128 * 129 * @see Settings.Global#BATTERY_SAVER_CONSTANTS 130 * @see #KEY_FIREWALL_DISABLED 131 */ 132 private boolean mFireWallDisabled; 133 134 /** 135 * {@code true} if adjust brightness is disabled in battery saver mode. 136 * 137 * @see Settings.Global#BATTERY_SAVER_CONSTANTS 138 * @see #KEY_ADJUST_BRIGHTNESS_DISABLED 139 */ 140 private boolean mAdjustBrightnessDisabled; 141 142 /** 143 * {@code true} if data saver is disabled in battery saver mode. 144 * 145 * @see Settings.Global#BATTERY_SAVER_CONSTANTS 146 * @see #KEY_DATASAVER_DISABLED 147 */ 148 private boolean mDataSaverDisabled; 149 150 /** 151 * This is the flag to decide the gps mode in battery saver mode. 152 * 153 * @see Settings.Global#BATTERY_SAVER_CONSTANTS 154 * @see #KEY_GPS_MODE 155 */ 156 private int mGpsMode; 157 158 /** 159 * This is the flag to decide the how much to adjust the screen brightness. This is 160 * the float value from 0 to 1 where 1 means don't change brightness. 161 * 162 * @see Settings.Global#BATTERY_SAVER_CONSTANTS 163 * @see #KEY_ADJUST_BRIGHTNESS_FACTOR 164 */ 165 private float mAdjustBrightnessFactor; 166 167 private ContentResolver mContentResolver; 168 BatterySaverPolicy(Handler handler)169 public BatterySaverPolicy(Handler handler) { 170 super(handler); 171 } 172 start(ContentResolver contentResolver)173 public void start(ContentResolver contentResolver) { 174 mContentResolver = contentResolver; 175 176 mContentResolver.registerContentObserver(Settings.Global.getUriFor( 177 Settings.Global.BATTERY_SAVER_CONSTANTS), false, this); 178 onChange(true, null); 179 } 180 181 @Override onChange(boolean selfChange, Uri uri)182 public void onChange(boolean selfChange, Uri uri) { 183 final String value = Settings.Global.getString(mContentResolver, 184 Settings.Global.BATTERY_SAVER_CONSTANTS); 185 updateConstants(value); 186 } 187 188 @VisibleForTesting updateConstants(final String value)189 void updateConstants(final String value) { 190 synchronized (BatterySaverPolicy.this) { 191 try { 192 mParser.setString(value); 193 } catch (IllegalArgumentException e) { 194 Slog.e(TAG, "Bad battery saver constants"); 195 } 196 197 mVibrationDisabled = mParser.getBoolean(KEY_VIBRATION_DISABLED, true); 198 mAnimationDisabled = mParser.getBoolean(KEY_ANIMATION_DISABLED, true); 199 mSoundTriggerDisabled = mParser.getBoolean(KEY_SOUNDTRIGGER_DISABLED, true); 200 mFullBackupDeferred = mParser.getBoolean(KEY_FULLBACKUP_DEFERRED, true); 201 mKeyValueBackupDeferred = mParser.getBoolean(KEY_KEYVALUE_DEFERRED, true); 202 mFireWallDisabled = mParser.getBoolean(KEY_FIREWALL_DISABLED, false); 203 mAdjustBrightnessDisabled = mParser.getBoolean(KEY_ADJUST_BRIGHTNESS_DISABLED, false); 204 mAdjustBrightnessFactor = mParser.getFloat(KEY_ADJUST_BRIGHTNESS_FACTOR, 0.5f); 205 mDataSaverDisabled = mParser.getBoolean(KEY_DATASAVER_DISABLED, true); 206 207 // Get default value from Settings.Secure 208 final int defaultGpsMode = Settings.Secure.getInt(mContentResolver, SECURE_KEY_GPS_MODE, 209 GPS_MODE_DISABLED_WHEN_SCREEN_OFF); 210 mGpsMode = mParser.getInt(KEY_GPS_MODE, defaultGpsMode); 211 } 212 } 213 214 /** 215 * Get the {@link PowerSaveState} based on {@paramref type} and {@paramref realMode}. 216 * The result will have {@link PowerSaveState#batterySaverEnabled} and some other 217 * parameters when necessary. 218 * 219 * @param type type of the service, one of {@link ServiceType} 220 * @param realMode whether the battery saver is on by default 221 * @return State data that contains battery saver data 222 */ getBatterySaverPolicy(@erviceType int type, boolean realMode)223 public PowerSaveState getBatterySaverPolicy(@ServiceType int type, boolean realMode) { 224 synchronized (BatterySaverPolicy.this) { 225 final PowerSaveState.Builder builder = new PowerSaveState.Builder() 226 .setGlobalBatterySaverEnabled(realMode); 227 if (!realMode) { 228 return builder.setBatterySaverEnabled(realMode) 229 .build(); 230 } 231 switch (type) { 232 case ServiceType.GPS: 233 return builder.setBatterySaverEnabled(realMode) 234 .setGpsMode(mGpsMode) 235 .build(); 236 case ServiceType.ANIMATION: 237 return builder.setBatterySaverEnabled(mAnimationDisabled) 238 .build(); 239 case ServiceType.FULL_BACKUP: 240 return builder.setBatterySaverEnabled(mFullBackupDeferred) 241 .build(); 242 case ServiceType.KEYVALUE_BACKUP: 243 return builder.setBatterySaverEnabled(mKeyValueBackupDeferred) 244 .build(); 245 case ServiceType.NETWORK_FIREWALL: 246 return builder.setBatterySaverEnabled(!mFireWallDisabled) 247 .build(); 248 case ServiceType.SCREEN_BRIGHTNESS: 249 return builder.setBatterySaverEnabled(!mAdjustBrightnessDisabled) 250 .setBrightnessFactor(mAdjustBrightnessFactor) 251 .build(); 252 case ServiceType.DATA_SAVER: 253 return builder.setBatterySaverEnabled(!mDataSaverDisabled) 254 .build(); 255 case ServiceType.SOUND: 256 return builder.setBatterySaverEnabled(mSoundTriggerDisabled) 257 .build(); 258 case ServiceType.VIBRATION: 259 return builder.setBatterySaverEnabled(mVibrationDisabled) 260 .build(); 261 default: 262 return builder.setBatterySaverEnabled(realMode) 263 .build(); 264 } 265 } 266 } 267 dump(PrintWriter pw)268 public void dump(PrintWriter pw) { 269 pw.println(); 270 pw.println("Battery saver policy"); 271 pw.println(" Settings " + Settings.Global.BATTERY_SAVER_CONSTANTS); 272 pw.println(" value: " + Settings.Global.getString(mContentResolver, 273 Settings.Global.BATTERY_SAVER_CONSTANTS)); 274 275 pw.println(); 276 pw.println(" " + KEY_VIBRATION_DISABLED + "=" + mVibrationDisabled); 277 pw.println(" " + KEY_ANIMATION_DISABLED + "=" + mAnimationDisabled); 278 pw.println(" " + KEY_FULLBACKUP_DEFERRED + "=" + mFullBackupDeferred); 279 pw.println(" " + KEY_KEYVALUE_DEFERRED + "=" + mKeyValueBackupDeferred); 280 pw.println(" " + KEY_FIREWALL_DISABLED + "=" + mFireWallDisabled); 281 pw.println(" " + KEY_DATASAVER_DISABLED + "=" + mDataSaverDisabled); 282 pw.println(" " + KEY_ADJUST_BRIGHTNESS_DISABLED + "=" + mAdjustBrightnessDisabled); 283 pw.println(" " + KEY_ADJUST_BRIGHTNESS_FACTOR + "=" + mAdjustBrightnessFactor); 284 pw.println(" " + KEY_GPS_MODE + "=" + mGpsMode); 285 286 } 287 } 288