Commit b2c29da6 by olly Committed by Oliver Woodman

Finalize DownloadManager interface

- Rename getAllDownloads to getCurrentDownloads to make it clear
  that it doesn't include completed and failed downloads
- Change getDownloadCount to isWaitingForRequirements, which is
  what it's used for. Added TODO to make it returns the right thing

PiperOrigin-RevId: 243257856
parent c1e25f77
......@@ -276,29 +276,22 @@ public final class DownloadManager {
return initialized;
}
/** Returns whether there are no active downloads. */
/**
* Returns whether the manager is currently idle. The manager is idle if all downloads are in a
* terminal state (i.e. completed or failed), or if no progress can be made (e.g. because the
* download requirements are not met).
*/
public boolean isIdle() {
return activeDownloadCount == 0 && pendingMessages == 0;
}
/** Returns the used {@link DownloadIndex}. */
public DownloadIndex getDownloadIndex() {
return downloadIndex;
}
/** Returns the number of downloads. */
public int getDownloadCount() {
return downloads.size();
}
/** Returns the states of all current downloads. */
public Download[] getAllDownloads() {
return downloads.toArray(new Download[0]);
}
/** Returns the requirements needed to be met to start downloads. */
public Requirements getRequirements() {
return requirementsWatcher.getRequirements();
/**
* Returns whether this manager has one or more downloads that are not progressing for the sole
* reason that the {@link #getRequirements() Requirements} are not met.
*/
public boolean isWaitingForRequirements() {
// TODO: Fix this to return the right thing.
return !downloads.isEmpty();
}
/**
......@@ -319,6 +312,11 @@ public final class DownloadManager {
listeners.remove(listener);
}
/** Returns the requirements needed to be met to start downloads. */
public Requirements getRequirements() {
return requirementsWatcher.getRequirements();
}
/**
* Sets the requirements needed to be met to start downloads.
*
......@@ -334,6 +332,20 @@ public final class DownloadManager {
onRequirementsStateChanged(requirementsWatcher, notMetRequirements);
}
/** Returns the used {@link DownloadIndex}. */
public DownloadIndex getDownloadIndex() {
return downloadIndex;
}
/**
* Returns current downloads. Downloads that are in terminal states (i.e. completed or failed) are
* not included. To query all downloads including those in terminal states, use {@link
* #getDownloadIndex()} instead.
*/
public Download[] getCurrentDownloads() {
return downloads.toArray(new Download[0]);
}
/**
* Starts all downloads except those that are manually stopped (i.e. have a non-zero {@link
* Download#manualStopReason}).
......@@ -537,7 +549,7 @@ public final class DownloadManager {
break;
case MSG_SET_DOWNLOADS_STARTED:
boolean downloadsStarted = message.arg1 != 0;
setDownloadsStartedInternal(downloadsStarted);
setDownloadsStarted(downloadsStarted);
break;
case MSG_SET_NOT_MET_REQUIREMENTS:
notMetRequirements = message.arg1;
......@@ -598,7 +610,7 @@ public final class DownloadManager {
}
}
private void setDownloadsStartedInternal(boolean downloadsStarted) {
private void setDownloadsStarted(boolean downloadsStarted) {
if (this.downloadsStarted == downloadsStarted) {
return;
}
......
......@@ -469,7 +469,7 @@ public abstract class DownloadService extends Service {
public void onDestroy() {
logd("onDestroy");
DownloadManagerHelper downloadManagerHelper = downloadManagerListeners.get(getClass());
boolean unschedule = downloadManager.getDownloadCount() <= 0;
boolean unschedule = !downloadManager.isWaitingForRequirements();
downloadManagerHelper.detachService(this, unschedule);
if (foregroundNotificationUpdater != null) {
foregroundNotificationUpdater.stopPeriodicUpdates();
......@@ -609,7 +609,7 @@ public abstract class DownloadService extends Service {
}
public void update() {
Download[] downloads = downloadManager.getAllDownloads();
Download[] downloads = downloadManager.getCurrentDownloads();
startForeground(notificationId, getForegroundNotification(downloads));
notificationDisplayed = true;
if (periodicUpdatesStarted) {
......
......@@ -346,12 +346,12 @@ public class DownloadManagerTest {
TaskWrapper task3 = new DownloadRunner(uri3).postDownloadAction().postRemoveAction().getTask();
task3.assertRemoving();
Download[] states = downloadManager.getAllDownloads();
Download[] downloads = downloadManager.getCurrentDownloads();
assertThat(states).hasLength(3);
assertThat(downloads).hasLength(3);
String[] taskIds = {task1.taskId, task2.taskId, task3.taskId};
String[] stateTaskIds = {states[0].action.id, states[1].action.id, states[2].action.id};
assertThat(stateTaskIds).isEqualTo(taskIds);
String[] downloadIds = {downloads[0].action.id, downloads[1].action.id, downloads[2].action.id};
assertThat(downloadIds).isEqualTo(taskIds);
}
@Test
......
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