Add fields to MediaMetadata requested by MediaAPIs

#minor-release

PiperOrigin-RevId: 372448985
This commit is contained in:
samrobinson 2021-05-07 00:34:17 +01:00 committed by bachinger
parent 15ce148877
commit 09a8993b56

View File

@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer2; package com.google.android.exoplayer2;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -39,6 +40,12 @@ public final class MediaMetadata implements Bundleable {
@Nullable private CharSequence trackArtist; @Nullable private CharSequence trackArtist;
@Nullable private CharSequence albumTitle; @Nullable private CharSequence albumTitle;
@Nullable private CharSequence albumArtist; @Nullable private CharSequence albumArtist;
@Nullable private CharSequence displayTitle;
@Nullable private CharSequence subtitle;
@Nullable private CharSequence description;
@Nullable private Uri mediaUri;
@Nullable private Rating userRating;
@Nullable private Rating overallRating;
public Builder() {} public Builder() {}
@ -47,6 +54,11 @@ public final class MediaMetadata implements Bundleable {
this.trackArtist = mediaMetadata.trackArtist; this.trackArtist = mediaMetadata.trackArtist;
this.albumTitle = mediaMetadata.albumTitle; this.albumTitle = mediaMetadata.albumTitle;
this.albumArtist = mediaMetadata.albumArtist; this.albumArtist = mediaMetadata.albumArtist;
this.displayTitle = mediaMetadata.displayTitle;
this.subtitle = mediaMetadata.subtitle;
this.mediaUri = mediaMetadata.mediaUri;
this.userRating = mediaMetadata.userRating;
this.overallRating = mediaMetadata.overallRating;
} }
/** @deprecated Use {@link #setTrackTitle(CharSequence)} instead. */ /** @deprecated Use {@link #setTrackTitle(CharSequence)} instead. */
@ -77,6 +89,36 @@ public final class MediaMetadata implements Bundleable {
return this; return this;
} }
public Builder setDisplayTitle(@Nullable CharSequence displayTitle) {
this.displayTitle = displayTitle;
return this;
}
public Builder setSubtitle(@Nullable CharSequence subtitle) {
this.subtitle = subtitle;
return this;
}
public Builder setDescription(@Nullable CharSequence description) {
this.description = description;
return this;
}
public Builder setMediaUri(@Nullable Uri mediaUri) {
this.mediaUri = mediaUri;
return this;
}
public Builder setUserRating(@Nullable Rating userRating) {
this.userRating = userRating;
return this;
}
public Builder setOverallRating(@Nullable Rating overallRating) {
this.overallRating = overallRating;
return this;
}
/** /**
* Sets all fields supported by the {@link Metadata.Entry entries} within the {@link Metadata}. * Sets all fields supported by the {@link Metadata.Entry entries} within the {@link Metadata}.
* *
@ -131,6 +173,12 @@ public final class MediaMetadata implements Bundleable {
@Nullable public final CharSequence trackArtist; @Nullable public final CharSequence trackArtist;
@Nullable public final CharSequence albumTitle; @Nullable public final CharSequence albumTitle;
@Nullable public final CharSequence albumArtist; @Nullable public final CharSequence albumArtist;
@Nullable public final CharSequence displayTitle;
@Nullable public final CharSequence subtitle;
@Nullable public final CharSequence description;
@Nullable public final Uri mediaUri;
@Nullable public final Rating userRating;
@Nullable public final Rating overallRating;
private MediaMetadata(Builder builder) { private MediaMetadata(Builder builder) {
this.title = builder.trackTitle != null ? builder.trackTitle.toString() : null; this.title = builder.trackTitle != null ? builder.trackTitle.toString() : null;
@ -138,6 +186,12 @@ public final class MediaMetadata implements Bundleable {
this.trackArtist = builder.trackArtist; this.trackArtist = builder.trackArtist;
this.albumTitle = builder.albumTitle; this.albumTitle = builder.albumTitle;
this.albumArtist = builder.albumArtist; this.albumArtist = builder.albumArtist;
this.displayTitle = builder.displayTitle;
this.subtitle = builder.subtitle;
this.description = builder.description;
this.mediaUri = builder.mediaUri;
this.userRating = builder.userRating;
this.overallRating = builder.overallRating;
} }
/** Returns a new {@link Builder} instance with the current {@link MediaMetadata} fields. */ /** Returns a new {@link Builder} instance with the current {@link MediaMetadata} fields. */
@ -157,25 +211,58 @@ public final class MediaMetadata implements Bundleable {
return Util.areEqual(trackTitle, that.trackTitle) return Util.areEqual(trackTitle, that.trackTitle)
&& Util.areEqual(trackArtist, that.trackArtist) && Util.areEqual(trackArtist, that.trackArtist)
&& Util.areEqual(albumTitle, that.albumTitle) && Util.areEqual(albumTitle, that.albumTitle)
&& Util.areEqual(albumArtist, that.albumArtist); && Util.areEqual(albumArtist, that.albumArtist)
&& Util.areEqual(displayTitle, that.displayTitle)
&& Util.areEqual(subtitle, that.subtitle)
&& Util.areEqual(description, that.description)
&& Util.areEqual(mediaUri, that.mediaUri)
&& Util.areEqual(userRating, that.userRating)
&& Util.areEqual(overallRating, that.overallRating);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hashCode(trackTitle, trackArtist, albumTitle, albumArtist); return Objects.hashCode(
trackTitle,
trackArtist,
albumTitle,
albumArtist,
displayTitle,
subtitle,
description,
mediaUri,
userRating,
overallRating);
} }
// Bundleable implementation. // Bundleable implementation.
@Documented @Documented
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
@IntDef({FIELD_TRACK_TITLE, FIELD_TRACK_ARTIST, FIELD_ALBUM_TITLE, FIELD_ALBUM_ARTIST}) @IntDef({
FIELD_TRACK_TITLE,
FIELD_TRACK_ARTIST,
FIELD_ALBUM_TITLE,
FIELD_ALBUM_ARTIST,
FIELD_DISPLAY_TITLE,
FIELD_SUBTITLE,
FIELD_DESCRIPTION,
FIELD_MEDIA_URI,
FIELD_USER_RATING,
FIELD_OVERALL_RATING,
})
private @interface FieldNumber {} private @interface FieldNumber {}
private static final int FIELD_TRACK_TITLE = 0; private static final int FIELD_TRACK_TITLE = 0;
private static final int FIELD_TRACK_ARTIST = 1; private static final int FIELD_TRACK_ARTIST = 1;
private static final int FIELD_ALBUM_TITLE = 2; private static final int FIELD_ALBUM_TITLE = 2;
private static final int FIELD_ALBUM_ARTIST = 3; private static final int FIELD_ALBUM_ARTIST = 3;
private static final int FIELD_DISPLAY_TITLE = 4;
private static final int FIELD_SUBTITLE = 5;
private static final int FIELD_DESCRIPTION = 6;
private static final int FIELD_MEDIA_URI = 7;
private static final int FIELD_USER_RATING = 8;
private static final int FIELD_OVERALL_RATING = 9;
@Override @Override
public Bundle toBundle() { public Bundle toBundle() {
@ -184,18 +271,51 @@ public final class MediaMetadata implements Bundleable {
bundle.putCharSequence(keyForField(FIELD_TRACK_ARTIST), trackArtist); bundle.putCharSequence(keyForField(FIELD_TRACK_ARTIST), trackArtist);
bundle.putCharSequence(keyForField(FIELD_ALBUM_TITLE), albumTitle); bundle.putCharSequence(keyForField(FIELD_ALBUM_TITLE), albumTitle);
bundle.putCharSequence(keyForField(FIELD_ALBUM_ARTIST), albumArtist); bundle.putCharSequence(keyForField(FIELD_ALBUM_ARTIST), albumArtist);
bundle.putCharSequence(keyForField(FIELD_DISPLAY_TITLE), displayTitle);
bundle.putCharSequence(keyForField(FIELD_SUBTITLE), subtitle);
bundle.putCharSequence(keyForField(FIELD_DESCRIPTION), description);
bundle.putParcelable(keyForField(FIELD_MEDIA_URI), mediaUri);
if (userRating != null) {
bundle.putBundle(keyForField(FIELD_USER_RATING), userRating.toBundle());
}
if (overallRating != null) {
bundle.putBundle(keyForField(FIELD_OVERALL_RATING), overallRating.toBundle());
}
return bundle; return bundle;
} }
/** Object that can restore {@link MediaMetadata} from a {@link Bundle}. */ /** Object that can restore {@link MediaMetadata} from a {@link Bundle}. */
public static final Creator<MediaMetadata> CREATOR = public static final Creator<MediaMetadata> CREATOR = MediaMetadata::fromBundle;
bundle ->
new MediaMetadata.Builder() private static MediaMetadata fromBundle(Bundle bundle) {
.setTrackTitle(bundle.getCharSequence(keyForField(FIELD_TRACK_TITLE))) Builder builder = new Builder();
.setTrackArtist(bundle.getCharSequence(keyForField(FIELD_TRACK_ARTIST))) builder
.setAlbumTitle(bundle.getCharSequence(keyForField(FIELD_ALBUM_TITLE))) .setTrackTitle(bundle.getCharSequence(keyForField(FIELD_TRACK_TITLE)))
.setAlbumArtist(bundle.getCharSequence(keyForField(FIELD_ALBUM_ARTIST))) .setTrackArtist(bundle.getCharSequence(keyForField(FIELD_TRACK_ARTIST)))
.build(); .setAlbumTitle(bundle.getCharSequence(keyForField(FIELD_ALBUM_TITLE)))
.setAlbumArtist(bundle.getCharSequence(keyForField(FIELD_ALBUM_ARTIST)))
.setDisplayTitle(bundle.getCharSequence(keyForField(FIELD_DISPLAY_TITLE)))
.setSubtitle(bundle.getCharSequence(keyForField(FIELD_SUBTITLE)))
.setDescription(bundle.getCharSequence(keyForField(FIELD_DESCRIPTION)))
.setMediaUri(bundle.getParcelable(keyForField(FIELD_MEDIA_URI)));
if (bundle.containsKey(keyForField(FIELD_USER_RATING))) {
@Nullable Bundle fieldBundle = bundle.getBundle(keyForField(FIELD_USER_RATING));
if (fieldBundle != null) {
builder.setUserRating(Rating.CREATOR.fromBundle(fieldBundle));
}
}
if (bundle.containsKey(keyForField(FIELD_OVERALL_RATING))) {
@Nullable Bundle fieldBundle = bundle.getBundle(keyForField(FIELD_OVERALL_RATING));
if (fieldBundle != null) {
builder.setUserRating(Rating.CREATOR.fromBundle(fieldBundle));
}
}
return builder.build();
}
private static String keyForField(@FieldNumber int field) { private static String keyForField(@FieldNumber int field) {
return Integer.toString(field, Character.MAX_RADIX); return Integer.toString(field, Character.MAX_RADIX);