Twelve: Enforce bitmaps to be 512x512 in widgets
There's an hard size limit on those in remote views
diff --git a/app/src/main/java/org/lineageos/twelve/ui/widgets/AppWidgetUpdater.kt b/app/src/main/java/org/lineageos/twelve/ui/widgets/AppWidgetUpdater.kt
index 2fb3bce..f0fb54b 100644
--- a/app/src/main/java/org/lineageos/twelve/ui/widgets/AppWidgetUpdater.kt
+++ b/app/src/main/java/org/lineageos/twelve/ui/widgets/AppWidgetUpdater.kt
@@ -10,7 +10,16 @@
import android.content.ComponentName
import android.content.Context
import android.widget.RemoteViews
+import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
+import coil3.Image
+import coil3.imageLoader
+import coil3.request.ImageRequest
+import coil3.request.allowHardware
+import coil3.request.maxBitmapSize
+import coil3.size.Size
+import coil3.target.Target
+import coil3.toBitmap
import kotlin.reflect.KClass
/**
@@ -47,4 +56,33 @@
),
)
}
+
+ suspend fun RemoteViews.fetchImage(
+ context: Context,
+ data: Any,
+ @IdRes imageViewResId: Int
+ ) {
+ val imageLoader = context.imageLoader
+
+ val imageRequest = ImageRequest.Builder(context)
+ .target(RemoteViewsTarget(this, imageViewResId))
+ .data(data)
+ .maxBitmapSize(Size(512, 512))
+ .allowHardware(false)
+ .build()
+
+ imageLoader.execute(imageRequest)
+ }
+
+ private inner class RemoteViewsTarget(
+ private val remoteViews: RemoteViews,
+ @IdRes private val imageViewResId: Int
+ ) : Target {
+ override fun onStart(placeholder: Image?) = setDrawable(placeholder)
+ override fun onError(error: Image?) = setDrawable(error)
+ override fun onSuccess(result: Image) = setDrawable(result)
+ private fun setDrawable(image: Image?) {
+ remoteViews.setImageViewBitmap(imageViewResId, image?.toBitmap())
+ }
+ }
}
diff --git a/app/src/main/java/org/lineageos/twelve/ui/widgets/NowPlayingAppWidgetProvider.kt b/app/src/main/java/org/lineageos/twelve/ui/widgets/NowPlayingAppWidgetProvider.kt
index 1b71ede..e8081d1 100644
--- a/app/src/main/java/org/lineageos/twelve/ui/widgets/NowPlayingAppWidgetProvider.kt
+++ b/app/src/main/java/org/lineageos/twelve/ui/widgets/NowPlayingAppWidgetProvider.kt
@@ -9,16 +9,10 @@
import android.content.ComponentName
import android.content.Context
import android.content.Intent
-import android.graphics.Bitmap
-import android.graphics.BitmapFactory
-import android.net.Uri
import android.view.View
import android.widget.RemoteViews
import androidx.media3.session.MediaController
import androidx.media3.session.SessionToken
-import coil3.imageLoader
-import coil3.request.ImageRequest
-import coil3.toBitmap
import kotlinx.coroutines.guava.await
import org.lineageos.twelve.MainActivity
import org.lineageos.twelve.R
@@ -81,13 +75,9 @@
}
else -> mediaMetadata.artworkData?.let {
- BitmapFactory.decodeByteArray(it, 0, it.size)?.also { bitmap ->
- setImageViewBitmap(R.id.thumbnailImageView, bitmap)
- }
+ fetchImage(context, it, R.id.thumbnailImageView)
} ?: mediaMetadata.artworkUri?.let {
- downloadBitmap(context, it)?.also { bitmap ->
- setImageViewBitmap(R.id.thumbnailImageView, bitmap)
- }
+ fetchImage(context, it, R.id.thumbnailImageView)
} ?: run {
setImageViewResource(R.id.thumbnailImageView, R.drawable.ic_music_note)
}
@@ -115,17 +105,5 @@
mediaController.release()
}
-
- private suspend fun downloadBitmap(context: Context, uri: Uri): Bitmap? {
- val imageLoader = context.imageLoader
-
- val imageRequest = ImageRequest.Builder(context)
- .data(uri)
- .build()
-
- val result = imageLoader.execute(imageRequest)
-
- return result.image?.toBitmap()
- }
}
}