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