From f601440e21f352f5e59dd27e6709c7137cd90090 Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Wed, 29 Apr 2015 15:36:11 -0700 Subject: Stash modulation alpha until ColorStateList has resolved base color Fixes an issue where specifying an explicit modulation alpha and a theme-dependent base color discarded the modulation alpha during applyTheme(). Sets the YOU MESSED UP YOUR THEME color to magenta, which matches the behavior for RippleDrawable and should expose any remaining issues in a painfully obvious way. Bug: 20690409 Change-Id: I7a44f4bc4a5a85be6d3f27087b2d6c9ea12e1d29 --- core/java/android/content/res/ColorStateList.java | 27 +++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'core/java/android/content/res') diff --git a/core/java/android/content/res/ColorStateList.java b/core/java/android/content/res/ColorStateList.java index fdafb04..14bfac5 100644 --- a/core/java/android/content/res/ColorStateList.java +++ b/core/java/android/content/res/ColorStateList.java @@ -271,7 +271,7 @@ public class ColorStateList implements Parcelable { final TypedArray a = Resources.obtainAttributes(r, theme, attrs, R.styleable.ColorStateListItem); final int[] themeAttrs = a.extractThemeAttrs(); - final int baseColor = a.getColor(R.styleable.ColorStateListItem_color, 0); + final int baseColor = a.getColor(R.styleable.ColorStateListItem_color, Color.MAGENTA); final float alphaMod = a.getFloat(R.styleable.ColorStateListItem_alpha, 1.0f); changingConfigurations |= a.getChangingConfigurations(); @@ -296,7 +296,9 @@ public class ColorStateList implements Parcelable { } stateSpec = StateSet.trimStateSet(stateSpec, j); - // Apply alpha modulation. + // Apply alpha modulation. If we couldn't resolve the color or + // alpha yet, the default values leave us enough information to + // modulate again during applyTheme(). final int color = modulateColorAlpha(baseColor, alphaMod); if (listSize == 0 || stateSpec.length == 0) { defaultColor = color; @@ -365,14 +367,31 @@ public class ColorStateList implements Parcelable { if (themeAttrsList[i] != null) { final TypedArray a = t.resolveAttributes(themeAttrsList[i], R.styleable.ColorStateListItem); + + final float defaultAlphaMod; + if (themeAttrsList[i][R.styleable.ColorStateListItem_color] != 0) { + // If the base color hasn't been resolved yet, the current + // color's alpha channel is either full-opacity (if we + // haven't resolved the alpha modulation yet) or + // pre-modulated. Either is okay as a default value. + defaultAlphaMod = Color.alpha(mColors[i]) / 255.0f; + } else { + // Otherwise, the only correct default value is 1. Even if + // nothing is resolved during this call, we can apply this + // multiple times without losing of information. + defaultAlphaMod = 1.0f; + } + final int baseColor = a.getColor( R.styleable.ColorStateListItem_color, mColors[i]); final float alphaMod = a.getFloat( - R.styleable.ColorStateListItem_alpha, 1.0f); - + R.styleable.ColorStateListItem_alpha, defaultAlphaMod); mColors[i] = modulateColorAlpha(baseColor, alphaMod); + + // Account for any configuration changes. mChangingConfigurations |= a.getChangingConfigurations(); + // Extract the theme attributes, if any. themeAttrsList[i] = a.extractThemeAttrs(themeAttrsList[i]); if (themeAttrsList[i] != null) { hasUnresolvedAttrs = true; -- cgit v1.1