Make SINGLE_TOP work with CLEAR_TOP.

Change-Id: Ie98b491dc4b1a2ae5e8fc8452c6410597bc9676b
diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java
index f991ee3..59529be 100644
--- a/core/java/android/content/Intent.java
+++ b/core/java/android/content/Intent.java
@@ -2128,12 +2128,14 @@
      * of activity B, then C and D will be finished and B receive the given
      * Intent, resulting in the stack now being: A, B.
      *
-     * <p>The currently running instance of task B in the above example will
+     * <p>The currently running instance of activity B in the above example will
      * either receive the new intent you are starting here in its
      * onNewIntent() method, or be itself finished and restarted with the
      * new intent.  If it has declared its launch mode to be "multiple" (the
-     * default) it will be finished and re-created; for all other launch modes
-     * it will receive the Intent in the current instance.
+     * default) and you have not set {@link #FLAG_ACTIVITY_SINGLE_TOP} in
+     * the same intent, then it will be finished and re-created; for all other
+     * launch modes or if {@link #FLAG_ACTIVITY_SINGLE_TOP} is set then this
+     * Intent will be delivered to the current instance's onNewIntent().
      *
      * <p>This launch mode can also be used to good effect in conjunction with
      * {@link #FLAG_ACTIVITY_NEW_TASK}: if used to start the root activity
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index 0d4a4bf..d4e69c0 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -2915,7 +2915,7 @@
      * or null if none was found.
      */
     private final HistoryRecord performClearTaskLocked(int taskId,
-            HistoryRecord newR, boolean doClear) {
+            HistoryRecord newR, int launchFlags, boolean doClear) {
         int i = mHistory.size();
         
         // First find the requested task.
@@ -2958,7 +2958,8 @@
                 // Finally, if this is a normal launch mode (that is, not
                 // expecting onNewIntent()), then we will finish the current
                 // instance of the activity so a new fresh one can be started.
-                if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE) {
+                if (ret.launchMode == ActivityInfo.LAUNCH_MULTIPLE
+                        && (launchFlags&Intent.FLAG_ACTIVITY_SINGLE_TOP) == 0) {
                     if (!ret.finishing) {
                         int index = indexOfTokenLocked(ret);
                         if (index >= 0) {
@@ -3355,7 +3356,7 @@
                         // cases this means we are resetting the task to its
                         // initial state.
                         HistoryRecord top = performClearTaskLocked(
-                                taskTop.task.taskId, r, true);
+                                taskTop.task.taskId, r, launchFlags, true);
                         if (top != null) {
                             if (top.frontOfTask) {
                                 // Activity aliases may mean we use different
@@ -3498,7 +3499,7 @@
                 // task, but the caller has asked to clear that task if the
                 // activity is already running.
                 HistoryRecord top = performClearTaskLocked(
-                        sourceRecord.task.taskId, r, true);
+                        sourceRecord.task.taskId, r, launchFlags, true);
                 if (top != null) {
                     logStartActivity(LOG_AM_NEW_INTENT, r, top.task);
                     deliverNewIntentLocked(top, r.intent);