Move attach/detach PiP menu calls to onTaskAppear/Vanish.

In an ideal case, onActivityPinned should always be followed by
an onActivityUpinned call in a PiP session. However, there are
possible times where it doesn't happen; PIP can crash suddenly,
or some other calls force closes PiP without it
getting a chance to call on Unpinned. In that case, menu isn't properly
cleaned up, so let's just make sure it is before trying to re-attach.
At the same time, move the call to onTaskAppear/Vanish, which is
guaranteed to be called.

Bug: 172268911
Bug: 172258041
Bug: 170315895
Test: Manual; Pause PiP, unpause PiP, menu still showing
Test: Locally hack so that detach doesn't get called the first time
(simulate crash), start PiP again - doesn't crash

Change-Id: I76984de59515f4178561ba92ae692fcc3c5edcb9
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java
index 39772fb..b2f4b55 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTaskOrganizer.java
@@ -495,6 +495,8 @@
             mOnDisplayIdChangeCallback.accept(info.displayId);
         }
 
+        mMenuActivityController.onTaskAppeared();
+
         if (mShouldIgnoreEnteringPipTransition) {
             final Rect destinationBounds = mPipBoundsState.getBounds();
             // animation is finished in the Launcher and here we directly apply the final touch.
@@ -666,6 +668,7 @@
         mPictureInPictureParams = null;
         mState = State.UNDEFINED;
         mPipUiEventLoggerLogger.setTaskInfo(null);
+        mMenuActivityController.onTaskVanished();
     }
 
     @Override
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipController.java
index 37a5919..49129b7 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipController.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipController.java
@@ -276,7 +276,6 @@
         mMainExecutor.execute(() -> {
             mTouchHandler.onActivityPinned();
             mMediaController.onActivityPinned();
-            mMenuController.onActivityPinned();
             mAppOpsListener.onActivityPinned(packageName);
         });
     }
@@ -284,7 +283,6 @@
     @Override
     public void onActivityUnpinned(ComponentName topActivity) {
         mMainExecutor.execute(() -> {
-            mMenuController.onActivityUnpinned();
             mTouchHandler.onActivityUnpinned(topActivity);
             mAppOpsListener.onActivityUnpinned();
         });
diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipMenuActivityController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipMenuActivityController.java
index 24144b2..aa367fcc 100644
--- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipMenuActivityController.java
+++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/phone/PipMenuActivityController.java
@@ -134,15 +134,22 @@
         return mPipMenuView != null && mMenuState != MENU_STATE_NONE;
     }
 
-    public void onActivityPinned() {
+    /**
+     * Attach the menu when the PiP task first appears.
+     */
+    public void onTaskAppeared() {
         attachPipMenuView();
     }
 
-    public void onActivityUnpinned() {
+    /**
+     * Detach the menu when the PiP task is gone.
+     */
+    public void onTaskVanished() {
         hideMenu();
         detachPipMenuView();
     }
 
+
     public void onPinnedStackAnimationEnded() {
         if (isMenuVisible()) {
             mPipMenuView.onPipAnimationEnded();
@@ -150,10 +157,11 @@
     }
 
     private void attachPipMenuView() {
-        if (mPipMenuView == null) {
-            mPipMenuView = new PipMenuView(mContext, this);
+        // In case detach was not called (e.g. PIP unexpectedly closed)
+        if (mPipMenuView != null) {
+            detachPipMenuView();
         }
-
+        mPipMenuView = new PipMenuView(mContext, this);
         mSystemWindows.addView(mPipMenuView, getPipMenuLayoutParams(0, 0), 0, SHELL_ROOT_LAYER_PIP);
     }