mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Stop using remove DownloadActions in DownloadManager
PiperOrigin-RevId: 240376409
This commit is contained in:
parent
709a712a5f
commit
ae5e5f7efc
2 changed files with 27 additions and 31 deletions
|
|
@ -665,7 +665,7 @@ public final class DownloadManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
@StartThreadResults
|
@StartThreadResults
|
||||||
private int startDownloadThread(Download download, DownloadAction action) {
|
private int startDownloadThread(Download download) {
|
||||||
if (!initialized || released) {
|
if (!initialized || released) {
|
||||||
return START_THREAD_NOT_ALLOWED;
|
return START_THREAD_NOT_ALLOWED;
|
||||||
}
|
}
|
||||||
|
|
@ -675,15 +675,13 @@ public final class DownloadManager {
|
||||||
}
|
}
|
||||||
return START_THREAD_WAIT_REMOVAL_TO_FINISH;
|
return START_THREAD_WAIT_REMOVAL_TO_FINISH;
|
||||||
}
|
}
|
||||||
if (!action.isRemoveAction) {
|
if (!download.isInRemoveState()) {
|
||||||
if (simultaneousDownloads == maxSimultaneousDownloads) {
|
if (simultaneousDownloads == maxSimultaneousDownloads) {
|
||||||
return START_THREAD_TOO_MANY_DOWNLOADS;
|
return START_THREAD_TOO_MANY_DOWNLOADS;
|
||||||
}
|
}
|
||||||
simultaneousDownloads++;
|
simultaneousDownloads++;
|
||||||
}
|
}
|
||||||
Downloader downloader = downloaderFactory.createDownloader(action);
|
activeDownloads.put(download, new DownloadThread(download));
|
||||||
DownloadThread downloadThread = new DownloadThread(download, downloader, action.isRemoveAction);
|
|
||||||
activeDownloads.put(download, downloadThread);
|
|
||||||
logd("Download is started", download);
|
logd("Download is started", download);
|
||||||
return START_THREAD_SUCCEEDED;
|
return START_THREAD_SUCCEEDED;
|
||||||
}
|
}
|
||||||
|
|
@ -823,8 +821,8 @@ public final class DownloadManager {
|
||||||
public void start() {
|
public void start() {
|
||||||
if (state == STATE_QUEUED || state == STATE_DOWNLOADING) {
|
if (state == STATE_QUEUED || state == STATE_DOWNLOADING) {
|
||||||
startOrQueue();
|
startOrQueue();
|
||||||
} else if (state == STATE_REMOVING || state == STATE_RESTARTING) {
|
} else if (isInRemoveState()) {
|
||||||
downloadManager.startDownloadThread(this, getAction());
|
downloadManager.startDownloadThread(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -838,6 +836,20 @@ public final class DownloadManager {
|
||||||
updateStopState();
|
updateStopState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DownloadAction getAction() {
|
||||||
|
Assertions.checkState(state != STATE_REMOVED);
|
||||||
|
return DownloadAction.createDownloadAction(
|
||||||
|
downloadState.type,
|
||||||
|
downloadState.uri,
|
||||||
|
Arrays.asList(downloadState.streamKeys),
|
||||||
|
downloadState.cacheKey,
|
||||||
|
downloadState.customMetadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isInRemoveState() {
|
||||||
|
return state == STATE_REMOVING || state == STATE_RESTARTING;
|
||||||
|
}
|
||||||
|
|
||||||
private void updateStopState() {
|
private void updateStopState() {
|
||||||
DownloadState oldDownloadState = downloadState;
|
DownloadState oldDownloadState = downloadState;
|
||||||
if (canStart()) {
|
if (canStart()) {
|
||||||
|
|
@ -859,8 +871,8 @@ public final class DownloadManager {
|
||||||
// Don't notify listeners with initial state until we make sure we don't switch to
|
// Don't notify listeners with initial state until we make sure we don't switch to
|
||||||
// another state immediately.
|
// another state immediately.
|
||||||
state = initialState;
|
state = initialState;
|
||||||
if (state == STATE_REMOVING || state == STATE_RESTARTING) {
|
if (isInRemoveState()) {
|
||||||
downloadManager.startDownloadThread(this, getAction());
|
downloadManager.startDownloadThread(this);
|
||||||
} else if (canStart()) {
|
} else if (canStart()) {
|
||||||
startOrQueue();
|
startOrQueue();
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -876,8 +888,8 @@ public final class DownloadManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startOrQueue() {
|
private void startOrQueue() {
|
||||||
Assertions.checkState(!(state == STATE_REMOVING || state == STATE_RESTARTING));
|
Assertions.checkState(!isInRemoveState());
|
||||||
@StartThreadResults int result = downloadManager.startDownloadThread(this, getAction());
|
@StartThreadResults int result = downloadManager.startDownloadThread(this);
|
||||||
Assertions.checkState(result != START_THREAD_WAIT_REMOVAL_TO_FINISH);
|
Assertions.checkState(result != START_THREAD_WAIT_REMOVAL_TO_FINISH);
|
||||||
if (result == START_THREAD_SUCCEEDED || result == START_THREAD_WAIT_DOWNLOAD_CANCELLATION) {
|
if (result == START_THREAD_SUCCEEDED || result == START_THREAD_WAIT_DOWNLOAD_CANCELLATION) {
|
||||||
setState(STATE_DOWNLOADING);
|
setState(STATE_DOWNLOADING);
|
||||||
|
|
@ -886,20 +898,6 @@ public final class DownloadManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private DownloadAction getAction() {
|
|
||||||
Assertions.checkState(state != STATE_REMOVED);
|
|
||||||
if (state == STATE_REMOVING || state == STATE_RESTARTING) {
|
|
||||||
return DownloadAction.createRemoveAction(
|
|
||||||
downloadState.type, downloadState.uri, downloadState.cacheKey);
|
|
||||||
}
|
|
||||||
return DownloadAction.createDownloadAction(
|
|
||||||
downloadState.type,
|
|
||||||
downloadState.uri,
|
|
||||||
Arrays.asList(downloadState.streamKeys),
|
|
||||||
downloadState.cacheKey,
|
|
||||||
downloadState.customMetadata);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setState(@DownloadState.State int newState) {
|
private void setState(@DownloadState.State int newState) {
|
||||||
if (state != newState) {
|
if (state != newState) {
|
||||||
state = newState;
|
state = newState;
|
||||||
|
|
@ -912,7 +910,7 @@ public final class DownloadManager {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (isCanceled) {
|
if (isCanceled) {
|
||||||
downloadManager.startDownloadThread(this, getAction());
|
downloadManager.startDownloadThread(this);
|
||||||
} else if (state == STATE_RESTARTING) {
|
} else if (state == STATE_RESTARTING) {
|
||||||
initialize(STATE_QUEUED);
|
initialize(STATE_QUEUED);
|
||||||
} else if (state == STATE_REMOVING) {
|
} else if (state == STATE_REMOVING) {
|
||||||
|
|
@ -937,10 +935,10 @@ public final class DownloadManager {
|
||||||
private final boolean isRemoveThread;
|
private final boolean isRemoveThread;
|
||||||
private volatile boolean isCanceled;
|
private volatile boolean isCanceled;
|
||||||
|
|
||||||
private DownloadThread(Download download, Downloader downloader, boolean isRemoveThread) {
|
private DownloadThread(Download download) {
|
||||||
this.download = download;
|
this.download = download;
|
||||||
this.downloader = downloader;
|
this.downloader = downloaderFactory.createDownloader(download.getAction());
|
||||||
this.isRemoveThread = isRemoveThread;
|
this.isRemoveThread = download.isInRemoveState();
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -702,7 +702,6 @@ public class DownloadManagerTest {
|
||||||
public void download() throws InterruptedException, IOException {
|
public void download() throws InterruptedException, IOException {
|
||||||
// It's ok to update this directly as no other thread will update it.
|
// It's ok to update this directly as no other thread will update it.
|
||||||
startCount++;
|
startCount++;
|
||||||
assertThat(action.isRemoveAction).isFalse();
|
|
||||||
started.countDown();
|
started.countDown();
|
||||||
block();
|
block();
|
||||||
if (enableDownloadIOException) {
|
if (enableDownloadIOException) {
|
||||||
|
|
@ -721,7 +720,6 @@ public class DownloadManagerTest {
|
||||||
public void remove() throws InterruptedException {
|
public void remove() throws InterruptedException {
|
||||||
// It's ok to update this directly as no other thread will update it.
|
// It's ok to update this directly as no other thread will update it.
|
||||||
startCount++;
|
startCount++;
|
||||||
assertThat(action.isRemoveAction).isTrue();
|
|
||||||
started.countDown();
|
started.countDown();
|
||||||
block();
|
block();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue