Merge tag android-5.1.0_r1 into AOSP_5.1_MERGE

Change-Id: I0020bac94a9a195a94802b346055c8690a3413ac
diff --git a/cmds/monkey/src/com/android/commands/monkey/Monkey.java b/cmds/monkey/src/com/android/commands/monkey/Monkey.java
index dfcb4b0..3e6917e 100644
--- a/cmds/monkey/src/com/android/commands/monkey/Monkey.java
+++ b/cmds/monkey/src/com/android/commands/monkey/Monkey.java
@@ -49,11 +49,13 @@
 import java.io.Writer;
 import java.security.SecureRandom;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Random;
 
+import android.app.ActivityManager;
 /**
  * Application that injects random key events and other actions into the system.
  */
@@ -68,6 +70,12 @@
 
     private final static int DEBUG_ALLOW_ANY_RESTARTS = 0;
 
+    private final static String DEFAULT_LAUNCHER_PKG_NAME =
+                                "com.android.launcher";
+
+    private final static String DEFAULT_LAUNCHER_CLASS_NAME =
+                                "com.android.launcher2.Launcher";
+
     private IActivityManager mAm;
 
     private IWindowManager mWm;
@@ -208,6 +216,9 @@
     /** The random number generator **/
     Random mRandom = null;
 
+    /** Quit after the maximum number of activity launches is reached **/
+    long mMaxActivityLaunches = -1;
+
     /** Dropped-event statistics **/
     long mDroppedKeyEvents = 0;
 
@@ -225,6 +236,18 @@
     /** Device idle time. This is for the scripted monkey. **/
     long mDeviceSleepTime = 30000;
 
+    /** The delay after App switch event. **/
+    long mAppSwitchDelay = 0;
+
+    /** Ensure Launcher in foreground before launching application */
+    boolean mLauchLauncher = false;
+
+    long mLauchLauncherDelay = -1;
+
+    String mLauncherPkgName = null;
+
+    String mLauncherClsName = null;
+
     boolean mRandomizeScript = false;
 
     boolean mScriptLog = false;
@@ -235,6 +258,9 @@
     /** a filename to the setup script (if any) */
     private String mSetupFileName = null;
 
+    /** Sort the app list */
+    private boolean mSortAppList = false;
+
     /** filenames of the script (if any) */
     private ArrayList<String> mScriptFileNames = new ArrayList<String>();
 
@@ -779,6 +805,15 @@
                     mKillProcessAfterError = true;
                 } else if (opt.equals("--hprof")) {
                     mGenerateHprof = true;
+                } else if(opt.equals("--launch-app-after-launcher")){
+                    mLauchLauncher = true;
+                    mLauncherPkgName = DEFAULT_LAUNCHER_PKG_NAME;
+                    mLauncherClsName = DEFAULT_LAUNCHER_CLASS_NAME;
+                } else if(opt.equals("--launch-app-after-app")){
+                    mLauchLauncher = true;
+                    mLauchLauncherDelay = nextOptionLong("delay (in ms) after app launch");
+                    mLauncherPkgName = nextOptionData();
+                    mLauncherClsName = nextOptionData();
                 } else if (opt.equals("--pct-touch")) {
                     int i = MonkeySourceRandom.FACTOR_TOUCH;
                     mFactors[i] = -nextOptionLong("touch events percentage");
@@ -828,6 +863,8 @@
                     mServerPort = (int) nextOptionLong("Server port to listen on for commands");
                 } else if (opt.equals("--setup")) {
                     mSetupFileName = nextOptionData();
+                } else if (opt.equals("--sort-app-list")) {
+                    mSortAppList = true;
                 } else if (opt.equals("-f")) {
                     mScriptFileNames.add(nextOptionData());
                 } else if (opt.equals("--profile-wait")) {
@@ -838,6 +875,8 @@
                                                       "(in milliseconds)");
                 } else if (opt.equals("--randomize-script")) {
                     mRandomizeScript = true;
+                } else if (opt.equals("--max-activity-launches")) {
+                    mMaxActivityLaunches = nextOptionLong("max activity launches");
                 } else if (opt.equals("--script-log")) {
                     mScriptLog = true;
                 } else if (opt.equals("--bugreport")) {
@@ -845,6 +884,8 @@
                 } else if (opt.equals("--periodic-bugreport")){
                     mGetPeriodicBugreport = true;
                     mBugreportFrequency = nextOptionLong("Number of iterations");
+                } else if (opt.equals("--delay-appswitch")) {
+                    mAppSwitchDelay = nextOptionLong("Delay(in ms) after app switch event");
                 } else if (opt.equals("-h")) {
                     showUsage();
                     return false;
@@ -1038,6 +1079,16 @@
             return false;
         }
 
+        if (mSortAppList) {
+            Collections.sort(mMainApps);
+            if (mVerbose >= 2) { // very verbose
+                System.out.println("// Sorted main activity list");
+                for (ComponentName cn : mMainApps) {
+                    System.out.println("//   + Using main activity " + cn);
+                }
+            }
+        }
+
         return true;
     }
 
