Make MediaItem.AdsConfiguration bundleable

PiperOrigin-RevId: 534848363
(cherry picked from commit 3cc4e44fe30f99be578cf5f817c9b3195d853e66)
This commit is contained in:
jbibik 2023-05-24 16:22:57 +01:00 committed by Tofunmi Adigun-Hameed
parent 8573a4ba85
commit 171f48fccc
2 changed files with 55 additions and 7 deletions

View File

@ -900,7 +900,7 @@ public final class MediaItem implements Bundleable {
private static final String FIELD_FORCED_SESSION_TRACK_TYPES = Util.intToStringMaxRadix(6); private static final String FIELD_FORCED_SESSION_TRACK_TYPES = Util.intToStringMaxRadix(6);
private static final String FIELD_KEY_SET_ID = Util.intToStringMaxRadix(7); private static final String FIELD_KEY_SET_ID = Util.intToStringMaxRadix(7);
/** Object that can restore {@link DrmConfiguration} from a {@link Bundle}. */ /** An object that can restore {@link DrmConfiguration} from a {@link Bundle}. */
@UnstableApi @UnstableApi
public static final Creator<DrmConfiguration> CREATOR = DrmConfiguration::fromBundle; public static final Creator<DrmConfiguration> CREATOR = DrmConfiguration::fromBundle;
@ -968,7 +968,7 @@ public final class MediaItem implements Bundleable {
} }
/** Configuration for playing back linear ads with a media item. */ /** Configuration for playing back linear ads with a media item. */
public static final class AdsConfiguration { public static final class AdsConfiguration implements Bundleable {
/** Builder for {@link AdsConfiguration} instances. */ /** Builder for {@link AdsConfiguration} instances. */
public static final class Builder { public static final class Builder {
@ -1052,6 +1052,39 @@ public final class MediaItem implements Bundleable {
result = 31 * result + (adsId != null ? adsId.hashCode() : 0); result = 31 * result + (adsId != null ? adsId.hashCode() : 0);
return result; return result;
} }
// Bundleable implementation.
private static final String FIELD_AD_TAG_URI = Util.intToStringMaxRadix(0);
/**
* An object that can restore {@link AdsConfiguration} from a {@link Bundle}.
*
* <p>The {@link #adsId} of a restored instance will always be {@code null}.
*/
@UnstableApi
public static final Creator<AdsConfiguration> CREATOR = AdsConfiguration::fromBundle;
@UnstableApi
private static AdsConfiguration fromBundle(Bundle bundle) {
@Nullable Uri adTagUri = bundle.getParcelable(FIELD_AD_TAG_URI);
checkNotNull(adTagUri);
return new AdsConfiguration.Builder(adTagUri).build();
}
/**
* {@inheritDoc}
*
* <p>It omits the {@link #adsId} field. The {@link #adsId} of an instance restored from such a
* bundle by {@link #CREATOR} will be {@code null}.
*/
@UnstableApi
@Override
public Bundle toBundle() {
Bundle bundle = new Bundle();
bundle.putParcelable(FIELD_AD_TAG_URI, adTagUri);
return bundle;
}
} }
/** Properties for local playback. */ /** Properties for local playback. */
@ -1403,7 +1436,7 @@ public final class MediaItem implements Bundleable {
return bundle; return bundle;
} }
/** Object that can restore {@link LiveConfiguration} from a {@link Bundle}. */ /** An object that can restore {@link LiveConfiguration} from a {@link Bundle}. */
@UnstableApi @UnstableApi
public static final Creator<LiveConfiguration> CREATOR = public static final Creator<LiveConfiguration> CREATOR =
bundle -> bundle ->
@ -1601,7 +1634,7 @@ public final class MediaItem implements Bundleable {
private static final String FIELD_LABEL = Util.intToStringMaxRadix(5); private static final String FIELD_LABEL = Util.intToStringMaxRadix(5);
private static final String FIELD_ID = Util.intToStringMaxRadix(6); private static final String FIELD_ID = Util.intToStringMaxRadix(6);
/** Object that can restore {@link SubtitleConfiguration} from a {@link Bundle}. */ /** An object that can restore {@link SubtitleConfiguration} from a {@link Bundle}. */
@UnstableApi @UnstableApi
public static final Creator<SubtitleConfiguration> CREATOR = SubtitleConfiguration::fromBundle; public static final Creator<SubtitleConfiguration> CREATOR = SubtitleConfiguration::fromBundle;
@ -1895,7 +1928,7 @@ public final class MediaItem implements Bundleable {
return bundle; return bundle;
} }
/** Object that can restore {@link ClippingConfiguration} from a {@link Bundle}. */ /** An object that can restore {@link ClippingConfiguration} from a {@link Bundle}. */
@UnstableApi @UnstableApi
public static final Creator<ClippingProperties> CREATOR = public static final Creator<ClippingProperties> CREATOR =
bundle -> bundle ->
@ -2055,7 +2088,7 @@ public final class MediaItem implements Bundleable {
return bundle; return bundle;
} }
/** Object that can restore {@link RequestMetadata} from a {@link Bundle}. */ /** An object that can restore {@link RequestMetadata} from a {@link Bundle}. */
@UnstableApi @UnstableApi
public static final Creator<RequestMetadata> CREATOR = public static final Creator<RequestMetadata> CREATOR =
bundle -> bundle ->
@ -2194,7 +2227,7 @@ public final class MediaItem implements Bundleable {
} }
/** /**
* Object that can restore {@link MediaItem} from a {@link Bundle}. * An object that can restore {@link MediaItem} from a {@link Bundle}.
* *
* <p>The {@link #localConfiguration} of a restored instance will always be {@code null}. * <p>The {@link #localConfiguration} of a restored instance will always be {@code null}.
*/ */

View File

@ -593,6 +593,21 @@ public class MediaItemTest {
assertThat(mediaItem.localConfiguration.adsConfiguration.adsId).isEqualTo(adsId); assertThat(mediaItem.localConfiguration.adsConfiguration.adsId).isEqualTo(adsId);
} }
@Test
public void createAdsConfigurationInstance_roundTripViaBundle_yieldsEqualInstanceExceptAdsId() {
Uri adTagUri = Uri.parse(URI_STRING + "/ad");
MediaItem.AdsConfiguration adsConfiguration =
new MediaItem.AdsConfiguration.Builder(adTagUri)
.setAdsId("Something that will be lost")
.build();
MediaItem.AdsConfiguration adsConfigurationFromBundle =
MediaItem.AdsConfiguration.CREATOR.fromBundle(adsConfiguration.toBundle());
assertThat(adsConfigurationFromBundle.adTagUri).isEqualTo(adsConfiguration.adTagUri);
assertThat(adsConfigurationFromBundle.adsId).isNull();
}
@Test @Test
public void builderSetMediaMetadata_setsMetadata() { public void builderSetMediaMetadata_setsMetadata() {
MediaMetadata mediaMetadata = new MediaMetadata.Builder().setTitle("title").build(); MediaMetadata mediaMetadata = new MediaMetadata.Builder().setTitle("title").build();