VectorDrawableCommon.java revision 2c3c8bff4c669316cdc2db24b72d9ac3f9b33725
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package android.support.graphics.drawable;
16
17import android.annotation.TargetApi;
18import android.content.res.Resources;
19import android.content.res.TypedArray;
20import android.graphics.ColorFilter;
21import android.graphics.Outline;
22import android.graphics.PorterDuff;
23import android.graphics.Rect;
24import android.graphics.Region;
25import android.graphics.drawable.Drawable;
26import android.os.Build;
27import android.util.AttributeSet;
28import android.view.View;
29
30/**
31 * Internal common delegation shared by VectorDrawableCompat and AnimatedVectorDrawableCompat
32 */
33@TargetApi(Build.VERSION_CODES.LOLLIPOP)
34abstract class VectorDrawableCommon extends Drawable {
35    /**
36     * Obtains styled attributes from the theme, if available, or unstyled
37     * resources if the theme is null.
38     */
39    static TypedArray obtainAttributes(
40            Resources res, Resources.Theme theme, AttributeSet set, int[] attrs) {
41        if (theme == null) {
42            return res.obtainAttributes(set, attrs);
43        }
44        return theme.obtainStyledAttributes(set, attrs, 0, 0);
45    }
46
47    // Drawable delegation for Lollipop and above.
48    Drawable mDelegateDrawable;
49
50    @Override
51    public void setColorFilter(int color, PorterDuff.Mode mode) {
52        if (mDelegateDrawable != null) {
53            mDelegateDrawable.setColorFilter(color, mode);
54            return;
55        }
56        super.setColorFilter(color, mode);
57    }
58
59    @Override
60    public ColorFilter getColorFilter() {
61        if (mDelegateDrawable != null) {
62            return mDelegateDrawable.getColorFilter();
63        }
64        return super.getColorFilter();
65    }
66
67    @Override
68    protected boolean onLevelChange(int level) {
69        if (mDelegateDrawable != null) {
70            return mDelegateDrawable.setLevel(level);
71        }
72        return super.onLevelChange(level);
73    }
74
75    @Override
76    protected void onBoundsChange(Rect bounds) {
77        if (mDelegateDrawable != null) {
78            mDelegateDrawable.setBounds(bounds);
79            return;
80        }
81        super.onBoundsChange(bounds);
82    }
83
84    @Override
85    public void setHotspot(float x, float y) {
86        // API >= 21 only.
87        if (mDelegateDrawable != null) {
88            mDelegateDrawable.setHotspot(x, y);
89        }
90        return;
91    }
92
93    @Override
94    public void setHotspotBounds(int left, int top, int right, int bottom) {
95        // API >= 21 only.
96        if (mDelegateDrawable != null) {
97            mDelegateDrawable.setHotspotBounds(left, top, right, bottom);
98            return;
99        }
100    }
101
102    @TargetApi(23)
103    @Override
104    public void getHotspotBounds(Rect outRect) {
105        // API >= 21 only.
106        if (mDelegateDrawable != null && Build.VERSION.SDK_INT >= 23) {
107            mDelegateDrawable.getHotspotBounds(outRect);
108            return;
109        }
110    }
111
112    @Override
113    public Rect getDirtyBounds() {
114        if (mDelegateDrawable != null) {
115            return mDelegateDrawable.getBounds();
116        }
117        return super.getDirtyBounds();
118    }
119
120    @Override
121    public void setFilterBitmap(boolean filter) {
122        if (mDelegateDrawable != null) {
123            mDelegateDrawable.setFilterBitmap(filter);
124            return;
125        }
126    }
127
128    @TargetApi(23)
129    @Override
130    public boolean isFilterBitmap() {
131        // API >= 23 only
132        if (mDelegateDrawable != null && Build.VERSION.SDK_INT >= 23) {
133            return mDelegateDrawable.isFilterBitmap();
134        }
135        return false;
136    }
137
138    @Override
139    public void getOutline(Outline outline) {
140        // API >= 21 only
141        if (mDelegateDrawable != null) {
142            mDelegateDrawable.getOutline(outline);
143            return;
144        }
145    }
146
147    @Override
148    public void jumpToCurrentState() {
149        if (mDelegateDrawable != null) {
150            mDelegateDrawable.jumpToCurrentState();
151            return;
152        }
153    }
154
155    @Override
156    public void setAutoMirrored(boolean mirrored) {
157        // API >= 21 only.
158        if (mDelegateDrawable != null) {
159            mDelegateDrawable.setAutoMirrored(mirrored);
160            return;
161        }
162    }
163
164    @Override
165    public boolean isAutoMirrored() {
166        // API >= 21 only.
167        if (mDelegateDrawable != null) {
168            return mDelegateDrawable.isAutoMirrored();
169        }
170        return false;
171    }
172
173    @Override
174    public void applyTheme(Resources.Theme t) {
175        // API >= 21 only.
176        if (mDelegateDrawable != null) {
177            mDelegateDrawable.applyTheme(t);
178            return;
179        }
180    }
181
182    @TargetApi(23)
183    @Override
184    public int getLayoutDirection() {
185        if (mDelegateDrawable != null && Build.VERSION.SDK_INT >= 23) {
186            return mDelegateDrawable.getLayoutDirection();
187        }
188        return View.LAYOUT_DIRECTION_LTR;
189    }
190
191    @TargetApi(23)
192    @Override
193    public boolean onLayoutDirectionChanged(int layoutDirection) {
194        if (mDelegateDrawable != null && Build.VERSION.SDK_INT >= 23) {
195            return mDelegateDrawable.onLayoutDirectionChanged(layoutDirection);
196        }
197        return false;
198    }
199
200    @Override
201    public void clearColorFilter() {
202        if (mDelegateDrawable != null) {
203            mDelegateDrawable.clearColorFilter();
204            return;
205        }
206        super.clearColorFilter();
207    }
208
209    @Override
210    public Drawable getCurrent() {
211        if (mDelegateDrawable != null) {
212            return mDelegateDrawable.getCurrent();
213        }
214        return super.getCurrent();
215    }
216
217    @Override
218    public int getMinimumWidth() {
219        if (mDelegateDrawable != null) {
220            return mDelegateDrawable.getMinimumWidth();
221        }
222        return super.getMinimumWidth();
223    }
224
225    @Override
226    public int getMinimumHeight() {
227        if (mDelegateDrawable != null) {
228            return mDelegateDrawable.getMinimumHeight();
229        }
230        return super.getMinimumHeight();
231    }
232
233    @Override
234    public boolean getPadding(Rect padding) {
235        if (mDelegateDrawable != null) {
236            return mDelegateDrawable.getPadding(padding);
237        }
238        return super.getPadding(padding);
239    }
240
241    @Override
242    public int[] getState() {
243        if (mDelegateDrawable != null) {
244            return mDelegateDrawable.getState();
245        }
246        return super.getState();
247    }
248
249
250    @Override
251    public Region getTransparentRegion() {
252        if (mDelegateDrawable != null) {
253            return mDelegateDrawable.getTransparentRegion();
254        }
255        return super.getTransparentRegion();
256    }
257
258    @Override
259    public void setChangingConfigurations(int configs) {
260        if (mDelegateDrawable != null) {
261            mDelegateDrawable.setChangingConfigurations(configs);
262            return;
263        }
264        super.setChangingConfigurations(configs);
265    }
266
267    @Override
268    public boolean setState(int[] stateSet) {
269        if (mDelegateDrawable != null) {
270            return mDelegateDrawable.setState(stateSet);
271        }
272        return super.setState(stateSet);
273    }
274}
275