Add additional guards to ensure input consumer is re-registered.

- Also removing some old code related to menu start, which is already
  called when the menu is shown.

Bug: 34240533
Test: Open PIP, ensure that you can drag it after the menu fades out.
Change-Id: Ia16b405f8507f5c3d81fdf4f1049ce9359298672
diff --git a/packages/SystemUI/src/com/android/systemui/pip/phone/PipManager.java b/packages/SystemUI/src/com/android/systemui/pip/phone/PipManager.java
index 7e275d8..a2d7d6b 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipManager.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipManager.java
@@ -30,6 +30,9 @@
 import android.view.IWindowManager;
 import android.view.WindowManagerGlobal;
 
+import com.android.systemui.recents.misc.SystemServicesProxy;
+import com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener;
+
 /**
  * Manages the picture-in-picture (PIP) UI and states for Phones.
  */
@@ -49,6 +52,26 @@
     private PipTouchHandler mTouchHandler;
 
     /**
+     * Handler for system task stack changes.
+     */
+    TaskStackListener mTaskStackListener = new TaskStackListener() {
+        @Override
+        public void onActivityPinned() {
+            mTouchHandler.onActivityPinned();
+        }
+
+        @Override
+        public void onPinnedStackAnimationEnded() {
+            // TODO(winsonc): Disable touch interaction with the PiP until the animation ends
+        }
+
+        @Override
+        public void onPinnedActivityRestartAttempt() {
+            // TODO(winsonc): Hide the menu and expand the PiP
+        }
+    };
+
+    /**
      * Handler for messages from the PIP controller.
      */
     private class PinnedStackListener extends IPinnedStackListener.Stub {
@@ -102,6 +125,7 @@
         } catch (RemoteException e) {
             Log.e(TAG, "Failed to register pinned stack listener", e);
         }
+        SystemServicesProxy.getInstance(mContext).registerTaskStackListener(mTaskStackListener);
 
         mMenuController = new PipMenuActivityController(context, mActivityManager, mWindowManager);
         mTouchHandler = new PipTouchHandler(context, mMenuController, mActivityManager,
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 1798730..438b8dd 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java
@@ -137,13 +137,6 @@
     }
 
     @Override
-    protected void onStart() {
-        super.onStart();
-        notifyMenuVisibility(true);
-        repostDelayedFinish(INITIAL_DISMISS_DELAY);
-    }
-
-    @Override
     public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
         if (!isInPictureInPictureMode) {
             finish();
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 0350cc6..512982b 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java
@@ -75,6 +75,8 @@
                 }
                 case MESSAGE_EXPAND_PIP: {
                     mListeners.forEach(l -> l.onPipExpand());
+                    // Preemptively mark the menu as invisible once we expand the PiP
+                    mListeners.forEach(l -> l.onPipMenuVisibilityChanged(false));
                     break;
                 }
                 case MESSAGE_MINIMIZE_PIP: {
@@ -83,10 +85,16 @@
                 }
                 case MESSAGE_DISMISS_PIP: {
                     mListeners.forEach(l -> l.onPipDismiss());
+                    // Preemptively mark the menu as invisible once we dismiss the PiP
+                    mListeners.forEach(l -> l.onPipMenuVisibilityChanged(false));
                     break;
                 }
                 case MESSAGE_UPDATE_ACTIVITY_CALLBACK: {
                     mToActivityMessenger = msg.replyTo;
+                    // Mark the menu as invisible once the activity finishes as well
+                    if (mToActivityMessenger == null) {
+                        mListeners.forEach(l -> l.onPipMenuVisibilityChanged(false));
+                    }
                     break;
                 }
             }
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 d5ae3ad..18ae3cf 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchHandler.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipTouchHandler.java
@@ -212,6 +212,17 @@
         }
     }
 
+    public void onActivityPinned() {
+        // Reset some states once we are pinned
+        if (mIsTappingThrough) {
+            mIsTappingThrough = false;
+            registerInputConsumer();
+        }
+        if (mIsMinimized) {
+            setMinimizedState(false);
+        }
+    }
+
     public void onConfigurationChanged() {
         mSnapAlgorithm.onConfigurationChanged();
         updateBoundedPinnedStackBounds(false /* updatePinnedStackBounds */);