diff --git a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt index e6ee2dcd7..28fefb948 100644 --- a/app/src/main/java/com/github/libretube/services/BackgroundMode.kt +++ b/app/src/main/java/com/github/libretube/services/BackgroundMode.kt @@ -68,11 +68,11 @@ class BackgroundMode : Service() { */ private lateinit var nowPlayingNotification: NowPlayingNotification + /** + * Setting the required [notification] for running as a foreground service + */ override fun onCreate() { super.onCreate() - /** - * setting the required notification for running as a foreground service - */ if (Build.VERSION.SDK_INT >= 26) { val channelId = BACKGROUND_CHANNEL_ID val channel = NotificationChannel( @@ -169,11 +169,7 @@ class BackgroundMode : Service() { if (autoplay) playNextVideo() } Player.STATE_IDLE -> { - // called when the user pressed stop in the notification - // stop the service from being in the foreground and remove the notification - stopForeground(true) - // destroy the service - stopSelf() + onDestroy() } } } @@ -243,6 +239,19 @@ class BackgroundMode : Service() { } } + /** + * destroy the [BackgroundMode] foreground service + */ + override fun onDestroy() { + // called when the user pressed stop in the notification + // stop the service from being in the foreground and remove the notification + stopForeground(true) + // destroy the service + stopSelf() + if (this::nowPlayingNotification.isInitialized) nowPlayingNotification.destroy() + super.onDestroy() + } + override fun onBind(p0: Intent?): IBinder? { TODO("Not yet implemented") } 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 3959438a6..f0f52809c 100644 --- a/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt +++ b/app/src/main/java/com/github/libretube/util/NowPlayingNotification.kt @@ -117,6 +117,9 @@ class NowPlayingNotification( } } + /** + * Creates a [MediaSessionCompat] amd a [MediaSessionConnector] for the player + */ private fun createMediaSession() { if (this::mediaSession.isInitialized) return mediaSession = MediaSessionCompat(context, this.javaClass.name) @@ -127,7 +130,7 @@ class NowPlayingNotification( } /** - * Initializes the [playerNotification] attached to the [player] and shows it. + * Updates or creates the [playerNotification] */ fun updatePlayerNotification( streams: Streams @@ -139,6 +142,9 @@ class NowPlayingNotification( } } + /** + * Initializes the [playerNotification] attached to the [player] and shows it. + */ private fun createNotification() { playerNotification = PlayerNotificationManager .Builder(context, PLAYER_NOTIFICATION_ID, BACKGROUND_CHANNEL_ID) @@ -157,6 +163,9 @@ class NowPlayingNotification( } } + /** + * Destroy the [NowPlayingNotification] + */ fun destroy() { mediaSession.isActive = false mediaSession.release()