Fix trampoline activities when relaunching PiP

- We should be checking the actual launched-from package since that stores
  the source package across trampoline activities

Bug: 35458117
Test: Enter PiP from a trampoline activity, launch again from Launcher
      and ensure that it is expanded

Change-Id: Ia0e586e8b21dee63b513bd61a41a24e7da4325e1
diff --git a/core/java/android/app/ITaskStackListener.aidl b/core/java/android/app/ITaskStackListener.aidl
index 5e420c0..9834350 100644
--- a/core/java/android/app/ITaskStackListener.aidl
+++ b/core/java/android/app/ITaskStackListener.aidl
@@ -32,9 +32,10 @@
      * running in the pinned stack and the activity is not actually started, but the task is either
      * brought to the front or a new Intent is delivered to it.
      *
-     * @param sourceComponent the component name of the activity that initiated the restart attempt
+     * @param launchedFromPackage the package name of the activity that initiated the restart
+     *                            attempt
      */
-    void onPinnedActivityRestartAttempt(in ComponentName sourceComponent);
+    void onPinnedActivityRestartAttempt(String launchedFromPackage);
 
     /**
      * Called whenever the pinned stack is done animating a resize.
diff --git a/core/java/android/app/TaskStackListener.java b/core/java/android/app/TaskStackListener.java
index 35c67d3..5a0845f 100644
--- a/core/java/android/app/TaskStackListener.java
+++ b/core/java/android/app/TaskStackListener.java
@@ -35,7 +35,7 @@
     }
 
     @Override
-    public void onPinnedActivityRestartAttempt(ComponentName sourceComponent) throws RemoteException {
+    public void onPinnedActivityRestartAttempt(String launchedFromPackage) throws RemoteException {
     }
 
     @Override
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 42f1b14..ae402ef 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/phone/PipManager.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/phone/PipManager.java
@@ -76,7 +76,7 @@
         }
 
         @Override
-        public void onPinnedActivityRestartAttempt(ComponentName sourceComponent) {
+        public void onPinnedActivityRestartAttempt(String launchedFromPackage) {
             if (!checkCurrentUserId(false /* debug */)) {
                 return;
             }
@@ -84,11 +84,11 @@
             // 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) {
+            if (launchedFromPackage != null) {
                 ComponentName topActivity = PipUtils.getTopPinnedActivity(mContext,
                         mActivityManager);
-                if (topActivity != null && topActivity.getPackageName().equals(
-                        sourceComponent.getPackageName())) {
+                if (topActivity != null
+                        && topActivity.getPackageName().equals(launchedFromPackage)) {
                     expandPipToFullscreen = false;
                 }
             }
diff --git a/packages/SystemUI/src/com/android/systemui/pip/tv/PipManager.java b/packages/SystemUI/src/com/android/systemui/pip/tv/PipManager.java
index 112fedb..376a0b6 100644
--- a/packages/SystemUI/src/com/android/systemui/pip/tv/PipManager.java
+++ b/packages/SystemUI/src/com/android/systemui/pip/tv/PipManager.java
@@ -695,7 +695,7 @@
         }
 
         @Override
