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.PorterDuff;
22import android.graphics.Rect;
23import android.graphics.Region;
24import android.graphics.drawable.Drawable;
25import android.os.Build;
26import android.support.v4.graphics.drawable.DrawableCompat;
27import android.support.v4.graphics.drawable.TintAwareDrawable;
28import android.util.AttributeSet;
29
30/**
31 * Internal common delegation shared by VectorDrawableCompat and AnimatedVectorDrawableCompat
32 */
33@TargetApi(Build.VERSION_CODES.LOLLIPOP)
34abstract class VectorDrawableCommon extends Drawable implements TintAwareDrawable {
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 DrawableCompat.getColorFilter(mDelegateDrawable);
63        }
64        return null;
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            DrawableCompat.setHotspot(mDelegateDrawable, x, y);
89        }
90        return;
91    }
92
93    @Override
94    public void setHotspotBounds(int left, int top, int right, int bottom) {
95        if (mDelegateDrawable != null) {
96            DrawableCompat.setHotspotBounds(mDelegateDrawable, left, top, right, bottom);
97            return;
98        }
99    }
100
101    @Override
102    public void setFilterBitmap(boolean filter) {
103        if (mDelegateDrawable != null) {
104            mDelegateDrawable.setFilterBitmap(filter);
105            return;
106        }
107    }
108
109    @Override
110    public void jumpToCurrentState() {
111        if (mDelegateDrawable != null) {
112            DrawableCompat.jumpToCurrentState(mDelegateDrawable);
113            return;
114        }
115    }
116
117    @Override
118    public void applyTheme(Resources.Theme t) {
119        // API >= 21 only.
120        if (mDelegateDrawable != null) {
121            DrawableCompat.applyTheme(mDelegateDrawable, t);
122            return;
123        }
124    }
125
126    @Override
127    public void clearColorFilter() {
128        if (mDelegateDrawable != null) {
129            mDelegateDrawable.clearColorFilter();
130            return;
131        }
132        super.clearColorFilter();
133    }
134
135    @Override
136    public Drawable getCurrent() {
137        if (mDelegateDrawable != null) {
138            return mDelegateDrawable.getCurrent();
139        }
140        return super.getCurrent();
141    }
142
143    @Override
144    public int getMinimumWidth() {
145        if (mDelegateDrawable != null) {
146            return mDelegateDrawable.getMinimumWidth();
147        }
148        return super.getMinimumWidth();
149    }
150
151    @Override
152    public int getMinimumHeight() {
153        if (mDelegateDrawable != null) {
154            return mDelegateDrawable.getMinimumHeight();
155        }
156        return super.getMinimumHeight();
157    }
158
159    @Override
160    public boolean getPadding(Rect padding) {
161        if (mDelegateDrawable != null) {
162            return mDelegateDrawable.getPadding(padding);
163        }
164        return super.getPadding(padding);
165    }
166
167    @Override
168    public int[] getState() {
169        if (mDelegateDrawable != null) {
170            return mDelegateDrawable.getState();
171        }
172        return super.getState();
173    }
174
175
176    @Override
177    public Region getTransparentRegion() {
178        if (mDelegateDrawable != null) {
179            return mDelegateDrawable.getTransparentRegion();
180        }
181        return super.getTransparentRegion();
182    }
183
184    @Override
185    public void setChangingConfigurations(int configs) {
186        if (mDelegateDrawable != null) {
187            mDelegateDrawable.setChangingConfigurations(configs);
188            return;
189        }
190        super.setChangingConfigurations(configs);
191    }
192
193    @Override
194    public boolean setState(int[] stateSet) {
195        if (mDelegateDrawable != null) {
196            return mDelegateDrawable.setState(stateSet);
197        }
198        return super.setState(stateSet);
199    }
200}
201