Skip to content

Commit

Permalink
Add track number & total to MediaMetadata
Browse files Browse the repository at this point in the history
#minor-release

PiperOrigin-RevId: 374235979
  • Loading branch information
Samrobbo authored and ojw28 committed Jun 6, 2021
1 parent 6bba218 commit 941a71a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 8 deletions.
Expand Up @@ -49,6 +49,8 @@ public static final class Builder {
@Nullable private Rating overallRating;
@Nullable private byte[] artworkData;
@Nullable private Uri artworkUri;
@Nullable private Integer trackNumber;
@Nullable private Integer totalTrackCount;

public Builder() {}

Expand All @@ -65,6 +67,8 @@ private Builder(MediaMetadata mediaMetadata) {
this.overallRating = mediaMetadata.overallRating;
this.artworkData = mediaMetadata.artworkData;
this.artworkUri = mediaMetadata.artworkUri;
this.trackNumber = mediaMetadata.trackNumber;
this.totalTrackCount = mediaMetadata.totalTrackCount;
}

/** Sets the title. */
Expand Down Expand Up @@ -143,6 +147,18 @@ public Builder setArtworkUri(@Nullable Uri artworkUri) {
return this;
}

/** Sets the track number. */
public Builder setTrackNumber(@Nullable Integer trackNumber) {
this.trackNumber = trackNumber;
return this;
}

/** Sets the total number of tracks. */
public Builder setTotalTrackCount(@Nullable Integer totalTrackCount) {
this.totalTrackCount = totalTrackCount;
return this;
}

/**
* Sets all fields supported by the {@link Metadata.Entry entries} within the {@link Metadata}.
*
Expand Down Expand Up @@ -218,6 +234,10 @@ public MediaMetadata build() {
@Nullable public final byte[] artworkData;
/** Optional artwork {@link Uri}. */
@Nullable public final Uri artworkUri;
/** Optional track number. */
@Nullable public final Integer trackNumber;
/** Optional total number of tracks. */
@Nullable public final Integer totalTrackCount;

private MediaMetadata(Builder builder) {
this.title = builder.title;
Expand All @@ -232,6 +252,8 @@ private MediaMetadata(Builder builder) {
this.overallRating = builder.overallRating;
this.artworkData = builder.artworkData;
this.artworkUri = builder.artworkUri;
this.trackNumber = builder.trackNumber;
this.totalTrackCount = builder.totalTrackCount;
}

/** Returns a new {@link Builder} instance with the current {@link MediaMetadata} fields. */
Expand Down Expand Up @@ -259,7 +281,9 @@ public boolean equals(@Nullable Object obj) {
&& Util.areEqual(userRating, that.userRating)
&& Util.areEqual(overallRating, that.overallRating)
&& Arrays.equals(artworkData, that.artworkData)
&& Util.areEqual(artworkUri, that.artworkUri);
&& Util.areEqual(artworkUri, that.artworkUri)
&& Util.areEqual(trackNumber, that.trackNumber)
&& Util.areEqual(totalTrackCount, that.totalTrackCount);
}

@Override
Expand All @@ -276,7 +300,9 @@ public int hashCode() {
userRating,
overallRating,
Arrays.hashCode(artworkData),
artworkUri);
artworkUri,
trackNumber,
totalTrackCount);
}

// Bundleable implementation.
Expand All @@ -295,7 +321,9 @@ public int hashCode() {
FIELD_USER_RATING,
FIELD_OVERALL_RATING,
FIELD_ARTWORK_DATA,
FIELD_ARTWORK_URI
FIELD_ARTWORK_URI,
FIELD_TRACK_NUMBER,
FIELD_TOTAL_TRACK_COUNT
})
private @interface FieldNumber {}

