1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics.drawable;
18
19import android.graphics.*;
20import android.content.res.Resources;
21import android.content.res.TypedArray;
22import android.util.AttributeSet;
23import android.view.ViewDebug;
24import org.xmlpull.v1.XmlPullParser;
25import org.xmlpull.v1.XmlPullParserException;
26
27import java.io.IOException;
28
29/**
30 * A specialized Drawable that fills the Canvas with a specified color.
31 * Note that a ColorDrawable ignores the ColorFilter.
32 *
33 * <p>It can be defined in an XML file with the <code>&lt;color></code> element.</p>
34 *
35 * @attr ref android.R.styleable#ColorDrawable_color
36 */
37public class ColorDrawable extends Drawable {
38    @ViewDebug.ExportedProperty(deepExport = true, prefix = "state_")
39    private ColorState mState;
40    private final Paint mPaint = new Paint();
41    private boolean mMutated;
42
43    /**
44     * Creates a new black ColorDrawable.
45     */
46    public ColorDrawable() {
47        this(null);
48    }
49
50    /**
51     * Creates a new ColorDrawable with the specified color.
52     *
53     * @param color The color to draw.
54     */
55    public ColorDrawable(int color) {
56        this(null);
57        setColor(color);
58    }
59
60    private ColorDrawable(ColorState state) {
61        mState = new ColorState(state);
62    }
63
64    @Override
65    public int getChangingConfigurations() {
66        return super.getChangingConfigurations() | mState.mChangingConfigurations;
67    }
68
69    /**
70     * A mutable BitmapDrawable still shares its Bitmap with any other Drawable
71     * that comes from the same resource.
72     *
73     * @return This drawable.
74     */
75    @Override
76    public Drawable mutate() {
77        if (!mMutated && super.mutate() == this) {
78            mState = new ColorState(mState);
79            mMutated = true;
80        }
81        return this;
82    }
83
84    @Override
85    public void draw(Canvas canvas) {
86        if ((mState.mUseColor >>> 24) != 0) {
87            mPaint.setColor(mState.mUseColor);
88            canvas.drawRect(getBounds(), mPaint);
89        }
90    }
91
92    /**
93     * Gets the drawable's color value.
94     *
95     * @return int The color to draw.
96     */
97    public int getColor() {
98        return mState.mUseColor;
99    }
100
101    /**
102     * Sets the drawable's color value. This action will clobber the results of prior calls to
103     * {@link #setAlpha(int)} on this object, which side-affected the underlying color.
104     *
105     * @param color The color to draw.
106     */
107    public void setColor(int color) {
108        if (mState.mBaseColor != color || mState.mUseColor != color) {
109            invalidateSelf();
110            mState.mBaseColor = mState.mUseColor = color;
111        }
112    }
113
114    /**
115     * Returns the alpha value of this drawable's color.
116     *
117     * @return A value between 0 and 255.
118     */
119    public int getAlpha() {
120        return mState.mUseColor >>> 24;
121    }
122
123    /**
124     * Sets the color's alpha value.
125     *
126     * @param alpha The alpha value to set, between 0 and 255.
127     */
128    public void setAlpha(int alpha) {
129        alpha += alpha >> 7;   // make it 0..256
130        int baseAlpha = mState.mBaseColor >>> 24;
131        int useAlpha = baseAlpha * alpha >> 8;
132        int oldUseColor = mState.mUseColor;
133        mState.mUseColor = (mState.mBaseColor << 8 >>> 8) | (useAlpha << 24);
134        if (oldUseColor != mState.mUseColor) {
135            invalidateSelf();
136        }
137    }
138
139    /**
140     * Setting a color filter on a ColorDrawable has no effect.
141     *
142     * @param colorFilter Ignore.
143     */
144    public void setColorFilter(ColorFilter colorFilter) {
145    }
146
147    public int getOpacity() {
148        switch (mState.mUseColor >>> 24) {
149            case 255:
150                return PixelFormat.OPAQUE;
151            case 0:
152                return PixelFormat.TRANSPARENT;
153        }
154        return PixelFormat.TRANSLUCENT;
155    }
156
157    @Override
158    public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
159            throws XmlPullParserException, IOException {
160        super.inflate(r, parser, attrs);
161
162        TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.ColorDrawable);
163
164        int color = mState.mBaseColor;
165        color = a.getColor(com.android.internal.R.styleable.ColorDrawable_color, color);
166        mState.mBaseColor = mState.mUseColor = color;
167
168        a.recycle();
169    }
170
171    @Override
172    public ConstantState getConstantState() {
173        mState.mChangingConfigurations = getChangingConfigurations();
174        return mState;
175    }
176
177    final static class ColorState extends ConstantState {
178        int mBaseColor; // base color, independent of setAlpha()
179        @ViewDebug.ExportedProperty
180        int mUseColor;  // basecolor modulated by setAlpha()
181        int mChangingConfigurations;
182
183        ColorState(ColorState state) {
184            if (state != null) {
185                mBaseColor = state.mBaseColor;
186                mUseColor = state.mUseColor;
187                mChangingConfigurations = state.mChangingConfigurations;
188            }
189        }
190
191        @Override
192        public Drawable newDrawable() {
193            return new ColorDrawable(this);
194        }
195
196        @Override
197        public Drawable newDrawable(Resources res) {
198            return new ColorDrawable(this);
199        }
200
201        @Override
202        public int getChangingConfigurations() {
203            return mChangingConfigurations;
204        }
205    }
206}
207