Skip to content

Commit

Permalink
Populate MediaMetadata.extras to MediaMetadataCompat
Browse files Browse the repository at this point in the history
Ensures backward compatibility.

Issue: #802
PiperOrigin-RevId: 583425114
  • Loading branch information
rohitjoins authored and copybara-github committed Nov 17, 2023
1 parent ad96ca3 commit 6df2408
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
6 changes: 4 additions & 2 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@
can't be played.
* Session:
* Put the custom keys and values in `MediaMetadataCompat` to
`MediaMetadata.extras`
([#756](https://github.com/androidx/media/issues/756)).
`MediaMetadata.extras` and `MediaMetadata.extras` to
`MediaMetadataCompat`
([#756](https://github.com/androidx/media/issues/756),
[#802](https://github.com/androidx/media/issues/802)).
* UI:
* Fix issue where forward and rewind buttons are not visible when used
with Material Design in a BottomSheetDialogFragment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,7 @@ public Builder buildUpon() {
return new Builder(/* mediaMetadata= */ this);
}

/** Note: Equality checking does not consider {@link #extras}. */
@SuppressWarnings("deprecation") // Comparing deprecated fields.
@Override
public boolean equals(@Nullable Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,9 @@ private static CharSequence getFirstText(
* duration should be included.
* @return An instance of the legacy {@link MediaMetadataCompat}.
*/
@SuppressWarnings("deprecation") // Converting deprecated fields.
// Converting deprecated fields and suppressing nullness.
// TODO: b/311689564 - Add @Nullable annotations to setters of MediaMetadataCompat.Builder
@SuppressWarnings({"deprecation", "nullness:argument"})
public static MediaMetadataCompat convertToMediaMetadataCompat(
MediaMetadata metadata,
String mediaId,
Expand Down Expand Up @@ -639,6 +641,20 @@ public static MediaMetadataCompat convertToMediaMetadataCompat(
builder.putLong(MediaConstants.EXTRAS_KEY_MEDIA_TYPE_COMPAT, metadata.mediaType);
}

if (metadata.extras != null) {
for (@Nullable String customKey : metadata.extras.keySet()) {
@Nullable Object customValue = metadata.extras.get(customKey);
if (customValue == null || customValue instanceof CharSequence) {
builder.putText(customKey, (CharSequence) customValue);
} else if (customValue instanceof Byte
|| customValue instanceof Short
|| customValue instanceof Integer
|| customValue instanceof Long) {
builder.putLong(customKey, ((Number) customValue).longValue());
}
}
}

return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.text.SpannedString;
import androidx.annotation.Nullable;
import androidx.media.AudioAttributesCompat;
import androidx.media.VolumeProviderCompat;
Expand Down Expand Up @@ -263,6 +264,42 @@ public void convertToMediaMetadataCompat_withMediaType_setsMediaType() {
.isEqualTo(MediaMetadata.MEDIA_TYPE_MUSIC);
}

@Test
public void convertToMediaMetadataCompat_populatesExtrasFromMediaMetadata() {
Bundle extras = new Bundle();
extras.putString("customNullValueKey", null);
extras.putString(null, "customNullKeyValue");
extras.putString("customStringKey", "customStringValue");
extras.putCharSequence("customCharSequenceKey", new SpannedString("customCharSequenceValue"));
extras.putByte("customByteKey", (byte) 1);
extras.putShort("customShortKey", (short) 5);
extras.putInt("customIntegerKey", 10);
extras.putLong("customLongKey", 20L);
MediaItem mediaItem =
new MediaItem.Builder()
.setMediaMetadata(new MediaMetadata.Builder().setExtras(extras).build())
.build();

MediaMetadataCompat mediaMetadataCompat =
LegacyConversions.convertToMediaMetadataCompat(
mediaItem.mediaMetadata,
"mediadId",
Uri.parse("http://www.test.com"),
/* durationMs= */ C.TIME_UNSET,
/* artworkBitmap= */ null);

assertThat(mediaMetadataCompat.getString("customNullValueKey")).isNull();
assertThat(mediaMetadataCompat.getString(null)).isEqualTo("customNullKeyValue");
assertThat(mediaMetadataCompat.getString("customStringKey")).isEqualTo("customStringValue");
CharSequence customCharSequence = mediaMetadataCompat.getText("customCharSequenceKey");
assertThat(customCharSequence).isInstanceOf(SpannedString.class);
assertThat(customCharSequence.toString()).isEqualTo("customCharSequenceValue");
assertThat(mediaMetadataCompat.getLong("customByteKey")).isEqualTo(1);
assertThat(mediaMetadataCompat.getLong("customShortKey")).isEqualTo(5);
assertThat(mediaMetadataCompat.getLong("customIntegerKey")).isEqualTo(10);
assertThat(mediaMetadataCompat.getLong("customLongKey")).isEqualTo(20);
}

@Test
public void convertBetweenRatingAndRatingCompat() {
assertRatingEquals(
Expand Down

0 comments on commit 6df2408

Please sign in to comment.