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
(cherry picked from commit 7fc2cdbe1bdf9968a1e73a670e5e32454090e1bd)
This commit is contained in:
rohks 2022-12-21 21:35:56 +00:00 committed by christosts
parent e07c887bcd
commit 0f8b861923
2 changed files with 61 additions and 11 deletions

View File

@ -507,9 +507,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
@ -1152,10 +1151,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;
}
@ -1180,10 +1187,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);
}

View File

@ -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 org.junit.Assert;
import org.junit.Test;
@ -402,7 +403,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)