Merge "[Device Controls] Fix background not showing up." into sc-dev

This commit is contained in:
Caitlin Cassidy
2021-05-17 20:07:26 +00:00
committed by Android (Google) Code Review

View File

@@ -46,6 +46,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import androidx.annotation.ColorInt
import com.android.internal.graphics.ColorUtils import com.android.internal.graphics.ColorUtils
import com.android.systemui.R import com.android.systemui.R
import com.android.systemui.animation.Interpolators import com.android.systemui.animation.Interpolators
@@ -313,7 +314,8 @@ class ControlViewHolder(
@ColorRes bgColor: Int @ColorRes bgColor: Int
) { ) {
val bg = context.resources.getColor(R.color.control_default_background, context.theme) 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 // allow color overrides for the enabled state only
val color = cws.control?.getCustomColor()?.let { val color = cws.control?.getCustomColor()?.let {
val state = intArrayOf(android.R.attr.state_enabled) val state = intArrayOf(android.R.attr.state_enabled)
@@ -326,37 +328,54 @@ class ControlViewHolder(
ALPHA_DISABLED ALPHA_DISABLED
) )
} }
clipLayer.getDrawable().apply {
clipLayer.alpha = ALPHA_DISABLED
val newBaseColor = if (behavior is ToggleRangeBehavior) { val newBaseColor = if (behavior is ToggleRangeBehavior) {
ColorUtils.blendARGB(bg, newClipColor, toggleBackgroundIntensity) ColorUtils.blendARGB(bg, newClipColor, toggleBackgroundIntensity)
} else { } else {
bg bg
} }
clipLayer.drawable?.apply {
clipLayer.alpha = ALPHA_DISABLED
stateAnimator?.cancel() stateAnimator?.cancel()
if (animated) { if (animated) {
val oldColor = if (this is GradientDrawable) { startBackgroundAnimation(this, newAlpha, newClipColor, newBaseColor)
this.color?.defaultColor ?: newClipColor } 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 { } else {
newClipColor newClipColor
} }
val oldBaseColor = baseLayer.color?.defaultColor ?: newBaseColor val oldBaseColor = baseLayer.color?.defaultColor ?: newBaseColor
val oldAlpha = layout.alpha 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 { stateAnimator = ValueAnimator.ofInt(clipLayer.alpha, newAlpha).apply {
addUpdateListener { addUpdateListener {
alpha = it.animatedValue as Int val updatedAlpha = it.animatedValue as Int
if (this is GradientDrawable) { val updatedClipColor = ColorUtils.blendARGB(oldClipColor, newClipColor,
this.setColor(ColorUtils.blendARGB(oldColor, newClipColor, it.animatedFraction)
it.animatedFraction)) val updatedBaseColor = ColorUtils.blendARGB(oldBaseColor, newBaseColor,
} it.animatedFraction)
baseLayer.setColor(ColorUtils.blendARGB(oldBaseColor, newBaseColor, val updatedLayoutAlpha = MathUtils.lerp(oldAlpha, 1f, it.animatedFraction)
it.animatedFraction)) applyBackgroundChange(
layout.alpha = MathUtils.lerp(oldAlpha, 1f, it.animatedFraction) clipDrawable,
updatedAlpha,
updatedClipColor,
updatedBaseColor,
updatedLayoutAlpha
)
} }
addListener(object : AnimatorListenerAdapter() { addListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator?) { override fun onAnimationEnd(animation: Animator?) {
@@ -367,15 +386,27 @@ class ControlViewHolder(
interpolator = Interpolators.CONTROL_STATE interpolator = Interpolators.CONTROL_STATE
start() start()
} }
} else { }
alpha = newAlpha
if (this is GradientDrawable) { /**
this.setColor(newClipColor) * 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) baseLayer.setColor(newBaseColor)
layout.alpha = 1f layout.alpha = newLayoutAlpha
}
}
} }
private fun animateStatusChange(animated: Boolean, statusRowUpdater: () -> Unit) { private fun animateStatusChange(animated: Boolean, statusRowUpdater: () -> Unit) {