• 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.anomaly;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.support.annotation.VisibleForTesting;
22 
23 import com.android.internal.os.BatteryStatsHelper;
24 import com.android.settings.fuelgauge.anomaly.action.AnomalyAction;
25 import com.android.settings.fuelgauge.anomaly.action.ForceStopAction;
26 import com.android.settings.fuelgauge.anomaly.action.LocationCheckAction;
27 import com.android.settings.fuelgauge.anomaly.action.StopAndBackgroundCheckAction;
28 import com.android.settings.fuelgauge.anomaly.checker.AnomalyDetector;
29 import com.android.settings.fuelgauge.anomaly.checker.BluetoothScanAnomalyDetector;
30 import com.android.settings.fuelgauge.anomaly.checker.WakeLockAnomalyDetector;
31 import com.android.settings.fuelgauge.anomaly.checker.WakeupAlarmAnomalyDetector;
32 
33 import java.util.ArrayList;
34 import java.util.List;
35 
36 /**
37  * Utility class for anomaly detection
38  */
39 public class AnomalyUtils {
40     private Context mContext;
41     private static AnomalyUtils sInstance;
42 
43     @VisibleForTesting
AnomalyUtils(Context context)44     AnomalyUtils(Context context) {
45         mContext = context.getApplicationContext();
46     }
47 
getInstance(Context context)48     public static AnomalyUtils getInstance(Context context) {
49         if (sInstance == null) {
50             sInstance = new AnomalyUtils(context);
51         }
52         return sInstance;
53     }
54 
55     /**
56      * Return the corresponding {@link AnomalyAction} according to
57      * {@link com.android.settings.fuelgauge.anomaly.Anomaly}
58      *
59      * @return corresponding {@link AnomalyAction}, or null if cannot find it.
60      */
getAnomalyAction(Anomaly anomaly)61     public AnomalyAction getAnomalyAction(Anomaly anomaly) {
62         switch (anomaly.type) {
63             case Anomaly.AnomalyType.WAKE_LOCK:
64                 return new ForceStopAction(mContext);
65             case Anomaly.AnomalyType.WAKEUP_ALARM:
66                 if (anomaly.targetSdkVersion >= Build.VERSION_CODES.O
67                         || (anomaly.targetSdkVersion < Build.VERSION_CODES.O
68                                 && anomaly.backgroundRestrictionEnabled)) {
69                     return new ForceStopAction(mContext);
70                 } else {
71                     return new StopAndBackgroundCheckAction(mContext);
72                 }
73             case Anomaly.AnomalyType.BLUETOOTH_SCAN:
74                 return new LocationCheckAction(mContext);
75             default:
76                 return null;
77         }
78     }
79 
80     /**
81      * Return the corresponding {@link AnomalyDetector} according to
82      * {@link com.android.settings.fuelgauge.anomaly.Anomaly.AnomalyType}
83      *
84      * @return corresponding {@link AnomalyDetector}, or null if cannot find it.
85      */
getAnomalyDetector(@nomaly.AnomalyType int anomalyType)86     public AnomalyDetector getAnomalyDetector(@Anomaly.AnomalyType int anomalyType) {
87         switch (anomalyType) {
88             case Anomaly.AnomalyType.WAKE_LOCK:
89                 return new WakeLockAnomalyDetector(mContext);
90             case Anomaly.AnomalyType.WAKEUP_ALARM:
91                 return new WakeupAlarmAnomalyDetector(mContext);
92             case Anomaly.AnomalyType.BLUETOOTH_SCAN:
93                 return new BluetoothScanAnomalyDetector(mContext);
94             default:
95                 return null;
96         }
97     }
98 
99     /**
100      * Detect whether application with {@code targetPackageName} has anomaly. When
101      * {@code targetPackageName} is null, start detection among all the applications.
102      *
103      * @param batteryStatsHelper contains battery stats, used to detect anomaly
104      * @param policy             contains configuration about anomaly check
105      * @param targetPackageName  represents the app need to be detected
106      * @return the list of anomalies
107      */
detectAnomalies(BatteryStatsHelper batteryStatsHelper, AnomalyDetectionPolicy policy, String targetPackageName)108     public List<Anomaly> detectAnomalies(BatteryStatsHelper batteryStatsHelper,
109             AnomalyDetectionPolicy policy, String targetPackageName) {
110         final List<Anomaly> anomalies = new ArrayList<>();
111         for (@Anomaly.AnomalyType int type : Anomaly.ANOMALY_TYPE_LIST) {
112             if (policy.isAnomalyDetectorEnabled(type)) {
113                 anomalies.addAll(getAnomalyDetector(type).detectAnomalies(
114                         batteryStatsHelper, targetPackageName));
115             }
116         }
117 
118         return anomalies;
119     }
120 
121 }
122