Ensure that we hide the menu without resizing when expanding.

- This should be done when expanding/dismissing from the menu activity,
  otherwise the menu state change can cause a stack-resize animation that
  clobbers the expand or dismiss animation.

Bug: 37650245
Test: Launch Duo in PIP, end call from notification tray
Change-Id: Ie880715ae7be28a4fe2dd0d294e369b62e776c35
diff --git a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java
index d5cf1dd..5afa53f 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java
@@ -132,9 +132,6 @@
                 }
                 case MESSAGE_EXPAND_PIP: {
                     mListeners.forEach(l -> l.onPipExpand());
-                    // Preemptively mark the menu as invisible once we expand the PiP, but don't
-                    // resize as we will be animating the stack
-                    onMenuStateChanged(MENU_STATE_NONE, false /* resize */);
                     break;
                 }
                 case MESSAGE_MINIMIZE_PIP: {
@@ -143,9 +140,6 @@
                 }
                 case MESSAGE_DISMISS_PIP: {
                     mListeners.forEach(l -> l.onPipDismiss());
-                    // Preemptively mark the menu as invisible once we dismiss the PiP, but don't
-                    // resize as we'll be removing the stack in place
-                    onMenuStateChanged(MENU_STATE_NONE, false /* resize */);
                     break;
                 }
                 case MESSAGE_SHOW_MENU: {
@@ -308,6 +302,15 @@
     }
 
     /**
+     * Preemptively mark the menu as invisible, used when we are directly manipulating the pinned
+     * stack and don't want to trigger a resize which can animate the stack in a conflicting way
+     * (ie. when manually expanding or dismissing).
+     */
+    public void hideMenuWithoutResize() {
+        onMenuStateChanged(MENU_STATE_NONE, false /* resize */);
+    }
+
+    /**
      * @return the current menu state.
      */
     public int getMenuState() {
diff --git a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMotionHelper.java b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMotionHelper.java
index 5121c8d..590e3c6 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMotionHelper.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMotionHelper.java
@@ -77,6 +77,7 @@
     private SurfaceFlingerVsyncChoreographer mVsyncChoreographer;
     private Handler mHandler;
 
+    private PipMenuActivityController mMenuController;
     private PipSnapAlgorithm mSnapAlgorithm;
     private FlingAnimationUtils mFlingAnimationUtils;
 
@@ -93,10 +94,12 @@
             };
 
     public PipMotionHelper(Context context, IActivityManager activityManager,
-            PipSnapAlgorithm snapAlgorithm, FlingAnimationUtils flingAnimationUtils) {
+            PipMenuActivityController menuController, PipSnapAlgorithm snapAlgorithm,
+            FlingAnimationUtils flingAnimationUtils) {
         mContext = context;
         mHandler = BackgroundThread.getHandler();
         mActivityManager = activityManager;
+        mMenuController = menuController;
         mSnapAlgorithm = snapAlgorithm;
         mFlingAnimationUtils = flingAnimationUtils;
         mVsyncChoreographer = new SurfaceFlingerVsyncChoreographer(mHandler, mContext.getDisplay(),
@@ -148,6 +151,7 @@
      */
     void expandPip(boolean skipAnimation) {
         cancelAnimations();
+        mMenuController.hideMenuWithoutResize();
         mHandler.post(() -> {
             try {
                 if (skipAnimation) {
@@ -168,6 +172,7 @@
      */
     void dismissPip() {
         cancelAnimations();
+        mMenuController.hideMenuWithoutResize();
         mHandler.post(() -> {
             try {
                 mActivityManager.removeStack(PINNED_STACK_ID);
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 9c7e3986..71d3d35 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchHandler.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchHandler.java
@@ -191,8 +191,8 @@
         mGestures = new PipTouchGesture[] {
                 mDefaultMovementGesture
         };
-        mMotionHelper = new PipMotionHelper(mContext, mActivityManager, mSnapAlgorithm,
-                mFlingAnimationUtils);
+        mMotionHelper = new PipMotionHelper(mContext, mActivityManager, mMenuController,
+                mSnapAlgorithm, mFlingAnimationUtils);
         mExpandedShortestEdgeSize = context.getResources().getDimensionPixelSize(
                 R.dimen.pip_expanded_shortest_edge_size);