Search code examples
androidandroid-intentandroid-sourceandroid-broadcast

Adding an implicit broadcast exception?


I've been digging through AOSP code, and I'm trying to locate where implicit broadcast exceptions are defined. Ultimately, I'd like to both understand how certain broadcasts are allowed and add an additional one.

I've tried grep-ing some of the exempted broadcasts hoping to find a list of them to understand how they are exempted, but I have not had any luck. Can anyone point me in the right direction? Thanks.


Solution

  • As my experience in AOSP 9.0.0_r35, there is not a defination for list of implicit broadcast exceptions in AOSP source code.

    By default broadcasts do not go to stopped apps. (please refer to file ActivityManagerService.java, function broadcastIntentLocked)

    final int broadcastIntentLocked(ProcessRecord callerApp,
                String callerPackage, Intent intent, String resolvedType,
                IIntentReceiver resultTo, int resultCode, String resultData,
                Bundle resultExtras, String[] requiredPermissions, int appOp, Bundle bOptions,
                boolean ordered, boolean sticky, int callingPid, int callingUid, int userId) {
            intent = new Intent(intent);
            
        ...
        // By default broadcasts do not go to stopped apps.
        intent.addFlags(Intent.FLAG_EXCLUDE_STOPPED_PACKAGES);
        ...
    }
    

    For the implicit broadcast exceptions, the AOSP manually add flag Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND or Intent.FLAG_INCLUDE_STOPPED_PACKAGES everywhere the broadcast intent is sent. These flags are defined in Intent.java as below

    /**
     * If set, this intent will always match any components in packages that
     * are currently stopped.  This is the default behavior when
     * {@link #FLAG_EXCLUDE_STOPPED_PACKAGES} is not set.  If both of these
     * flags are set, this one wins (it allows overriding of exclude for
     * places where the framework may automatically set the exclude flag).
     */
    public static final int FLAG_INCLUDE_STOPPED_PACKAGES = 0x00000020;
    
      /**
     * If set, the broadcast will always go to manifest receivers in background (cached
     * or not running) apps, regardless of whether that would be done by default.  By
     * default they will only receive broadcasts if the broadcast has specified an
     * explicit component or package name.
     *
     * NOTE: dumpstate uses this flag numerically, so when its value is changed
     * the broadcast code there must also be changed to match.
     *
     * @hide
     */
    public static final int FLAG_RECEIVER_INCLUDE_BACKGROUND = 0x01000000;
    

    For example of ACTION_TIMEZONE_CHANGED, it is sent in AlarmManagerService.java

    Intent intent = new Intent(Intent.ACTION_TIMEZONE_CHANGED);
    intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING
            | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND
            | Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS);
    intent.putExtra("time-zone", zone.getID());
    getContext().sendBroadcastAsUser(intent, UserHandle.ALL);