Fix issue #17322903: Crash in systemUI while launching an app from recents tab

We now do sane things when adding a NEW_TASK to a task that already
has activities in it.

Change-Id: I965d6d44f9523e6199b1d90293f61ae5b8c352a4
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index 1d2f7a9..5a95ef8 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -1729,16 +1729,19 @@
                         | (baseIntent.getFlags()&flagsOfInterest);
                 intent.setFlags(launchFlags);
                 inTask.setIntent(r);
+                addingToTask = true;
 
-            // If the task is not empty, then we are going to add the new activity on top
-            // of the task, so it can not be launching as a new task.
+            // If the task is not empty and the caller is asking to start it as the root
+            // of a new task, then we don't actually want to start this on the task.  We
+            // will bring the task to the front, and possibly give it a new intent.
             } else if ((launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
-                ActivityOptions.abort(options);
-                throw new IllegalStateException("Caller has inTask " + inTask
-                        + " but target is a new task");
+                addingToTask = false;
+
+            } else {
+                addingToTask = true;
             }
+
             reuseTask = inTask;
-            addingToTask = true;
         } else {
             inTask = null;
         }
@@ -1979,7 +1982,7 @@
                 sourceRecord.task : null;
 
         // Should this be considered a new task?
-        if (r.resultTo == null && !addingToTask
+        if (r.resultTo == null && inTask == null && !addingToTask
                 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
             if (isLockTaskModeViolation(reuseTask)) {
                 Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
@@ -2092,6 +2095,13 @@
                 }
             }
 
+            if (!addingToTask) {
+                // We don't actually want to have this activity added to the task, so just
+                // stop here but still tell the caller that we consumed the intent.
+                ActivityOptions.abort(options);
+                return ActivityManager.START_TASK_TO_FRONT;
+            }
+
             r.setTask(inTask, null);
             if (DEBUG_TASKS) Slog.v(TAG, "Starting new activity " + r
                     + " in explicit task " + r.task);