diff --git a/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicy.java b/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicy.java index eacb31f910d..603bca02e8f 100644 --- a/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicy.java +++ b/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicy.java @@ -35,7 +35,11 @@ public class AnomalyDetectionPolicy { @VisibleForTesting static final String KEY_WAKELOCK_DETECTION_ENABLED = "wakelock_enabled"; @VisibleForTesting + static final String KEY_WAKEUP_ALARM_DETECTION_ENABLED = "wakeup_alarm_enabled"; + @VisibleForTesting static final String KEY_WAKELOCK_THRESHOLD = "wakelock_threshold"; + @VisibleForTesting + static final String KEY_WAKEUP_ALARM_THRESHOLD = "wakeup_alarm_threshold"; /** * {@code true} if general anomaly detection is enabled @@ -53,6 +57,14 @@ public class AnomalyDetectionPolicy { */ public final boolean wakeLockDetectionEnabled; + /** + * {@code true} if wakeup alarm detection is enabled + * + * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS + * @see #KEY_WAKEUP_ALARM_DETECTION_ENABLED + */ + public final boolean wakeupAlarmDetectionEnabled; + /** * Threshold for wakelock time in milli seconds * @@ -61,6 +73,14 @@ public class AnomalyDetectionPolicy { */ public final long wakeLockThreshold; + /** + * Threshold for wakeup alarm count per hour + * + * @see Settings.Global#ANOMALY_DETECTION_CONSTANTS + * @see #KEY_WAKEUP_ALARM_THRESHOLD + */ + public final long wakeupAlarmThreshold; + private final KeyValueListParserWrapper mParserWrapper; public AnomalyDetectionPolicy(Context context) { @@ -81,8 +101,11 @@ public class AnomalyDetectionPolicy { anomalyDetectionEnabled = mParserWrapper.getBoolean(KEY_ANOMALY_DETECTION_ENABLED, true); wakeLockDetectionEnabled = mParserWrapper.getBoolean(KEY_WAKELOCK_DETECTION_ENABLED, true); + wakeupAlarmDetectionEnabled = mParserWrapper.getBoolean(KEY_WAKEUP_ALARM_DETECTION_ENABLED, + true); wakeLockThreshold = mParserWrapper.getLong(KEY_WAKELOCK_THRESHOLD, DateUtils.HOUR_IN_MILLIS); + wakeupAlarmThreshold = mParserWrapper.getLong(KEY_WAKEUP_ALARM_THRESHOLD, 60); } } diff --git a/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetector.java b/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetector.java index 82c009ed296..b97fd46d1fb 100644 --- a/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetector.java +++ b/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetector.java @@ -39,15 +39,20 @@ import java.util.List; */ public class WakeupAlarmAnomalyDetector implements AnomalyDetector { private static final String TAG = "WakeupAlarmAnomalyDetector"; - //TODO: add this threshold into AnomalyDetectionPolicy - private static final int WAKEUP_ALARM_THRESHOLD = 60; - private Context mContext; @VisibleForTesting BatteryUtils mBatteryUtils; + private long mWakeupAlarmThreshold; + private Context mContext; public WakeupAlarmAnomalyDetector(Context context) { + this(context, new AnomalyDetectionPolicy(context)); + } + + @VisibleForTesting + WakeupAlarmAnomalyDetector(Context context, AnomalyDetectionPolicy policy) { mContext = context; mBatteryUtils = BatteryUtils.getInstance(context); + mWakeupAlarmThreshold = policy.wakeupAlarmThreshold; } @Override @@ -66,7 +71,7 @@ public class WakeupAlarmAnomalyDetector implements AnomalyDetector { } final int wakeups = getWakeupAlarmCountFromUid(uid); - if ((wakeups / totalRunningHours) > WAKEUP_ALARM_THRESHOLD) { + if ((wakeups / totalRunningHours) > mWakeupAlarmThreshold) { final String packageName = mBatteryUtils.getPackageName(uid.getUid()); final CharSequence displayName = Utils.getApplicationLabel(mContext, packageName); diff --git a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicyTest.java b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicyTest.java index bb8bac5dc7e..2cd6b9ef473 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicyTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/AnomalyDetectionPolicyTest.java @@ -42,7 +42,9 @@ import org.robolectric.annotation.Config; public class AnomalyDetectionPolicyTest { private static final String ANOMALY_DETECTION_CONSTANTS_VALUE = "anomaly_detection_enabled=true" + ",wakelock_enabled=false" - + ",wakelock_threshold=3000"; + + ",wakelock_threshold=3000" + + ",wakeup_alarm_enabled=true" + + ",wakeup_alarm_threshold=100"; private Context mContext; private KeyValueListParserWrapper mKeyValueListParserWrapper; @@ -62,6 +64,8 @@ public class AnomalyDetectionPolicyTest { AnomalyDetectionPolicy.KEY_ANOMALY_DETECTION_ENABLED, true); doReturn(false).when(mKeyValueListParserWrapper).getBoolean( AnomalyDetectionPolicy.KEY_WAKELOCK_DETECTION_ENABLED, true); + doReturn(true).when(mKeyValueListParserWrapper).getBoolean( + AnomalyDetectionPolicy.KEY_WAKEUP_ALARM_DETECTION_ENABLED, true); AnomalyDetectionPolicy anomalyDetectionPolicy = new AnomalyDetectionPolicy(mContext, mKeyValueListParserWrapper); @@ -69,6 +73,8 @@ public class AnomalyDetectionPolicyTest { assertThat(anomalyDetectionPolicy.anomalyDetectionEnabled).isTrue(); assertThat(anomalyDetectionPolicy.wakeLockDetectionEnabled).isFalse(); assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(3000); + assertThat(anomalyDetectionPolicy.wakeupAlarmDetectionEnabled).isTrue(); + assertThat(anomalyDetectionPolicy.wakeupAlarmThreshold).isEqualTo(100); } @Test @@ -85,5 +91,7 @@ public class AnomalyDetectionPolicyTest { assertThat(anomalyDetectionPolicy.anomalyDetectionEnabled).isTrue(); assertThat(anomalyDetectionPolicy.wakeLockDetectionEnabled).isTrue(); assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(DateUtils.HOUR_IN_MILLIS); + assertThat(anomalyDetectionPolicy.wakeupAlarmDetectionEnabled).isTrue(); + assertThat(anomalyDetectionPolicy.wakeupAlarmThreshold).isEqualTo(60); } } diff --git a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetectorTest.java b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetectorTest.java index 826694d6242..2e1b7fb13f9 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetectorTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/anomaly/checker/WakeupAlarmAnomalyDetectorTest.java @@ -39,6 +39,7 @@ import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; import com.android.settings.fuelgauge.BatteryUtils; import com.android.settings.fuelgauge.anomaly.Anomaly; +import com.android.settings.fuelgauge.anomaly.AnomalyDetectionPolicy; import org.junit.Before; import org.junit.Test; @@ -47,6 +48,7 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; +import org.robolectric.util.ReflectionHelpers; import java.util.ArrayList; import java.util.List; @@ -77,6 +79,8 @@ public class WakeupAlarmAnomalyDetectorTest { private BatteryStats.Uid.Pkg mPkg; @Mock private BatteryStats.Counter mCounter; + @Mock + private AnomalyDetectionPolicy mPolicy; private WakeupAlarmAnomalyDetector mWakeupAlarmAnomalyDetector; private Context mContext; @@ -87,6 +91,7 @@ public class WakeupAlarmAnomalyDetectorTest { MockitoAnnotations.initMocks(this); mContext = spy(RuntimeEnvironment.application); + ReflectionHelpers.setField(mPolicy, "wakeupAlarmThreshold", 60); doReturn(false).when(mBatteryUtils).shouldHideSipper(any()); doReturn(RUNNING_TIME_MS).when(mBatteryUtils).calculateRunningTimeBasedOnStatsType(any(), @@ -102,7 +107,7 @@ public class WakeupAlarmAnomalyDetectorTest { mUsageList.add(mNormalSipper); doReturn(mUsageList).when(mBatteryStatsHelper).getUsageList(); - mWakeupAlarmAnomalyDetector = spy(new WakeupAlarmAnomalyDetector(mContext)); + mWakeupAlarmAnomalyDetector = spy(new WakeupAlarmAnomalyDetector(mContext, mPolicy)); mWakeupAlarmAnomalyDetector.mBatteryUtils = mBatteryUtils; }