Commit f2cef123 by olly Committed by Oliver Woodman

Reorder DownloadManager methods into a more natural order

- Start before stop
- Release near bottom
- Private after public

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=195570480
parent c5e8f6ff
...@@ -179,52 +179,6 @@ public final class DownloadManager { ...@@ -179,52 +179,6 @@ public final class DownloadManager {
} }
/** /**
* Stops all of the tasks and releases resources. If the action file isn't up to date, waits for
* the changes to be written. The manager must not be accessed after this method has been called.
*/
public void release() {
if (released) {
return;
}
released = true;
for (int i = 0; i < tasks.size(); i++) {
tasks.get(i).stop();
}
final ConditionVariable fileIOFinishedCondition = new ConditionVariable();
fileIOHandler.post(new Runnable() {
@Override
public void run() {
fileIOFinishedCondition.open();
}
});
fileIOFinishedCondition.block();
fileIOThread.quit();
logd("Released");
}
/** Stops all of the download tasks. Call {@link #startDownloads()} to restart tasks. */
public void stopDownloads() {
Assertions.checkState(!released);
if (!downloadsStopped) {
downloadsStopped = true;
for (int i = 0; i < activeDownloadTasks.size(); i++) {
activeDownloadTasks.get(i).stop();
}
logd("Downloads are stopping");
}
}
/** Starts the download tasks. */
public void startDownloads() {
Assertions.checkState(!released);
if (downloadsStopped) {
downloadsStopped = false;
maybeStartTasks();
logd("Downloads are started");
}
}
/**
* Adds a {@link Listener}. * Adds a {@link Listener}.
* *
* @param listener The listener to be added. * @param listener The listener to be added.
...@@ -242,6 +196,28 @@ public final class DownloadManager { ...@@ -242,6 +196,28 @@ public final class DownloadManager {
listeners.remove(listener); listeners.remove(listener);
} }
/** Starts the download tasks. */
public void startDownloads() {
Assertions.checkState(!released);
if (downloadsStopped) {
downloadsStopped = false;
maybeStartTasks();
logd("Downloads are started");
}
}
/** Stops all of the download tasks. Call {@link #startDownloads()} to restart tasks. */
public void stopDownloads() {
Assertions.checkState(!released);
if (!downloadsStopped) {
downloadsStopped = true;
for (int i = 0; i < activeDownloadTasks.size(); i++) {
activeDownloadTasks.get(i).stop();
}
logd("Downloads are stopping");
}
}
/** /**
* Deserializes an action from {@code actionData}, and calls {@link * Deserializes an action from {@code actionData}, and calls {@link
* #handleAction(DownloadAction)}. * #handleAction(DownloadAction)}.
...@@ -275,13 +251,6 @@ public final class DownloadManager { ...@@ -275,13 +251,6 @@ public final class DownloadManager {
return task.id; return task.id;
} }
private Task addTaskForAction(DownloadAction action) {
Task task = new Task(nextTaskId++, this, action, minRetryCount);
tasks.add(task);
logd("Task is added", task);
return task;
}
/** Returns the current number of tasks. */ /** Returns the current number of tasks. */
public int getTaskCount() { public int getTaskCount() {
Assertions.checkState(!released); Assertions.checkState(!released);
...@@ -325,6 +294,37 @@ public final class DownloadManager { ...@@ -325,6 +294,37 @@ public final class DownloadManager {
} }
/** /**
* Stops all of the tasks and releases resources. If the action file isn't up to date, waits for
* the changes to be written. The manager must not be accessed after this method has been called.
*/
public void release() {
if (released) {
return;
}
released = true;
for (int i = 0; i < tasks.size(); i++) {
tasks.get(i).stop();
}
final ConditionVariable fileIOFinishedCondition = new ConditionVariable();
fileIOHandler.post(new Runnable() {
@Override
public void run() {
fileIOFinishedCondition.open();
}
});
fileIOFinishedCondition.block();
fileIOThread.quit();
logd("Released");
}
private Task addTaskForAction(DownloadAction action) {
Task task = new Task(nextTaskId++, this, action, minRetryCount);
tasks.add(task);
logd("Task is added", task);
return task;
}
/**
* Iterates through the task queue and starts any task if all of the following are true: * Iterates through the task queue and starts any task if all of the following are true:
* *
* <ul> * <ul>
......
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