-        public void onPinnedActivityRestartAttempt(ComponentName sourceComponent) {
+        public void onPinnedActivityRestartAttempt(String launchedFromPackage) {
             if (DEBUG) Log.d(TAG, "onPinnedActivityRestartAttempt()");
             if (!checkCurrentUserId(DEBUG)) {
                 return;
diff --git a/packages/SystemUI/src/com/android/systemui/recents/misc/SystemServicesProxy.java b/packages/SystemUI/src/com/android/systemui/recents/misc/SystemServicesProxy.java
index eae1b81..cda902b 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/misc/SystemServicesProxy.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/misc/SystemServicesProxy.java
@@ -153,7 +153,7 @@
         public void onTaskStackChanged() { }
         public void onTaskSnapshotChanged(int taskId, TaskSnapshot snapshot) { }
         public void onActivityPinned() { }
-        public void onPinnedActivityRestartAttempt(ComponentName sourceComponent) { }
+        public void onPinnedActivityRestartAttempt(String launchedFromPackage) { }
         public void onPinnedStackAnimationEnded() { }
         public void onActivityForcedResizable(String packageName, int taskId) { }
         public void onActivityDismissingDockedStack() { }
@@ -198,10 +198,10 @@
         }
 
         @Override
-        public void onPinnedActivityRestartAttempt(ComponentName sourceComponent)
+        public void onPinnedActivityRestartAttempt(String launchedFromPackage)
                 throws RemoteException{
             mHandler.removeMessages(H.ON_PINNED_ACTIVITY_RESTART_ATTEMPT);
-            mHandler.obtainMessage(H.ON_PINNED_ACTIVITY_RESTART_ATTEMPT, sourceComponent)
+            mHandler.obtainMessage(H.ON_PINNED_ACTIVITY_RESTART_ATTEMPT, launchedFromPackage)
                     .sendToTarget();
         }
 
@@ -1244,8 +1244,7 @@
                 }
                 case ON_PINNED_ACTIVITY_RESTART_ATTEMPT: {
                     for (int i = mTaskStackListeners.size() - 1; i >= 0; i--) {
-                        mTaskStackListeners.get(i).onPinnedActivityRestartAttempt(
-                                (ComponentName) msg.obj);
+                        mTaskStackListeners.get(i).onPinnedActivityRestartAttempt((String) msg.obj);
                     }
                     break;
                 }
diff --git a/services/core/java/com/android/server/am/ActivityStarter.java b/services/core/java/com/android/server/am/ActivityStarter.java
index 4b07af0..7605a1e 100644
--- a/services/core/java/com/android/server/am/ActivityStarter.java
+++ b/services/core/java/com/android/server/am/ActivityStarter.java
@@ -585,10 +585,8 @@
             // The activity was already running in the pinned stack so it wasn't started, but either
             // brought to the front or the new intent was delivered to it since it was already in
             // front. Notify anyone interested in this piece of information.
-            final ComponentName sourceComponent = sourceRecord == null ? null :
-                    sourceRecord.realActivity;
             mService.mTaskChangeNotificationController.notifyPinnedActivityRestartAttempt(
-                    sourceComponent);
+                    sourceRecord.launchedFromPackage);
             return;
         }
     }
diff --git a/services/core/java/com/android/server/am/TaskChangeNotificationController.java b/services/core/java/com/android/server/am/TaskChangeNotificationController.java
index 2990dff..9dfc7cd 100644
--- a/services/core/java/com/android/server/am/TaskChangeNotificationController.java
+++ b/services/core/java/com/android/server/am/TaskChangeNotificationController.java
@@ -97,7 +97,7 @@
     };
 
     private final TaskStackConsumer mNotifyPinnedActivityRestartAttempt = (l, m) -> {
-        l.onPinnedActivityRestartAttempt((ComponentName) m.obj);
+        l.onPinnedActivityRestartAttempt((String) m.obj);
     };
 
     private final TaskStackConsumer mNotifyPinnedStackAnimationEnded = (l, m) -> {
@@ -267,11 +267,11 @@
      * running in the pinned stack and the activity was not actually started, but the task is
      * either brought to the front or a new Intent is delivered to it.
      */
-    void notifyPinnedActivityRestartAttempt(ComponentName sourceComponent) {
+    void notifyPinnedActivityRestartAttempt(String launchedFromPackage) {
         mHandler.removeMessages(NOTIFY_PINNED_ACTIVITY_RESTART_ATTEMPT_LISTENERS_MSG);
         final Message msg =
                 mHandler.obtainMessage(NOTIFY_PINNED_ACTIVITY_RESTART_ATTEMPT_LISTENERS_MSG,
-                        sourceComponent);
+                        launchedFromPackage);
         forAllLocalListeners(mNotifyPinnedActivityRestartAttempt, msg);
         msg.sendToTarget();
     }
diff --git a/services/core/java/com/android/server/wm/PinnedStackController.java b/services/core/java/com/android/server/wm/PinnedStackController.java
index 6a8417dc..cfeb198 100644
--- a/services/core/java/com/android/server/wm/PinnedStackController.java
+++ b/services/core/java/com/android/server/wm/PinnedStackController.java
@@ -233,7 +233,7 @@
      * @return the movement bounds for the given {@param stackBounds} and the current state of the
      *         controller.
      */
-    Rect getMovementBounds(Rect stackBounds) {
+    private Rect getMovementBounds(Rect stackBounds) {
         return getMovementBounds(stackBounds, true /* adjustForIme */);
     }
 
@@ -241,7 +241,7 @@
      * @return the movement bounds for the given {@param stackBounds} and the current state of the
      *         controller.
      */
-    Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) {
+    private Rect getMovementBounds(Rect stackBounds, boolean adjustForIme) {
         final Rect movementBounds = new Rect();
         getInsetBounds(movementBounds);