diff --git a/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java b/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java index 925fb6540a6..80a19e8bcb1 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java @@ -2218,8 +2218,9 @@ public RequestMetadata build() { /** * Optional extras {@link Bundle}. * - *

Given the complexities of checking the equality of two {@link Bundle}s, the contents of - * these extras are not considered in the {@link #equals(Object)} or {@link #hashCode()}. + *

Given the complexities of checking the equality of two {@link Bundle} instances, the + * contents of these extras are not considered in the {@link #equals(Object)} or {@link + * #hashCode()} implementation. */ @Nullable public final Bundle extras; diff --git a/library/common/src/main/java/com/google/android/exoplayer2/MediaMetadata.java b/library/common/src/main/java/com/google/android/exoplayer2/MediaMetadata.java index 14fa0078f32..2f22a7fcbee 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/MediaMetadata.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/MediaMetadata.java @@ -1086,8 +1086,9 @@ public MediaMetadata build() { /** * Optional extras {@link Bundle}. * - *

Given the complexities of checking the equality of two {@link Bundle}s, this is not - * considered in the {@link #equals(Object)} or {@link #hashCode()}. + *

Given the complexities of checking the equality of two {@link Bundle} instances, the + * contents of these extras are not considered in the {@link #equals(Object)} and {@link + * #hashCode()} implementation. */ @Nullable public final Bundle extras; @@ -1192,7 +1193,8 @@ public boolean equals(@Nullable Object obj) { && Util.areEqual(genre, that.genre) && Util.areEqual(compilation, that.compilation) && Util.areEqual(station, that.station) - && Util.areEqual(mediaType, that.mediaType); + && Util.areEqual(mediaType, that.mediaType) + && ((extras == null) == (that.extras == null)); } @SuppressWarnings("deprecation") // Hashing deprecated fields. @@ -1230,7 +1232,8 @@ public int hashCode() { genre, compilation, station, - mediaType); + mediaType, + extras == null); } // Bundleable implementation. diff --git a/library/common/src/test/java/com/google/android/exoplayer2/MediaMetadataTest.java b/library/common/src/test/java/com/google/android/exoplayer2/MediaMetadataTest.java index 2e649fed128..1340f276983 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/MediaMetadataTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/MediaMetadataTest.java @@ -163,6 +163,20 @@ public void createFullyPopulatedMediaMetadata_roundTripViaBundle_yieldsEqualInst assertThat(mediaMetadataFromBundle.extras.getString(EXTRAS_KEY)).isEqualTo(EXTRAS_VALUE); } + /** Regression test for https://github.com/androidx/media/issues/1176. */ + @Test + public void roundTripViaBundle_withJustNonNullExtras_restoresAllData() { + Bundle extras = new Bundle(); + extras.putString("key", "value"); + MediaMetadata mediaMetadata = new MediaMetadata.Builder().setExtras(extras).build(); + + MediaMetadata restoredMetadata = MediaMetadata.fromBundle(mediaMetadata.toBundle()); + + assertThat(restoredMetadata).isEqualTo(mediaMetadata); + assertThat(restoredMetadata.extras).isNotNull(); + assertThat(restoredMetadata.extras.get("key")).isEqualTo("value"); + } + @SuppressWarnings("deprecation") // Testing deprecated setter. @Test public void builderSetFolderType_toNone_setsIsBrowsableToFalse() {