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

Listener for DownloadManager events.

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(
    context: Context!,
    downloadIndex: WritableDownloadIndex!,
    downloaderFactory: DownloaderFactory!
)

Constructs a DownloadManager.

DownloadManager(
    context: Context!,
    databaseProvider: DatabaseProvider!,
    cache: Cache!,
    upstreamFactory: DataSource.Factory!,
    executor: Executor!
)

Constructs a DownloadManager.

Public functions

Unit

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

Adds a Listener.

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.

(Mutable)List<Download!>!

Returns current downloads.

DownloadIndex!

Returns the used DownloadIndex.

Requirements!

Returns the requirements needed to be met to progress.

Boolean

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 Requirements are not met.

Unit

Pauses downloads.

Unit

Stops the downloads and releases resources.

Unit

Cancels all pending downloads and removes all downloaded data.

Unit

Cancels the download with the id and removes all downloaded data.

Unit

Removes a Listener.

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_REQUIREMENTSRequirements!

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 Downloaders.

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 CacheEvictor that will not evict downloaded content, for example .

upstreamFactory: DataSource.Factory!

A Factory for creating DataSources for downloading data.

executor: Executor!

An Executor used to download data. Passing Runnable::run will cause each download task to download data on its own thread. Passing an Executor that uses multiple threads will speed up download tasks that can be split into smaller parts for parallel execution.

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 STOP_REASON_NONE if the download should be started.

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.

getDownloadIndex

fun getDownloadIndex(): DownloadIndex!

Returns the used DownloadIndex.

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:

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 Requirements.

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 null to set the stop reason for all downloads.

stopReason: Int

The stop reason, or STOP_REASON_NONE.

Public properties

downloadsPaused

val downloadsPausedBoolean

maxParallelDownloads

var maxParallelDownloadsInt

minRetryCount

var minRetryCountInt

notMetRequirements

val notMetRequirementsInt