diff options
author | Alan Viverette <alanv@google.com> | 2015-01-06 18:47:25 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-01-06 18:47:29 +0000 |
commit | 304ea5aac77cae03b4e8440a7c73c18086d96a20 (patch) | |
tree | 6411b0eb6ee19f4334988bee2f13e6c3712ed864 /graphics | |
parent | 2b2ad7ce9f008cc383394cc91389d667575ec45d (diff) | |
parent | 45c4bbbbce6bbad50a033efcba7948a23f1f117a (diff) | |
download | frameworks_base-304ea5aac77cae03b4e8440a7c73c18086d96a20.zip frameworks_base-304ea5aac77cae03b4e8440a7c73c18086d96a20.tar.gz frameworks_base-304ea5aac77cae03b4e8440a7c73c18086d96a20.tar.bz2 |
Merge "Allow use of theme attributes in color state lists"
Diffstat (limited to 'graphics')
7 files changed, 192 insertions, 97 deletions
diff --git a/graphics/java/android/graphics/drawable/BitmapDrawable.java b/graphics/java/android/graphics/drawable/BitmapDrawable.java index 9be296a..f2b635a 100644 --- a/graphics/java/android/graphics/drawable/BitmapDrawable.java +++ b/graphics/java/android/graphics/drawable/BitmapDrawable.java @@ -705,8 +705,8 @@ public class BitmapDrawable extends Drawable { @Override public boolean isStateful() { - final BitmapState s = mBitmapState; - return super.isStateful() || (s.mTint != null && s.mTint.isStateful()); + return (mBitmapState.mTint != null && mBitmapState.mTint.isStateful()) + || super.isStateful(); } @Override @@ -718,6 +718,9 @@ public class BitmapDrawable extends Drawable { updateStateFromTypedArray(a); verifyState(a); a.recycle(); + + // Update local properties. + updateLocalState(r); } /** @@ -800,9 +803,6 @@ public class BitmapDrawable extends Drawable { if (tileModeY != TILE_MODE_UNDEFINED) { setTileModeY(parseTileMode(tileModeY)); } - - // Update local properties. - initializeWithState(state, r); } @Override @@ -810,18 +810,28 @@ public class BitmapDrawable extends Drawable { super.applyTheme(t); final BitmapState state = mBitmapState; - if (state == null || state.mThemeAttrs == null) { + if (state == null) { return; } - final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.BitmapDrawable); - try { - updateStateFromTypedArray(a); - } catch (XmlPullParserException e) { - throw new RuntimeException(e); - } finally { - a.recycle(); + if (state.mThemeAttrs != null) { + final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.BitmapDrawable); + try { + updateStateFromTypedArray(a); + } catch (XmlPullParserException e) { + throw new RuntimeException(e); + } finally { + a.recycle(); + } } + + // Apply theme to contained color state list. + if (state.mTint != null && state.mTint.canApplyTheme()) { + state.mTint.applyTheme(t); + } + + // Update local properties. + updateLocalState(t.getResources()); } private static Shader.TileMode parseTileMode(int tileMode) { @@ -839,7 +849,7 @@ public class BitmapDrawable extends Drawable { @Override public boolean canApplyTheme() { - return mBitmapState != null && mBitmapState.mThemeAttrs != null; + return mBitmapState != null && mBitmapState.canApplyTheme(); } @Override @@ -910,7 +920,7 @@ public class BitmapDrawable extends Drawable { @Override public boolean canApplyTheme() { - return mThemeAttrs != null; + return mThemeAttrs != null || mTint != null && mTint.canApplyTheme(); } @Override @@ -944,7 +954,7 @@ public class BitmapDrawable extends Drawable { private BitmapDrawable(BitmapState state, Resources res) { mBitmapState = state; - initializeWithState(mBitmapState, res); + updateLocalState(res); } /** @@ -952,14 +962,14 @@ public class BitmapDrawable extends Drawable { * after significant state changes, e.g. from the One True Constructor and * after inflating or applying a theme. */ - private void initializeWithState(BitmapState state, Resources res) { + private void updateLocalState(Resources res) { if (res != null) { mTargetDensity = res.getDisplayMetrics().densityDpi; } else { - mTargetDensity = state.mTargetDensity; + mTargetDensity = mBitmapState.mTargetDensity; } - mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode); + mTintFilter = updateTintFilter(mTintFilter, mBitmapState.mTint, mBitmapState.mTintMode); computeBitmapSize(); } } diff --git a/graphics/java/android/graphics/drawable/ColorDrawable.java b/graphics/java/android/graphics/drawable/ColorDrawable.java index e3b50ea..85e02b7 100644 --- a/graphics/java/android/graphics/drawable/ColorDrawable.java +++ b/graphics/java/android/graphics/drawable/ColorDrawable.java @@ -70,7 +70,7 @@ public class ColorDrawable extends Drawable { @Override public int getChangingConfigurations() { - return super.getChangingConfigurations() | mColorState.mChangingConfigurations; + return super.getChangingConfigurations() | mColorState.getChangingConfigurations(); } /** @@ -233,6 +233,8 @@ public class ColorDrawable extends Drawable { final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.ColorDrawable); updateStateFromTypedArray(a); a.recycle(); + + updateLocalState(r); } /** @@ -261,13 +263,21 @@ public class ColorDrawable extends Drawable { super.applyTheme(t); final ColorState state = mColorState; - if (state == null || state.mThemeAttrs == null) { + if (state == null) { return; } - final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.ColorDrawable); - updateStateFromTypedArray(a); - a.recycle(); + if (state.mThemeAttrs != null) { + final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.ColorDrawable); + updateStateFromTypedArray(a); + a.recycle(); + } + + if (state.mTint != null && state.mTint.canApplyTheme()) { + state.mTint.applyTheme(t); + } + + updateLocalState(t.getResources()); } @Override @@ -299,17 +309,18 @@ public class ColorDrawable extends Drawable { @Override public boolean canApplyTheme() { - return mThemeAttrs != null; + return mThemeAttrs != null + || (mTint != null && mTint.canApplyTheme()); } @Override public Drawable newDrawable() { - return new ColorDrawable(this); + return new ColorDrawable(this, null); } @Override public Drawable newDrawable(Resources res) { - return new ColorDrawable(this); + return new ColorDrawable(this, res); } @Override @@ -318,8 +329,18 @@ public class ColorDrawable extends Drawable { } } - private ColorDrawable(ColorState state) { + private ColorDrawable(ColorState state, Resources res) { mColorState = state; - mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode); + + updateLocalState(res); + } + + /** + * Initializes local dynamic properties from state. This should be called + * after significant state changes, e.g. from the One True Constructor and + * after inflating or applying a theme. + */ + private void updateLocalState(Resources r) { + mTintFilter = updateTintFilter(mTintFilter, mColorState.mTint, mColorState.mTintMode); } } diff --git a/graphics/java/android/graphics/drawable/GradientDrawable.java b/graphics/java/android/graphics/drawable/GradientDrawable.java index 72707e6..7882705 100644 --- a/graphics/java/android/graphics/drawable/GradientDrawable.java +++ b/graphics/java/android/graphics/drawable/GradientDrawable.java @@ -143,7 +143,7 @@ public class GradientDrawable extends Drawable { private final RectF mRect = new RectF(); private Paint mLayerPaint; // internal, used if we use saveLayer() - private boolean mGradientIsDirty; // internal state + private boolean mGradientIsDirty; private boolean mMutated; private Path mRingPath; private boolean mPathIsDirty = true; @@ -174,7 +174,7 @@ public class GradientDrawable extends Drawable { } public GradientDrawable() { - this(new GradientState(Orientation.TOP_BOTTOM, null)); + this(new GradientState(Orientation.TOP_BOTTOM, null), null); } /** @@ -182,7 +182,7 @@ public class GradientDrawable extends Drawable { * of colors for the gradient. */ public GradientDrawable(Orientation orientation, int[] colors) { - this(new GradientState(orientation, colors)); + this(new GradientState(orientation, colors), null); } @Override @@ -1018,7 +1018,7 @@ public class GradientDrawable extends Drawable { inflateChildElements(r, parser, attrs, theme); - mGradientState.computeOpacity(); + updateLocalState(r); } @Override @@ -1037,9 +1037,21 @@ public class GradientDrawable extends Drawable { a.recycle(); } + if (state.mTint != null && state.mTint.canApplyTheme()) { + state.mTint.applyTheme(t); + } + + if (state.mColorStateList != null && state.mColorStateList.canApplyTheme()) { + state.mColorStateList.applyTheme(t); + } + + if (state.mStrokeColorStateList != null && state.mStrokeColorStateList.canApplyTheme()) { + state.mStrokeColorStateList.applyTheme(t); + } + applyThemeChildElements(t); - state.computeOpacity(); + updateLocalState(t.getResources()); } /** @@ -1087,8 +1099,6 @@ public class GradientDrawable extends Drawable { if (tint != null) { state.mTint = tint; } - - mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode); } @Override @@ -1516,7 +1526,7 @@ public class GradientDrawable extends Drawable { public Drawable mutate() { if (!mMutated && super.mutate() == this) { mGradientState = new GradientState(mGradientState); - initializeWithState(mGradientState); + updateLocalState(null); mMutated = true; } return this; @@ -1577,7 +1587,7 @@ public class GradientDrawable extends Drawable { int[] mAttrCorners; int[] mAttrPadding; - GradientState(Orientation orientation, int[] colors) { + public GradientState(Orientation orientation, int[] colors) { mOrientation = orientation; setColors(colors); } @@ -1634,19 +1644,24 @@ public class GradientDrawable extends Drawable { @Override public boolean canApplyTheme() { - return mThemeAttrs != null || mAttrSize != null || mAttrGradient != null - || mAttrSolid != null || mAttrStroke != null || mAttrCorners != null - || mAttrPadding != null || super.canApplyTheme(); + return mThemeAttrs != null + || mAttrSize != null || mAttrGradient != null + || mAttrSolid != null || mAttrStroke != null + || mAttrCorners != null || mAttrPadding != null + || (mTint != null && mTint.canApplyTheme()) + || (mStrokeColorStateList != null && mStrokeColorStateList.canApplyTheme()) + || (mColorStateList != null && mColorStateList.canApplyTheme()) + || super.canApplyTheme(); } @Override public Drawable newDrawable() { - return new GradientDrawable(this); + return new GradientDrawable(this, null); } @Override public Drawable newDrawable(Resources res) { - return new GradientDrawable(this); + return new GradientDrawable(this, res); } @Override @@ -1751,16 +1766,15 @@ public class GradientDrawable extends Drawable { * * @param state Constant state from which the drawable inherits */ - private GradientDrawable(GradientState state) { + private GradientDrawable(GradientState state, Resources res) { mGradientState = state; - initializeWithState(mGradientState); - - mGradientIsDirty = true; - mMutated = false; + updateLocalState(res); } - private void initializeWithState(GradientState state) { + private void updateLocalState(Resources res) { + final GradientState state = mGradientState; + if (state.mColorStateList != null) { final int[] currentState = getState(); final int stateColor = state.mColorStateList.getColorForState(currentState, 0); @@ -1797,5 +1811,8 @@ public class GradientDrawable extends Drawable { } mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode); + mGradientIsDirty = true; + + state.computeOpacity(); } } diff --git a/graphics/java/android/graphics/drawable/NinePatchDrawable.java b/graphics/java/android/graphics/drawable/NinePatchDrawable.java index b87ae92..a6299be 100644 --- a/graphics/java/android/graphics/drawable/NinePatchDrawable.java +++ b/graphics/java/android/graphics/drawable/NinePatchDrawable.java @@ -401,6 +401,8 @@ public class NinePatchDrawable extends Drawable { final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.NinePatchDrawable); updateStateFromTypedArray(a); a.recycle(); + + updateLocalState(r); } /** @@ -467,12 +469,6 @@ public class NinePatchDrawable extends Drawable { if (tint != null) { state.mTint = tint; } - - // Update local properties. - initializeWithState(state, r); - - // Push density applied by setNinePatchState into state. - state.mTargetDensity = mTargetDensity; } @Override @@ -480,23 +476,32 @@ public class NinePatchDrawable extends Drawable { super.applyTheme(t); final NinePatchState state = mNinePatchState; - if (state == null || state.mThemeAttrs == null) { + if (state == null) { return; } - final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.NinePatchDrawable); - try { - updateStateFromTypedArray(a); - } catch (XmlPullParserException e) { - throw new RuntimeException(e); - } finally { - a.recycle(); + if (state.mThemeAttrs != null) { + final TypedArray a = t.resolveAttributes( + state.mThemeAttrs, R.styleable.NinePatchDrawable); + try { + updateStateFromTypedArray(a); + } catch (XmlPullParserException e) { + throw new RuntimeException(e); + } finally { + a.recycle(); + } + } + + if (state.mTint != null && state.mTint.canApplyTheme()) { + state.mTint.applyTheme(t); } + + updateLocalState(t.getResources()); } @Override public boolean canApplyTheme() { - return mNinePatchState != null && mNinePatchState.mThemeAttrs != null; + return mNinePatchState != null && mNinePatchState.canApplyTheme(); } public Paint getPaint() { @@ -645,7 +650,8 @@ public class NinePatchDrawable extends Drawable { @Override public boolean canApplyTheme() { - return mThemeAttrs != null; + return mThemeAttrs != null + || (mTint != null && mTint.canApplyTheme()); } @Override @@ -680,19 +686,25 @@ public class NinePatchDrawable extends Drawable { private NinePatchDrawable(NinePatchState state, Resources res) { mNinePatchState = state; - initializeWithState(mNinePatchState, res); + updateLocalState(res); + + // Push density applied by setNinePatchState into state. + mNinePatchState.mTargetDensity = mTargetDensity; } /** * Initializes local dynamic properties from state. */ - private void initializeWithState(NinePatchState state, Resources res) { + private void updateLocalState(Resources res) { + final NinePatchState state = mNinePatchState; + if (res != null) { mTargetDensity = res.getDisplayMetrics().densityDpi; } else { mTargetDensity = state.mTargetDensity; } + // If we can, avoid calling any methods that initialize Paint. if (state.mDither != DEFAULT_DITHER) { setDither(state.mDither); diff --git a/graphics/java/android/graphics/drawable/RippleDrawable.java b/graphics/java/android/graphics/drawable/RippleDrawable.java index 9809606..c7b506e 100644 --- a/graphics/java/android/graphics/drawable/RippleDrawable.java +++ b/graphics/java/android/graphics/drawable/RippleDrawable.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 The Android Open Source Project + * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -198,7 +198,7 @@ public class RippleDrawable extends LayerDrawable { setColor(color); ensurePadding(); - initializeFromState(); + updateLocalState(); } @Override @@ -352,6 +352,11 @@ public class RippleDrawable extends LayerDrawable { return true; } + /** + * Sets the ripple color. + * + * @param color Ripple color as a color state list. + */ public void setColor(ColorStateList color) { mState.mColor = color; invalidateSelf(); @@ -370,7 +375,8 @@ public class RippleDrawable extends LayerDrawable { super.inflate(r, parser, attrs, theme); setTargetDensity(r.getDisplayMetrics()); - initializeFromState(); + + updateLocalState(); } @Override @@ -450,21 +456,27 @@ public class RippleDrawable extends LayerDrawable { super.applyTheme(t); final RippleState state = mState; - if (state == null || state.mTouchThemeAttrs == null) { + if (state == null) { return; } - final TypedArray a = t.resolveAttributes(state.mTouchThemeAttrs, - R.styleable.RippleDrawable); - try { - updateStateFromTypedArray(a); - } catch (XmlPullParserException e) { - throw new RuntimeException(e); - } finally { - a.recycle(); + if (state.mTouchThemeAttrs != null) { + final TypedArray a = t.resolveAttributes(state.mTouchThemeAttrs, + R.styleable.RippleDrawable); + try { + updateStateFromTypedArray(a); + } catch (XmlPullParserException e) { + throw new RuntimeException(e); + } finally { + a.recycle(); + } + } + + if (state.mColor != null && state.mColor.canApplyTheme()) { + state.mColor.applyTheme(t); } - initializeFromState(); + updateLocalState(); } @Override @@ -931,7 +943,9 @@ public class RippleDrawable extends LayerDrawable { @Override public boolean canApplyTheme() { - return mTouchThemeAttrs != null || super.canApplyTheme(); + return mTouchThemeAttrs != null + || (mColor != null && mColor.canApplyTheme()) + || super.canApplyTheme(); } @Override @@ -987,10 +1001,10 @@ public class RippleDrawable extends LayerDrawable { mDensity = res.getDisplayMetrics().density; } - initializeFromState(); + updateLocalState(); } - private void initializeFromState() { + private void updateLocalState() { // Initialize from constant state. mMask = findDrawableByLayerId(R.id.mask); } diff --git a/graphics/java/android/graphics/drawable/ShapeDrawable.java b/graphics/java/android/graphics/drawable/ShapeDrawable.java index a3d8c92..c49bc8c 100644 --- a/graphics/java/android/graphics/drawable/ShapeDrawable.java +++ b/graphics/java/android/graphics/drawable/ShapeDrawable.java @@ -402,7 +402,7 @@ public class ShapeDrawable extends Drawable { } // Update local properties. - initializeWithState(mShapeState, r); + updateLocalState(r); } @Override @@ -410,16 +410,23 @@ public class ShapeDrawable extends Drawable { super.applyTheme(t); final ShapeState state = mShapeState; - if (state == null || state.mThemeAttrs == null) { + if (state == null) { return; } - final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.ShapeDrawable); - updateStateFromTypedArray(a); - a.recycle(); + if (state.mThemeAttrs != null) { + final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.ShapeDrawable); + updateStateFromTypedArray(a); + a.recycle(); + } + + // Apply theme to contained color state list. + if (state.mTint != null && state.mTint.canApplyTheme()) { + state.mTint.applyTheme(t); + } // Update local properties. - initializeWithState(state, t.getResources()); + updateLocalState(t.getResources()); } private void updateStateFromTypedArray(TypedArray a) { @@ -550,7 +557,8 @@ public class ShapeDrawable extends Drawable { @Override public boolean canApplyTheme() { - return mThemeAttrs != null; + return mThemeAttrs != null + || (mTint != null && mTint.canApplyTheme()); } @Override @@ -576,7 +584,7 @@ public class ShapeDrawable extends Drawable { private ShapeDrawable(ShapeState state, Resources res) { mShapeState = state; - initializeWithState(state, res); + updateLocalState(res); } /** @@ -584,8 +592,8 @@ public class ShapeDrawable extends Drawable { * after significant state changes, e.g. from the One True Constructor and * after inflating or applying a theme. */ - private void initializeWithState(ShapeState state, Resources res) { - mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode); + private void updateLocalState(Resources res) { + mTintFilter = updateTintFilter(mTintFilter, mShapeState.mTint, mShapeState.mTintMode); } /** diff --git a/graphics/java/android/graphics/drawable/VectorDrawable.java b/graphics/java/android/graphics/drawable/VectorDrawable.java index 8b0f635..21dba43 100644 --- a/graphics/java/android/graphics/drawable/VectorDrawable.java +++ b/graphics/java/android/graphics/drawable/VectorDrawable.java @@ -369,8 +369,13 @@ public class VectorDrawable extends Drawable { super.applyTheme(t); final VectorDrawableState state = mVectorState; - if (state != null && state.mThemeAttrs != null) { - final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.VectorDrawable); + if (state == null) { + return; + } + + if (state.mThemeAttrs != null) { + final TypedArray a = t.resolveAttributes( + state.mThemeAttrs, R.styleable.VectorDrawable); try { state.mCacheDirty = true; updateStateFromTypedArray(a); @@ -379,14 +384,20 @@ public class VectorDrawable extends Drawable { } finally { a.recycle(); } + } - mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode); + // Apply theme to contained color state list. + if (state.mTint != null && state.mTint.canApplyTheme()) { + state.mTint.applyTheme(t); } final VPathRenderer path = state.mVPathRenderer; if (path != null && path.canApplyTheme()) { path.applyTheme(t); } + + // Update local state. + mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode); } /** @@ -750,7 +761,9 @@ public class VectorDrawable extends Drawable { @Override public boolean canApplyTheme() { - return mThemeAttrs != null || (mVPathRenderer != null && mVPathRenderer.canApplyTheme()) + return mThemeAttrs != null + || (mVPathRenderer != null && mVPathRenderer.canApplyTheme()) + || (mTint != null && mTint.canApplyTheme()) || super.canApplyTheme(); } |