diff --git a/library/common/src/main/java/com/google/android/exoplayer2/source/ads/AdPlaybackState.java b/library/common/src/main/java/com/google/android/exoplayer2/source/ads/AdPlaybackState.java index 8d609f507e1..719a215244f 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/source/ads/AdPlaybackState.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/source/ads/AdPlaybackState.java @@ -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 @@ -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; } @@ -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); } diff --git a/library/common/src/test/java/com/google/android/exoplayer2/source/ads/AdPlaybackStateTest.java b/library/common/src/test/java/com/google/android/exoplayer2/source/ads/AdPlaybackStateTest.java index 114a22f9904..3f7e2c7d643 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/source/ads/AdPlaybackStateTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/source/ads/AdPlaybackStateTest.java @@ -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; @@ -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)