Hide PiP menu when expanding

When expanding the PiP window, hide first the close/resize button/handle
if applicable to eliminate possible jankiness.

Vide: http://go/recall/-/aaaaaabFQoRHlzixHdtY/fNFmYOXaILVu0OpHtBNDey
Bug: 157303621
Test: manually expand PiP window
Change-Id: Ic15173f96fc02e38728957e64bb8d90cada8e974
diff --git a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java
index 3396f70..a3185a2 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java
@@ -99,6 +99,7 @@
     public static final int MESSAGE_ANIMATION_ENDED = 6;
     public static final int MESSAGE_POINTER_EVENT = 7;
     public static final int MESSAGE_MENU_EXPANDED = 8;
+    public static final int MESSAGE_FADE_OUT_MENU = 9;
 
     private static final int INITIAL_DISMISS_DELAY = 3500;
     private static final int POST_INTERACTION_DISMISS_DELAY = 2000;
@@ -182,6 +183,10 @@
                     mMenuContainerAnimator.start();
                     break;
                 }
+                case MESSAGE_FADE_OUT_MENU: {
+                    fadeOutMenu();
+                    break;
+                }
             }
         }
     };
@@ -409,6 +414,18 @@
         }
     }
 
+    /**
+     * Different from {@link #hideMenu()}, this function does not try to finish this menu activity
+     * and instead, it fades out the controls by setting the alpha to 0 directly without menu
+     * visibility callbacks invoked.
+     */
+    private void fadeOutMenu() {
+        mMenuContainer.setAlpha(0f);
+        mSettingsButton.setAlpha(0f);
+        mDismissButton.setAlpha(0f);
+        mResizeHandle.setAlpha(0f);
+    }
+
     private void hideMenu() {
         hideMenu(null);
     }
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 bf2c3e9..8b4d932 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java
@@ -262,6 +262,9 @@
      */
     public void showMenuWithDelay(int menuState, Rect stackBounds, boolean allowMenuTimeout,
             boolean willResizeMenu, boolean showResizeHandle) {
+        // hide all visible controls including close button and etc. first, this is to ensure
+        // menu is totally invisible during the transition to eliminate unpleasant artifacts
+        fadeOutMenu();
         showMenuInternal(menuState, stackBounds, allowMenuTimeout, willResizeMenu,
                 true /* withDelay */, showResizeHandle);
     }
@@ -347,6 +350,23 @@
         }
     }
 
+    private void fadeOutMenu() {
+        if (DEBUG) {
+            Log.d(TAG, "fadeOutMenu() state=" + mMenuState
+                    + " hasActivity=" + (mToActivityMessenger != null)
+                    + " callers=\n" + Debug.getCallers(5, "    "));
+        }
+        if (mToActivityMessenger != null) {
+            Message m = Message.obtain();
+            m.what = PipMenuActivity.MESSAGE_FADE_OUT_MENU;
+            try {
+                mToActivityMessenger.send(m);
+            } catch (RemoteException e) {
+                Log.e(TAG, "Could not notify menu to fade out", e);
+            }
+        }
+    }
+
     /**
      * Hides the menu activity.
      */
@@ -513,7 +533,8 @@
     private void onMenuStateChanged(int menuState, boolean resize, Runnable callback) {
         if (DEBUG) {
             Log.d(TAG, "onMenuStateChanged() mMenuState=" + mMenuState
-                    + " menuState=" + menuState + " resize=" + resize);
+                    + " menuState=" + menuState + " resize=" + resize
+                    + " callers=\n" + Debug.getCallers(5, "    "));
         }
 
         if (menuState != mMenuState) {