diff --git a/app/src/main/java/com/github/libretube/enums/NotificationId.kt b/app/src/main/java/com/github/libretube/enums/NotificationId.kt new file mode 100644 index 000000000..bcf9b403b --- /dev/null +++ b/app/src/main/java/com/github/libretube/enums/NotificationId.kt @@ -0,0 +1,7 @@ +package com.github.libretube.enums + +enum class NotificationId(val id: Int) { + PLAYER_PLAYBACK(1), + DOWNLOAD_IN_PROGRESS(2), + ENQUEUE_PLAYLIST_DOWNLOAD(3) +} \ No newline at end of file diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index a6a01de4e..852f83f71 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -25,6 +25,7 @@ import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.obj.Download import com.github.libretube.db.obj.DownloadItem import com.github.libretube.enums.FileType +import com.github.libretube.enums.NotificationId import com.github.libretube.extensions.formatAsFileSize import com.github.libretube.extensions.getContentLength import com.github.libretube.extensions.parcelableExtra @@ -411,7 +412,7 @@ class DownloadService : LifecycleService() { .setOnlyAlertOnce(true) .setGroupSummary(true) - startForeground(DOWNLOAD_PROGRESS_NOTIFICATION_ID, summaryNotificationBuilder.build()) + startForeground(NotificationId.DOWNLOAD_IN_PROGRESS.id, summaryNotificationBuilder.build()) } private fun getNotificationBuilder(item: DownloadItem): NotificationCompat.Builder { @@ -534,7 +535,6 @@ class DownloadService : LifecycleService() { } companion object { - private const val DOWNLOAD_PROGRESS_NOTIFICATION_ID = 2 private const val DOWNLOAD_NOTIFICATION_GROUP = "download_notification_group" const val ACTION_SERVICE_STARTED = "com.github.libretube.services.DownloadService.ACTION_SERVICE_STARTED" diff --git a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt index dff8dbeeb..2cda51045 100644 --- a/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OfflinePlayerService.kt @@ -19,12 +19,12 @@ import com.github.libretube.constants.IntentData import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.obj.DownloadWithItems import com.github.libretube.enums.FileType +import com.github.libretube.enums.NotificationId import com.github.libretube.extensions.toAndroidUri import com.github.libretube.extensions.updateParameters import com.github.libretube.helpers.PlayerHelper import com.github.libretube.obj.PlayerNotificationData import com.github.libretube.util.NowPlayingNotification -import com.github.libretube.util.NowPlayingNotification.Companion.PLAYER_NOTIFICATION_ID import kotlin.io.path.exists import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -63,7 +63,7 @@ class OfflinePlayerService : LifecycleService() { .setSmallIcon(R.drawable.ic_launcher_lockscreen) .build() - startForeground(PLAYER_NOTIFICATION_ID, notification) + startForeground(NotificationId.PLAYER_PLAYBACK.id, notification) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { diff --git a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt index ad4fafde5..5817ce7af 100644 --- a/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt +++ b/app/src/main/java/com/github/libretube/services/OnlinePlayerService.kt @@ -30,6 +30,7 @@ import com.github.libretube.constants.IntentData import com.github.libretube.db.DatabaseHelper import com.github.libretube.db.DatabaseHolder.Database import com.github.libretube.db.obj.WatchPosition +import com.github.libretube.enums.NotificationId import com.github.libretube.extensions.parcelableExtra import com.github.libretube.extensions.setMetadata import com.github.libretube.extensions.toID @@ -40,7 +41,6 @@ import com.github.libretube.helpers.ProxyHelper import com.github.libretube.obj.PlayerNotificationData import com.github.libretube.parcelable.PlayerData import com.github.libretube.util.NowPlayingNotification -import com.github.libretube.util.NowPlayingNotification.Companion.PLAYER_NOTIFICATION_ID import com.github.libretube.util.PlayingQueue import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -159,7 +159,7 @@ class OnlinePlayerService : LifecycleService() { .setSmallIcon(R.drawable.ic_launcher_lockscreen) .build() - startForeground(PLAYER_NOTIFICATION_ID, notification) + startForeground(NotificationId.PLAYER_PLAYBACK.id, notification) } /** diff --git a/app/src/main/java/com/github/libretube/services/PlaylistDownloadEnqueueService.kt b/app/src/main/java/com/github/libretube/services/PlaylistDownloadEnqueueService.kt index 265956756..81b7129e8 100644 --- a/app/src/main/java/com/github/libretube/services/PlaylistDownloadEnqueueService.kt +++ b/app/src/main/java/com/github/libretube/services/PlaylistDownloadEnqueueService.kt @@ -15,6 +15,7 @@ import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.obj.PipedStream import com.github.libretube.api.obj.StreamItem import com.github.libretube.constants.IntentData +import com.github.libretube.enums.NotificationId import com.github.libretube.enums.PlaylistType import com.github.libretube.extensions.getWhileDigit import com.github.libretube.extensions.serializableExtra @@ -24,7 +25,6 @@ import com.github.libretube.helpers.DownloadHelper import com.github.libretube.parcelable.DownloadData import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext class PlaylistDownloadEnqueueService : LifecycleService() { private lateinit var nManager: NotificationManager @@ -42,7 +42,7 @@ class PlaylistDownloadEnqueueService : LifecycleService() { override fun onCreate() { super.onCreate() - startForeground(ENQUEUE_PROGRESS_NOTIFICATION_ID, buildNotification()) + startForeground(NotificationId.ENQUEUE_PLAYLIST_DOWNLOAD.id, buildNotification()) } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @@ -56,7 +56,7 @@ class PlaylistDownloadEnqueueService : LifecycleService() { captionLanguage = intent.getStringExtra(IntentData.captionLanguage) audioLanguage = intent.getStringExtra(IntentData.audioLanguage) - nManager.notify(ENQUEUE_PROGRESS_NOTIFICATION_ID, buildNotification()) + nManager.notify(NotificationId.ENQUEUE_PLAYLIST_DOWNLOAD.id, buildNotification()) lifecycleScope.launch(Dispatchers.IO) { if (playlistType != PlaylistType.PUBLIC) { @@ -82,7 +82,7 @@ class PlaylistDownloadEnqueueService : LifecycleService() { val playlist = try { PlaylistsHelper.getPlaylist(playlistId) } catch (e: Exception) { - toastFromMainDispatcher(e.localizedMessage) + toastFromMainDispatcher(e.localizedMessage.orEmpty()) stopSelf() return } @@ -94,7 +94,7 @@ class PlaylistDownloadEnqueueService : LifecycleService() { val playlist = try { RetrofitInstance.api.getPlaylist(playlistId) } catch (e: Exception) { - toastFromMainDispatcher(e.localizedMessage) + toastFromMainDispatcher(e.localizedMessage.orEmpty()) stopSelf() return } @@ -130,7 +130,7 @@ class PlaylistDownloadEnqueueService : LifecycleService() { } private suspend fun enqueueStreams(streams: List) { - nManager.notify(ENQUEUE_PROGRESS_NOTIFICATION_ID, buildNotification()) + nManager.notify(NotificationId.ENQUEUE_PLAYLIST_DOWNLOAD.id, buildNotification()) for (stream in streams) { val videoInfo = runCatching { @@ -157,7 +157,7 @@ class PlaylistDownloadEnqueueService : LifecycleService() { DownloadHelper.startDownloadService(this, downloadData) amountOfVideosDone++ - nManager.notify(ENQUEUE_PROGRESS_NOTIFICATION_ID, buildNotification()) + nManager.notify(NotificationId.ENQUEUE_PLAYLIST_DOWNLOAD.id, buildNotification()) } if (amountOfVideos == amountOfVideosDone) stopSelf() @@ -183,8 +183,4 @@ class PlaylistDownloadEnqueueService : LifecycleService() { super.onDestroy() } - - companion object { - private const val ENQUEUE_PROGRESS_NOTIFICATION_ID = 3 - } } \ No newline at end of file diff --git a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt index d65d05868..f740a5f59 100644 --- a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt +++ b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt @@ -24,6 +24,7 @@ import coil.request.ImageRequest import com.github.libretube.LibreTubeApp.Companion.PLAYER_CHANNEL_NAME import com.github.libretube.R import com.github.libretube.constants.IntentData +import com.github.libretube.enums.NotificationId import com.github.libretube.extensions.seekBy import com.github.libretube.extensions.toMediaMetadataCompat import com.github.libretube.helpers.BackgroundHelper @@ -370,7 +371,7 @@ class NowPlayingNotification( } .build() updateSessionMetadata() - nManager.notify(PLAYER_NOTIFICATION_ID, notification) + nManager.notify(NotificationId.PLAYER_PLAYBACK.id, notification) } private val notificationActionReceiver = object : BroadcastReceiver() { @@ -398,11 +399,11 @@ class NowPlayingNotification( context.unregisterReceiver(notificationActionReceiver) } - nManager.cancel(PLAYER_NOTIFICATION_ID) + nManager.cancel(NotificationId.PLAYER_PLAYBACK.id) } fun cancelNotification() { - nManager.cancel(PLAYER_NOTIFICATION_ID) + nManager.cancel(NotificationId.PLAYER_PLAYBACK.id) } fun refreshNotification() { @@ -410,7 +411,6 @@ class NowPlayingNotification( } companion object { - const val PLAYER_NOTIFICATION_ID = 1 private const val PREV = "prev" private const val NEXT = "next" private const val REWIND = "rewind"