Remove parameters with null values from bundle in MediaMetadata

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
(cherry picked from commit d11e0a35c114225261a8fe472b0b93d4a8a6b727)
This commit is contained in:
rohks 2022-12-15 17:23:27 +00:00 committed by christosts
parent b1e4ac446f
commit 4f8d71e872
2 changed files with 63 additions and 20 deletions

View File

@ -1183,22 +1183,51 @@ public final class MediaMetadata implements Bundleable {
@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());
}

View File

@ -107,13 +107,27 @@ public class MediaMetadataTest {
}
@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