Skip to content

Commit

Permalink
Optimise bundling for AdPlaybackState using AdPlaybackState.NONE
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rohitjoins authored and marcbaechinger committed Jan 4, 2023
1 parent 4d1283f commit f2eac2d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,8 @@ public Bundle toBundle() {
@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
Expand Down Expand Up @@ -1153,10 +1152,18 @@ public Bundle toBundle() {
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;
}

Expand All @@ -1181,10 +1188,15 @@ private static AdPlaybackState fromBundle(Bundle bundle) {
}
}
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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;
Expand Down Expand Up @@ -403,7 +404,44 @@ public void withResetAdGroup_resetsAdsInFinalStates() {
}

@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)
Expand Down

0 comments on commit f2eac2d

Please sign in to comment.