2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.fuelgauge.anomaly;
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;
27 * Class to store the policy for anomaly detection, which comes from
28 * {@link android.provider.Settings.Global}
30 public class AnomalyDetectionPolicy {
31 public static final String TAG = "AnomalyDetectionPolicy";
34 static final String KEY_ANOMALY_DETECTION_ENABLED = "anomaly_detection_enabled";
36 static final String KEY_WAKELOCK_DETECTION_ENABLED = "wakelock_enabled";
38 static final String KEY_WAKEUP_ALARM_DETECTION_ENABLED = "wakeup_alarm_enabled";
40 static final String KEY_BLUETOOTH_SCAN_DETECTION_ENABLED = "bluetooth_scan_enabled";
42 static final String KEY_WAKELOCK_THRESHOLD = "wakelock_threshold";
44 static final String KEY_WAKEUP_ALARM_THRESHOLD = "wakeup_alarm_threshold";
46 static final String KEY_BLUETOOTH_SCAN_THRESHOLD = "bluetooth_scan_threshold";
49 * {@code true} if general anomaly detection is enabled
51 * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
52 * @see #KEY_ANOMALY_DETECTION_ENABLED
54 final boolean anomalyDetectionEnabled;
57 * {@code true} if wakelock anomaly detection is enabled
59 * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
60 * @see #KEY_WAKELOCK_DETECTION_ENABLED
62 final boolean wakeLockDetectionEnabled;
65 * {@code true} if wakeup alarm detection is enabled
67 * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
68 * @see #KEY_WAKEUP_ALARM_DETECTION_ENABLED
70 final boolean wakeupAlarmDetectionEnabled;
73 * {@code true} if bluetooth scanning detection is enabled
75 * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
76 * @see #KEY_BLUETOOTH_SCAN_THRESHOLD
78 final boolean bluetoothScanDetectionEnabled;
81 * Threshold for wakelock time in milli seconds
83 * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
84 * @see #KEY_WAKELOCK_THRESHOLD
86 public final long wakeLockThreshold;
89 * Threshold for wakeup alarm count per hour
91 * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
92 * @see #KEY_WAKEUP_ALARM_THRESHOLD
94 public final long wakeupAlarmThreshold;
97 * Threshold for bluetooth unoptimized scanning time in milli seconds
99 * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
100 * @see #KEY_BLUETOOTH_SCAN_THRESHOLD
102 public final long bluetoothScanThreshold;
104 private final KeyValueListParserWrapper mParserWrapper;
106 public AnomalyDetectionPolicy(Context context) {
107 this(context, new KeyValueListParserWrapperImpl(new KeyValueListParser(',')));
111 AnomalyDetectionPolicy(Context context, KeyValueListParserWrapper parserWrapper) {
112 mParserWrapper = parserWrapper;
113 final String value = Settings.Global.getString(context.getContentResolver(),
114 Settings.Global.ANOMALY_DETECTION_CONSTANTS);
117 mParserWrapper.setString(value);
118 } catch (IllegalArgumentException e) {
119 Log.e(TAG, "Bad anomaly detection constants");
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,
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);
135 public boolean isAnomalyDetectionEnabled() {
136 return anomalyDetectionEnabled;
139 public boolean isAnomalyDetectorEnabled(@Anomaly.AnomalyType int 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;
148 return false; // Disabled when no this type