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 android.text.Html
|
|
|
|
import androidx.core.app.NotificationCompat
|
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
|
2018-12-06 08:52:57 +00:00
|
|
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
|
|
|
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
|
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)
|
2019-12-08 14:35:03 +00:00
|
|
|
val playButtonResId = if (isPlaying) R.drawable.ic_pause_white_48dp else R.drawable.ic_play_arrow_white_48dp
|
|
|
|
val favoriteResId = if (isFavorite) R.drawable.ic_favorite_white_24dp else R.drawable.ic_favorite_border_white_24dp
|
2019-08-02 08:59:40 +00:00
|
|
|
|
2018-12-06 08:52:57 +00:00
|
|
|
val action = Intent(service, MainActivity::class.java)
|
|
|
|
action.putExtra("expand", true)
|
|
|
|
action.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
2019-12-10 14:36:28 +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
|
|
|
|
.getDimensionPixelSize(R.dimen.notification_big_image_size)
|
|
|
|
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)
|
|
|
|
.checkIgnoreMediaStore(service)
|
|
|
|
.generatePalette(service).build()
|
|
|
|
.centerCrop()
|
|
|
|
.into(object : SimpleTarget<BitmapPaletteWrapper>(bigNotificationImageSize, bigNotificationImageSize) {
|
|
|
|
override fun onResourceReady(resource: BitmapPaletteWrapper, glideAnimation: GlideAnimation<in BitmapPaletteWrapper>) {
|
2018-12-06 08:52:57 +00:00
|
|
|
update(resource.bitmap, when {
|
2019-09-09 13:04:53 +00:00
|
|
|
PreferenceUtil.getInstance(service).isDominantColor -> RetroColorUtil.getDominantColor(resource.bitmap, Color.TRANSPARENT)
|
2018-12-06 08:52:57 +00:00
|
|
|
else -> RetroColorUtil.getColor(resource.palette, Color.TRANSPARENT)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-16 18:02:40 +00:00
|
|
|
override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) {
|
|
|
|
super.onLoadFailed(e, errorDrawable)
|
2018-12-06 08:52:57 +00:00
|
|
|
update(null, Color.TRANSPARENT)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun update(bitmap: Bitmap?, color: Int) {
|
|
|
|
var bitmapFinal = bitmap
|
|
|
|
if (bitmapFinal == null) {
|
|
|
|
bitmapFinal = BitmapFactory.decodeResource(service.resources, R.drawable.default_album_art)
|
|
|
|
}
|
2019-08-02 08:59:40 +00:00
|
|
|
|
2019-12-08 14:35:03 +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
|
|
|
|
|
|
|
val builder = NotificationCompat.Builder(service,
|
2019-08-02 08:59:40 +00:00
|
|
|
NOTIFICATION_CHANNEL_ID)
|
2018-12-06 08:52:57 +00:00
|
|
|
.setSmallIcon(R.drawable.ic_notification)
|
|
|
|
.setLargeIcon(bitmapFinal)
|
|
|
|
.setContentIntent(clickIntent)
|
|
|
|
.setDeleteIntent(deleteIntent)
|
|
|
|
.setContentTitle(Html.fromHtml("<b>" + song.title + "</b>"))
|
|
|
|
.setContentText(song.artistName)
|
|
|
|
.setSubText(Html.fromHtml("<b>" + song.albumName + "</b>"))
|
|
|
|
.setOngoing(isPlaying)
|
|
|
|
.setShowWhen(false)
|
2019-08-04 15:56:32 +00:00
|
|
|
.addAction(toggleFavorite)
|
2018-12-06 08:52:57 +00:00
|
|
|
.addAction(previousAction)
|
|
|
|
.addAction(playPauseAction)
|
|
|
|
.addAction(nextAction)
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
2019-08-04 15:56:32 +00:00
|
|
|
builder.setStyle(MediaStyle()
|
2018-12-06 08:52:57 +00:00
|
|
|
.setMediaSession(service.mediaSession.sessionToken)
|
2019-09-16 18:02:40 +00:00
|
|
|
.setShowActionsInCompactView(1, 2, 3))
|
2018-12-06 08:52:57 +00:00
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
2019-09-09 13:04:53 +00:00
|
|
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O && PreferenceUtil.getInstance(service).coloredNotification()) {
|
2018-12-06 08:52:57 +00:00
|
|
|
builder.color = color
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stopped) {
|
|
|
|
return // notification has been stopped before loading was finished
|
|
|
|
}
|
|
|
|
updateNotifyModeAndPostNotification(builder.build())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|