adding some widget improvements + cleanup

This commit is contained in:
tibbi 2020-04-19 10:54:15 +02:00
parent 28a637ec88
commit e1d4d45cfa
7 changed files with 117 additions and 111 deletions

View file

@ -58,7 +58,7 @@ android {
}
dependencies {
implementation 'com.simplemobiletools:commons:5.25.27'
implementation 'com.simplemobiletools:commons:5.26.0'
implementation 'org.greenrobot:eventbus:3.2.0'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'androidx.media:media:1.1.0'

View file

@ -55,12 +55,7 @@ class WidgetConfigureActivity : SimpleActivity() {
private fun initVariables() {
mBgColor = config.widgetBgColor
if (mBgColor == 1) {
mBgColor = Color.BLACK
mBgAlpha = .2f
} else {
mBgAlpha = Color.alpha(mBgColor) / 255.toFloat()
}
mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor))
config_bg_seekbar.setOnSeekBarChangeListener(seekbarChangeListener)
@ -74,8 +69,7 @@ class WidgetConfigureActivity : SimpleActivity() {
private fun saveConfig() {
val appWidgetManager = AppWidgetManager.getInstance(this)
val views = RemoteViews(packageName, R.layout.widget).apply {
setInt(R.id.widget_background, "setColorFilter", mBgColor)
setInt(R.id.widget_background, "setImageAlpha", Color.alpha(mBgColor))
applyColorFilter(R.id.widget_background, mBgColor)
}
appWidgetManager.updateAppWidget(mWidgetId, views)

View file

@ -6,9 +6,9 @@ import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.widget.RemoteViews
import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.getColoredBitmap
import com.simplemobiletools.commons.extensions.getLaunchIntent
import com.simplemobiletools.musicplayer.R
@ -17,121 +17,104 @@ import com.simplemobiletools.musicplayer.extensions.config
import com.simplemobiletools.musicplayer.extensions.sendIntent
import com.simplemobiletools.musicplayer.models.Song
import com.simplemobiletools.musicplayer.services.MusicService
import com.simplemobiletools.musicplayer.services.MusicService.Companion.mCurrSong
class MyWidgetProvider : AppWidgetProvider() {
private var mContext: Context? = null
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
performUpdate(context)
}
private fun performUpdate(context: Context) {
mContext = context
val appWidgetManager = AppWidgetManager.getInstance(context)
appWidgetManager.getAppWidgetIds(getComponentName()).forEach {
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
val views = getRemoteViews(appWidgetManager, context, it)
updateColors(views)
setupButtons(views)
updateSongInfo(views, MusicService.mCurrSong)
updatePlayPauseButton(views, MusicService.getIsPlaying())
updateColors(context, views)
setupButtons(context, views)
updatePlayPauseButton(context, views, MusicService.getIsPlaying())
appWidgetManager.updateAppWidget(it, views)
}
}
private fun getComponentName() = ComponentName(mContext!!, MyWidgetProvider::class.java)
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
when (action) {
SONG_CHANGED -> songChanged(context, intent)
SONG_STATE_CHANGED -> songStateChanged(context, intent)
PREVIOUS, PLAYPAUSE, NEXT -> context.sendIntent(action)
else -> super.onReceive(context, intent)
}
}
private fun setupIntent(views: RemoteViews, action: String, id: Int) {
val intent = Intent(mContext, MyWidgetProvider::class.java)
override fun onAppWidgetOptionsChanged(context: Context, appWidgetManager: AppWidgetManager, widgetId: Int, newOptions: Bundle) {
performUpdate(context)
super.onAppWidgetOptionsChanged(context, appWidgetManager, widgetId, newOptions)
}
private fun setupIntent(context: Context, views: RemoteViews, action: String, id: Int) {
val intent = Intent(context, MyWidgetProvider::class.java)
intent.action = action
val pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0)
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0)
views.setOnClickPendingIntent(id, pendingIntent)
}
private fun setupAppOpenIntent(views: RemoteViews, id: Int) {
val intent = mContext?.getLaunchIntent() ?: Intent(mContext, SplashActivity::class.java)
val pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0)
private fun setupAppOpenIntent(context: Context, views: RemoteViews, id: Int) {
val intent = context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
views.setOnClickPendingIntent(id, pendingIntent)
}
private fun songChanged(intent: Intent) {
private fun songChanged(context: Context, intent: Intent) {
val song = intent.getSerializableExtra(NEW_SONG) as? Song ?: return
val appWidgetManager = AppWidgetManager.getInstance(mContext)
appWidgetManager.getAppWidgetIds(getComponentName()).forEach {
val views = getRemoteViews(appWidgetManager, mContext!!, it)
val appWidgetManager = AppWidgetManager.getInstance(context)
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
val views = getRemoteViews(appWidgetManager, context, it)
updateSongInfo(views, song)
appWidgetManager.updateAppWidget(it, views)
}
}
private fun updateSongInfo(views: RemoteViews, currSong: Song?) {
var title = ""
var artist = ""
if (currSong != null) {
title = mCurrSong!!.title
artist = mCurrSong!!.artist
views.setTextViewText(R.id.song_info_title, currSong?.title ?: "")
views.setTextViewText(R.id.song_info_artist, currSong?.artist ?: "")
}
views.setTextViewText(R.id.song_info_title, title)
views.setTextViewText(R.id.song_info_artist, artist)
}
private fun songStateChanged(intent: Intent) {
private fun songStateChanged(context: Context, intent: Intent) {
val isPlaying = intent.getBooleanExtra(IS_PLAYING, false)
val appWidgetManager = AppWidgetManager.getInstance(mContext)
appWidgetManager.getAppWidgetIds(getComponentName()).forEach {
val views = getRemoteViews(appWidgetManager, mContext!!, it)
updatePlayPauseButton(views, isPlaying)
val appWidgetManager = AppWidgetManager.getInstance(context)
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
val views = getRemoteViews(appWidgetManager, context, it)
updatePlayPauseButton(context, views, isPlaying)
appWidgetManager.updateAppWidget(it, views)
}
}
private fun updatePlayPauseButton(views: RemoteViews, isPlaying: Boolean) {
private fun updatePlayPauseButton(context: Context, views: RemoteViews, isPlaying: Boolean) {
val drawableId = if (isPlaying) R.drawable.ic_pause_vector else R.drawable.ic_play_vector
val widgetTextColor = mContext!!.config.widgetTextColor
val icon = mContext!!.resources.getColoredBitmap(drawableId, widgetTextColor)
val widgetTextColor = context.config.widgetTextColor
val icon = context.resources.getColoredBitmap(drawableId, widgetTextColor)
views.setImageViewBitmap(R.id.play_pause_btn, icon)
}
private fun updateColors(views: RemoteViews) {
val config = mContext!!.config
val res = mContext!!.resources
private fun updateColors(context: Context, views: RemoteViews) {
val config = context.config
val widgetBgColor = config.widgetBgColor
val widgetTextColor = config.widgetTextColor
views.apply {
setInt(R.id.widget_background, "setColorFilter", widgetBgColor)
setInt(R.id.widget_background, "setImageAlpha", Color.alpha(widgetBgColor))
applyColorFilter(R.id.widget_background, widgetBgColor)
setTextColor(R.id.song_info_title, widgetTextColor)
setTextColor(R.id.song_info_artist, widgetTextColor)
setImageViewBitmap(R.id.previous_btn, res.getColoredBitmap(R.drawable.ic_previous_vector, widgetTextColor))
setImageViewBitmap(R.id.next_btn, res.getColoredBitmap(R.drawable.ic_next_vector, widgetTextColor))
setImageViewBitmap(R.id.previous_btn, context.resources.getColoredBitmap(R.drawable.ic_previous_vector, widgetTextColor))
setImageViewBitmap(R.id.next_btn, context.resources.getColoredBitmap(R.drawable.ic_next_vector, widgetTextColor))
}
}
override fun onReceive(context: Context, intent: Intent) {
mContext = context
val action = intent.action
when (action) {
SONG_CHANGED -> songChanged(intent)
SONG_STATE_CHANGED -> songStateChanged(intent)
PREVIOUS, PLAYPAUSE, NEXT -> context.sendIntent(action)
else -> super.onReceive(context, intent)
}
}
private fun setupButtons(context: Context, views: RemoteViews) {
setupIntent(context, views, PREVIOUS, R.id.previous_btn)
setupIntent(context, views, PLAYPAUSE, R.id.play_pause_btn)
setupIntent(context, views, NEXT, R.id.next_btn)
private fun setupButtons(views: RemoteViews) {
setupIntent(views, PREVIOUS, R.id.previous_btn)
setupIntent(views, PLAYPAUSE, R.id.play_pause_btn)
setupIntent(views, NEXT, R.id.next_btn)
setupAppOpenIntent(views, R.id.song_info_title)
setupAppOpenIntent(views, R.id.song_info_artist)
}
override fun onAppWidgetOptionsChanged(context: Context, appWidgetManager: AppWidgetManager, widgetId: Int, newOptions: Bundle) {
performUpdate(context)
super.onAppWidgetOptionsChanged(context, appWidgetManager, widgetId, newOptions)
setupAppOpenIntent(context, views, R.id.song_info_title)
setupAppOpenIntent(context, views, R.id.song_info_artist)
}
private fun getRemoteViews(appWidgetManager: AppWidgetManager, context: Context, widgetId: Int): RemoteViews {
@ -142,7 +125,14 @@ class MyWidgetProvider : AppWidgetProvider() {
context.config.initialWidgetHeight = minHeight
}
val layoutId = if (minHeight < context.config.initialWidgetHeight / 2) R.layout.small_widget else R.layout.widget
val layoutId = if (minHeight < context.config.initialWidgetHeight / 2) {
R.layout.small_widget
} else {
R.layout.widget
}
return RemoteViews(context.packageName, layoutId)
}
private fun getComponentName(context: Context) = ComponentName(context, MyWidgetProvider::class.java)
}

View file

@ -689,9 +689,11 @@ class MusicService : Service(), MediaPlayer.OnPreparedListener, MediaPlayer.OnEr
}
private fun broadcastSongChange(song: Song?) {
Handler(Looper.getMainLooper()).post {
broadcastUpdateWidgetSong(song)
EventBus.getDefault().post(Events.SongChanged(song))
}
}
private fun broadcastSongStateChange(isPlaying: Boolean) {
broadcastUpdateWidgetSongState(isPlaying)

View file

@ -1,12 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:gravity="center_horizontal">
android:gravity="center">
<include layout="@layout/widget_controls"/>
<ImageView
android:id="@+id/widget_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/widget_controls"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:src="@drawable/widget_background" />
<include
android:id="@+id/widget_controls"
layout="@layout/widget_controls"
android:layout_width="match_parent"
android:layout_height="@dimen/controls_row_height" />
</RelativeLayout>

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/widget_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -15,40 +16,46 @@
android:layout_alignParentEnd="true"
android:src="@drawable/widget_background" />
<RelativeLayout
android:id="@+id/song_info_holder"
android:layout_width="match_parent"
android:layout_height="@dimen/controls_row_height"
android:gravity="center_vertical"
android:paddingTop="@dimen/small_margin">
<TextView
android:id="@+id/song_info_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_margin"
android:layout_marginTop="@dimen/small_margin"
android:layout_marginRight="@dimen/activity_margin"
android:layout_marginStart="@dimen/activity_margin"
android:layout_marginEnd="@dimen/activity_margin"
android:ellipsize="end"
android:gravity="center"
android:lines="1"
android:text="@string/title"
android:textColor="@android:color/white"
android:textSize="@dimen/big_text_size" />
android:textSize="@dimen/big_text_size"
tools:text="@string/title" />
<TextView
android:id="@+id/song_info_artist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/song_info_title"
android:layout_marginLeft="@dimen/activity_margin"
android:layout_marginRight="@dimen/activity_margin"
android:layout_marginBottom="@dimen/small_margin"
android:layout_marginStart="@dimen/activity_margin"
android:layout_marginEnd="@dimen/activity_margin"
android:ellipsize="end"
android:gravity="center"
android:lines="1"
android:text="@string/artist"
android:textColor="@android:color/white"
android:textSize="@dimen/bigger_text_size" />
android:textSize="@dimen/bigger_text_size"
tools:text="@string/artist" />
</RelativeLayout>
<include
android:id="@+id/widget_controls"
layout="@layout/widget_controls"
android:layout_width="match_parent"
android:layout_height="@dimen/controls_row_height"
android:layout_below="@+id/song_info_artist" />
android:layout_below="@+id/song_info_holder" />
</RelativeLayout>

View file

@ -3,6 +3,7 @@
android:id="@+id/main_controls"
android:layout_width="match_parent"
android:layout_height="@dimen/controls_row_height"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView