forked from jellyfin/jellyfin-android
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move media and subtitle download to ExoPlayer Download Service
- Loading branch information
Emily Ehlert
committed
Jun 24, 2024
1 parent
fa9937c
commit d3b7ae5
Showing
15 changed files
with
432 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
app/src/main/java/org/jellyfin/mobile/downloads/DownloadProgress.kt
This file was deleted.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
app/src/main/java/org/jellyfin/mobile/downloads/DownloadService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.jellyfin.mobile.downloads | ||
|
||
import android.app.Notification | ||
import android.content.Context | ||
import com.google.android.exoplayer2.offline.Download | ||
import com.google.android.exoplayer2.offline.DownloadManager | ||
import com.google.android.exoplayer2.offline.DownloadService | ||
import com.google.android.exoplayer2.scheduler.PlatformScheduler | ||
import com.google.android.exoplayer2.scheduler.Scheduler | ||
import com.google.android.exoplayer2.ui.DownloadNotificationHelper | ||
import com.google.android.exoplayer2.util.NotificationUtil | ||
import com.google.android.exoplayer2.util.Util | ||
import org.jellyfin.mobile.R | ||
import org.jellyfin.mobile.utils.Constants | ||
|
||
class JellyfinDownloadService : DownloadService(Constants.DOWNLOAD_NOTIFICATION_ID, DEFAULT_FOREGROUND_NOTIFICATION_UPDATE_INTERVAL) { | ||
private val jobId = 1 | ||
|
||
override fun getDownloadManager(): DownloadManager { | ||
val downloadManager: DownloadManager = DownloadServiceUtil.getDownloadManager() | ||
val downloadNotificationHelper: DownloadNotificationHelper = | ||
DownloadServiceUtil.getDownloadNotificationHelper(this) | ||
downloadManager.addListener( | ||
TerminalStateNotificationHelper( | ||
this, downloadNotificationHelper, Constants.DOWNLOAD_NOTIFICATION_ID + 1, | ||
), | ||
) | ||
return downloadManager | ||
} | ||
|
||
override fun getScheduler(): Scheduler? { | ||
return if (Util.SDK_INT >= 21) PlatformScheduler(this, jobId) else null | ||
} | ||
|
||
override fun getForegroundNotification(downloads: MutableList<Download>, notMetRequirements: Int): Notification { | ||
return DownloadServiceUtil.getDownloadNotificationHelper(this) | ||
.buildProgressNotification( | ||
this, | ||
R.drawable.ic_notification, | ||
null, | ||
if (downloads.isEmpty()) null else Util.fromUtf8Bytes(downloads[0].request.data), | ||
downloads, | ||
notMetRequirements, | ||
) | ||
} | ||
|
||
private class TerminalStateNotificationHelper(context: Context, private val notificationHelper: DownloadNotificationHelper, private var nextNotificationId: Int) : DownloadManager.Listener { | ||
private val context: Context = context.applicationContext | ||
|
||
override fun onDownloadChanged( | ||
downloadManager: DownloadManager, download: Download, finalException: Exception?, | ||
) { | ||
if (download.request.data.isEmpty()) { | ||
// Do not display download complete notification for external subtitles | ||
// Can be identified by request data being empty | ||
return | ||
} | ||
val notification = when (download.state) { | ||
Download.STATE_COMPLETED -> { | ||
notificationHelper.buildDownloadCompletedNotification( | ||
context, | ||
R.drawable.ic_notification, | ||
null, | ||
Util.fromUtf8Bytes(download.request.data), | ||
) | ||
} | ||
Download.STATE_FAILED -> { | ||
notificationHelper.buildDownloadFailedNotification( | ||
context, | ||
R.drawable.ic_notification, | ||
null, | ||
Util.fromUtf8Bytes(download.request.data), | ||
) | ||
} | ||
else -> return | ||
} | ||
NotificationUtil.setNotification(context, nextNotificationId++, notification) | ||
} | ||
} | ||
} |
Oops, something went wrong.