[Android] Refresh all widgets when leaving the preferences

Nicolas Pomepuy git at videolan.org
Mon Nov 18 08:15:30 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Nov 18 08:59:13 2024 +0100| [6ef124525c9935b8ee838c9384b3309531662d83] | committer: Nicolas Pomepuy

Refresh all widgets when leaving the preferences

Fixes #3104

> https://code.videolan.org/videolan/vlc-android/commit/6ef124525c9935b8ee838c9384b3309531662d83
---

 .../videolan/vlc/gui/preferences/PreferencesActivity.kt |  6 ++++++
 .../src/org/videolan/vlc/widget/utils/WidgetUtils.kt    | 17 +++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesActivity.kt b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesActivity.kt
index 103a3d21e5..2d3105f561 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesActivity.kt
@@ -49,6 +49,7 @@ import org.videolan.vlc.gui.PinCodeReason
 import org.videolan.vlc.gui.preferences.search.PreferenceItem
 import org.videolan.vlc.gui.preferences.search.PreferenceParser
 import org.videolan.vlc.gui.preferences.search.PreferenceSearchActivity
+import org.videolan.vlc.widget.utils.refreshAllWidgets
 
 const val EXTRA_PREF_END_POINT = "extra_pref_end_point"
 class PreferencesActivity : BaseActivity() {
@@ -81,6 +82,11 @@ class PreferencesActivity : BaseActivity() {
         mAppBarLayout!!.post { ViewCompat.setElevation(mAppBarLayout!!, resources.getDimensionPixelSize(R.dimen.default_appbar_elevation).toFloat()) }
     }
 
+    override fun onStop() {
+        refreshAllWidgets()
+        super.onStop()
+    }
+
     internal fun expandBar() {
         mAppBarLayout!!.setExpanded(true)
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/widget/utils/WidgetUtils.kt b/application/vlc-android/src/org/videolan/vlc/widget/utils/WidgetUtils.kt
index b477af5703..57c3abd4b4 100644
--- a/application/vlc-android/src/org/videolan/vlc/widget/utils/WidgetUtils.kt
+++ b/application/vlc-android/src/org/videolan/vlc/widget/utils/WidgetUtils.kt
@@ -24,7 +24,10 @@
 
 package org.videolan.vlc.widget.utils
 
+import android.appwidget.AppWidgetManager
+import android.content.ComponentName
 import android.content.Context
+import android.content.Intent
 import android.graphics.*
 import androidx.annotation.LayoutRes
 import androidx.core.content.ContextCompat
@@ -36,6 +39,7 @@ import com.google.android.material.color.DynamicColors
 import org.videolan.tools.dp
 import org.videolan.vlc.R
 import org.videolan.vlc.mediadb.models.Widget
+import org.videolan.vlc.widget.MiniPlayerAppWidgetProvider
 
 
 /**
@@ -170,6 +174,19 @@ fun WidgetCacheEntry.generateCircularProgressbar(context: Context, size: Float,
 
 }
 
+fun Context.refreshAllWidgets() {
+    val intent = Intent(this, MiniPlayerAppWidgetProvider::class.java)
+    intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE)
+
+
+    // Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
+// since it seems the onUpdate() is only fired on that:
+    val ids: IntArray = AppWidgetManager.getInstance(this)
+        .getAppWidgetIds(ComponentName(this, MiniPlayerAppWidgetProvider::class.java))
+    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
+    sendBroadcast(intent)
+}
+
 /**
  * Generates a pill progress bar [Bitmap]
  *



More information about the Android mailing list