Merge "Propagate calling UID to AM from CrossProfileApps" into pi-dev
diff --git a/core/java/android/content/pm/CrossProfileApps.java b/core/java/android/content/pm/CrossProfileApps.java
index 7d5d609..87f4dab 100644
--- a/core/java/android/content/pm/CrossProfileApps.java
+++ b/core/java/android/content/pm/CrossProfileApps.java
@@ -64,7 +64,11 @@
     public void startMainActivity(@NonNull ComponentName component,
             @NonNull UserHandle targetUser) {
         try {
-            mService.startActivityAsUser(mContext.getPackageName(), component, targetUser);
+            mService.startActivityAsUser(
+                    mContext.getIApplicationThread(),
+                    mContext.getPackageName(),
+                    component,
+                    targetUser);
         } catch (RemoteException ex) {
             throw ex.rethrowFromSystemServer();
         }
diff --git a/core/java/android/content/pm/ICrossProfileApps.aidl b/core/java/android/content/pm/ICrossProfileApps.aidl
index e79deb9..bc2f92a 100644
--- a/core/java/android/content/pm/ICrossProfileApps.aidl
+++ b/core/java/android/content/pm/ICrossProfileApps.aidl
@@ -16,6 +16,7 @@
 
 package android.content.pm;
 
+import android.app.IApplicationThread;
 import android.content.ComponentName;
 import android.content.Intent;
 import android.graphics.Rect;
@@ -26,7 +27,7 @@
  * @hide
  */
 interface ICrossProfileApps {
-    void startActivityAsUser(in String callingPackage, in ComponentName component,
-        in UserHandle user);
+    void startActivityAsUser(in IApplicationThread caller, in String callingPackage,
+            in ComponentName component, in UserHandle user);
     List<UserHandle> getTargetUserProfiles(in String callingPackage);
 }
\ No newline at end of file
diff --git a/services/core/java/com/android/server/pm/CrossProfileAppsServiceImpl.java b/services/core/java/com/android/server/pm/CrossProfileAppsServiceImpl.java
index 2007a0e..97f6aa2 100644
--- a/services/core/java/com/android/server/pm/CrossProfileAppsServiceImpl.java
+++ b/services/core/java/com/android/server/pm/CrossProfileAppsServiceImpl.java
@@ -19,8 +19,10 @@
 import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
 
 import android.annotation.UserIdInt;
+import android.app.ActivityManagerInternal;
 import android.app.ActivityOptions;
 import android.app.AppOpsManager;
+import android.app.IApplicationThread;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -71,6 +73,7 @@
 
     @Override
     public void startActivityAsUser(
+            IApplicationThread caller,
             String callingPackage,
             ComponentName component,
             UserHandle user) throws RemoteException {
@@ -107,15 +110,12 @@
         launchIntent.setPackage(component.getPackageName());
         verifyActivityCanHandleIntentAndExported(launchIntent, component, callingUid, user);
 
-        final long ident = mInjector.clearCallingIdentity();
-        try {
-            launchIntent.setPackage(null);
-            launchIntent.setComponent(component);
-            mContext.startActivityAsUser(launchIntent,
-                    ActivityOptions.makeOpenCrossProfileAppsAnimation().toBundle(), user);
-        } finally {
-            mInjector.restoreCallingIdentity(ident);
-        }
+        launchIntent.setPackage(null);
+        launchIntent.setComponent(component);
+        mInjector.getActivityManagerInternal().startActivityAsUser(
+                caller, callingPackage, launchIntent,
+                ActivityOptions.makeOpenCrossProfileAppsAnimation().toBundle(),
+                user.getIdentifier());
     }
 
     private List<UserHandle> getTargetUserProfilesUnchecked(
@@ -236,6 +236,11 @@
         public AppOpsManager getAppOpsManager() {
             return mContext.getSystemService(AppOpsManager.class);
         }
+
+        @Override
+        public ActivityManagerInternal getActivityManagerInternal() {
+            return LocalServices.getService(ActivityManagerInternal.class);
+        }
     }
 
     @VisibleForTesting
@@ -258,5 +263,6 @@
 
         AppOpsManager getAppOpsManager();
 
+        ActivityManagerInternal getActivityManagerInternal();
     }
 }
diff --git a/services/tests/servicestests/src/com/android/server/pm/CrossProfileAppsServiceImplTest.java b/services/tests/servicestests/src/com/android/server/pm/CrossProfileAppsServiceImplTest.java
index c69437dc..33acc44 100644
--- a/services/tests/servicestests/src/com/android/server/pm/CrossProfileAppsServiceImplTest.java
+++ b/services/tests/servicestests/src/com/android/server/pm/CrossProfileAppsServiceImplTest.java
@@ -13,7 +13,9 @@
 import static org.mockito.Mockito.when;
 import static org.testng.Assert.assertThrows;
 
+import android.app.ActivityManagerInternal;
 import android.app.AppOpsManager;
+import android.app.IApplicationThread;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -68,10 +70,13 @@
     private PackageManagerInternal mPackageManagerInternal;
     @Mock
     private AppOpsManager mAppOpsManager;
