PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/appwidgets/AppWidgetBig.kt

234 lines
8.5 KiB
Kotlin
Raw Normal View History

2019-03-03 09:20:15 +00:00
/*
2020-10-06 08:46:04 +00:00
* Copyright (c) 2020 Hemanth Savarla.
2019-03-03 09:20:15 +00:00
*
* Licensed under the GNU General Public License v3
*
2020-10-06 08:46:04 +00:00
* 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.
2019-03-03 09:20:15 +00:00
*
* 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.
2020-10-06 08:46:04 +00:00
*
2019-03-03 09:20:15 +00:00
*/
2018-12-08 03:33:02 +00:00
package code.name.monkey.retromusic.appwidgets
import android.app.PendingIntent
2019-12-30 04:53:51 +00:00
import android.content.ComponentName
import android.content.Context
import android.content.Intent
2018-12-08 03:33:02 +00:00
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
import code.name.monkey.retromusic.activities.MainActivity
2018-12-08 03:33:02 +00:00
import code.name.monkey.retromusic.appwidgets.base.BaseAppWidget
import code.name.monkey.retromusic.glide.SongGlideRequest
2018-12-08 03:33:02 +00:00
import code.name.monkey.retromusic.service.MusicService
import code.name.monkey.retromusic.service.MusicService.*
2018-12-08 03:33:02 +00:00
import code.name.monkey.retromusic.util.RetroUtil
import com.bumptech.glide.Glide
import com.bumptech.glide.request.animation.GlideAnimation
2019-12-30 04:53:51 +00:00
import com.bumptech.glide.request.target.SimpleTarget
2018-12-08 03:33:02 +00:00
import com.bumptech.glide.request.target.Target
2018-12-12 20:59:07 +00:00
2018-12-08 03:33:02 +00:00
class AppWidgetBig : BaseAppWidget() {
2019-12-30 04:53:51 +00:00
private var target: Target<Bitmap>? = 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) {
val appWidgetView = RemoteViews(
2020-05-13 18:35:34 +00:00
context.packageName, R.layout.app_widget_big
2019-12-30 04:53:51 +00:00
)
appWidgetView.setViewVisibility(
2020-05-13 18:35:34 +00:00
R.id.media_titles,
View.INVISIBLE
2019-12-30 04:53:51 +00:00
)
2020-02-25 13:15:23 +00:00
appWidgetView.setImageViewResource(R.id.image, R.drawable.default_audio_art)
2019-12-30 04:53:51 +00:00
appWidgetView.setImageViewBitmap(
2020-05-13 18:35:34 +00:00
R.id.button_next, createBitmap(
2019-12-30 04:53:51 +00:00
RetroUtil.getTintedVectorDrawable(
context,
2020-07-19 21:00:30 +00:00
R.drawable.ic_skip_next,
MaterialValueHelper.getPrimaryTextColor(context, false)
2019-12-30 04:53:51 +00:00
)!!, 1f
)
2019-12-30 04:53:51 +00:00
)
appWidgetView.setImageViewBitmap(
2020-05-13 18:35:34 +00:00
R.id.button_prev, createBitmap(
2019-12-30 04:53:51 +00:00
RetroUtil.getTintedVectorDrawable(
context,
2020-07-19 21:00:30 +00:00
R.drawable.ic_skip_previous,
MaterialValueHelper.getPrimaryTextColor(context, false)
2019-12-30 04:53:51 +00:00
)!!, 1f
)
2019-12-30 04:53:51 +00:00
)
appWidgetView.setImageViewBitmap(
R.id.button_toggle_play_pause, BaseAppWidget.Companion.createBitmap(
2019-12-30 04:53:51 +00:00
RetroUtil.getTintedVectorDrawable(
context,
2020-05-13 18:35:34 +00:00
R.drawable.ic_play_arrow_white_32dp,
MaterialValueHelper.getPrimaryTextColor(context, false)
2019-12-30 04:53:51 +00:00
)!!, 1f
)
2019-12-30 04:53:51 +00:00
)
linkButtons(context, appWidgetView)
pushUpdate(context, appWidgetIds, appWidgetView)
}
/**
* Update all active widget instances by pushing changes
*/
override fun performUpdate(service: MusicService, appWidgetIds: IntArray?) {
val appWidgetView = RemoteViews(
2020-05-13 18:35:34 +00:00
service.packageName, R.layout.app_widget_big
2019-12-30 04:53:51 +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(
2020-05-13 18:35:34 +00:00
R.id.media_titles,
View.INVISIBLE
2019-12-30 04:53:51 +00:00
)
} else {
appWidgetView.setViewVisibility(
2020-05-13 18:35:34 +00:00
R.id.media_titles,
View.VISIBLE
2019-12-30 04:53:51 +00:00
)
2020-05-13 18:35:34 +00:00
appWidgetView.setTextViewText(R.id.title, song.title)
2019-12-30 04:53:51 +00:00
appWidgetView.setTextViewText(
2020-05-13 18:35:34 +00:00
R.id.text,
getSongArtistAndAlbum(song)
2019-12-30 04:53:51 +00:00
)
}
2020-05-13 18:35:34 +00:00
val primaryColor = MaterialValueHelper.getPrimaryTextColor(service, false)
2019-12-30 04:53:51 +00:00
// Set correct drawable for pause state
val playPauseRes =
2020-07-19 21:00:30 +00:00
if (isPlaying) R.drawable.ic_pause else R.drawable.ic_play_arrow_white_32dp
2019-12-30 04:53:51 +00:00
appWidgetView.setImageViewBitmap(
2020-05-13 18:35:34 +00:00
R.id.button_toggle_play_pause, createBitmap(
2019-12-30 04:53:51 +00:00
RetroUtil.getTintedVectorDrawable(
service,
playPauseRes,
2020-05-13 18:35:34 +00:00
primaryColor
2019-12-30 04:53:51 +00:00
)!!, 1f
)
2019-12-30 04:53:51 +00:00
)
// Set prev/next button drawables
appWidgetView.setImageViewBitmap(
2020-05-13 18:35:34 +00:00
R.id.button_next, createBitmap(
2019-12-30 04:53:51 +00:00
RetroUtil.getTintedVectorDrawable(
service,
2020-07-19 21:00:30 +00:00
R.drawable.ic_skip_next,
2020-05-13 18:35:34 +00:00
primaryColor
2019-12-30 04:53:51 +00:00
)!!, 1f
)
2019-12-30 04:53:51 +00:00
)
appWidgetView.setImageViewBitmap(
2020-05-13 18:35:34 +00:00
R.id.button_prev, createBitmap(
2019-12-30 04:53:51 +00:00
RetroUtil.getTintedVectorDrawable(
service,
2020-07-19 21:00:30 +00:00
R.drawable.ic_skip_previous,
2020-05-13 18:35:34 +00:00
primaryColor
2019-12-30 04:53:51 +00:00
)!!, 1f
)
2019-12-30 04:53:51 +00:00
)
// Link actions buttons to intents
linkButtons(service, appWidgetView)
// Load the album cover async and push the update on completion
val p = RetroUtil.getScreenSize(service)
val widgetImageSize = Math.min(p.x, p.y)
val appContext = service.applicationContext
service.runOnUiThread {
if (target != null) {
Glide.clear(target)
}
target = SongGlideRequest.Builder.from(Glide.with(appContext), song)
.checkIgnoreMediaStore(appContext).asBitmap().build()
.into(object : SimpleTarget<Bitmap>(widgetImageSize, widgetImageSize) {
override fun onResourceReady(
resource: Bitmap,
glideAnimation: GlideAnimation<in Bitmap>
) {
update(resource)
}
override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) {
super.onLoadFailed(e, errorDrawable)
update(null)
}
private fun update(bitmap: Bitmap?) {
if (bitmap == null) {
appWidgetView.setImageViewResource(
R.id.image,
R.drawable.default_audio_art
)
} else {
appWidgetView.setImageViewBitmap(R.id.image, bitmap)
2019-12-30 04:53:51 +00:00
}
pushUpdate(appContext, appWidgetIds, appWidgetView)
}
2020-05-13 18:35:34 +00:00
})
2019-12-30 04:53:51 +00:00
}
}
/**
* Link up various button actions using [PendingIntent].
*/
private fun linkButtons(context: Context, views: RemoteViews) {
2020-05-23 13:53:10 +00:00
val action =
Intent(context, MainActivity::class.java).putExtra(MainActivity.EXPAND_PANEL, true)
2019-12-30 04:53:51 +00:00
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.clickable_area, pendingIntent)
// Previous track
pendingIntent = buildPendingIntent(context, ACTION_REWIND, serviceName)
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent)
// Play and pause
pendingIntent = buildPendingIntent(context, ACTION_TOGGLE_PAUSE, serviceName)
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent)
// Next track
pendingIntent = buildPendingIntent(context, ACTION_SKIP, serviceName)
views.setOnClickPendingIntent(R.id.button_next, pendingIntent)
}
companion object {
const val NAME: String = "app_widget_big"
private var mInstance: AppWidgetBig? = null
val instance: AppWidgetBig
@Synchronized get() {
if (mInstance == null) {
mInstance = AppWidgetBig()
}
return mInstance!!
}
}
2018-12-08 03:33:02 +00:00
}