DefaultDownloadIndex


@UnstableApi
public final class DefaultDownloadIndex implements WritableDownloadIndex


A DownloadIndex that uses SQLite to persist Downloads.

Summary

Public constructors

Creates an instance that stores the Downloads in an SQLite database provided by a DatabaseProvider.

DefaultDownloadIndex(DatabaseProvider databaseProvider, String name)

Creates an instance that stores the Downloads in an SQLite database provided by a DatabaseProvider.

Public methods

@Nullable Download

Returns the Download with the given id, or null.

DownloadCursor

Returns a DownloadCursor to Downloads with the given states.

void

Adds or replaces a Download.

void

Removes the download with the given ID.

void

Sets all STATE_DOWNLOADING states to STATE_QUEUED.

void

Sets all states to STATE_REMOVING.

void
setStopReason(int stopReason)

Sets the stop reason of the downloads in a terminal state (STATE_COMPLETED, STATE_FAILED).

void
setStopReason(String id, int stopReason)

Sets the stop reason of the download with the given ID in a terminal state (STATE_COMPLETED, STATE_FAILED).

Public constructors

DefaultDownloadIndex

public DefaultDownloadIndex(DatabaseProvider databaseProvider)

Creates an instance that stores the Downloads in an SQLite database provided by a DatabaseProvider.

Equivalent to calling DefaultDownloadIndex with name="".

Applications that only have one download index may use this constructor. Applications that have multiple download indices should call DefaultDownloadIndex to specify a unique name for each index.

Parameters
DatabaseProvider databaseProvider

Provides the SQLite database in which downloads are persisted.

DefaultDownloadIndex

public DefaultDownloadIndex(DatabaseProvider databaseProvider, String name)

Creates an instance that stores the Downloads in an SQLite database provided by a DatabaseProvider.

Parameters
DatabaseProvider databaseProvider

Provides the SQLite database in which downloads are persisted.

String name

The name of the index. This name is incorporated into the names of the SQLite tables in which downloads are persisted.

Public methods

getDownload

public @Nullable Download getDownload(String id)

Returns the Download with the given id, or null.

This method may be slow and shouldn't normally be called on the main thread.

Parameters
String id

ID of a Download.

Returns
@Nullable Download

The Download with the given id, or null if a download state with this id doesn't exist.

Throws
java.io.IOException

If an error occurs reading the state.

androidx.media3.database.DatabaseIOException

getDownloads

public DownloadCursor getDownloads(@Download.State int[] states)

Returns a DownloadCursor to Downloads with the given states.

This method may be slow and shouldn't normally be called on the main thread.

Parameters
@Download.State int[] states

Returns only the Downloads with this states. If empty, returns all.

Returns
DownloadCursor

A cursor to Downloads with the given states.

Throws
java.io.IOException

If an error occurs reading the state.

androidx.media3.database.DatabaseIOException

putDownload

public void putDownload(Download download)

Adds or replaces a Download.

This method may be slow and shouldn't normally be called on the main thread.

Parameters
Download download

The Download to be added.

Throws
java.io.IOException

If an error occurs setting the state.

androidx.media3.database.DatabaseIOException

removeDownload

public void removeDownload(String id)

Removes the download with the given ID. Does nothing if a download with the given ID does not exist.

This method may be slow and shouldn't normally be called on the main thread.

Parameters
String id

The ID of the download to remove.

Throws
java.io.IOException

If an error occurs removing the state.

androidx.media3.database.DatabaseIOException

setDownloadingStatesToQueued

public void setDownloadingStatesToQueued()

Sets all STATE_DOWNLOADING states to STATE_QUEUED.

This method may be slow and shouldn't normally be called on the main thread.

Throws
java.io.IOException

If an error occurs updating the state.

androidx.media3.database.DatabaseIOException

setStatesToRemoving

public void setStatesToRemoving()

Sets all states to STATE_REMOVING.

This method may be slow and shouldn't normally be called on the main thread.

Throws
java.io.IOException

If an error occurs updating the state.

androidx.media3.database.DatabaseIOException

setStopReason

public void setStopReason(int stopReason)

Sets the stop reason of the downloads in a terminal state (STATE_COMPLETED, STATE_FAILED).

This method may be slow and shouldn't normally be called on the main thread.

Parameters
int stopReason

The stop reason.

Throws
java.io.IOException

If an error occurs updating the state.

androidx.media3.database.DatabaseIOException

setStopReason

public void setStopReason(String id, int stopReason)

Sets the stop reason of the download with the given ID in a terminal state (STATE_COMPLETED, STATE_FAILED). Does nothing if a download with the given ID does not exist, or if it's not in a terminal state.

This method may be slow and shouldn't normally be called on the main thread.

Parameters
String id

The ID of the download to update.

int stopReason

The stop reason.

Throws
java.io.IOException

If an error occurs updating the state.

androidx.media3.database.DatabaseIOException