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 17 package com.android.settings.fuelgauge.batterytip; 18 19 import android.content.Context; 20 import android.provider.Settings; 21 import android.util.KeyValueListParser; 22 import android.util.Log; 23 24 import androidx.annotation.VisibleForTesting; 25 26 import java.time.Duration; 27 28 /** 29 * Class to store the policy for battery tips, which comes from 30 * {@link Settings.Global} 31 */ 32 public class BatteryTipPolicy { 33 public static final String TAG = "BatteryTipPolicy"; 34 35 private static final String KEY_BATTERY_TIP_ENABLED = "battery_tip_enabled"; 36 private static final String KEY_SUMMARY_ENABLED = "summary_enabled"; 37 private static final String KEY_BATTERY_SAVER_TIP_ENABLED = "battery_saver_tip_enabled"; 38 private static final String KEY_HIGH_USAGE_ENABLED = "high_usage_enabled"; 39 private static final String KEY_HIGH_USAGE_APP_COUNT = "high_usage_app_count"; 40 private static final String KEY_HIGH_USAGE_PERIOD_MS = "high_usage_period_ms"; 41 private static final String KEY_HIGH_USAGE_BATTERY_DRAINING = "high_usage_battery_draining"; 42 private static final String KEY_APP_RESTRICTION_ENABLED = "app_restriction_enabled"; 43 private static final String KEY_APP_RESTRICTION_ACTIVE_HOUR = "app_restriction_active_hour"; 44 private static final String KEY_REDUCED_BATTERY_ENABLED = "reduced_battery_enabled"; 45 private static final String KEY_REDUCED_BATTERY_PERCENT = "reduced_battery_percent"; 46 private static final String KEY_LOW_BATTERY_ENABLED = "low_battery_enabled"; 47 private static final String KEY_LOW_BATTERY_HOUR = "low_battery_hour"; 48 private static final String KEY_DATA_HISTORY_RETAIN_DAY = "data_history_retain_day"; 49 private static final String KEY_EXCESSIVE_BG_DRAIN_PERCENTAGE = "excessive_bg_drain_percentage"; 50 51 private static final String KEY_TEST_BATTERY_SAVER_TIP = "test_battery_saver_tip"; 52 private static final String KEY_TEST_HIGH_USAGE_TIP = "test_high_usage_tip"; 53 private static final String KEY_TEST_SMART_BATTERY_TIP = "test_smart_battery_tip"; 54 private static final String KEY_TEST_LOW_BATTERY_TIP = "test_low_battery_tip"; 55 56 /** 57 * {@code true} if general battery tip is enabled 58 * 59 * @see Settings.Global#BATTERY_TIP_CONSTANTS 60 * @see #KEY_BATTERY_TIP_ENABLED 61 */ 62 public final boolean batteryTipEnabled; 63 64 /** 65 * {@code true} if summary tip is enabled 66 * 67 * @see Settings.Global#BATTERY_TIP_CONSTANTS 68 * @see #KEY_SUMMARY_ENABLED 69 */ 70 public final boolean summaryEnabled; 71 72 /** 73 * {@code true} if battery saver tip is enabled 74 * 75 * @see Settings.Global#BATTERY_TIP_CONSTANTS 76 * @see #KEY_BATTERY_SAVER_TIP_ENABLED 77 */ 78 public final boolean batterySaverTipEnabled; 79 80 /** 81 * {@code true} if high usage tip is enabled 82 * 83 * @see Settings.Global#BATTERY_TIP_CONSTANTS 84 * @see #KEY_HIGH_USAGE_ENABLED 85 */ 86 public final boolean highUsageEnabled; 87 88 /** 89 * The maximum number of apps shown in high usage 90 * 91 * @see Settings.Global#BATTERY_TIP_CONSTANTS 92 * @see #KEY_HIGH_USAGE_APP_COUNT 93 */ 94 public final int highUsageAppCount; 95 96 /** 97 * The size of the window(milliseconds) for checking if the device is being heavily used 98 * 99 * @see Settings.Global#BATTERY_TIP_CONSTANTS 100 * @see #KEY_HIGH_USAGE_PERIOD_MS 101 */ 102 public final long highUsagePeriodMs; 103 104 /** 105 * The battery draining threshold to detect whether device is heavily used. 106 * If battery drains more than {@link #highUsageBatteryDraining} in last {@link 107 * #highUsagePeriodMs}, treat device as heavily used. 108 * 109 * @see Settings.Global#BATTERY_TIP_CONSTANTS 110 * @see #KEY_HIGH_USAGE_BATTERY_DRAINING 111 */ 112 public final int highUsageBatteryDraining; 113 114 /** 115 * {@code true} if app restriction tip is enabled 116 * 117 * @see Settings.Global#BATTERY_TIP_CONSTANTS 118 * @see #KEY_APP_RESTRICTION_ENABLED 119 */ 120 public final boolean appRestrictionEnabled; 121 122 /** 123 * Period(hour) to show anomaly apps. If it is 24 hours, it means only show anomaly apps 124 * happened in last 24 hours. 125 * 126 * @see Settings.Global#BATTERY_TIP_CONSTANTS 127 * @see #KEY_APP_RESTRICTION_ACTIVE_HOUR 128 */ 129 public final int appRestrictionActiveHour; 130 131 /** 132 * {@code true} if reduced battery tip is enabled 133 * 134 * @see Settings.Global#BATTERY_TIP_CONSTANTS 135 * @see #KEY_REDUCED_BATTERY_ENABLED 136 */ 137 public final boolean reducedBatteryEnabled; 138 139 /** 140 * The percentage of reduced battery to trigger the tip(e.g. 50%) 141 * 142 * @see Settings.Global#BATTERY_TIP_CONSTANTS 143 * @see #KEY_REDUCED_BATTERY_PERCENT 144 */ 145 public final int reducedBatteryPercent; 146 147 /** 148 * {@code true} if low battery tip is enabled 149 * 150 * @see Settings.Global#BATTERY_TIP_CONSTANTS 151 * @see #KEY_LOW_BATTERY_ENABLED 152 */ 153 public final boolean lowBatteryEnabled; 154 155 /** 156 * Remaining battery hour to trigger the tip(e.g. 16 hours) 157 * 158 * @see Settings.Global#BATTERY_TIP_CONSTANTS 159 * @see #KEY_LOW_BATTERY_HOUR 160 */ 161 public final int lowBatteryHour; 162 163 /** 164 * TTL day for anomaly data stored in database 165 * 166 * @see Settings.Global#BATTERY_TIP_CONSTANTS 167 * @see #KEY_DATA_HISTORY_RETAIN_DAY 168 */ 169 public final int dataHistoryRetainDay; 170 171 /** 172 * Battery drain percentage threshold for excessive background anomaly(i.e. 10%) 173 * 174 * This is an additional check for excessive background, to check whether battery drain 175 * for an app is larger than x% 176 * 177 * @see Settings.Global#BATTERY_TIP_CONSTANTS 178 * @see #KEY_EXCESSIVE_BG_DRAIN_PERCENTAGE 179 */ 180 public final int excessiveBgDrainPercentage; 181 182 /** 183 * {@code true} if we want to test battery saver tip. 184 * 185 * @see Settings.Global#BATTERY_TIP_CONSTANTS 186 * @see #KEY_TEST_BATTERY_SAVER_TIP 187 */ 188 public final boolean testBatterySaverTip; 189 190 /** 191 * {@code true} if we want to test high usage tip. 192 * 193 * @see Settings.Global#BATTERY_TIP_CONSTANTS 194 * @see #KEY_TEST_HIGH_USAGE_TIP 195 */ 196 public final boolean testHighUsageTip; 197 198 /** 199 * {@code true} if we want to test smart battery tip. 200 * 201 * @see Settings.Global#BATTERY_TIP_CONSTANTS 202 * @see #KEY_TEST_SMART_BATTERY_TIP 203 */ 204 public final boolean testSmartBatteryTip; 205 206 /** 207 * {@code true} if we want to test low battery tip. 208 * 209 * @see Settings.Global#BATTERY_TIP_CONSTANTS 210 * @see #KEY_TEST_LOW_BATTERY_TIP 211 */ 212 public final boolean testLowBatteryTip; 213 214 private final KeyValueListParser mParser; 215 BatteryTipPolicy(Context context)216 public BatteryTipPolicy(Context context) { 217 this(context, new KeyValueListParser(',')); 218 } 219 220 @VisibleForTesting BatteryTipPolicy(Context context, KeyValueListParser parser)221 BatteryTipPolicy(Context context, KeyValueListParser parser) { 222 mParser = parser; 223 final String value = Settings.Global.getString(context.getContentResolver(), 224 Settings.Global.BATTERY_TIP_CONSTANTS); 225 226 try { 227 mParser.setString(value); 228 } catch (IllegalArgumentException e) { 229 Log.e(TAG, "Bad battery tip constants"); 230 } 231 232 batteryTipEnabled = mParser.getBoolean(KEY_BATTERY_TIP_ENABLED, true); 233 summaryEnabled = mParser.getBoolean(KEY_SUMMARY_ENABLED, false); 234 batterySaverTipEnabled = mParser.getBoolean(KEY_BATTERY_SAVER_TIP_ENABLED, true); 235 highUsageEnabled = mParser.getBoolean(KEY_HIGH_USAGE_ENABLED, true); 236 highUsageAppCount = mParser.getInt(KEY_HIGH_USAGE_APP_COUNT, 3); 237 highUsagePeriodMs = mParser.getLong(KEY_HIGH_USAGE_PERIOD_MS, 238 Duration.ofHours(2).toMillis()); 239 highUsageBatteryDraining = mParser.getInt(KEY_HIGH_USAGE_BATTERY_DRAINING, 25); 240 appRestrictionEnabled = mParser.getBoolean(KEY_APP_RESTRICTION_ENABLED, true); 241 appRestrictionActiveHour = mParser.getInt(KEY_APP_RESTRICTION_ACTIVE_HOUR, 24); 242 reducedBatteryEnabled = mParser.getBoolean(KEY_REDUCED_BATTERY_ENABLED, false); 243 reducedBatteryPercent = mParser.getInt(KEY_REDUCED_BATTERY_PERCENT, 50); 244 lowBatteryEnabled = mParser.getBoolean(KEY_LOW_BATTERY_ENABLED, true); 245 lowBatteryHour = mParser.getInt(KEY_LOW_BATTERY_HOUR, 3); 246 dataHistoryRetainDay = mParser.getInt(KEY_DATA_HISTORY_RETAIN_DAY, 30); 247 excessiveBgDrainPercentage = mParser.getInt(KEY_EXCESSIVE_BG_DRAIN_PERCENTAGE, 10); 248 249 testBatterySaverTip = mParser.getBoolean(KEY_TEST_BATTERY_SAVER_TIP, false); 250 testHighUsageTip = mParser.getBoolean(KEY_TEST_HIGH_USAGE_TIP, false); 251 testSmartBatteryTip = mParser.getBoolean(KEY_TEST_SMART_BATTERY_TIP, false); 252 testLowBatteryTip = mParser.getBoolean(KEY_TEST_LOW_BATTERY_TIP, false); 253 } 254 255 } 256