Periodically persist progress to index whilst downloading
PiperOrigin-RevId: 246173972
This commit is contained in:
parent
eed5d957d8
commit
ea0efa7464
@ -134,7 +134,8 @@ public final class DownloadManager {
|
||||
private static final int MSG_REMOVE_DOWNLOAD = 7;
|
||||
private static final int MSG_TASK_STOPPED = 8;
|
||||
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";
|
||||
|
||||
@ -569,6 +570,8 @@ public final class DownloadManager {
|
||||
|
||||
private static final class InternalHandler extends Handler {
|
||||
|
||||
private static final int UPDATE_PROGRESS_INTERVAL_MS = 5000;
|
||||
|
||||
public boolean released;
|
||||
|
||||
private final HandlerThread thread;
|
||||
@ -650,11 +653,13 @@ public final class DownloadManager {
|
||||
case MSG_CONTENT_LENGTH_CHANGED:
|
||||
task = (Task) message.obj;
|
||||
onContentLengthChanged(task);
|
||||
processedExternalMessage = false; // This message is posted internally.
|
||||
break;
|
||||
return; // No need to post back to mainHandler.
|
||||
case MSG_UPDATE_PROGRESS:
|
||||
updateProgress();
|
||||
return; // No need to post back to mainHandler.
|
||||
case MSG_RELEASE:
|
||||
release();
|
||||
return; // Don't post back to mainHandler on release.
|
||||
return; // No need to post back to mainHandler.
|
||||
default:
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
@ -868,7 +873,9 @@ public final class DownloadManager {
|
||||
minRetryCount,
|
||||
/* internalHandler= */ this);
|
||||
activeTasks.put(download.request.id, activeTask);
|
||||
activeDownloadTaskCount++;
|
||||
if (activeDownloadTaskCount++ == 0) {
|
||||
sendEmptyMessageDelayed(MSG_UPDATE_PROGRESS, UPDATE_PROGRESS_INTERVAL_MS);
|
||||
}
|
||||
activeTask.start();
|
||||
return activeTask;
|
||||
}
|
||||
@ -933,8 +940,8 @@ public final class DownloadManager {
|
||||
activeTasks.remove(downloadId);
|
||||
|
||||
boolean isRemove = task.isRemove;
|
||||
if (!isRemove) {
|
||||
activeDownloadTaskCount--;
|
||||
if (!isRemove && --activeDownloadTaskCount == 0) {
|
||||
removeMessages(MSG_UPDATE_PROGRESS);
|
||||
}
|
||||
|
||||
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.
|
||||
|
||||
private boolean canDownloadsRun() {
|
||||
|
@ -683,7 +683,7 @@ public abstract class DownloadService extends Service {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
private void notifyDownloadChange(Download download) {
|
||||
private void notifyDownloadChanged(Download download) {
|
||||
onDownloadChanged(download);
|
||||
if (foregroundNotificationUpdater != null) {
|
||||
if (download.state == Download.STATE_DOWNLOADING
|
||||
@ -834,7 +834,7 @@ public abstract class DownloadService extends Service {
|
||||
@Override
|
||||
public void onDownloadChanged(DownloadManager downloadManager, Download download) {
|
||||
if (downloadService != null) {
|
||||
downloadService.notifyDownloadChange(download);
|
||||
downloadService.notifyDownloadChanged(download);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user