Verify the pinned stack boundary when adjusting offset.

When user touch home key and moved activity to pinned stack, Nexus
launcher also calls setShelfHeight to adjust pinned bounds, and then the
PinnedStackController sends the animation bounds to system ui to update
it to current bounds.
However, if system ui receives this bounds before system server process
configuration changes(such as rotation), the bounds sent to system ui will
become obsolete. Then system ui will call
ActivityTaskManager#resizeStack to overwrite the correct bounds with
the obsolete one.
In this case, what system ui want to do is adjust the PIP position with
an offset, so sending an offset is sufficient instead of sending a new
bounds to system server.

Fix: 122058651
Test: Manual test the pinned stack can be adjust with show/hide IME and shelf.
Test: atest ActivityManagerPinnedStackTests ActivityManagerAppConfigurationTests

Change-Id: I5e9271812c00b8e1d45af7a774b99b780102ad23
diff --git a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
index 875fc4e..7e9979d 100644
--- a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
+++ b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
@@ -2452,6 +2452,40 @@
         }
     }
 
+    @Override
+    public void offsetPinnedStackBounds(int stackId, Rect compareBounds, int xOffset, int yOffset,
+            int animationDuration) {
+        enforceCallerIsRecentsOrHasPermission(MANAGE_ACTIVITY_STACKS, "offsetPinnedStackBounds()");
+
+        final long ident = Binder.clearCallingIdentity();
+        try {
+            synchronized (mGlobalLock) {
+                if (xOffset == 0 && yOffset == 0) {
+                    return;
+                }
+                final ActivityStack stack = mRootActivityContainer.getStack(stackId);
+                if (stack == null) {
+                    Slog.w(TAG, "offsetPinnedStackBounds: stackId " + stackId + " not found.");
+                    return;
+                }
+                if (stack.getWindowingMode() != WINDOWING_MODE_PINNED) {
+                    throw new IllegalArgumentException("Stack: " + stackId
+                            + " doesn't support animated resize.");
+                }
+                final Rect destBounds = new Rect();
+                stack.getAnimationOrCurrentBounds(destBounds);
+                if (!destBounds.isEmpty() || !destBounds.equals(compareBounds)) {
+                    Slog.w(TAG, "The current stack bounds does not matched! It may be obsolete.");
+                    return;
+                }
+                destBounds.offset(xOffset, yOffset);
+                stack.animateResizePinnedStack(null /* sourceHintBounds */, destBounds,
+                        animationDuration, false /* fromFullscreen */);
+            }
+        } finally {
+            Binder.restoreCallingIdentity(ident);
+        }
+    }
     /**
      * Moves the specified task to the primary-split-screen stack.
      *