Allow background activity starts for callers that have
the same uid as the recents component

Bug: 110956953
Test: atest WmTests:ActivityStarterTests
Test: manual - both with nexuslauncher and 3P launcher
Change-Id: I4f7a9db2ad22205d37c7b9a52325011a3906c247
diff --git a/services/core/java/com/android/server/wm/ActivityStarter.java b/services/core/java/com/android/server/wm/ActivityStarter.java
index 1735ef8..cce3340 100644
--- a/services/core/java/com/android/server/wm/ActivityStarter.java
+++ b/services/core/java/com/android/server/wm/ActivityStarter.java
@@ -783,7 +783,7 @@
             // they will just get a cancel result.
             ActivityOptions.abort(checkedOptions);
             maybeLogActivityStart(callingUid, callingPackage, realCallingUid, intent, callerApp,
-                    /* r= */ null, originatingPendingIntent, /* abortedStart= */ true);
+                    null /*r*/, originatingPendingIntent, true /*abortedStart*/);
             return START_ABORTED;
         }
 
@@ -876,7 +876,7 @@
         mController.doPendingActivityLaunches(false);
 
         maybeLogActivityStart(callingUid, callingPackage, realCallingUid, intent, callerApp, r,
-                originatingPendingIntent, /* abortedStart= */ false);
+                originatingPendingIntent, false /*abortedStart*/);
 
         return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags,
                 true /* doResume */, checkedOptions, inTask, outActivity);
@@ -903,6 +903,10 @@
         if (mService.mWindowManager.isAnyWindowVisibleForUid(callingUid)) {
             return false;
         }
+        // don't abort if the caller has the same uid as the recents component
+        if (mSupervisor.mRecentTasks.isCallerRecents(callingUid)) {
+            return false;
+        }
         // anything that has fallen through will currently be aborted
         // TODO: remove this toast after feature development is done
         mService.mUiHandler.post(() -> {
diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java
index f6ff05b..5fcc950 100644
--- a/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java
+++ b/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java
@@ -562,7 +562,7 @@
         doReturn(true).when(mService).isBackgroundActivityStartsEnabled();
 
         runAndVerifyBackgroundActivityStartsSubtest("allowed_noStartsAborted",
-                false, UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, false);
+                false, UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, false, false);
     }
 
     /**
@@ -574,7 +574,7 @@
         doReturn(false).when(mService).isBackgroundActivityStartsEnabled();
 
         runAndVerifyBackgroundActivityStartsSubtest("disallowed_unsupportedUsecase_aborted",
-                true, UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, false);
+                true, UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, false, false);
     }
 
     /**
@@ -587,20 +587,22 @@
         doReturn(false).when(mService).isBackgroundActivityStartsEnabled();
 
         runAndVerifyBackgroundActivityStartsSubtest("disallowed_rootUid_notAborted",
-                false, Process.ROOT_UID, false, PROCESS_STATE_TOP + 1, false);
+                false, Process.ROOT_UID, false, PROCESS_STATE_TOP + 1, false, false);
         runAndVerifyBackgroundActivityStartsSubtest("disallowed_systemUid_notAborted",
-                false, Process.SYSTEM_UID, false, PROCESS_STATE_TOP + 1, false);
+                false, Process.SYSTEM_UID, false, PROCESS_STATE_TOP + 1, false, false);
         runAndVerifyBackgroundActivityStartsSubtest("disallowed_hasVisibleWindow_notAborted",
-                false, UNIMPORTANT_UID, true, PROCESS_STATE_TOP + 1, false);
+                false, UNIMPORTANT_UID, true, PROCESS_STATE_TOP + 1, false, false);
         runAndVerifyBackgroundActivityStartsSubtest("disallowed_processStateTop_notAborted",
-                false, UNIMPORTANT_UID, false, PROCESS_STATE_TOP, false);
+                false, UNIMPORTANT_UID, false, PROCESS_STATE_TOP, false, false);
         runAndVerifyBackgroundActivityStartsSubtest("disallowed_hasForegroundActivities_notAborted",
-                false, UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, true);
+                false, UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, true, false);
+        runAndVerifyBackgroundActivityStartsSubtest("disallowed_callerIsRecents_notAborted",
+                false, UNIMPORTANT_UID, false, PROCESS_STATE_TOP + 1, false, true);
     }
 
     private void runAndVerifyBackgroundActivityStartsSubtest(String name, boolean shouldHaveAborted,
             int testCallingUid, boolean hasVisibleWindow, int procState,
-            boolean hasForegroundActivities) {
+            boolean hasForegroundActivities, boolean callerIsRecents) {
         // window visibility
         doReturn(hasVisibleWindow).when(mService.mWindowManager).isAnyWindowVisibleForUid(
                 testCallingUid);
@@ -614,6 +616,10 @@
                 new WindowProcessController(mService, ai, null, testCallingUid, -1, null, null);
         callerApp.setHasForegroundActivities(hasForegroundActivities);
         doReturn(callerApp).when(mService).getProcessController(caller);
+        // caller is recents
+        RecentTasks recentTasks = mock(RecentTasks.class);
+        mService.mStackSupervisor.setRecentTasks(recentTasks);
+        doReturn(callerIsRecents).when(recentTasks).isCallerRecents(testCallingUid);
 
         final ActivityOptions options = spy(ActivityOptions.makeBasic());
         ActivityStarter starter = prepareStarter(FLAG_ACTIVITY_NEW_TASK).setCaller(caller)