app launch test fixes and improvements

* fixed NPE when specified app name does not exist
* force stop package before starting, because some names may
  resolve into the same package
* ensure app is launched in the order as sepcified in command
  line
* fixed time recording: it should have been 'thisTime' as
  reported by ActivityManager, to be consistent with previous
  harness

Change-Id: I411a568580feef21821dcbe6ec15884f697af6fd
diff --git a/tests/AppLaunch/src/com/android/tests/applaunch/AppLaunch.java b/tests/AppLaunch/src/com/android/tests/applaunch/AppLaunch.java
index e9374e0..af74da2 100644
--- a/tests/AppLaunch/src/com/android/tests/applaunch/AppLaunch.java
+++ b/tests/AppLaunch/src/com/android/tests/applaunch/AppLaunch.java
@@ -17,6 +17,7 @@
 
 import android.app.ActivityManager;
 import android.app.ActivityManager.ProcessErrorStateInfo;
+import android.app.IActivityManager.WaitResult;
 import android.app.ActivityManagerNative;
 import android.app.IActivityManager;
 import android.content.Context;
@@ -31,7 +32,7 @@
 import android.test.InstrumentationTestRunner;
 import android.util.Log;
 
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -79,7 +80,7 @@
     }
 
     private void parseArgs(Bundle args) {
-        mNameToResultKey = new HashMap<String, String>();
+        mNameToResultKey = new LinkedHashMap<String, String>();
         String appList = args.getString(KEY_APPS);
 
         if (appList == null)
@@ -98,8 +99,8 @@
     }
 
     private void createMappings() {
-        mNameToIntent = new HashMap<String, Intent>();
-        mNameToProcess = new HashMap<String, String>();
+        mNameToIntent = new LinkedHashMap<String, Intent>();
+        mNameToProcess = new LinkedHashMap<String, String>();
 
         PackageManager pm = getInstrumentation().getContext()
                 .getPackageManager();
@@ -127,24 +128,26 @@
         Log.i(TAG, "Starting " + appName);
 
         Intent startIntent = mNameToIntent.get(appName);
+        if (startIntent == null) {
+            Log.w(TAG, "App does not exist: " + appName);
+            return;
+        }
         AppLaunchRunnable runnable = new AppLaunchRunnable(startIntent);
         Thread t = new Thread(runnable);
-        long startTime = System.currentTimeMillis();
         t.start();
         try {
             t.join(JOIN_TIMEOUT);
         } catch (InterruptedException e) {
             // ignore
         }
-        if(t.isAlive() || (runnable.getResult() != null &&
-                runnable.getResult().result != ActivityManager.START_SUCCESS)) {
+        WaitResult result = runnable.getResult();
+        if(t.isAlive() || (result != null && result.result != ActivityManager.START_SUCCESS)) {
             Log.w(TAG, "Assuming app " + appName + " crashed.");
             reportError(appName, mNameToProcess.get(appName), results);
             return;
         }
-        long startUpTime = System.currentTimeMillis() - startTime;
-        results.putString(mNameToResultKey.get(appName), String.valueOf(startUpTime));
-        sleep(5000);
+        results.putString(mNameToResultKey.get(appName), String.valueOf(result.thisTime));
+        sleep(1000);
     }
 
     private void closeApp() {
@@ -153,7 +156,7 @@
         homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
         getInstrumentation().getContext().startActivity(homeIntent);
-        sleep(3000);
+        sleep(1000);
     }
 
     private void sleep(int time) {
@@ -198,6 +201,8 @@
 
         public void run() {
             try {
+                String packageName = mLaunchIntent.getComponent().getPackageName();
+                mAm.forceStopPackage(packageName, UserHandle.USER_CURRENT);
                 String mimeType = mLaunchIntent.getType();
                 if (mimeType == null && mLaunchIntent.getData() != null
                         && "content".equals(mLaunchIntent.getData().getScheme())) {