Skip to content

Commit

Permalink
Add icon URI to bundle serialization
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 581333583
  • Loading branch information
Googler authored and copybara-github committed Nov 10, 2023
1 parent e798096 commit 7014bc6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -346,13 +348,17 @@ 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);
}
if (playerCommand != Player.COMMAND_INVALID) {
builder.setPlayerCommand(playerCommand);
}
if (iconUri != null) {
builder.setIconUri(iconUri);
}
return builder
.setIconResId(iconResId)
.setDisplayName(displayName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 7014bc6

Please sign in to comment.