Add receiver to CTS to disable the screen lock.

The receiver was added to the existing 'DeviceInfoCollector' apk, and that apk
was renamed to TestDeviceSetup to reflect its new purpose.
This replaces the old method of injecting a menu key event, which no longer
works.

Bug 2099347

Change-Id: Icf0c2ba6e65fb0e401dfcc98468c91bb0c7b521d
diff --git a/tools/plugin/Android.mk b/tools/device-setup/Android.mk
similarity index 100%
rename from tools/plugin/Android.mk
rename to tools/device-setup/Android.mk
diff --git a/tools/plugin/DeviceInfoCollector/Android.mk b/tools/device-setup/TestDeviceSetup/Android.mk
similarity index 92%
rename from tools/plugin/DeviceInfoCollector/Android.mk
rename to tools/device-setup/TestDeviceSetup/Android.mk
index 1816649..3704be5 100644
--- a/tools/plugin/DeviceInfoCollector/Android.mk
+++ b/tools/device-setup/TestDeviceSetup/Android.mk
@@ -24,7 +24,9 @@
 
 LOCAL_JAVA_LIBRARIES := android.test.runner
 
-LOCAL_PACKAGE_NAME := DeviceInfoCollector
+LOCAL_SDK_VERSION := current
+
+LOCAL_PACKAGE_NAME := TestDeviceSetup
 
 include $(BUILD_PACKAGE)
 
diff --git a/tools/plugin/DeviceInfoCollector/AndroidManifest.xml b/tools/device-setup/TestDeviceSetup/AndroidManifest.xml
similarity index 65%
rename from tools/plugin/DeviceInfoCollector/AndroidManifest.xml
rename to tools/device-setup/TestDeviceSetup/AndroidManifest.xml
index 7eacb1a..d55ecdf 100644
--- a/tools/plugin/DeviceInfoCollector/AndroidManifest.xml
+++ b/tools/device-setup/TestDeviceSetup/AndroidManifest.xml
@@ -16,22 +16,30 @@
 -->
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="android.tests.getinfo">
+    package="android.tests.devicesetup">
 
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
+    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
 
     <application>
         <uses-library android:name="android.test.runner" />
-        <activity android:name="DeviceInfoActivity" android:label="DeviceInfoActivity">
+        <activity android:name="android.tests.getinfo.DeviceInfoActivity"
+                  android:label="DeviceInfoActivity">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN"/>
                 <category android:name="android.intent.category.LAUNCHER"/>
             </intent-filter>
         </activity>
+
+    <receiver android:name="android.tests.util.DisableKeyguardReceiver">
+            <intent-filter>
+                <action android:name="android.tests.util.disablekeyguard" />
+            </intent-filter>
+        </receiver>
     </application>
 
-    <instrumentation android:name=".DeviceInfoInstrument"
-                     android:targetPackage="android.tests.getinfo"
+    <instrumentation android:name="android.tests.getinfo.DeviceInfoInstrument"
+                     android:targetPackage="android.tests.devicesetup"
                      android:label="app to get info from device"/>
 
 </manifest>
diff --git a/tools/plugin/DeviceInfoCollector/src/android/tests/getinfo/DeviceInfoActivity.java b/tools/device-setup/TestDeviceSetup/src/android/tests/getinfo/DeviceInfoActivity.java
similarity index 96%
rename from tools/plugin/DeviceInfoCollector/src/android/tests/getinfo/DeviceInfoActivity.java
rename to tools/device-setup/TestDeviceSetup/src/android/tests/getinfo/DeviceInfoActivity.java
index 94da1cf..e6c0e14 100644
--- a/tools/plugin/DeviceInfoCollector/src/android/tests/getinfo/DeviceInfoActivity.java
+++ b/tools/device-setup/TestDeviceSetup/src/android/tests/getinfo/DeviceInfoActivity.java
@@ -16,14 +16,13 @@
 
 package android.tests.getinfo;
 
