Skip to content

Fix StrictMode unsafe intent launch violation #2330

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ public final void onReceive(Context context, @Nullable Intent intent) {
for (String action : ACTIONS) {
ComponentName mediaButtonServiceComponentName = getServiceComponentByAction(context, action);
if (mediaButtonServiceComponentName != null) {
intent.setComponent(mediaButtonServiceComponentName);
if (!shouldStartForegroundService(context, intent)) {
Intent serviceIntent = new Intent();
serviceIntent.setComponent(mediaButtonServiceComponentName);
serviceIntent.fillIn(intent, 0);
if (!shouldStartForegroundService(context, serviceIntent)) {
Log.i(
TAG,
"onReceive(Intent) does not start the media button event target service into the"
Expand All @@ -157,12 +159,12 @@ public final void onReceive(Context context, @Nullable Intent intent) {
return;
}
try {
ContextCompat.startForegroundService(context, intent);
ContextCompat.startForegroundService(context, serviceIntent);
} catch (/* ForegroundServiceStartNotAllowedException */ IllegalStateException e) {
if (Build.VERSION.SDK_INT >= 31
&& Api31.instanceOfForegroundServiceStartNotAllowedException(e)) {
onForegroundServiceStartNotAllowedException(
intent, Api31.castToForegroundServiceStartNotAllowedException(e));
serviceIntent, Api31.castToForegroundServiceStartNotAllowedException(e));
} else {
throw e;
}
Expand All @@ -188,8 +190,8 @@ public final void onReceive(Context context, @Nullable Intent intent) {
*
* @param context The {@link Context} that {@linkplain #onReceive(Context, Intent) was received by
* the media button event receiver}.
* @param intent The intent that {@linkplain #onReceive(Context, Intent) was received by the media
* button event receiver}.
* @param intent The intent that would be used {@linkplain Context#startForegroundService(Intent)
* for starting the foreground service}.
* @return true if the service should be {@linkplain ContextCompat#startForegroundService(Context,
* Intent) started as a foreground service}. If false is returned the service is not started
* and the receiver call is a no-op.
Expand Down