Merge "Add wakelock action in testing app" into oc-mr1-dev
am: 08fecc4143

Change-Id: Id3125640f4d62da1161b9e894577c6285fe7c83a
diff --git a/tests/anomaly-tester/AndroidManifest.xml b/tests/anomaly-tester/AndroidManifest.xml
index c057a66..68e2dd7 100644
--- a/tests/anomaly-tester/AndroidManifest.xml
+++ b/tests/anomaly-tester/AndroidManifest.xml
@@ -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"
diff --git a/tests/anomaly-tester/res/layout/activity_main.xml b/tests/anomaly-tester/res/layout/activity_main.xml
index 63d025e..0561cff 100644
--- a/tests/anomaly-tester/res/layout/activity_main.xml
+++ b/tests/anomaly-tester/res/layout/activity_main.xml
@@ -52,6 +52,8 @@
 
             <include layout="@layout/bluetooth_anomaly"/>
 
+            <include layout="@layout/wakelock_anomaly"/>
+
         </LinearLayout>
     </ScrollView>
 </LinearLayout>
\ No newline at end of file
diff --git a/tests/anomaly-tester/res/layout/wakelock_anomaly.xml b/tests/anomaly-tester/res/layout/wakelock_anomaly.xml
new file mode 100644
index 0000000..08cb795
--- /dev/null
+++ b/tests/anomaly-tester/res/layout/wakelock_anomaly.xml
@@ -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>
\ No newline at end of file
diff --git a/tests/anomaly-tester/src/com/android/settings/anomaly/tester/AnomalyActivity.java b/tests/anomaly-tester/src/com/android/settings/anomaly/tester/AnomalyActivity.java
index 773dd27..e4567c2 100644
--- a/tests/anomaly-tester/src/com/android/settings/anomaly/tester/AnomalyActivity.java
+++ b/tests/anomaly-tester/src/com/android/settings/anomaly/tester/AnomalyActivity.java
@@ -74,6 +74,34 @@
         }
     }
 
+    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) {
diff --git a/tests/anomaly-tester/src/com/android/settings/anomaly/tester/utils/AnomalyActions.java b/tests/anomaly-tester/src/com/android/settings/anomaly/tester/utils/AnomalyActions.java
index a55efdb..58e5a99 100644
--- a/tests/anomaly-tester/src/com/android/settings/anomaly/tester/utils/AnomalyActions.java
+++ b/tests/anomaly-tester/src/com/android/settings/anomaly/tester/utils/AnomalyActions.java
@@ -22,6 +22,7 @@
 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 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 @@
             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 @@
         }
         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();
+    }
 }