diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java index 2b60f50cb5..cec46c3664 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadAction.java @@ -15,6 +15,7 @@ */ package com.google.android.exoplayer2.offline; +import android.support.annotation.Nullable; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; @@ -108,7 +109,7 @@ public abstract class DownloadAction { private final String data; /** @param data Optional custom data for this action. If null, an empty string is used. */ - protected DownloadAction(String data) { + protected DownloadAction(@Nullable String data) { this.data = data != null ? data : ""; } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloadAction.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloadAction.java index 7c9549cd3a..130e9c0dc7 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloadAction.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloadAction.java @@ -15,7 +15,9 @@ */ package com.google.android.exoplayer2.offline; +import android.net.Uri; import android.support.annotation.Nullable; +import com.google.android.exoplayer2.upstream.cache.CacheUtil; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; import java.io.DataInputStream; @@ -49,13 +51,13 @@ public final class ProgressiveDownloadAction extends DownloadAction { /** * @param uri Uri of the data to be downloaded. - * @param customCacheKey A custom key that uniquely identifies the original stream. Used for cache - * indexing. May be null. + * @param customCacheKey A custom key that uniquely identifies the original stream. If not null it + * is used for cache indexing. * @param removeAction Whether the data should be downloaded or removed. * @param data Optional custom data for this action. If null, an empty string is used. */ - public ProgressiveDownloadAction(String uri, @Nullable String customCacheKey, - boolean removeAction, String data) { + public ProgressiveDownloadAction( + String uri, @Nullable String customCacheKey, boolean removeAction, @Nullable String data) { super(data); this.uri = Assertions.checkNotNull(uri); this.customCacheKey = customCacheKey; @@ -94,9 +96,7 @@ public final class ProgressiveDownloadAction extends DownloadAction { if (!(other instanceof ProgressiveDownloadAction)) { return false; } - ProgressiveDownloadAction action = (ProgressiveDownloadAction) other; - return customCacheKey != null ? customCacheKey.equals(action.customCacheKey) - : uri.equals(action.uri); + return getCacheKey().equals(((ProgressiveDownloadAction) other).getCacheKey()); } @Override @@ -119,4 +119,7 @@ public final class ProgressiveDownloadAction extends DownloadAction { return result; } + private String getCacheKey() { + return customCacheKey != null ? customCacheKey : CacheUtil.generateKey(Uri.parse(uri)); + } } 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 6d689d53b3..6bc9a10c38 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 @@ -16,6 +16,7 @@ package com.google.android.exoplayer2.offline; import android.net.Uri; +import android.support.annotation.Nullable; import com.google.android.exoplayer2.util.Assertions; import java.io.DataInputStream; import java.io.DataOutputStream; @@ -72,15 +73,15 @@ public abstract class SegmentDownloadAction extends DownloadAction { * @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) { + protected SegmentDownloadAction( + Uri manifestUri, boolean removeAction, @Nullable String data, K[] keys) { super(data); - Assertions.checkNotNull(keys); + this.manifestUri = manifestUri; + this.keys = Assertions.checkNotNull(keys); + this.removeAction = removeAction; if (removeAction) { Assertions.checkArgument(keys.length == 0); } - this.manifestUri = manifestUri; - this.keys = keys; - this.removeAction = removeAction; } @Override diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/ProgressiveDownloadActionTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/ProgressiveDownloadActionTest.java index 1445d33845..e5b67a6da6 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/ProgressiveDownloadActionTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/ProgressiveDownloadActionTest.java @@ -61,28 +61,35 @@ public class ProgressiveDownloadActionTest { public void testSameUriCacheKeyDifferentAction_IsSameMedia() throws Exception { ProgressiveDownloadAction action1 = new ProgressiveDownloadAction("uri", null, true, null); ProgressiveDownloadAction action2 = new ProgressiveDownloadAction("uri", null, false, null); - assertThat(action1.isSameMedia(action2)).isTrue(); + assertSameMedia(action1, action2); } @Test public void testNullCacheKeyDifferentUriAction_IsNotSameMedia() throws Exception { ProgressiveDownloadAction action3 = new ProgressiveDownloadAction("uri2", null, true, null); ProgressiveDownloadAction action4 = new ProgressiveDownloadAction("uri", null, false, null); - assertThat(action3.isSameMedia(action4)).isFalse(); + assertNotSameMedia(action3, action4); } @Test public void testSameCacheKeyDifferentUriAction_IsSameMedia() throws Exception { ProgressiveDownloadAction action5 = new ProgressiveDownloadAction("uri2", "key", true, null); ProgressiveDownloadAction action6 = new ProgressiveDownloadAction("uri", "key", false, null); - assertThat(action5.isSameMedia(action6)).isTrue(); + assertSameMedia(action5, action6); } @Test public void testSameUriDifferentCacheKeyAction_IsNotSameMedia() throws Exception { ProgressiveDownloadAction action7 = new ProgressiveDownloadAction("uri", "key", true, null); ProgressiveDownloadAction action8 = new ProgressiveDownloadAction("uri", "key2", false, null); - assertThat(action7.isSameMedia(action8)).isFalse(); + assertNotSameMedia(action7, action8); + } + + @Test + public void testSameUriNullCacheKeyAction_IsNotSameMedia() throws Exception { + ProgressiveDownloadAction action1 = new ProgressiveDownloadAction("uri", "key", true, null); + ProgressiveDownloadAction action2 = new ProgressiveDownloadAction("uri", null, false, null); + assertNotSameMedia(action1, action2); } @Test @@ -123,6 +130,18 @@ public class ProgressiveDownloadActionTest { doTestSerializationRoundTrip(new ProgressiveDownloadAction("uri2", "key", true, null)); } + private void assertSameMedia( + ProgressiveDownloadAction action1, ProgressiveDownloadAction action2) { + assertThat(action1.isSameMedia(action2)).isTrue(); + assertThat(action2.isSameMedia(action1)).isTrue(); + } + + private void assertNotSameMedia( + ProgressiveDownloadAction action1, ProgressiveDownloadAction action2) { + assertThat(action1.isSameMedia(action2)).isFalse(); + assertThat(action2.isSameMedia(action1)).isFalse(); + } + private static void doTestSerializationRoundTrip(ProgressiveDownloadAction action1) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); diff --git a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/offline/DashDownloadAction.java b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/offline/DashDownloadAction.java index ed87a835cf..dea1277959 100644 --- a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/offline/DashDownloadAction.java +++ b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/offline/DashDownloadAction.java @@ -16,6 +16,7 @@ package com.google.android.exoplayer2.source.dash.offline; import android.net.Uri; +import android.support.annotation.Nullable; import com.google.android.exoplayer2.offline.DownloadAction; import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; import com.google.android.exoplayer2.offline.SegmentDownloadAction; @@ -56,8 +57,8 @@ public final class DashDownloadAction extends SegmentDownloadAction { private static final String TYPE = "HlsDownloadAction"; /** @see SegmentDownloadAction#SegmentDownloadAction(Uri, boolean, String, Object[]) */ - public HlsDownloadAction(Uri manifestUri, boolean removeAction, String data, String... keys) { + public HlsDownloadAction( + Uri manifestUri, boolean removeAction, @Nullable String data, String... keys) { super(manifestUri, removeAction, data, keys); } diff --git a/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloadAction.java b/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloadAction.java index 1aabe51237..84c0eb5c4a 100644 --- a/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloadAction.java +++ b/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloadAction.java @@ -16,6 +16,7 @@ package com.google.android.exoplayer2.source.smoothstreaming.offline; import android.net.Uri; +import android.support.annotation.Nullable; import com.google.android.exoplayer2.offline.DownloadAction; import com.google.android.exoplayer2.offline.DownloaderConstructorHelper; import com.google.android.exoplayer2.offline.SegmentDownloadAction; @@ -56,7 +57,8 @@ public final class SsDownloadAction extends SegmentDownloadAction { private static final String TYPE = "SsDownloadAction"; /** @see SegmentDownloadAction#SegmentDownloadAction(Uri, boolean, String, Object[]) */ - public SsDownloadAction(Uri manifestUri, boolean removeAction, String data, TrackKey... keys) { + public SsDownloadAction( + Uri manifestUri, boolean removeAction, @Nullable String data, TrackKey... keys) { super(manifestUri, removeAction, data, keys); }