Move activity to fullscreen stack when it is relaunched in PIP.

- Only happens when the caller is not from the same package.

Bug: 33754261
Test: Open a PIP activity, try to launch it again from launcher
Change-Id: I3c364ebe31a7626b9133d9c4c1fafc718c2eecf9
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 a2d7d6b..c828ede 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipManager.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipManager.java
@@ -16,14 +16,20 @@
 
 package com.android.systemui.pip.phone;
 
+import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
 import static android.view.Display.DEFAULT_DISPLAY;
 
 import android.app.ActivityManager;
+import android.app.ActivityManager.StackInfo;
+import android.app.ActivityOptions;
 import android.app.IActivityManager;
+import android.content.ComponentName;
 import android.content.Context;
+import android.content.Intent;
 import android.content.pm.ParceledListSlice;
 import android.os.Handler;
 import android.os.RemoteException;
+import android.os.UserHandle;
 import android.util.Log;
 import android.view.IPinnedStackController;
 import android.view.IPinnedStackListener;
@@ -57,6 +63,9 @@
     TaskStackListener mTaskStackListener = new TaskStackListener() {
         @Override
         public void onActivityPinned() {
+            if (!checkCurrentUserId(false /* debug */)) {
+                return;
+            }
             mTouchHandler.onActivityPinned();
         }
 
@@ -66,8 +75,32 @@
         }
 
         @Override
-        public void onPinnedActivityRestartAttempt() {
-            // TODO(winsonc): Hide the menu and expand the PiP
+        public void onPinnedActivityRestartAttempt(ComponentName sourceComponent) {
+            if (!checkCurrentUserId(false /* debug */)) {
+                return;
+            }
+
+            // Expand the activity back to fullscreen only if it was attempted to be restarted from
+            // another package than the top activity in the stack
+            boolean expandPipToFullscreen = true;
+            if (sourceComponent != null) {
+                try {
+                    StackInfo pinnedStackInfo = mActivityManager.getStackInfo(PINNED_STACK_ID);
+                    if (pinnedStackInfo != null && pinnedStackInfo.taskIds != null &&
+                            pinnedStackInfo.taskIds.length > 0) {
+                        expandPipToFullscreen =
+                                !pinnedStackInfo.topActivity.getPackageName().equals(
+                                        sourceComponent.getPackageName());
+                    }
+                } catch (RemoteException e) {
+                    Log.w(TAG, "Unable to get pinned stack.");
+                }
+            }
+            if (expandPipToFullscreen) {
+                mTouchHandler.expandPinnedStackToFullscreen();
+            } else {
+                Log.w(TAG, "Can not expand PiP to fullscreen via intent from the same package.");
+            }
         }
     };