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

Commit 513b67ae authored by Fabian Kozynski's avatar Fabian Kozynski
Browse files

Animate tiles

Properly animate background color  of the tiles.

Test: manual, light and dark mode
Bug: 171319433
Change-Id: I67e3f31240f665e948a417262290cab03a1f00e6
parent 11623bb1
Loading
Loading
Loading
Loading
+39 −3
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.systemui.qs.tileimpl

import android.animation.ValueAnimator
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
@@ -31,12 +32,17 @@ import com.android.systemui.plugins.qs.QSIconView
import com.android.systemui.plugins.qs.QSTile
import com.android.systemui.qs.tileimpl.QSTileImpl.getColorForState

// Placeholder
private const val CORNER_RADIUS = 40f

class QSTileViewHorizontal(
    context: Context,
    icon: QSIconView
) : QSTileView(context, icon, false) {

    private var paintDrawable: PaintDrawable? = null
    private var paintColor = Color.TRANSPARENT
    private var paintAnimator: ValueAnimator? = null

    init {
        orientation = HORIZONTAL
@@ -70,8 +76,8 @@ class QSTileViewHorizontal(
    override fun newTileBackground(): Drawable? {
        val d = super.newTileBackground()
        if (paintDrawable == null) {
            paintDrawable = PaintDrawable(Color.WHITE).apply {
                setCornerRadius(50f)
            paintDrawable = PaintDrawable(paintColor).apply {
                setCornerRadius(CORNER_RADIUS)
            }
        }
        if (d is RippleDrawable) {
@@ -94,9 +100,39 @@ class QSTileViewHorizontal(

    override fun handleStateChanged(state: QSTile.State) {
        super.handleStateChanged(state)
        paintDrawable?.setTint(getCircleColor(state.state))
        mSecondLine.setTextColor(mLabel.textColors)
        mLabelContainer.background = null

        val allowAnimations = animationsEnabled() && paintColor != Color.TRANSPARENT
        val newColor = getCircleColor(state.state)
        if (allowAnimations) {
            animateToNewState(newColor)
        } else {
            if (newColor != paintColor) {
                clearAnimator()
                paintDrawable?.paint?.color = newColor
                paintDrawable?.invalidateSelf()
            }
        }
        paintColor = newColor
    }

    private fun animateToNewState(newColor: Int) {
        if (newColor != paintColor) {
            clearAnimator()
            paintAnimator = ValueAnimator.ofArgb(paintColor, newColor)
                .setDuration(QSIconViewImpl.QS_ANIM_LENGTH).apply {
                    addUpdateListener { animation: ValueAnimator ->
                        paintDrawable?.paint?.color = animation.animatedValue as Int
                        paintDrawable?.invalidateSelf()
                    }
                    start()
                }
        }
    }

    private fun clearAnimator() {
        paintAnimator?.cancel()?.also { paintAnimator = null }
    }

    override fun handleExpand(dualTarget: Boolean) {}