Optimise bundling for AdPlaybackState
using AdPlaybackState.NONE
Did not do this optimisation for `AdPlaybackState.AdGroup` as its length is zero for `AdPlaybackState` with no ads. No need to pass default values while fetching keys, which we always set in `AdPlaybackState.AdGroup.toBundle()`. PiperOrigin-RevId: 496995048
This commit is contained in:
parent
4d1283f43e
commit
f2eac2df71
@ -508,9 +508,8 @@ public final class AdPlaybackState implements Bundleable {
|
||||
@SuppressWarnings("nullness:type.argument")
|
||||
private static AdGroup fromBundle(Bundle bundle) {
|
||||
long timeUs = bundle.getLong(keyForField(FIELD_TIME_US));
|
||||
int count = bundle.getInt(keyForField(FIELD_COUNT), /* defaultValue= */ C.LENGTH_UNSET);
|
||||
int originalCount =
|
||||
bundle.getInt(keyForField(FIELD_ORIGINAL_COUNT), /* defaultValue= */ C.LENGTH_UNSET);
|
||||
int count = bundle.getInt(keyForField(FIELD_COUNT));
|
||||
int originalCount = bundle.getInt(keyForField(FIELD_ORIGINAL_COUNT));
|
||||
@Nullable
|
||||
ArrayList<@NullableType Uri> uriList = bundle.getParcelableArrayList(keyForField(FIELD_URIS));
|
||||
@Nullable
|
||||
@ -1153,10 +1152,18 @@ public final class AdPlaybackState implements Bundleable {
|
||||
for (AdGroup adGroup : adGroups) {
|
||||
adGroupBundleList.add(adGroup.toBundle());
|
||||
}
|
||||
bundle.putParcelableArrayList(keyForField(FIELD_AD_GROUPS), adGroupBundleList);
|
||||
bundle.putLong(keyForField(FIELD_AD_RESUME_POSITION_US), adResumePositionUs);
|
||||
bundle.putLong(keyForField(FIELD_CONTENT_DURATION_US), contentDurationUs);
|
||||
bundle.putInt(keyForField(FIELD_REMOVED_AD_GROUP_COUNT), removedAdGroupCount);
|
||||
if (!adGroupBundleList.isEmpty()) {
|
||||
bundle.putParcelableArrayList(keyForField(FIELD_AD_GROUPS), adGroupBundleList);
|
||||
}
|
||||
if (adResumePositionUs != NONE.adResumePositionUs) {
|
||||
bundle.putLong(keyForField(FIELD_AD_RESUME_POSITION_US), adResumePositionUs);
|
||||
}
|
||||
if (contentDurationUs != NONE.contentDurationUs) {
|
||||
bundle.putLong(keyForField(FIELD_CONTENT_DURATION_US), contentDurationUs);
|
||||
}
|
||||
if (removedAdGroupCount != NONE.removedAdGroupCount) {
|
||||
bundle.putInt(keyForField(FIELD_REMOVED_AD_GROUP_COUNT), removedAdGroupCount);
|
||||
}
|
||||
return bundle;
|
||||
}
|
||||
|
||||
@ -1181,10 +1188,15 @@ public final class AdPlaybackState implements Bundleable {
|
||||
}
|
||||
}
|
||||
long adResumePositionUs =
|
||||
bundle.getLong(keyForField(FIELD_AD_RESUME_POSITION_US), /* defaultValue= */ 0);
|
||||
bundle.getLong(
|
||||
keyForField(FIELD_AD_RESUME_POSITION_US), /* defaultValue= */ NONE.adResumePositionUs);
|
||||
long contentDurationUs =
|
||||
bundle.getLong(keyForField(FIELD_CONTENT_DURATION_US), /* defaultValue= */ C.TIME_UNSET);
|
||||
int removedAdGroupCount = bundle.getInt(keyForField(FIELD_REMOVED_AD_GROUP_COUNT));
|
||||
bundle.getLong(
|
||||
keyForField(FIELD_CONTENT_DURATION_US), /* defaultValue= */ NONE.contentDurationUs);
|
||||
int removedAdGroupCount =
|
||||
bundle.getInt(
|
||||
keyForField(FIELD_REMOVED_AD_GROUP_COUNT),
|
||||
/* defaultValue= */ NONE.removedAdGroupCount);
|
||||
return new AdPlaybackState(
|
||||
/* adsId= */ null, adGroups, adResumePositionUs, contentDurationUs, removedAdGroupCount);
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
import com.google.android.exoplayer2.C;
|
||||
import org.junit.Assert;
|
||||
@ -403,7 +404,44 @@ public class AdPlaybackStateTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void roundTripViaBundle_yieldsEqualFieldsExceptAdsId() {
|
||||
public void adPlaybackStateWithNoAds_checkValues() {
|
||||
AdPlaybackState adPlaybackStateWithNoAds = AdPlaybackState.NONE;
|
||||
|
||||
// Please refrain from altering these values since doing so would cause issues with backwards
|
||||
// compatibility.
|
||||
assertThat(adPlaybackStateWithNoAds.adsId).isNull();
|
||||
assertThat(adPlaybackStateWithNoAds.adGroupCount).isEqualTo(0);
|
||||
assertThat(adPlaybackStateWithNoAds.adResumePositionUs).isEqualTo(0);
|
||||
assertThat(adPlaybackStateWithNoAds.contentDurationUs).isEqualTo(C.TIME_UNSET);
|
||||
assertThat(adPlaybackStateWithNoAds.removedAdGroupCount).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adPlaybackStateWithNoAds_toBundleSkipsDefaultValues_fromBundleRestoresThem() {
|
||||
AdPlaybackState adPlaybackStateWithNoAds = AdPlaybackState.NONE;
|
||||
|
||||
Bundle adPlaybackStateWithNoAdsBundle = adPlaybackStateWithNoAds.toBundle();
|
||||
|
||||
// check Bundle created above, contains no keys.
|
||||
assertThat(adPlaybackStateWithNoAdsBundle.keySet()).isEmpty();
|
||||
|
||||
AdPlaybackState adPlaybackStateWithNoAdsFromBundle =
|
||||
AdPlaybackState.CREATOR.fromBundle(adPlaybackStateWithNoAdsBundle);
|
||||
|
||||
// check object retrieved from adPlaybackStateWithNoAdsBundle is equal to AdPlaybackState.NONE
|
||||
assertThat(adPlaybackStateWithNoAdsFromBundle.adsId).isEqualTo(adPlaybackStateWithNoAds.adsId);
|
||||
assertThat(adPlaybackStateWithNoAdsFromBundle.adGroupCount)
|
||||
.isEqualTo(adPlaybackStateWithNoAds.adGroupCount);
|
||||
assertThat(adPlaybackStateWithNoAdsFromBundle.adResumePositionUs)
|
||||
.isEqualTo(adPlaybackStateWithNoAds.adResumePositionUs);
|
||||
assertThat(adPlaybackStateWithNoAdsFromBundle.contentDurationUs)
|
||||
.isEqualTo(adPlaybackStateWithNoAds.contentDurationUs);
|
||||
assertThat(adPlaybackStateWithNoAdsFromBundle.removedAdGroupCount)
|
||||
.isEqualTo(adPlaybackStateWithNoAds.removedAdGroupCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAdPlaybackState_roundTripViaBundle_yieldsEqualFieldsExceptAdsId() {
|
||||
AdPlaybackState originalState =
|
||||
new AdPlaybackState(TEST_ADS_ID, TEST_AD_GROUP_TIMES_US)
|
||||
.withRemovedAdGroupCount(1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user