-import java.util.Locale;
-
 import android.app.Activity;
-import android.app.ActivityThread;
 import android.content.res.Configuration;
 import android.os.Bundle;
 import android.widget.TextView;
 
+import java.util.Locale;
+
 
 /**
  * Collect device information on target device.
@@ -53,8 +52,7 @@
         TextView view = new TextView(this);
         view.setText("hello");
         setContentView(view);
-        ActivityThread t = ActivityThread.currentActivityThread();
-        Configuration con = t.getConfiguration();
+        Configuration con = getResources().getConfiguration();
         String touchScreen = null;
         if (con.touchscreen == Configuration.TOUCHSCREEN_UNDEFINED) {
             touchScreen = "undefined";
diff --git a/tools/plugin/DeviceInfoCollector/src/android/tests/getinfo/DeviceInfoInstrument.java b/tools/device-setup/TestDeviceSetup/src/android/tests/getinfo/DeviceInfoInstrument.java
similarity index 93%
rename from tools/plugin/DeviceInfoCollector/src/android/tests/getinfo/DeviceInfoInstrument.java
rename to tools/device-setup/TestDeviceSetup/src/android/tests/getinfo/DeviceInfoInstrument.java
index bf6f4dd..64abfcf 100644
--- a/tools/plugin/DeviceInfoCollector/src/android/tests/getinfo/DeviceInfoInstrument.java
+++ b/tools/device-setup/TestDeviceSetup/src/android/tests/getinfo/DeviceInfoInstrument.java
@@ -22,6 +22,7 @@
 import android.content.Intent;
 import android.os.Build;
 import android.os.Bundle;
+import android.telephony.TelephonyManager;
 import android.util.DisplayMetrics;
 import android.view.Display;
 import android.view.WindowManager;
@@ -99,20 +100,22 @@
         waitForIdleSync();
         activity.waitForAcitityToFinish();
 
+        TelephonyManager tm = (TelephonyManager) getContext().getSystemService(
+                Context.TELEPHONY_SERVICE);
         // network
-        String network = android.telephony.TelephonyManager.getDefault().getNetworkOperatorName();
+        String network = tm.getNetworkOperatorName();
         addResult(NETWORK, network);
 
         // imei
-        String imei = android.telephony.TelephonyManager.getDefault().getDeviceId();
+        String imei = tm.getDeviceId();
         addResult(IMEI, imei);
 
         // imsi
-        String imsi = android.telephony.TelephonyManager.getDefault().getSubscriberId();
+        String imsi = tm.getSubscriberId();
         addResult(IMSI, imsi);
 
         // phone number
-        String phoneNumber = android.telephony.TelephonyManager.getDefault().getLine1Number();
+        String phoneNumber = tm.getLine1Number();
         addResult(PHONE_NUMBER, phoneNumber);
 
         finish(Activity.RESULT_OK, mResults);
diff --git a/tools/device-setup/TestDeviceSetup/src/android/tests/util/DisableKeyguardReceiver.java b/tools/device-setup/TestDeviceSetup/src/android/tests/util/DisableKeyguardReceiver.java
new file mode 100644
index 0000000..3e5012d
--- /dev/null
+++ b/tools/device-setup/TestDeviceSetup/src/android/tests/util/DisableKeyguardReceiver.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2009 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 android.tests.util;
+
+import android.app.KeyguardManager;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+
+/**
+ * A utility receiver to disable the keyguard on devices.
+ * Intended to be used so UI related tests can be executed successfully.
+ */
+public class DisableKeyguardReceiver extends BroadcastReceiver {
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        KeyguardManager keyguardManager =
+            (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
+        keyguardManager.newKeyguardLock("cts").disableKeyguard();
+    }
+}
diff --git a/tools/host/src/com/android/cts/DeviceManager.java b/tools/host/src/com/android/cts/DeviceManager.java
index 9dd292a..b92128a 100644
--- a/tools/host/src/com/android/cts/DeviceManager.java
+++ b/tools/host/src/com/android/cts/DeviceManager.java
@@ -354,9 +354,6 @@
                 }
                 attempts += 1;
             }
