DismissKeyguardUtil tweaks.

Add activity so it can be prelaunched and thus work on ICS+.
Change makefile to build it in data app.
Add ability to AppSetup to skip uninstalling packages, so DismissKeyguardUtil
can be left installed on device.

Bug 7697481

Change-Id: I464b21a6961df029e3ea30037bd6902ea2bbe3ba
diff --git a/Android.mk b/Android.mk
index dcd7d86..d17703f 100644
--- a/Android.mk
+++ b/Android.mk
@@ -71,7 +71,7 @@
 tradefed_dist_host_exes := tradefed.sh
 tradefed_dist_host_exe_files := $(foreach m, $(tradefed_dist_host_exes), $(BUILD_OUT_EXECUTABLES)/$(m))
 
-tradefed_dist_test_apks := TradeFedUiTestApp TradeFedTestApp
+tradefed_dist_test_apks := TradeFedUiTestApp TradeFedTestApp DismissKeyguardUtil
 tradefed_dist_test_apk_files := $(foreach m, $(tradefed_dist_test_apks), $(TARGET_OUT_DATA_APPS)/$(m).apk)
 
 tradefed_dist_files := \
diff --git a/src/com/android/tradefed/targetprep/AppSetup.java b/src/com/android/tradefed/targetprep/AppSetup.java
index d1120a1..75c32fb 100644
--- a/src/com/android/tradefed/targetprep/AppSetup.java
+++ b/src/com/android/tradefed/targetprep/AppSetup.java
@@ -15,30 +15,37 @@
  */
 package com.android.tradefed.targetprep;
 
-import com.android.ddmlib.Log;
 import com.android.tradefed.build.IAppBuildInfo;
 import com.android.tradefed.build.IBuildInfo;
 import com.android.tradefed.build.VersionedFile;
 import com.android.tradefed.config.Option;
+import com.android.tradefed.config.OptionClass;
 import com.android.tradefed.device.DeviceNotAvailableException;
 import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.log.LogUtil.CLog;
 
+import java.util.HashSet;
 import java.util.Set;
 
 /**
  * A {@link ITargetPreparer} that installs an apk and its tests.
  */
+@OptionClass(alias="app-setup")
 public class AppSetup implements ITargetPreparer, ITargetCleaner {
 
-    private static final String LOG_TAG = "AppSetup";
-
-    @Option(name="reboot", description="reboot device during setup.")
+    @Option(name = "reboot", description = "reboot device during setup.")
     private boolean mReboot = true;
 
-    @Option(name="uninstall", description="uninstall all apks after test completes.")
+    @Option(name = "install", description = "install all apks in build.")
+    private boolean mInstall = true;
+
+    @Option(name = "uninstall", description = "uninstall all apks after test completes.")
     private boolean mUninstall = true;
 
+    @Option(name = "skip-uninstall-pkg", description =
+            "force retention of this package when --uninstall is set.")
+    private Set<String> mSkipUninstallPkgs = new HashSet<String>();
+
     /**
      * {@inheritDoc}
      */
@@ -49,18 +56,21 @@
             throw new IllegalArgumentException("Provided buildInfo is not a AppBuildInfo");
         }
         IAppBuildInfo appBuild = (IAppBuildInfo)buildInfo;
-        Log.i(LOG_TAG, String.format("Performing setup on %s", device.getSerialNumber()));
+        CLog.i("Performing setup on %s", device.getSerialNumber());
 
         if (mReboot) {
             // reboot device to get a clean state
             device.reboot();
         }
 
