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 org.xmlpull.v1.XmlPullParser;
20import org.xmlpull.v1.XmlPullParserException;
21
22import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.*;
25import android.util.AttributeSet;
26import android.util.Log;
27
28import java.io.IOException;
29
30/**
31 * A Drawable that insets another Drawable by a specified distance.
32 * This is used when a View needs a background that is smaller than
33 * the View's actual bounds.
34 *
35 * <p>It can be defined in an XML file with the <code>&lt;inset></code> element. For more
36 * information, see the guide to <a
37 * href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a>.</p>
38 *
39 * @attr ref android.R.styleable#InsetDrawable_visible
40 * @attr ref android.R.styleable#InsetDrawable_drawable
41 * @attr ref android.R.styleable#InsetDrawable_insetLeft
42 * @attr ref android.R.styleable#InsetDrawable_insetRight
43 * @attr ref android.R.styleable#InsetDrawable_insetTop
44 * @attr ref android.R.styleable#InsetDrawable_insetBottom
45 */
46public class InsetDrawable extends Drawable implements Drawable.Callback
47{
48    // Most of this is copied from ScaleDrawable.
49    private InsetState mInsetState;
50    private final Rect mTmpRect = new Rect();
51    private boolean mMutated;
52
53    /*package*/ InsetDrawable() {
54        this(null, null);
55    }
56
57    public InsetDrawable(Drawable drawable, int inset) {
58        this(drawable, inset, inset, inset, inset);
59    }
60
61    public InsetDrawable(Drawable drawable, int insetLeft, int insetTop,
62                         int insetRight, int insetBottom) {
63        this(null, null);
64
65        mInsetState.mDrawable = drawable;
66        mInsetState.mInsetLeft = insetLeft;
67        mInsetState.mInsetTop = insetTop;
68        mInsetState.mInsetRight = insetRight;
69        mInsetState.mInsetBottom = insetBottom;
70
71        if (drawable != null) {
72            drawable.setCallback(this);
73        }
74    }
75
76    @Override public void inflate(Resources r, XmlPullParser parser,
77                                  AttributeSet attrs)
78    throws XmlPullParserException, IOException {
79        int type;
80
81        TypedArray a = r.obtainAttributes(attrs,
82                com.android.internal.R.styleable.InsetDrawable);
83
84        super.inflateWithAttributes(r, parser, a,
85                com.android.internal.R.styleable.InsetDrawable_visible);
86
87        int drawableRes = a.getResourceId(com.android.internal.R.styleable.
88                                    InsetDrawable_drawable, 0);
89
90        int inLeft = a.getDimensionPixelOffset(com.android.internal.R.styleable.
91                                    InsetDrawable_insetLeft, 0);
92        int inTop = a.getDimensionPixelOffset(com.android.internal.R.styleable.
93                                    InsetDrawable_insetTop, 0);
94        int inRight = a.getDimensionPixelOffset(com.android.internal.R.styleable.
95                                    InsetDrawable_insetRight, 0);
96        int inBottom = a.getDimensionPixelOffset(com.android.internal.R.styleable.
97                                    InsetDrawable_insetBottom, 0);
98
99        a.recycle();
100
101        Drawable dr;
102        if (drawableRes != 0) {
103            dr = r.getDrawable(drawableRes);
104        } else {
105            while ((type=parser.next()) == XmlPullParser.TEXT) {
106            }
107            if (type != XmlPullParser.START_TAG) {
108                throw new XmlPullParserException(
109                        parser.getPositionDescription()
110                        + ": <inset> tag requires a 'drawable' attribute or "
111                        + "child tag defining a drawable");
112            }
113            dr = Drawable.createFromXmlInner(r, parser, attrs);
114        }
115
116        if (dr == null) {
117            Log.w("drawable", "No drawable specified for <inset>");
118        }
119
120        mInsetState.mDrawable = dr;
121        mInsetState.mInsetLeft = inLeft;
122        mInsetState.mInsetRight = inRight;
123        mInsetState.mInsetTop = inTop;
124        mInsetState.mInsetBottom = inBottom;
125
126        if (dr != null) {
127            dr.setCallback(this);
128        }
129    }
130
131    // overrides from Drawable.Callback
132
133    public void invalidateDrawable(Drawable who) {
134        if (mCallback != null) {
135            mCallback.invalidateDrawable(this);
136        }
137    }
138
139    public void scheduleDrawable(Drawable who, Runnable what, long when) {
140        if (mCallback != null) {
141            mCallback.scheduleDrawable(this, what, when);
142        }
143    }
144
145    public void unscheduleDrawable(Drawable who, Runnable what) {
146        if (mCallback != null) {
147            mCallback.unscheduleDrawable(this, what);
148        }
149    }
150
151    // overrides from Drawable
152
153    @Override
154    public void draw(Canvas canvas) {
155        mInsetState.mDrawable.draw(canvas);
156    }
157
158    @Override
159    public int getChangingConfigurations() {
160        return super.getChangingConfigurations()
161                | mInsetState.mChangingConfigurations
162                | mInsetState.mDrawable.getChangingConfigurations();
163    }
164
165    @Override
166    public boolean getPadding(Rect padding) {
167        boolean pad = mInsetState.mDrawable.getPadding(padding);
168
169        padding.left += mInsetState.mInsetLeft;
170        padding.right += mInsetState.mInsetRight;
171        padding.top += mInsetState.mInsetTop;
172        padding.bottom += mInsetState.mInsetBottom;
173
174        if (pad || (mInsetState.mInsetLeft | mInsetState.mInsetRight |
175                    mInsetState.mInsetTop | mInsetState.mInsetBottom) != 0) {
176            return true;
177        } else {
178            return false;
179        }
180    }
181
182    @Override
183    public boolean setVisible(boolean visible, boolean restart) {
184        mInsetState.mDrawable.setVisible(visible, restart);
185        return super.setVisible(visible, restart);
186    }
187
188    @Override
189    public void setAlpha(int alpha) {
190        mInsetState.mDrawable.setAlpha(alpha);
191    }
192
193    @Override
194    public void setColorFilter(ColorFilter cf) {
195        mInsetState.mDrawable.setColorFilter(cf);
196    }
197
198    @Override
199    public int getOpacity() {
200        return mInsetState.mDrawable.getOpacity();
201    }
202
203    @Override
204    public boolean isStateful() {
205        return mInsetState.mDrawable.isStateful();
206    }
207
208    @Override
209    protected boolean onStateChange(int[] state) {
210        boolean changed = mInsetState.mDrawable.setState(state);
211        onBoundsChange(getBounds());
212        return changed;
213    }
214
215    @Override
216    protected void onBoundsChange(Rect bounds) {
217        final Rect r = mTmpRect;
218        r.set(bounds);
219
220        r.left += mInsetState.mInsetLeft;
221        r.top += mInsetState.mInsetTop;
222        r.right -= mInsetState.mInsetRight;
223        r.bottom -= mInsetState.mInsetBottom;
224
225        mInsetState.mDrawable.setBounds(r.left, r.top, r.right, r.bottom);
226    }
227
228    @Override
229    public int getIntrinsicWidth() {
230        return mInsetState.mDrawable.getIntrinsicWidth();
231    }
232
233    @Override
234    public int getIntrinsicHeight() {
235        return mInsetState.mDrawable.getIntrinsicHeight();
236    }
237
238    @Override
239    public ConstantState getConstantState() {
240        if (mInsetState.canConstantState()) {
241            mInsetState.mChangingConfigurations = super.getChangingConfigurations();
242            return mInsetState;
243        }
244        return null;
245    }
246
247    @Override
248    public Drawable mutate() {
249        if (!mMutated && super.mutate() == this) {
250            mInsetState.mDrawable.mutate();
251            mMutated = true;
252        }
253        return this;
254    }
255
256    final static class InsetState extends ConstantState {
257        Drawable mDrawable;
258        int mChangingConfigurations;
259
260        int mInsetLeft;
261        int mInsetTop;
262        int mInsetRight;
263        int mInsetBottom;
264
265        boolean mCheckedConstantState;
266        boolean mCanConstantState;
267
268        InsetState(InsetState orig, InsetDrawable owner, Resources res) {
269            if (orig != null) {
270                if (res != null) {
271                    mDrawable = orig.mDrawable.getConstantState().newDrawable(res);
272                } else {
273                    mDrawable = orig.mDrawable.getConstantState().newDrawable();
274                }
275                mDrawable.setCallback(owner);
276                mInsetLeft = orig.mInsetLeft;
277                mInsetTop = orig.mInsetTop;
278                mInsetRight = orig.mInsetRight;
279                mInsetBottom = orig.mInsetBottom;
280                mCheckedConstantState = mCanConstantState = true;
281            }
282        }
283
284        @Override
285        public Drawable newDrawable() {
286            return new InsetDrawable(this, null);
287        }
288
289        @Override
290        public Drawable newDrawable(Resources res) {
291            return new InsetDrawable(this, res);
292        }
293
294        @Override
295        public int getChangingConfigurations() {
296            return mChangingConfigurations;
297        }
298
299        boolean canConstantState() {
300            if (!mCheckedConstantState) {
301                mCanConstantState = mDrawable.getConstantState() != null;
302                mCheckedConstantState = true;
303            }
304
305            return mCanConstantState;
306        }
307    }
308
309    private InsetDrawable(InsetState state, Resources res) {
310        mInsetState = new InsetState(state, this, res);
311    }
312}
313
314