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

Commit 1f0baa63 authored by Caitlin Cassidy's avatar Caitlin Cassidy
Browse files

[Device Controls] Fix background not showing up.

Root cause was that `this` on lines 353 and 354 was referring to the
ValueAnimator, not the clipLayer.getDrawable().apply{} that was started
on line 330. I refactored the code a bit so it uses fewer Kotlin idioms
but should be less error-prone, but let me know if you'd rather I just
update lines 353 and 354 to specify which `this` it's referring to.

Test: manual
Fixes: 187903455

Change-Id: I3505c1fc80f7dda6d6cc318b0dfa2754171b8a2b
parent 54333007
Loading
Loading
Loading
Loading
+75 −44
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorInt
import com.android.internal.graphics.ColorUtils
import com.android.systemui.R
import com.android.systemui.animation.Interpolators
@@ -313,7 +314,8 @@ class ControlViewHolder(
        @ColorRes bgColor: Int
    ) {
        val bg = context.resources.getColor(R.color.control_default_background, context.theme)
        var (newClipColor, newAlpha) = if (enabled) {

        val (newClipColor, newAlpha) = if (enabled) {
            // allow color overrides for the enabled state only
            val color = cws.control?.getCustomColor()?.let {
                val state = intArrayOf(android.R.attr.state_enabled)
@@ -326,37 +328,54 @@ class ControlViewHolder(
                ALPHA_DISABLED
            )
        }

        clipLayer.getDrawable().apply {
            clipLayer.alpha = ALPHA_DISABLED

        val newBaseColor = if (behavior is ToggleRangeBehavior) {
            ColorUtils.blendARGB(bg, newClipColor, toggleBackgroundIntensity)
        } else {
            bg
        }

        clipLayer.drawable?.apply {
            clipLayer.alpha = ALPHA_DISABLED
            stateAnimator?.cancel()
            if (animated) {
                val oldColor = if (this is GradientDrawable) {
                    this.color?.defaultColor ?: newClipColor
                startBackgroundAnimation(this, newAlpha, newClipColor, newBaseColor)
            } else {
                applyBackgroundChange(
                        this, newAlpha, newClipColor, newBaseColor, newLayoutAlpha = 1f
                )
            }
        }
    }

    private fun startBackgroundAnimation(
        clipDrawable: Drawable,
        newAlpha: Int,
        @ColorInt newClipColor: Int,
        @ColorInt newBaseColor: Int
    ) {
        val oldClipColor = if (clipDrawable is GradientDrawable) {
            clipDrawable.color?.defaultColor ?: newClipColor
        } else {
            newClipColor
        }
        val oldBaseColor = baseLayer.color?.defaultColor ?: newBaseColor
        val oldAlpha = layout.alpha

                // Animate both alpha and background colors. Only animate colors for
                // GradientDrawables and not static images as used for the ThumbnailTemplate.
        stateAnimator = ValueAnimator.ofInt(clipLayer.alpha, newAlpha).apply {
            addUpdateListener {
                        alpha = it.animatedValue as Int
                        if (this is GradientDrawable) {
                            this.setColor(ColorUtils.blendARGB(oldColor, newClipColor,
                                it.animatedFraction))
                        }
                        baseLayer.setColor(ColorUtils.blendARGB(oldBaseColor, newBaseColor,
                                it.animatedFraction))
                        layout.alpha = MathUtils.lerp(oldAlpha, 1f, it.animatedFraction)
                val updatedAlpha = it.animatedValue as Int
                val updatedClipColor = ColorUtils.blendARGB(oldClipColor, newClipColor,
                        it.animatedFraction)
                val updatedBaseColor = ColorUtils.blendARGB(oldBaseColor, newBaseColor,
                        it.animatedFraction)
                val updatedLayoutAlpha = MathUtils.lerp(oldAlpha, 1f, it.animatedFraction)
                applyBackgroundChange(
                        clipDrawable,
                        updatedAlpha,
                        updatedClipColor,
                        updatedBaseColor,
                        updatedLayoutAlpha
                )
            }
            addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator?) {
@@ -367,15 +386,27 @@ class ControlViewHolder(
            interpolator = Interpolators.CONTROL_STATE
            start()
        }
            } else {
                alpha = newAlpha
                if (this is GradientDrawable) {
                    this.setColor(newClipColor)
                }
                baseLayer.setColor(newBaseColor)
                layout.alpha = 1f
    }

    /**
     * Applies a change in background.
     *
     * Updates both alpha and background colors. Only updates colors for GradientDrawables and not
     * static images as used for the ThumbnailTemplate.
     */
    private fun applyBackgroundChange(
        clipDrawable: Drawable,
        newAlpha: Int,
        @ColorInt newClipColor: Int,
        @ColorInt newBaseColor: Int,
        newLayoutAlpha: Float
    ) {
        clipDrawable.alpha = newAlpha
        if (clipDrawable is GradientDrawable) {
            clipDrawable.setColor(newClipColor)
        }
        baseLayer.setColor(newBaseColor)
        layout.alpha = newLayoutAlpha
    }

    private fun animateStatusChange(animated: Boolean, statusRowUpdater: () -> Unit) {