Merge "Add wakelock action in testing app" into oc-mr1-dev

This commit is contained in:
TreeHugger Robot
2017-08-25 03:35:46 +00:00
committed by Android (Google) Code Review
5 changed files with 110 additions and 2 deletions

View File

@@ -16,11 +16,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
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.BLUETOOTH"/>
<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
android:allowBackup="false"
android:label="@string/app_name"

View File

@@ -52,6 +52,8 @@
<include layout="@layout/bluetooth_anomaly"/>
<include layout="@layout/wakelock_anomaly"/>
</LinearLayout>
</ScrollView>
</LinearLayout>

View 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>

View File

@@ -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) {

View File

@@ -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();
}
}