summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Alan Viverette <alanv@google.com> 2014-06-16 14:43:27 -0700
committer Alan Viverette <alanv@google.com> 2014-06-16 14:43:27 -0700
commit611c1ff0de00b636ca0f9d5500c91d75b9c5257a (patch)
treeff505864240c2d8b9f3467b58f2317684497132d
parent382512ecaef2a9828bf1e9bf4dd6fea5ff591d1a (diff)
Fix ShapeDrawable constant state and theming
Change-Id: I085d7705b63ec5f94e9b9fb5ece85ae3c7d39512
-rw-r--r--graphics/java/android/graphics/drawable/ShapeDrawable.java86
1 files changed, 62 insertions, 24 deletions
diff --git a/graphics/java/android/graphics/drawable/ShapeDrawable.java b/graphics/java/android/graphics/drawable/ShapeDrawable.java
index a2aef215c2a3..981efb8262a2 100644
--- a/graphics/java/android/graphics/drawable/ShapeDrawable.java
+++ b/graphics/java/android/graphics/drawable/ShapeDrawable.java
@@ -32,6 +32,7 @@ import android.graphics.drawable.shapes.Shape;
import android.content.res.Resources.Theme;
import android.util.AttributeSet;
+import com.android.internal.R;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@@ -74,7 +75,7 @@ public class ShapeDrawable extends Drawable {
* ShapeDrawable constructor.
*/
public ShapeDrawable() {
- this((ShapeState) null);
+ this(new ShapeState(null), null, null);
}
/**
@@ -83,17 +84,11 @@ public class ShapeDrawable extends Drawable {
* @param s the Shape that this ShapeDrawable should be
*/
public ShapeDrawable(Shape s) {
- this((ShapeState) null);
+ this(new ShapeState(null), null, null);
mShapeState.mShape = s;
}
- private ShapeDrawable(ShapeState state) {
- mShapeState = new ShapeState(state);
-
- updateTintFilter(mTintFilter, state.mTint, state.mTintMode);
- }
-
/**
* Returns the Shape of this ShapeDrawable.
*/
@@ -405,20 +400,8 @@ public class ShapeDrawable extends Drawable {
throws XmlPullParserException, IOException {
super.inflate(r, parser, attrs, theme);
- TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ShapeDrawable);
-
- int color = mShapeState.mPaint.getColor();
- color = a.getColor(com.android.internal.R.styleable.ShapeDrawable_color, color);
- mShapeState.mPaint.setColor(color);
-
- boolean dither = a.getBoolean(com.android.internal.R.styleable.ShapeDrawable_dither, false);
- mShapeState.mPaint.setDither(dither);
-
- setIntrinsicWidth((int)
- a.getDimension(com.android.internal.R.styleable.ShapeDrawable_width, 0f));
- setIntrinsicHeight((int)
- a.getDimension(com.android.internal.R.styleable.ShapeDrawable_height, 0f));
-
+ final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.ShapeDrawable);
+ updateStateFromTypedArray(a);
a.recycle();
int type;
@@ -438,6 +421,38 @@ public class ShapeDrawable extends Drawable {
}
}
+ @Override
+ public void applyTheme(Theme t) {
+ super.applyTheme(t);
+
+ final ShapeState state = mShapeState;
+ if (state == null || state.mThemeAttrs == null) {
+ return;
+ }
+
+ final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.ShapeDrawable);
+ updateStateFromTypedArray(a);
+ a.recycle();
+ }
+
+ private void updateStateFromTypedArray(TypedArray a) {
+ final ShapeState state = mShapeState;
+ final Paint paint = state.mPaint;
+
+ int color = paint.getColor();
+ color = a.getColor(R.styleable.ShapeDrawable_color, color);
+ paint.setColor(color);
+
+ boolean dither = paint.isDither();
+ dither = a.getBoolean(R.styleable.ShapeDrawable_dither, dither);
+ paint.setDither(dither);
+
+ setIntrinsicWidth((int) a.getDimension(
+ R.styleable.ShapeDrawable_width, state.mIntrinsicWidth));
+ setIntrinsicHeight((int) a.getDimension(
+ R.styleable.ShapeDrawable_height, state.mIntrinsicHeight));
+ }
+
private void updateShape() {
if (mShapeState.mShape != null) {
final Rect r = getBounds();
@@ -495,6 +510,7 @@ public class ShapeDrawable extends Drawable {
* Defines the intrinsic properties of this ShapeDrawable's Shape.
*/
final static class ShapeState extends ConstantState {
+ int[] mThemeAttrs;
int mChangingConfigurations;
Paint mPaint;
Shape mShape;
@@ -508,6 +524,7 @@ public class ShapeDrawable extends Drawable {
ShapeState(ShapeState orig) {
if (orig != null) {
+ mThemeAttrs = orig.mThemeAttrs;
mPaint = orig.mPaint;
mShape = orig.mShape;
mTint = orig.mTint;
@@ -523,13 +540,23 @@ public class ShapeDrawable extends Drawable {
}
@Override
+ public boolean canApplyTheme() {
+ return mThemeAttrs != null;
+ }
+
+ @Override
public Drawable newDrawable() {
- return new ShapeDrawable(this);
+ return new ShapeDrawable(this, null, null);
}
@Override
public Drawable newDrawable(Resources res) {
- return new ShapeDrawable(this);
+ return new ShapeDrawable(this, res, null);
+ }
+
+ @Override
+ public Drawable newDrawable(Resources res, Theme theme) {
+ return new ShapeDrawable(this, res, theme);
}
@Override
@@ -538,6 +565,17 @@ public class ShapeDrawable extends Drawable {
}
}
+ private ShapeDrawable(ShapeState state, Resources res, Theme theme) {
+ if (theme != null && state.canApplyTheme()) {
+ mShapeState = new ShapeState(state);
+ applyTheme(theme);
+ } else {
+ mShapeState = state;
+ }
+
+ mTintFilter = updateTintFilter(mTintFilter, mShapeState.mTint, mShapeState.mTintMode);
+ }
+
/**
* Base class defines a factory object that is called each time the drawable
* is resized (has a new width or height). Its resize() method returns a