Removing per-user PiP component.

- This was added in ag/923778 for TV, where the TV recents activity, which
  is started per-user, needed to reference the PiP bounds to coordinate the
  layout of the PiP UI in recents.  As a result of that change, the PiP
  manager for both phones and TV was being instantiated multiple times,
  once for the primary user, and another for secondary/managed users.  With
  each instantiation of the PipManager, we were re-registering the input
  consumer, and once the process was killed, the input consumer was not
  being cleaned up correctly and it not longer was registered with the
  primary SystemUI which drives the PiP.

  As of ag/1964066, the TV recents code is removed, so we can now safely
  remove the PipUI component for secondary users as well, ensuring only a
  single PipManager/InputConsumerController instance.

  This does not prevent PiP from working in secondary users, but only
  leaves the input consumer and menu controller in the primary user's
  SystemUI.
- Fix some crashes when interacting with the PiP in a secondary user,
  all communication between the menu controller and the menu activity
  should be done in a parcelable way as the menu activity runs per-user
- Adding exception when the PipUI component is not created for the primary
  user
- Initial changes to dump input consumers in WM to be able to correlate
  them with SysUI's state

Bug: 35792308
Test: Ensure PiP component is not started for secondary user, verify that
      it still works on secondary users

Change-Id: I3df10860227498bc37799ad296f0a4b71b87d30e
Signed-off-by: Winson Chung <winsonc@google.com>
(cherry picked from commit 853c99a083dc6a96694373c48f427e4d3466d4a9)
diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java b/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
index be69867..bff1054 100644
--- a/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
+++ b/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
@@ -92,8 +92,7 @@
      */
     private final Class<?>[] SERVICES_PER_USER = new Class[] {
             Dependency.class,
-            Recents.class,
-            PipUI.class
+            Recents.class
     };
 
     /**
diff --git a/packages/SystemUI/src/com/android/systemui/pip/PipUI.java b/packages/SystemUI/src/com/android/systemui/pip/PipUI.java
index cc35f3c..9a8090d 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/PipUI.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/PipUI.java
@@ -23,6 +23,7 @@
 import android.content.res.Configuration;
 
 import com.android.systemui.SystemUI;
+import com.android.systemui.recents.misc.SystemServicesProxy;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
@@ -35,7 +36,6 @@
     private BasePipManager mPipManager;
 
     private boolean mSupportsPip;
-    private boolean mIsLeanBackOnly;
 
     @Override
     public void start() {
@@ -45,6 +45,12 @@
             return;
         }
 
+        // Ensure that we are the primary user's SystemUI.
+        final int processUser = SystemServicesProxy.getInstance(mContext).getProcessUser();
+        if (!SystemServicesProxy.getInstance(mContext).isSystemUser(processUser)) {
+            throw new IllegalStateException("Non-primary Pip component not currently supported.");
+        }
+
         mPipManager = pm.hasSystemFeature(FEATURE_LEANBACK_ONLY)
                 ? com.android.systemui.pip.tv.PipManager.getInstance()
                 : com.android.systemui.pip.phone.PipManager.getInstance();
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 2597ce1..ebda2e8 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivity.java
@@ -18,6 +18,7 @@
 
 import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_ACTIONS;
 import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_CONTROLLER_MESSENGER;
+import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_DISMISS_FRACTION;
 import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_MOVEMENT_BOUNDS;
 import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_SHOW_MENU;
 import static com.android.systemui.pip.phone.PipMenuActivityController.EXTRA_STACK_BOUNDS;
@@ -113,24 +114,29 @@
         @Override
         public void handleMessage(Message msg) {
             switch (msg.what) {
-                case MESSAGE_SHOW_MENU:
-                    Pair<Rect, Rect> bounds = (Pair<Rect, Rect>) msg.obj;
-                    showMenu(bounds.first, bounds.second);
+                case MESSAGE_SHOW_MENU: {
+                    final Bundle data = (Bundle) msg.obj;
+                    showMenu(data.getParcelable(EXTRA_STACK_BOUNDS),
+                            data.getParcelable(EXTRA_MOVEMENT_BOUNDS));
                     break;
+                }
                 case MESSAGE_POKE_MENU:
                     cancelDelayedFinish();
                     break;
                 case MESSAGE_HIDE_MENU:
                     hideMenu();
                     break;
-                case MESSAGE_UPDATE_ACTIONS:
-                    Pair<Rect, ParceledListSlice> data = (Pair<Rect, ParceledListSlice>) msg.obj;
-                    setActions(data.first, data.second.getList());
+                case MESSAGE_UPDATE_ACTIONS: {
+                    final Bundle data = (Bundle) msg.obj;
+                    setActions(data.getParcelable(EXTRA_STACK_BOUNDS),
+                            ((ParceledListSlice) data.getParcelable(EXTRA_ACTIONS)).getList());
                     break;
-                case MESSAGE_UPDATE_DISMISS_FRACTION:
-                    float fraction = (float) msg.obj;
-                    updateDismissFraction(fraction);
+                }
+                case MESSAGE_UPDATE_DISMISS_FRACTION: {
+                    final Bundle data = (Bundle) msg.obj;
+                    updateDismissFraction(data.getFloat(EXTRA_DISMISS_FRACTION));
                     break;
+                }
             }
         }
     });
@@ -313,9 +319,8 @@
             mActions.addAll(actions.getList());
         }
         if (intent.getBooleanExtra(EXTRA_SHOW_MENU, false)) {
-            Rect stackBounds = Rect.unflattenFromString(intent.getStringExtra(EXTRA_STACK_BOUNDS));
-            Rect movementBounds = Rect.unflattenFromString(intent.getStringExtra(
-                    EXTRA_MOVEMENT_BOUNDS));
+            Rect stackBounds = intent.getParcelableExtra(EXTRA_STACK_BOUNDS);
+            Rect movementBounds = intent.getParcelableExtra(EXTRA_MOVEMENT_BOUNDS);
             showMenu(stackBounds, movementBounds);
         }
     }
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 724f453..e2069e2 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipMenuActivityController.java
@@ -26,6 +26,7 @@
 import android.content.Intent;
 import android.content.pm.ParceledListSlice;
 import android.graphics.Rect;
+import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
 import android.os.Messenger;
@@ -56,6 +57,7 @@
     public static final String EXTRA_STACK_BOUNDS = "stack_bounds";
     public static final String EXTRA_MOVEMENT_BOUNDS = "movement_bounds";
     public static final String EXTRA_SHOW_MENU = "show_menu";
+    public static final String EXTRA_DISMISS_FRACTION = "dismiss_fraction";
 
     public static final int MESSAGE_MENU_VISIBILITY_CHANGED = 100;
     public static final int MESSAGE_EXPAND_PIP = 101;
@@ -103,6 +105,8 @@
     private ParceledListSlice mMediaActions;
     private boolean mMenuVisible;
 
+    private Bundle mTmpData = new Bundle();
+
     private boolean mStartActivityRequested;
     private Messenger mToActivityMessenger;
     private Messenger mMessenger = new Messenger(new Handler() {
@@ -191,9 +195,11 @@
      */
     public void setDismissFraction(float fraction) {
         if (mToActivityMessenger != null) {
+            mTmpData.clear();
+            mTmpData.putFloat(EXTRA_DISMISS_FRACTION, fraction);
             Message m = Message.obtain();
             m.what = PipMenuActivity.MESSAGE_UPDATE_DISMISS_FRACTION;
-            m.obj = fraction;
+            m.obj = mTmpData;
             try {
                 mToActivityMessenger.send(m);
             } catch (RemoteException e) {
@@ -210,9 +216,12 @@
      */
     public void showMenu(Rect stackBounds, Rect movementBounds) {
         if (mToActivityMessenger != null) {
+            mTmpData.clear();
+            mTmpData.putParcelable(EXTRA_STACK_BOUNDS, stackBounds);
+            mTmpData.putParcelable(EXTRA_MOVEMENT_BOUNDS, movementBounds);
             Message m = Message.obtain();
             m.what = PipMenuActivity.MESSAGE_SHOW_MENU;
-            m.obj = new Pair<>(stackBounds, movementBounds);
+            m.obj = mTmpData;
             try {
                 mToActivityMessenger.send(m);
             } catch (RemoteException e) {
@@ -290,10 +299,10 @@
                 intent.putExtra(EXTRA_CONTROLLER_MESSENGER, mMessenger);
                 intent.putExtra(EXTRA_ACTIONS, resolveMenuActions());
                 if (stackBounds != null) {
-                    intent.putExtra(EXTRA_STACK_BOUNDS, stackBounds.flattenToString());
+                    intent.putExtra(EXTRA_STACK_BOUNDS, stackBounds);
                 }
                 if (movementBounds != null) {
-                    intent.putExtra(EXTRA_MOVEMENT_BOUNDS, movementBounds.flattenToString());
+                    intent.putExtra(EXTRA_MOVEMENT_BOUNDS, movementBounds);
                 }
                 intent.putExtra(EXTRA_SHOW_MENU, showMenu);
                 ActivityOptions options = ActivityOptions.makeCustomAnimation(mContext, 0, 0);
@@ -327,9 +336,12 @@
                 Log.e(TAG, "Error showing PIP menu activity", e);
             }
 
+            mTmpData.clear();
+            mTmpData.putParcelable(EXTRA_STACK_BOUNDS, stackBounds);
+            mTmpData.putParcelable(EXTRA_ACTIONS, resolveMenuActions());
             Message m = Message.obtain();
             m.what = PipMenuActivity.MESSAGE_UPDATE_ACTIONS;
-            m.obj = new Pair<>(stackBounds, resolveMenuActions());
+            m.obj = mTmpData;
             try {
                 mToActivityMessenger.send(m);
             } catch (RemoteException e) {
diff --git a/services/core/java/com/android/server/wm/InputMonitor.java b/services/core/java/com/android/server/wm/InputMonitor.java
index dc4806a..d041c11 100644
--- a/services/core/java/com/android/server/wm/InputMonitor.java
+++ b/services/core/java/com/android/server/wm/InputMonitor.java
@@ -53,6 +53,7 @@
 
 import java.io.PrintWriter;
 import java.util.Arrays;
+import java.util.Set;
 import java.util.function.Consumer;
 
 final class InputMonitor implements InputManagerService.WindowManagerCallbacks {
@@ -583,6 +584,13 @@
         if (mInputFreezeReason != null) {
             pw.println(prefix + "mInputFreezeReason=" + mInputFreezeReason);
         }
+        final Set<String> inputConsumerKeys = mInputConsumers.keySet();
+        if (!inputConsumerKeys.isEmpty()) {
+            pw.println(prefix + "InputConsumers:");
+            for (String key : inputConsumerKeys) {
+                pw.println(prefix + "  name=" + key);
+            }
+        }
     }
 
     private final class UpdateInputForAllWindowsConsumer implements Consumer<WindowState> {