am 02c7b070: Added screen turn off capabilities to CTS sensor tests when Device Admin feature is not available.

* commit '02c7b070c66a1115b0098fb4411456feba9a8a14':
  Added screen turn off capabilities to CTS sensor tests when Device Admin feature is not available.
diff --git a/apps/CtsVerifier/AndroidManifest.xml b/apps/CtsVerifier/AndroidManifest.xml
index 33244cf..21546d5 100644
--- a/apps/CtsVerifier/AndroidManifest.xml
+++ b/apps/CtsVerifier/AndroidManifest.xml
@@ -1407,6 +1407,9 @@
         <service android:name=".jobscheduler.MockJobService"
             android:permission="android.permission.BIND_JOB_SERVICE"/>
 
+        <!-- Used by the SensorTestScreenManipulator to reset the screen timeout after turn off. -->
+        <activity android:name=".os.TimeoutResetActivity"/>
+
     </application>
 
 </manifest>
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/os/TimeoutResetActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/os/TimeoutResetActivity.java
new file mode 100644
index 0000000..be78556
--- /dev/null
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/os/TimeoutResetActivity.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+package com.android.cts.verifier.os;
+
+import android.app.Activity;
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.WindowManager;
+
+import java.lang.reflect.Field;
+
+/**
+ * Activity resets the screen timeout to its original timeout. Used devices without Device Admin.
+ */
+public class TimeoutResetActivity extends Activity {
+    public static final String EXTRA_OLD_TIMEOUT = "com.android.cts.verifier.extra.OLD_TIMEOUT";
+    /** Set the timeout to the default to reset the activity to if not specified. */
+    public static final long FALLBACK_TIMEOUT = -1L;
+    /**
+     * Empirically determined buffer time in milliseconds between setting short timeout time and
+     * resetting the timeout.
+     */
+    public static final long RESET_BUFFER_TIME = 2000L;
+    /** Short timeout to trigger screen off. */
+    public static final long SCREEN_OFF_TIMEOUT = 0L;
+    public static final String TAG = TimeoutResetActivity.class.getSimpleName();
+
+    private static long getUserActivityTimeout(WindowManager.LayoutParams params) {
+        try {
+            return getUserActivityTimeoutField(params).getLong(params);
+        } catch (Exception e) {
+            Log.e(TAG, "error loading the userActivityTimeout field", e);
+            return -1;
+        }
+    }
+
+    private static Field getUserActivityTimeoutField(WindowManager.LayoutParams params)
+            throws NoSuchFieldException {
+        return params.getClass().getField("userActivityTimeout");
+    }
+
+    private static void setUserActivityTimeout(WindowManager.LayoutParams params, long timeout) {
+        try {
+            getUserActivityTimeoutField(params).setLong(params, timeout);
+            Log.d(TAG, "UserActivityTimeout set to " + timeout);
+        } catch (Exception e) {
+            Log.e(TAG, "error setting the userActivityTimeout field", e);
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static void turnOffScreen(final Activity activity) {
+        activity.runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
+
+                // to restore timeout after shutoff
+                final long oldTimeout = getUserActivityTimeout(params);
+
+                final long timeout = SCREEN_OFF_TIMEOUT;
+                setUserActivityTimeout(params, timeout);
+
+                // upon setting this, timeout will be reduced
+                activity.getWindow().setAttributes(params);
+
+                ((AlarmManager) activity.getSystemService(ALARM_SERVICE)).setExact(
+                        AlarmManager.RTC,
+                        System.currentTimeMillis() + RESET_BUFFER_TIME,
+                        PendingIntent.getActivity(
+                                activity.getApplicationContext(),
+                                0,
+                                new Intent(activity, TimeoutResetActivity.class)
+                                        .putExtra(EXTRA_OLD_TIMEOUT, oldTimeout),
+                                0));
+            }
+        });
+    }
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        long timeout = getIntent().getLongExtra(EXTRA_OLD_TIMEOUT, FALLBACK_TIMEOUT);
+        if (timeout < 1000) { // in case the old timeout was super low by accident
+            timeout = FALLBACK_TIMEOUT;
+        }
+
+        WindowManager.LayoutParams params = getWindow().getAttributes();
+        setUserActivityTimeout(params, timeout);
+        getWindow().setAttributes(params);
+
+        finish();
+    }
+}
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SensorPowerTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SensorPowerTestActivity.java
index 8370d3e..74d51e4 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SensorPowerTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/SensorPowerTestActivity.java
@@ -63,7 +63,7 @@
 
     @Override
     protected void activitySetUp() throws InterruptedException {
-        mScreenManipulator = new SensorTestScreenManipulator(getApplicationContext());
+        mScreenManipulator = new SensorTestScreenManipulator(this);
         mScreenManipulator.initialize(this);
     }
 
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/SensorCtsTestActivity.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/SensorCtsTestActivity.java
index 16c5fcd..6512fd3 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/SensorCtsTestActivity.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/base/SensorCtsTestActivity.java
@@ -69,7 +69,7 @@
     protected void activitySetUp() throws InterruptedException {
         PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
         mWakeLock =  powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SensorCtsTests");
-        mScreenManipulator = new SensorTestScreenManipulator(getApplicationContext());
+        mScreenManipulator = new SensorTestScreenManipulator(this);
         mScreenManipulator.initialize(this);
 
         SensorTestLogger logger = getTestLogger();
@@ -80,6 +80,7 @@
         // automated CTS tests run with the USB connected, so the AP doesn't go to sleep
         // here we are not connected to USB, so we need to hold a wake-lock to avoid going to sleep
         mWakeLock.acquire();
+
         mScreenManipulator.turnScreenOff();
     }
 
