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