mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +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) {
|
public void toggleDownload(Activity activity, String name, Uri uri, String extension) {
|
||||||
if (isDownloaded(uri)) {
|
if (isDownloaded(uri)) {
|
||||||
DownloadAction removeAction =
|
DownloadAction removeAction = getDownloadHelper(uri, extension).getRemoveAction();
|
||||||
getDownloadHelper(uri, extension).getRemoveAction(Util.getUtf8Bytes(name));
|
|
||||||
startServiceWithAction(removeAction);
|
startServiceWithAction(removeAction);
|
||||||
} else {
|
} else {
|
||||||
StartDownloadDialogHelper helper =
|
StartDownloadDialogHelper helper =
|
||||||
|
|
|
||||||
|
|
@ -96,12 +96,16 @@ public final class DownloadAction {
|
||||||
* @param type The type of the action.
|
* @param type The type of the action.
|
||||||
* @param uri The URI of the media to be removed.
|
* @param uri The URI of the media to be removed.
|
||||||
* @param customCacheKey A custom key for cache indexing, or null.
|
* @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(
|
public static DownloadAction createRemoveAction(
|
||||||
String type, Uri uri, @Nullable String customCacheKey, @Nullable byte[] data) {
|
String type, Uri uri, @Nullable String customCacheKey) {
|
||||||
return new DownloadAction(
|
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. */
|
/** 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
|
* @param keys Keys of tracks to be downloaded. If empty, all tracks will be downloaded. Empty if
|
||||||
* this action is a remove action.
|
* this action is a remove action.
|
||||||
* @param customCacheKey A custom key for cache indexing, or null.
|
* @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(
|
private DownloadAction(
|
||||||
String type,
|
String type,
|
||||||
|
|
@ -140,14 +144,16 @@ public final class DownloadAction {
|
||||||
this.uri = uri;
|
this.uri = uri;
|
||||||
this.isRemoveAction = isRemoveAction;
|
this.isRemoveAction = isRemoveAction;
|
||||||
this.customCacheKey = customCacheKey;
|
this.customCacheKey = customCacheKey;
|
||||||
this.data = data != null ? data : Util.EMPTY_BYTE_ARRAY;
|
|
||||||
if (isRemoveAction) {
|
if (isRemoveAction) {
|
||||||
Assertions.checkArgument(keys.isEmpty());
|
Assertions.checkArgument(keys.isEmpty());
|
||||||
|
Assertions.checkArgument(data == null);
|
||||||
this.keys = Collections.emptyList();
|
this.keys = Collections.emptyList();
|
||||||
|
this.data = Util.EMPTY_BYTE_ARRAY;
|
||||||
} else {
|
} else {
|
||||||
ArrayList<StreamKey> mutableKeys = new ArrayList<>(keys);
|
ArrayList<StreamKey> mutableKeys = new ArrayList<>(keys);
|
||||||
Collections.sort(mutableKeys);
|
Collections.sort(mutableKeys);
|
||||||
this.keys = Collections.unmodifiableList(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());
|
Uri uri = Uri.parse(input.readUTF());
|
||||||
boolean isRemoveAction = input.readBoolean();
|
boolean isRemoveAction = input.readBoolean();
|
||||||
|
|
||||||
int dataLength = input.readInt();
|
int dataLength = input.readInt();
|
||||||
byte[] data = new byte[dataLength];
|
byte[] data;
|
||||||
|
if (dataLength != 0) {
|
||||||
|
data = new byte[dataLength];
|
||||||
input.readFully(data);
|
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.
|
// Serialized version 0 progressive actions did not contain keys.
|
||||||
boolean isLegacyProgressive = version == 0 && TYPE_PROGRESSIVE.equals(type);
|
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.
|
* 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}.
|
* @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
|
@Override
|
||||||
public DownloadAction getRemoveAction(@Nullable byte[] data) {
|
public DownloadAction getRemoveAction() {
|
||||||
return DownloadAction.createRemoveAction(
|
return DownloadAction.createRemoveAction(DownloadAction.TYPE_PROGRESSIVE, uri, customCacheKey);
|
||||||
DownloadAction.TYPE_PROGRESSIVE, uri, customCacheKey, data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,15 +88,15 @@ public class DownloadActionTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSameCacheKeyDifferentUri_IsSameMedia() {
|
public void testSameCacheKeyDifferentUri_IsSameMedia() {
|
||||||
DownloadAction action1 = DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123", data);
|
DownloadAction action1 = DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123");
|
||||||
DownloadAction action2 = DownloadAction.createRemoveAction(TYPE_DASH, uri2, "key123", data);
|
DownloadAction action2 = DownloadAction.createRemoveAction(TYPE_DASH, uri2, "key123");
|
||||||
assertThat(action1.isSameMedia(action2)).isTrue();
|
assertThat(action1.isSameMedia(action2)).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDifferentCacheDifferentUri_IsNotSameMedia() {
|
public void testDifferentCacheDifferentUri_IsNotSameMedia() {
|
||||||
DownloadAction action1 = DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123", data);
|
DownloadAction action1 = DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123");
|
||||||
DownloadAction action2 = DownloadAction.createRemoveAction(TYPE_DASH, uri2, "key456", data);
|
DownloadAction action2 = DownloadAction.createRemoveAction(TYPE_DASH, uri2, "key456");
|
||||||
assertThat(action1.isSameMedia(action2)).isFalse();
|
assertThat(action1.isSameMedia(action2)).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,7 +152,7 @@ public class DownloadActionTest {
|
||||||
"key123",
|
"key123",
|
||||||
data));
|
data));
|
||||||
assertStreamSerializationRoundTrip(
|
assertStreamSerializationRoundTrip(
|
||||||
DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123", data));
|
DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -164,8 +164,7 @@ public class DownloadActionTest {
|
||||||
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
|
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
|
||||||
"key123",
|
"key123",
|
||||||
data));
|
data));
|
||||||
assertArraySerializationRoundTrip(
|
assertArraySerializationRoundTrip(DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123"));
|
||||||
DownloadAction.createRemoveAction(TYPE_DASH, uri1, "key123", data));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -176,7 +175,7 @@ public class DownloadActionTest {
|
||||||
TYPE_PROGRESSIVE, uri1, Collections.emptyList(), "key123", data));
|
TYPE_PROGRESSIVE, uri1, Collections.emptyList(), "key123", data));
|
||||||
assertDeserialization(
|
assertDeserialization(
|
||||||
"progressive-remove-v0",
|
"progressive-remove-v0",
|
||||||
DownloadAction.createRemoveAction(TYPE_PROGRESSIVE, uri1, "key123", data));
|
DownloadAction.createRemoveAction(TYPE_PROGRESSIVE, uri1, "key123"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -191,7 +190,7 @@ public class DownloadActionTest {
|
||||||
data));
|
data));
|
||||||
assertDeserialization(
|
assertDeserialization(
|
||||||
"dash-remove-v0",
|
"dash-remove-v0",
|
||||||
DownloadAction.createRemoveAction(TYPE_DASH, uri1, /* customCacheKey= */ null, data));
|
DownloadAction.createRemoveAction(TYPE_DASH, uri1, /* customCacheKey= */ null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -206,7 +205,7 @@ public class DownloadActionTest {
|
||||||
data));
|
data));
|
||||||
assertDeserialization(
|
assertDeserialization(
|
||||||
"hls-remove-v0",
|
"hls-remove-v0",
|
||||||
DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null, data));
|
DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -221,7 +220,7 @@ public class DownloadActionTest {
|
||||||
data));
|
data));
|
||||||
assertDeserialization(
|
assertDeserialization(
|
||||||
"hls-remove-v1",
|
"hls-remove-v1",
|
||||||
DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null, data));
|
DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -236,7 +235,7 @@ public class DownloadActionTest {
|
||||||
data));
|
data));
|
||||||
assertDeserialization(
|
assertDeserialization(
|
||||||
"ss-remove-v0",
|
"ss-remove-v0",
|
||||||
DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null, data));
|
DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -251,7 +250,7 @@ public class DownloadActionTest {
|
||||||
data));
|
data));
|
||||||
assertDeserialization(
|
assertDeserialization(
|
||||||
"ss-remove-v1",
|
"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) {
|
private DownloadAction createDownloadAction(Uri uri, StreamKey... keys) {
|
||||||
|
|
@ -260,7 +259,7 @@ public class DownloadActionTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private DownloadAction createRemoveAction(Uri uri) {
|
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) {
|
private static void assertNotEqual(DownloadAction action1, DownloadAction action2) {
|
||||||
|
|
|
||||||
|
|
@ -466,10 +466,7 @@ public class DownloadManagerTest {
|
||||||
action =
|
action =
|
||||||
isRemoveAction
|
isRemoveAction
|
||||||
? DownloadAction.createRemoveAction(
|
? DownloadAction.createRemoveAction(
|
||||||
DownloadAction.TYPE_PROGRESSIVE,
|
DownloadAction.TYPE_PROGRESSIVE, uri, /* customCacheKey= */ null)
|
||||||
uri,
|
|
||||||
/* customCacheKey= */ null,
|
|
||||||
/* data= */ null)
|
|
||||||
: DownloadAction.createDownloadAction(
|
: DownloadAction.createDownloadAction(
|
||||||
DownloadAction.TYPE_PROGRESSIVE,
|
DownloadAction.TYPE_PROGRESSIVE,
|
||||||
uri,
|
uri,
|
||||||
|
|
|
||||||
|
|
@ -93,9 +93,9 @@ public final class DashDownloadHelper extends DownloadHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DownloadAction getRemoveAction(@Nullable byte[] data) {
|
public DownloadAction getRemoveAction() {
|
||||||
return DownloadAction.createRemoveAction(
|
return DownloadAction.createRemoveAction(
|
||||||
DownloadAction.TYPE_DASH, uri, /* customCacheKey= */ null, data);
|
DownloadAction.TYPE_DASH, uri, /* customCacheKey= */ null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<StreamKey> toStreamKeys(List<TrackKey> trackKeys) {
|
private static List<StreamKey> toStreamKeys(List<TrackKey> trackKeys) {
|
||||||
|
|
|
||||||
|
|
@ -260,7 +260,7 @@ public class DownloadManagerDashTest {
|
||||||
if (isRemoveAction) {
|
if (isRemoveAction) {
|
||||||
result =
|
result =
|
||||||
DownloadAction.createRemoveAction(
|
DownloadAction.createRemoveAction(
|
||||||
DownloadAction.TYPE_DASH, uri, /* customCacheKey= */ null, data);
|
DownloadAction.TYPE_DASH, uri, /* customCacheKey= */ null);
|
||||||
} else {
|
} else {
|
||||||
result =
|
result =
|
||||||
DownloadAction.createDownloadAction(
|
DownloadAction.createDownloadAction(
|
||||||
|
|
|
||||||
|
|
@ -214,7 +214,7 @@ public class DownloadServiceDashTest {
|
||||||
if (isRemoveAction) {
|
if (isRemoveAction) {
|
||||||
result =
|
result =
|
||||||
DownloadAction.createRemoveAction(
|
DownloadAction.createRemoveAction(
|
||||||
DownloadAction.TYPE_DASH, uri, /* customCacheKey= */ null, data);
|
DownloadAction.TYPE_DASH, uri, /* customCacheKey= */ null);
|
||||||
} else {
|
} else {
|
||||||
result =
|
result =
|
||||||
DownloadAction.createDownloadAction(
|
DownloadAction.createDownloadAction(
|
||||||
|
|
|
||||||
|
|
@ -109,9 +109,9 @@ public final class HlsDownloadHelper extends DownloadHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DownloadAction getRemoveAction(@Nullable byte[] data) {
|
public DownloadAction getRemoveAction() {
|
||||||
return DownloadAction.createRemoveAction(
|
return DownloadAction.createRemoveAction(
|
||||||
DownloadAction.TYPE_HLS, uri, /* customCacheKey= */ null, data);
|
DownloadAction.TYPE_HLS, uri, /* customCacheKey= */ null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Format[] toFormats(List<HlsMasterPlaylist.HlsUrl> hlsUrls) {
|
private static Format[] toFormats(List<HlsMasterPlaylist.HlsUrl> hlsUrls) {
|
||||||
|
|
|
||||||
|
|
@ -83,9 +83,9 @@ public final class SsDownloadHelper extends DownloadHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DownloadAction getRemoveAction(@Nullable byte[] data) {
|
public DownloadAction getRemoveAction() {
|
||||||
return DownloadAction.createRemoveAction(
|
return DownloadAction.createRemoveAction(
|
||||||
DownloadAction.TYPE_SS, uri, /* customCacheKey= */ null, data);
|
DownloadAction.TYPE_SS, uri, /* customCacheKey= */ null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<StreamKey> toStreamKeys(List<TrackKey> trackKeys) {
|
private static List<StreamKey> toStreamKeys(List<TrackKey> trackKeys) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue