Skip to content

Commit

Permalink
[APT-9577] Add DRM for downloads for offline usage : catch errors hap…
Browse files Browse the repository at this point in the history
…pening in the download coroutines and dispatching the corresponding ErrorAction to notify the client app of the error
  • Loading branch information
rubeus90 committed Feb 14, 2024
1 parent f7bbb7a commit 894f316
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import com.scribd.armadillo.HeadersStore
import com.scribd.armadillo.StateStore
import com.scribd.armadillo.actions.ErrorAction
import com.scribd.armadillo.download.drm.OfflineDrmManager
import com.scribd.armadillo.error.ArmadilloException
import com.scribd.armadillo.error.DownloadServiceLaunchedInBackground
import com.scribd.armadillo.error.UnexpectedDownloadException
import com.scribd.armadillo.extensions.encodeInByteArray
import com.scribd.armadillo.extensions.toUri
import com.scribd.armadillo.hasSnowCone
import com.scribd.armadillo.models.AudioPlayable
import com.scribd.armadillo.playback.createRenderersFactory
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import java.io.IOException
Expand Down Expand Up @@ -53,10 +56,15 @@ internal class ExoplayerDownloadEngine @Inject constructor(
private val offlineDrmManager: OfflineDrmManager,
@Named(Constants.DI.GLOBAL_SCOPE) private val globalScope: CoroutineScope,
) : DownloadEngine {
override fun init() = downloadTracker.init()
private val errorHandler = CoroutineExceptionHandler { _, e ->
stateModifier.dispatch(ErrorAction(
error = e as? ArmadilloException ?: UnexpectedDownloadException(e)
))
}

override fun init() = downloadTracker.init()
override fun download(audioPlayable: AudioPlayable) {
globalScope.launch {
globalScope.launch(errorHandler) {
launch {
// Download DRM license for offline use
offlineDrmManager.downloadDrmLicenseForOffline(audioPlayable)
Expand Down Expand Up @@ -87,14 +95,14 @@ internal class ExoplayerDownloadEngine @Inject constructor(
}

override fun removeDownload(audioPlayable: AudioPlayable) {
globalScope.launch {
globalScope.launch(errorHandler) {
launch { downloadManager.removeDownload(audioPlayable.request.url) }
launch { offlineDrmManager.removeDownloadedDrmLicense(audioPlayable) }
}
}

override fun removeAllDownloads() {
globalScope.launch {
globalScope.launch(errorHandler) {
launch { downloadManager.removeAllDownloads() }
launch { offlineDrmManager.removeAllDownloadedDrmLicenses() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ data class DownloadServiceLaunchedInBackground(val id: Int) : ArmadilloException
override val errorCode = 304
}

data class UnexpectedDownloadException(val throwable: Throwable): ArmadilloException(exception = Exception(throwable)){
override val errorCode = 305
}

/**
* Misc Errors
*/
Expand Down

0 comments on commit 894f316

Please sign in to comment.