@@ -1052,6 +1103,7 @@
     private int runMonkeyCycles() {
         int eventCounter = 0;
         int cycleCounter = 0;
+        int numActivityLaunches = 0;
 
         boolean shouldReportAnrTraces = false;
         boolean shouldReportDumpsysMemInfo = false;
@@ -1148,7 +1200,47 @@
 
             MonkeyEvent ev = mEventSource.getNextEvent();
             if (ev != null) {
+                if (ev instanceof MonkeyActivityEvent && mLauchLauncher) {
+                    launchHomeScreen();
+                    try {
+                        long delay = mAppSwitchDelay;
+                        if (mLauchLauncherDelay >= 0) {
+                            delay = mLauchLauncherDelay;
+                        }
+                        System.out.println("After Launcher Before app launch sleep "
+                            + delay + " ms");
+                        Thread.sleep(delay);
+                    } catch (InterruptedException e1) {
+                        System.out.println("Monkey interrupted in sleep after Home Screen Launch.");
+                    }
+                }
+
+                if  ((mMaxActivityLaunches > -1) && (ev instanceof MonkeyActivityEvent)) {
+                    if (numActivityLaunches >= mMaxActivityLaunches) {
+                        if (mVerbose > 0) {
+                            System.out.println("Exit Monkey,"
+                                + " reached max number of activity launches:"
+                                + numActivityLaunches);
+                            return eventCounter;
+                        }
+                    }
+                    numActivityLaunches++;
+                }
+
                 int injectCode = ev.injectEvent(mWm, mAm, mVerbose);
+
+                if  ((mAppSwitchDelay > 0) && (ev instanceof MonkeyActivityEvent)) {
+                    if (mVerbose > 0) {
+                        System.out.println("After App switch about to sleep "
+                                + mAppSwitchDelay + " ms");
+                    }
+                    try {
+                        Thread.sleep(mAppSwitchDelay);
+                    } catch (InterruptedException e1) {
+                        System.out.println("** Monkey interrupted in sleep after App switch.");
+                    }
+                }
+
                 if (injectCode == MonkeyEvent.INJECT_FAIL) {
                     System.out.println("    // Injection Failed");
                     if (ev instanceof MonkeyKeyEvent) {
@@ -1198,6 +1290,44 @@
     }
 
     /**
+     * Check for Top Activity if not Home, launch Home
+     **/
+    private void launchHomeScreen() {
+        //Get Current forground Activity
+        boolean launcherIsForeground = isLauncherForeground();
+        // Launch HomeScreen if it is not the current top activity
+        if (!launcherIsForeground) {
+            System.out.println("Launching Home Screen pkg:"
+                + mLauncherPkgName + " class:" + mLauncherClsName);
+            ComponentName lauchHomeApp = new ComponentName(mLauncherPkgName, mLauncherClsName);
+            MonkeyActivityEvent mActEvent = new MonkeyActivityEvent(lauchHomeApp);
+            mActEvent.injectEvent(mWm, mAm, mVerbose);
+        }
+        return;
+    }
+
+    /**
+     * Returns true if the launcher is in the foreground
+     **/
+     private boolean isLauncherForeground() {
+        boolean launcherForeground = false;
+        try {
+            List<ActivityManager.RunningAppProcessInfo> mRunningProcessInfo= mAm.getRunningAppProcesses();
+            for (ActivityManager.RunningAppProcessInfo pi : mRunningProcessInfo) {
+                if (pi.processName.equals(mLauncherPkgName) &&
+                    pi.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
+                    launcherForeground = true;
+                    break;
+                }
+            }
+        } catch (Exception e) {
+            System.out.println("isLauncherForeground Exception, ex:" + e);
+            e.printStackTrace();
+        }
+        return launcherForeground;
+     }
+
+    /**
      * Send SIGNAL_USR1 to all processes. This will generate large (5mb)
      * profiling reports in data/misc, so use with care.
      */
@@ -1358,6 +1488,7 @@
         usage.append("              [--setup scriptfile] [-f scriptfile [-f scriptfile] ...]\n");
         usage.append("              [--port port]\n");
         usage.append("              [-s SEED] [-v [-v] ...]\n");
+        usage.append("              [--sort-app-list]\n");
         usage.append("              [--throttle MILLISEC] [--randomize-throttle]\n");
         usage.append("              [--profile-wait MILLISEC]\n");
         usage.append("              [--device-sleep-time MILLISEC]\n");
@@ -1365,6 +1496,9 @@
         usage.append("              [--script-log]\n");
         usage.append("              [--bugreport]\n");
         usage.append("              [--periodic-bugreport]\n");
+        usage.append("              [--delay-appswitch MILLISEC]\n");
+        usage.append("              [--launch-app-after-launcher\n");
+        usage.append("              [--launch-app-after-app MILLISEC PACKAGE_NAME CLASS_NAME]\n");
         usage.append("              COUNT\n");
         System.err.println(usage.toString());
     }
diff --git a/samples/ApiDemos/Android.mk b/samples/ApiDemos/Android.mk
index 66100b1..656718f 100644
--- a/samples/ApiDemos/Android.mk
+++ b/samples/ApiDemos/Android.mk
@@ -18,7 +18,7 @@
 
 LOCAL_SDK_VERSION := current
 
-include $(BUILD_PACKAGE)
+#include $(BUILD_PACKAGE)
 
 # Use the folloing include to make our test apk.
 include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/samples/ApiDemos/tests/Android.mk b/samples/ApiDemos/tests/Android.mk
index f975662..116b4b6 100644
--- a/samples/ApiDemos/tests/Android.mk
+++ b/samples/ApiDemos/tests/Android.mk
@@ -19,5 +19,5 @@
 
 LOCAL_SDK_VERSION := current
 
-include $(BUILD_PACKAGE)
+#include $(BUILD_PACKAGE)
 
diff --git a/sys-img/images_arm64-v8a_source.prop_template b/sys-img/images_arm64-v8a_source.prop_template
index 1a18cf8..0fa83e9 100644
--- a/sys-img/images_arm64-v8a_source.prop_template
+++ b/sys-img/images_arm64-v8a_source.prop_template
@@ -1,6 +1,6 @@
 Pkg.Desc=Android SDK Platform ${PLATFORM_VERSION}
 Pkg.UserSrc=false
-Pkg.Revision=1
+Pkg.Revision=3
 AndroidVersion.ApiLevel=${PLATFORM_SDK_VERSION}
 AndroidVersion.CodeName=${PLATFORM_VERSION_CODENAME}
 SystemImage.Abi=${TARGET_CPU_ABI}
diff --git a/sys-img/images_armeabi-v7a_source.prop_template b/sys-img/images_armeabi-v7a_source.prop_template
index 9c7a332..9def03d 100644
--- a/sys-img/images_armeabi-v7a_source.prop_template
+++ b/sys-img/images_armeabi-v7a_source.prop_template
@@ -1,6 +1,6 @@
 Pkg.Desc=Android SDK Platform ${PLATFORM_VERSION}
 Pkg.UserSrc=false
-Pkg.Revision=1
+Pkg.Revision=3
 AndroidVersion.ApiLevel=${PLATFORM_SDK_VERSION}
 AndroidVersion.CodeName=${PLATFORM_VERSION_CODENAME}
 SystemImage.Abi=armeabi-v7a
diff --git a/sys-img/images_armeabi_source.prop_template b/sys-img/images_armeabi_source.prop_template
index 91e9d21..98281d2 100644
--- a/sys-img/images_armeabi_source.prop_template
+++ b/sys-img/images_armeabi_source.prop_template
@@ -1,6 +1,6 @@
 Pkg.Desc=Android SDK Platform ${PLATFORM_VERSION}
 Pkg.UserSrc=false
-Pkg.Revision=1
+Pkg.Revision=3
 AndroidVersion.ApiLevel=${PLATFORM_SDK_VERSION}
 AndroidVersion.CodeName=${PLATFORM_VERSION_CODENAME}
 SystemImage.Abi=armeabi
diff --git a/sys-img/images_mips_source.prop_template b/sys-img/images_mips_source.prop_template
index 78dc63f..a53eb04 100644
--- a/sys-img/images_mips_source.prop_template
+++ b/sys-img/images_mips_source.prop_template
@@ -1,6 +1,6 @@
 Pkg.Desc=Android SDK Platform ${PLATFORM_VERSION}
 Pkg.UserSrc=false
-Pkg.Revision=1
+Pkg.Revision=3
 AndroidVersion.ApiLevel=${PLATFORM_SDK_VERSION}
 AndroidVersion.CodeName=${PLATFORM_VERSION_CODENAME}
 SystemImage.Abi=mips
diff --git a/sys-img/images_x86_64_source.prop_template b/sys-img/images_x86_64_source.prop_template
index 1a18cf8..0fa83e9 100644
--- a/sys-img/images_x86_64_source.prop_template
+++ b/sys-img/images_x86_64_source.prop_template
@@ -1,6 +1,6 @@
 Pkg.Desc=Android SDK Platform ${PLATFORM_VERSION}
 Pkg.UserSrc=false
-Pkg.Revision=1
+Pkg.Revision=3
 AndroidVersion.ApiLevel=${PLATFORM_SDK_VERSION}
 AndroidVersion.CodeName=${PLATFORM_VERSION_CODENAME}
 SystemImage.Abi=${TARGET_CPU_ABI}
diff --git a/sys-img/images_x86_source.prop_template b/sys-img/images_x86_source.prop_template
index 1a18cf8..0fa83e9 100644
--- a/sys-img/images_x86_source.prop_template
+++ b/sys-img/images_x86_source.prop_template
@@ -1,6 +1,6 @@
 Pkg.Desc=Android SDK Platform ${PLATFORM_VERSION}
 Pkg.UserSrc=false
-Pkg.Revision=1
+Pkg.Revision=3
 AndroidVersion.ApiLevel=${PLATFORM_SDK_VERSION}
 AndroidVersion.CodeName=${PLATFORM_VERSION_CODENAME}
 SystemImage.Abi=${TARGET_CPU_ABI}