Merge "Add wakelock action in testing app" into oc-mr1-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
08fecc4143
@@ -16,11 +16,12 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="com.android.settings.anomaly.tester">
|
package="com.android.settings.anomaly.tester">
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
|
|
||||||
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
|
|
||||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
||||||
<uses-permission android:name="android.permission.BLUETOOTH"/>
|
<uses-permission android:name="android.permission.BLUETOOTH"/>
|
||||||
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
|
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
|
||||||
|
<uses-permission android:name="android.permission.WAKE_LOCK"/>
|
||||||
|
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>
|
||||||
|
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
|
||||||
<application
|
<application
|
||||||
android:allowBackup="false"
|
android:allowBackup="false"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
|
@@ -52,6 +52,8 @@
|
|||||||
|
|
||||||
<include layout="@layout/bluetooth_anomaly"/>
|
<include layout="@layout/bluetooth_anomaly"/>
|
||||||
|
|
||||||
|
<include layout="@layout/wakelock_anomaly"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
</ScrollView>
|
</ScrollView>
|
||||||
</LinearLayout>
|
</LinearLayout>
|
60
tests/anomaly-tester/res/layout/wakelock_anomaly.xml
Normal file
60
tests/anomaly-tester/res/layout/wakelock_anomaly.xml
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright (C) 2017 The Android Open Source Project
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-->
|
||||||
|
<LinearLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:paddingBottom="6dp">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Wakelock Anomaly"
|
||||||
|
android:textSize="16sp"/>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/wakelock_threshold"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_weight="3"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:hint="Threshold(ms)"
|
||||||
|
android:text="3000"
|
||||||
|
android:inputType="number"/>
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/wakelock_run_time"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_weight="3"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:hint="Run time(ms)"
|
||||||
|
android:text="6000"
|
||||||
|
android:inputType="number"/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/wakelock_button"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_weight="2"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="START"
|
||||||
|
android:onClick="startWakelockAnomaly"/>
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</LinearLayout>
|
@@ -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 {
|
private long getValueFromEditText(final int id) throws NumberFormatException {
|
||||||
final EditText editText = findViewById(id);
|
final EditText editText = findViewById(id);
|
||||||
if (editText != null) {
|
if (editText != null) {
|
||||||
|
@@ -22,6 +22,7 @@ import android.bluetooth.le.ScanCallback;
|
|||||||
import android.bluetooth.le.ScanResult;
|
import android.bluetooth.le.ScanResult;
|
||||||
import android.bluetooth.le.ScanSettings;
|
import android.bluetooth.le.ScanSettings;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.PowerManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -37,6 +38,7 @@ public class AnomalyActions {
|
|||||||
public static final String KEY_RESULT_RECEIVER = "result_receiver";
|
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_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) {
|
public static void doAction(Context ctx, String actionCode, long durationMs) {
|
||||||
if (actionCode == null) {
|
if (actionCode == null) {
|
||||||
@@ -47,6 +49,8 @@ public class AnomalyActions {
|
|||||||
case ACTION_BLE_SCAN_UNOPTIMIZED:
|
case ACTION_BLE_SCAN_UNOPTIMIZED:
|
||||||
doUnoptimizedBleScan(ctx, durationMs);
|
doUnoptimizedBleScan(ctx, durationMs);
|
||||||
break;
|
break;
|
||||||
|
case ACTION_WAKE_LOCK:
|
||||||
|
doHoldWakelock(ctx, durationMs);
|
||||||
default:
|
default:
|
||||||
Log.e(TAG, "Intent had invalid action");
|
Log.e(TAG, "Intent had invalid action");
|
||||||
}
|
}
|
||||||
@@ -93,4 +97,17 @@ public class AnomalyActions {
|
|||||||
}
|
}
|
||||||
bleScanner.stopScan(scanCallback);
|
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