Fixing issue with TaskRecord lookup.

- In ag/1721556, we switched from looking up the task in WM to looking
  up the task in AM (and using the window container controller). However,
  AM's call, anyTaskForIdLocked(), will return tasks in the recent tasks
  list along with the current stacks.  The naming of the parameter
  restoreFromRecents is partly to blame as it does not prevent lookup in
  the recent tasks and only prevents that task from being restored into
  the provided stack.

  As a result, when we removed a task from a stack, we also remove the
  associated window container controller, but when we query for the task
  to make a WCC call to it, it finds the one in the recent tasks list
  and happily uses that one (which crashes because we've the WCC).

  Instead, we break up the call to explicitly search for tasks in the
  stacks only, in stacks or recents, and in stacks or recents with restore.
  All existing calls to anyTaskForIdLocked() that are actually looking up
  a task for a WCC call (like those in ag/1721556) can use STACK_ONLY,
  while existing calls will match recents tasks and restore as required.

Bug: 35960163
Test: Open up settings > display > brightness dialog, quick switch to
      another task using overview

Change-Id: I835116fa80d47450da8c4de41fe0d54fc70cb43b
Signed-off-by: Winson Chung <winsonc@google.com>
diff --git a/services/core/java/com/android/server/am/TaskPersister.java b/services/core/java/com/android/server/am/TaskPersister.java
index 9dde39e..9deabb3 100644
--- a/services/core/java/com/android/server/am/TaskPersister.java
+++ b/services/core/java/com/android/server/am/TaskPersister.java
@@ -55,6 +55,9 @@
 import java.util.List;
 
 import static android.app.ActivityManager.StackId.HOME_STACK_ID;
+import static android.app.ActivityManager.StackId.INVALID_STACK_ID;
+
+import static com.android.server.am.ActivityStackSupervisor.MATCH_TASK_IN_STACKS_OR_RECENT_TASKS;
 
 public class TaskPersister {
     static final String TAG = "TaskPersister";
@@ -452,7 +455,8 @@
 
                                 final int taskId = task.taskId;
                                 if (mStackSupervisor.anyTaskForIdLocked(taskId,
-                                        /* restoreFromRecents= */ false, HOME_STACK_ID) != null) {
+                                        MATCH_TASK_IN_STACKS_OR_RECENT_TASKS,
+                                        INVALID_STACK_ID) != null) {
                                     // Should not happen.
                                     Slog.wtf(TAG, "Existing task with taskId " + taskId + "found");
                                 } else if (userId != task.userId) {