Skip to content

Commit f14eb49

Browse files
committed
Fix StrictMode unsafe intent launch violation
1 parent 45bcf3f commit f14eb49

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

libraries/session/src/main/java/androidx/media3/session/MediaButtonReceiver.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ public final void onReceive(Context context, @Nullable Intent intent) {
147147
for (String action : ACTIONS) {
148148
ComponentName mediaButtonServiceComponentName = getServiceComponentByAction(context, action);
149149
if (mediaButtonServiceComponentName != null) {
150-
intent.setComponent(mediaButtonServiceComponentName);
151-
if (!shouldStartForegroundService(context, intent)) {
150+
Intent serviceIntent = new Intent();
151+
serviceIntent.setComponent(mediaButtonServiceComponentName);
152+
serviceIntent.fillIn(intent, 0);
153+
if (!shouldStartForegroundService(context, serviceIntent)) {
152154
Log.i(
153155
TAG,
154156
"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) {
157159
return;
158160
}
159161
try {
160-
ContextCompat.startForegroundService(context, intent);
162+
ContextCompat.startForegroundService(context, serviceIntent);
161163
} catch (/* ForegroundServiceStartNotAllowedException */ IllegalStateException e) {
162164
if (Build.VERSION.SDK_INT >= 31
163165
&& Api31.instanceOfForegroundServiceStartNotAllowedException(e)) {
164166
onForegroundServiceStartNotAllowedException(
165-
intent, Api31.castToForegroundServiceStartNotAllowedException(e));
167+
serviceIntent, Api31.castToForegroundServiceStartNotAllowedException(e));
166168
} else {
167169
throw e;
168170
}
@@ -188,8 +190,8 @@ public final void onReceive(Context context, @Nullable Intent intent) {
188190
*
189191
* @param context The {@link Context} that {@linkplain #onReceive(Context, Intent) was received by
190192
* the media button event receiver}.
191-
* @param intent The intent that {@linkplain #onReceive(Context, Intent) was received by the media
192-
* button event receiver}.
193+
* @param intent The intent that would be used {@linkplain Context#startForegroundService(Intent)
194+
* for starting the foreground service}.
193195
* @return true if the service should be {@linkplain ContextCompat#startForegroundService(Context,
194196
* Intent) started as a foreground service}. If false is returned the service is not started
195197
* and the receiver call is a no-op.

0 commit comments

Comments
 (0)