From f7bbb7a989b4e738828e82221032bd8079badb14 Mon Sep 17 00:00:00 2001 From: Hong Ngoc Nguyen Date: Tue, 13 Feb 2024 17:02:33 -0500 Subject: [PATCH] [APT-9577] Add DRM for downloads for offline usage : code improvements : rename various params "audiobook" to "audioPlayable" --- .../armadillo/download/DownloadEngine.kt | 20 +++++++++---------- .../download/drm/OfflineDrmManager.kt | 20 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/Armadillo/src/main/java/com/scribd/armadillo/download/DownloadEngine.kt b/Armadillo/src/main/java/com/scribd/armadillo/download/DownloadEngine.kt index 8753dbd..d028ea5 100644 --- a/Armadillo/src/main/java/com/scribd/armadillo/download/DownloadEngine.kt +++ b/Armadillo/src/main/java/com/scribd/armadillo/download/DownloadEngine.kt @@ -31,8 +31,8 @@ import javax.inject.Singleton internal interface DownloadEngine { fun init() - fun download(audiobook: AudioPlayable) - fun removeDownload(audiobook: AudioPlayable) + fun download(audioPlayable: AudioPlayable) + fun removeDownload(audioPlayable: AudioPlayable) fun removeAllDownloads() fun updateProgress() } @@ -55,24 +55,24 @@ internal class ExoplayerDownloadEngine @Inject constructor( ) : DownloadEngine { override fun init() = downloadTracker.init() - override fun download(audiobook: AudioPlayable) { + override fun download(audioPlayable: AudioPlayable) { globalScope.launch { launch { // Download DRM license for offline use - offlineDrmManager.downloadDrmLicenseForOffline(audiobook) + offlineDrmManager.downloadDrmLicenseForOffline(audioPlayable) } launch { - val downloadHelper = downloadHelper(context, audiobook.request) + val downloadHelper = downloadHelper(context, audioPlayable.request) downloadHelper.prepare(object : DownloadHelper.Callback { override fun onPrepared(helper: DownloadHelper) { - val request = helper.getDownloadRequest(audiobook.id.encodeInByteArray()) + val request = helper.getDownloadRequest(audioPlayable.id.encodeInByteArray()) try { startDownload(context, request) } catch (e: Exception) { if (hasSnowCone() && e is ForegroundServiceStartNotAllowedException) { - stateModifier.dispatch(ErrorAction(DownloadServiceLaunchedInBackground(audiobook.id))) + stateModifier.dispatch(ErrorAction(DownloadServiceLaunchedInBackground(audioPlayable.id))) } else { stateModifier.dispatch(ErrorAction(com.scribd.armadillo.error.ArmadilloIOException(e))) } @@ -86,10 +86,10 @@ internal class ExoplayerDownloadEngine @Inject constructor( } } - override fun removeDownload(audiobook: AudioPlayable) { + override fun removeDownload(audioPlayable: AudioPlayable) { globalScope.launch { - launch { downloadManager.removeDownload(audiobook.request.url) } - launch { offlineDrmManager.removeDownloadedDrmLicense(audiobook) } + launch { downloadManager.removeDownload(audioPlayable.request.url) } + launch { offlineDrmManager.removeDownloadedDrmLicense(audioPlayable) } } } diff --git a/Armadillo/src/main/java/com/scribd/armadillo/download/drm/OfflineDrmManager.kt b/Armadillo/src/main/java/com/scribd/armadillo/download/drm/OfflineDrmManager.kt index 7960d13..d480586 100644 --- a/Armadillo/src/main/java/com/scribd/armadillo/download/drm/OfflineDrmManager.kt +++ b/Armadillo/src/main/java/com/scribd/armadillo/download/drm/OfflineDrmManager.kt @@ -28,31 +28,31 @@ internal class OfflineDrmManager @Inject constructor( private const val TAG = "OfflineDrmManager" } - suspend fun downloadDrmLicenseForOffline(audiobook: AudioPlayable) { + suspend fun downloadDrmLicenseForOffline(audioPlayable: AudioPlayable) { withContext(Dispatchers.IO) { - audiobook.request.drmInfo?.let { drmInfo -> - val drmResult = when (@C.ContentType val type = Util.inferContentType(audiobook.request.url.toUri(), null)) { + audioPlayable.request.drmInfo?.let { drmInfo -> + val drmResult = when (@C.ContentType val type = Util.inferContentType(audioPlayable.request.url.toUri(), null)) { C.TYPE_DASH -> dashDrmLicenseDownloader else -> throw DrmContentTypeUnsupportedException(type) }.downloadDrmLicense( - requestUrl = audiobook.request.url, - customRequestHeaders = audiobook.request.headers, + requestUrl = audioPlayable.request.url, + customRequestHeaders = audioPlayable.request.headers, drmInfo = drmInfo, ) // Persist DRM result, which includes the key ID that can be used to retrieve the offline license - secureStorage.saveDrmDownload(context, audiobook.request.url, drmResult) + secureStorage.saveDrmDownload(context, audioPlayable.request.url, drmResult) Log.i(TAG, "DRM license ready for offline usage") } } } - suspend fun removeDownloadedDrmLicense(audiobook: AudioPlayable) { + suspend fun removeDownloadedDrmLicense(audioPlayable: AudioPlayable) { withContext(Dispatchers.IO) { - audiobook.request.drmInfo?.let { drmInfo -> - secureStorage.getDrmDownload(context, audiobook.request.url, drmInfo.drmType)?.let { drmDownload -> + audioPlayable.request.drmInfo?.let { drmInfo -> + secureStorage.getDrmDownload(context, audioPlayable.request.url, drmInfo.drmType)?.let { drmDownload -> // Remove the persisted download info immediately so audio playback would stop using the offline license - secureStorage.removeDrmDownload(context, audiobook.request.url, drmInfo.drmType) + secureStorage.removeDrmDownload(context, audioPlayable.request.url, drmInfo.drmType) // Release the DRM license when (val type = drmDownload.audioType) {