2022-08-07 21:52:40 +05:30
|
|
|
package com.github.libretube.util
|
|
|
|
|
2022-08-07 22:40:16 +05:30
|
|
|
import android.app.NotificationManager
|
2022-08-07 21:52:40 +05:30
|
|
|
import android.app.PendingIntent
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.graphics.Bitmap
|
2022-09-09 00:13:57 +05:30
|
|
|
import android.graphics.drawable.BitmapDrawable
|
2022-09-18 22:11:16 +05:30
|
|
|
import android.os.Build
|
2022-08-07 21:52:40 +05:30
|
|
|
import android.support.v4.media.session.MediaSessionCompat
|
2022-09-09 00:13:57 +05:30
|
|
|
import coil.request.ImageRequest
|
2022-09-20 23:23:34 +05:30
|
|
|
import com.github.libretube.ui.activities.MainActivity
|
2022-09-08 22:11:57 +05:30
|
|
|
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
|
|
|
|
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
|
2022-08-07 21:52:40 +05:30
|
|
|
import com.google.android.exoplayer2.ExoPlayer
|
|
|
|
import com.google.android.exoplayer2.Player
|
2022-08-07 22:31:03 +05:30
|
|
|
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
|
2022-08-07 21:52:40 +05:30
|
|
|
import com.google.android.exoplayer2.ui.PlayerNotificationManager
|
|
|
|
|
|
|
|
class NowPlayingNotification(
|
|
|
|
private val context: Context,
|
|
|
|
private val player: ExoPlayer
|
|
|
|
) {
|
2022-09-20 23:23:34 +05:30
|
|
|
private var streams: com.github.libretube.api.obj.Streams? = null
|
2022-08-07 21:52:40 +05:30
|
|
|
|
2022-08-07 22:31:03 +05:30
|
|
|
/**
|
|
|
|
* The [MediaSessionCompat] for the [streams].
|
|
|
|
*/
|
|
|
|
private lateinit var mediaSession: MediaSessionCompat
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The [MediaSessionConnector] to connect with the [mediaSession] and implement it with the [player].
|
|
|
|
*/
|
|
|
|
private lateinit var mediaSessionConnector: MediaSessionConnector
|
|
|
|
|
2022-08-07 21:52:40 +05:30
|
|
|
/**
|
|
|
|
* The [PlayerNotificationManager] to load the [mediaSession] content on it.
|
|
|
|
*/
|
|
|
|
private var playerNotification: PlayerNotificationManager? = null
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The [DescriptionAdapter] is used to show title, uploaderName and thumbnail of the video in the notification
|
|
|
|
* Basic example [here](https://github.com/AnthonyMarkD/AudioPlayerSampleTest)
|
|
|
|
*/
|
2022-09-09 00:13:57 +05:30
|
|
|
inner class DescriptionAdapter :
|
2022-08-07 21:52:40 +05:30
|
|
|
PlayerNotificationManager.MediaDescriptionAdapter {
|
|
|
|
/**
|
|
|
|
* sets the title of the notification
|
|
|
|
*/
|
|
|
|
override fun getCurrentContentTitle(player: Player): CharSequence {
|
|
|
|
// return controller.metadata.description.title.toString()
|
|
|
|
return streams?.title!!
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* overrides the action when clicking the notification
|
|
|
|
*/
|
|
|
|
override fun createCurrentContentIntent(player: Player): PendingIntent? {
|
|
|
|
// return controller.sessionActivity
|
|
|
|
/**
|
|
|
|
* starts a new MainActivity Intent when the player notification is clicked
|
|
|
|
* it doesn't start a completely new MainActivity because the MainActivity's launchMode
|
|
|
|
* is set to "singleTop" in the AndroidManifest (important!!!)
|
|
|
|
* that's the only way to launch back into the previous activity (e.g. the player view
|
|
|
|
*/
|
|
|
|
val intent = Intent(context, MainActivity::class.java)
|
|
|
|
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the description of the notification (below the title)
|
|
|
|
*/
|
|
|
|
override fun getCurrentContentText(player: Player): CharSequence? {
|
|
|
|
// return controller.metadata.description.subtitle.toString()
|
|
|
|
return streams?.uploader
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return the icon/thumbnail of the video
|
|
|
|
*/
|
|
|
|
override fun getCurrentLargeIcon(
|
|
|
|
player: Player,
|
|
|
|
callback: PlayerNotificationManager.BitmapCallback
|
|
|
|
): Bitmap? {
|
2022-09-18 22:11:16 +05:30
|
|
|
var bitmap: Bitmap? = null
|
2022-09-09 00:13:57 +05:30
|
|
|
var resizedBitmap: Bitmap? = null
|
2022-08-07 21:52:40 +05:30
|
|
|
|
2022-09-09 00:13:57 +05:30
|
|
|
val request = ImageRequest.Builder(context)
|
|
|
|
.data(streams?.thumbnailUrl)
|
|
|
|
.target { result ->
|
2022-09-18 22:11:16 +05:30
|
|
|
bitmap = (result as BitmapDrawable).bitmap
|
2022-09-09 00:13:57 +05:30
|
|
|
resizedBitmap = Bitmap.createScaledBitmap(
|
2022-09-18 22:11:16 +05:30
|
|
|
bitmap!!,
|
|
|
|
bitmap!!.width,
|
|
|
|
bitmap!!.width,
|
2022-09-09 00:13:57 +05:30
|
|
|
false
|
|
|
|
)
|
2022-08-07 21:52:40 +05:30
|
|
|
}
|
2022-09-09 00:13:57 +05:30
|
|
|
.build()
|
|
|
|
|
|
|
|
ImageHelper.imageLoader.enqueue(request)
|
|
|
|
|
|
|
|
// returns the scaled bitmap if it got fetched successfully
|
2022-09-18 22:11:16 +05:30
|
|
|
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) resizedBitmap else bitmap
|
2022-08-07 21:52:40 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 22:45:23 +05:30
|
|
|
/**
|
|
|
|
* Creates a [MediaSessionCompat] amd a [MediaSessionConnector] for the player
|
|
|
|
*/
|
2022-08-07 22:31:03 +05:30
|
|
|
private fun createMediaSession() {
|
|
|
|
if (this::mediaSession.isInitialized) return
|
|
|
|
mediaSession = MediaSessionCompat(context, this.javaClass.name)
|
|
|
|
mediaSession.isActive = true
|
|
|
|
|
|
|
|
mediaSessionConnector = MediaSessionConnector(mediaSession)
|
|
|
|
mediaSessionConnector.setPlayer(player)
|
|
|
|
}
|
|
|
|
|
2022-08-07 21:52:40 +05:30
|
|
|
/**
|
2022-08-07 22:45:23 +05:30
|
|
|
* Updates or creates the [playerNotification]
|
2022-08-07 21:52:40 +05:30
|
|
|
*/
|
2022-08-07 22:31:03 +05:30
|
|
|
fun updatePlayerNotification(
|
2022-09-20 23:23:34 +05:30
|
|
|
streams: com.github.libretube.api.obj.Streams
|
2022-08-07 21:52:40 +05:30
|
|
|
) {
|
|
|
|
this.streams = streams
|
2022-08-07 22:31:03 +05:30
|
|
|
if (playerNotification == null) {
|
|
|
|
createMediaSession()
|
|
|
|
createNotification()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 22:45:23 +05:30
|
|
|
/**
|
|
|
|
* Initializes the [playerNotification] attached to the [player] and shows it.
|
|
|
|
*/
|
2022-08-07 22:31:03 +05:30
|
|
|
private fun createNotification() {
|
2022-08-07 21:52:40 +05:30
|
|
|
playerNotification = PlayerNotificationManager
|
|
|
|
.Builder(context, PLAYER_NOTIFICATION_ID, BACKGROUND_CHANNEL_ID)
|
|
|
|
// set the description of the notification
|
|
|
|
.setMediaDescriptionAdapter(
|
|
|
|
DescriptionAdapter()
|
|
|
|
)
|
|
|
|
.build()
|
|
|
|
playerNotification?.apply {
|
|
|
|
setPlayer(player)
|
|
|
|
setUseNextAction(false)
|
|
|
|
setUsePreviousAction(false)
|
|
|
|
setUseStopAction(true)
|
|
|
|
setColorized(true)
|
|
|
|
setMediaSessionToken(mediaSession.sessionToken)
|
2022-08-23 19:29:14 +05:30
|
|
|
setUseFastForwardActionInCompactView(true)
|
|
|
|
setUseRewindActionInCompactView(true)
|
2022-08-07 21:52:40 +05:30
|
|
|
}
|
|
|
|
}
|
2022-08-07 22:40:16 +05:30
|
|
|
|
2022-08-07 22:45:23 +05:30
|
|
|
/**
|
|
|
|
* Destroy the [NowPlayingNotification]
|
|
|
|
*/
|
2022-08-07 22:40:16 +05:30
|
|
|
fun destroy() {
|
|
|
|
mediaSession.isActive = false
|
|
|
|
mediaSession.release()
|
|
|
|
mediaSessionConnector.setPlayer(null)
|
|
|
|
playerNotification?.setPlayer(null)
|
|
|
|
val notificationManager = context.getSystemService(
|
|
|
|
Context.NOTIFICATION_SERVICE
|
|
|
|
) as NotificationManager
|
|
|
|
notificationManager.cancel(PLAYER_NOTIFICATION_ID)
|
2022-09-20 01:13:13 +05:30
|
|
|
player.stop()
|
2022-08-07 22:40:16 +05:30
|
|
|
player.release()
|
|
|
|
}
|
2022-08-07 21:52:40 +05:30
|
|
|
}
|