[Android] Fix race condition preventing widgets to be deleted

Nicolas Pomepuy git at videolan.org
Wed May 18 09:57:56 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri May  6 09:55:43 2022 +0200| [982569cb51607e98f9c3617a5587e730f95f5330] | committer: Duncan McNamara

Fix race condition preventing widgets to be deleted

> https://code.videolan.org/videolan/vlc-android/commit/982569cb51607e98f9c3617a5587e730f95f5330
---

 .../src/org/videolan/vlc/widget/MiniPlayerAppWidgetProvider.kt   | 6 +++---
 .../src/org/videolan/vlc/widget/MiniPlayerConfigureActivity.kt   | 9 +++++----
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerAppWidgetProvider.kt b/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerAppWidgetProvider.kt
index da37c58b0..6dfecdcb4 100644
--- a/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerAppWidgetProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerAppWidgetProvider.kt
@@ -124,14 +124,14 @@ class MiniPlayerAppWidgetProvider : AppWidgetProvider() {
 
     }
 
-    suspend fun layoutWidget(context: Context, appWidgetId: Int, intent: Intent, forPreview: Boolean = false, previewBitmap: Bitmap? = null, previewPalette: Palette? = null): RemoteViews {
+    suspend fun layoutWidget(context: Context, appWidgetId: Int, intent: Intent, forPreview: Boolean = false, previewBitmap: Bitmap? = null, previewPalette: Palette? = null): RemoteViews? {
 
         val partial = ACTION_WIDGET_INIT != intent.action
         if (BuildConfig.DEBUG) Log.d("AppWidget", "layoutWidget widget id $appWidgetId / partial: $partial / action = ${intent.action}")
 
         val widgetRepository = getWidgetRepository(context)
         val persitedWidget = widgetRepository.getWidget(appWidgetId)
-                ?: widgetRepository.createNew(context, appWidgetId)
+                ?: return null
 
         val widgetCacheEntry = if (forPreview) WidgetCacheEntry(persitedWidget, getFakeMedia()) else WidgetCache.getEntry(persitedWidget)
                 ?: WidgetCache.addEntry(persitedWidget)
@@ -472,7 +472,7 @@ class MiniPlayerAppWidgetProvider : AppWidgetProvider() {
     }
 
 
-    private fun applyUpdate(context: Context, views: RemoteViews, partial: Boolean, appWidgetId: Int) {
+    private fun applyUpdate(context: Context, views: RemoteViews?, partial: Boolean, appWidgetId: Int) {
         if (BuildConfig.DEBUG) Log.d("AppWidget", "Apply update")
         val manager = AppWidgetManager.getInstance(context)
         try {
diff --git a/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerConfigureActivity.kt b/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerConfigureActivity.kt
index 7e471b5c0..f8260ac61 100644
--- a/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerConfigureActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerConfigureActivity.kt
@@ -31,7 +31,6 @@ import android.graphics.Bitmap
 import android.graphics.BitmapFactory
 import android.os.Bundle
 import android.util.Log
-import android.view.View
 import androidx.core.os.bundleOf
 import androidx.lifecycle.ViewModelProvider
 import androidx.lifecycle.lifecycleScope
@@ -127,9 +126,11 @@ class MiniPlayerConfigureActivity : BaseActivity() {
                         val width = if (widget.width <= 0 || widget.height <= 0) 276 else widget.width
                         val height = if (widget.width <= 0 || widget.height <= 0) 94 else widget.height
                         val views = provider.layoutWidget(this at MiniPlayerConfigureActivity, id, Intent(MiniPlayerAppWidgetProvider.ACTION_WIDGET_INIT), true, coverBitmap, palette)
-                        val preview: View = views.apply(applicationContext, null)
-                        val bm: Bitmap = bitmapFromView(preview, width.dp, height.dp)
-                        withContext(Dispatchers.Main) { binding.widgetPreview.setImageBitmap(bm) }
+                        val preview = views?.apply(applicationContext, null)
+                        preview?.let {
+                            val bm: Bitmap = bitmapFromView(it, width.dp, height.dp)
+                            withContext(Dispatchers.Main) { binding.widgetPreview.setImageBitmap(bm) }
+                        }
                     }
                 }
             }



More information about the Android mailing list