• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 package android.support.graphics.drawable;
16 
17 import android.annotation.TargetApi;
18 import android.content.res.Resources;
19 import android.content.res.TypedArray;
20 import android.graphics.ColorFilter;
21 import android.graphics.Outline;
22 import android.graphics.PorterDuff;
23 import android.graphics.Rect;
24 import android.graphics.Region;
25 import android.graphics.drawable.Drawable;
26 import android.os.Build;
27 import android.support.v4.graphics.drawable.DrawableCompat;
28 import android.support.v4.graphics.drawable.TintAwareDrawable;
29 import android.util.AttributeSet;
30 import android.view.View;
31 
32 /**
33  * Internal common delegation shared by VectorDrawableCompat and AnimatedVectorDrawableCompat
34  */
35 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
36 abstract 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      */
obtainAttributes( Resources res, Resources.Theme theme, AttributeSet set, int[] attrs)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
setColorFilter(int color, PorterDuff.Mode mode)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
getColorFilter()62     public ColorFilter getColorFilter() {
63         if (mDelegateDrawable != null) {
64             return DrawableCompat.getColorFilter(mDelegateDrawable);
65         }
66         return null;
67     }
68 
69     @Override
onLevelChange(int level)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
onBoundsChange(Rect bounds)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
setHotspot(float x, float y)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
setHotspotBounds(int left, int top, int right, int bottom)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
setFilterBitmap(boolean filter)104     public void setFilterBitmap(boolean filter) {
105         if (mDelegateDrawable != null) {
106             mDelegateDrawable.setFilterBitmap(filter);
107             return;
108         }
109     }
110 
111     @Override
jumpToCurrentState()112     public void jumpToCurrentState() {
113         if (mDelegateDrawable != null) {
114             DrawableCompat.jumpToCurrentState(mDelegateDrawable);
115             return;
116         }
117     }
118 
119     @Override
setAutoMirrored(boolean mirrored)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
isAutoMirrored()130     public boolean isAutoMirrored() {
131         // API >= 21 only.
132         if (mDelegateDrawable != null) {
133             DrawableCompat.isAutoMirrored(mDelegateDrawable);
134         }
135         return false;
136     }
137 
138     @Override
applyTheme(Resources.Theme t)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
getLayoutDirection()148     public int getLayoutDirection() {
149         if (mDelegateDrawable != null) {
150             DrawableCompat.getLayoutDirection(mDelegateDrawable);
151         }
152         return View.LAYOUT_DIRECTION_LTR;
153     }
154 
155     @Override
clearColorFilter()156     public void clearColorFilter() {
157         if (mDelegateDrawable != null) {
158             mDelegateDrawable.clearColorFilter();
159             return;
160         }
161         super.clearColorFilter();
162     }
163 
164     @Override
getCurrent()165     public Drawable getCurrent() {
166         if (mDelegateDrawable != null) {
167             return mDelegateDrawable.getCurrent();
168         }
169         return super.getCurrent();
170     }
171 
172     @Override
getMinimumWidth()173     public int getMinimumWidth() {
174         if (mDelegateDrawable != null) {
175             return mDelegateDrawable.getMinimumWidth();
176         }
177         return super.getMinimumWidth();
178     }
179 
180     @Override
getMinimumHeight()181     public int getMinimumHeight() {
182         if (mDelegateDrawable != null) {
183             return mDelegateDrawable.getMinimumHeight();
184         }
185         return super.getMinimumHeight();
186     }
187 
188     @Override
getPadding(Rect padding)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
getState()197     public int[] getState() {
198         if (mDelegateDrawable != null) {
199             return mDelegateDrawable.getState();
200         }
201         return super.getState();
202     }
203 
204 
205     @Override
getTransparentRegion()206     public Region getTransparentRegion() {
207         if (mDelegateDrawable != null) {
208             return mDelegateDrawable.getTransparentRegion();
209         }
210         return super.getTransparentRegion();
211     }
212 
213     @Override
setChangingConfigurations(int configs)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
setState(int[] stateSet)223     public boolean setState(int[] stateSet) {
224         if (mDelegateDrawable != null) {
225             return mDelegateDrawable.setState(stateSet);
226         }
227         return super.setState(stateSet);
228     }
229 }
230