DownloadManager
@UnstableApi
class DownloadManager
Manages downloads.
Normally a download manager should be accessed via a DownloadService. When a download manager is used directly instead, downloads will be initially paused and so must be resumed by calling resumeDownloads.
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. In all cases the Looper of the thread from which the manager must be accessed can be queried using getApplicationLooper.
Summary
Nested types |
|---|
interface DownloadManager.ListenerListener for |
Constants |
|
|---|---|
const Int |
The default maximum number of parallel downloads. |
const Int |
The default minimum number of times a download must be retried before failing. |
const Requirements! |
The default requirement is that the device has network connectivity. |
Public constructors |
|---|
DownloadManager(Constructs a |
DownloadManager(Constructs a |
Public functions |
|
|---|---|
Unit |
addDownload(request: DownloadRequest!)Adds a download defined by the given request. |
Unit |
addDownload(request: DownloadRequest!, stopReason: Int)Adds a download defined by the given request and with the specified stop reason. |
Unit |
addListener(listener: DownloadManager.Listener!)Adds a |
Looper! |
Returns the |
(Mutable)List<Download!>! |
Returns current downloads. |
DownloadIndex! |
Returns the used |
Requirements! |
Returns the requirements needed to be met to progress. |
Boolean |
isIdle()Returns whether the manager is currently idle. |
Boolean |
Returns whether the manager has completed initialization. |
Boolean |
Returns whether this manager has one or more downloads that are not progressing for the sole reason that the |
Unit |
Pauses downloads. |
Unit |
release()Stops the downloads and releases resources. |
Unit |
Cancels all pending downloads and removes all downloaded data. |
Unit |
removeDownload(id: String!)Cancels the download with the |
Unit |
removeListener(listener: DownloadManager.Listener!)Removes a |
Unit |
Resumes downloads. |
Unit |
setRequirements(requirements: Requirements!)Sets the requirements that need to be met for downloads to progress. |
Unit |
setStopReason(id: String?, stopReason: Int)Sets the stop reason for one or all downloads. |
Constants
DEFAULT_MAX_PARALLEL_DOWNLOADS
const val DEFAULT_MAX_PARALLEL_DOWNLOADS = 3: Int
The default maximum number of parallel downloads.
DEFAULT_MIN_RETRY_COUNT
const val DEFAULT_MIN_RETRY_COUNT = 5: Int
The default minimum number of times a download must be retried before failing.
DEFAULT_REQUIREMENTS
const val DEFAULT_REQUIREMENTS: Requirements!
The default requirement is that the device has network connectivity.
Public constructors
DownloadManager
DownloadManager(
context: Context!,
downloadIndex: WritableDownloadIndex!,
downloaderFactory: DownloaderFactory!
)
Constructs a DownloadManager.
| Parameters | |
|---|---|
context: Context! |
Any context. |
downloadIndex: WritableDownloadIndex! |
The download index used to hold the download information. |
downloaderFactory: DownloaderFactory! |
A factory for creating |
DownloadManager
DownloadManager(
context: Context!,
databaseProvider: DatabaseProvider!,
cache: Cache!,
upstreamFactory: DataSource.Factory!,
executor: Executor!
)
Constructs a DownloadManager.
| Parameters | |
|---|---|
context: Context! |
Any context. |
databaseProvider: DatabaseProvider! |
Provides the SQLite database in which downloads are persisted. |
cache: Cache! |
A cache to be used to store downloaded data. The cache should be configured with an |
upstreamFactory: DataSource.Factory! |
A |
executor: Executor! |
An |
Public functions
addDownload
fun addDownload(request: DownloadRequest!): Unit
Adds a download defined by the given request.
| Parameters | |
|---|---|
request: DownloadRequest! |
The download request. |
addDownload
fun addDownload(request: DownloadRequest!, stopReason: Int): Unit
Adds a download defined by the given request and with the specified stop reason.
| Parameters | |
|---|---|
request: DownloadRequest! |
The download request. |
stopReason: Int |
An initial stop reason for the download, or |
addListener
fun addListener(listener: DownloadManager.Listener!): Unit
Adds a Listener.
| Parameters | |
|---|---|
listener: DownloadManager.Listener! |
The listener to be added. |
getApplicationLooper
fun getApplicationLooper(): Looper!
Returns the Looper associated with the application thread that's used to access the manager, and on which the manager will call its Listeners.
getCurrentDownloads
fun getCurrentDownloads(): (Mutable)List<Download!>!
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 getDownloadIndex instead.
getRequirements
fun getRequirements(): Requirements!
Returns the requirements needed to be met to progress.
isIdle
fun isIdle(): Boolean
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).
isInitialized
fun isInitialized(): Boolean
Returns whether the manager has completed initialization.
isWaitingForRequirements
fun isWaitingForRequirements(): Boolean
Returns whether this manager has one or more downloads that are not progressing for the sole reason that the Requirements are not met. This is true if:
- The
Requirementsare not met. - The downloads are not paused (i.e.
getDownloadsPausedisfalse). - There are downloads in the
queued state.
pauseDownloads
fun pauseDownloads(): Unit
Pauses downloads. Downloads that would otherwise be making progress will transition to STATE_QUEUED.
release
fun release(): Unit
Stops the downloads and releases resources. Waits until the downloads are persisted to the download index. The manager must not be accessed after this method has been called.
removeAllDownloads
fun removeAllDownloads(): Unit
Cancels all pending downloads and removes all downloaded data.
removeDownload
fun removeDownload(id: String!): Unit
Cancels the download with the id and removes all downloaded data.
| Parameters | |
|---|---|
id: String! |
The unique content id of the download to be started. |
removeListener
fun removeListener(listener: DownloadManager.Listener!): Unit
Removes a Listener.
| Parameters | |
|---|---|
listener: DownloadManager.Listener! |
The listener to be removed. |
resumeDownloads
fun resumeDownloads(): Unit
Resumes downloads.
If the Requirements are met up to maxParallelDownloads will be started, excluding those with non-zero stopReasons.
setRequirements
fun setRequirements(requirements: Requirements!): Unit
Sets the requirements that need to be met for downloads to progress.
| Parameters | |
|---|---|
requirements: Requirements! |
A |
setStopReason
fun setStopReason(id: String?, stopReason: Int): Unit
Sets the stop reason for one or all downloads. To clear the stop reason, pass STOP_REASON_NONE.
| Parameters | |
|---|---|
id: String? |
The content id of the download to update, or |
stopReason: Int |
The stop reason, or |