-
Notifications
You must be signed in to change notification settings - Fork 171
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
Conversation
There was a problem hiding this 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:
- TransportManager line 415 - Exported
- MediaStreamingStatus line 264 - Exported
@@ -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); |
There was a problem hiding this comment.
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.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above.
registerReceiver(lockScreenBroadcastReceiver, lockscreenFilter, RECEIVER_EXPORTED); | |
registerReceiver(lockScreenBroadcastReceiver, lockscreenFilter, RECEIVER_NOT_EXPORTED); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as above.
…and SDLLockScreenActivity
…rService and ServiceFinder
Fixes #1857
This PR is [ready] for review.
Risk
This PR makes [no] API changes.
Testing Plan
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
CLA