From 3cd38943c8ddcd3bfe264d33f8ad0a4da435fe7b Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Thu, 22 Jul 2021 22:39:45 +0200 Subject: [PATCH] Load cover art when creating notification instead of when creating metadata objects (#235) Also make sure to create the notification asynchronously. --- .../vanpetegem/accentor/media/MusicService.kt | 64 ++++++++----------- .../accentor/media/NotificationBuilder.kt | 11 +++- 2 files changed, 35 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/me/vanpetegem/accentor/media/MusicService.kt b/app/src/main/java/me/vanpetegem/accentor/media/MusicService.kt index 2f2950fc..0c90207b 100644 --- a/app/src/main/java/me/vanpetegem/accentor/media/MusicService.kt +++ b/app/src/main/java/me/vanpetegem/accentor/media/MusicService.kt @@ -4,7 +4,6 @@ import android.content.Intent import android.os.Bundle import androidx.core.app.NotificationManagerCompat import androidx.core.content.ContextCompat -import androidx.core.graphics.drawable.toBitmap import androidx.media2.common.MediaItem import androidx.media2.common.MediaMetadata import androidx.media2.common.SessionPlayer @@ -13,10 +12,6 @@ import androidx.media2.session.MediaSessionService import androidx.media2.session.SessionCommand import androidx.media2.session.SessionCommandGroup import androidx.media2.session.SessionResult -import coil.executeBlocking -import coil.imageLoader -import coil.request.CachePolicy -import coil.request.ImageRequest import com.google.android.exoplayer2.C import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.SimpleExoPlayer @@ -34,6 +29,7 @@ import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvicto import com.google.android.exoplayer2.upstream.cache.SimpleCache import java.io.File import java.util.concurrent.Executors +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch @@ -215,50 +211,42 @@ class MusicService : MediaSessionService() { builder.putString(MediaMetadata.METADATA_KEY_MEDIA_URI, mediaUri) builder.putString(MediaMetadata.METADATA_KEY_MEDIA_ID, track.id.toString()) - if (album.image500 != null) { - val bitmap = (this@MusicService).imageLoader.executeBlocking( - ImageRequest.Builder(this@MusicService).data(album.image500).networkCachePolicy(CachePolicy.DISABLED).build() - ).drawable?.toBitmap() - if (bitmap != null) { - builder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, bitmap) - builder.putBitmap(MediaMetadata.METADATA_KEY_ART, bitmap) - } - } - return MediaItem.Builder().setMetadata(builder.build()).build() } override fun onGetSession(info: MediaSession.ControllerInfo): MediaSession? = mediaSession override fun onUpdateNotification(session: MediaSession): MediaSessionService.MediaNotification? { - val notification = if (session.player.currentMediaItem?.metadata != null) { - notificationBuilder.buildNotification(session) - } else { null } + mainScope.launch(IO) { + val notification = if (session.player.currentMediaItem?.metadata != null) { + notificationBuilder.buildNotification(session) + } else { null } - when (session.player.playerState) { - SessionPlayer.PLAYER_STATE_PLAYING -> { - if (notification != null) { - notificationManager.notify(NOW_PLAYING_NOTIFICATION, notification) - if (!isForegroudService) { - ContextCompat.startForegroundService(application, Intent(application, this.javaClass)) - startForeground(NOW_PLAYING_NOTIFICATION, notification) - isForegroudService = true + when (session.player.playerState) { + SessionPlayer.PLAYER_STATE_PLAYING -> { + if (notification != null) { + notificationManager.notify(NOW_PLAYING_NOTIFICATION, notification) + if (!isForegroudService) { + ContextCompat.startForegroundService(application, Intent(application, this.javaClass)) + startForeground(NOW_PLAYING_NOTIFICATION, notification) + isForegroudService = true + } } } - } - else -> { - if (isForegroudService) { - stopForeground(false) - isForegroudService = false + else -> { + if (isForegroudService) { + stopForeground(false) + isForegroudService = false - if (session.player.playerState == SessionPlayer.PLAYER_STATE_IDLE) { - stopSelf() - } + if (session.player.playerState == SessionPlayer.PLAYER_STATE_IDLE) { + stopSelf() + } - if (notification != null) { - notificationManager.notify(NOW_PLAYING_NOTIFICATION, notification) - } else { - notificationManager.cancel(NOW_PLAYING_NOTIFICATION) + if (notification != null) { + notificationManager.notify(NOW_PLAYING_NOTIFICATION, notification) + } else { + notificationManager.cancel(NOW_PLAYING_NOTIFICATION) + } } } } diff --git a/app/src/main/java/me/vanpetegem/accentor/media/NotificationBuilder.kt b/app/src/main/java/me/vanpetegem/accentor/media/NotificationBuilder.kt index 210201dc..e52c9e84 100644 --- a/app/src/main/java/me/vanpetegem/accentor/media/NotificationBuilder.kt +++ b/app/src/main/java/me/vanpetegem/accentor/media/NotificationBuilder.kt @@ -10,10 +10,13 @@ import android.content.Intent import android.support.v4.media.session.PlaybackStateCompat import android.view.KeyEvent import androidx.core.app.NotificationCompat +import androidx.core.graphics.drawable.toBitmap import androidx.media.app.NotificationCompat.MediaStyle import androidx.media2.common.MediaMetadata import androidx.media2.common.SessionPlayer import androidx.media2.session.MediaSession +import coil.imageLoader +import coil.request.ImageRequest import me.vanpetegem.accentor.R import me.vanpetegem.accentor.ui.main.MainActivity @@ -50,7 +53,7 @@ class NotificationBuilder(private val context: Context) { private val stopPendingIntent = createPendingIntent(PlaybackStateCompat.ACTION_STOP) - fun buildNotification(session: MediaSession): Notification { + suspend fun buildNotification(session: MediaSession): Notification { if (shouldCreateNowPlayingChannel()) { createNowPlayingChannel() } @@ -77,11 +80,15 @@ class NotificationBuilder(private val context: Context) { val openIntent = Intent(context, MainActivity::class.java) val pendingIntent = PendingIntent.getActivity(context, 0, openIntent, 0) + val bitmap = metadata?.getString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI)?.let { + context.imageLoader.execute(ImageRequest.Builder(context).data(it).build()).drawable?.toBitmap() + } + return builder.setContentIntent(pendingIntent) .setContentTitle(metadata?.getString(MediaMetadata.METADATA_KEY_TITLE)) .setContentText(metadata?.getString(MediaMetadata.METADATA_KEY_ARTIST)) .setDeleteIntent(stopPendingIntent) - .setLargeIcon(metadata?.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART)) + .setLargeIcon(bitmap) .setOnlyAlertOnce(true) .setSmallIcon(R.drawable.ic_notification) .setStyle(mediaStyle)