-        for (VersionedFile apkFile : appBuild.getAppPackageFiles()) {
-            String result = device.installPackage(apkFile.getFile(), true);
-            if (result != null) {
-                throw new TargetSetupError(String.format("Failed to install %s on %s. Reason: %s",
-                        apkFile.getFile().getName(), device.getSerialNumber(), result));
+        if (mInstall) {
+            for (VersionedFile apkFile : appBuild.getAppPackageFiles()) {
+                String result = device.installPackage(apkFile.getFile(), true);
+                if (result != null) {
+                    throw new TargetSetupError(String.format(
+                            "Failed to install %s on %s. Reason: %s",
+                            apkFile.getFile().getName(), device.getSerialNumber(), result));
+                }
             }
         }
     }
@@ -74,10 +84,12 @@
         if (mUninstall) {
             Set<String> pkgs = device.getInstalledNonSystemPackageNames();
             for (String pkg : pkgs) {
-                String result = device.uninstallPackage(pkg);
-                if (result != null) {
-                    CLog.w("Uninstall of %s on %s failed: %s", pkg, device.getSerialNumber(),
-                            result);
+                if (!mSkipUninstallPkgs.contains(pkg)) {
+                    String result = device.uninstallPackage(pkg);
+                    if (result != null) {
+                        CLog.w("Uninstall of %s on %s failed: %s", pkg, device.getSerialNumber(),
+                                result);
+                    }
                 }
             }
         }
diff --git a/util-apps/DismissKeyguardUtil/Android.mk b/util-apps/DismissKeyguardUtil/Android.mk
index 91b8934..fff2c7c 100644
--- a/util-apps/DismissKeyguardUtil/Android.mk
+++ b/util-apps/DismissKeyguardUtil/Android.mk
@@ -17,6 +17,7 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 LOCAL_SDK_VERSION := 8
 LOCAL_PACKAGE_NAME := DismissKeyguardUtil
diff --git a/util-apps/DismissKeyguardUtil/AndroidManifest.xml b/util-apps/DismissKeyguardUtil/AndroidManifest.xml
index c4d00c6..4620642 100644
--- a/util-apps/DismissKeyguardUtil/AndroidManifest.xml
+++ b/util-apps/DismissKeyguardUtil/AndroidManifest.xml
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2012 The Android Open Source Project
+<!--
+     Copyright (C) 2012 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.
@@ -15,18 +16,28 @@
 -->
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    package="com.android.tradefed.utils">
+    package="com.android.tradefed.utils" >
 
     <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
 
     <application>
-        <receiver android:name=".DismissKeyguardIntentReceiver" >
+        <receiver android:name="com.android.tradefed.utils.DismissKeyguardIntentReceiver" >
             <intent-filter>
                 <action android:name="com.android.tradefed.utils.DISMISS_KEYGUARD" />
             </intent-filter>
         </receiver>
 
-        <service android:name=".DismissKeyguardService" />
+        <service android:name="com.android.tradefed.utils.DismissKeyguardService" />
+
+        <activity
+            android:name="com.android.tradefed.utils.MainActivity"
+            android:label="@string/title_activity_main" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
     </application>
 
-</manifest>
+</manifest>
\ No newline at end of file
diff --git a/util-apps/DismissKeyguardUtil/res/layout/activity_main.xml b/util-apps/DismissKeyguardUtil/res/layout/activity_main.xml
new file mode 100644
index 0000000..9c2ffef
--- /dev/null
+++ b/util-apps/DismissKeyguardUtil/res/layout/activity_main.xml
@@ -0,0 +1,29 @@
+<!--
+     Copyright (C) 2012 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.
+-->
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context=".MainActivity" >
+
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_centerHorizontal="true"
+        android:layout_centerVertical="true"
+        android:text="@string/hello_world" />
+
+</RelativeLayout>
\ No newline at end of file
diff --git a/util-apps/DismissKeyguardUtil/res/values/strings.xml b/util-apps/DismissKeyguardUtil/res/values/strings.xml
new file mode 100644
index 0000000..c8b4b6a
--- /dev/null
+++ b/util-apps/DismissKeyguardUtil/res/values/strings.xml
@@ -0,0 +1,21 @@
+<!--
+     Copyright (C) 2012 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.
+-->
+<resources>
+
+    <string name="title_activity_main">MainActivity</string>
+    <string name="hello_world">This is the dismiss keyguard util!</string>
+
+</resources>
\ No newline at end of file
diff --git a/util-apps/DismissKeyguardUtil/src/com/android/tradefed/utils/MainActivity.java b/util-apps/DismissKeyguardUtil/src/com/android/tradefed/utils/MainActivity.java
new file mode 100644
index 0000000..2ecd90d
--- /dev/null
+++ b/util-apps/DismissKeyguardUtil/src/com/android/tradefed/utils/MainActivity.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2012 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.tradefed.utils;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+/**
+ * Dummy activity used to launch app for first time, so in future it can handle Dismiss Keyguard
+ * intents on ICS and above devices.
+ */
+public class MainActivity extends Activity {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_main);
+    }
+}