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,39 +179,21 @@ public final class DownloadManager { ...@@ -179,39 +179,21 @@ public final class DownloadManager {
} }
/** /**
* Stops all of the tasks and releases resources. If the action file isn't up to date, waits for * Adds a {@link Listener}.
* the changes to be written. The manager must not be accessed after this method has been called. *
* @param listener The listener to be added.
*/ */
public void release() { public void addListener(Listener listener) {
if (released) { listeners.add(listener);
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() { * Removes a {@link Listener}.
Assertions.checkState(!released); *
if (!downloadsStopped) { * @param listener The listener to be removed.
downloadsStopped = true; */
for (int i = 0; i < activeDownloadTasks.size(); i++) { public void removeListener(Listener listener) {
activeDownloadTasks.get(i).stop(); listeners.remove(listener);
}
logd("Downloads are stopping");
}
} }
/** Starts the download tasks. */ /** Starts the download tasks. */
...@@ -224,22 +206,16 @@ public final class DownloadManager { ...@@ -224,22 +206,16 @@ public final class DownloadManager {
} }
} }
/** /** Stops all of the download tasks. Call {@link #startDownloads()} to restart tasks. */
* Adds a {@link Listener}. public void stopDownloads() {
* Assertions.checkState(!released);
* @param listener The listener to be added. if (!downloadsStopped) {
*/ downloadsStopped = true;
public void addListener(Listener listener) { for (int i = 0; i < activeDownloadTasks.size(); i++) {
listeners.add(listener); activeDownloadTasks.get(i).stop();
}
logd("Downloads are stopping");
} }
/**
* Removes a {@link Listener}.
*
* @param listener The listener to be removed.
*/
public void removeListener(Listener listener) {
listeners.remove(listener);
} }
/** /**
...@@ -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