From 7014bc6bf463595ec2926bd3efaba867a33121f1 Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 10 Nov 2023 12:17:28 -0800 Subject: [PATCH] Add icon URI to bundle serialization PiperOrigin-RevId: 581333583 --- .../media3/session/CommandButton.java | 6 ++++ .../media3/session/CommandButtonTest.java | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/libraries/session/src/main/java/androidx/media3/session/CommandButton.java b/libraries/session/src/main/java/androidx/media3/session/CommandButton.java index 34911f2b376..1ec624c2f4b 100644 --- a/libraries/session/src/main/java/androidx/media3/session/CommandButton.java +++ b/libraries/session/src/main/java/androidx/media3/session/CommandButton.java @@ -306,6 +306,7 @@ public int hashCode() { private static final String FIELD_DISPLAY_NAME = Util.intToStringMaxRadix(3); private static final String FIELD_EXTRAS = Util.intToStringMaxRadix(4); private static final String FIELD_ENABLED = Util.intToStringMaxRadix(5); + private static final String FIELD_ICON_URI = Util.intToStringMaxRadix(6); @UnstableApi @Override @@ -318,6 +319,7 @@ public Bundle toBundle() { bundle.putInt(FIELD_ICON_RES_ID, iconResId); bundle.putCharSequence(FIELD_DISPLAY_NAME, displayName); bundle.putBundle(FIELD_EXTRAS, extras); + bundle.putParcelable(FIELD_ICON_URI, iconUri); bundle.putBoolean(FIELD_ENABLED, isEnabled); return bundle; } @@ -346,6 +348,7 @@ public static CommandButton fromBundle(Bundle bundle) { CharSequence displayName = bundle.getCharSequence(FIELD_DISPLAY_NAME, /* defaultValue= */ ""); @Nullable Bundle extras = bundle.getBundle(FIELD_EXTRAS); boolean enabled = bundle.getBoolean(FIELD_ENABLED, /* defaultValue= */ false); + @Nullable Uri iconUri = bundle.getParcelable(FIELD_ICON_URI); Builder builder = new Builder(); if (sessionCommand != null) { builder.setSessionCommand(sessionCommand); @@ -353,6 +356,9 @@ public static CommandButton fromBundle(Bundle bundle) { if (playerCommand != Player.COMMAND_INVALID) { builder.setPlayerCommand(playerCommand); } + if (iconUri != null) { + builder.setIconUri(iconUri); + } return builder .setIconResId(iconResId) .setDisplayName(displayName) diff --git a/libraries/session/src/test/java/androidx/media3/session/CommandButtonTest.java b/libraries/session/src/test/java/androidx/media3/session/CommandButtonTest.java index 0882f271a0f..fc2632f70ed 100644 --- a/libraries/session/src/test/java/androidx/media3/session/CommandButtonTest.java +++ b/libraries/session/src/test/java/androidx/media3/session/CommandButtonTest.java @@ -140,6 +140,36 @@ public void getIconUri_returnsNullIfUnset() { assertThat(button.iconUri).isNull(); } + @Test + public void getIconUri_returnsUriAfterSerialisation() { + Uri uri = Uri.parse("content://test"); + CommandButton button = + new CommandButton.Builder() + .setDisplayName("button1") + .setIconResId(R.drawable.media3_notification_small_icon) + .setIconUri(uri) + .setPlayerCommand(Player.COMMAND_SEEK_TO_PREVIOUS) + .build(); + + CommandButton serialisedButton = CommandButton.fromBundle(button.toBundle()); + + assertThat(serialisedButton.iconUri).isEqualTo(uri); + } + + @Test + public void getIconUri_returnsNullIfUnsetAfterSerialisation() { + CommandButton button = + new CommandButton.Builder() + .setDisplayName("button1") + .setIconResId(R.drawable.media3_notification_small_icon) + .setPlayerCommand(Player.COMMAND_SEEK_TO_PREVIOUS) + .build(); + + CommandButton serialisedButton = CommandButton.fromBundle(button.toBundle()); + + assertThat(serialisedButton.iconUri).isNull(); + } + @Test public void equals() { assertThat(