@@ -350,15 +350,9 @@ public boolean equals(@Nullable Object o) {
350350 return false ;
351351 }
352352 PositionInfo that = (PositionInfo ) o ;
353- return mediaItemIndex == that .mediaItemIndex
354- && periodIndex == that .periodIndex
355- && positionMs == that .positionMs
356- && contentPositionMs == that .contentPositionMs
357- && adGroupIndex == that .adGroupIndex
358- && adIndexInAdGroup == that .adIndexInAdGroup
353+ return equalsForBundling (that )
359354 && Objects .equal (windowUid , that .windowUid )
360- && Objects .equal (periodUid , that .periodUid )
361- && Objects .equal (mediaItem , that .mediaItem );
355+ && Objects .equal (periodUid , that .periodUid );
362356 }
363357
364358 @ Override
@@ -375,6 +369,21 @@ public int hashCode() {
375369 adIndexInAdGroup );
376370 }
377371
372+ /**
373+ * Returns whether this position info and the other position info would result in the same
374+ * {@link #toBundle() Bundle}.
375+ */
376+ @ UnstableApi
377+ public boolean equalsForBundling (PositionInfo other ) {
378+ return mediaItemIndex == other .mediaItemIndex
379+ && periodIndex == other .periodIndex
380+ && positionMs == other .positionMs
381+ && contentPositionMs == other .contentPositionMs
382+ && adGroupIndex == other .adGroupIndex
383+ && adIndexInAdGroup == other .adIndexInAdGroup
384+ && Objects .equal (mediaItem , other .mediaItem );
385+ }
386+
378387 // Bundleable implementation.
379388
380389 private static final String FIELD_MEDIA_ITEM_INDEX = Util .intToStringMaxRadix (0 );
@@ -386,39 +395,66 @@ public int hashCode() {
386395 private static final String FIELD_AD_INDEX_IN_AD_GROUP = Util .intToStringMaxRadix (6 );
387396
388397 /**
389- * {@inheritDoc}
398+ * Returns a copy of this position info, filtered by the specified available commands.
390399 *
391- * <p>It omits the {@link #windowUid} and {@link #periodUid} fields. The {@link #windowUid} and
392- * {@link #periodUid} of an instance restored by {@link #CREATOR} will always be {@code null}.
400+ * <p>The filtered fields are reset to their default values.
401+ *
402+ * <p>The return value may be the same object if nothing is filtered.
403+ *
404+ * @param canAccessCurrentMediaItem Whether {@link Player#COMMAND_GET_CURRENT_MEDIA_ITEM} is
405+ * available.
406+ * @param canAccessTimeline Whether {@link Player#COMMAND_GET_TIMELINE} is available.
407+ * @return The filtered position info.
393408 */
394409 @ UnstableApi
395- @ Override
396- public Bundle toBundle () {
397- return toBundle (/* canAccessCurrentMediaItem= */ true , /* canAccessTimeline= */ true );
410+ public PositionInfo filterByAvailableCommands (
411+ boolean canAccessCurrentMediaItem , boolean canAccessTimeline ) {
412+ if (canAccessCurrentMediaItem && canAccessTimeline ) {
413+ return this ;
414+ }
415+ return new PositionInfo (
416+ windowUid ,
417+ canAccessTimeline ? mediaItemIndex : 0 ,
418+ canAccessCurrentMediaItem ? mediaItem : null ,
419+ periodUid ,
420+ canAccessTimeline ? periodIndex : 0 ,
421+ canAccessCurrentMediaItem ? positionMs : 0 ,
422+ canAccessCurrentMediaItem ? contentPositionMs : 0 ,
423+ canAccessCurrentMediaItem ? adGroupIndex : C .INDEX_UNSET ,
424+ canAccessCurrentMediaItem ? adIndexInAdGroup : C .INDEX_UNSET );
398425 }
399426
400427 /**
401- * Returns a {@link Bundle} representing the information stored in this object, filtered by
402- * available commands.
428+ * {@inheritDoc}
403429 *
404- * @param canAccessCurrentMediaItem Whether the {@link Bundle} should contain information
405- * accessbile with {@link #COMMAND_GET_CURRENT_MEDIA_ITEM}.
406- * @param canAccessTimeline Whether the {@link Bundle} should contain information accessbile
407- * with {@link #COMMAND_GET_TIMELINE}.
430+ * <p>It omits the {@link #windowUid} and {@link #periodUid} fields. The {@link #windowUid} and
431+ * {@link #periodUid} of an instance restored by {@link #CREATOR} will always be {@code null}.
408432 */
409433 @ UnstableApi
410- public Bundle toBundle (boolean canAccessCurrentMediaItem , boolean canAccessTimeline ) {
434+ @ Override
435+ public Bundle toBundle () {
411436 Bundle bundle = new Bundle ();
412- bundle .putInt (FIELD_MEDIA_ITEM_INDEX , canAccessTimeline ? mediaItemIndex : 0 );
413- if (mediaItem != null && canAccessCurrentMediaItem ) {
437+ if (mediaItemIndex != 0 ) {
438+ bundle .putInt (FIELD_MEDIA_ITEM_INDEX , mediaItemIndex );
439+ }
440+ if (mediaItem != null ) {
414441 bundle .putBundle (FIELD_MEDIA_ITEM , mediaItem .toBundle ());
415442 }
416- bundle .putInt (FIELD_PERIOD_INDEX , canAccessTimeline ? periodIndex : 0 );
417- bundle .putLong (FIELD_POSITION_MS , canAccessCurrentMediaItem ? positionMs : 0 );
418- bundle .putLong (FIELD_CONTENT_POSITION_MS , canAccessCurrentMediaItem ? contentPositionMs : 0 );
419- bundle .putInt (FIELD_AD_GROUP_INDEX , canAccessCurrentMediaItem ? adGroupIndex : C .INDEX_UNSET );
420- bundle .putInt (
421- FIELD_AD_INDEX_IN_AD_GROUP , canAccessCurrentMediaItem ? adIndexInAdGroup : C .INDEX_UNSET );
443+ if (periodIndex != 0 ) {
444+ bundle .putInt (FIELD_PERIOD_INDEX , periodIndex );
445+ }
446+ if (positionMs != 0 ) {
447+ bundle .putLong (FIELD_POSITION_MS , positionMs );
448+ }
449+ if (contentPositionMs != 0 ) {
450+ bundle .putLong (FIELD_CONTENT_POSITION_MS , contentPositionMs );
451+ }
452+ if (adGroupIndex != C .INDEX_UNSET ) {
453+ bundle .putInt (FIELD_AD_GROUP_INDEX , adGroupIndex );
454+ }
455+ if (adIndexInAdGroup != C .INDEX_UNSET ) {
456+ bundle .putInt (FIELD_AD_INDEX_IN_AD_GROUP , adIndexInAdGroup );
457+ }
422458 return bundle ;
423459 }
424460
0 commit comments