Periodically persist progress to index whilst downloading

PiperOrigin-RevId: 246173972
This commit is contained in:
olly 2019-05-01 20:06:11 +01:00 committed by Oliver Woodman
parent eed5d957d8
commit ea0efa7464
2 changed files with 32 additions and 9 deletions

View File

@ -134,7 +134,8 @@ public final class DownloadManager {
private static final int MSG_REMOVE_DOWNLOAD = 7; private static final int MSG_REMOVE_DOWNLOAD = 7;
private static final int MSG_TASK_STOPPED = 8; private static final int MSG_TASK_STOPPED = 8;
private static final int MSG_CONTENT_LENGTH_CHANGED = 9; private static final int MSG_CONTENT_LENGTH_CHANGED = 9;
private static final int MSG_RELEASE = 10; private static final int MSG_UPDATE_PROGRESS = 10;
private static final int MSG_RELEASE = 11;
private static final String TAG = "DownloadManager"; private static final String TAG = "DownloadManager";
@ -569,6 +570,8 @@ public final class DownloadManager {
private static final class InternalHandler extends Handler { private static final class InternalHandler extends Handler {
private static final int UPDATE_PROGRESS_INTERVAL_MS = 5000;
public boolean released; public boolean released;
private final HandlerThread thread; private final HandlerThread thread;
@ -650,11 +653,13 @@ public final class DownloadManager {
case MSG_CONTENT_LENGTH_CHANGED: case MSG_CONTENT_LENGTH_CHANGED:
task = (Task) message.obj; task = (Task) message.obj;
onContentLengthChanged(task); onContentLengthChanged(task);
processedExternalMessage = false; // This message is posted internally. return; // No need to post back to mainHandler.
break; case MSG_UPDATE_PROGRESS:
updateProgress();
return; // No need to post back to mainHandler.
case MSG_RELEASE: case MSG_RELEASE:
release(); release();
return; // Don't post back to mainHandler on release. return; // No need to post back to mainHandler.
default: default:
throw new IllegalStateException(); throw new IllegalStateException();
} }
@ -868,7 +873,9 @@ public final class DownloadManager {
minRetryCount, minRetryCount,
/* internalHandler= */ this); /* internalHandler= */ this);
activeTasks.put(download.request.id, activeTask); activeTasks.put(download.request.id, activeTask);
activeDownloadTaskCount++; if (activeDownloadTaskCount++ == 0) {
sendEmptyMessageDelayed(MSG_UPDATE_PROGRESS, UPDATE_PROGRESS_INTERVAL_MS);
}
activeTask.start(); activeTask.start();
return activeTask; return activeTask;
} }
@ -933,8 +940,8 @@ public final class DownloadManager {
activeTasks.remove(downloadId); activeTasks.remove(downloadId);
boolean isRemove = task.isRemove; boolean isRemove = task.isRemove;
if (!isRemove) { if (!isRemove && --activeDownloadTaskCount == 0) {
activeDownloadTaskCount--; removeMessages(MSG_UPDATE_PROGRESS);
} }
if (task.isCanceled) { if (task.isCanceled) {
@ -1013,6 +1020,22 @@ public final class DownloadManager {
} }
} }
// Progress updates.
private void updateProgress() {
for (int i = 0; i < downloads.size(); i++) {
Download download = downloads.get(i);
if (download.state == STATE_DOWNLOADING) {
try {
downloadIndex.putDownload(download);
} catch (IOException e) {
Log.e(TAG, "Failed to update index.", e);
}
}
}
sendEmptyMessageDelayed(MSG_UPDATE_PROGRESS, UPDATE_PROGRESS_INTERVAL_MS);
}
// Helper methods. // Helper methods.
private boolean canDownloadsRun() { private boolean canDownloadsRun() {

View File

@ -683,7 +683,7 @@ public abstract class DownloadService extends Service {
// Do nothing. // Do nothing.
} }
private void notifyDownloadChange(Download download) { private void notifyDownloadChanged(Download download) {
onDownloadChanged(download); onDownloadChanged(download);
if (foregroundNotificationUpdater != null) { if (foregroundNotificationUpdater != null) {
if (download.state == Download.STATE_DOWNLOADING if (download.state == Download.STATE_DOWNLOADING
@ -834,7 +834,7 @@ public abstract class DownloadService extends Service {
@Override @Override
public void onDownloadChanged(DownloadManager downloadManager, Download download) { public void onDownloadChanged(DownloadManager downloadManager, Download download) {
if (downloadService != null) { if (downloadService != null) {
downloadService.notifyDownloadChange(download); downloadService.notifyDownloadChanged(download);
} }
} }