Include nullness of MediaMetadata.extras in equals method

This ensures MediaMetadata with just non-null extras is not
considered equal to MediaMetadata.EMPTY. This makes sure the
contents are bundled when a controller sets the extras in a
new MediaItem

Issue: androidx/media#1176

#minor-release

PiperOrigin-RevId: 618876642
This commit is contained in:
tonihei 2024-03-25 10:02:23 -07:00 committed by Copybara-Service
parent fd268eed46
commit ab0391167c
4 changed files with 27 additions and 6 deletions

View File

@ -90,6 +90,9 @@
available.
* Add icon constants for `CommandButton` that should be used instead of
custom icon resources.
* Fix issue where `MediaMetadata` with just non-null `extras` is not
transmitted between media controllers and sessions
([#1176](https://github.com/androidx/media/issues/1176)).
* UI:
* Fallback to include audio track language name if `Locale` cannot
identify a display name

View File

@ -2267,8 +2267,9 @@ public final class MediaItem implements Bundleable {
/**
* Optional extras {@link Bundle}.
*
* <p>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()}.
* <p>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;

View File

@ -1085,8 +1085,9 @@ public final class MediaMetadata implements Bundleable {
/**
* Optional extras {@link Bundle}.
*
* <p>Given the complexities of checking the equality of two {@link Bundle}s, this is not
* considered in the {@link #equals(Object)} or {@link #hashCode()}.
* <p>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;
@ -1191,7 +1192,8 @@ public final class MediaMetadata implements Bundleable {
&& 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.
@ -1229,7 +1231,8 @@ public final class MediaMetadata implements Bundleable {
genre,
compilation,
station,
mediaType);
mediaType,
extras == null);
}
// Bundleable implementation.

View File

@ -163,6 +163,20 @@ public class MediaMetadataTest {
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() {