VectorDrawableCommon.java revision 78fe6d45794f3e5f2b879ea4f931081f5ebfa334
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.support.v4.graphics.drawable.DrawableCompat;
28import android.util.AttributeSet;
29import android.view.View;
30
31/**
32 * Internal common delegation shared by VectorDrawableCompat and AnimatedVectorDrawableCompat
33 */
34@TargetApi(Build.VERSION_CODES.LOLLIPOP)
35abstract class VectorDrawableCommon extends Drawable {
36    /**
37     * Obtains styled attributes from the theme, if available, or unstyled
38     * resources if the theme is null.
39     */
40    static TypedArray obtainAttributes(
41            Resources res, Resources.Theme theme, AttributeSet set, int[] attrs) {
42        if (theme == null) {
43            return res.obtainAttributes(set, attrs);
44        }
45        return theme.obtainStyledAttributes(set, attrs, 0, 0);
46    }
47
48    // Drawable delegation for Lollipop and above.
49    Drawable mDelegateDrawable;
50
51    @Override
52    public void setColorFilter(int color, PorterDuff.Mode mode) {
53        if (mDelegateDrawable != null) {
54            mDelegateDrawable.setColorFilter(color, mode);
55            return;
56        }
57        super.setColorFilter(color, mode);
58    }
59
60    @Override
61    public ColorFilter getColorFilter() {
62        if (mDelegateDrawable != null) {
63            return DrawableCompat.getColorFilter(mDelegateDrawable);
64        }
65        return null;
66    }
67
68    @Override
69    protected boolean onLevelChange(int level) {
70        if (mDelegateDrawable != null) {
71            return mDelegateDrawable.setLevel(level);
72        }
73        return super.onLevelChange(level);
74    }
75
76    @Override
77    protected void onBoundsChange(Rect bounds) {
78        if (mDelegateDrawable != null) {
79            mDelegateDrawable.setBounds(bounds);
80            return;
81        }
82        super.onBoundsChange(bounds);
83    }
84
85    @Override
86    public void setHotspot(float x, float y) {
87        // API >= 21 only.
88        if (mDelegateDrawable != null) {
89            DrawableCompat.setHotspot(mDelegateDrawable, x, y);
90        }
91        return;
92    }
93
94    @Override
95    public void setHotspotBounds(int left, int top, int right, int bottom) {
96        if (mDelegateDrawable != null) {
97            DrawableCompat.setHotspotBounds(mDelegateDrawable, left, top, right, bottom);
98            return;
99        }
100    }
101
102    @Override
103    public void setFilterBitmap(boolean filter) {
104        if (mDelegateDrawable != null) {
105            mDelegateDrawable.setFilterBitmap(filter);
106            return;
107        }
108    }
109
110    @Override
111    public void jumpToCurrentState() {
112        if (mDelegateDrawable != null) {
113            DrawableCompat.jumpToCurrentState(mDelegateDrawable);
114            return;
115        }
116    }
117
118    @Override
119    public void setAutoMirrored(boolean mirrored) {
120        // API >= 21 only.
121        if (mDelegateDrawable != null) {
122            DrawableCompat.setAutoMirrored(mDelegateDrawable, mirrored);
123
124            return;
125        }
126    }
127
128    @Override
129    public boolean isAutoMirrored() {
130        // API >= 21 only.
131        if (mDelegateDrawable != null) {
132            DrawableCompat.isAutoMirrored(mDelegateDrawable);
133        }
134        return false;
135    }
136
137    @Override
138    public void applyTheme(Resources.Theme t) {
139        // API >= 21 only.
140        if (mDelegateDrawable != null) {
141            DrawableCompat.applyTheme(mDelegateDrawable, t);
142            return;
143        }
144    }
145
146    @Override
147    public int getLayoutDirection() {
148        if (mDelegateDrawable != null) {
149            DrawableCompat.getLayoutDirection(mDelegateDrawable);
150        }
151        return View.LAYOUT_DIRECTION_LTR;
152    }
153
154    @Override
155    public void clearColorFilter() {
156        if (mDelegateDrawable != null) {
157            mDelegateDrawable.clearColorFilter();
158            return;
159        }
160        super.clearColorFilter();
161    }
162
163    @Override
164    public Drawable getCurrent() {
165        if (mDelegateDrawable != null) {
166            return mDelegateDrawable.getCurrent();
167        }
168        return super.getCurrent();
169    }
170
171    @Override
172    public int getMinimumWidth() {
173        if (mDelegateDrawable != null) {
174            return mDelegateDrawable.getMinimumWidth();
175        }
176        return super.getMinimumWidth();
177    }
178
179    @Override
180    public int getMinimumHeight() {
181        if (mDelegateDrawable != null) {
182            return mDelegateDrawable.getMinimumHeight();
183        }
184        return super.getMinimumHeight();
185    }
186
187    @Override
188    public boolean getPadding(Rect padding) {
189        if (mDelegateDrawable != null) {
190            return mDelegateDrawable.getPadding(padding);
191        }
192        return super.getPadding(padding);
193    }
194
195    @Override
196    public int[] getState() {
197        if (mDelegateDrawable != null) {
198            return mDelegateDrawable.getState();
199        }
200        return super.getState();
201    }
202
203
204    @Override
205    public Region getTransparentRegion() {
206        if (mDelegateDrawable != null) {
207            return mDelegateDrawable.getTransparentRegion();
208        }
209        return super.getTransparentRegion();
210    }
211
212    @Override
213    public void setChangingConfigurations(int configs) {
214        if (mDelegateDrawable != null) {
215            mDelegateDrawable.setChangingConfigurations(configs);
216            return;
217        }
218        super.setChangingConfigurations(configs);
219    }
220
221    @Override
222    public boolean setState(int[] stateSet) {
223        if (mDelegateDrawable != null) {
224            return mDelegateDrawable.setState(stateSet);
225        }
226        return super.setState(stateSet);
227    }
228}
229