Commit 47601980 by olly Committed by Oliver Woodman

Use DownloadAction.id properly

- Fix ID being dropped when DownloadAction is serialized and
  de-serialized as it's passed to DownloadService
- Properly set DownloadAction.id when building an action from
  a DownloadState
- Make ID a required constructor argument. Else it's too easy
  to not propagate it by accident.

PiperOrigin-RevId: 242457831
parent 401e20d9
...@@ -45,7 +45,7 @@ public final class DownloadAction { ...@@ -45,7 +45,7 @@ public final class DownloadAction {
/** Type for SmoothStreaming downloads. */ /** Type for SmoothStreaming downloads. */
public static final String TYPE_SS = "ss"; public static final String TYPE_SS = "ss";
private static final int VERSION = 2; private static final int VERSION = 3;
/** /**
* Deserializes an action from the {@code data}. * Deserializes an action from the {@code data}.
...@@ -81,25 +81,6 @@ public final class DownloadAction { ...@@ -81,25 +81,6 @@ public final class DownloadAction {
/** /**
* Creates a DASH download action. * Creates a DASH download action.
* *
* @param type The type of the action.
* @param uri The URI of the media to be downloaded.
* @param keys Keys of streams to be downloaded. If empty, all streams will be downloaded.
* @param customCacheKey A custom key for cache indexing, or null.
* @param data Optional custom data for this action. If {@code null} an empty array will be used.
*/
public static DownloadAction createDownloadAction(
String type,
Uri uri,
List<StreamKey> keys,
@Nullable String customCacheKey,
@Nullable byte[] data) {
return createDownloadAction(
generateId(uri, customCacheKey), type, uri, keys, customCacheKey, data);
}
/**
* Creates a DASH download action.
*
* @param id The content id. * @param id The content id.
* @param type The type of the action. * @param type The type of the action.
* @param uri The URI of the media to be downloaded. * @param uri The URI of the media to be downloaded.
...@@ -224,6 +205,7 @@ public final class DownloadAction { ...@@ -224,6 +205,7 @@ public final class DownloadAction {
if (customCacheKey != null) { if (customCacheKey != null) {
dataOutputStream.writeUTF(customCacheKey); dataOutputStream.writeUTF(customCacheKey);
} }
dataOutputStream.writeUTF(id);
dataOutputStream.flush(); dataOutputStream.flush();
} }
...@@ -261,15 +243,17 @@ public final class DownloadAction { ...@@ -261,15 +243,17 @@ public final class DownloadAction {
customCacheKey = input.readBoolean() ? input.readUTF() : null; customCacheKey = input.readBoolean() ? input.readUTF() : null;
} }
// Serialized version 0, 1 and 2 did not contain an id.
String id = version < 3 ? generateId(uri, customCacheKey) : input.readUTF();
if (isRemoveAction) { if (isRemoveAction) {
// Remove actions are not supported anymore. // Remove actions are not supported anymore.
throw new UnsupportedActionException(); throw new UnsupportedActionException();
} }
return new DownloadAction( return new DownloadAction(id, type, uri, keys, customCacheKey, data);
generateId(uri, customCacheKey), type, uri, keys, customCacheKey, data);
} }
private static String generateId(Uri uri, @Nullable String customCacheKey) { /* package */ static String generateId(Uri uri, @Nullable String customCacheKey) {
return customCacheKey != null ? customCacheKey : uri.toString(); return customCacheKey != null ? customCacheKey : uri.toString();
} }
......
...@@ -584,9 +584,10 @@ public final class DownloadHelper { ...@@ -584,9 +584,10 @@ public final class DownloadHelper {
* @return The built {@link DownloadAction}. * @return The built {@link DownloadAction}.
*/ */
public DownloadAction getDownloadAction(@Nullable byte[] data) { public DownloadAction getDownloadAction(@Nullable byte[] data) {
String downloadId = uri.toString();
if (mediaSource == null) { if (mediaSource == null) {
return DownloadAction.createDownloadAction( return DownloadAction.createDownloadAction(
downloadType, uri, /* keys= */ Collections.emptyList(), cacheKey, data); downloadId, downloadType, uri, /* keys= */ Collections.emptyList(), cacheKey, data);
} }
assertPreparedWithMedia(); assertPreparedWithMedia();
List<StreamKey> streamKeys = new ArrayList<>(); List<StreamKey> streamKeys = new ArrayList<>();
...@@ -600,7 +601,8 @@ public final class DownloadHelper { ...@@ -600,7 +601,8 @@ public final class DownloadHelper {
} }
streamKeys.addAll(mediaPreparer.mediaPeriods[periodIndex].getStreamKeys(allSelections)); streamKeys.addAll(mediaPreparer.mediaPeriods[periodIndex].getStreamKeys(allSelections));
} }
return DownloadAction.createDownloadAction(downloadType, uri, streamKeys, cacheKey, data); return DownloadAction.createDownloadAction(
downloadId, downloadType, uri, streamKeys, cacheKey, data);
} }
// Initialization of array of Lists. // Initialization of array of Lists.
......
...@@ -886,6 +886,7 @@ public final class DownloadManager { ...@@ -886,6 +886,7 @@ public final class DownloadManager {
public DownloadAction getAction() { public DownloadAction getAction() {
return DownloadAction.createDownloadAction( return DownloadAction.createDownloadAction(
downloadState.id,
downloadState.type, downloadState.type,
downloadState.uri, downloadState.uri,
Arrays.asList(downloadState.streamKeys), Arrays.asList(downloadState.streamKeys),
......
...@@ -171,6 +171,6 @@ public class ActionFileTest { ...@@ -171,6 +171,6 @@ public class ActionFileTest {
private static DownloadAction buildAction(String type, Uri uri, byte[] data) { private static DownloadAction buildAction(String type, Uri uri, byte[] data) {
return DownloadAction.createDownloadAction( return DownloadAction.createDownloadAction(
type, uri, /* keys= */ Collections.emptyList(), /* customCacheKey= */ null, data); "id", type, uri, /* keys= */ Collections.emptyList(), /* customCacheKey= */ null, data);
} }
} }
...@@ -39,6 +39,7 @@ public final class DefaultDownloaderFactoryTest { ...@@ -39,6 +39,7 @@ public final class DefaultDownloaderFactoryTest {
Downloader downloader = Downloader downloader =
factory.createDownloader( factory.createDownloader(
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id",
DownloadAction.TYPE_PROGRESSIVE, DownloadAction.TYPE_PROGRESSIVE,
Uri.parse("https://www.test.com/download"), Uri.parse("https://www.test.com/download"),
/* keys= */ Collections.emptyList(), /* keys= */ Collections.emptyList(),
......
...@@ -84,13 +84,6 @@ public class DownloadActionTest { ...@@ -84,13 +84,6 @@ public class DownloadActionTest {
assertThat(action1.id.equals(action2.id)).isTrue(); assertThat(action1.id.equals(action2.id)).isTrue();
} }
@Test
public void testDifferentCacheKeyDifferentUri_hasDifferentId() {
DownloadAction action1 = createAction(uri1, "key123");
DownloadAction action2 = createAction(uri2, "key456");
assertThat(action1.id.equals(action2.id)).isFalse();
}
@SuppressWarnings("EqualsWithItself") @SuppressWarnings("EqualsWithItself")
@Test @Test
public void testEquals() { public void testEquals() {
...@@ -130,6 +123,7 @@ public class DownloadActionTest { ...@@ -130,6 +123,7 @@ public class DownloadActionTest {
public void testSerializerWriteRead() throws Exception { public void testSerializerWriteRead() throws Exception {
assertStreamSerializationRoundTrip( assertStreamSerializationRoundTrip(
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id",
TYPE_DASH, TYPE_DASH,
uri1, uri1,
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)), toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
...@@ -142,6 +136,7 @@ public class DownloadActionTest { ...@@ -142,6 +136,7 @@ public class DownloadActionTest {
public void testArraySerialization() throws Exception { public void testArraySerialization() throws Exception {
assertArraySerializationRoundTrip( assertArraySerializationRoundTrip(
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id",
TYPE_DASH, TYPE_DASH,
uri1, uri1,
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)), toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
...@@ -152,10 +147,12 @@ public class DownloadActionTest { ...@@ -152,10 +147,12 @@ public class DownloadActionTest {
@Test @Test
public void testSerializerProgressiveVersion0() throws Exception { public void testSerializerProgressiveVersion0() throws Exception {
String customCacheKey = "key123";
String expectedId = DownloadAction.generateId(uri1, customCacheKey);
assertDeserialization( assertDeserialization(
"progressive-download-v0", "progressive-download-v0",
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
TYPE_PROGRESSIVE, uri1, Collections.emptyList(), "key123", data)); expectedId, TYPE_PROGRESSIVE, uri1, Collections.emptyList(), customCacheKey, data));
assertUnsupportedAction("progressive-remove-v0"); assertUnsupportedAction("progressive-remove-v0");
} }
...@@ -164,6 +161,7 @@ public class DownloadActionTest { ...@@ -164,6 +161,7 @@ public class DownloadActionTest {
assertDeserialization( assertDeserialization(
"dash-download-v0", "dash-download-v0",
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
uri1.toString(),
TYPE_DASH, TYPE_DASH,
uri1, uri1,
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)), toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
...@@ -177,6 +175,7 @@ public class DownloadActionTest { ...@@ -177,6 +175,7 @@ public class DownloadActionTest {
assertDeserialization( assertDeserialization(
"hls-download-v0", "hls-download-v0",
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
uri1.toString(),
TYPE_HLS, TYPE_HLS,
uri1, uri1,
toList(new StreamKey(0, 1), new StreamKey(2, 3)), toList(new StreamKey(0, 1), new StreamKey(2, 3)),
...@@ -190,6 +189,7 @@ public class DownloadActionTest { ...@@ -190,6 +189,7 @@ public class DownloadActionTest {
assertDeserialization( assertDeserialization(
"hls-download-v1", "hls-download-v1",
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
uri1.toString(),
TYPE_HLS, TYPE_HLS,
uri1, uri1,
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)), toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
...@@ -203,6 +203,7 @@ public class DownloadActionTest { ...@@ -203,6 +203,7 @@ public class DownloadActionTest {
assertDeserialization( assertDeserialization(
"ss-download-v0", "ss-download-v0",
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
uri1.toString(),
TYPE_SS, TYPE_SS,
uri1, uri1,
toList(new StreamKey(0, 1), new StreamKey(2, 3)), toList(new StreamKey(0, 1), new StreamKey(2, 3)),
...@@ -216,6 +217,7 @@ public class DownloadActionTest { ...@@ -216,6 +217,7 @@ public class DownloadActionTest {
assertDeserialization( assertDeserialization(
"ss-download-v1", "ss-download-v1",
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
uri1.toString(),
TYPE_SS, TYPE_SS,
uri1, uri1,
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)), toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
...@@ -226,12 +228,17 @@ public class DownloadActionTest { ...@@ -226,12 +228,17 @@ public class DownloadActionTest {
private DownloadAction createAction(Uri uri, StreamKey... keys) { private DownloadAction createAction(Uri uri, StreamKey... keys) {
return DownloadAction.createDownloadAction( return DownloadAction.createDownloadAction(
TYPE_DASH, uri, toList(keys), /* customCacheKey= */ null, data); uri.toString(), TYPE_DASH, uri, toList(keys), /* customCacheKey= */ null, data);
} }
private DownloadAction createAction(Uri uri, @Nullable String customCacheKey) { private DownloadAction createAction(Uri uri, @Nullable String customCacheKey) {
return DownloadAction.createDownloadAction( return DownloadAction.createDownloadAction(
DownloadAction.TYPE_DASH, uri, Collections.emptyList(), customCacheKey, /* data= */ null); "id",
DownloadAction.TYPE_DASH,
uri,
Collections.emptyList(),
customCacheKey,
/* data= */ null);
} }
private static void assertNotEqual(DownloadAction action1, DownloadAction action2) { private static void assertNotEqual(DownloadAction action1, DownloadAction action2) {
......
...@@ -58,6 +58,7 @@ public class DownloadIndexUtilTest { ...@@ -58,6 +58,7 @@ public class DownloadIndexUtilTest {
byte[] data = new byte[] {1, 2, 3, 4}; byte[] data = new byte[] {1, 2, 3, 4};
DownloadAction action = DownloadAction action =
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id",
TYPE_DASH, TYPE_DASH,
Uri.parse("https://www.test.com/download"), Uri.parse("https://www.test.com/download"),
asList( asList(
...@@ -79,6 +80,7 @@ public class DownloadIndexUtilTest { ...@@ -79,6 +80,7 @@ public class DownloadIndexUtilTest {
new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2); new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2);
DownloadAction action1 = DownloadAction action1 =
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id",
TYPE_DASH, TYPE_DASH,
Uri.parse("https://www.test.com/download1"), Uri.parse("https://www.test.com/download1"),
asList(streamKey1), asList(streamKey1),
...@@ -86,6 +88,7 @@ public class DownloadIndexUtilTest { ...@@ -86,6 +88,7 @@ public class DownloadIndexUtilTest {
new byte[] {1, 2, 3, 4}); new byte[] {1, 2, 3, 4});
DownloadAction action2 = DownloadAction action2 =
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id",
TYPE_DASH, TYPE_DASH,
Uri.parse("https://www.test.com/download2"), Uri.parse("https://www.test.com/download2"),
asList(streamKey2), asList(streamKey2),
...@@ -114,6 +117,7 @@ public class DownloadIndexUtilTest { ...@@ -114,6 +117,7 @@ public class DownloadIndexUtilTest {
new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2); new StreamKey(/* periodIndex= */ 0, /* groupIndex= */ 1, /* trackIndex= */ 2);
DownloadAction action1 = DownloadAction action1 =
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id1",
TYPE_DASH, TYPE_DASH,
Uri.parse("https://www.test.com/download1"), Uri.parse("https://www.test.com/download1"),
asList(streamKey1), asList(streamKey1),
...@@ -121,6 +125,7 @@ public class DownloadIndexUtilTest { ...@@ -121,6 +125,7 @@ public class DownloadIndexUtilTest {
new byte[] {1, 2, 3, 4}); new byte[] {1, 2, 3, 4});
DownloadAction action2 = DownloadAction action2 =
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id2",
TYPE_DASH, TYPE_DASH,
Uri.parse("https://www.test.com/download2"), Uri.parse("https://www.test.com/download2"),
asList(streamKey2), asList(streamKey2),
......
...@@ -100,6 +100,7 @@ public class DownloadStateTest { ...@@ -100,6 +100,7 @@ public class DownloadStateTest {
public void mergeAction_actionHaveDifferentData_downloadStateDataIsUpdated() { public void mergeAction_actionHaveDifferentData_downloadStateDataIsUpdated() {
DownloadAction downloadAction = DownloadAction downloadAction =
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id",
DownloadAction.TYPE_DASH, DownloadAction.TYPE_DASH,
testUri, testUri,
Collections.emptyList(), Collections.emptyList(),
...@@ -246,6 +247,7 @@ public class DownloadStateTest { ...@@ -246,6 +247,7 @@ public class DownloadStateTest {
StreamKey[] keys1, StreamKey[] keys2, StreamKey[] expectedKeys) { StreamKey[] keys1, StreamKey[] keys2, StreamKey[] expectedKeys) {
DownloadAction downloadAction = DownloadAction downloadAction =
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id",
DownloadAction.TYPE_DASH, DownloadAction.TYPE_DASH,
testUri, testUri,
Arrays.asList(keys2), Arrays.asList(keys2),
...@@ -290,6 +292,7 @@ public class DownloadStateTest { ...@@ -290,6 +292,7 @@ public class DownloadStateTest {
private DownloadAction createDownloadAction() { private DownloadAction createDownloadAction() {
return DownloadAction.createDownloadAction( return DownloadAction.createDownloadAction(
"id",
DownloadAction.TYPE_DASH, DownloadAction.TYPE_DASH,
testUri, testUri,
Collections.emptyList(), Collections.emptyList(),
......
...@@ -85,6 +85,7 @@ public class DashDownloaderTest { ...@@ -85,6 +85,7 @@ public class DashDownloaderTest {
Downloader downloader = Downloader downloader =
factory.createDownloader( factory.createDownloader(
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id",
DownloadAction.TYPE_DASH, DownloadAction.TYPE_DASH,
Uri.parse("https://www.test.com/download"), Uri.parse("https://www.test.com/download"),
Collections.singletonList(new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)), Collections.singletonList(new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)),
......
...@@ -233,7 +233,12 @@ public class DownloadManagerDashTest { ...@@ -233,7 +233,12 @@ public class DownloadManagerDashTest {
Collections.addAll(keysList, keys); Collections.addAll(keysList, keys);
DownloadAction action = DownloadAction action =
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
DownloadAction.TYPE_DASH, TEST_MPD_URI, keysList, /* customCacheKey= */ null, null); TEST_ID,
DownloadAction.TYPE_DASH,
TEST_MPD_URI,
keysList,
/* customCacheKey= */ null,
null);
downloadManager.addDownload(action); downloadManager.addDownload(action);
} }
......
...@@ -209,7 +209,12 @@ public class DownloadServiceDashTest { ...@@ -209,7 +209,12 @@ public class DownloadServiceDashTest {
Collections.addAll(keysList, keys); Collections.addAll(keysList, keys);
DownloadAction action = DownloadAction action =
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
DownloadAction.TYPE_DASH, TEST_MPD_URI, keysList, /* customCacheKey= */ null, null); TEST_ID,
DownloadAction.TYPE_DASH,
TEST_MPD_URI,
keysList,
/* customCacheKey= */ null,
null);
dummyMainThread.runOnMainThread( dummyMainThread.runOnMainThread(
() -> { () -> {
Intent startIntent = Intent startIntent =
......
...@@ -102,6 +102,7 @@ public class HlsDownloaderTest { ...@@ -102,6 +102,7 @@ public class HlsDownloaderTest {
Downloader downloader = Downloader downloader =
factory.createDownloader( factory.createDownloader(
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id",
DownloadAction.TYPE_HLS, DownloadAction.TYPE_HLS,
Uri.parse("https://www.test.com/download"), Uri.parse("https://www.test.com/download"),
Collections.singletonList(new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)), Collections.singletonList(new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)),
......
...@@ -45,6 +45,7 @@ public final class SsDownloaderTest { ...@@ -45,6 +45,7 @@ public final class SsDownloaderTest {
Downloader downloader = Downloader downloader =
factory.createDownloader( factory.createDownloader(
DownloadAction.createDownloadAction( DownloadAction.createDownloadAction(
"id",
DownloadAction.TYPE_SS, DownloadAction.TYPE_SS,
Uri.parse("https://www.test.com/download"), Uri.parse("https://www.test.com/download"),
Collections.singletonList(new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)), Collections.singletonList(new StreamKey(/* groupIndex= */ 0, /* trackIndex= */ 0)),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment