From b884d7ee9b7d0913d350b64e7a71f754bf5a8d9e Mon Sep 17 00:00:00 2001 From: bachinger Date: Mon, 23 Sep 2024 12:50:25 -0700 Subject: [PATCH] Handle IllegalArgumentException when setting broadcast receiver Some devices seem to throw an `IllegalArgumentException` when attempting to set a valid media button broadcast receiver for playback resumption. This change handles this exception as a no-op to avoid crashing the app. As a result, playback resumption with media keys isn't going to work on these devices. This change needs to be reverted once the root cause on these devices has been fixed (see internal bug ref in source). Issue: androidx/media#1730 PiperOrigin-RevId: 677904243 --- RELEASENOTES.md | 3 +++ .../session/MediaSessionLegacyStub.java | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index cec580ab4b..427de24b98 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -79,6 +79,9 @@ legacy service. This prevented the `MediaBrowser` to receive the actual return value sent back by the legacy service ([#1474](https://github.com/androidx/media/issues/1474)). + * Handle `IllegalArgumentException` thrown by devices of certain + manufacturers when setting the broadcast receiver for media button + intents ([#1730](https://github.com/androidx/media/issues/1730)). * UI: * Make the stretched/cropped video in `PlayerView`-in-Compose-`AndroidView` workaround opt-in, due to issues diff --git a/libraries/session/src/main/java/androidx/media3/session/MediaSessionLegacyStub.java b/libraries/session/src/main/java/androidx/media3/session/MediaSessionLegacyStub.java index 271a4ba752..f9e506ea7a 100644 --- a/libraries/session/src/main/java/androidx/media3/session/MediaSessionLegacyStub.java +++ b/libraries/session/src/main/java/androidx/media3/session/MediaSessionLegacyStub.java @@ -50,6 +50,7 @@ import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.Bitmap; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Looper; @@ -1483,8 +1484,22 @@ import org.checkerframework.checker.initialization.qual.Initialized; private static final class Api31 { public static void setMediaButtonBroadcastReceiver( MediaSessionCompat mediaSessionCompat, ComponentName broadcastReceiver) { - ((android.media.session.MediaSession) checkNotNull(mediaSessionCompat.getMediaSession())) - .setMediaButtonBroadcastReceiver(broadcastReceiver); + try { + ((android.media.session.MediaSession) checkNotNull(mediaSessionCompat.getMediaSession())) + .setMediaButtonBroadcastReceiver(broadcastReceiver); + } catch (IllegalArgumentException e) { + if (Build.MANUFACTURER.equals("motorola")) { + // Internal bug ref: b/367415658 + Log.e( + TAG, + "caught IllegalArgumentException on a motorola device when attempting to set the" + + " media button broadcast receiver. See" + + " https://github.com/androidx/media/issues/1730 for details.", + e); + } else { + throw e; + } + } } } }