diff --git a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/helpers/SensorTestScreenManipulator.java b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/helpers/SensorTestScreenManipulator.java
index 835ff56..2956ed7 100644
--- a/apps/CtsVerifier/src/com/android/cts/verifier/sensors/helpers/SensorTestScreenManipulator.java
+++ b/apps/CtsVerifier/src/com/android/cts/verifier/sensors/helpers/SensorTestScreenManipulator.java
@@ -16,6 +16,7 @@
 
 package com.android.cts.verifier.sensors.helpers;
 
+import com.android.cts.verifier.os.TimeoutResetActivity;
 import com.android.cts.verifier.sensors.base.BaseSensorTestActivity;
 import com.android.cts.verifier.sensors.base.ISensorTestStateContainer;
 
@@ -27,8 +28,12 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.pm.PackageManager;
 import android.os.PowerManager;
 import android.text.TextUtils;
+import android.util.Log;
+
+import java.util.concurrent.CountDownLatch;
 
 /**
  * A class that provides functionality to manipulate the state of the device's screen.
@@ -52,8 +57,9 @@
  * - in a single-threaded environment
  */
 public class SensorTestScreenManipulator {
+    private static final String TAG = SensorTestScreenManipulator.class.getSimpleName();
 
-    private final Context mContext;
+    private final Activity mActivity;
     private final DevicePolicyManager mDevicePolicyManager;
     private final ComponentName mComponentName;
     private final PowerManager.WakeLock mWakeUpScreenWakeLock;
@@ -62,16 +68,17 @@
     private InternalBroadcastReceiver mBroadcastReceiver;
     private boolean mTurnOffScreenOnPowerDisconnected;
 
-    public SensorTestScreenManipulator(Context context) {
-        mContext = context;
-        mComponentName = SensorDeviceAdminReceiver.getComponentName(context);
+
+    public SensorTestScreenManipulator(Activity activity) {
+        mActivity = activity;
+        mComponentName = SensorDeviceAdminReceiver.getComponentName(activity);
         mDevicePolicyManager =
-                (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
+                (DevicePolicyManager) activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
 
         int levelAndFlags = PowerManager.FULL_WAKE_LOCK
                 | PowerManager.ON_AFTER_RELEASE
                 | PowerManager.ACQUIRE_CAUSES_WAKEUP;
-        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
+        PowerManager powerManager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
         mWakeUpScreenWakeLock = powerManager.newWakeLock(levelAndFlags, "SensorTestWakeUpScreen");
         mWakeUpScreenWakeLock.setReferenceCounted(false);
         mKeepScreenOnWakeLock = powerManager.newWakeLock(levelAndFlags, "SensorTestKeepScreenOn");
@@ -87,7 +94,7 @@
      */
     public synchronized void initialize(ISensorTestStateContainer stateContainer)
             throws InterruptedException {
-        if (!isDeviceAdminInitialized()) {
+        if (hasDeviceAdminFeature() && !isDeviceAdminInitialized()) {
             Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
             intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName);
             int resultCode = stateContainer.executeActivity(intent);
@@ -101,7 +108,7 @@
             mBroadcastReceiver = new InternalBroadcastReceiver();
             IntentFilter intentFilter = new IntentFilter();
             intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
-            mContext.registerReceiver(mBroadcastReceiver, intentFilter);
+            mActivity.registerReceiver(mBroadcastReceiver, intentFilter);
         }
     }
 
@@ -111,7 +118,7 @@
      */
     public synchronized  void close() {
         if (mBroadcastReceiver != null) {
-            mContext.unregisterReceiver(mBroadcastReceiver);
+            mActivity.unregisterReceiver(mBroadcastReceiver);
             mBroadcastReceiver = null;
         }
     }
@@ -121,8 +128,30 @@
      */
     public synchronized void turnScreenOff() {
         ensureDeviceAdminInitialized();
+
+        final CountDownLatch screenOffSignal = new CountDownLatch(1);
+        BroadcastReceiver screenOffBroadcastReceiver = new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                mActivity.unregisterReceiver(this);
+                screenOffSignal.countDown();
+            }
+        };
+        mActivity.registerReceiver(
+                screenOffBroadcastReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
+
         releaseScreenOn();
-        mDevicePolicyManager.lockNow();
+        if (hasDeviceAdminFeature()) {
+            mDevicePolicyManager.lockNow();
+        } else {
+            TimeoutResetActivity.turnOffScreen(mActivity);
+        }
+
+        try {
+            screenOffSignal.await();
+        } catch (InterruptedException e) {
+            Log.wtf(TAG, "error waiting for screen off signal", e);
+        }
     }
 
     /**
@@ -175,7 +204,7 @@
     }
 
     private void ensureDeviceAdminInitialized() throws IllegalStateException {
-        if (!isDeviceAdminInitialized()) {
+        if (hasDeviceAdminFeature() && !isDeviceAdminInitialized()) {
             throw new IllegalStateException("Component must be initialized before it can be used.");
         }
     }
@@ -188,6 +217,10 @@
                 .hasGrantedPolicy(mComponentName, DeviceAdminInfo.USES_POLICY_FORCE_LOCK);
     }
 
+    private boolean hasDeviceAdminFeature() {
+        return mActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN);
+    }
+
     private class InternalBroadcastReceiver extends BroadcastReceiver {
         @Override
         public void onReceive(Context context, Intent intent) {