Merge "Hook up bt related knobs to AnomalyDetectionPolicy" into oc-dr1-dev

am: 43ea43a6e5

Change-Id: Ie2e58d02d9fe1a68a8583ad31158df68384cfa91
This commit is contained in:
Lei Yu
2017-06-19 21:30:18 +00:00
committed by android-build-merger
4 changed files with 42 additions and 3 deletions

View File

@@ -37,9 +37,13 @@ public class AnomalyDetectionPolicy {
@VisibleForTesting @VisibleForTesting
static final String KEY_WAKEUP_ALARM_DETECTION_ENABLED = "wakeup_alarm_enabled"; static final String KEY_WAKEUP_ALARM_DETECTION_ENABLED = "wakeup_alarm_enabled";
@VisibleForTesting @VisibleForTesting
static final String KEY_BLUETOOTH_SCAN_DETECTION_ENABLED = "bluetooth_scan_enabled";
@VisibleForTesting
static final String KEY_WAKELOCK_THRESHOLD = "wakelock_threshold"; static final String KEY_WAKELOCK_THRESHOLD = "wakelock_threshold";
@VisibleForTesting @VisibleForTesting
static final String KEY_WAKEUP_ALARM_THRESHOLD = "wakeup_alarm_threshold"; static final String KEY_WAKEUP_ALARM_THRESHOLD = "wakeup_alarm_threshold";
@VisibleForTesting
static final String KEY_BLUETOOTH_SCAN_THRESHOLD = "bluetooth_scan_threshold";
/** /**
* {@code true} if general anomaly detection is enabled * {@code true} if general anomaly detection is enabled
@@ -65,6 +69,14 @@ public class AnomalyDetectionPolicy {
*/ */
public final boolean wakeupAlarmDetectionEnabled; public final boolean wakeupAlarmDetectionEnabled;
/**
* {@code true} if bluetooth scanning detection is enabled
*
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
* @see #KEY_BLUETOOTH_SCAN_THRESHOLD
*/
public final boolean bluetoothScanDetectionEnabled;
/** /**
* Threshold for wakelock time in milli seconds * Threshold for wakelock time in milli seconds
* *
@@ -81,6 +93,14 @@ public class AnomalyDetectionPolicy {
*/ */
public final long wakeupAlarmThreshold; public final long wakeupAlarmThreshold;
/**
* Threshold for bluetooth unoptimized scanning time in milli seconds
*
* @see Settings.Global#ANOMALY_DETECTION_CONSTANTS
* @see #KEY_BLUETOOTH_SCAN_THRESHOLD
*/
public final long bluetoothScanThreshold;
private final KeyValueListParserWrapper mParserWrapper; private final KeyValueListParserWrapper mParserWrapper;
public AnomalyDetectionPolicy(Context context) { public AnomalyDetectionPolicy(Context context) {
@@ -103,9 +123,13 @@ public class AnomalyDetectionPolicy {
wakeLockDetectionEnabled = mParserWrapper.getBoolean(KEY_WAKELOCK_DETECTION_ENABLED, true); wakeLockDetectionEnabled = mParserWrapper.getBoolean(KEY_WAKELOCK_DETECTION_ENABLED, true);
wakeupAlarmDetectionEnabled = mParserWrapper.getBoolean(KEY_WAKEUP_ALARM_DETECTION_ENABLED, wakeupAlarmDetectionEnabled = mParserWrapper.getBoolean(KEY_WAKEUP_ALARM_DETECTION_ENABLED,
true); true);
bluetoothScanDetectionEnabled = mParserWrapper.getBoolean(
KEY_BLUETOOTH_SCAN_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); wakeupAlarmThreshold = mParserWrapper.getLong(KEY_WAKEUP_ALARM_THRESHOLD, 60);
bluetoothScanThreshold = mParserWrapper.getLong(KEY_BLUETOOTH_SCAN_THRESHOLD,
30 * DateUtils.MINUTE_IN_MILLIS);
} }
public boolean isAnomalyDetectorEnabled(@Anomaly.AnomalyType int type) { public boolean isAnomalyDetectorEnabled(@Anomaly.AnomalyType int type) {
@@ -114,6 +138,8 @@ public class AnomalyDetectionPolicy {
return wakeLockDetectionEnabled; return wakeLockDetectionEnabled;
case Anomaly.AnomalyType.WAKEUP_ALARM: case Anomaly.AnomalyType.WAKEUP_ALARM:
return wakeupAlarmDetectionEnabled; return wakeupAlarmDetectionEnabled;
case Anomaly.AnomalyType.BLUETOOTH_SCAN:
return bluetoothScanDetectionEnabled;
default: default:
return false; // Disabled when no this type return false; // Disabled when no this type
} }

View File

@@ -57,8 +57,7 @@ public class BluetoothScanAnomalyDetector implements AnomalyDetector {
mBatteryUtils = BatteryUtils.getInstance(context); mBatteryUtils = BatteryUtils.getInstance(context);
mAnomalyAction = AnomalyUtils.getInstance(context).getAnomalyAction( mAnomalyAction = AnomalyUtils.getInstance(context).getAnomalyAction(
Anomaly.AnomalyType.BLUETOOTH_SCAN); Anomaly.AnomalyType.BLUETOOTH_SCAN);
//TODO(b/36921532): hook up it to AnomalyDectionPolicy mBluetoothScanningThreshold = policy.bluetoothScanThreshold;
mBluetoothScanningThreshold = 30 * DateUtils.MINUTE_IN_MILLIS;
} }
@Override @Override

View File

@@ -44,7 +44,9 @@ public class AnomalyDetectionPolicyTest {
+ ",wakelock_enabled=false" + ",wakelock_enabled=false"
+ ",wakelock_threshold=3000" + ",wakelock_threshold=3000"
+ ",wakeup_alarm_enabled=true" + ",wakeup_alarm_enabled=true"
+ ",wakeup_alarm_threshold=100"; + ",wakeup_alarm_threshold=100"
+ ",bluetooth_scan_enabled=true"
+ ",bluetooth_scan_threshold=2000";
private Context mContext; private Context mContext;
private KeyValueListParserWrapper mKeyValueListParserWrapper; private KeyValueListParserWrapper mKeyValueListParserWrapper;
@@ -64,6 +66,8 @@ public class AnomalyDetectionPolicyTest {
assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(3000); assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(3000);
assertThat(anomalyDetectionPolicy.wakeupAlarmDetectionEnabled).isTrue(); assertThat(anomalyDetectionPolicy.wakeupAlarmDetectionEnabled).isTrue();
assertThat(anomalyDetectionPolicy.wakeupAlarmThreshold).isEqualTo(100); assertThat(anomalyDetectionPolicy.wakeupAlarmThreshold).isEqualTo(100);
assertThat(anomalyDetectionPolicy.bluetoothScanDetectionEnabled).isTrue();
assertThat(anomalyDetectionPolicy.bluetoothScanThreshold).isEqualTo(2000);
} }
@Test @Test
@@ -82,6 +86,9 @@ public class AnomalyDetectionPolicyTest {
assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(DateUtils.HOUR_IN_MILLIS); assertThat(anomalyDetectionPolicy.wakeLockThreshold).isEqualTo(DateUtils.HOUR_IN_MILLIS);
assertThat(anomalyDetectionPolicy.wakeupAlarmDetectionEnabled).isTrue(); assertThat(anomalyDetectionPolicy.wakeupAlarmDetectionEnabled).isTrue();
assertThat(anomalyDetectionPolicy.wakeupAlarmThreshold).isEqualTo(60); assertThat(anomalyDetectionPolicy.wakeupAlarmThreshold).isEqualTo(60);
assertThat(anomalyDetectionPolicy.bluetoothScanDetectionEnabled).isTrue();
assertThat(anomalyDetectionPolicy.bluetoothScanThreshold).isEqualTo(
30 * DateUtils.MINUTE_IN_MILLIS);
} }
@Test @Test
@@ -92,6 +99,8 @@ public class AnomalyDetectionPolicyTest {
Anomaly.AnomalyType.WAKE_LOCK)).isFalse(); Anomaly.AnomalyType.WAKE_LOCK)).isFalse();
assertThat(anomalyDetectionPolicy.isAnomalyDetectorEnabled( assertThat(anomalyDetectionPolicy.isAnomalyDetectorEnabled(
Anomaly.AnomalyType.WAKEUP_ALARM)).isTrue(); Anomaly.AnomalyType.WAKEUP_ALARM)).isTrue();
assertThat(anomalyDetectionPolicy.isAnomalyDetectorEnabled(
Anomaly.AnomalyType.BLUETOOTH_SCAN)).isTrue();
} }
private AnomalyDetectionPolicy createAnomalyPolicyWithConfig() { private AnomalyDetectionPolicy createAnomalyPolicyWithConfig() {
@@ -104,6 +113,8 @@ public class AnomalyDetectionPolicyTest {
AnomalyDetectionPolicy.KEY_WAKELOCK_DETECTION_ENABLED, true); AnomalyDetectionPolicy.KEY_WAKELOCK_DETECTION_ENABLED, true);
doReturn(true).when(mKeyValueListParserWrapper).getBoolean( doReturn(true).when(mKeyValueListParserWrapper).getBoolean(
AnomalyDetectionPolicy.KEY_WAKEUP_ALARM_DETECTION_ENABLED, true); AnomalyDetectionPolicy.KEY_WAKEUP_ALARM_DETECTION_ENABLED, true);
doReturn(true).when(mKeyValueListParserWrapper).getBoolean(
AnomalyDetectionPolicy.KEY_BLUETOOTH_SCAN_DETECTION_ENABLED, true);
return new AnomalyDetectionPolicy(mContext, mKeyValueListParserWrapper); return new AnomalyDetectionPolicy(mContext, mKeyValueListParserWrapper);
} }

View File

@@ -46,6 +46,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;
@@ -89,6 +90,8 @@ public class BluetoothScanAnomalyDetectorTest {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application); mContext = spy(RuntimeEnvironment.application);
ReflectionHelpers.setField(mPolicy, "bluetoothScanThreshold",
30 * DateUtils.MINUTE_IN_MILLIS);
mAnomalySipper.uidObj = mAnomalyUid; mAnomalySipper.uidObj = mAnomalyUid;
doReturn(ANOMALY_UID).when(mAnomalyUid).getUid(); doReturn(ANOMALY_UID).when(mAnomalyUid).getUid();