Skip to content

Commit

Permalink
Remove parameters with null values from bundle in MediaMetadata
Browse files Browse the repository at this point in the history
Improves the time taken to construct `playerInfo` from its bundle from ~450 ms to ~400 ms. Each `MediaItem` inside `Timeline.Window` contains `MediaMetadata` and hence is a good candidate for bundling optimisations. There already exists a test to check all parameters for null values when unset.

PiperOrigin-RevId: 495614719
  • Loading branch information
rohitjoins authored and tianyif committed Dec 21, 2022
1 parent e46b4c0 commit 8dea624
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1174,22 +1174,51 @@ public int hashCode() {
@Override
public Bundle toBundle() {
Bundle bundle = new Bundle();
bundle.putCharSequence(keyForField(FIELD_TITLE), title);
bundle.putCharSequence(keyForField(FIELD_ARTIST), artist);
bundle.putCharSequence(keyForField(FIELD_ALBUM_TITLE), albumTitle);
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.putByteArray(keyForField(FIELD_ARTWORK_DATA), artworkData);
bundle.putParcelable(keyForField(FIELD_ARTWORK_URI), artworkUri);
bundle.putCharSequence(keyForField(FIELD_WRITER), writer);
bundle.putCharSequence(keyForField(FIELD_COMPOSER), composer);
bundle.putCharSequence(keyForField(FIELD_CONDUCTOR), conductor);
bundle.putCharSequence(keyForField(FIELD_GENRE), genre);
bundle.putCharSequence(keyForField(FIELD_COMPILATION), compilation);
bundle.putCharSequence(keyForField(FIELD_STATION), station);

if (title != null) {
bundle.putCharSequence(keyForField(FIELD_TITLE), title);
}
if (artist != null) {
bundle.putCharSequence(keyForField(FIELD_ARTIST), artist);
}
if (albumTitle != null) {
bundle.putCharSequence(keyForField(FIELD_ALBUM_TITLE), albumTitle);
}
if (albumArtist != null) {
bundle.putCharSequence(keyForField(FIELD_ALBUM_ARTIST), albumArtist);
}
if (displayTitle != null) {
bundle.putCharSequence(keyForField(FIELD_DISPLAY_TITLE), displayTitle);
}
if (subtitle != null) {
bundle.putCharSequence(keyForField(FIELD_SUBTITLE), subtitle);
}
if (description != null) {
bundle.putCharSequence(keyForField(FIELD_DESCRIPTION), description);
}
if (artworkData != null) {
bundle.putByteArray(keyForField(FIELD_ARTWORK_DATA), artworkData);
}
if (artworkUri != null) {
bundle.putParcelable(keyForField(FIELD_ARTWORK_URI), artworkUri);
}
if (writer != null) {
bundle.putCharSequence(keyForField(FIELD_WRITER), writer);
}
if (composer != null) {
bundle.putCharSequence(keyForField(FIELD_COMPOSER), composer);
}
if (conductor != null) {
bundle.putCharSequence(keyForField(FIELD_CONDUCTOR), conductor);
}
if (genre != null) {
bundle.putCharSequence(keyForField(FIELD_GENRE), genre);
}
if (compilation != null) {
bundle.putCharSequence(keyForField(FIELD_COMPILATION), compilation);
}
if (station != null) {
bundle.putCharSequence(keyForField(FIELD_STATION), station);
}
if (userRating != null) {
bundle.putBundle(keyForField(FIELD_USER_RATING), userRating.toBundle());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,27 @@ public void populate_populatesEveryField() {
}

@Test
public void roundTripViaBundle_yieldsEqualInstance() {
public void createMinimalMediaMetadata_roundTripViaBundle_yieldsEqualInstance() {
MediaMetadata mediaMetadata = new MediaMetadata.Builder().build();

MediaMetadata mediaMetadataFromBundle =
MediaMetadata.CREATOR.fromBundle(mediaMetadata.toBundle());

assertThat(mediaMetadataFromBundle).isEqualTo(mediaMetadata);
// Extras is not implemented in MediaMetadata.equals(Object o).
assertThat(mediaMetadataFromBundle.extras).isNull();
}

@Test
public void createFullyPopulatedMediaMetadata_roundTripViaBundle_yieldsEqualInstance() {
MediaMetadata mediaMetadata = getFullyPopulatedMediaMetadata();

MediaMetadata fromBundle = MediaMetadata.CREATOR.fromBundle(mediaMetadata.toBundle());
assertThat(fromBundle).isEqualTo(mediaMetadata);
MediaMetadata mediaMetadataFromBundle =
MediaMetadata.CREATOR.fromBundle(mediaMetadata.toBundle());

assertThat(mediaMetadataFromBundle).isEqualTo(mediaMetadata);
// Extras is not implemented in MediaMetadata.equals(Object o).
assertThat(fromBundle.extras.getString(EXTRAS_KEY)).isEqualTo(EXTRAS_VALUE);
assertThat(mediaMetadataFromBundle.extras.getString(EXTRAS_KEY)).isEqualTo(EXTRAS_VALUE);
}

@Test
Expand Down

0 comments on commit 8dea624

Please sign in to comment.