public final class DownloadManager
extends java.lang.Object
A download manager instance must be accessed only from the thread that created it, unless that
thread does not have a Looper. In that case, it must be accessed only from the
application's main thread. Registered listeners will be called on the same thread.
| Modifier and Type | Class and Description |
|---|---|
static interface |
DownloadManager.Listener
Listener for
DownloadManager events. |
static class |
DownloadManager.TaskState
Represents state of a task.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_MAX_SIMULTANEOUS_DOWNLOADS
The default maximum number of simultaneous download tasks.
|
static int |
DEFAULT_MIN_RETRY_COUNT
The default minimum number of times a task must be retried before failing.
|
| Constructor and Description |
|---|
DownloadManager(Cache cache,
DataSource.Factory upstreamDataSourceFactory,
java.io.File actionSaveFile,
DownloadAction.Deserializer... deserializers)
Creates a
DownloadManager. |
DownloadManager(DownloaderConstructorHelper constructorHelper,
java.io.File actionFile,
DownloadAction.Deserializer... deserializers)
Constructs a
DownloadManager. |
DownloadManager(DownloaderConstructorHelper constructorHelper,
int maxSimultaneousDownloads,
int minRetryCount,
java.io.File actionFile,
DownloadAction.Deserializer... deserializers)
Constructs a
DownloadManager. |
| Modifier and Type | Method and Description |
|---|---|
void |
addListener(DownloadManager.Listener listener)
Adds a
DownloadManager.Listener. |
DownloadManager.TaskState[] |
getAllTaskStates()
Returns the states of all current tasks.
|
int |
getDownloadCount()
Returns the number of download tasks.
|
int |
getTaskCount()
Returns the number of tasks.
|
DownloadManager.TaskState |
getTaskState(int taskId)
Returns the state of a task, or null if no such task exists
|
int |
handleAction(byte[] actionData)
Deserializes an action from
actionData, and calls handleAction(DownloadAction). |
int |
handleAction(DownloadAction action)
Handles the given action.
|
boolean |
isIdle()
Returns whether there are no active tasks.
|
boolean |
isInitialized()
Returns whether the manager has completed initialization.
|
void |
release()
Stops all of the tasks and releases resources.
|
void |
removeListener(DownloadManager.Listener listener)
Removes a
DownloadManager.Listener. |
void |
startDownloads()
Starts the download tasks.
|
void |
stopDownloads()
Stops all of the download tasks.
|
public static final int DEFAULT_MAX_SIMULTANEOUS_DOWNLOADS
public static final int DEFAULT_MIN_RETRY_COUNT
public DownloadManager(Cache cache, DataSource.Factory upstreamDataSourceFactory, java.io.File actionSaveFile, DownloadAction.Deserializer... deserializers)
DownloadManager.cache - Cache instance to be used to store downloaded data.upstreamDataSourceFactory - A DataSource.Factory for creating data sources for
downloading upstream data.actionSaveFile - File to save active actions.deserializers - Used to deserialize DownloadActions.public DownloadManager(DownloaderConstructorHelper constructorHelper, java.io.File actionFile, DownloadAction.Deserializer... deserializers)
DownloadManager.constructorHelper - A DownloaderConstructorHelper to create Downloaders
for downloading data.actionFile - The file in which active actions are saved.deserializers - Used to deserialize DownloadActions.public DownloadManager(DownloaderConstructorHelper constructorHelper, int maxSimultaneousDownloads, int minRetryCount, java.io.File actionFile, DownloadAction.Deserializer... deserializers)
DownloadManager.constructorHelper - A DownloaderConstructorHelper to create Downloaders
for downloading data.maxSimultaneousDownloads - The maximum number of simultaneous download tasks.minRetryCount - The minimum number of times a task must be retried before failing.actionFile - The file in which active actions are saved.deserializers - Used to deserialize DownloadActions.public void addListener(DownloadManager.Listener listener)
DownloadManager.Listener.listener - The listener to be added.public void removeListener(DownloadManager.Listener listener)
DownloadManager.Listener.listener - The listener to be removed.public void startDownloads()
public void stopDownloads()
startDownloads() to restart tasks.public int handleAction(byte[] actionData)
throws java.io.IOException
actionData, and calls handleAction(DownloadAction).actionData - Serialized version of the action to be executed.java.io.IOException - If an error occurs deserializing the action.public int handleAction(DownloadAction action)
action - The action to be executed.public int getTaskCount()
public int getDownloadCount()
@Nullable public DownloadManager.TaskState getTaskState(int taskId)
public DownloadManager.TaskState[] getAllTaskStates()
public boolean isInitialized()
public boolean isIdle()
public void release()