Skip to content

Commit

Permalink
[APT-9577] Add DRM for downloads for offline usage : code improvement…
Browse files Browse the repository at this point in the history
…s : rename various params "audiobook" to "audioPlayable"
  • Loading branch information
rubeus90 committed Feb 13, 2024
1 parent 208aa3f commit f7bbb7a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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)))
}
Expand All @@ -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) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit f7bbb7a

Please sign in to comment.