Finally fixed wrong/previous cover art in notification

This commit is contained in:
Prathamesh More 2022-05-28 21:43:03 +05:30
parent 1f7a00e999
commit b2b9bac42d
3 changed files with 37 additions and 38 deletions

View file

@ -136,7 +136,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
def kotlin_coroutines_version = '1.6.1' def kotlin_coroutines_version = '1.6.2'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_coroutines_version"

View file

@ -171,7 +171,7 @@ class MusicService : MediaBrowserServiceCompat(),
@JvmField @JvmField
var playingQueue = ArrayList<Song>() var playingQueue = ArrayList<Song>()
private var playerHandler: Handler? = null private var playerHandler: Handler? = null
private var playingNotification: PlayingNotification? = null private var playingNotification: PlayingNotification? = null
@ -633,7 +633,7 @@ class MusicService : MediaBrowserServiceCompat(),
restorePlaybackState(wasPlaying, progress) restorePlaybackState(wasPlaying, progress)
} }
} }
ALBUM_ART_ON_LOCK_SCREEN, BLURRED_ALBUM_ART -> updateMediaSessionMetaData() ALBUM_ART_ON_LOCK_SCREEN, BLURRED_ALBUM_ART -> updateMediaSessionMetaData(::updateMediaSessionPlaybackState)
COLORED_NOTIFICATION -> { COLORED_NOTIFICATION -> {
playingNotification?.updateMetadata(currentSong) { playingNotification?.updateMetadata(currentSong) {
playingNotification?.setPlaying(isPlaying) playingNotification?.setPlaying(isPlaying)
@ -775,14 +775,14 @@ class MusicService : MediaBrowserServiceCompat(),
@Synchronized @Synchronized
fun play() { fun play() {
playbackManager.play(onNotInitialized = { playSongAt(getPosition()) }) { playbackManager.play(onNotInitialized = { playSongAt(getPosition()) }) {
if (notHandledMetaChangedForCurrentTrack) { if (notHandledMetaChangedForCurrentTrack) {
handleChangeInternal(META_CHANGED) handleChangeInternal(META_CHANGED)
notHandledMetaChangedForCurrentTrack = false notHandledMetaChangedForCurrentTrack = false
}
} }
notifyChange(PLAY_STATE_CHANGED)
} }
notifyChange(PLAY_STATE_CHANGED)
}
fun playNextSong(force: Boolean) { fun playNextSong(force: Boolean) {
playSongAt(getNextPosition(force)) playSongAt(getNextPosition(force))
@ -1006,7 +1006,7 @@ class MusicService : MediaBrowserServiceCompat(),
} }
@SuppressLint("CheckResult") @SuppressLint("CheckResult")
fun updateMediaSessionMetaData() { fun updateMediaSessionMetaData(onCompletion: () -> Unit) {
Log.i(TAG, "onResourceReady: ") Log.i(TAG, "onResourceReady: ")
val song = currentSong val song = currentSong
if (song.id == -1L) { if (song.id == -1L) {
@ -1036,32 +1036,31 @@ class MusicService : MediaBrowserServiceCompat(),
if (isBlurredAlbumArt) { if (isBlurredAlbumArt) {
request.transform(BlurTransformation.Builder(this@MusicService).build()) request.transform(BlurTransformation.Builder(this@MusicService).build())
} }
runOnUiThread { request.into(object :
request.into(object : CustomTarget<Bitmap?>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
CustomTarget<Bitmap?>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) { override fun onLoadFailed(errorDrawable: Drawable?) {
override fun onLoadFailed(errorDrawable: Drawable?) { super.onLoadFailed(errorDrawable)
super.onLoadFailed(errorDrawable) mediaSession?.setMetadata(metaData.build())
mediaSession?.setMetadata(metaData.build()) onCompletion()
} }
override fun onResourceReady( override fun onResourceReady(
resource: Bitmap, resource: Bitmap,
transition: Transition<in Bitmap?>?, transition: Transition<in Bitmap?>?,
) { ) {
metaData.putBitmap( metaData.putBitmap(
MediaMetadataCompat.METADATA_KEY_ALBUM_ART, MediaMetadataCompat.METADATA_KEY_ALBUM_ART,
copy(resource) resource
) )
mediaSession?.setMetadata(metaData.build()) mediaSession?.setMetadata(metaData.build())
} onCompletion()
}
override fun onLoadCleared(placeholder: Drawable?) { override fun onLoadCleared(placeholder: Drawable?) {}
mediaSession?.setMetadata(metaData.build()) })
}
})
}
} else { } else {
mediaSession?.setMetadata(metaData.build()) mediaSession?.setMetadata(metaData.build())
onCompletion()
} }
} }
@ -1090,8 +1089,9 @@ class MusicService : MediaBrowserServiceCompat(),
startForegroundOrNotify() startForegroundOrNotify()
} }
updateMediaSessionMetaData() // We must call updateMediaSessionPlaybackState after the load of album art is completed
updateMediaSessionPlaybackState() // if we are loading it or it won't be updated in the notification
updateMediaSessionMetaData(::updateMediaSessionPlaybackState)
serviceScope.launch(IO) { serviceScope.launch(IO) {
savePosition() savePosition()
savePositionInTrack() savePositionInTrack()
@ -1107,7 +1107,7 @@ class MusicService : MediaBrowserServiceCompat(),
QUEUE_CHANGED -> { QUEUE_CHANGED -> {
mediaSession?.setQueueTitle(getString(R.string.now_playing_queue)) mediaSession?.setQueueTitle(getString(R.string.now_playing_queue))
mediaSession?.setQueue(playingQueue.toMediaSessionQueue()) mediaSession?.setQueue(playingQueue.toMediaSessionQueue())
updateMediaSessionMetaData() // because playing queue size might have changed updateMediaSessionMetaData(::updateMediaSessionPlaybackState) // because playing queue size might have changed
saveState() saveState()
if (playingQueue.size > 0) { if (playingQueue.size > 0) {
prepareNext() prepareNext()

View file

@ -24,7 +24,6 @@ import android.graphics.BitmapFactory
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.MediaSessionCompat
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.text.parseAsHtml
import androidx.media.app.NotificationCompat.MediaStyle import androidx.media.app.NotificationCompat.MediaStyle
import code.name.monkey.appthemehelper.util.VersionUtils import code.name.monkey.appthemehelper.util.VersionUtils
import code.name.monkey.retromusic.R import code.name.monkey.retromusic.R
@ -111,9 +110,9 @@ class PlayingNotificationImpl24(
} }
override fun updateMetadata(song: Song, onUpdate: () -> Unit) { override fun updateMetadata(song: Song, onUpdate: () -> Unit) {
setContentTitle(("<b>" + song.title + "</b>").parseAsHtml()) setContentTitle(song.title)
setContentText(song.artistName) setContentText(song.artistName)
setSubText(("<b>" + song.albumName + "</b>").parseAsHtml()) setSubText(song.albumName)
val bigNotificationImageSize = context.resources val bigNotificationImageSize = context.resources
.getDimensionPixelSize(R.dimen.notification_big_image_size) .getDimensionPixelSize(R.dimen.notification_big_image_size)
GlideApp.with(context) GlideApp.with(context)