-            // dismiss the screen lock by sending a MENU key event
-            // TODO: this command isn't going to work on a user device, seems like its not needed
-            executeCommand("adb -s " + deviceSerialNumber + " shell input keyevent 82");
         }
     }
 
diff --git a/tools/host/src/com/android/cts/TestDevice.java b/tools/host/src/com/android/cts/TestDevice.java
index dd53a7f..2146b1e 100644
--- a/tools/host/src/com/android/cts/TestDevice.java
+++ b/tools/host/src/com/android/cts/TestDevice.java
@@ -21,6 +21,7 @@
 import com.android.ddmlib.IDevice;
 import com.android.ddmlib.IShellOutputReceiver;
 import com.android.ddmlib.MultiLineReceiver;
+import com.android.ddmlib.NullOutputReceiver;
 import com.android.ddmlib.RawImage;
 import com.android.ddmlib.SyncService;
 import com.android.ddmlib.SyncService.ISyncProgressMonitor;
@@ -50,7 +51,8 @@
  * </ul>
  */
 public class TestDevice implements DeviceObserver {
-    private static final String GET_INFO_APP_PACKAGE_NAME = "android.tests.getinfo";
+    private static final String DEVICE_SETUP_APK = "TestDeviceSetup";
+    private static final String DEVICE_SETUP_APP_PACKAGE_NAME = "android.tests.devicesetup";
     private static final String DEFAULT_TEST_RUNNER_NAME =
                                   "android.test.InstrumentationTestRunner";
     private static final String ACTION_INSTALL = "install";
@@ -144,7 +146,10 @@
     }
 
     /**
-     * get the information of this device
+     * Gets this device's information.
+     *
+     * Assumes that the test device setup apk is already installed.
+     * See {@link #installDeviceSetupApp()}.
      *
      * @return information of this device.
      */
@@ -160,6 +165,24 @@
     }
 
     /**
+     * Attempt to disable the screen guard on device.
+     *
+     * Assumes the test device setup apk is already installed.
+     * See {@link #installDeviceSetupApp()}.
+     *
+     * Note: uninstalling the device setup app {@link #uninstallDeviceSetupApp()} will re-enable
+     * keyguard.
+     *
+     * @throws DeviceDisconnectedException
+     */
+    public void disableKeyguard () throws DeviceDisconnectedException {
+        final String commandStr = "am broadcast -a android.tests.util.disablekeyguard";
+        Log.d(commandStr);
+
+        executeShellCommand(commandStr, new NullOutputReceiver());
+    }
+
+    /**
      * Return the Device instance associated with this TestDevice.
      */
     public IDevice getDevice() {
@@ -246,20 +269,51 @@
     }
 
     /**
-     * Run device information collector apk to got the device info.
+     * Run device information collector command to got the device info.
      */
     private void genDeviceInfo() throws DeviceDisconnectedException,
                 InvalidNameSpaceException, InvalidApkPathException {
-        String apkName = "DeviceInfoCollector";
+        mDeviceInfo.set(DeviceParameterCollector.SERIAL_NUMBER, getSerialNumber());
+        // run shell command to run device information collector
+        Log.d("run device information collector");
+        runDeviceInfoCollectorCommand();
+        waitForCommandFinish();
+    }
 
