Skip to content

Commit

Permalink
Load cover art when creating notification instead of when creating me…
Browse files Browse the repository at this point in the history
…tadata objects (#235)

Also make sure to create the notification asynchronously.
  • Loading branch information
chvp authored Jul 22, 2021
1 parent 82980b8 commit 3cd3894
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 40 deletions.
64 changes: 26 additions & 38 deletions app/src/main/java/me/vanpetegem/accentor/media/MusicService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
}
Expand All @@ -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)
Expand Down

0 comments on commit 3cd3894

Please sign in to comment.