+    @Mock
+    private ActivityManagerInternal mActivityManagerInternal;
 
     private TestInjector mTestInjector;
     private ActivityInfo mActivityInfo;
     private CrossProfileAppsServiceImpl mCrossProfileAppsServiceImpl;
+    private IApplicationThread mIApplicationThread;
 
     private SparseArray<Boolean> mUserEnabled = new SparseArray<>();
 
@@ -200,15 +205,18 @@
                 SecurityException.class,
                 () ->
                         mCrossProfileAppsServiceImpl.startActivityAsUser(
+                                mIApplicationThread,
                                 PACKAGE_ONE,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(PRIMARY_USER)));
 
-        verify(mContext, never())
+        verify(mActivityManagerInternal, never())
                 .startActivityAsUser(
+                        nullable(IApplicationThread.class),
+                        anyString(),
                         any(Intent.class),
                         nullable(Bundle.class),
-                        any(UserHandle.class));
+                        anyInt());
     }
 
     @Test
@@ -219,15 +227,18 @@
                 SecurityException.class,
                 () ->
                         mCrossProfileAppsServiceImpl.startActivityAsUser(
+                                mIApplicationThread,
                                 PACKAGE_ONE,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(PROFILE_OF_PRIMARY_USER)));
 
-        verify(mContext, never())
+        verify(mActivityManagerInternal, never())
                 .startActivityAsUser(
+                        nullable(IApplicationThread.class),
+                        anyString(),
                         any(Intent.class),
                         nullable(Bundle.class),
-                        any(UserHandle.class));
+                        anyInt());
     }
 
     @Test
@@ -236,15 +247,18 @@
                 SecurityException.class,
                 () ->
                         mCrossProfileAppsServiceImpl.startActivityAsUser(
+                                mIApplicationThread,
                                 PACKAGE_TWO,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(PROFILE_OF_PRIMARY_USER)));
 
-        verify(mContext, never())
+        verify(mActivityManagerInternal, never())
                 .startActivityAsUser(
+                        nullable(IApplicationThread.class),
+                        anyString(),
                         any(Intent.class),
                         nullable(Bundle.class),
-                        any(UserHandle.class));
+                        anyInt());
     }
 
     @Test
@@ -255,15 +269,18 @@
                 SecurityException.class,
                 () ->
                         mCrossProfileAppsServiceImpl.startActivityAsUser(
+                                mIApplicationThread,
                                 PACKAGE_ONE,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(PROFILE_OF_PRIMARY_USER)));
 
-        verify(mContext, never())
+        verify(mActivityManagerInternal, never())
                 .startActivityAsUser(
+                        nullable(IApplicationThread.class),
+                        anyString(),
                         any(Intent.class),
                         nullable(Bundle.class),
-                        any(UserHandle.class));
+                        anyInt());
     }
 
     @Test
@@ -272,15 +289,18 @@
                 SecurityException.class,
                 () ->
                         mCrossProfileAppsServiceImpl.startActivityAsUser(
+                                mIApplicationThread,
                                 PACKAGE_ONE,
                                 new ComponentName(PACKAGE_TWO, "test"),
                                 UserHandle.of(PROFILE_OF_PRIMARY_USER)));
 
-        verify(mContext, never())
+        verify(mActivityManagerInternal, never())
                 .startActivityAsUser(
+                        nullable(IApplicationThread.class),
+                        anyString(),
                         any(Intent.class),
                         nullable(Bundle.class),
-                        any(UserHandle.class));
+                        anyInt());
     }
 
     @Test
@@ -289,15 +309,18 @@
                 SecurityException.class,
                 () ->
                         mCrossProfileAppsServiceImpl.startActivityAsUser(
+                                mIApplicationThread,
                                 PACKAGE_ONE,
                                 ACTIVITY_COMPONENT,
                                 UserHandle.of(SECONDARY_USER)));
 
-        verify(mContext, never())
+        verify(mActivityManagerInternal, never())
                 .startActivityAsUser(
+                        nullable(IApplicationThread.class),
+                        anyString(),
                         any(Intent.class),
                         nullable(Bundle.class),
-                        any(UserHandle.class));
+                        anyInt());
     }
 
     @Test
@@ -305,15 +328,18 @@
         mTestInjector.setCallingUserId(PROFILE_OF_PRIMARY_USER);
 
         mCrossProfileAppsServiceImpl.startActivityAsUser(
+                mIApplicationThread,
                 PACKAGE_ONE,
                 ACTIVITY_COMPONENT,
                 UserHandle.of(PRIMARY_USER));
 
-        verify(mContext)
+        verify(mActivityManagerInternal)
                 .startActivityAsUser(
+                        nullable(IApplicationThread.class),
+                        eq(PACKAGE_ONE),
                         any(Intent.class),
                         nullable(Bundle.class),
-                        eq(UserHandle.of(PRIMARY_USER)));
+                        eq(PRIMARY_USER));
     }
 
     private void mockAppsInstalled(String packageName, int user, boolean installed) {
@@ -401,5 +427,10 @@
         public AppOpsManager getAppOpsManager() {
             return mAppOpsManager;
         }
+
+        @Override
+        public ActivityManagerInternal getActivityManagerInternal() {
+            return mActivityManagerInternal;
+        }
     }
 }