diff --git a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlViewHolder.kt b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlViewHolder.kt index 43607dd7b75dd..b15bb45b3c321 100644 --- a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlViewHolder.kt +++ b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlViewHolder.kt @@ -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,58 +328,87 @@ class ControlViewHolder( ALPHA_DISABLED ) } + val newBaseColor = if (behavior is ToggleRangeBehavior) { + ColorUtils.blendARGB(bg, newClipColor, toggleBackgroundIntensity) + } else { + bg + } - clipLayer.getDrawable().apply { + clipLayer.drawable?.apply { clipLayer.alpha = ALPHA_DISABLED - - val newBaseColor = if (behavior is ToggleRangeBehavior) { - ColorUtils.blendARGB(bg, newClipColor, toggleBackgroundIntensity) - } else { - bg - } stateAnimator?.cancel() if (animated) { - val oldColor = if (this is GradientDrawable) { - this.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) - } - addListener(object : AnimatorListenerAdapter() { - override fun onAnimationEnd(animation: Animator?) { - stateAnimator = null - } - }) - duration = STATE_ANIMATION_DURATION - interpolator = Interpolators.CONTROL_STATE - start() - } + startBackgroundAnimation(this, newAlpha, newClipColor, newBaseColor) } else { - alpha = newAlpha - if (this is GradientDrawable) { - this.setColor(newClipColor) - } - baseLayer.setColor(newBaseColor) - layout.alpha = 1f + 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 + + stateAnimator = ValueAnimator.ofInt(clipLayer.alpha, newAlpha).apply { + addUpdateListener { + 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?) { + stateAnimator = null + } + }) + duration = STATE_ANIMATION_DURATION + interpolator = Interpolators.CONTROL_STATE + start() + } + } + + /** + * 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) { statusAnimator?.cancel()