Commit ee2e89e0 by eguven Committed by Oliver Woodman

Remove DownloadManager Download interim state when created

When a Download is created it's set to queued state but doesn't notify
listeners about this state. DownloadManager checks if it can start the
download. After this checks it notifies the listeners.

With this change Download can immediately check if it can be started and
sends correct notification.

PiperOrigin-RevId: 225967129
parent 4d282b22
...@@ -196,15 +196,8 @@ public final class DownloadManager { ...@@ -196,15 +196,8 @@ public final class DownloadManager {
public void handleAction(DownloadAction action) { public void handleAction(DownloadAction action) {
Assertions.checkState(!released); Assertions.checkState(!released);
if (initialized) { if (initialized) {
Download download = getOrAddDownloadForAction(action); addDownloadForAction(action);
saveActions(); saveActions();
maybeStartDownloads();
if (download.state == STATE_QUEUED) {
// Download did not change out of its initial state, and so its initial state won't have
// been
// reported to listeners. Do so now.
notifyListenersDownloadStateChange(download);
}
} else { } else {
actionQueue.add(action); actionQueue.add(action);
} }
...@@ -284,19 +277,18 @@ public final class DownloadManager { ...@@ -284,19 +277,18 @@ public final class DownloadManager {
logd("Released"); logd("Released");
} }
private Download getOrAddDownloadForAction(DownloadAction action) { private void addDownloadForAction(DownloadAction action) {
for (int i = 0; i < downloads.size(); i++) { for (int i = 0; i < downloads.size(); i++) {
Download download = downloads.get(i); Download download = downloads.get(i);
if (download.action.isSameMedia(action)) { if (download.action.isSameMedia(action)) {
download.addAction(action); download.addAction(action);
logd("Action is added to existing download", download); logd("Action is added to existing download", download);
return download; return;
} }
} }
Download download = new Download(this, downloaderFactory, action, minRetryCount); Download download = new Download(this, downloaderFactory, action, minRetryCount);
downloads.add(download); downloads.add(download);
logd("Download is added", download); logd("Download is added", download);
return download;
} }
/** /**
...@@ -311,22 +303,21 @@ public final class DownloadManager { ...@@ -311,22 +303,21 @@ public final class DownloadManager {
if (!initialized || released) { if (!initialized || released) {
return; return;
} }
boolean skipDownloads = downloadsStopped || activeDownloads.size() == maxActiveDownloads;
for (int i = 0; i < downloads.size(); i++) { for (int i = 0; i < downloads.size(); i++) {
Download download = downloads.get(i); maybeStartDownload(downloads.get(i));
if (!download.canStart()) { }
continue; }
}
boolean isRemoveAction = download.action.isRemoveAction; private boolean maybeStartDownload(Download download) {
if (isRemoveAction || !skipDownloads) { if (download.action.isRemoveAction) {
download.start(); return download.start();
if (!isRemoveAction) { } else if (!downloadsStopped && activeDownloads.size() < maxActiveDownloads) {
activeDownloads.add(download); if (download.start()) {
skipDownloads = activeDownloads.size() == maxActiveDownloads; activeDownloads.add(download);
} return true;
} }
} }
return false;
} }
private void maybeNotifyListenersIdle() { private void maybeNotifyListenersIdle() {
...@@ -384,7 +375,7 @@ public final class DownloadManager { ...@@ -384,7 +375,7 @@ public final class DownloadManager {
return; return;
} }
for (DownloadAction action : actions) { for (DownloadAction action : actions) {
getOrAddDownloadForAction(action); addDownloadForAction(action);
} }
logd("Downloads are created."); logd("Downloads are created.");
initialized = true; initialized = true;
...@@ -393,19 +384,10 @@ public final class DownloadManager { ...@@ -393,19 +384,10 @@ public final class DownloadManager {
} }
if (!actionQueue.isEmpty()) { if (!actionQueue.isEmpty()) {
while (!actionQueue.isEmpty()) { while (!actionQueue.isEmpty()) {
getOrAddDownloadForAction(actionQueue.remove()); addDownloadForAction(actionQueue.remove());
} }
saveActions(); saveActions();
} }
maybeStartDownloads();
for (int i = 0; i < downloads.size(); i++) {
Download download = downloads.get(i);
if (download.state == STATE_QUEUED) {
// Download did not change out of its initial state, and so its initial state
// won't have been reported to listeners. Do so now.
notifyListenersDownloadStateChange(download);
}
}
}); });
}); });
} }
...@@ -581,6 +563,11 @@ public final class DownloadManager { ...@@ -581,6 +563,11 @@ public final class DownloadManager {
state = STATE_QUEUED; state = STATE_QUEUED;
actionQueue = new ArrayDeque<>(); actionQueue = new ArrayDeque<>();
actionQueue.add(action); actionQueue.add(action);
if (!downloadManager.maybeStartDownload(this)) {
// If download is started, listeners are already notified about the started state. Otherwise
// notify them here about the queued state.
downloadManager.onDownloadStateChange(this);
}
} }
public void addAction(DownloadAction newAction) { public void addAction(DownloadAction newAction) {
...@@ -637,20 +624,18 @@ public final class DownloadManager { ...@@ -637,20 +624,18 @@ public final class DownloadManager {
+ DownloadState.getStateString(state); + DownloadState.getStateString(state);
} }
public boolean canStart() { public boolean start() {
return state == STATE_QUEUED; if (state != STATE_QUEUED) {
} return false;
public void start() {
if (state == STATE_QUEUED) {
state = STATE_STARTED;
action = actionQueue.peek();
downloader = downloaderFactory.createDownloader(action);
downloadThread =
new DownloadThread(
this, downloader, action.isRemoveAction, minRetryCount, downloadManager.handler);
downloadManager.onDownloadStateChange(this);
} }
state = STATE_STARTED;
action = actionQueue.peek();
downloader = downloaderFactory.createDownloader(action);
downloadThread =
new DownloadThread(
this, downloader, action.isRemoveAction, minRetryCount, downloadManager.handler);
downloadManager.onDownloadStateChange(this);
return true;
} }
public void stop() { public void stop() {
......
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