Add wakelock action in testing app
This cl adds action to hold the wakelock for specific time. With this action we could test whether wakelock detector is effective. Bug: 2731722 Test: make -j40 AnomalyTester Change-Id: I1d44c12363f126ea9b8fc44f4c135e46b898186c
This commit is contained in:
@@ -74,6 +74,34 @@ public class AnomalyActivity extends Activity {
|
||||
}
|
||||
}
|
||||
|
||||
public void startWakelockAnomaly(View view) {
|
||||
try {
|
||||
// Enable anomaly detection and change the threshold
|
||||
final String config = new AnomalyPolicyBuilder()
|
||||
.addPolicy(AnomalyPolicyBuilder.KEY_ANOMALY_DETECTION_ENABLED, true)
|
||||
.addPolicy(AnomalyPolicyBuilder.KEY_WAKELOCK_DETECTION_ENABLED, true)
|
||||
.addPolicy(AnomalyPolicyBuilder.KEY_WAKELOCK_THRESHOLD,
|
||||
getValueFromEditText(R.id.wakelock_threshold))
|
||||
.build();
|
||||
Settings.Global.putString(getContentResolver(),
|
||||
Settings.Global.ANOMALY_DETECTION_CONSTANTS,
|
||||
config);
|
||||
|
||||
// Start the anomaly service
|
||||
Intent intent = new Intent(this, AnomalyService.class);
|
||||
intent.putExtra(AnomalyActions.KEY_ACTION, AnomalyActions.ACTION_WAKE_LOCK);
|
||||
intent.putExtra(AnomalyActions.KEY_DURATION_MS,
|
||||
getValueFromEditText(R.id.wakelock_run_time));
|
||||
intent.putExtra(AnomalyActions.KEY_RESULT_RECEIVER, mResultReceiver);
|
||||
intent.putExtra(KEY_TARGET_BUTTON, view.getId());
|
||||
startService(intent);
|
||||
|
||||
view.setEnabled(false);
|
||||
} catch (NumberFormatException e) {
|
||||
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
private long getValueFromEditText(final int id) throws NumberFormatException {
|
||||
final EditText editText = findViewById(id);
|
||||
if (editText != null) {
|
||||
|
@@ -22,6 +22,7 @@ import android.bluetooth.le.ScanCallback;
|
||||
import android.bluetooth.le.ScanResult;
|
||||
import android.bluetooth.le.ScanSettings;
|
||||
import android.content.Context;
|
||||
import android.os.PowerManager;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.List;
|
||||
@@ -37,6 +38,7 @@ public class AnomalyActions {
|
||||
public static final String KEY_RESULT_RECEIVER = "result_receiver";
|
||||
|
||||
public static final String ACTION_BLE_SCAN_UNOPTIMIZED = "action.ble_scan_unoptimized";
|
||||
public static final String ACTION_WAKE_LOCK = "action.wake_lock";
|
||||
|
||||
public static void doAction(Context ctx, String actionCode, long durationMs) {
|
||||
if (actionCode == null) {
|
||||
@@ -47,6 +49,8 @@ public class AnomalyActions {
|
||||
case ACTION_BLE_SCAN_UNOPTIMIZED:
|
||||
doUnoptimizedBleScan(ctx, durationMs);
|
||||
break;
|
||||
case ACTION_WAKE_LOCK:
|
||||
doHoldWakelock(ctx, durationMs);
|
||||
default:
|
||||
Log.e(TAG, "Intent had invalid action");
|
||||
}
|
||||
@@ -93,4 +97,17 @@ public class AnomalyActions {
|
||||
}
|
||||
bleScanner.stopScan(scanCallback);
|
||||
}
|
||||
|
||||
private static void doHoldWakelock(Context ctx, long durationMs) {
|
||||
PowerManager powerManager = ctx.getSystemService(PowerManager.class);
|
||||
PowerManager.WakeLock wl = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
|
||||
"AnomalyWakeLock");
|
||||
wl.acquire();
|
||||
try {
|
||||
Thread.sleep(durationMs);
|
||||
} catch (InterruptedException e) {
|
||||
Log.e(TAG, "Thread couldn't sleep for " + durationMs, e);
|
||||
}
|
||||
wl.release();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user