2019-03-03 09:29:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Hemanth Savarala.
|
|
|
|
*
|
|
|
|
* Licensed under the GNU General Public License v3
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2018-12-06 08:52:57 +00:00
|
|
|
package code.name.monkey.retromusic.service.notification
|
|
|
|
|
|
|
|
import android.app.PendingIntent
|
|
|
|
import android.content.ComponentName
|
|
|
|
import android.content.Intent
|
|
|
|
import android.graphics.Bitmap
|
|
|
|
import android.graphics.BitmapFactory
|
|
|
|
import android.graphics.Color
|
|
|
|
import android.graphics.drawable.Drawable
|
|
|
|
import android.os.Build
|
|
|
|
import androidx.core.app.NotificationCompat
|
2020-08-21 18:01:52 +00:00
|
|
|
import androidx.core.text.HtmlCompat
|
2019-09-16 18:02:40 +00:00
|
|
|
import androidx.media.app.NotificationCompat.MediaStyle
|
2018-12-06 08:52:57 +00:00
|
|
|
import code.name.monkey.retromusic.R
|
2019-08-02 08:59:40 +00:00
|
|
|
import code.name.monkey.retromusic.activities.MainActivity
|
2019-09-16 18:02:40 +00:00
|
|
|
import code.name.monkey.retromusic.glide.SongGlideRequest
|
2018-12-06 08:52:57 +00:00
|
|
|
import code.name.monkey.retromusic.glide.palette.BitmapPaletteWrapper
|
|
|
|
import code.name.monkey.retromusic.service.MusicService
|
2019-08-02 08:59:40 +00:00
|
|
|
import code.name.monkey.retromusic.service.MusicService.*
|
|
|
|
import code.name.monkey.retromusic.util.MusicUtil
|
2020-06-06 18:57:28 +00:00
|
|
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
2018-12-06 08:52:57 +00:00
|
|
|
import code.name.monkey.retromusic.util.RetroColorUtil
|
2019-09-16 18:02:40 +00:00
|
|
|
import com.bumptech.glide.Glide
|
|
|
|
import com.bumptech.glide.request.animation.GlideAnimation
|
|
|
|
import com.bumptech.glide.request.target.SimpleTarget
|
2018-12-12 20:59:07 +00:00
|
|
|
import com.bumptech.glide.request.target.Target
|
2018-12-06 08:52:57 +00:00
|
|
|
|
2019-12-10 14:36:28 +00:00
|
|
|
class PlayingNotificationImpl : PlayingNotification() {
|
2018-12-12 20:59:07 +00:00
|
|
|
private var target: Target<BitmapPaletteWrapper>? = null
|
2020-05-11 19:39:44 +00:00
|
|
|
|
2018-12-06 08:52:57 +00:00
|
|
|
@Synchronized
|
|
|
|
override fun update() {
|
|
|
|
stopped = false
|
|
|
|
|
|
|
|
val song = service.currentSong
|
|
|
|
val isPlaying = service.isPlaying
|
2019-08-02 08:59:40 +00:00
|
|
|
val isFavorite = MusicUtil.isFavorite(service, song)
|
2020-02-26 16:49:24 +00:00
|
|
|
val playButtonResId =
|
|
|
|
if (isPlaying) R.drawable.ic_pause_white_48dp else R.drawable.ic_play_arrow_white_48dp
|
|
|
|
val favoriteResId =
|
2020-07-19 21:00:30 +00:00
|
|
|
if (isFavorite) R.drawable.ic_favorite else R.drawable.ic_favorite_border
|
2019-08-02 08:59:40 +00:00
|
|
|
|
2018-12-06 08:52:57 +00:00
|
|
|
val action = Intent(service, MainActivity::class.java)
|
2020-10-11 17:15:27 +00:00
|
|
|
action.putExtra(MainActivity.EXPAND_PANEL, PreferenceUtil.isExpandPanel)
|
2018-12-06 08:52:57 +00:00
|
|
|
action.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
2020-02-26 16:49:24 +00:00
|
|
|
val clickIntent =
|
|
|
|
PendingIntent.getActivity(service, 0, action, PendingIntent.FLAG_UPDATE_CURRENT)
|
2018-12-06 08:52:57 +00:00
|
|
|
|
|
|
|
val serviceName = ComponentName(service, MusicService::class.java)
|
|
|
|
val intent = Intent(ACTION_QUIT)
|
|
|
|
intent.component = serviceName
|
|
|
|
val deleteIntent = PendingIntent.getService(service, 0, intent, 0)
|
|
|
|
|
|
|
|
val bigNotificationImageSize = service.resources
|
2020-02-26 16:49:24 +00:00
|
|
|
.getDimensionPixelSize(R.dimen.notification_big_image_size)
|
2018-12-06 08:52:57 +00:00
|
|
|
service.runOnUiThread {
|
2018-12-12 20:59:07 +00:00
|
|
|
if (target != null) {
|
2019-09-16 18:02:40 +00:00
|
|
|
Glide.clear(target)
|
2018-12-12 20:59:07 +00:00
|
|
|
}
|
2019-09-16 18:02:40 +00:00
|
|
|
target = SongGlideRequest.Builder.from(Glide.with(service), song)
|
2020-02-26 16:49:24 +00:00
|
|
|
.checkIgnoreMediaStore(service)
|
|
|
|
.generatePalette(service).build()
|
|
|
|
.centerCrop()
|
|
|
|
.into(object : SimpleTarget<BitmapPaletteWrapper>(
|
|
|
|
bigNotificationImageSize,
|
|
|
|
bigNotificationImageSize
|
|
|
|
) {
|
|
|
|
override fun onResourceReady(
|
|
|
|
resource: BitmapPaletteWrapper,
|
|
|
|
glideAnimation: GlideAnimation<in BitmapPaletteWrapper>
|
|
|
|
) {
|
|
|
|
update(
|
2020-05-11 19:39:44 +00:00
|
|
|
resource.bitmap,
|
|
|
|
RetroColorUtil.getColor(resource.palette, Color.TRANSPARENT)
|
2020-02-26 16:49:24 +00:00
|
|
|
)
|
|
|
|
}
|
2018-12-06 08:52:57 +00:00
|
|
|
|
2020-02-26 16:49:24 +00:00
|
|
|
override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) {
|
|
|
|
super.onLoadFailed(e, errorDrawable)
|
|
|
|
update(null, Color.TRANSPARENT)
|
|
|
|
}
|
2018-12-06 08:52:57 +00:00
|
|
|
|
2020-02-26 16:49:24 +00:00
|
|
|
fun update(bitmap: Bitmap?, color: Int) {
|
|
|
|
var bitmapFinal = bitmap
|
|
|
|
if (bitmapFinal == null) {
|
|
|
|
bitmapFinal = BitmapFactory.decodeResource(
|
|
|
|
service.resources,
|
|
|
|
R.drawable.default_audio_art
|
|
|
|
)
|
|
|
|
}
|
2019-08-02 08:59:40 +00:00
|
|
|
|
2020-02-26 16:49:24 +00:00
|
|
|
val toggleFavorite = NotificationCompat.Action(
|
|
|
|
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)
|
|
|
|
)
|
2018-12-06 08:52:57 +00:00
|
|
|
|
2020-02-26 16:49:24 +00:00
|
|
|
val builder = NotificationCompat.Builder(
|
|
|
|
service,
|
|
|
|
NOTIFICATION_CHANNEL_ID
|
|
|
|
)
|
|
|
|
.setSmallIcon(R.drawable.ic_notification)
|
|
|
|
.setLargeIcon(bitmapFinal)
|
|
|
|
.setContentIntent(clickIntent)
|
|
|
|
.setDeleteIntent(deleteIntent)
|
2020-08-21 18:01:52 +00:00
|
|
|
.setContentTitle(
|
|
|
|
HtmlCompat.fromHtml(
|
|
|
|
"<b>" + song.title + "</b>",
|
|
|
|
HtmlCompat.FROM_HTML_MODE_LEGACY
|
|
|
|
)
|
|
|
|
)
|
2020-02-26 16:49:24 +00:00
|
|
|
.setContentText(song.artistName)
|
2020-08-21 18:01:52 +00:00
|
|
|
.setSubText(
|
|
|
|
HtmlCompat.fromHtml(
|
|
|
|
"<b>" + song.albumName + "</b>",
|
|
|
|
HtmlCompat.FROM_HTML_MODE_LEGACY
|
|
|
|
)
|
|
|
|
)
|
2020-02-26 16:49:24 +00:00
|
|
|
.setOngoing(isPlaying)
|
|
|
|
.setShowWhen(false)
|
|
|
|
.addAction(toggleFavorite)
|
|
|
|
.addAction(previousAction)
|
|
|
|
.addAction(playPauseAction)
|
|
|
|
.addAction(nextAction)
|
2018-12-06 08:52:57 +00:00
|
|
|
|
2020-02-26 16:49:24 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
builder.setStyle(
|
|
|
|
MediaStyle()
|
|
|
|
.setMediaSession(service.mediaSession.sessionToken)
|
|
|
|
.setShowActionsInCompactView(1, 2, 3)
|
|
|
|
)
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
2020-05-23 09:27:55 +00:00
|
|
|
if (Build.VERSION.SDK_INT <=
|
2020-06-06 18:57:28 +00:00
|
|
|
Build.VERSION_CODES.O && PreferenceUtil.isColoredNotification
|
2020-02-26 16:49:24 +00:00
|
|
|
) {
|
|
|
|
builder.color = color
|
2018-12-06 08:52:57 +00:00
|
|
|
}
|
2020-02-26 16:49:24 +00:00
|
|
|
}
|
2018-12-06 08:52:57 +00:00
|
|
|
|
2020-02-26 16:49:24 +00:00
|
|
|
if (stopped) {
|
|
|
|
return // notification has been stopped before loading was finished
|
2018-12-06 08:52:57 +00:00
|
|
|
}
|
2020-02-26 16:49:24 +00:00
|
|
|
updateNotifyModeAndPostNotification(builder.build())
|
|
|
|
}
|
|
|
|
})
|
2018-12-06 08:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun retrievePlaybackAction(action: String): PendingIntent {
|
|
|
|
val serviceName = ComponentName(service, MusicService::class.java)
|
|
|
|
val intent = Intent(action)
|
|
|
|
intent.component = serviceName
|
|
|
|
return PendingIntent.getService(service, 0, intent, 0)
|
|
|
|
}
|
|
|
|
}
|