add documentation

This commit is contained in:
Bnyro 2022-08-07 19:15:23 +02:00
parent 955d6de690
commit 6945f29282
2 changed files with 27 additions and 9 deletions

View File

@ -68,11 +68,11 @@ class BackgroundMode : Service() {
*/ */
private lateinit var nowPlayingNotification: NowPlayingNotification private lateinit var nowPlayingNotification: NowPlayingNotification
/**
* Setting the required [notification] for running as a foreground service
*/
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
/**
* setting the required notification for running as a foreground service
*/
if (Build.VERSION.SDK_INT >= 26) { if (Build.VERSION.SDK_INT >= 26) {
val channelId = BACKGROUND_CHANNEL_ID val channelId = BACKGROUND_CHANNEL_ID
val channel = NotificationChannel( val channel = NotificationChannel(
@ -169,11 +169,7 @@ class BackgroundMode : Service() {
if (autoplay) playNextVideo() if (autoplay) playNextVideo()
} }
Player.STATE_IDLE -> { Player.STATE_IDLE -> {
// called when the user pressed stop in the notification onDestroy()
// stop the service from being in the foreground and remove the notification
stopForeground(true)
// destroy the service
stopSelf()
} }
} }
} }
@ -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? { override fun onBind(p0: Intent?): IBinder? {
TODO("Not yet implemented") TODO("Not yet implemented")
} }

View File

@ -117,6 +117,9 @@ class NowPlayingNotification(
} }
} }
/**
* Creates a [MediaSessionCompat] amd a [MediaSessionConnector] for the player
*/
private fun createMediaSession() { private fun createMediaSession() {
if (this::mediaSession.isInitialized) return if (this::mediaSession.isInitialized) return
mediaSession = MediaSessionCompat(context, this.javaClass.name) 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( fun updatePlayerNotification(
streams: Streams streams: Streams
@ -139,6 +142,9 @@ class NowPlayingNotification(
} }
} }
/**
* Initializes the [playerNotification] attached to the [player] and shows it.
*/
private fun createNotification() { private fun createNotification() {
playerNotification = PlayerNotificationManager playerNotification = PlayerNotificationManager
.Builder(context, PLAYER_NOTIFICATION_ID, BACKGROUND_CHANNEL_ID) .Builder(context, PLAYER_NOTIFICATION_ID, BACKGROUND_CHANNEL_ID)
@ -157,6 +163,9 @@ class NowPlayingNotification(
} }
} }
/**
* Destroy the [NowPlayingNotification]
*/
fun destroy() { fun destroy() {
mediaSession.isActive = false mediaSession.isActive = false
mediaSession.release() mediaSession.release()