OSDN Git Service

Turn off some anomaly detectors by default
[android-x86/packages-apps-Settings.git] / src / com / android / settings / fuelgauge / anomaly / AnomalyDetectionPolicy.java
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.anomaly;
18
19 import android.content.Context;
20 import android.provider.Settings;
21 import android.support.annotation.VisibleForTesting;
22 import android.text.format.DateUtils;
23 import android.util.KeyValueListParser;
24 import android.util.Log;
25
26 /**
27  * Class to store the policy for anomaly detection, which comes from
28  * {@link android.provider.Settings.Global}
29  */
30 public class AnomalyDetectionPolicy {
31     public static final String TAG = "AnomalyDetectionPolicy";
32
33     @VisibleForTesting
34     static final String KEY_ANOMALY_DETECTION_ENABLED = "anomaly_detection_enabled";
35     @VisibleForTesting
36     static final String KEY_WAKELOCK_DETECTION_ENABLED = "wakelock_enabled";
37     @VisibleForTesting
38     static final String KEY_WAKEUP_ALARM_DETECTION_ENABLED = "wakeup_alarm_enabled";
39     @VisibleForTesting
40     static final String KEY_BLUETOOTH_SCAN_DETECTION_ENABLED = "bluetooth_scan_enabled";
41     @VisibleForTesting
42     static final String KEY_WAKELOCK_THRESHOLD = "wakelock_threshold";
43     @VisibleForTesting
44     static final String KEY_WAKEUP_ALARM_THRESHOLD = "wakeup_alarm_threshold";
45     @VisibleForTesting
46     static final String KEY_BLUETOOTH_SCAN_THRESHOLD = "bluetooth_scan_threshold";
47
48     /**
49      * {@code true} if general anomaly detection is enabled
50      *
51      * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
52      * @see #KEY_ANOMALY_DETECTION_ENABLED
53      */
54     final boolean anomalyDetectionEnabled;
55
56     /**
57      * {@code true} if wakelock anomaly detection is enabled
58      *
59      * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
60      * @see #KEY_WAKELOCK_DETECTION_ENABLED
61      */
62     final boolean wakeLockDetectionEnabled;
63
64     /**
65      * {@code true} if wakeup alarm detection is enabled
66      *
67      * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
68      * @see #KEY_WAKEUP_ALARM_DETECTION_ENABLED
69      */
70     final boolean wakeupAlarmDetectionEnabled;
71
72     /**
73      * {@code true} if bluetooth scanning detection is enabled
74      *
75      * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
76      * @see #KEY_BLUETOOTH_SCAN_THRESHOLD
77      */
78     final boolean bluetoothScanDetectionEnabled;
79
80     /**
81      * Threshold for wakelock time in milli seconds
82      *
83      * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
84      * @see #KEY_WAKELOCK_THRESHOLD
85      */
86     public final long wakeLockThreshold;
87
88     /**
89      * Threshold for wakeup alarm count per hour
90      *
91      * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
92      * @see #KEY_WAKEUP_ALARM_THRESHOLD
93      */
94     public final long wakeupAlarmThreshold;
95
96     /**
97      * Threshold for bluetooth unoptimized scanning time in milli seconds
98      *
99      * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
100      * @see #KEY_BLUETOOTH_SCAN_THRESHOLD
101      */
102     public final long bluetoothScanThreshold;
103
104     private final KeyValueListParserWrapper mParserWrapper;
105
106     public AnomalyDetectionPolicy(Context context) {
107         this(context, new KeyValueListParserWrapperImpl(new KeyValueListParser(',')));
108     }
109
110     @VisibleForTesting
111     AnomalyDetectionPolicy(Context context, KeyValueListParserWrapper parserWrapper) {
112         mParserWrapper = parserWrapper;
113         final String value = Settings.Global.getString(context.getContentResolver(),
114                 Settings.Global.ANOMALY_DETECTION_CONSTANTS);
115
116         try {
117             mParserWrapper.setString(value);
118         } catch (IllegalArgumentException e) {
119             Log.e(TAG, "Bad anomaly detection constants");
120         }
121
122         anomalyDetectionEnabled = mParserWrapper.getBoolean(KEY_ANOMALY_DETECTION_ENABLED, true);
123         wakeLockDetectionEnabled = mParserWrapper.getBoolean(KEY_WAKELOCK_DETECTION_ENABLED, true);
124         wakeupAlarmDetectionEnabled = mParserWrapper.getBoolean(KEY_WAKEUP_ALARM_DETECTION_ENABLED,
125                 false);
126         bluetoothScanDetectionEnabled = mParserWrapper.getBoolean(
127                 KEY_BLUETOOTH_SCAN_DETECTION_ENABLED, false);
128         wakeLockThreshold = mParserWrapper.getLong(KEY_WAKELOCK_THRESHOLD,
129                 DateUtils.HOUR_IN_MILLIS);
130         wakeupAlarmThreshold = mParserWrapper.getLong(KEY_WAKEUP_ALARM_THRESHOLD, 60);
131         bluetoothScanThreshold = mParserWrapper.getLong(KEY_BLUETOOTH_SCAN_THRESHOLD,
132                 30 * DateUtils.MINUTE_IN_MILLIS);
133     }
134
135     public boolean isAnomalyDetectionEnabled() {
136         return anomalyDetectionEnabled;
137     }
138
139     public boolean isAnomalyDetectorEnabled(@Anomaly.AnomalyType int type) {
140         switch (type) {
141             case Anomaly.AnomalyType.WAKE_LOCK:
142                 return wakeLockDetectionEnabled;
143             case Anomaly.AnomalyType.WAKEUP_ALARM:
144                 return wakeupAlarmDetectionEnabled;
145             case Anomaly.AnomalyType.BLUETOOTH_SCAN:
146                 return bluetoothScanDetectionEnabled;
147             default:
148                 return false; // Disabled when no this type
149         }
150     }
151 }