From b33dd2a9944aff04033986701aa89b7bece5b31a Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Sun, 23 May 2021 11:21:49 -0500 Subject: [PATCH] Ensure RingView.diameter is never zero In rare cases, it seems either that `onDraw` is called before `onMeasure` or that `onMeasure` is called with a view that has zero height/width. This causes `reallocateCache` to produce an IllegalArgumentException. This patch ensures that diameter is never zero. Fixes #904 --- .../org/isoron/uhabits/activities/common/views/RingView.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/activities/common/views/RingView.kt b/uhabits-android/src/main/java/org/isoron/uhabits/activities/common/views/RingView.kt index 8f552f50b..4a3b10679 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/activities/common/views/RingView.kt +++ b/uhabits-android/src/main/java/org/isoron/uhabits/activities/common/views/RingView.kt @@ -41,6 +41,7 @@ import org.isoron.uhabits.utils.InterfaceUtils.getFontAwesome import org.isoron.uhabits.utils.InterfaceUtils.spToPixels import org.isoron.uhabits.utils.PaletteUtils.getAndroidTestColor import org.isoron.uhabits.utils.StyledResources +import kotlin.math.max import kotlin.math.min import kotlin.math.roundToLong @@ -48,7 +49,7 @@ class RingView : View { private var color: Int private var precision: Float private var percentage: Float - private var diameter = 0 + private var diameter = 1 private var thickness: Float private var rect: RectF? = null private var pRing: TextPaint? = null @@ -169,7 +170,7 @@ class RingView : View { super.onMeasure(widthMeasureSpec, heightMeasureSpec) val width = MeasureSpec.getSize(widthMeasureSpec) val height = MeasureSpec.getSize(heightMeasureSpec) - diameter = min(height, width) + diameter = max(1, min(height, width)) pRing!!.textSize = textSize em = pRing!!.measureText("M") setMeasuredDimension(diameter, diameter)