Add DownloadManager.Listener.onInitialized

If an app wants to reconcile its own state with that of a DownloadManager,
it's helpful to know when the DownloadManager has finished restoring any
previously persisted tasks.

Also suppress initial "state changed to paused" listener invocations for
tasks that are immediately started.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=195594881
This commit is contained in:
olly 2018-05-06 12:04:45 -07:00 committed by Oliver Woodman
parent b9aaf1ebab
commit 983fe9cc9f
4 changed files with 51 additions and 11 deletions

View File

@ -136,6 +136,11 @@ public class DownloadTracker implements DownloadManager.Listener {
// DownloadManager.Listener // DownloadManager.Listener
@Override
public void onInitialized(DownloadManager downloadManager) {
// Do nothing.
}
@Override @Override
public void onTaskStateChanged(DownloadManager downloadManager, TaskState taskState) { public void onTaskStateChanged(DownloadManager downloadManager, TaskState taskState) {
DownloadAction action = taskState.action; DownloadAction action = taskState.action;

View File

@ -53,6 +53,12 @@ public final class DownloadManager {
/** Listener for {@link DownloadManager} events. */ /** Listener for {@link DownloadManager} events. */
public interface Listener { public interface Listener {
/**
* Called when all actions have been restored.
*
* @param downloadManager The reporting instance.
*/
void onInitialized(DownloadManager downloadManager);
/** /**
* Called when the state of a task changes. * Called when the state of a task changes.
* *
@ -90,7 +96,7 @@ public final class DownloadManager {
private final CopyOnWriteArraySet<Listener> listeners; private final CopyOnWriteArraySet<Listener> listeners;
private int nextTaskId; private int nextTaskId;
private boolean actionFileLoadCompleted; private boolean initialized;
private boolean released; private boolean released;
private boolean downloadsStopped; private boolean downloadsStopped;
@ -243,10 +249,15 @@ public final class DownloadManager {
public int handleAction(DownloadAction action) { public int handleAction(DownloadAction action) {
Assertions.checkState(!released); Assertions.checkState(!released);
Task task = addTaskForAction(action); Task task = addTaskForAction(action);
if (actionFileLoadCompleted) { if (initialized) {
notifyListenersTaskStateChange(task); notifyListenersTaskStateChange(task);
saveActions(); saveActions();
maybeStartTasks(); maybeStartTasks();
if (task.currentState == STATE_QUEUED) {
// Task did not change out of its initial state, and so its initial state won't have been
// reported to listeners. Do so now.
notifyListenersTaskStateChange(task);
}
} }
return task.id; return task.id;
} }
@ -279,10 +290,16 @@ public final class DownloadManager {
return states; return states;
} }
/** Returns whether the manager has completed initialization. */
public boolean isInitialized() {
Assertions.checkState(!released);
return initialized;
}
/** Returns whether there are no active tasks. */ /** Returns whether there are no active tasks. */
public boolean isIdle() { public boolean isIdle() {
Assertions.checkState(!released); Assertions.checkState(!released);
if (!actionFileLoadCompleted) { if (!initialized) {
return false; return false;
} }
for (int i = 0; i < tasks.size(); i++) { for (int i = 0; i < tasks.size(); i++) {
@ -337,7 +354,7 @@ public final class DownloadManager {
* If the task is a remove action then preceding conflicting tasks are canceled. * If the task is a remove action then preceding conflicting tasks are canceled.
*/ */
private void maybeStartTasks() { private void maybeStartTasks() {
if (!actionFileLoadCompleted || released) { if (!initialized || released) {
return; return;
} }
@ -437,26 +454,34 @@ public final class DownloadManager {
new Runnable() { new Runnable() {
@Override @Override
public void run() { public void run() {
actionFileLoadCompleted = true;
if (released) { if (released) {
return; return;
} }
List<Task> pendingTasks = new ArrayList<>(tasks); List<Task> pendingTasks = new ArrayList<>(tasks);
tasks.clear(); tasks.clear();
for (DownloadAction action : actions) { for (DownloadAction action : actions) {
Task task = addTaskForAction(action); addTaskForAction(action);
notifyListenersTaskStateChange(task); }
logd("Tasks are created.");
initialized = true;
for (Listener listener : listeners) {
listener.onInitialized(DownloadManager.this);
} }
if (!pendingTasks.isEmpty()) { if (!pendingTasks.isEmpty()) {
for (int i = 0; i < pendingTasks.size(); i++) { for (int i = 0; i < pendingTasks.size(); i++) {
Task pendingTask = pendingTasks.get(i); tasks.add(pendingTasks.get(i));
tasks.add(pendingTask);
notifyListenersTaskStateChange(pendingTask);
} }
saveActions(); saveActions();
} }
logd("Tasks are created.");
maybeStartTasks(); maybeStartTasks();
for (int i = 0; i < pendingTasks.size(); i++) {
Task pendingTask = pendingTasks.get(i);
if (pendingTask.currentState == STATE_QUEUED) {
// Task did not change out of its initial state, and so its initial state
// won't have been reported to listeners. Do so now.
notifyListenersTaskStateChange(pendingTask);
}
}
} }
}); });
} }

View File

@ -313,6 +313,11 @@ public abstract class DownloadService extends Service {
} }
private final class DownloadManagerListener implements DownloadManager.Listener { private final class DownloadManagerListener implements DownloadManager.Listener {
@Override
public void onInitialized(DownloadManager downloadManager) {
// Do nothing.
}
@Override @Override
public void onTaskStateChanged(DownloadManager downloadManager, TaskState taskState) { public void onTaskStateChanged(DownloadManager downloadManager, TaskState taskState) {
DownloadService.this.onTaskStateChanged(taskState); DownloadService.this.onTaskStateChanged(taskState);

View File

@ -51,6 +51,11 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
this.downloadError = null; this.downloadError = null;
} }
@Override
public void onInitialized(DownloadManager downloadManager) {
// Do nothing.
}
@Override @Override
public void onTaskStateChanged( public void onTaskStateChanged(
DownloadManager downloadManager, DownloadManager.TaskState taskState) { DownloadManager downloadManager, DownloadManager.TaskState taskState) {