Fixing test due to null PiP default bounds.

- Also moving the default bounds call into the pinned stack window
  controller

Bug: 37682538
Test: bit FrameworksServicesTests:com.android.server.am.ActivityStackSupervisorTests\#testReplacingTaskInPinnedStack

Change-Id: Id6608d484617ec1a596e608aab649a082b4b1e95
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index dfeff52..61d185f 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -2977,8 +2977,8 @@
 
         // Calculate the default bounds (don't use existing stack bounds as we may have just created
         // the stack
-        final Rect destBounds = mWindowManager.getPictureInPictureBounds(DEFAULT_DISPLAY,
-                aspectRatio, false /* useExistingStackBounds */);
+        final Rect destBounds = stack.getPictureInPictureBounds(aspectRatio,
+                false /* useExistingStackBounds */);
 
         stack.animateResizePinnedStack(sourceHintBounds, destBounds, -1 /* animationDuration */,
                 true /* schedulePipModeChangedOnAnimationEnd */);
diff --git a/services/core/java/com/android/server/am/PinnedActivityStack.java b/services/core/java/com/android/server/am/PinnedActivityStack.java
index a4932bb..672f563 100644
--- a/services/core/java/com/android/server/am/PinnedActivityStack.java
+++ b/services/core/java/com/android/server/am/PinnedActivityStack.java
@@ -43,6 +43,11 @@
         return new PinnedStackWindowController(mStackId, this, displayId, onTop, outBounds);
     }
 