Expand All @@ -311,6 +339,8 @@ public int hashCode() {
private static final int FIELD_OVERALL_RATING = 9;
private static final int FIELD_ARTWORK_DATA = 10;
private static final int FIELD_ARTWORK_URI = 11;
private static final int FIELD_TRACK_NUMBER = 12;
private static final int FIELD_TOTAL_TRACK_COUNT = 13;

@Override
public Bundle toBundle() {
Expand All @@ -332,7 +362,12 @@ public Bundle toBundle() {
if (overallRating != null) {
bundle.putBundle(keyForField(FIELD_OVERALL_RATING), overallRating.toBundle());
}

if (trackNumber != null) {
bundle.putInt(keyForField(FIELD_TRACK_NUMBER), trackNumber);
}
if (totalTrackCount != null) {
bundle.putInt(keyForField(FIELD_TOTAL_TRACK_COUNT), totalTrackCount);
}
return bundle;
}

Expand Down Expand Up @@ -365,6 +400,12 @@ private static MediaMetadata fromBundle(Bundle bundle) {
builder.setOverallRating(Rating.CREATOR.fromBundle(fieldBundle));
}
}
if (bundle.containsKey(keyForField(FIELD_TRACK_NUMBER))) {
builder.setTrackNumber(bundle.getInt(keyForField(FIELD_TRACK_NUMBER)));
}
if (bundle.containsKey(keyForField(FIELD_TOTAL_TRACK_COUNT))) {
builder.setTotalTrackCount(bundle.getInt(keyForField(FIELD_TOTAL_TRACK_COUNT)));
}

return builder.build();
}
Expand Down
Expand Up @@ -60,6 +60,19 @@ public void populateMediaMetadata(MediaMetadata.Builder builder) {
case "TALB":
builder.setAlbumTitle(value);
break;
case "TRK":
case "TRCK":
String[] trackNumbers = Util.split(value, "/");
try {
int trackNumber = Integer.parseInt(trackNumbers[0]);
@Nullable
Integer totalTrackCount =
trackNumbers.length > 1 ? Integer.parseInt(trackNumbers[1]) : null;
builder.setTrackNumber(trackNumber).setTotalTrackCount(totalTrackCount);
} catch (NumberFormatException e) {
// Do nothing, invalid input.
}
break;
default:
break;
}
Expand Down
Expand Up @@ -23,7 +23,9 @@
import com.google.android.exoplayer2.metadata.id3.ApicFrame;
import com.google.android.exoplayer2.metadata.id3.TextInformationFrame;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand Down Expand Up @@ -91,14 +93,35 @@ public void roundTripViaBundle_yieldsEqualInstance() {
}

@Test
public void builderPopulatedFromTextInformationFrameEntry_setsTitle() {
public void builderPopulatedFromTextInformationFrameEntry_setsValues() {
String title = "the title";
Metadata.Entry entry =
new TextInformationFrame(/* id= */ "TT2", /* description= */ null, /* value= */ title);
String artist = "artist";
String albumTitle = "album title";
String albumArtist = "album Artist";
String trackNumberInfo = "11/17";

List<Metadata.Entry> entries =
ImmutableList.of(
new TextInformationFrame(/* id= */ "TT2", /* description= */ null, /* value= */ title),
new TextInformationFrame(/* id= */ "TP1", /* description= */ null, /* value= */ artist),
new TextInformationFrame(
/* id= */ "TAL", /* description= */ null, /* value= */ albumTitle),
new TextInformationFrame(
/* id= */ "TP2", /* description= */ null, /* value= */ albumArtist),
new TextInformationFrame(
/* id= */ "TRK", /* description= */ null, /* value= */ trackNumberInfo));
MediaMetadata.Builder builder = MediaMetadata.EMPTY.buildUpon();

entry.populateMediaMetadata(builder);
for (Metadata.Entry entry : entries) {
entry.populateMediaMetadata(builder);
}

assertThat(builder.build().title.toString()).isEqualTo(title);
assertThat(builder.build().artist.toString()).isEqualTo(artist);
assertThat(builder.build().albumTitle.toString()).isEqualTo(albumTitle);
assertThat(builder.build().albumArtist.toString()).isEqualTo(albumArtist);
assertThat(builder.build().trackNumber).isEqualTo(11);
assertThat(builder.build().totalTrackCount).isEqualTo(17);
}

@Test
Expand Down

0 comments on commit 941a71a

Please sign in to comment.