Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b24ff023 authored by Hawkwood Glazier's avatar Hawkwood Glazier Committed by Android (Google) Code Review
Browse files

Merge "Update TextAnimator to retry with updated layout when failed" into main

parents 50d3b7f4 b0da3984
Loading
Loading
Loading
Loading
+75 −50
Original line number Original line Diff line number Diff line
@@ -27,6 +27,7 @@ import android.graphics.fonts.FontVariationAxis
import android.text.Layout
import android.text.Layout
import android.util.LruCache
import android.util.LruCache
import kotlin.math.roundToInt
import kotlin.math.roundToInt
import android.util.Log


private const val DEFAULT_ANIMATION_DURATION: Long = 300
private const val DEFAULT_ANIMATION_DURATION: Long = 300
private const val TYPEFACE_CACHE_MAX_ENTRIES = 5
private const val TYPEFACE_CACHE_MAX_ENTRIES = 5
@@ -140,7 +141,6 @@ class TextAnimator(
    }
    }


    sealed class PositionedGlyph {
    sealed class PositionedGlyph {

        /** Mutable X coordinate of the glyph position relative from drawing offset. */
        /** Mutable X coordinate of the glyph position relative from drawing offset. */
        var x: Float = 0f
        var x: Float = 0f


@@ -269,8 +269,23 @@ class TextAnimator(
        duration: Long = -1L,
        duration: Long = -1L,
        interpolator: TimeInterpolator? = null,
        interpolator: TimeInterpolator? = null,
        delay: Long = 0,
        delay: Long = 0,
        onAnimationEnd: Runnable? = null
        onAnimationEnd: Runnable? = null,
    ) = setTextStyleInternal(fvar, textSize, color, strokeWidth, animate, duration,
        interpolator, delay, onAnimationEnd, updateLayoutOnFailure = true)

    private fun setTextStyleInternal(
        fvar: String?,
        textSize: Float,
        color: Int?,
        strokeWidth: Float,
        animate: Boolean,
        duration: Long,
        interpolator: TimeInterpolator?,
        delay: Long,
        onAnimationEnd: Runnable?,
        updateLayoutOnFailure: Boolean,
    ) {
    ) {
        try {
            if (animate) {
            if (animate) {
                animator.cancel()
                animator.cancel()
                textInterpolator.rebase()
                textInterpolator.rebase()
@@ -279,11 +294,9 @@ class TextAnimator(
            if (textSize >= 0) {
            if (textSize >= 0) {
                textInterpolator.targetPaint.textSize = textSize
                textInterpolator.targetPaint.textSize = textSize
            }
            }

            if (!fvar.isNullOrBlank()) {
            if (!fvar.isNullOrBlank()) {
                textInterpolator.targetPaint.typeface = typefaceCache.getTypefaceForVariant(fvar)
                textInterpolator.targetPaint.typeface = typefaceCache.getTypefaceForVariant(fvar)
            }
            }

            if (color != null) {
            if (color != null) {
                textInterpolator.targetPaint.color = color
                textInterpolator.targetPaint.color = color
            }
            }
@@ -302,8 +315,7 @@ class TextAnimator(
                    }
                    }
                interpolator?.let { animator.interpolator = it }
                interpolator?.let { animator.interpolator = it }
                if (onAnimationEnd != null) {
                if (onAnimationEnd != null) {
                val listener =
                    val listener = object : AnimatorListenerAdapter() {
                    object : AnimatorListenerAdapter() {
                        override fun onAnimationEnd(animation: Animator) {
                        override fun onAnimationEnd(animation: Animator) {
                            onAnimationEnd.run()
                            onAnimationEnd.run()
                            animator.removeListener(this)
                            animator.removeListener(this)
@@ -321,6 +333,17 @@ class TextAnimator(
                textInterpolator.rebase()
                textInterpolator.rebase()
                invalidateCallback()
                invalidateCallback()
            }
            }
        } catch (ex: IllegalArgumentException) {
            if (updateLayoutOnFailure) {
                Log.e(TAG, "setTextStyleInternal: Exception caught but retrying. This is usually" +
                    " due to the layout having changed unexpectedly without being notified.", ex)
                updateLayout(textInterpolator.layout)
                setTextStyleInternal(fvar, textSize, color, strokeWidth, animate, duration,
                    interpolator, delay, onAnimationEnd, updateLayoutOnFailure = false)
            } else {
                throw ex
            }
        }
    }
    }


    /**
    /**
@@ -355,15 +378,13 @@ class TextAnimator(
        interpolator: TimeInterpolator? = null,
        interpolator: TimeInterpolator? = null,
        delay: Long = 0,
        delay: Long = 0,
        onAnimationEnd: Runnable? = null
        onAnimationEnd: Runnable? = null
    ) {
    ) = setTextStyleInternal(
        val fvar = fontVariationUtils.updateFontVariation(
            fvar = fontVariationUtils.updateFontVariation(
                weight = weight,
                weight = weight,
                width = width,
                width = width,
                opticalSize = opticalSize,
                opticalSize = opticalSize,
                roundness = roundness,
                roundness = roundness,
        )
            ),
        setTextStyle(
            fvar = fvar,
            textSize = textSize,
            textSize = textSize,
            color = color,
            color = color,
            strokeWidth = strokeWidth,
            strokeWidth = strokeWidth,
@@ -372,6 +393,10 @@ class TextAnimator(
            interpolator = interpolator,
            interpolator = interpolator,
            delay = delay,
            delay = delay,
            onAnimationEnd = onAnimationEnd,
            onAnimationEnd = onAnimationEnd,
            updateLayoutOnFailure = true,
        )
        )

    companion object {
        private val TAG = TextAnimator::class.simpleName!!
    }
    }
}
}