+    Rect getPictureInPictureBounds(float aspectRatio, boolean useExistingStackBounds) {
+        return getWindowContainerController().getPictureInPictureBounds(aspectRatio,
+                useExistingStackBounds);
+    }
+
     void animateResizePinnedStack(Rect sourceHintBounds, Rect toBounds, int animationDuration,
             boolean schedulePipModeChangedOnAnimationEnd) {
         getWindowContainerController().animateResizePinnedStack(toBounds, sourceHintBounds,
diff --git a/services/core/java/com/android/server/wm/PinnedStackWindowController.java b/services/core/java/com/android/server/wm/PinnedStackWindowController.java
index 5b9c16c..b0b93ab 100644
--- a/services/core/java/com/android/server/wm/PinnedStackWindowController.java
+++ b/services/core/java/com/android/server/wm/PinnedStackWindowController.java
@@ -17,6 +17,8 @@
 package com.android.server.wm;
 
 import static android.app.ActivityManager.StackId.FULLSCREEN_WORKSPACE_STACK_ID;
+import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
+
 import static com.android.server.wm.BoundsAnimationController.NO_PIP_MODE_CHANGED_CALLBACKS;
 import static com.android.server.wm.BoundsAnimationController.SCHEDULE_PIP_MODE_CHANGED_ON_END;
 import static com.android.server.wm.BoundsAnimationController.SCHEDULE_PIP_MODE_CHANGED_ON_START;
@@ -42,6 +44,42 @@
     }
 
     /**
+     * @param useExistingStackBounds Apply {@param aspectRatio} to the existing target stack bounds
+     *                               if possible
+     */
+    public Rect getPictureInPictureBounds(float aspectRatio, boolean useExistingStackBounds) {
+        synchronized (mWindowMap) {
+            if (!mService.mSupportsPictureInPicture || mContainer == null) {
+                return null;
+            }
+
+            final Rect stackBounds;
+            final DisplayContent displayContent = mContainer.getDisplayContent();
+            if (displayContent == null) {
+                return null;
+            }
+
+            final PinnedStackController pinnedStackController =
+                    displayContent.getPinnedStackController();
+            if (useExistingStackBounds) {
+                // If the stack exists, then use its final bounds to calculate the new aspect ratio
+                // bounds
+                stackBounds = new Rect();
+                mContainer.getAnimationOrCurrentBounds(stackBounds);
+            } else {
+                // Otherwise, just calculate the aspect ratio bounds from the default bounds
+                stackBounds = pinnedStackController.getDefaultBounds();
+            }
+
+            if (pinnedStackController.isValidPictureInPictureAspectRatio(aspectRatio)) {
+                return pinnedStackController.transformBoundsToAspectRatio(stackBounds, aspectRatio);
+            } else {
+                return stackBounds;
+            }
+        }
+    }
+
+    /**
      * Animates the pinned stack.
      */
     public void animateResizePinnedStack(Rect toBounds, Rect sourceHintBounds,
@@ -104,8 +142,7 @@
                 return;
             }
 
-            final int displayId = mContainer.getDisplayContent().getDisplayId();
-            final Rect toBounds = mService.getPictureInPictureBounds(displayId, aspectRatio,
+            final Rect toBounds = getPictureInPictureBounds(aspectRatio,
                     true /* useExistingStackBounds */);
             final Rect targetBounds = new Rect();
             mContainer.getAnimationOrCurrentBounds(targetBounds);
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index 05ec479..a7f6600 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -2761,44 +2761,6 @@
         mDockedStackCreateBounds = bounds;
     }
 
-    /**
-     * @param useExistingStackBounds Apply {@param aspectRatio} to the existing target stack bounds
-     *                               if possible
-     */
-    public Rect getPictureInPictureBounds(int displayId, float aspectRatio,
-            boolean useExistingStackBounds) {
-        synchronized (mWindowMap) {
-            if (!mSupportsPictureInPicture) {
-                return null;
-            }
-
-            final Rect stackBounds;
-            final DisplayContent displayContent = mRoot.getDisplayContent(displayId);
-            if (displayContent == null) {
-                return null;
-            }
-
-            final PinnedStackController pinnedStackController =
-                    displayContent.getPinnedStackController();
-            final TaskStack stack = displayContent.getStackById(PINNED_STACK_ID);
-            if (stack != null && useExistingStackBounds) {
-                // If the stack exists, then use its final bounds to calculate the new aspect ratio
-                // bounds.
-                stackBounds = new Rect();
-                stack.getAnimationOrCurrentBounds(stackBounds);
-            } else {
-                // Otherwise, just calculate the aspect ratio bounds from the default bounds
-                stackBounds = pinnedStackController.getDefaultBounds();
-            }
-
-            if (pinnedStackController.isValidPictureInPictureAspectRatio(aspectRatio)) {
-                return pinnedStackController.transformBoundsToAspectRatio(stackBounds, aspectRatio);
-            } else {
-                return stackBounds;
-            }
-        }
-    }
-
     public boolean isValidPictureInPictureAspectRatio(int displayId, float aspectRatio) {
         final DisplayContent displayContent = mRoot.getDisplayContent(displayId);
         return displayContent.getPinnedStackController().isValidPictureInPictureAspectRatio(
diff --git a/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java b/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java
index 3fc2c12..28051f9 100644
--- a/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java
+++ b/services/tests/servicestests/src/com/android/server/am/ActivityTestsBase.java
@@ -230,9 +230,17 @@
                 if (mStack == null) {
                     final RecentTasks recents =
                             new RecentTasks(mService, mService.mStackSupervisor);
-                    mStack = mStackId == ActivityManager.StackId.PINNED_STACK_ID
-                    ? new PinnedActivityStack(this, recents, mOnTop)
-                    : new TestActivityStack(this, recents, mOnTop);
+                    if (mStackId == ActivityManager.StackId.PINNED_STACK_ID) {
+                        mStack = new PinnedActivityStack(this, recents, mOnTop) {
+                            @Override
+                            Rect getPictureInPictureBounds(float aspectRatio,
+                                    boolean useExistingStackBounds) {
+                                return new Rect(50, 50, 100, 100);
+                            }
+                        };
+                    } else {
+                        mStack = new TestActivityStack(this, recents, mOnTop);
+                    }
                 }
 
                 return mStack;