From 581e543d395dcf01434b8c435214ba413c626cf0 Mon Sep 17 00:00:00 2001 From: jinpark Date: Mon, 14 Jun 2021 15:14:29 +0100 Subject: [PATCH] Add toBundle(boolean excludeMediaItems) to Timeline. Add MediaItem.EMPTY. PiperOrigin-RevId: 379273172 --- .../google/android/exoplayer2/MediaItem.java | 3 ++ .../google/android/exoplayer2/Timeline.java | 49 +++++++++++++------ 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java b/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java index 0b4f7409d8..e8373532a1 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/MediaItem.java @@ -1203,6 +1203,9 @@ public final class MediaItem implements Bundleable { */ public static final String DEFAULT_MEDIA_ID = ""; + /** Empty {@link MediaItem}. */ + public static final MediaItem EMPTY = new MediaItem.Builder().build(); + /** Identifies the media item. */ public final String mediaId; diff --git a/library/common/src/main/java/com/google/android/exoplayer2/Timeline.java b/library/common/src/main/java/com/google/android/exoplayer2/Timeline.java index d332272801..b0e153d780 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/Timeline.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/Timeline.java @@ -440,18 +440,11 @@ public abstract class Timeline implements Bundleable { private static final int FIELD_LAST_PERIOD_INDEX = 12; private static final int FIELD_POSITION_IN_FIRST_PERIOD_US = 13; - /** - * {@inheritDoc} - * - *

It omits the {@link #uid} and {@link #manifest} fields. The {@link #uid} of an instance - * restored by {@link #CREATOR} will be a fake {@link Object} and the {@link #manifest} of the - * instance will be {@code null}. - */ - // TODO(b/166765820): See if missing fields would be okay and add them to the Bundle otherwise. - @Override - public Bundle toBundle() { + private final Bundle toBundle(boolean excludeMediaItem) { Bundle bundle = new Bundle(); - bundle.putBundle(keyForField(FIELD_MEDIA_ITEM), mediaItem.toBundle()); + bundle.putBundle( + keyForField(FIELD_MEDIA_ITEM), + excludeMediaItem ? MediaItem.EMPTY.toBundle() : mediaItem.toBundle()); bundle.putLong(keyForField(FIELD_PRESENTATION_START_TIME_MS), presentationStartTimeMs); bundle.putLong(keyForField(FIELD_WINDOW_START_TIME_MS), windowStartTimeMs); bundle.putLong( @@ -471,6 +464,19 @@ public abstract class Timeline implements Bundleable { return bundle; } + /** + * {@inheritDoc} + * + *

It omits the {@link #uid} and {@link #manifest} fields. The {@link #uid} of an instance + * restored by {@link #CREATOR} will be a fake {@link Object} and the {@link #manifest} of the + * instance will be {@code null}. + */ + // TODO(b/166765820): See if missing fields would be okay and add them to the Bundle otherwise. + @Override + public Bundle toBundle() { + return toBundle(/* excludeMediaItem= */ false); + } + /** * Object that can restore {@link Period} from a {@link Bundle}. * @@ -1309,14 +1315,17 @@ public abstract class Timeline implements Bundleable { *

The {@link #getWindow(int, Window)} windows} and {@link #getPeriod(int, Period) periods} of * an instance restored by {@link #CREATOR} may have missing fields as described in {@link * Window#toBundle()} and {@link Period#toBundle()}. + * + * @param excludeMediaItems Whether to exclude all {@link Window#mediaItem media items} of windows + * in the timeline. */ - @Override - public final Bundle toBundle() { + public final Bundle toBundle(boolean excludeMediaItems) { List windowBundles = new ArrayList<>(); int windowCount = getWindowCount(); Window window = new Window(); for (int i = 0; i < windowCount; i++) { - windowBundles.add(getWindow(i, window, /* defaultPositionProjectionUs= */ 0).toBundle()); + windowBundles.add( + getWindow(i, window, /* defaultPositionProjectionUs= */ 0).toBundle(excludeMediaItems)); } List periodBundles = new ArrayList<>(); @@ -1345,6 +1354,18 @@ public abstract class Timeline implements Bundleable { return bundle; } + /** + * {@inheritDoc} + * + *

The {@link #getWindow(int, Window)} windows} and {@link #getPeriod(int, Period) periods} of + * an instance restored by {@link #CREATOR} may have missing fields as described in {@link + * Window#toBundle()} and {@link Period#toBundle()}. + */ + @Override + public final Bundle toBundle() { + return toBundle(/* excludeMediaItems= */ false); + } + /** * Object that can restore a {@link Timeline} from a {@link Bundle}. *