-        String apkPath = HostConfig.getInstance().getCaseRepository().getApkPath(apkName);
+    /**
+     * Uninstall the device setup apk from device.
+     *
+     * See {@link #installDeviceSetupApp}
+     *
+     * @throws DeviceDisconnectedException
+     * @throws InvalidNameSpaceException
+     */
+    public void uninstallDeviceSetupApp() throws DeviceDisconnectedException,
+            InvalidNameSpaceException {
+        // reset device observer
+        DeviceObserver tmpDeviceObserver = mDeviceObserver;
+        mDeviceObserver = this;
+        Log.d("uninstall get info ...");
+        uninstallAPK(DEVICE_SETUP_APP_PACKAGE_NAME);
+        waitForCommandFinish();
+        Log.d("uninstall device information collector successfully");
+        mDeviceObserver = tmpDeviceObserver;
+    }
+
+    /**
+     * Install the device setup apk on the device.
+     *
+     * @throws DeviceDisconnectedException
+     * @throws InvalidApkPathException
+     */
+    public void installDeviceSetupApp() throws DeviceDisconnectedException, InvalidApkPathException {
+        String apkPath = HostConfig.getInstance().getCaseRepository().getApkPath(DEVICE_SETUP_APK);
         if (!HostUtils.isFileExist(apkPath)) {
             Log.e("File doesn't exist: " + apkPath, null);
             return;
         }
 
-        mDeviceInfo.set(DeviceParameterCollector.SERIAL_NUMBER, getSerialNumber());
-        Log.d("installing " + apkName + " apk");
+        Log.d("installing " + DEVICE_SETUP_APK + " apk");
         mObjectSync = new ObjectSync();
 
         // reset device observer
@@ -269,16 +323,6 @@
         Log.d("install get info ...");
         installAPK(apkPath);
         waitForCommandFinish();
-
-        // run shell command to run device information collector
-        Log.d("run device information collector");
-        runDeviceInfoCollectorCommand();
-        waitForCommandFinish();
-
-        Log.d("uninstall get info ...");
-        uninstallAPK(GET_INFO_APP_PACKAGE_NAME);
-        waitForCommandFinish();
-        Log.d("uninstall device information collector successfully");
         mDeviceObserver = tmpDeviceObserver;
     }
 
@@ -286,8 +330,9 @@
      * Run command to collect device info.
      */
     private void runDeviceInfoCollectorCommand() throws DeviceDisconnectedException {
-        final String commandStr = "am instrument -w -e bundle"
-            + " true android.tests.getinfo/.DeviceInfoInstrument";
+        final String commandStr = "am instrument -w -e bundle true "
+            + String.format("%s/android.tests.getinfo.DeviceInfoInstrument",
+                    DEVICE_SETUP_APP_PACKAGE_NAME);
         Log.d(commandStr);
 
         mPackageActionTimer.start(ACTION_GET_DEV_INFO, this);
@@ -1545,6 +1590,8 @@
             final LogReceiver logReceiver)
             throws DeviceDisconnectedException {
         if (mStatus == STATUS_OFFLINE) {
+            Log.d(String.format("device %s is offline when attempting to execute %s",
+                    getSerialNumber(), cmd));
             throw new DeviceDisconnectedException(getSerialNumber());
         }
 
diff --git a/tools/host/src/com/android/cts/TestHost.java b/tools/host/src/com/android/cts/TestHost.java
index 7d86ffe..90dc97f 100644
--- a/tools/host/src/com/android/cts/TestHost.java
+++ b/tools/host/src/com/android/cts/TestHost.java
@@ -458,11 +458,12 @@
         TestDevice device = sDeviceManager.allocateFreeDeviceById(deviceId);
         TestSessionLog sessionLog = ts.getSessionLog();
         ts.setTestDevice(device);
-
-        sessionLog.setDeviceInfo(device.getDeviceInfo());
+        ts.getDevice().installDeviceSetupApp();
+        sessionLog.setDeviceInfo(ts.getDevice().getDeviceInfo());
 
         boolean finish = false;
         while (!finish) {
+            ts.getDevice().disableKeyguard();
             try {
                 switch (type) {
                 case RUN_SINGLE_TEST:
@@ -498,6 +499,8 @@
         if (HostConfig.getMaxTestCount() > 0) {
             sDeviceManager.resetTestDevice(ts.getDevice());
         }
+
+        ts.getDevice().uninstallDeviceSetupApp();
     }
 
     /**