Instantiating the OTVDownloadManager
The OTVDownloadManager
needs to be instantiated in the lifecycle of your application, for example, in the onCreate()
method of your main activity. Upon instantiation, the OTVDownloadManager
recovers the persistent database of previous download operations and makes them accessible to the application. However, operations cannot be triggered unless the application implements a listener for download events.
Implement a listener class (either an internal class within your activity or your class can implement the listener interface), instantiate it, and pass the object reference to your OTVDownloadManager
instance.
Setting the storage location for downloaded content
The SDK will choose by default to store all persistent offline data inside the application's data path under the offline sub-directory. The application can change the path by calling setStorage()
. However, this is not recommended, particularly for API level 29 or above, as the application and the SDK must be granted write permissions to the new path, should the path change.
If you need to set a different path, such as external storage, the storage’s root path may differ between devices. To fetch the root path, use Context.getExternalFilesDir()
for API level 29 - Android 10 or above, or Environment.getExternalStorageDirectory()
for older versions. Ensure the storage exists and permissions are granted using Environment.getExternalStorageState()
; see Android Developers’ Guide - Access from external storage.
Finding existing download items
Especially at the application start-up, it may be necessary to determine what downloads persist in the database. OTVDownloadManager
provides a method for this:
OTVDownloadItem[] items = mDlManager.getDownloadItems();
All download items are indexed by their UUID, assigned when a download is ‘added’. Most download operations are actioned by calling the OTVDownloadManager
instance’s API, with the UUID as a parameter. For example:
mDlManager.removeDownload(uuid);
For some operations, it is necessary to access the OTVDownloadItem
instance. A reference to such an instance is sometimes passed in the listener’s event methods but can also be fetched based on the UUID index:
OTVDownloadItem item = mDlManager.getDownloadByUUID(uuid);
The opposite can also be achieved - finding a download item’s UUID:
String uuid = dlItem.getUUID();
Recovering items in downloading progress
If the application terminates during the download process, items downloading remain at STATE_RUNNING
even though there is no longer an active thread progressing the download. When the application restarts, we recommend these items are brought to the paused state and let the users decide whether they wish to resume the download (with resumeDownload(uuid)
). For example, call the following just after creating the Download Manager and retrieving the items:
private void pauseRunningDownloads() {
for (OTVDownloadItem downloadItem : mDlManager.getDownloadItems()) {
if (downloadItem.getState() == OTVDownloadState.STATE_RUNNING) {
mDlManager.pauseDownload(downloadItem.getUUID());
}
}
}
Next step: Register the download