2022-08-07 21:52:40 +05:30
|
|
|
package com.github.libretube.util
|
|
|
|
|
2022-10-17 22:39:08 +05:30
|
|
|
import android.annotation.SuppressLint
|
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-10-22 21:07:49 +05:30
|
|
|
import android.graphics.BitmapFactory
|
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-10-23 15:23:28 +05:30
|
|
|
import android.os.Bundle
|
2022-10-22 20:41:56 +05:30
|
|
|
import android.support.v4.media.MediaDescriptionCompat
|
2022-10-23 15:23:28 +05:30
|
|
|
import android.support.v4.media.MediaMetadataCompat
|
2022-08-07 21:52:40 +05:30
|
|
|
import android.support.v4.media.session.MediaSessionCompat
|
2023-01-15 18:56:55 +05:30
|
|
|
import android.support.v4.media.session.PlaybackStateCompat
|
|
|
|
import androidx.annotation.DrawableRes
|
|
|
|
import androidx.core.app.NotificationCompat
|
2022-09-09 00:13:57 +05:30
|
|
|
import coil.request.ImageRequest
|
2022-10-22 21:07:49 +05:30
|
|
|
import com.github.libretube.R
|
2022-10-17 22:39:08 +05:30
|
|
|
import com.github.libretube.api.obj.Streams
|
2023-01-15 17:56:16 +05:30
|
|
|
import com.github.libretube.compat.PendingIntentCompat
|
2022-09-08 22:11:57 +05:30
|
|
|
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
|
2022-10-17 22:39:08 +05:30
|
|
|
import com.github.libretube.constants.IntentData
|
2022-09-08 22:11:57 +05:30
|
|
|
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
|
2022-09-20 23:30:51 +05:30
|
|
|
import com.github.libretube.ui.activities.MainActivity
|
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-10-22 20:41:56 +05:30
|
|
|
import com.google.android.exoplayer2.ext.mediasession.TimelineQueueNavigator
|
2022-08-07 21:52:40 +05:30
|
|
|
import com.google.android.exoplayer2.ui.PlayerNotificationManager
|
2023-01-15 18:56:55 +05:30
|
|
|
import com.google.android.exoplayer2.ui.PlayerNotificationManager.CustomActionReceiver
|
2022-08-07 21:52:40 +05:30
|
|
|
|
|
|
|
class NowPlayingNotification(
|
|
|
|
private val context: Context,
|
2022-10-17 22:39:08 +05:30
|
|
|
private val player: ExoPlayer,
|
|
|
|
private val isBackgroundPlayerNotification: Boolean
|
2022-08-07 21:52:40 +05:30
|
|
|
) {
|
2022-10-17 22:39:08 +05:30
|
|
|
private var videoId: String? = null
|
|
|
|
private var streams: 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
|
|
|
|
|
|
|
|
/**
|
2023-01-15 18:56:55 +05:30
|
|
|
* The [descriptionAdapter] is used to show title, uploaderName and thumbnail of the video in the notification
|
2022-08-07 21:52:40 +05:30
|
|
|
* Basic example [here](https://github.com/AnthonyMarkD/AudioPlayerSampleTest)
|
|
|
|
*/
|
2023-01-15 18:56:55 +05:30
|
|
|
private val descriptionAdapter = object : PlayerNotificationManager.MediaDescriptionAdapter {
|
2022-08-07 21:52:40 +05:30
|
|
|
/**
|
|
|
|
* sets the title of the notification
|
|
|
|
*/
|
|
|
|
override fun getCurrentContentTitle(player: Player): CharSequence {
|
|
|
|
return streams?.title!!
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* overrides the action when clicking the notification
|
|
|
|
*/
|
2022-10-17 22:39:08 +05:30
|
|
|
@SuppressLint("UnspecifiedImmutableFlag")
|
2022-08-07 21:52:40 +05:30
|
|
|
override fun createCurrentContentIntent(player: Player): PendingIntent? {
|
2022-10-17 22:39:08 +05:30
|
|
|
// 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!!!)
|
2023-01-15 17:56:16 +05:30
|
|
|
// that's the only way to launch back into the previous activity (e.g. the player view
|
2022-10-17 22:39:08 +05:30
|
|
|
val intent = Intent(context, MainActivity::class.java).apply {
|
|
|
|
if (isBackgroundPlayerNotification) {
|
2023-01-13 22:50:00 +05:30
|
|
|
putExtra(IntentData.openAudioPlayer, true)
|
2022-10-17 22:39:08 +05:30
|
|
|
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
|
|
|
}
|
|
|
|
}
|
2023-01-15 17:56:16 +05:30
|
|
|
return PendingIntent.getActivity(
|
|
|
|
context,
|
|
|
|
0,
|
|
|
|
intent,
|
|
|
|
PendingIntentCompat.updateCurrentFlags
|
|
|
|
)
|
2022-08-07 21:52:40 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the description of the notification (below the title)
|
|
|
|
*/
|
|
|
|
override fun getCurrentContentText(player: Player): CharSequence? {
|
|
|
|
return streams?.uploader
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return the icon/thumbnail of the video
|
|
|
|
*/
|
|
|
|
override fun getCurrentLargeIcon(
|
|
|
|
player: Player,
|
|
|
|
callback: PlayerNotificationManager.BitmapCallback
|
|
|
|
): Bitmap? {
|
2022-12-25 20:38:52 +05:30
|
|
|
if (DataSaverMode.isEnabled(context)) return null
|
2022-12-12 23:25:54 +05:30
|
|
|
|
2022-09-18 22:11:16 +05:30
|
|
|
var bitmap: 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 ->
|
2023-01-16 19:14:37 +05:30
|
|
|
val bm = (result as BitmapDrawable).bitmap
|
|
|
|
// returns the bitmap on Android 13+, for everything below scaled down to a square
|
|
|
|
bitmap = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
|
|
|
|
ImageHelper.getSquareBitmap(bm)
|
|
|
|
} else bm
|
|
|
|
callback.onBitmap(bitmap!!)
|
2022-08-07 21:52:40 +05:30
|
|
|
}
|
2022-09-09 00:13:57 +05:30
|
|
|
.build()
|
|
|
|
|
2023-01-16 19:14:37 +05:30
|
|
|
// enqueue the thumbnail loading request
|
2022-09-09 00:13:57 +05:30
|
|
|
ImageHelper.imageLoader.enqueue(request)
|
|
|
|
|
2023-01-16 19:14:37 +05:30
|
|
|
return bitmap
|
2022-08-07 21:52:40 +05:30
|
|
|
}
|
2023-01-16 19:03:38 +05:30
|
|
|
|
|
|
|
override fun getCurrentSubText(player: Player): CharSequence? {
|
|
|
|
return streams?.uploader
|
|
|
|
}
|
2022-08-07 21:52:40 +05:30
|
|
|
}
|
|
|
|
|
2023-01-15 18:56:55 +05:30
|
|
|
private val customActionReceiver = object : CustomActionReceiver {
|
|
|
|
override fun createCustomActions(
|
|
|
|
context: Context,
|
|
|
|
instanceId: Int
|
|
|
|
): MutableMap<String, NotificationCompat.Action> {
|
|
|
|
return mutableMapOf(
|
2023-01-16 18:53:36 +05:30
|
|
|
PREV to createNotificationAction(R.drawable.ic_prev_outlined, PREV, instanceId),
|
|
|
|
NEXT to createNotificationAction(R.drawable.ic_next_outlined, NEXT, instanceId),
|
2023-01-16 19:00:48 +05:30
|
|
|
REWIND to createNotificationAction(R.drawable.ic_rewind_md, REWIND, instanceId),
|
|
|
|
FORWARD to createNotificationAction(R.drawable.ic_forward_md, FORWARD, instanceId)
|
2023-01-15 18:56:55 +05:30
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getCustomActions(player: Player): MutableList<String> {
|
|
|
|
return mutableListOf(PREV, NEXT, REWIND, FORWARD)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCustomAction(player: Player, action: String, intent: Intent) {
|
|
|
|
handlePlayerAction(action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun createNotificationAction(drawableRes: Int, actionName: String, instanceId: Int): NotificationCompat.Action {
|
|
|
|
val intent: Intent = Intent(actionName).setPackage(context.packageName)
|
|
|
|
val pendingIntent = PendingIntent.getBroadcast(
|
|
|
|
context,
|
|
|
|
instanceId,
|
|
|
|
intent,
|
|
|
|
PendingIntentCompat.cancelCurrentFlags
|
|
|
|
)
|
|
|
|
return NotificationCompat.Action.Builder(drawableRes, actionName, pendingIntent).build()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun createMediaSessionAction(@DrawableRes drawableRes: Int, actionName: String): MediaSessionConnector.CustomActionProvider {
|
|
|
|
return object : MediaSessionConnector.CustomActionProvider {
|
|
|
|
override fun getCustomAction(player: Player): PlaybackStateCompat.CustomAction? {
|
|
|
|
return PlaybackStateCompat.CustomAction.Builder(actionName, actionName, drawableRes).build()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCustomAction(player: Player, action: String, extras: Bundle?) {
|
|
|
|
handlePlayerAction(action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2023-01-16 18:53:36 +05:30
|
|
|
mediaSession = MediaSessionCompat(context, this.javaClass.name).apply {
|
|
|
|
isActive = true
|
|
|
|
}
|
|
|
|
|
|
|
|
mediaSessionConnector = MediaSessionConnector(mediaSession).apply {
|
|
|
|
setPlayer(player)
|
|
|
|
setQueueNavigator(object : TimelineQueueNavigator(mediaSession) {
|
|
|
|
override fun getMediaDescription(
|
|
|
|
player: Player,
|
|
|
|
windowIndex: Int
|
|
|
|
): MediaDescriptionCompat {
|
|
|
|
return MediaDescriptionCompat.Builder().apply {
|
|
|
|
setTitle(streams?.title!!)
|
|
|
|
setSubtitle(streams?.uploader)
|
|
|
|
val appIcon = BitmapFactory.decodeResource(
|
|
|
|
context.resources,
|
|
|
|
R.drawable.ic_launcher_monochrome
|
|
|
|
)
|
|
|
|
val extras = Bundle().apply {
|
|
|
|
putParcelable(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, appIcon)
|
|
|
|
putString(MediaMetadataCompat.METADATA_KEY_TITLE, streams?.title!!)
|
|
|
|
putString(MediaMetadataCompat.METADATA_KEY_ARTIST, streams?.uploader)
|
|
|
|
}
|
|
|
|
setIconBitmap(appIcon)
|
|
|
|
setExtras(extras)
|
|
|
|
}.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getSupportedQueueNavigatorActions(player: Player): Long {
|
|
|
|
return PlaybackStateCompat.ACTION_PLAY_PAUSE
|
|
|
|
}
|
|
|
|
})
|
|
|
|
setCustomActionProviders(
|
|
|
|
createMediaSessionAction(R.drawable.ic_prev_outlined, PREV),
|
|
|
|
createMediaSessionAction(R.drawable.ic_next_outlined, NEXT),
|
2023-01-16 19:00:48 +05:30
|
|
|
createMediaSessionAction(R.drawable.ic_rewind_md, REWIND),
|
|
|
|
createMediaSessionAction(R.drawable.ic_forward_md, FORWARD)
|
2023-01-16 18:53:36 +05:30
|
|
|
)
|
|
|
|
}
|
2022-08-07 22:31:03 +05:30
|
|
|
}
|
|
|
|
|
2023-01-15 18:56:55 +05:30
|
|
|
private fun handlePlayerAction(action: String) {
|
|
|
|
when (action) {
|
|
|
|
NEXT -> {
|
2023-01-16 18:53:36 +05:30
|
|
|
if (PlayingQueue.hasNext()) {
|
|
|
|
PlayingQueue.onQueueItemSelected(
|
|
|
|
PlayingQueue.currentIndex() + 1
|
|
|
|
)
|
|
|
|
}
|
2023-01-15 18:56:55 +05:30
|
|
|
}
|
|
|
|
PREV -> {
|
2023-01-16 18:53:36 +05:30
|
|
|
if (PlayingQueue.hasPrev()) {
|
|
|
|
PlayingQueue.onQueueItemSelected(
|
|
|
|
PlayingQueue.currentIndex() - 1
|
|
|
|
)
|
|
|
|
}
|
2023-01-15 18:56:55 +05:30
|
|
|
}
|
|
|
|
REWIND -> {
|
|
|
|
player.seekTo(player.currentPosition - PlayerHelper.seekIncrement)
|
|
|
|
}
|
|
|
|
FORWARD -> {
|
|
|
|
player.seekTo(player.currentPosition + PlayerHelper.seekIncrement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-17 22:39:08 +05:30
|
|
|
videoId: String,
|
|
|
|
streams: Streams
|
2022-08-07 21:52:40 +05:30
|
|
|
) {
|
2022-10-17 22:39:08 +05:30
|
|
|
this.videoId = videoId
|
2022-08-07 21:52:40 +05:30
|
|
|
this.streams = streams
|
2022-10-17 22:39:08 +05:30
|
|
|
|
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
|
2023-01-15 18:56:55 +05:30
|
|
|
.setMediaDescriptionAdapter(descriptionAdapter)
|
|
|
|
// register the receiver for custom actions, doesn't seem to change anything
|
|
|
|
.setCustomActionReceiver(customActionReceiver)
|
|
|
|
.build().apply {
|
|
|
|
setPlayer(player)
|
|
|
|
setColorized(true)
|
|
|
|
setMediaSessionToken(mediaSession.sessionToken)
|
|
|
|
setSmallIcon(R.drawable.ic_launcher_lockscreen)
|
|
|
|
setUseNextAction(false)
|
|
|
|
setUsePreviousAction(false)
|
|
|
|
setUseStopAction(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-10-18 23:10:28 +05:30
|
|
|
fun destroySelfAndPlayer() {
|
2022-11-19 19:48:05 +05:30
|
|
|
playerNotification?.setPlayer(null)
|
|
|
|
|
2022-08-07 22:40:16 +05:30
|
|
|
mediaSession.isActive = false
|
|
|
|
mediaSession.release()
|
2022-11-19 19:48:05 +05:30
|
|
|
|
|
|
|
player.stop()
|
|
|
|
player.release()
|
2022-10-17 22:39:08 +05:30
|
|
|
|
2022-08-07 22:40:16 +05:30
|
|
|
val notificationManager = context.getSystemService(
|
|
|
|
Context.NOTIFICATION_SERVICE
|
|
|
|
) as NotificationManager
|
|
|
|
notificationManager.cancel(PLAYER_NOTIFICATION_ID)
|
|
|
|
}
|
2023-01-15 18:56:55 +05:30
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val PREV = "prev"
|
|
|
|
private const val NEXT = "next"
|
|
|
|
private const val REWIND = "rewind"
|
|
|
|
private const val FORWARD = "forward"
|
|
|
|
}
|
2022-08-07 21:52:40 +05:30
|
|
|
}
|