Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android 14 Runtime registered broadcast receivers export behavior #1858

Merged
merged 13 commits into from
Nov 17, 2023

Conversation

JulianKast
Copy link
Contributor

@JulianKast JulianKast commented Jul 26, 2023

Fixes #1857

This PR is [ready] for review.

Risk

This PR makes [no] API changes.

Testing Plan

  • I have verified that I have not introduced new warnings in this PR (or explain why below)
  • I have run the unit tests with this PR
  • I have tested this PR against Core and verified behavior (if applicable, if not applicable, explain why below).
  • I have tested Android

Unit Tests

No additional unit test were added

Core Tests

Note used an integration branch with All Android 14 PRs to test.
Build variants on Android 14 test plan card on trello.

All Test are designed to trigger BroadcastReceivers that I added the export flag behavior.

Test 1:
Install app A
Connect to TDK via Bluetooth
Enable moving on TDK
Observe: LockScreen in enabled.

Test 2
Add LockScreenConfig to SdlManager builder with enableDismissGesture to true.
Install Hello_Sdl with TCP build variant.
Connect to manticore
Enable DD
Dismiss LockScreen.
Observe: that LockScreen dismisses.

Test 3.
Install App A
Connect to tdk via Bluetooth
Observe App A connect to tdk
Install App B (Note, Before you install App B, unplug the phone from the computer and switch the build variant before deploying to the phone otherwise Android Studio will kill App A as it deploys App B)
Observe: App B connects to tdk.

Core version / branch / commit hash / module tested against: Sync 3 and manticore
HMI name / version / branch / commit hash / module tested against: Sync 3 and manticore

Summary

This PR updates the gradle files for SDL and Hello SDL to target API level 34, as well as specifies export behavior for runtime-registered broadcasts receivers.

If a BroadcastReceiver only receives System broadcast, it is not necessary to add the export flag to when we register.

Changelog

Bug Fixes
  • Tragets Android 14 and updates context registered broadcast receiver behavior for API 34

CLA

Base automatically changed from bugfix/issue_1852_update_gradle to develop October 19, 2023 15:00
Copy link
Member

@joeygrover joeygrover left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of places I don't believe the receivers need to be exported. I think it might be helpful to make a method in AndroidTools to handle the version check to make the code more concise (Note: we use the Oreo code because that's when Android added the method signature that includes flags):

    /**
     * A helper method to handle adding flags to registering a run time broadcast receiver.
     *
     * @param context  a context that will be used to register the receiver with
     * @param receiver the receiver that will be registered
     * @param filter   the filter that will be use to filter intents sent to the broadcast receiver
     * @param flags    any flags that should be used to register the receiver. In most cases this
     *                 will be {@link  Context#RECEIVER_NOT_EXPORTED} or
     *                 {@link  Context#RECEIVER_EXPORTED}
     * @see Context#registerReceiver(BroadcastReceiver, IntentFilter)
     * @see Context#registerReceiver(BroadcastReceiver, IntentFilter, int)
     */
    @SuppressLint("UnspecifiedRegisterReceiverFlag")
    public static void registerReceiver(Context context, BroadcastReceiver receiver, IntentFilter filter, int flags) {
        if (context != null && receiver != null && filter != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.registerReceiver(receiver, filter, flags);
            } else {
                context.registerReceiver(receiver, filter);
            }
        }
    }

I also found two more instances of where we register a receiver that could use the new flag for correctness:

@@ -332,9 +333,12 @@ private void launchLockScreenActivity() {
// pass in icon, background color, and custom view
if (lockScreenEnabled && isApplicationForegrounded && context.get() != null) {
if (isLockscreenDismissible && !lockscreenDismissReceiverRegistered) {
context.get().registerReceiver(mLockscreenDismissedReceiver, new IntentFilter(SDLLockScreenActivity.KEY_LOCKSCREEN_DISMISSED));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
context.get().registerReceiver(mLockscreenDismissedReceiver, new IntentFilter(SDLLockScreenActivity.KEY_LOCKSCREEN_DISMISSED), Context.RECEIVER_EXPORTED);
Copy link
Member

@joeygrover joeygrover Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this receiver needs to be exported as the only intent it should receive is from the app that this class is part of.

Suggested change
context.get().registerReceiver(mLockscreenDismissedReceiver, new IntentFilter(SDLLockScreenActivity.KEY_LOCKSCREEN_DISMISSED), Context.RECEIVER_EXPORTED);
context.get().registerReceiver(mLockscreenDismissedReceiver, new IntentFilter(SDLLockScreenActivity.KEY_LOCKSCREEN_DISMISSED), Context.RECEIVER_NOT_EXPORTED);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This receiver does not work unless it is exported.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that the original intent does not have a package attached to it. So do a find usage on the intents received by this receiver, and where they are created the following line needs to get added before they are sent via broadcast and then setting these to not exported works correctly:

intent.setPackage(context.get().getPackageName())

// register broadcast receivers
registerReceiver(lockScreenBroadcastReceiver, lockscreenFilter);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
registerReceiver(lockScreenBroadcastReceiver, lockscreenFilter, RECEIVER_EXPORTED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

Suggested change
registerReceiver(lockScreenBroadcastReceiver, lockscreenFilter, RECEIVER_EXPORTED);
registerReceiver(lockScreenBroadcastReceiver, lockscreenFilter, RECEIVER_NOT_EXPORTED);

Copy link
Contributor Author

@JulianKast JulianKast Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This receiver also does not work unless it is exported.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.

@joeygrover joeygrover merged commit d0845a9 into develop Nov 17, 2023
4 checks passed
@joeygrover joeygrover deleted the bugfix/issue_1857_android_14_broadcast branch November 17, 2023 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants