From f14eb49b4b66f7ad96019b69ad8fd7b668e6f19b Mon Sep 17 00:00:00 2001 From: nift4 Date: Fri, 11 Apr 2025 14:42:48 +0200 Subject: [PATCH] Fix StrictMode unsafe intent launch violation --- .../media3/session/MediaButtonReceiver.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libraries/session/src/main/java/androidx/media3/session/MediaButtonReceiver.java b/libraries/session/src/main/java/androidx/media3/session/MediaButtonReceiver.java index c71dd52f7bd..da873efa0b3 100644 --- a/libraries/session/src/main/java/androidx/media3/session/MediaButtonReceiver.java +++ b/libraries/session/src/main/java/androidx/media3/session/MediaButtonReceiver.java @@ -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" @@ -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; } @@ -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.