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 { dependencies {
implementation 'com.simplemobiletools:commons:5.25.27' implementation 'com.simplemobiletools:commons:5.26.0'
implementation 'org.greenrobot:eventbus:3.2.0' implementation 'org.greenrobot:eventbus:3.2.0'
implementation 'com.squareup.picasso:picasso:2.71828' implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'androidx.media:media:1.1.0' implementation 'androidx.media:media:1.1.0'

View file

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

View file

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

View file

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

View file

@ -1,12 +1,24 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_holder" android:id="@+id/widget_holder"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@android:color/black" android:gravity="center">
android:gravity="center_horizontal">
<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> </RelativeLayout>

View file

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

View file

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