2019-03-03 09:20:15 +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-08 03:33:02 +00:00
|
|
|
package code.name.monkey.retromusic.appwidgets
|
|
|
|
|
|
|
|
import android.app.PendingIntent
|
|
|
|
import android.content.ComponentName
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.graphics.Bitmap
|
|
|
|
import android.graphics.drawable.Drawable
|
|
|
|
import android.text.TextUtils
|
|
|
|
import android.view.View
|
|
|
|
import android.widget.RemoteViews
|
|
|
|
import code.name.monkey.appthemehelper.util.MaterialValueHelper
|
|
|
|
import code.name.monkey.retromusic.R
|
2019-08-02 08:59:40 +00:00
|
|
|
import code.name.monkey.retromusic.activities.MainActivity
|
2018-12-08 03:33:02 +00:00
|
|
|
import code.name.monkey.retromusic.appwidgets.base.BaseAppWidget
|
2019-09-16 18:02:40 +00:00
|
|
|
import code.name.monkey.retromusic.glide.SongGlideRequest
|
2018-12-08 03:33:02 +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.*
|
2019-09-16 18:02:40 +00:00
|
|
|
import code.name.monkey.retromusic.util.ImageUtil
|
2018-12-08 03:33:02 +00:00
|
|
|
import code.name.monkey.retromusic.util.RetroUtil
|
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-08 03:33:02 +00:00
|
|
|
import com.bumptech.glide.request.target.Target
|
2019-09-16 18:02:40 +00:00
|
|
|
|
2018-12-08 03:33:02 +00:00
|
|
|
|
|
|
|
class AppWidgetCard : BaseAppWidget() {
|
|
|
|
private var target: Target<BitmapPaletteWrapper>? = null // for cancellation
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize given widgets to default state, where we launch Music on default click and hide
|
|
|
|
* actions if service not running.
|
|
|
|
*/
|
|
|
|
override fun defaultAppWidget(context: Context, appWidgetIds: IntArray) {
|
2019-11-08 17:07:30 +00:00
|
|
|
val appWidgetView = RemoteViews(context.packageName, R.layout.app_widget_card)
|
2018-12-08 03:33:02 +00:00
|
|
|
|
|
|
|
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE)
|
|
|
|
appWidgetView.setImageViewResource(R.id.image, R.drawable.default_album_art)
|
2019-08-02 08:59:40 +00:00
|
|
|
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_next_white_24dp, MaterialValueHelper.getSecondaryTextColor(context, true))!!, 1f))
|
|
|
|
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_skip_previous_white_24dp, MaterialValueHelper.getSecondaryTextColor(context, true))!!, 1f))
|
|
|
|
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, createBitmap(RetroUtil.getTintedVectorDrawable(context, R.drawable.ic_play_arrow_white_32dp, MaterialValueHelper.getSecondaryTextColor(context, true))!!, 1f))
|
2018-12-08 03:33:02 +00:00
|
|
|
|
|
|
|
linkButtons(context, appWidgetView)
|
|
|
|
pushUpdate(context, appWidgetIds, appWidgetView)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update all active widget instances by pushing changes
|
|
|
|
*/
|
|
|
|
override fun performUpdate(service: MusicService, appWidgetIds: IntArray?) {
|
2019-11-08 17:07:30 +00:00
|
|
|
val appWidgetView = RemoteViews(service.packageName, R.layout.app_widget_card)
|
2018-12-08 03:33:02 +00:00
|
|
|
|
|
|
|
val isPlaying = service.isPlaying
|
|
|
|
val song = service.currentSong
|
|
|
|
|
|
|
|
// Set the titles and artwork
|
|
|
|
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
|
|
|
|
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE)
|
|
|
|
} else {
|
|
|
|
appWidgetView.setViewVisibility(R.id.media_titles, View.VISIBLE)
|
|
|
|
appWidgetView.setTextViewText(R.id.title, song.title)
|
|
|
|
appWidgetView.setTextViewText(R.id.text, getSongArtistAndAlbum(song))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set correct drawable for pause state
|
2019-06-05 08:17:45 +00:00
|
|
|
val playPauseRes = if (isPlaying) R.drawable.ic_pause_white_24dp else R.drawable.ic_play_arrow_white_32dp
|
2019-09-16 18:02:40 +00:00
|
|
|
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, createBitmap(
|
2018-12-08 03:33:02 +00:00
|
|
|
RetroUtil.getTintedVectorDrawable(service, playPauseRes,
|
|
|
|
MaterialValueHelper.getSecondaryTextColor(service, true))!!, 1f))
|
|
|
|
|
|
|
|
// Set prev/next button drawables
|
2019-09-16 18:02:40 +00:00
|
|
|
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(
|
2018-12-08 03:33:02 +00:00
|
|
|
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp,
|
|
|
|
MaterialValueHelper.getSecondaryTextColor(service, true))!!, 1f))
|
2019-09-16 18:02:40 +00:00
|
|
|
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(
|
2018-12-08 03:33:02 +00:00
|
|
|
RetroUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp,
|
|
|
|
MaterialValueHelper.getSecondaryTextColor(service, true))!!, 1f))
|
|
|
|
|
|
|
|
// Link actions buttons to intents
|
|
|
|
linkButtons(service, appWidgetView)
|
|
|
|
|
|
|
|
if (imageSize == 0) {
|
2019-09-16 18:02:40 +00:00
|
|
|
imageSize = service.resources.getDimensionPixelSize(code.name.monkey.retromusic.R.dimen.app_widget_card_image_size)
|
2018-12-08 03:33:02 +00:00
|
|
|
}
|
|
|
|
if (cardRadius == 0f) {
|
2019-09-16 18:02:40 +00:00
|
|
|
cardRadius = service.resources.getDimension(code.name.monkey.retromusic.R.dimen.app_widget_card_radius)
|
2018-12-08 03:33:02 +00:00
|
|
|
}
|
2018-12-12 20:59:07 +00:00
|
|
|
val appContext = service.applicationContext
|
2018-12-08 03:33:02 +00:00
|
|
|
// Load the album cover async and push the update on completion
|
|
|
|
service.runOnUiThread {
|
|
|
|
if (target != null) {
|
2019-09-16 18:02:40 +00:00
|
|
|
Glide.clear(target)
|
2018-12-08 03:33:02 +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>(imageSize, imageSize) {
|
|
|
|
override fun onResourceReady(resource: BitmapPaletteWrapper, glideAnimation: GlideAnimation<in BitmapPaletteWrapper>) {
|
2018-12-08 03:33:02 +00:00
|
|
|
val palette = resource.palette
|
2019-09-16 18:02:40 +00:00
|
|
|
update(resource.bitmap, palette.getVibrantColor(palette.getMutedColor(MaterialValueHelper.getSecondaryTextColor(service, true))))
|
2018-12-08 03:33:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 18:02:40 +00:00
|
|
|
override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) {
|
|
|
|
super.onLoadFailed(e, errorDrawable)
|
2018-12-08 03:33:02 +00:00
|
|
|
update(null, MaterialValueHelper.getSecondaryTextColor(service, true))
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun update(bitmap: Bitmap?, color: Int) {
|
|
|
|
// Set correct drawable for pause state
|
2019-09-16 18:02:40 +00:00
|
|
|
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, playPauseRes, color)))
|
|
|
|
|
2018-12-08 03:33:02 +00:00
|
|
|
// Set prev/next button drawables
|
2019-09-16 18:02:40 +00:00
|
|
|
appWidgetView.setImageViewBitmap(R.id.button_next, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_next_white_24dp, color)))
|
|
|
|
appWidgetView.setImageViewBitmap(R.id.button_prev, ImageUtil.createBitmap(ImageUtil.getTintedVectorDrawable(service, R.drawable.ic_skip_previous_white_24dp, color)))
|
2018-12-08 03:33:02 +00:00
|
|
|
|
|
|
|
val image = getAlbumArtDrawable(service.resources, bitmap)
|
2019-09-16 18:02:40 +00:00
|
|
|
val roundedBitmap = createRoundedBitmap(image, imageSize, imageSize, cardRadius, 0F, cardRadius, 0F)
|
2018-12-08 03:33:02 +00:00
|
|
|
appWidgetView.setImageViewBitmap(R.id.image, roundedBitmap)
|
|
|
|
|
|
|
|
pushUpdate(service, appWidgetIds, appWidgetView)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Link up various button actions using [PendingIntent].
|
|
|
|
*/
|
|
|
|
private fun linkButtons(context: Context, views: RemoteViews) {
|
|
|
|
val action: Intent = Intent(context, MainActivity::class.java).putExtra("expand", true)
|
|
|
|
var pendingIntent: PendingIntent
|
|
|
|
|
|
|
|
val serviceName = ComponentName(context, MusicService::class.java)
|
|
|
|
|
|
|
|
// Home
|
|
|
|
action.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
|
|
|
pendingIntent = PendingIntent.getActivity(context, 0, action, 0)
|
|
|
|
views.setOnClickPendingIntent(R.id.image, pendingIntent)
|
|
|
|
views.setOnClickPendingIntent(R.id.media_titles, pendingIntent)
|
|
|
|
|
|
|
|
// Previous track
|
2019-08-02 08:59:40 +00:00
|
|
|
pendingIntent = buildPendingIntent(context, ACTION_REWIND, serviceName)
|
2018-12-08 03:33:02 +00:00
|
|
|
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent)
|
|
|
|
|
|
|
|
// Play and pause
|
2019-08-02 08:59:40 +00:00
|
|
|
pendingIntent = buildPendingIntent(context, ACTION_TOGGLE_PAUSE, serviceName)
|
2018-12-08 03:33:02 +00:00
|
|
|
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent)
|
|
|
|
|
|
|
|
// Next track
|
2019-08-02 08:59:40 +00:00
|
|
|
pendingIntent = buildPendingIntent(context, ACTION_SKIP, serviceName)
|
2018-12-08 03:33:02 +00:00
|
|
|
views.setOnClickPendingIntent(R.id.button_next, pendingIntent)
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
|
|
|
const val NAME = "app_widget_card"
|
|
|
|
|
|
|
|
private var mInstance: AppWidgetCard? = null
|
|
|
|
private var imageSize = 0
|
|
|
|
private var cardRadius = 0f
|
|
|
|
|
|
|
|
val instance: AppWidgetCard
|
|
|
|
@Synchronized get() {
|
|
|
|
if (mInstance == null) {
|
|
|
|
mInstance = AppWidgetCard()
|
|
|
|
}
|
|
|
|
return mInstance!!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|