Fixed Classic Notification crash

This commit is contained in:
Prathamesh More 2022-03-24 22:51:11 +05:30
parent 802cab7818
commit fe1a5c8ab0
4 changed files with 48 additions and 56 deletions

View file

@ -68,8 +68,8 @@ import code.name.monkey.retromusic.providers.MusicPlaybackQueueStore
import code.name.monkey.retromusic.providers.SongPlayCountStore
import code.name.monkey.retromusic.service.AudioFader.Companion.startFadeAnimator
import code.name.monkey.retromusic.service.notification.PlayingNotification
import code.name.monkey.retromusic.service.notification.PlayingNotificationClassic.Companion.from
import code.name.monkey.retromusic.service.notification.PlayingNotificationImpl24.Companion.from
import code.name.monkey.retromusic.service.notification.PlayingNotificationClassic
import code.name.monkey.retromusic.service.notification.PlayingNotificationImpl24
import code.name.monkey.retromusic.service.playback.Playback
import code.name.monkey.retromusic.service.playback.Playback.PlaybackCallbacks
import code.name.monkey.retromusic.util.MusicUtil.getMediaStoreAlbumCoverUri
@ -194,10 +194,10 @@ class MusicService : MediaBrowserServiceCompat(),
playerHandler?.obtainMessage(FOCUS_CHANGE, focusChange, 0)?.sendToTarget()
}
private var playingNotification: PlayingNotification? = null
private val updateFavoriteReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
playingNotification?.updateFavorite(currentSong) { startForegroundOrNotify() }
startForegroundOrNotify()
appWidgetCircle.notifyChange(this@MusicService, FAVORITE_STATE_CHANGED)
}
}
@ -594,9 +594,9 @@ class MusicService : MediaBrowserServiceCompat(),
playingNotification = if (VERSION.SDK_INT >= VERSION_CODES.N
&& !isClassicNotification
) {
from(this, notificationManager!!, mediaSession!!)
PlayingNotificationImpl24.from(this, notificationManager!!, mediaSession!!)
} else {
from(this, notificationManager!!)
PlayingNotificationClassic.from(this, notificationManager!!)
}
}
@ -732,11 +732,18 @@ class MusicService : MediaBrowserServiceCompat(),
}
}
ALBUM_ART_ON_LOCK_SCREEN, BLURRED_ALBUM_ART -> updateMediaSessionMetaData()
COLORED_NOTIFICATION -> updateNotification()
COLORED_NOTIFICATION -> {
playingNotification?.updateMetadata(currentSong) {
playingNotification?.setPlaying(isPlaying)
startForegroundOrNotify()
}
}
CLASSIC_NOTIFICATION -> {
updateNotification()
playingNotification?.setPlaying(isPlaying) { startForegroundOrNotify() }
playingNotification?.updateMetadata(currentSong) { startForegroundOrNotify() }
playingNotification?.updateMetadata(currentSong) {
playingNotification?.setPlaying(isPlaying)
startForegroundOrNotify()
}
}
PLAYBACK_SPEED -> updateMediaSessionPlaybackState()
TOGGLE_HEADSET -> registerHeadsetEvents()
@ -1195,12 +1202,14 @@ class MusicService : MediaBrowserServiceCompat(),
savePositionInTrack()
}
songPlayCountHelper.notifyPlayStateChanged(isPlaying)
playingNotification?.setPlaying(isPlaying) { startForegroundOrNotify() }
playingNotification?.setPlaying(isPlaying)
startForegroundOrNotify()
}
FAVORITE_STATE_CHANGED -> {
playingNotification?.updateFavorite(currentSong) { startForegroundOrNotify() }
playingNotification?.updateMetadata(currentSong) { startForegroundOrNotify() }
playingNotification?.updateFavorite(currentSong) {
startForegroundOrNotify()
}
updateMediaSessionMetaData()
updateMediaSessionPlaybackState()
savePosition()

View file

@ -29,7 +29,7 @@ abstract class PlayingNotification(context: Context) :
abstract fun updateMetadata(song: Song, onUpdate: () -> Unit)
abstract fun setPlaying(isPlaying: Boolean, onUpdate: () -> Unit)
abstract fun setPlaying(isPlaying: Boolean)
abstract fun updateFavorite(song: Song, onUpdate: () -> Unit)

View file

@ -56,13 +56,28 @@ import com.bumptech.glide.request.transition.Transition
class PlayingNotificationClassic(
val context: Context
) : PlayingNotification(context) {
private var primaryColor: Int = 0
private var isInitialized = false
init {
val notificationLayout = getCombinedRemoteViews(true)
val notificationLayoutBig = getCombinedRemoteViews(false)
private fun getCombinedRemoteViews(collapsed: Boolean, song: Song): RemoteViews {
val remoteViews = RemoteViews(
context.packageName,
if (collapsed) R.layout.layout_notification_collapsed else R.layout.layout_notification_expanded
)
remoteViews.setTextViewText(
R.id.appName,
context.getString(R.string.app_name) + "" + song.albumName
)
remoteViews.setTextViewText(R.id.title, song.title)
remoteViews.setTextViewText(R.id.subtitle, song.artistName)
linkButtons(remoteViews)
return remoteViews
}
override fun updateMetadata(song: Song, onUpdate: () -> Unit) {
isInitialized = true
val notificationLayout = getCombinedRemoteViews(true, song)
val notificationLayoutBig = getCombinedRemoteViews(false, song)
val action = Intent(context, MainActivity::class.java)
action.putExtra(MainActivity.EXPAND_PANEL, PreferenceUtil.isExpandPanel)
@ -83,25 +98,12 @@ class PlayingNotificationClassic(
setContentIntent(clickIntent)
setDeleteIntent(deleteIntent)
setCategory(NotificationCompat.CATEGORY_SERVICE)
setColorized(true)
setColorized(PreferenceUtil.isColoredNotification)
priority = NotificationCompat.PRIORITY_MAX
setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
setCustomContentView(notificationLayout)
setCustomBigContentView(notificationLayoutBig)
setOngoing(true)
}
private fun getCombinedRemoteViews(collapsed: Boolean): RemoteViews {
val remoteViews = RemoteViews(
context.packageName,
if (collapsed) R.layout.layout_notification_collapsed else R.layout.layout_notification_expanded
)
linkButtons(remoteViews)
return remoteViews
}
override fun updateMetadata(song: Song, onUpdate: () -> Unit) {
isInitialized = true
val bigNotificationImageSize = context.resources
.getDimensionPixelSize(R.dimen.notification_big_image_size)
GlideApp.with(context).asBitmapPalette().songCoverOptions(song)
@ -128,6 +130,10 @@ class PlayingNotificationClassic(
}
override fun onLoadCleared(placeholder: Drawable?) {
update(
null,
resolveColor(context, R.attr.colorSurface, Color.WHITE)
)
}
private fun update(bitmap: Bitmap?, bgColor: Int) {
@ -208,14 +214,6 @@ class PlayingNotificationClassic(
contentView.setImageViewBitmap(R.id.action_next, next)
contentView.setImageViewBitmap(R.id.action_play_pause, playPause)
contentView.setTextViewText(
R.id.appName,
context.getString(R.string.app_name) + "" + song.albumName
)
contentView.setTextViewText(R.id.title, song.title)
contentView.setTextViewText(R.id.subtitle, song.artistName)
bigContentView.setTextColor(R.id.title, primary)
bigContentView.setTextColor(R.id.subtitle, secondary)
bigContentView.setTextColor(R.id.appName, secondary)
@ -225,14 +223,6 @@ class PlayingNotificationClassic(
bigContentView.setImageViewBitmap(R.id.action_next, next)
bigContentView.setImageViewBitmap(R.id.action_play_pause, playPause)
bigContentView.setTextViewText(
R.id.appName,
context.getString(R.string.app_name) + "" + song.albumName
)
bigContentView.setTextViewText(R.id.title, song.title)
bigContentView.setTextViewText(R.id.subtitle, song.artistName)
contentView.setImageViewBitmap(
R.id.smallIcon,
createBitmap(
@ -269,7 +259,7 @@ class PlayingNotificationClassic(
)
}
override fun setPlaying(isPlaying: Boolean, onUpdate: () -> Unit) {
override fun setPlaying(isPlaying: Boolean) {
getPlayPauseBitmap(isPlaying).also {
contentView.setImageViewBitmap(R.id.action_play_pause, it)
bigContentView.setImageViewBitmap(R.id.action_play_pause, it)
@ -277,9 +267,6 @@ class PlayingNotificationClassic(
}
override fun updateFavorite(song: Song, onUpdate: () -> Unit) {
if (!isInitialized) {
updateMetadata(song, onUpdate)
}
}
private fun buildPendingIntent(

View file

@ -46,10 +46,7 @@ import code.name.monkey.retromusic.util.PreferenceUtil
import code.name.monkey.retromusic.util.RetroColorUtil
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*
@SuppressLint("RestrictedApi")
class PlayingNotificationImpl24(
@ -207,9 +204,8 @@ class PlayingNotificationImpl24(
).build()
}
override fun setPlaying(isPlaying: Boolean, onUpdate: () -> Unit) {
override fun setPlaying(isPlaying: Boolean) {
mActions[2] = buildPlayAction(isPlaying)
onUpdate()
}
override fun updateFavorite(song: Song, onUpdate: () -> Unit) {