Allow instant apps to send broadcasts to themselves

When resolving the broadcast targets we did not consider instant apps
even if the source is an instant app. This prevents the case of the
instant app sending the broadcast to itself. The broadcast queue already
enforces that if the broadcast is not from a full app that wants to
advertise to instant apps or not from an instant app sending it to itself
then it is not dispatched. Hence, the broadcast queue already determines
the right targeting. This change makes sure that if the source is an
instant app we also resolve instant apps and then the targeting logic
in the boradcast queue would ensure that an instant app can only send
a boradcast to its own UID.

Test: cts-instant-tradefed run cts-instant-dev -m CtsBackgroundRestrictionsTestCases
          -t android.app.cts.backgroundrestrictions.BroadcastsTest
              #testNonSupportedBroadcastsNotDelivered_manifestReceiver

bug:109583877

Change-Id: Ifb7230df87c4721e4c05d77b5957d825f3846ca7
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index e95a932..96733e2 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -21021,9 +21021,15 @@
     }
 
     private List<ResolveInfo> collectReceiverComponents(Intent intent, String resolvedType,
-            int callingUid, int[] users) {
+            int callingUid, boolean callerInstantApp, int[] users) {
         // TODO: come back and remove this assumption to triage all broadcasts
         int pmFlags = STOCK_PM_FLAGS | MATCH_DEBUG_TRIAGED_MISSING;
+        // Instant apps should be able to send broadcasts to themselves, so we would
+        // match instant receivers and later the broadcast queue would enforce that
+        // the broadcast cannot be sent to a receiver outside the instant UID.
+        if (callerInstantApp) {
+            pmFlags |= PackageManager.MATCH_INSTANT;
+        }
 
         List<ResolveInfo> receivers = null;
         try {
@@ -21652,7 +21658,8 @@
         // Need to resolve the intent to interested receivers...
         if ((intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY)
                  == 0) {
-            receivers = collectReceiverComponents(intent, resolvedType, callingUid, users);
+            receivers = collectReceiverComponents(intent, resolvedType, callingUid,
+                    callerInstantApp, users);
         }
         if (intent.getComponent() == null) {
             if (userId == UserHandle.USER_ALL && callingUid == SHELL_UID) {