Reparent the activity in the task with associated window containers.

- Currently moveActivityToStack() adds the activity to the top of the
  new task in the new stack, but the code path to update the window
  container controllers assumes that they are in the same task and
  attempts to position it by index.  Instead, we should properly
  reparent the activity in the new task (just like we reparent tasks
  into new stacks), and also reparent the associated app window tokens
  into their new tasks on the WM side.
- Also should fix an issue when trying to reparent an activity from one
  task to another task by affinity.

Bug: 34394702
Test: AppWidgetContainerControllerTests#testReparent
Test: android.server.cts.ActivityManagerPinnedStackTests#testEnterPipFromTaskWithMultipleActivities
Change-Id: Ib767f85eb4f469cfd1c108c55242996325bdb866
diff --git a/services/tests/servicestests/src/com/android/server/wm/WindowTestsBase.java b/services/tests/servicestests/src/com/android/server/wm/WindowTestsBase.java
index 72157b6..dd4b344 100644
--- a/services/tests/servicestests/src/com/android/server/wm/WindowTestsBase.java
+++ b/services/tests/servicestests/src/com/android/server/wm/WindowTestsBase.java
@@ -222,6 +222,16 @@
             super(sWm, null, false, dc);
         }
 
+        TestAppWindowToken(WindowManagerService service, IApplicationToken token,
+                boolean voiceInteraction, DisplayContent dc, long inputDispatchingTimeoutNanos,
+                boolean fullscreen, boolean showForAllUsers, int targetSdk, int orientation,
+                int rotationAnimationHint, int configChanges, boolean launchTaskBehind,
+                boolean alwaysFocusable, AppWindowContainerController controller) {
+            super(service, token, voiceInteraction, dc, inputDispatchingTimeoutNanos, fullscreen,
+                    showForAllUsers, targetSdk, orientation, rotationAnimationHint, configChanges,
+                    launchTaskBehind, alwaysFocusable, controller);
+        }
+
         int getWindowsCount() {
             return mChildren.size();
         }
@@ -237,6 +247,10 @@
         WindowState getLastChild() {
             return mChildren.getLast();
         }
+
+        int positionInParent() {
+            return getParent().mChildren.indexOf(this);
+        }
     }
 
     /* Used so we can gain access to some protected members of the {@link Task} class */
@@ -337,6 +351,19 @@
             mToken = token;
         }
 
+        @Override
+        AppWindowToken createAppWindow(WindowManagerService service, IApplicationToken token,
+                boolean voiceInteraction, DisplayContent dc, long inputDispatchingTimeoutNanos,
+                boolean fullscreen, boolean showForAllUsers, int targetSdk, int orientation,
+                int rotationAnimationHint, int configChanges, boolean launchTaskBehind,
+                boolean alwaysFocusable, AppWindowContainerController controller) {
+            return new TestAppWindowToken(service, token, voiceInteraction, dc,
+                    inputDispatchingTimeoutNanos, fullscreen, showForAllUsers, targetSdk,
+                    orientation,
+                    rotationAnimationHint, configChanges, launchTaskBehind, alwaysFocusable,
+                    controller);
+        }
+
         AppWindowToken getAppWindowToken() {
             return (AppWindowToken) sDisplayContent.getWindowToken(mToken.asBinder());
         }