Fix toggle notification favourite icon #954
This commit is contained in:
parent
1a5d74d9cf
commit
85bd49f391
2 changed files with 144 additions and 123 deletions
|
@ -27,6 +27,8 @@ import androidx.core.text.HtmlCompat
|
||||||
import androidx.media.app.NotificationCompat.MediaStyle
|
import androidx.media.app.NotificationCompat.MediaStyle
|
||||||
import code.name.monkey.retromusic.R
|
import code.name.monkey.retromusic.R
|
||||||
import code.name.monkey.retromusic.activities.MainActivity
|
import code.name.monkey.retromusic.activities.MainActivity
|
||||||
|
import code.name.monkey.retromusic.db.PlaylistEntity
|
||||||
|
import code.name.monkey.retromusic.db.toSongEntity
|
||||||
import code.name.monkey.retromusic.glide.SongGlideRequest
|
import code.name.monkey.retromusic.glide.SongGlideRequest
|
||||||
import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper
|
import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper
|
||||||
import code.name.monkey.retromusic.service.MusicService
|
import code.name.monkey.retromusic.service.MusicService
|
||||||
|
@ -38,140 +40,149 @@ import com.bumptech.glide.Glide
|
||||||
import com.bumptech.glide.request.animation.GlideAnimation
|
import com.bumptech.glide.request.animation.GlideAnimation
|
||||||
import com.bumptech.glide.request.target.SimpleTarget
|
import com.bumptech.glide.request.target.SimpleTarget
|
||||||
import com.bumptech.glide.request.target.Target
|
import com.bumptech.glide.request.target.Target
|
||||||
|
import kotlinx.coroutines.GlobalScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import org.koin.core.KoinComponent
|
||||||
|
|
||||||
class PlayingNotificationImpl : PlayingNotification() {
|
class PlayingNotificationImpl : PlayingNotification(), KoinComponent {
|
||||||
private var target: Target<BitmapPaletteWrapper>? = null
|
private var target: Target<BitmapPaletteWrapper>? = null
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
override fun update() {
|
override fun update() {
|
||||||
stopped = false
|
stopped = false
|
||||||
|
GlobalScope.launch {
|
||||||
|
val song = service.currentSong
|
||||||
|
val playlist: PlaylistEntity? = MusicUtil.repository.favoritePlaylist()
|
||||||
|
val isPlaying = service.isPlaying
|
||||||
|
val isFavorite = if (playlist != null) {
|
||||||
|
val songEntity = song.toSongEntity(playlist.playListId)
|
||||||
|
MusicUtil.repository.isFavoriteSong(songEntity).isNotEmpty()
|
||||||
|
} else false
|
||||||
|
|
||||||
val song = service.currentSong
|
val playButtonResId =
|
||||||
val isPlaying = service.isPlaying
|
if (isPlaying) R.drawable.ic_pause_white_48dp else R.drawable.ic_play_arrow_white_48dp
|
||||||
val isFavorite = MusicUtil.isFavorite(service, song)
|
val favoriteResId =
|
||||||
val playButtonResId =
|
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border
|
||||||
if (isPlaying) R.drawable.ic_pause_white_48dp else R.drawable.ic_play_arrow_white_48dp
|
|
||||||
val favoriteResId =
|
|
||||||
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border
|
|
||||||
|
|
||||||
val action = Intent(service, MainActivity::class.java)
|
val action = Intent(service, MainActivity::class.java)
|
||||||
action.putExtra(MainActivity.EXPAND_PANEL, PreferenceUtil.isExpandPanel)
|
action.putExtra(MainActivity.EXPAND_PANEL, PreferenceUtil.isExpandPanel)
|
||||||
action.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
action.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||||
val clickIntent =
|
val clickIntent =
|
||||||
PendingIntent.getActivity(service, 0, action, PendingIntent.FLAG_UPDATE_CURRENT)
|
PendingIntent.getActivity(service, 0, action, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||||
|
|
||||||
val serviceName = ComponentName(service, MusicService::class.java)
|
val serviceName = ComponentName(service, MusicService::class.java)
|
||||||
val intent = Intent(ACTION_QUIT)
|
val intent = Intent(ACTION_QUIT)
|
||||||
intent.component = serviceName
|
intent.component = serviceName
|
||||||
val deleteIntent = PendingIntent.getService(service, 0, intent, 0)
|
val deleteIntent = PendingIntent.getService(service, 0, intent, 0)
|
||||||
|
|
||||||
val bigNotificationImageSize = service.resources
|
val bigNotificationImageSize = service.resources
|
||||||
.getDimensionPixelSize(R.dimen.notification_big_image_size)
|
.getDimensionPixelSize(R.dimen.notification_big_image_size)
|
||||||
service.runOnUiThread {
|
service.runOnUiThread {
|
||||||
if (target != null) {
|
if (target != null) {
|
||||||
Glide.clear(target)
|
Glide.clear(target)
|
||||||
}
|
}
|
||||||
target = SongGlideRequest.Builder.from(Glide.with(service), song)
|
target = SongGlideRequest.Builder.from(Glide.with(service), song)
|
||||||
.checkIgnoreMediaStore(service)
|
.checkIgnoreMediaStore(service)
|
||||||
.generatePalette(service).build()
|
.generatePalette(service).build()
|
||||||
.centerCrop()
|
.centerCrop()
|
||||||
.into(object : SimpleTarget<BitmapPaletteWrapper>(
|
.into(object : SimpleTarget<BitmapPaletteWrapper>(
|
||||||
bigNotificationImageSize,
|
bigNotificationImageSize,
|
||||||
bigNotificationImageSize
|
bigNotificationImageSize
|
||||||
) {
|
|
||||||
override fun onResourceReady(
|
|
||||||
resource: BitmapPaletteWrapper,
|
|
||||||
glideAnimation: GlideAnimation<in BitmapPaletteWrapper>
|
|
||||||
) {
|
) {
|
||||||
update(
|
override fun onResourceReady(
|
||||||
resource.bitmap,
|
resource: BitmapPaletteWrapper,
|
||||||
RetroColorUtil.getColor(resource.palette, Color.TRANSPARENT)
|
glideAnimation: GlideAnimation<in BitmapPaletteWrapper>
|
||||||
)
|
) {
|
||||||
}
|
update(
|
||||||
|
resource.bitmap,
|
||||||
override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) {
|
RetroColorUtil.getColor(resource.palette, Color.TRANSPARENT)
|
||||||
super.onLoadFailed(e, errorDrawable)
|
|
||||||
update(null, Color.TRANSPARENT)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun update(bitmap: Bitmap?, color: Int) {
|
|
||||||
var bitmapFinal = bitmap
|
|
||||||
if (bitmapFinal == null) {
|
|
||||||
bitmapFinal = BitmapFactory.decodeResource(
|
|
||||||
service.resources,
|
|
||||||
R.drawable.default_audio_art
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
val toggleFavorite = NotificationCompat.Action(
|
override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) {
|
||||||
favoriteResId,
|
super.onLoadFailed(e, errorDrawable)
|
||||||
service.getString(R.string.action_toggle_favorite),
|
update(null, Color.TRANSPARENT)
|
||||||
retrievePlaybackAction(TOGGLE_FAVORITE)
|
}
|
||||||
)
|
|
||||||
val playPauseAction = NotificationCompat.Action(
|
|
||||||
playButtonResId,
|
|
||||||
service.getString(R.string.action_play_pause),
|
|
||||||
retrievePlaybackAction(ACTION_TOGGLE_PAUSE)
|
|
||||||
)
|
|
||||||
val previousAction = NotificationCompat.Action(
|
|
||||||
R.drawable.ic_skip_previous_round_white_32dp,
|
|
||||||
service.getString(R.string.action_previous),
|
|
||||||
retrievePlaybackAction(ACTION_REWIND)
|
|
||||||
)
|
|
||||||
val nextAction = NotificationCompat.Action(
|
|
||||||
R.drawable.ic_skip_next_round_white_32dp,
|
|
||||||
service.getString(R.string.action_next),
|
|
||||||
retrievePlaybackAction(ACTION_SKIP)
|
|
||||||
)
|
|
||||||
|
|
||||||
val builder = NotificationCompat.Builder(
|
fun update(bitmap: Bitmap?, color: Int) {
|
||||||
service,
|
var bitmapFinal = bitmap
|
||||||
NOTIFICATION_CHANNEL_ID
|
if (bitmapFinal == null) {
|
||||||
)
|
bitmapFinal = BitmapFactory.decodeResource(
|
||||||
.setSmallIcon(R.drawable.ic_notification)
|
service.resources,
|
||||||
.setLargeIcon(bitmapFinal)
|
R.drawable.default_audio_art
|
||||||
.setContentIntent(clickIntent)
|
|
||||||
.setDeleteIntent(deleteIntent)
|
|
||||||
.setContentTitle(
|
|
||||||
HtmlCompat.fromHtml(
|
|
||||||
"<b>" + song.title + "</b>",
|
|
||||||
HtmlCompat.FROM_HTML_MODE_LEGACY
|
|
||||||
)
|
)
|
||||||
)
|
|
||||||
.setContentText(song.artistName)
|
|
||||||
.setSubText(
|
|
||||||
HtmlCompat.fromHtml(
|
|
||||||
"<b>" + song.albumName + "</b>",
|
|
||||||
HtmlCompat.FROM_HTML_MODE_LEGACY
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.setOngoing(isPlaying)
|
|
||||||
.setShowWhen(false)
|
|
||||||
.addAction(toggleFavorite)
|
|
||||||
.addAction(previousAction)
|
|
||||||
.addAction(playPauseAction)
|
|
||||||
.addAction(nextAction)
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
||||||
builder.setStyle(
|
|
||||||
MediaStyle()
|
|
||||||
.setMediaSession(service.mediaSession.sessionToken)
|
|
||||||
.setShowActionsInCompactView(1, 2, 3)
|
|
||||||
)
|
|
||||||
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
|
||||||
if (Build.VERSION.SDK_INT <=
|
|
||||||
Build.VERSION_CODES.O && PreferenceUtil.isColoredNotification
|
|
||||||
) {
|
|
||||||
builder.color = color
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (stopped) {
|
val toggleFavorite = NotificationCompat.Action(
|
||||||
return // notification has been stopped before loading was finished
|
favoriteResId,
|
||||||
|
service.getString(R.string.action_toggle_favorite),
|
||||||
|
retrievePlaybackAction(TOGGLE_FAVORITE)
|
||||||
|
)
|
||||||
|
val playPauseAction = NotificationCompat.Action(
|
||||||
|
playButtonResId,
|
||||||
|
service.getString(R.string.action_play_pause),
|
||||||
|
retrievePlaybackAction(ACTION_TOGGLE_PAUSE)
|
||||||
|
)
|
||||||
|
val previousAction = NotificationCompat.Action(
|
||||||
|
R.drawable.ic_skip_previous_round_white_32dp,
|
||||||
|
service.getString(R.string.action_previous),
|
||||||
|
retrievePlaybackAction(ACTION_REWIND)
|
||||||
|
)
|
||||||
|
val nextAction = NotificationCompat.Action(
|
||||||
|
R.drawable.ic_skip_next_round_white_32dp,
|
||||||
|
service.getString(R.string.action_next),
|
||||||
|
retrievePlaybackAction(ACTION_SKIP)
|
||||||
|
)
|
||||||
|
|
||||||
|
val builder = NotificationCompat.Builder(
|
||||||
|
service,
|
||||||
|
NOTIFICATION_CHANNEL_ID
|
||||||
|
)
|
||||||
|
.setSmallIcon(R.drawable.ic_notification)
|
||||||
|
.setLargeIcon(bitmapFinal)
|
||||||
|
.setContentIntent(clickIntent)
|
||||||
|
.setDeleteIntent(deleteIntent)
|
||||||
|
.setContentTitle(
|
||||||
|
HtmlCompat.fromHtml(
|
||||||
|
"<b>" + song.title + "</b>",
|
||||||
|
HtmlCompat.FROM_HTML_MODE_LEGACY
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.setContentText(song.artistName)
|
||||||
|
.setSubText(
|
||||||
|
HtmlCompat.fromHtml(
|
||||||
|
"<b>" + song.albumName + "</b>",
|
||||||
|
HtmlCompat.FROM_HTML_MODE_LEGACY
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.setOngoing(isPlaying)
|
||||||
|
.setShowWhen(false)
|
||||||
|
.addAction(toggleFavorite)
|
||||||
|
.addAction(previousAction)
|
||||||
|
.addAction(playPauseAction)
|
||||||
|
.addAction(nextAction)
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
|
builder.setStyle(
|
||||||
|
MediaStyle()
|
||||||
|
.setMediaSession(service.mediaSession.sessionToken)
|
||||||
|
.setShowActionsInCompactView(1, 2, 3)
|
||||||
|
)
|
||||||
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
||||||
|
if (Build.VERSION.SDK_INT <=
|
||||||
|
Build.VERSION_CODES.O && PreferenceUtil.isColoredNotification
|
||||||
|
) {
|
||||||
|
builder.color = color
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stopped) {
|
||||||
|
return // notification has been stopped before loading was finished
|
||||||
|
}
|
||||||
|
updateNotifyModeAndPostNotification(builder.build())
|
||||||
}
|
}
|
||||||
updateNotifyModeAndPostNotification(builder.build())
|
})
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,9 @@ import android.widget.Toast
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
import androidx.fragment.app.FragmentActivity
|
import androidx.fragment.app.FragmentActivity
|
||||||
import code.name.monkey.retromusic.R
|
import code.name.monkey.retromusic.R
|
||||||
|
import code.name.monkey.retromusic.db.PlaylistEntity
|
||||||
import code.name.monkey.retromusic.db.SongEntity
|
import code.name.monkey.retromusic.db.SongEntity
|
||||||
|
import code.name.monkey.retromusic.db.toSongEntity
|
||||||
import code.name.monkey.retromusic.extensions.getLong
|
import code.name.monkey.retromusic.extensions.getLong
|
||||||
import code.name.monkey.retromusic.helper.MusicPlayerRemote.removeFromQueue
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote.removeFromQueue
|
||||||
import code.name.monkey.retromusic.model.Artist
|
import code.name.monkey.retromusic.model.Artist
|
||||||
|
@ -24,8 +26,11 @@ import code.name.monkey.retromusic.model.Song
|
||||||
import code.name.monkey.retromusic.model.lyrics.AbsSynchronizedLyrics
|
import code.name.monkey.retromusic.model.lyrics.AbsSynchronizedLyrics
|
||||||
import code.name.monkey.retromusic.repository.RealPlaylistRepository
|
import code.name.monkey.retromusic.repository.RealPlaylistRepository
|
||||||
import code.name.monkey.retromusic.repository.RealSongRepository
|
import code.name.monkey.retromusic.repository.RealSongRepository
|
||||||
|
import code.name.monkey.retromusic.repository.Repository
|
||||||
import code.name.monkey.retromusic.repository.SongRepository
|
import code.name.monkey.retromusic.repository.SongRepository
|
||||||
import code.name.monkey.retromusic.service.MusicService
|
import code.name.monkey.retromusic.service.MusicService
|
||||||
|
import kotlinx.coroutines.GlobalScope
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import org.jaudiotagger.audio.AudioFileIO
|
import org.jaudiotagger.audio.AudioFileIO
|
||||||
import org.jaudiotagger.tag.FieldKey
|
import org.jaudiotagger.tag.FieldKey
|
||||||
import org.koin.core.KoinComponent
|
import org.koin.core.KoinComponent
|
||||||
|
@ -321,16 +326,21 @@ object MusicUtil : KoinComponent {
|
||||||
return playlist.name == context.getString(R.string.favorites)
|
return playlist.name == context.getString(R.string.favorites)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val repository = get<Repository>()
|
||||||
fun toggleFavorite(context: Context, song: Song) {
|
fun toggleFavorite(context: Context, song: Song) {
|
||||||
if (isFavorite(context, song)) {
|
GlobalScope.launch {
|
||||||
PlaylistsUtil.removeFromPlaylist(context, song, getFavoritesPlaylist(context).id)
|
val playlist: PlaylistEntity? = repository.favoritePlaylist()
|
||||||
} else {
|
if (playlist != null) {
|
||||||
PlaylistsUtil.addToPlaylist(
|
val songEntity = song.toSongEntity(playlist.playListId)
|
||||||
context, song, getOrCreateFavoritesPlaylist(context).id,
|
val isFavorite = repository.isFavoriteSong(songEntity).isNotEmpty()
|
||||||
false
|
if (isFavorite) {
|
||||||
)
|
repository.removeSongFromPlaylist(songEntity)
|
||||||
|
} else {
|
||||||
|
repository.insertSongs(listOf(song.toSongEntity(playlist.playListId)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
context.sendBroadcast(Intent(MusicService.FAVORITE_STATE_CHANGED))
|
||||||
}
|
}
|
||||||
context.sendBroadcast(Intent(MusicService.FAVORITE_STATE_CHANGED))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getFavoritesPlaylist(context: Context): Playlist {
|
private fun getFavoritesPlaylist(context: Context): Playlist {
|
||||||
|
|
Loading…
Reference in a new issue