[Android] Prevent a crash when thumbnail size is too large for widgets

Nicolas Pomepuy git at videolan.org
Mon Jan 27 16:15:25 CET 2020


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Jan 27 15:17:13 2020 +0100| [e6d4686f02f1f12ebb85a71cabe3720b716fd8a9] | committer: Geoffrey Métais

Prevent a crash when thumbnail size is too large for widgets

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

 .../org/videolan/vlc/widget/VLCAppWidgetProvider.kt    | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.kt b/application/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.kt
index dd728392e..a64a28b01 100644
--- a/application/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.kt
+++ b/application/vlc-android/src/org/videolan/vlc/widget/VLCAppWidgetProvider.kt
@@ -27,10 +27,13 @@ import android.appwidget.AppWidgetProvider
 import android.content.ComponentName
 import android.content.Context
 import android.content.Intent
+import android.graphics.Bitmap
 import android.net.Uri
 import android.os.Build
 import android.text.TextUtils
+import android.util.DisplayMetrics
 import android.view.View
+import android.view.WindowManager
 import android.widget.RemoteViews
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
@@ -115,10 +118,12 @@ abstract class VLCAppWidgetProvider : AppWidgetProvider() {
                 if (!TextUtils.isEmpty(artworkMrl)) {
                     runIO(Runnable {
                         val cover = AudioUtil.readCoverBitmap(Uri.decode(artworkMrl), 320)
+                        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
+                        val dm = DisplayMetrics().also { wm.defaultDisplay.getMetrics(it) }
                         runOnMainThread(Runnable {
-                            if (cover != null)
-                                views.setImageViewBitmap(R.id.cover, cover)
-                            else
+                            if (cover != null) {
+                                if (cover.byteSize() < dm.widthPixels * dm.heightPixels * 6) views.setImageViewBitmap(R.id.cover, cover)
+                            } else
                                 views.setImageViewResource(R.id.cover, R.drawable.icon)
                             views.setProgressBar(R.id.timeline, 100, 0, false)
                             applyUpdate(context, views, partial)
@@ -169,3 +174,10 @@ abstract class VLCAppWidgetProvider : AppWidgetProvider() {
         val ACTION_WIDGET_DISABLED = ACTION_WIDGET_PREFIX + "DISABLED"
     }
 }
+
+fun Bitmap.byteSize(): Int {
+    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
+        return allocationByteCount
+    }
+    return rowBytes * height
+}
\ No newline at end of file



More information about the Android mailing list