From e2ac535308a1270ac29d9c9fccd8495947ba00c8 Mon Sep 17 00:00:00 2001 From: Michatec Date: Tue, 7 Jul 2026 12:21:31 +0200 Subject: [PATCH] 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. --- app/build.gradle.kts | 4 +- app/src/main/AndroidManifest.xml | 20 +++ app/src/main/java/com/michatec/radio/Keys.kt | 1 + .../java/com/michatec/radio/MainActivity.kt | 6 + app/src/main/java/com/michatec/radio/Radio.kt | 21 ++- .../radio/helpers/CollectionHelper.kt | 4 + .../radio/widget/FavoritesWidgetProvider.kt | 120 ++++++++++++++++++ .../radio/widget/FavoritesWidgetService.kt | 72 +++++++++++ .../main/res/drawable/widget_background.xml | 6 + .../widget_background_custom_base.xml | 5 + .../res/drawable/widget_background_dark.xml | 6 + app/src/main/res/layout/favorites_widget.xml | 68 ++++++++++ .../main/res/layout/favorites_widget_item.xml | 30 +++++ app/src/main/res/values-da/strings.xml | 5 + app/src/main/res/values-de/strings.xml | 5 + app/src/main/res/values-el/strings.xml | 5 + app/src/main/res/values-fr/strings.xml | 5 + app/src/main/res/values-ja/strings.xml | 5 + app/src/main/res/values-nl/strings.xml | 5 + app/src/main/res/values-pl/strings.xml | 5 + app/src/main/res/values-ru/strings.xml | 5 + app/src/main/res/values-uk/strings.xml | 5 + app/src/main/res/values/dimens.xml | 5 + app/src/main/res/values/strings.xml | 4 + .../res/xml-v31/favorites_widget_info.xml | 13 ++ .../main/res/xml/favorites_widget_info.xml | 11 ++ 26 files changed, 438 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/com/michatec/radio/widget/FavoritesWidgetProvider.kt create mode 100644 app/src/main/java/com/michatec/radio/widget/FavoritesWidgetService.kt create mode 100644 app/src/main/res/drawable/widget_background.xml create mode 100644 app/src/main/res/drawable/widget_background_custom_base.xml create mode 100644 app/src/main/res/drawable/widget_background_dark.xml create mode 100644 app/src/main/res/layout/favorites_widget.xml create mode 100644 app/src/main/res/layout/favorites_widget_item.xml create mode 100644 app/src/main/res/values/dimens.xml create mode 100644 app/src/main/res/xml-v31/favorites_widget_info.xml create mode 100644 app/src/main/res/xml/favorites_widget_info.xml diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 3bfd7dc..ac150fc 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -11,8 +11,8 @@ android { applicationId = "com.michatec.radio" minSdk = 28 targetSdk = 37 - versionCode = 150 - versionName = "15" + versionCode = 151 + versionName = "15.1" } compileOptions { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 01c783b..4c5211f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -154,6 +154,26 @@ + + + + + + + + + + + + { + val intent = Intent(Keys.ACTION_THEME_CHANGED).apply { + setPackage(packageName) + } + sendBroadcast(intent) + } + } } } diff --git a/app/src/main/java/com/michatec/radio/helpers/CollectionHelper.kt b/app/src/main/java/com/michatec/radio/helpers/CollectionHelper.kt index 1d59e03..56c5465 100644 --- a/app/src/main/java/com/michatec/radio/helpers/CollectionHelper.kt +++ b/app/src/main/java/com/michatec/radio/helpers/CollectionHelper.kt @@ -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 */ diff --git a/app/src/main/java/com/michatec/radio/widget/FavoritesWidgetProvider.kt b/app/src/main/java/com/michatec/radio/widget/FavoritesWidgetProvider.kt new file mode 100644 index 0000000..7704e88 --- /dev/null +++ b/app/src/main/java/com/michatec/radio/widget/FavoritesWidgetProvider.kt @@ -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) + } + } +} diff --git a/app/src/main/java/com/michatec/radio/widget/FavoritesWidgetService.kt b/app/src/main/java/com/michatec/radio/widget/FavoritesWidgetService.kt new file mode 100644 index 0000000..2d6cc4e --- /dev/null +++ b/app/src/main/java/com/michatec/radio/widget/FavoritesWidgetService.kt @@ -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 = 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 +} diff --git a/app/src/main/res/drawable/widget_background.xml b/app/src/main/res/drawable/widget_background.xml new file mode 100644 index 0000000..459ca62 --- /dev/null +++ b/app/src/main/res/drawable/widget_background.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/app/src/main/res/drawable/widget_background_custom_base.xml b/app/src/main/res/drawable/widget_background_custom_base.xml new file mode 100644 index 0000000..2ac4c9d --- /dev/null +++ b/app/src/main/res/drawable/widget_background_custom_base.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/drawable/widget_background_dark.xml b/app/src/main/res/drawable/widget_background_dark.xml new file mode 100644 index 0000000..ea0b4df --- /dev/null +++ b/app/src/main/res/drawable/widget_background_dark.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/app/src/main/res/layout/favorites_widget.xml b/app/src/main/res/layout/favorites_widget.xml new file mode 100644 index 0000000..ed34373 --- /dev/null +++ b/app/src/main/res/layout/favorites_widget.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/favorites_widget_item.xml b/app/src/main/res/layout/favorites_widget_item.xml new file mode 100644 index 0000000..d9a4bbe --- /dev/null +++ b/app/src/main/res/layout/favorites_widget_item.xml @@ -0,0 +1,30 @@ + + + + + + + diff --git a/app/src/main/res/values-da/strings.xml b/app/src/main/res/values-da/strings.xml index 383f569..4c39cc2 100644 --- a/app/src/main/res/values-da/strings.xml +++ b/app/src/main/res/values-da/strings.xml @@ -180,4 +180,9 @@ Grøn Blå Hex-kode + + + Favoritter + Ingen favoritter tilføjet endnu. + Viser en liste over dine yndlingsstationer for hurtig adgang. diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index b32cf54..4313611 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -182,4 +182,9 @@ Grün Blau Hex-Code + + + Favoriten + Noch keine Favoriten hinzugefügt. + Zeigt eine Liste deiner Favoriten-Sender für den Schnellzugriff. diff --git a/app/src/main/res/values-el/strings.xml b/app/src/main/res/values-el/strings.xml index 9ae0575..99a6273 100644 --- a/app/src/main/res/values-el/strings.xml +++ b/app/src/main/res/values-el/strings.xml @@ -182,4 +182,9 @@ Πράσινο Μπλε Κωδικός Hex + + + Αγαπημένα + Δεν έχουν προστεθεί ακόμα αγαπημένα. + Εμφανίζει μια λίστα με τους αγαπημένους σας σταθμούς για γρήγορη πρόσβαση. diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index ec3e935..2690963 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -180,4 +180,9 @@ Vert Bleu Code Hex + + + Favoris + Aucun favori ajouté pour le moment. + Affiche une liste de vos stations préférées pour un accès rapide. diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 5248550..adac38d 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -181,4 +181,9 @@ 緑 青 Hexコード + + + お気に入り + お気に入りはまだ追加されていません。 + お気に入りのステーションを一覧表示し、素早くアクセスできます。 diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index 60b5d92..968bd05 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -182,4 +182,9 @@ Groen Blauw Hex-code + + + Favorieten + Nog geen favorieten toegevoegd. + Toont een lijst met je favoriete zenders voor snelle toegang. diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 6bee091..83dc00e 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -182,4 +182,9 @@ Zielony Niebieski Kod Hex + + + Ulubione + Nie dodano jeszcze żadnych ulubionych. + Wyświetla listę ulubionych stacji, zapewniając szybki dostęp. diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 08f3be4..c98b423 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -182,4 +182,9 @@ Зеленый Синий Hex-код + + + Избранное + Избранное пока не добавлено. + Показывает список ваших любимых станций для быстрого доступа. diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index c9d4ec3..e929ca4 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -182,4 +182,9 @@ Зелений Синій Hex-код + + + Обране + Ще не додано жодної станції до обраного. + Показує список ваших улюблених станцій для швидкого доступу. diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml new file mode 100644 index 0000000..6f02627 --- /dev/null +++ b/app/src/main/res/values/dimens.xml @@ -0,0 +1,5 @@ + + + 8dp + 16dp + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7acc6a2..6c39671 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -218,4 +218,8 @@ Green Blue Hex Code + + Favorites + No favorites added yet. + Shows a list of your favorite stations for quick access. diff --git a/app/src/main/res/xml-v31/favorites_widget_info.xml b/app/src/main/res/xml-v31/favorites_widget_info.xml new file mode 100644 index 0000000..a903d1e --- /dev/null +++ b/app/src/main/res/xml-v31/favorites_widget_info.xml @@ -0,0 +1,13 @@ + + + diff --git a/app/src/main/res/xml/favorites_widget_info.xml b/app/src/main/res/xml/favorites_widget_info.xml new file mode 100644 index 0000000..7b53bab --- /dev/null +++ b/app/src/main/res/xml/favorites_widget_info.xml @@ -0,0 +1,11 @@ + + +