mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 14:20:30 +05:30
Merge pull request #3813 from Bnyro/independent-notification
Replace the `PlayerNotificationManager` with a custom implementation
This commit is contained in:
commit
31e5d2bcc2
@ -1,14 +1,18 @@
|
||||
package com.github.libretube.util
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.graphics.Bitmap
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.support.v4.media.session.MediaSessionCompat
|
||||
import android.util.Log
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.PendingIntentCompat
|
||||
@ -19,14 +23,15 @@ import androidx.media3.common.Player
|
||||
import androidx.media3.exoplayer.ExoPlayer
|
||||
import androidx.media3.session.CommandButton
|
||||
import androidx.media3.session.MediaSession
|
||||
import androidx.media3.session.MediaStyleNotificationHelper
|
||||
import androidx.media3.session.SessionCommand
|
||||
import androidx.media3.session.SessionResult
|
||||
import androidx.media3.ui.PlayerNotificationManager
|
||||
import coil.request.ImageRequest
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
|
||||
import com.github.libretube.constants.IntentData
|
||||
import com.github.libretube.constants.PLAYER_NOTIFICATION_ID
|
||||
import com.github.libretube.helpers.BackgroundHelper
|
||||
import com.github.libretube.helpers.ImageHelper
|
||||
import com.github.libretube.helpers.PlayerHelper
|
||||
import com.github.libretube.obj.PlayerNotificationData
|
||||
@ -40,8 +45,12 @@ class NowPlayingNotification(
|
||||
private val isBackgroundPlayerNotification: Boolean,
|
||||
) {
|
||||
private var videoId: String? = null
|
||||
private val nManager = context.getSystemService<NotificationManager>()!!
|
||||
|
||||
/**
|
||||
* The metadata of the current playing song (thumbnail, title, uploader)
|
||||
*/
|
||||
private var notificationData: PlayerNotificationData? = null
|
||||
private var bitmap: Bitmap? = null
|
||||
|
||||
/**
|
||||
* The [MediaSessionCompat] for the [notificationData].
|
||||
@ -49,72 +58,49 @@ class NowPlayingNotification(
|
||||
private lateinit var mediaSession: MediaSession
|
||||
|
||||
/**
|
||||
* The [PlayerNotificationManager] to load the [mediaSession] content on it.
|
||||
* The [NotificationCompat.Builder] to load the [mediaSession] content on it.
|
||||
*/
|
||||
private var playerNotification: PlayerNotificationManager? = null
|
||||
private var notificationBuilder: NotificationCompat.Builder? = 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)
|
||||
* The [Bitmap] which represents the background / thumbnail of the notification
|
||||
*/
|
||||
private val descriptionAdapter = object : PlayerNotificationManager.MediaDescriptionAdapter {
|
||||
/**
|
||||
* sets the title of the notification
|
||||
*/
|
||||
override fun getCurrentContentTitle(player: Player): CharSequence {
|
||||
return notificationData?.title.orEmpty()
|
||||
}
|
||||
private var notificationBitmap: Bitmap? = null
|
||||
|
||||
/**
|
||||
* overrides the action when clicking the notification
|
||||
*/
|
||||
override fun createCurrentContentIntent(player: Player): PendingIntent {
|
||||
// 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).apply {
|
||||
if (isBackgroundPlayerNotification) {
|
||||
putExtra(IntentData.openAudioPlayer, true)
|
||||
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
||||
}
|
||||
}
|
||||
return PendingIntentCompat.getActivity(context, 0, intent, FLAG_UPDATE_CURRENT, false)
|
||||
}
|
||||
|
||||
/**
|
||||
* the description of the notification (below the title)
|
||||
*/
|
||||
override fun getCurrentContentText(player: Player): CharSequence? {
|
||||
return notificationData?.uploaderName
|
||||
}
|
||||
|
||||
/**
|
||||
* return the icon/thumbnail of the video
|
||||
*/
|
||||
override fun getCurrentLargeIcon(
|
||||
player: Player,
|
||||
callback: PlayerNotificationManager.BitmapCallback,
|
||||
): Bitmap? {
|
||||
// On Android 13 and up, the metadata is responsible for the thumbnail
|
||||
if (DataSaverMode.isEnabled(context) ||
|
||||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) return null
|
||||
if (bitmap == null) enqueueThumbnailRequest(callback)
|
||||
return bitmap
|
||||
}
|
||||
|
||||
override fun getCurrentSubText(player: Player): CharSequence? {
|
||||
return notificationData?.uploaderName
|
||||
private fun loadCurrentLargeIcon() {
|
||||
if (DataSaverMode.isEnabled(context)) return
|
||||
if (notificationBitmap == null) enqueueThumbnailRequest {
|
||||
createOrUpdateNotification()
|
||||
}
|
||||
}
|
||||
|
||||
private fun enqueueThumbnailRequest(callback: PlayerNotificationManager.BitmapCallback) {
|
||||
private fun createCurrentContentIntent(): PendingIntent {
|
||||
// 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).apply {
|
||||
if (isBackgroundPlayerNotification) {
|
||||
putExtra(IntentData.openAudioPlayer, true)
|
||||
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
||||
}
|
||||
}
|
||||
return PendingIntentCompat.getActivity(context, 0, intent, FLAG_UPDATE_CURRENT, false)
|
||||
}
|
||||
|
||||
private fun createDeleteIntent(): PendingIntent {
|
||||
val intent = Intent(STOP).setPackage(context.packageName)
|
||||
return PendingIntentCompat
|
||||
.getBroadcast(context, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT, false)
|
||||
}
|
||||
|
||||
private fun enqueueThumbnailRequest(callback: (Bitmap) -> Unit) {
|
||||
// If playing a downloaded file, show the downloaded thumbnail instead of loading an
|
||||
// online image
|
||||
notificationData?.thumbnailPath?.let { path ->
|
||||
ImageHelper.getDownloadedImage(context, path)?.let {
|
||||
bitmap = ImageHelper.getSquareBitmap(it)
|
||||
callback.onBitmap(bitmap!!)
|
||||
notificationBitmap = processBitmap(it)
|
||||
callback.invoke(notificationBitmap!!)
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -122,8 +108,8 @@ class NowPlayingNotification(
|
||||
val request = ImageRequest.Builder(context)
|
||||
.data(notificationData?.thumbnailUrl)
|
||||
.target {
|
||||
bitmap = ImageHelper.getSquareBitmap(it.toBitmap())
|
||||
callback.onBitmap(bitmap!!)
|
||||
notificationBitmap = processBitmap(it.toBitmap())
|
||||
callback.invoke(notificationBitmap!!)
|
||||
}
|
||||
.build()
|
||||
|
||||
@ -131,36 +117,31 @@ class NowPlayingNotification(
|
||||
ImageHelper.imageLoader.enqueue(request)
|
||||
}
|
||||
|
||||
private val customActionReceiver = object : PlayerNotificationManager.CustomActionReceiver {
|
||||
override fun createCustomActions(
|
||||
context: Context,
|
||||
instanceId: Int,
|
||||
): MutableMap<String, NotificationCompat.Action> {
|
||||
return mutableMapOf(
|
||||
PREV to createNotificationAction(R.drawable.ic_prev_outlined, PREV, instanceId),
|
||||
NEXT to createNotificationAction(R.drawable.ic_next_outlined, NEXT, instanceId),
|
||||
REWIND to createNotificationAction(R.drawable.ic_rewind_md, REWIND, instanceId),
|
||||
FORWARD to createNotificationAction(R.drawable.ic_forward_md, FORWARD, instanceId),
|
||||
)
|
||||
}
|
||||
|
||||
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 processBitmap(bitmap: Bitmap): Bitmap {
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
bitmap
|
||||
} else ImageHelper.getSquareBitmap(bitmap)
|
||||
}
|
||||
|
||||
private val legacyNotificationButtons
|
||||
get() = listOf(
|
||||
createNotificationAction(R.drawable.ic_prev_outlined, PREV),
|
||||
createNotificationAction(
|
||||
if (player.isPlaying) R.drawable.ic_pause else R.drawable.ic_play,
|
||||
PLAY_PAUSE
|
||||
),
|
||||
createNotificationAction(R.drawable.ic_next_outlined, NEXT),
|
||||
createNotificationAction(R.drawable.ic_rewind_md, REWIND),
|
||||
createNotificationAction(R.drawable.ic_forward_md, FORWARD),
|
||||
)
|
||||
|
||||
private fun createNotificationAction(
|
||||
drawableRes: Int,
|
||||
actionName: String,
|
||||
instanceId: Int,
|
||||
actionName: String
|
||||
): NotificationCompat.Action {
|
||||
val intent = Intent(actionName).setPackage(context.packageName)
|
||||
val pendingIntent = PendingIntentCompat
|
||||
.getBroadcast(context, instanceId, intent, PendingIntent.FLAG_CANCEL_CURRENT, false)
|
||||
.getBroadcast(context, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT, false)
|
||||
return NotificationCompat.Action.Builder(drawableRes, actionName, pendingIntent).build()
|
||||
}
|
||||
|
||||
@ -188,11 +169,16 @@ class NowPlayingNotification(
|
||||
): MediaSession.ConnectionResult {
|
||||
val connectionResult = super.onConnect(session, controller)
|
||||
val availableSessionCommands = connectionResult.availableSessionCommands.buildUpon()
|
||||
val availablePlayerCommands = connectionResult.availablePlayerCommands // Player.Commands.Builder().add(Player.COMMAND_PLAY_PAUSE).build()
|
||||
val availablePlayerCommands =
|
||||
connectionResult.availablePlayerCommands // Player.Commands.Builder().add(Player.COMMAND_PLAY_PAUSE).build()
|
||||
getCustomActions().forEach { button ->
|
||||
button.sessionCommand?.let { availableSessionCommands.add(it) }
|
||||
}
|
||||
session.setAvailableCommands(controller, availableSessionCommands.build(), availablePlayerCommands)
|
||||
session.setAvailableCommands(
|
||||
controller,
|
||||
availableSessionCommands.build(),
|
||||
availablePlayerCommands
|
||||
)
|
||||
return MediaSession.ConnectionResult.accept(
|
||||
availableSessionCommands.build(),
|
||||
availablePlayerCommands,
|
||||
@ -268,11 +254,22 @@ class NowPlayingNotification(
|
||||
FORWARD -> {
|
||||
player.seekTo(player.currentPosition + PlayerHelper.seekIncrement)
|
||||
}
|
||||
|
||||
PLAY_PAUSE -> {
|
||||
if (player.isPlaying) player.pause() else player.play()
|
||||
}
|
||||
|
||||
STOP -> {
|
||||
Log.e("stop", "stop")
|
||||
if (isBackgroundPlayerNotification) {
|
||||
BackgroundHelper.stopBackgroundPlay(context)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates or creates the [playerNotification]
|
||||
* Updates or creates the [notificationBuilder]
|
||||
*/
|
||||
fun updatePlayerNotification(
|
||||
videoId: String,
|
||||
@ -281,49 +278,82 @@ class NowPlayingNotification(
|
||||
this.videoId = videoId
|
||||
this.notificationData = data
|
||||
// reset the thumbnail bitmap in order to become reloaded for the new video
|
||||
this.bitmap = null
|
||||
this.notificationBitmap = null
|
||||
|
||||
if (playerNotification == null) {
|
||||
loadCurrentLargeIcon()
|
||||
|
||||
if (notificationBuilder == null) {
|
||||
createMediaSession()
|
||||
createNotification()
|
||||
createNotificationBuilder()
|
||||
createActionReceiver()
|
||||
// update the notification each time the player continues playing or pauses
|
||||
player.addListener(object : Player.Listener {
|
||||
override fun onIsPlayingChanged(isPlaying: Boolean) {
|
||||
createOrUpdateNotification()
|
||||
super.onIsPlayingChanged(isPlaying)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
createOrUpdateNotification()
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the [playerNotification] attached to the [player] and shows it.
|
||||
* Initializes the [notificationBuilder] attached to the [player] and shows it.
|
||||
*/
|
||||
private fun createNotification() {
|
||||
playerNotification = PlayerNotificationManager
|
||||
.Builder(context, PLAYER_NOTIFICATION_ID, BACKGROUND_CHANNEL_ID)
|
||||
// set the description of the notification
|
||||
.setMediaDescriptionAdapter(descriptionAdapter)
|
||||
// register the receiver for custom actions, doesn't seem to change anything
|
||||
.setCustomActionReceiver(customActionReceiver)
|
||||
.build().apply {
|
||||
setPlayer(player)
|
||||
setColorized(true)
|
||||
setMediaSessionToken(mediaSession.sessionCompatToken)
|
||||
setSmallIcon(R.drawable.ic_launcher_lockscreen)
|
||||
setUseNextAction(false)
|
||||
setUsePreviousAction(false)
|
||||
setUseRewindAction(false)
|
||||
setUseFastForwardAction(false)
|
||||
setUseStopAction(true)
|
||||
private fun createNotificationBuilder() {
|
||||
notificationBuilder = NotificationCompat.Builder(context, BACKGROUND_CHANNEL_ID)
|
||||
.setSmallIcon(R.drawable.ic_launcher_lockscreen)
|
||||
.setContentIntent(createCurrentContentIntent())
|
||||
.setDeleteIntent(createDeleteIntent())
|
||||
.setStyle(
|
||||
MediaStyleNotificationHelper.MediaStyle(mediaSession)
|
||||
.setShowActionsInCompactView(1)
|
||||
)
|
||||
}
|
||||
|
||||
private fun createOrUpdateNotification() {
|
||||
if (notificationBuilder == null) return
|
||||
val notification = notificationBuilder!!
|
||||
.setContentTitle(notificationData?.title)
|
||||
.setContentText(notificationData?.uploaderName)
|
||||
.setLargeIcon(notificationBitmap)
|
||||
.clearActions()
|
||||
.apply {
|
||||
legacyNotificationButtons.forEach {
|
||||
addAction(it)
|
||||
}
|
||||
}
|
||||
.build()
|
||||
nManager.notify(PLAYER_NOTIFICATION_ID, notification)
|
||||
}
|
||||
|
||||
private val notificationActionReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
handlePlayerAction(intent.action ?: return)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createActionReceiver() {
|
||||
listOf(PREV, NEXT, REWIND, FORWARD, PLAY_PAUSE, STOP).forEach {
|
||||
context.registerReceiver(notificationActionReceiver, IntentFilter(it))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the [NowPlayingNotification]
|
||||
*/
|
||||
fun destroySelfAndPlayer() {
|
||||
playerNotification?.setPlayer(null)
|
||||
|
||||
mediaSession.release()
|
||||
|
||||
player.stop()
|
||||
player.release()
|
||||
|
||||
context.getSystemService<NotificationManager>()!!.cancel(PLAYER_NOTIFICATION_ID)
|
||||
runCatching {
|
||||
context.unregisterReceiver(notificationActionReceiver)
|
||||
}
|
||||
|
||||
nManager.cancel(PLAYER_NOTIFICATION_ID)
|
||||
}
|
||||
|
||||
companion object {
|
||||
@ -331,5 +361,7 @@ class NowPlayingNotification(
|
||||
private const val NEXT = "next"
|
||||
private const val REWIND = "rewind"
|
||||
private const val FORWARD = "forward"
|
||||
private const val PLAY_PAUSE = "play_pause"
|
||||
private const val STOP = "stop"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user