From 2c2aaf0a4bcb18d67da074aaa2401e30eb0c8d27 Mon Sep 17 00:00:00 2001 From: eguven Date: Fri, 9 Feb 2018 06:36:19 -0800 Subject: [PATCH] Make SegmentDownloadAction constructor keys parameter simpler Removed option to pass null keys parameter. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=185134822 --- .../offline/SegmentDownloadAction.java | 41 ++++++++----------- .../exoplayer2/offline/SegmentDownloader.java | 4 +- .../dash/offline/DashDownloaderTest.java | 5 --- .../source/hls/offline/HlsDownloaderTest.java | 5 --- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloadAction.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloadAction.java index b77ac5bad8..6d689d53b3 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloadAction.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloadAction.java @@ -40,16 +40,11 @@ public abstract class SegmentDownloadAction extends DownloadAction { public DownloadAction readFromStream(int version, DataInputStream input) throws IOException { Uri manifestUri = Uri.parse(input.readUTF()); String data = input.readUTF(); + boolean removeAction = input.readBoolean(); int keyCount = input.readInt(); - boolean removeAction = keyCount == -1; - K[] keys; - if (removeAction) { - keys = null; - } else { - keys = createKeyArray(keyCount); - for (int i = 0; i < keyCount; i++) { - keys[i] = readKey(input); - } + K[] keys = createKeyArray(keyCount); + for (int i = 0; i < keyCount; i++) { + keys[i] = readKey(input); } return createDownloadAction(manifestUri, removeAction, data, keys); } @@ -74,12 +69,15 @@ public abstract class SegmentDownloadAction extends DownloadAction { * @param manifestUri The {@link Uri} of the manifest to be downloaded. * @param removeAction Whether the data will be removed. If {@code false} it will be downloaded. * @param data Optional custom data for this action. If null, an empty string is used. - * @param keys Keys of representations to be downloaded. If empty or null, all representations are - * downloaded. If {@code removeAction} is true, this is ignored. + * @param keys Keys of representations to be downloaded. If empty, all representations are + * downloaded. If {@code removeAction} is true, {@code keys} should be an empty array. */ protected SegmentDownloadAction(Uri manifestUri, boolean removeAction, String data, K[] keys) { super(data); - Assertions.checkArgument(!removeAction || keys == null || keys.length == 0); + Assertions.checkNotNull(keys); + if (removeAction) { + Assertions.checkArgument(keys.length == 0); + } this.manifestUri = manifestUri; this.keys = keys; this.removeAction = removeAction; @@ -94,13 +92,10 @@ public abstract class SegmentDownloadAction extends DownloadAction { public final void writeToStream(DataOutputStream output) throws IOException { output.writeUTF(manifestUri.toString()); output.writeUTF(getData()); - if (isRemoveAction()) { - output.writeInt(-1); - } else { - output.writeInt(keys.length); - for (K key : keys) { - writeKey(output, key); - } + output.writeBoolean(removeAction); + output.writeInt(keys.length); + for (K key : keys) { + writeKey(output, key); } } @@ -124,11 +119,9 @@ public abstract class SegmentDownloadAction extends DownloadAction { } SegmentDownloadAction that = (SegmentDownloadAction) o; return manifestUri.equals(that.manifestUri) - && (keys == null || keys.length == 0 - ? (that.keys == null || that.keys.length == 0) - : (that.keys != null - && that.keys.length == keys.length - && Arrays.asList(keys).containsAll(Arrays.asList(that.keys)))); + && removeAction == that.removeAction + && keys.length == that.keys.length + && Arrays.asList(keys).containsAll(Arrays.asList(that.keys)); } @Override diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java index 6abb950254..63414dc39e 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java @@ -101,10 +101,10 @@ public abstract class SegmentDownloader implements Downloader { /** * Selects multiple representations pointed to by the keys for downloading, checking status. Any - * previous selection is cleared. If keys are null or empty, all representations are downloaded. + * previous selection is cleared. If keys array is empty, all representations are downloaded. */ public final void selectRepresentations(K[] keys) { - this.keys = (keys != null && keys.length > 0) ? keys.clone() : null; + this.keys = keys.length > 0 ? keys.clone() : null; resetCounters(); } diff --git a/library/dash/src/androidTest/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java b/library/dash/src/androidTest/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java index 5e7ce43ebf..868166673a 100644 --- a/library/dash/src/androidTest/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java +++ b/library/dash/src/androidTest/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java @@ -184,11 +184,6 @@ public class DashDownloaderTest extends InstrumentationTestCase { // select something random dashDownloader.selectRepresentations(new RepresentationKey[] {new RepresentationKey(0, 0, 0)}); // clear selection - dashDownloader.selectRepresentations(null); - dashDownloader.download(null); - assertCachedData(cache, fakeDataSet); - dashDownloader.remove(); - dashDownloader.selectRepresentations(new RepresentationKey[0]); dashDownloader.download(null); assertCachedData(cache, fakeDataSet); diff --git a/library/hls/src/androidTest/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java b/library/hls/src/androidTest/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java index c068a8182b..13083dc01c 100644 --- a/library/hls/src/androidTest/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java +++ b/library/hls/src/androidTest/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java @@ -156,11 +156,6 @@ public class HlsDownloaderTest extends InstrumentationTestCase { // select something random hlsDownloader.selectRepresentations(new String[] {MEDIA_PLAYLIST_1_URI}); // clear selection - hlsDownloader.selectRepresentations(null); - hlsDownloader.download(null); - assertCachedData(cache, fakeDataSet); - hlsDownloader.remove(); - hlsDownloader.selectRepresentations(new String[0]); hlsDownloader.download(null); assertCachedData(cache, fakeDataSet);