Create new window-side task on demand.

The activity manager can create a new task under certain
circumstances in resetTargetTaskIfNeededLocked(). In that case the
window manager was not told to create a new task. This fix creates the
task in window manager when it recognizes that there is a new task
required.

Fixes bug 10412588.

Change-Id: Ife9ecfeb7f53f9df5950322c9359c836adc258a2
diff --git a/services/java/com/android/server/wm/WindowManagerService.java b/services/java/com/android/server/wm/WindowManagerService.java
index a888731..3db807f 100644
--- a/services/java/com/android/server/wm/WindowManagerService.java
+++ b/services/java/com/android/server/wm/WindowManagerService.java
@@ -3379,6 +3379,18 @@
         Binder.restoreCallingIdentity(origId);
     }
 
+    private Task createTask(int taskId, int stackId, int userId, AppWindowToken atoken) {
+        final TaskStack stack = mStackIdToStack.get(stackId);
+        if (stack == null) {
+            throw new IllegalArgumentException("addAppToken: invalid stackId=" + stackId);
+        }
+        Task task = new Task(atoken, stack, userId);
+        stack.addTask(task, true);
+        stack.getDisplayContent().moveStack(stack, true);
+        mTaskIdToTask.put(taskId, task);
+        return task;
+    }
+
     @Override
     public void addAppToken(int addPos, IApplicationToken token, int taskId, int stackId,
             int requestedOrientation, boolean fullscreen, boolean showWhenLocked, int userId) {
@@ -3418,14 +3430,7 @@
 
             Task task = mTaskIdToTask.get(taskId);
             if (task == null) {
-                TaskStack stack = mStackIdToStack.get(stackId);
-                if (stack == null) {
-                    throw new IllegalArgumentException("addAppToken: invalid stackId=" + stackId);
-                }
-                task = new Task(atoken, stack, userId);
-                stack.addTask(task, true);
-                stack.getDisplayContent().moveStack(stack, true);
-                mTaskIdToTask.put(taskId, task);
+                task = createTask(taskId, stackId, userId, atoken);
             } else {
                 task.addAppToken(addPos, atoken);
             }
@@ -3459,8 +3464,7 @@
             atoken.groupId = groupId;
             Task newTask = mTaskIdToTask.get(groupId);
             if (newTask == null) {
-                throw new IllegalStateException("setAppGroupId: groupId=" + groupId
-                        + " does not exist");
+                newTask = createTask(groupId, oldTask.mStack.mStackId, oldTask.mUserId, atoken);
             }
             newTask.mAppTokens.add(atoken);
         }
@@ -4786,6 +4790,7 @@
             synchronized(mWindowMap) {
                 Task task = mTaskIdToTask.get(taskId);
                 if (task == null) {
+                    // Normal behavior, addAppToken will be called next and task will be created.
                     return;
                 }
                 final TaskStack stack = task.mStack;