Fixed some issues with tasks with the same affinity in different stacks

- It is possible for tasks in different stacks to have the same root
affinity. This can be an issue when we are looking for the best task to
launch an acitivty into since there can be a better match in a different
stack based on class name. We now save the task matched by root afinity
and try to find a better match by class name. If we don't find a better
match we use the match based on root affinity.
- For pinned stack we don't allow root affinity matching as no other
task should be launching in the stack based on affinity.
- Correct ASS#moveActivityToStackLocked to use the passed in stack id
instead of the PINNED_STACK_ID.

Bug: 26015860
Change-Id: I6ec44bc97bf3c669c2305a58563518cf9bfc7804
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index 80d531e..2d5f76c 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -418,6 +418,12 @@
 
     private final ActivityMetricsLogger mActivityMetricsLogger;
 
+    static class FindTaskResult {
+        ActivityRecord r;
+        boolean matchedByRootAffinity;
+    }
+    private final FindTaskResult mTmpFindTaskResult = new FindTaskResult();
+
     /**
      * Description of a request to start a new activity, which has been held
      * due to app switches being disabled.
@@ -3640,7 +3646,7 @@
         }
 
         if (bounds != null) {
-            resizeStackLocked(PINNED_STACK_ID, bounds, !PRESERVE_WINDOWS, true);
+            resizeStackLocked(stackId, bounds, !PRESERVE_WINDOWS, true);
         }
 
         // The task might have already been running and its visibility needs to be synchronized with
@@ -3669,6 +3675,8 @@
     }
 
     ActivityRecord findTaskLocked(ActivityRecord r) {
+        mTmpFindTaskResult.r = null;
+        mTmpFindTaskResult.matchedByRootAffinity = false;
         if (DEBUG_TASKS) Slog.d(TAG_TASKS, "Looking for task of " + r);
         for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
             final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
@@ -3683,14 +3691,18 @@
                             "Skipping stack: (new task not allowed) " + stack);
                     continue;
                 }
-                final ActivityRecord ar = stack.findTaskLocked(r);
-                if (ar != null) {
-                    return ar;
+                stack.findTaskLocked(r, mTmpFindTaskResult);
+                // It is possible to have task in multiple stacks with the same root affinity.
+                // If the match we found was based on root affinity we keep on looking to see if
+                // there is a better match in another stack. We eventually return the match based
+                // on root affinity if we don't find a better match.
+                if (mTmpFindTaskResult.r != null && !mTmpFindTaskResult.matchedByRootAffinity) {
+                    return mTmpFindTaskResult.r;
                 }
             }
         }
-        if (DEBUG_TASKS) Slog.d(TAG_TASKS, "No task found");
-        return null;
+        if (DEBUG_TASKS && mTmpFindTaskResult.r == null) Slog.d(TAG_TASKS, "No task found");
+        return mTmpFindTaskResult.r;
     }
 
     ActivityRecord findActivityLocked(Intent intent, ActivityInfo info) {