widgets: fix insanity

Fix two issues where Auxio's widget could not be resized to a single
cell, and another where covers would not load into the widget.

The first is caused by a random, subtle change that completely changed
up the minHeight size calculation regarding widgets. Thus, we need to
lower it for android to recognize it still as 1 cell. I cannot believe
we can't just specify the specific minimum grid size that our widget
takes up, like you know, iOS did, nearly a decade after widgets were
first added in Android.

The second is an absurd race condition stemming from me hitting the
RemoteViews memory limit. Turns out my cover bitmaps were simply too
big. Getting them below the limit requires me to resize them to a puny
~500 pixels. Why can't we just render our own views into our widget?
You know, like iOS did, nearly a decade after widgets were first added
to Android.

Nah, screw modernizing the broken widget API. Let's just vaguely copy
iOS widgets because we have to while not fixing a single issue plaguing
widget development on this OS. That way some google engineer can get
promoted faster.
This commit is contained in:
OxygenCobalt
2022-07-30 20:41:24 -06:00
parent c3d8509069
commit 170cdf80ef
5 changed files with 19 additions and 20 deletions
@@ -22,6 +22,7 @@ import android.graphics.Bitmap
import android.os.Build
import coil.request.ImageRequest
import coil.transform.RoundedCornersTransformation
import kotlin.math.sqrt
import org.oxycblt.auxio.R
import org.oxycblt.auxio.image.BitmapProvider
import org.oxycblt.auxio.image.SquareFrameTransform
@@ -93,23 +94,27 @@ class WidgetComponent(private val context: Context) :
0
}
// Resize the image in a such a way that we don't hit the RemoteView size
// limit. The limit is technically the byte-size of an RGB_8888 bitmap 1.5x
// the screen size, but the size of a RemoteView can for some reason be 10x
// the size of the binded bitmaps, which means we need to heavily reduce
// our image size as to make sure we stay around an order of magnitude below
// the memory limit. This fixed size is also needed to ensure consistent
// outlines on rounded images.
val metrics = context.resources.displayMetrics
val sw = metrics.widthPixels
val sh = metrics.heightPixels
builder.size((sqrt((6f * sw * sh)) / 8f).toInt())
return if (cornerRadius > 0) {
this@WidgetComponent.logD("Loading round covers: $cornerRadius")
val metrics = context.resources.displayMetrics
// Use RoundedCornersTransformation. This is because our hack to get a 1:1
// aspect ratio on widget ImageViews doesn't actually result in a square
// ImageView, so clipToOutline won't work.
builder
.transformations(
SquareFrameTransform.INSTANCE,
RoundedCornersTransformation(cornerRadius.toFloat()))
// The output of RoundedCornersTransformation is dimension-dependent,
// so scale up the image to the screen size to ensure consistent radii.
// Make sure we stop at 1024, so we don't accidentally make a massive
// bitmap on very large screens.
.size(minOf(metrics.widthPixels, metrics.heightPixels, 1024))
builder.transformations(
SquareFrameTransform.INSTANCE,
RoundedCornersTransformation(cornerRadius.toFloat()))
} else {
builder
}