mirror of
https://github.com/Michatec/Radio.git
synced 2026-11-17 05:06:53 +01:00
feat(widget): add favorites widget
Implement a home screen widget for quick access to favorite stations. Features theme-aware styling, custom theme color support, and updates on collection or theme changes. Includes localized strings for all supported languages.
This commit is contained in:
1 parent
12f8b649ce
commit
e2ac535308
26 files changed
+438
-3
No files matched your search
@@ -18,6 +18,7 @@ object Keys {
|
||||
// intent actions
|
||||
const val ACTION_SHOW_PLAYER: String = "com.michatec.radio.action.SHOW_PLAYER"
|
||||
const val ACTION_COLLECTION_CHANGED: String = "com.michatec.radio.action.COLLECTION_CHANGED"
|
||||
const val ACTION_THEME_CHANGED: String = "com.michatec.radio.action.THEME_CHANGED"
|
||||
const val ACTION_START: String = "com.michatec.radio.action.START"
|
||||
|
||||
// intent extras
|
||||
|
||||
@@ -179,6 +179,12 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
/* Overrides onNewIntent from AppCompatActivity */
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
setIntent(intent)
|
||||
}
|
||||
|
||||
|
||||
/* Overrides onSupportNavigateUp from AppCompatActivity */
|
||||
override fun onSupportNavigateUp(): Boolean {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.michatec.radio
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Intent
|
||||
import android.content.SharedPreferences
|
||||
import com.michatec.radio.helpers.AppThemeHelper
|
||||
import com.michatec.radio.helpers.LanguageHelper
|
||||
import com.michatec.radio.helpers.PreferencesHelper
|
||||
@@ -10,7 +12,7 @@ import com.michatec.radio.helpers.PreferencesHelper.initPreferences
|
||||
/**
|
||||
* Radio.class
|
||||
*/
|
||||
class Radio : Application() {
|
||||
class Radio : Application(), SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
|
||||
/* Implements onCreate */
|
||||
@@ -20,6 +22,23 @@ class Radio : Application() {
|
||||
// set Dark / Light theme state
|
||||
AppThemeHelper.setTheme(PreferencesHelper.loadThemeSelection())
|
||||
LanguageHelper.setLanguage(this, PreferencesHelper.loadSelectedLanguage())
|
||||
|
||||
// register listener for changes in shared preferences
|
||||
PreferencesHelper.registerPreferenceChangeListener(this)
|
||||
}
|
||||
|
||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
||||
when (key) {
|
||||
Keys.PREF_THEME_SELECTION,
|
||||
Keys.PREF_CUSTOM_THEME_COLOR,
|
||||
Keys.PREF_CUSTOM_THEME_ENABLED,
|
||||
Keys.PREF_CUSTOM_THEME_INDEX -> {
|
||||
val intent = Intent(Keys.ACTION_THEME_CHANGED).apply {
|
||||
setPackage(packageName)
|
||||
}
|
||||
sendBroadcast(intent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -623,11 +623,15 @@ object CollectionHelper {
|
||||
Log.v(TAG, "Broadcasting that collection has changed.")
|
||||
val collectionChangedIntent = Intent()
|
||||
collectionChangedIntent.action = Keys.ACTION_COLLECTION_CHANGED
|
||||
collectionChangedIntent.setPackage(context.packageName)
|
||||
collectionChangedIntent.putExtra(
|
||||
Keys.EXTRA_COLLECTION_MODIFICATION_DATE,
|
||||
modificationDate.time
|
||||
)
|
||||
// Send local broadcast
|
||||
LocalBroadcastManager.getInstance(context).sendBroadcast(collectionChangedIntent)
|
||||
// Send global broadcast for widgets
|
||||
context.sendBroadcast(collectionChangedIntent)
|
||||
}
|
||||
|
||||
/* Creates a MediaItem with MediaMetadata for a single radio station - used to prepare player */
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
package com.michatec.radio.widget
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.appwidget.AppWidgetProvider
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.res.Configuration
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.view.View
|
||||
import android.widget.RemoteViews
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.michatec.radio.Keys
|
||||
import com.michatec.radio.MainActivity
|
||||
import com.michatec.radio.R
|
||||
import com.michatec.radio.helpers.FileHelper
|
||||
import com.michatec.radio.helpers.PreferencesHelper
|
||||
import com.michatec.radio.helpers.PreferencesHelper.initPreferences
|
||||
|
||||
class FavoritesWidgetProvider : AppWidgetProvider() {
|
||||
|
||||
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
|
||||
for (appWidgetId in appWidgetIds) {
|
||||
updateAppWidget(context, appWidgetManager, appWidgetId)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
super.onReceive(context, intent)
|
||||
if (intent.action == Keys.ACTION_COLLECTION_CHANGED || intent.action == Keys.ACTION_THEME_CHANGED) {
|
||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||
val componentName = ComponentName(context, FavoritesWidgetProvider::class.java)
|
||||
val appWidgetIds = appWidgetManager.getAppWidgetIds(componentName)
|
||||
|
||||
onUpdate(context, appWidgetManager, appWidgetIds)
|
||||
|
||||
// For older versions, also explicitly notify the list that data has changed
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
|
||||
@Suppress("DEPRECATION")
|
||||
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.favorites_list)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun updateAppWidget(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
|
||||
context.initPreferences()
|
||||
|
||||
val themeSelection = PreferencesHelper.loadThemeSelection()
|
||||
val configuration = Configuration(context.resources.configuration)
|
||||
when (themeSelection) {
|
||||
Keys.STATE_THEME_LIGHT_MODE -> configuration.uiMode = (configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK.inv()) or Configuration.UI_MODE_NIGHT_NO
|
||||
Keys.STATE_THEME_DARK_MODE -> configuration.uiMode = (configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK.inv()) or Configuration.UI_MODE_NIGHT_YES
|
||||
}
|
||||
val themedContext = context.createConfigurationContext(configuration)
|
||||
|
||||
val views = RemoteViews(themedContext.packageName, R.layout.favorites_widget)
|
||||
val iconColor = ContextCompat.getColor(themedContext, R.color.icon_default)
|
||||
val textColor = ContextCompat.getColor(themedContext, R.color.text_default)
|
||||
|
||||
views.setTextColor(R.id.widget_title, textColor)
|
||||
views.setInt(R.id.widget_icon, "setColorFilter", iconColor)
|
||||
views.setTextColor(R.id.empty_view, textColor)
|
||||
|
||||
if (PreferencesHelper.loadCustomThemeEnabled()) {
|
||||
val customColor = PreferencesHelper.loadCustomThemeColor(themedContext)
|
||||
views.setViewVisibility(R.id.widget_background_image, View.VISIBLE)
|
||||
views.setImageViewResource(R.id.widget_background_image, R.drawable.widget_background_custom_base)
|
||||
views.setInt(R.id.widget_background_image, "setColorFilter", customColor)
|
||||
views.setInt(R.id.widget_root, "setBackgroundResource", 0)
|
||||
} else {
|
||||
views.setViewVisibility(R.id.widget_background_image, View.GONE)
|
||||
val backgroundRes = when (themeSelection) {
|
||||
Keys.STATE_THEME_DARK_MODE -> R.drawable.widget_background_dark
|
||||
else -> R.drawable.widget_background
|
||||
}
|
||||
views.setInt(R.id.widget_root, "setBackgroundResource", backgroundRes)
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
val collection = FileHelper.readCollection(themedContext)
|
||||
val favorites = collection.stations.filter { it.starred }
|
||||
|
||||
val builder = RemoteViews.RemoteCollectionItems.Builder()
|
||||
for (station in favorites) {
|
||||
val itemViews = RemoteViews(themedContext.packageName, R.layout.favorites_widget_item)
|
||||
itemViews.setTextViewText(R.id.station_name, station.name)
|
||||
itemViews.setTextColor(R.id.station_name, textColor)
|
||||
itemViews.setInt(R.id.station_icon, "setColorFilter", iconColor)
|
||||
|
||||
val fillInIntent = Intent().apply { putExtra(Keys.EXTRA_STATION_UUID, station.uuid) }
|
||||
itemViews.setOnClickFillInIntent(R.id.station_name, fillInIntent)
|
||||
itemViews.setOnClickFillInIntent(R.id.station_icon, fillInIntent)
|
||||
|
||||
builder.addItem(station.uuid.hashCode().toLong(), itemViews)
|
||||
}
|
||||
views.setRemoteAdapter(R.id.favorites_list, builder.build())
|
||||
} else {
|
||||
val intent = Intent(themedContext, FavoritesWidgetService::class.java).apply {
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
|
||||
}
|
||||
intent.data = Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))
|
||||
@Suppress("DEPRECATION")
|
||||
views.setRemoteAdapter(R.id.favorites_list, intent)
|
||||
}
|
||||
|
||||
views.setEmptyView(R.id.favorites_list, R.id.empty_view)
|
||||
|
||||
val clickIntent = Intent(themedContext, MainActivity::class.java).apply {
|
||||
action = Keys.ACTION_START
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
}
|
||||
views.setPendingIntentTemplate(R.id.favorites_list, PendingIntent.getActivity(themedContext, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE))
|
||||
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.michatec.radio.widget
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.res.Configuration
|
||||
import android.widget.RemoteViews
|
||||
import android.widget.RemoteViewsService
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.michatec.radio.Keys
|
||||
import com.michatec.radio.R
|
||||
import com.michatec.radio.core.Station
|
||||
import com.michatec.radio.helpers.FileHelper
|
||||
import com.michatec.radio.helpers.PreferencesHelper
|
||||
|
||||
class FavoritesWidgetService : RemoteViewsService() {
|
||||
override fun onGetViewFactory(intent: Intent): RemoteViewsFactory {
|
||||
return FavoritesWidgetFactory(this.applicationContext)
|
||||
}
|
||||
}
|
||||
|
||||
class FavoritesWidgetFactory(private val context: Context) : RemoteViewsService.RemoteViewsFactory {
|
||||
|
||||
private var favorites: List<Station> = listOf()
|
||||
|
||||
override fun onCreate() {}
|
||||
|
||||
override fun onDataSetChanged() {
|
||||
val collection = FileHelper.readCollection(context)
|
||||
favorites = collection.stations.filter { it.starred }
|
||||
}
|
||||
|
||||
override fun onDestroy() {}
|
||||
|
||||
override fun getCount(): Int = favorites.size
|
||||
|
||||
override fun getViewAt(position: Int): RemoteViews {
|
||||
val station = favorites[position]
|
||||
|
||||
val themeSelection = PreferencesHelper.loadThemeSelection()
|
||||
val configuration = Configuration(context.resources.configuration)
|
||||
when (themeSelection) {
|
||||
Keys.STATE_THEME_LIGHT_MODE -> configuration.uiMode = (configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK.inv()) or Configuration.UI_MODE_NIGHT_NO
|
||||
Keys.STATE_THEME_DARK_MODE -> configuration.uiMode = (configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK.inv()) or Configuration.UI_MODE_NIGHT_YES
|
||||
}
|
||||
val themedContext = context.createConfigurationContext(configuration)
|
||||
|
||||
val views = RemoteViews(themedContext.packageName, R.layout.favorites_widget_item)
|
||||
views.setTextViewText(R.id.station_name, station.name)
|
||||
|
||||
val iconColor = ContextCompat.getColor(themedContext, R.color.icon_default)
|
||||
val textColor = ContextCompat.getColor(themedContext, R.color.text_default)
|
||||
views.setTextColor(R.id.station_name, textColor)
|
||||
views.setInt(R.id.station_icon, "setColorFilter", iconColor)
|
||||
|
||||
// Handle click
|
||||
val fillInIntent = Intent().apply {
|
||||
putExtra(Keys.EXTRA_STATION_UUID, station.uuid)
|
||||
}
|
||||
views.setOnClickFillInIntent(R.id.station_name, fillInIntent)
|
||||
views.setOnClickFillInIntent(R.id.station_icon, fillInIntent)
|
||||
|
||||
return views
|
||||
}
|
||||
|
||||
override fun getLoadingView(): RemoteViews? = null
|
||||
|
||||
override fun getViewTypeCount(): Int = 1
|
||||
|
||||
override fun getItemId(position: Int): Long = position.toLong()
|
||||
|
||||
override fun hasStableIds(): Boolean = true
|
||||
}
|
||||
Reference in new issue
Block a user