Skip to content

Commit

Permalink
Pause/resume individual download
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksanderMielczarek committed Nov 29, 2018
1 parent f45943b commit 09e45d5
Show file tree
Hide file tree
Showing 3 changed files with 244 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public final class DownloadAction {
/** Type for SmoothStreaming downloads. */
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}.
Expand Down Expand Up @@ -87,7 +87,7 @@ public static DownloadAction createDownloadAction(
List<StreamKey> keys,
@Nullable String customCacheKey,
@Nullable byte[] data) {
return new DownloadAction(type, uri, /* isRemoveAction= */ false, keys, customCacheKey, data);
return new DownloadAction(type, uri, /* isRemoveAction= */ false, false, keys, customCacheKey, data);
}

/**
Expand All @@ -103,6 +103,7 @@ public static DownloadAction createRemoveAction(
type,
uri,
/* isRemoveAction= */ true,
false,
Collections.emptyList(),
customCacheKey,
/* data= */ null);
Expand All @@ -114,6 +115,8 @@ public static DownloadAction createRemoveAction(
public final Uri uri;
/** Whether this is a remove action. If false, this is a download action. */
public final boolean isRemoveAction;
/** Whether this action is paused. */
public final boolean isPaused;
/**
* Keys of tracks to be downloaded. If empty, all tracks will be downloaded. Empty if this action
* is a remove action.
Expand All @@ -137,12 +140,14 @@ private DownloadAction(
String type,
Uri uri,
boolean isRemoveAction,
boolean isPaused,
List<StreamKey> keys,
@Nullable String customCacheKey,
@Nullable byte[] data) {
this.type = type;
this.uri = uri;
this.isRemoveAction = isRemoveAction;
this.isPaused = isPaused;
this.customCacheKey = customCacheKey;
if (isRemoveAction) {
Assertions.checkArgument(keys.isEmpty());
Expand Down Expand Up @@ -190,6 +195,7 @@ public boolean equals(@Nullable Object o) {
return type.equals(that.type)
&& uri.equals(that.uri)
&& isRemoveAction == that.isRemoveAction
&& isPaused == that.isPaused
&& keys.equals(that.keys)
&& Util.areEqual(customCacheKey, that.customCacheKey)
&& Arrays.equals(data, that.data);
Expand All @@ -200,6 +206,7 @@ public final int hashCode() {
int result = type.hashCode();
result = 31 * result + uri.hashCode();
result = 31 * result + (isRemoveAction ? 1 : 0);
result = 31 * result + (isPaused ? 1 : 0);
result = 31 * result + keys.hashCode();
result = 31 * result + (customCacheKey != null ? customCacheKey.hashCode() : 0);
result = 31 * result + Arrays.hashCode(data);
Expand All @@ -220,6 +227,7 @@ public final void serializeToStream(OutputStream output) throws IOException {
dataOutputStream.writeInt(VERSION);
dataOutputStream.writeUTF(uri.toString());
dataOutputStream.writeBoolean(isRemoveAction);
dataOutputStream.writeBoolean(isPaused);
dataOutputStream.writeInt(data.length);
dataOutputStream.write(data);
dataOutputStream.writeInt(keys.size());
Expand All @@ -236,13 +244,36 @@ public final void serializeToStream(OutputStream output) throws IOException {
dataOutputStream.flush();
}

/**
* Create new action with {@link DownloadAction#isPaused} set to true.
*
* @return paused action
*/
final DownloadAction createPauseAction() {
return new DownloadAction(type, uri, isRemoveAction, true, keys, customCacheKey, data);
}

/**
* Create new action with {@link DownloadAction#isPaused} set to false.
*
* @return resumed action
*/
final DownloadAction createResumeAction() {
return new DownloadAction(type, uri, isRemoveAction, false, keys, customCacheKey, data);
}

private static DownloadAction readFromStream(DataInputStream input) throws IOException {
String type = input.readUTF();
int version = input.readInt();

Uri uri = Uri.parse(input.readUTF());
boolean isRemoveAction = input.readBoolean();

boolean isPauseAction = false;
if (version == 3) {
isPauseAction = input.readBoolean();
}

int dataLength = input.readInt();
byte[] data;
if (dataLength != 0) {
Expand Down Expand Up @@ -274,7 +305,7 @@ private static DownloadAction readFromStream(DataInputStream input) throws IOExc
customCacheKey = input.readBoolean() ? input.readUTF() : null;
}

return new DownloadAction(type, uri, isRemoveAction, keys, customCacheKey, data);
return new DownloadAction(type, uri, isRemoveAction, isPauseAction, keys, customCacheKey, data);
}

private static StreamKey readKey(String type, int version, DataInputStream input)
Expand Down
Loading

0 comments on commit 09e45d5

Please sign in to comment.