Fix regressions in PiP movement bounds handling

- When we return early when detecting when to skip applying the bounds
  the stored bounds and rotation weren't being updated
- In landscape, the addition of the nav bar inset in the bottom pushed
  the movement bounds to be out of range, which cause an incorrect
  offset to be calculated
- Simplified the logic to calculate the offset for the adjustment for
  IME and shelf -- keep track of the previous offset, check the previous
  movement+offset bounds with the next movement+offset bounds and only
  update if the PIP is currently between those two bounds
- Tweak to landscape policy, if there isn't enough space for the ime
  offset, then don't apply it as a part of the shift

Bug: 126199952
Test: Go to landscape + pip, open IME and dismiss IME and ensure the
      PIP is pushed off and restored correctly
Test: In portrait, test going from app/home/ime and verify that the right
      offsets are applied, and then if the stack is not in the bounds of
      the offset change, that it is not applied

Change-Id: I67eee7d321995f403137db940b06f685d0c919af
diff --git a/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchHandler.java b/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchHandler.java
index 3ec6cb7..b05058a 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchHandler.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchHandler.java
@@ -125,6 +125,7 @@
     private int mImeOffset;
     private boolean mIsShelfShowing;
     private int mShelfHeight;
+    private int mMovementBoundsExtraOffsets;
     private float mSavedSnapFraction = -1f;
     private boolean mSendingHoverAccessibilityEvents;
     private boolean mMovementWithinMinimize;
@@ -262,7 +263,7 @@
         mShelfHeight = shelfHeight;
     }
 
-    public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds, Rect animatingBounds,
+    public void onMovementBoundsChanged(Rect insetBounds, Rect normalBounds, Rect curBounds,
             boolean fromImeAdjustment, boolean fromShelfAdjustment, int displayRotation) {
         final int bottomOffset = mIsImeShowing ? mImeHeight : 0;
 
@@ -283,6 +284,12 @@
         mSnapAlgorithm.getMovementBounds(mExpandedBounds, insetBounds, expandedMovementBounds,
                 bottomOffset);
 
+        // The extra offset does not really affect the movement bounds, but are applied based on the
+        // current state (ime showing, or shelf offset) when we need to actually shift
+        int extraOffset = Math.max(
+                mIsImeShowing ? mImeOffset : 0,
+                !mIsImeShowing && mIsShelfShowing ? mShelfHeight : 0);
+
         // If this is from an IME or shelf adjustment, then we should move the PiP so that it is not
         // occluded by the IME or shelf.
         if (fromImeAdjustment || fromShelfAdjustment) {
@@ -290,41 +297,19 @@
                 // Defer the update of the current movement bounds until after the user finishes
                 // touching the screen
             } else {
-                final int adjustedOffset = Math.max(mIsImeShowing ? mImeHeight + mImeOffset : 0,
-                        mIsShelfShowing ? mShelfHeight : 0);
-                Rect normalAdjustedBounds = new Rect();
-                mSnapAlgorithm.getMovementBounds(mNormalBounds, insetBounds, normalAdjustedBounds,
-                        adjustedOffset);
-                Rect expandedAdjustedBounds = new Rect();
-                mSnapAlgorithm.getMovementBounds(mExpandedBounds, insetBounds,
-                        expandedAdjustedBounds, adjustedOffset);
-                final Rect toAdjustedBounds = mMenuState == MENU_STATE_FULL
-                        ? expandedAdjustedBounds
-                        : normalAdjustedBounds;
-                final Rect toMovementBounds = mMenuState == MENU_STATE_FULL
-                        ? expandedMovementBounds
-                        : normalMovementBounds;
-
-                // If the PIP window needs to shift to right above shelf/IME and it's already above
-                // that, don't move the PIP window.
-                if (toAdjustedBounds.bottom < mMovementBounds.bottom
-                        && animatingBounds.top < toAdjustedBounds.bottom) {
-                    return;
-                }
-
-                // If the PIP window needs to shift down due to dismissal of shelf/IME but it's way
-                // above the position as if shelf/IME shows, don't move the PIP window.
-                int movementBoundsAdjustment = toMovementBounds.bottom - mMovementBounds.bottom;
-                int offsetAdjustment = fromImeAdjustment ? mImeOffset : mShelfHeight;
-                final float bottomOffsetBufferInPx = BOTTOM_OFFSET_BUFFER_DP
+                final float offsetBufferPx = BOTTOM_OFFSET_BUFFER_DP
                         * mContext.getResources().getDisplayMetrics().density;
-                if (toAdjustedBounds.bottom >= mMovementBounds.bottom
-                        && animatingBounds.top + Math.round(bottomOffsetBufferInPx)
-                        < toAdjustedBounds.bottom - movementBoundsAdjustment - offsetAdjustment) {
-                    return;
+                final Rect toMovementBounds = mMenuState == MENU_STATE_FULL
+                        ? new Rect(expandedMovementBounds)
+                        : new Rect(normalMovementBounds);
+                final int prevBottom = mMovementBounds.bottom - mMovementBoundsExtraOffsets;
+                final int toBottom = toMovementBounds.bottom < toMovementBounds.top
+                        ? toMovementBounds.bottom
+                        : toMovementBounds.bottom - extraOffset;
+                if ((Math.min(prevBottom, toBottom) - offsetBufferPx) <= curBounds.top
+                        && curBounds.top <= (Math.max(prevBottom, toBottom) + offsetBufferPx)) {
+                    mMotionHelper.animateToOffset(curBounds, toBottom - curBounds.top);
                 }
-
-                animateToOffset(animatingBounds, toAdjustedBounds);
             }
         }
 
@@ -335,6 +320,7 @@
         mDisplayRotation = displayRotation;
         mInsetBounds.set(insetBounds);
         updateMovementBounds(mMenuState);
+        mMovementBoundsExtraOffsets = extraOffset;
 
         // If we have a deferred resize, apply it now
         if (mDeferResizeToNormalBoundsUntilRotation == displayRotation) {
@@ -346,14 +332,6 @@
         }
     }
 
-    private void animateToOffset(Rect animatingBounds, Rect toAdjustedBounds) {
-        int offset = toAdjustedBounds.bottom - animatingBounds.top;
-        // In landscape mode, PIP window can go offset while launching IME. We want to align the
-        // the top of the PIP window with the top of the movement bounds in that case.
-        offset += Math.max(0, mMovementBounds.top - animatingBounds.top);
-        mMotionHelper.animateToOffset(animatingBounds, offset);
-    }
-
     private void onRegistrationChanged(boolean isRegistered) {
         mAccessibilityManager.setPictureInPictureActionReplacingConnection(isRegistered
                 ? new PipAccessibilityInteractionConnection(mMotionHelper,