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.PorterDuff; 22 import android.graphics.Rect; 23 import android.graphics.Region; 24 import android.graphics.drawable.Drawable; 25 import android.os.Build; 26 import android.support.v4.graphics.drawable.DrawableCompat; 27 import android.support.v4.graphics.drawable.TintAwareDrawable; 28 import android.util.AttributeSet; 29 30 /** 31 * Internal common delegation shared by VectorDrawableCompat and AnimatedVectorDrawableCompat 32 */ 33 @TargetApi(Build.VERSION_CODES.LOLLIPOP) 34 abstract 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 */ obtainAttributes( Resources res, Resources.Theme theme, AttributeSet set, int[] attrs)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 setColorFilter(int color, PorterDuff.Mode mode)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 getColorFilter()60 public ColorFilter getColorFilter() { 61 if (mDelegateDrawable != null) { 62 return DrawableCompat.getColorFilter(mDelegateDrawable); 63 } 64 return null; 65 } 66 67 @Override onLevelChange(int level)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 onBoundsChange(Rect bounds)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 setHotspot(float x, float y)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 setHotspotBounds(int left, int top, int right, int bottom)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 setFilterBitmap(boolean filter)102 public void setFilterBitmap(boolean filter) { 103 if (mDelegateDrawable != null) { 104 mDelegateDrawable.setFilterBitmap(filter); 105 return; 106 } 107 } 108 109 @Override jumpToCurrentState()110 public void jumpToCurrentState() { 111 if (mDelegateDrawable != null) { 112 DrawableCompat.jumpToCurrentState(mDelegateDrawable); 113 return; 114 } 115 } 116 117 @Override applyTheme(Resources.Theme t)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 clearColorFilter()127 public void clearColorFilter() { 128 if (mDelegateDrawable != null) { 129 mDelegateDrawable.clearColorFilter(); 130 return; 131 } 132 super.clearColorFilter(); 133 } 134 135 @Override getCurrent()136 public Drawable getCurrent() { 137 if (mDelegateDrawable != null) { 138 return mDelegateDrawable.getCurrent(); 139 } 140 return super.getCurrent(); 141 } 142 143 @Override getMinimumWidth()144 public int getMinimumWidth() { 145 if (mDelegateDrawable != null) { 146 return mDelegateDrawable.getMinimumWidth(); 147 } 148 return super.getMinimumWidth(); 149 } 150 151 @Override getMinimumHeight()152 public int getMinimumHeight() { 153 if (mDelegateDrawable != null) { 154 return mDelegateDrawable.getMinimumHeight(); 155 } 156 return super.getMinimumHeight(); 157 } 158 159 @Override getPadding(Rect padding)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 getState()168 public int[] getState() { 169 if (mDelegateDrawable != null) { 170 return mDelegateDrawable.getState(); 171 } 172 return super.getState(); 173 } 174 175 176 @Override getTransparentRegion()177 public Region getTransparentRegion() { 178 if (mDelegateDrawable != null) { 179 return mDelegateDrawable.getTransparentRegion(); 180 } 181 return super.getTransparentRegion(); 182 } 183 184 @Override setChangingConfigurations(int configs)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 setState(int[] stateSet)194 public boolean setState(int[] stateSet) { 195 if (mDelegateDrawable != null) { 196 return mDelegateDrawable.setState(stateSet); 197 } 198 return super.setState(stateSet); 199 } 200 } 201