mirror of
https://github.com/samsonjs/media.git
synced 2026-04-21 13:55:47 +00:00
Remove custom data from remove actions
A remove action will eventually just be the unique ID of the download that should be removed. This is a step toward that goal. PiperOrigin-RevId: 222832456
This commit is contained in:
parent
f4d9deddaf
commit
e317305909
11 changed files with 49 additions and 40 deletions
|
|
@ -116,8 +116,7 @@ public class DownloadTracker implements DownloadManager.Listener {
|
|||
|
||||
public void toggleDownload(Activity activity, String name, Uri uri, String extension) {
|
||||
if (isDownloaded(uri)) {
|
||||
DownloadAction removeAction =
|
||||
getDownloadHelper(uri, extension).getRemoveAction(Util.getUtf8Bytes(name));
|
||||
DownloadAction removeAction = getDownloadHelper(uri, extension).getRemoveAction();
|
||||
startServiceWithAction(removeAction);
|
||||
} else {
|
||||
StartDownloadDialogHelper helper =
|
||||
|
|
|
|||
|
|
@ -96,12 +96,16 @@ public final class DownloadAction {
|
|||
* @param type The type of the action.
|
||||
* @param uri The URI of the media to be removed.
|
||||
* @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 createRemoveAction(
|
||||
String type, Uri uri, @Nullable String customCacheKey, @Nullable byte[] data) {
|
||||
String type, Uri uri, @Nullable String customCacheKey) {
|
||||
return new DownloadAction(
|
||||
type, uri, /* isRemoveAction= */ true, Collections.emptyList(), customCacheKey, data);
|
||||
type,
|
||||
uri,
|
||||
/* isRemoveAction= */ true,
|
||||
Collections.emptyList(),
|
||||
customCacheKey,
|
||||
/* data= */ null);
|
||||
}
|
||||
|
||||
/** The type of the action. */
|
||||
|
|
@ -127,7 +131,7 @@ public final class DownloadAction {
|
|||
* @param keys Keys of tracks to be downloaded. If empty, all tracks will be downloaded. Empty if
|
||||
* this action is a remove action.
|
||||
* @param customCacheKey A custom key for cache indexing, or null.
|
||||
* @param data Optional custom data for this action.
|
||||
* @param data Custom data for this action. Null if this action is a remove action.
|
||||
*/
|
||||
private DownloadAction(
|
||||
String type,
|
||||
|
|
@ -140,14 +144,16 @@ public final class DownloadAction {
|
|||
this.uri = uri;
|
||||
this.isRemoveAction = isRemoveAction;
|
||||
this.customCacheKey = customCacheKey;
|
||||
this.data = data != null ? data : Util.EMPTY_BYTE_ARRAY;
|
||||
if (isRemoveAction) {
|
||||
Assertions.checkArgument(keys.isEmpty());
|
||||
Assertions.checkArgument(data == null);
|
||||
this.keys = Collections.emptyList();
|
||||
this.data = Util.EMPTY_BYTE_ARRAY;
|
||||
} else {
|
||||
ArrayList<StreamKey> mutableKeys = new ArrayList<>(keys);
|
||||
Collections.sort(mutableKeys);
|
||||
this.keys = Collections.unmodifiableList(mutableKeys);
|
||||
this.data = data != null ? data : Util.EMPTY_BYTE_ARRAY;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -236,9 +242,19 @@ public final class DownloadAction {
|
|||
|
||||
Uri uri = Uri.parse(input.readUTF());
|
||||
boolean isRemoveAction = input.readBoolean();
|
||||
|
||||
int dataLength = input.readInt();
|
||||
byte[] data = new byte[dataLength];
|
||||
input.readFully(data);
|
||||
byte[] data;
|
||||
if (dataLength != 0) {
|
||||
data = new byte[dataLength];
|
||||
input.readFully(data);
|
||||
if (isRemoveAction) {
|
||||
// Remove actions are no longer permitted to have data.
|
||||
data = null;
|
||||
}
|
||||
} else {
|
||||
data = null;
|
||||
}
|
||||
|
||||
// Serialized version 0 progressive actions did not contain keys.
|
||||
boolean isLegacyProgressive = version == 0 && TYPE_PROGRESSIVE.equals(type);
|
||||
|
|
|
|||
|
|
@ -103,8 +103,7 @@ public abstract class DownloadHelper {
|
|||
/**
|
||||
* Builds a {@link DownloadAction} for removing the media. May be called in any state.
|
||||
*
|
||||
* @param data Application provided data to store in {@link DownloadAction#data}.
|
||||
* @return The built {@link DownloadAction}.
|
||||
*/
|
||||
public abstract DownloadAction getRemoveAction(@Nullable byte[] data);
|
||||
public abstract DownloadAction getRemoveAction();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,8 +62,7 @@ public final class ProgressiveDownloadHelper extends DownloadHelper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public DownloadAction getRemoveAction(@Nullable byte[] data) {
|
||||
return DownloadAction.createRemoveAction(
|
||||
DownloadAction.TYPE_PROGRESSIVE, uri, customCacheKey, data);
|
||||
public DownloadAction getRemoveAction() {
|
||||
return DownloadAction.createRemoveAction(DownloadAction.TYPE_PROGRESSIVE, uri, customCacheKey);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -88,15 +88,15 @@ public class DownloadActionTest {
|
|||
|
||||
@Test
|
||||
public void testSameCacheKeyDifferentUri_IsSameMedia() {
|
||||
DownloadAction action1 = DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123", data);
|
||||
DownloadAction action2 = DownloadAction.createRemoveAction(TYPE_DASH, uri2, "key123", data);
|
||||
DownloadAction action1 = DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123");
|
||||
DownloadAction action2 = DownloadAction.createRemoveAction(TYPE_DASH, uri2, "key123");
|
||||
assertThat(action1.isSameMedia(action2)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDifferentCacheDifferentUri_IsNotSameMedia() {
|
||||
DownloadAction action1 = DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123", data);
|
||||
DownloadAction action2 = DownloadAction.createRemoveAction(TYPE_DASH, uri2, "key456", data);
|
||||
DownloadAction action1 = DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123");
|
||||
DownloadAction action2 = DownloadAction.createRemoveAction(TYPE_DASH, uri2, "key456");
|
||||
assertThat(action1.isSameMedia(action2)).isFalse();
|
||||
}
|
||||
|
||||
|
|
@ -152,7 +152,7 @@ public class DownloadActionTest {
|
|||
"key123",
|
||||
data));
|
||||
assertStreamSerializationRoundTrip(
|
||||
DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123", data));
|
||||
DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -164,8 +164,7 @@ public class DownloadActionTest {
|
|||
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
|
||||
"key123",
|
||||
data));
|
||||
assertArraySerializationRoundTrip(
|
||||
DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123", data));
|
||||
assertArraySerializationRoundTrip(DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -176,7 +175,7 @@ public class DownloadActionTest {
|
|||
TYPE_PROGRESSIVE, uri1, Collections.emptyList(), "key123", data));
|
||||
assertDeserialization(
|
||||
"progressive-remove-v0",
|
||||
DownloadAction.createRemoveAction(TYPE_PROGRESSIVE, uri1, "key123", data));
|
||||
DownloadAction.createRemoveAction(TYPE_PROGRESSIVE, uri1, "key123"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -191,7 +190,7 @@ public class DownloadActionTest {
|
|||
data));
|
||||
assertDeserialization(
|
||||
"dash-remove-v0",
|
||||
DownloadAction.createRemoveAction(TYPE_DASH, uri1, /* customCacheKey= */ null, data));
|
||||
DownloadAction.createRemoveAction(TYPE_DASH, uri1, /* customCacheKey= */ null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -206,7 +205,7 @@ public class DownloadActionTest {
|
|||
data));
|
||||
assertDeserialization(
|
||||
"hls-remove-v0",
|
||||
DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null, data));
|
||||
DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -221,7 +220,7 @@ public class DownloadActionTest {
|
|||
data));
|
||||
assertDeserialization(
|
||||
"hls-remove-v1",
|
||||
DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null, data));
|
||||
DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -236,7 +235,7 @@ public class DownloadActionTest {
|
|||
data));
|
||||
assertDeserialization(
|
||||
"ss-remove-v0",
|
||||
DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null, data));
|
||||
DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -251,7 +250,7 @@ public class DownloadActionTest {
|
|||
data));
|
||||
assertDeserialization(
|
||||
"ss-remove-v1",
|
||||
DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null, data));
|
||||
DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null));
|
||||
}
|
||||
|
||||
private DownloadAction createDownloadAction(Uri uri, StreamKey... keys) {
|
||||
|
|
@ -260,7 +259,7 @@ public class DownloadActionTest {
|
|||
}
|
||||
|
||||
private DownloadAction createRemoveAction(Uri uri) {
|
||||
return DownloadAction.createRemoveAction(TYPE_DASH, uri, /* customCacheKey= */ null, data);
|
||||
return DownloadAction.createRemoveAction(TYPE_DASH, uri, /* customCacheKey= */ null);
|
||||
}
|
||||
|
||||
private static void assertNotEqual(DownloadAction action1, DownloadAction action2) {
|
||||
|
|
|
|||
|
|
@ -466,10 +466,7 @@ public class DownloadManagerTest {
|
|||
action =
|
||||
isRemoveAction
|
||||
? DownloadAction.createRemoveAction(
|
||||
DownloadAction.TYPE_PROGRESSIVE,
|
||||
uri,
|
||||
/* customCacheKey= */ null,
|
||||
/* data= */ null)
|
||||
DownloadAction.TYPE_PROGRESSIVE, uri, /* customCacheKey= */ null)
|
||||
: DownloadAction.createDownloadAction(
|
||||
DownloadAction.TYPE_PROGRESSIVE,
|
||||
uri,
|
||||
|
|
|
|||
|
|
@ -93,9 +93,9 @@ public final class DashDownloadHelper extends DownloadHelper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public DownloadAction getRemoveAction(@Nullable byte[] data) {
|
||||
public DownloadAction getRemoveAction() {
|
||||
return DownloadAction.createRemoveAction(
|
||||
DownloadAction.TYPE_DASH, uri, /* customCacheKey= */ null, data);
|
||||
DownloadAction.TYPE_DASH, uri, /* customCacheKey= */ null);
|
||||
}
|
||||
|
||||
private static List<StreamKey> toStreamKeys(List<TrackKey> trackKeys) {
|
||||
|
|
|
|||
|
|
@ -260,7 +260,7 @@ public class DownloadManagerDashTest {
|
|||
if (isRemoveAction) {
|
||||
result =
|
||||
DownloadAction.createRemoveAction(
|
||||
DownloadAction.TYPE_DASH, uri, /* customCacheKey= */ null, data);
|
||||
DownloadAction.TYPE_DASH, uri, /* customCacheKey= */ null);
|
||||
} else {
|
||||
result =
|
||||
DownloadAction.createDownloadAction(
|
||||
|
|
|
|||
|
|
@ -214,7 +214,7 @@ public class DownloadServiceDashTest {
|
|||
if (isRemoveAction) {
|
||||
result =
|
||||
DownloadAction.createRemoveAction(
|
||||
DownloadAction.TYPE_DASH, uri, /* customCacheKey= */ null, data);
|
||||
DownloadAction.TYPE_DASH, uri, /* customCacheKey= */ null);
|
||||
} else {
|
||||
result =
|
||||
DownloadAction.createDownloadAction(
|
||||
|
|
|
|||
|
|
@ -109,9 +109,9 @@ public final class HlsDownloadHelper extends DownloadHelper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public DownloadAction getRemoveAction(@Nullable byte[] data) {
|
||||
public DownloadAction getRemoveAction() {
|
||||
return DownloadAction.createRemoveAction(
|
||||
DownloadAction.TYPE_HLS, uri, /* customCacheKey= */ null, data);
|
||||
DownloadAction.TYPE_HLS, uri, /* customCacheKey= */ null);
|
||||
}
|
||||
|
||||
private static Format[] toFormats(List<HlsMasterPlaylist.HlsUrl> hlsUrls) {
|
||||
|
|
|
|||
|
|
@ -83,9 +83,9 @@ public final class SsDownloadHelper extends DownloadHelper {
|
|||
}
|
||||
|
||||
@Override
|
||||
public DownloadAction getRemoveAction(@Nullable byte[] data) {
|
||||
public DownloadAction getRemoveAction() {
|
||||
return DownloadAction.createRemoveAction(
|
||||
DownloadAction.TYPE_SS, uri, /* customCacheKey= */ null, data);
|
||||
DownloadAction.TYPE_SS, uri, /* customCacheKey= */ null);
|
||||
}
|
||||
|
||||
private static List<StreamKey> toStreamKeys(List<TrackKey> trackKeys) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue