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