1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.support.v7.widget;
18 
19 import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
20 
21 import android.graphics.PorterDuff;
22 import android.graphics.Rect;
23 import android.graphics.drawable.Drawable;
24 import android.graphics.drawable.DrawableContainer;
25 import android.graphics.drawable.GradientDrawable;
26 import android.graphics.drawable.InsetDrawable;
27 import android.graphics.drawable.LayerDrawable;
28 import android.graphics.drawable.ScaleDrawable;
29 import android.os.Build;
30 import android.support.annotation.NonNull;
31 import android.support.annotation.RestrictTo;
32 import android.support.v4.graphics.drawable.DrawableCompat;
33 import android.util.Log;
34 
35 import java.lang.reflect.Field;
36 import java.lang.reflect.Method;
37 
38 /** @hide */
39 @RestrictTo(LIBRARY_GROUP)
40 public class DrawableUtils {
41 
42     private static final String TAG = "DrawableUtils";
43 
44     public static final Rect INSETS_NONE = new Rect();
45     private static Class<?> sInsetsClazz;
46 
47     private static final String VECTOR_DRAWABLE_CLAZZ_NAME
48             = "android.graphics.drawable.VectorDrawable";
49 
50     static {
51         if (Build.VERSION.SDK_INT >= 18) {
52             try {
53                 sInsetsClazz = Class.forName("android.graphics.Insets");
54             } catch (ClassNotFoundException e) {
55                 // Oh well...
56             }
57         }
58     }
59 
DrawableUtils()60     private DrawableUtils() {}
61 
62     /**
63      * Allows us to get the optical insets for a {@link Drawable}. Since this is hidden we need to
64      * use reflection. Since the {@code Insets} class is hidden also, we return a Rect instead.
65      */
getOpticalBounds(Drawable drawable)66     public static Rect getOpticalBounds(Drawable drawable) {
67         if (sInsetsClazz != null) {
68             try {
69                 // If the Drawable is wrapped, we need to manually unwrap it and process
70                 // the wrapped drawable.
71                 drawable = DrawableCompat.unwrap(drawable);
72 
73                 final Method getOpticalInsetsMethod = drawable.getClass()
74                         .getMethod("getOpticalInsets");
75                 final Object insets = getOpticalInsetsMethod.invoke(drawable);
76 
77                 if (insets != null) {
78                     // If the drawable has some optical insets, let's copy them into a Rect
79                     final Rect result = new Rect();
80 
81                     for (Field field : sInsetsClazz.getFields()) {
82                         switch (field.getName()) {
83                             case "left":
84                                result.left = field.getInt(insets);
85                                 break;
86                             case "top":
87                                 result.top = field.getInt(insets);
88                                 break;
89                             case "right":
90                                 result.right = field.getInt(insets);
91                                 break;
92                             case "bottom":
93                                 result.bottom = field.getInt(insets);
94                                 break;
95                         }
96                     }
97                     return result;
98                 }
99             } catch (Exception e) {
100                 // Eugh, we hit some kind of reflection issue...
101                 Log.e(TAG, "Couldn't obtain the optical insets. Ignoring.");
102             }
103         }
104 
105         // If we reach here, either we're running on a device pre-v18, the Drawable didn't have
106         // any optical insets, or a reflection issue, so we'll just return an empty rect
107         return INSETS_NONE;
108     }
109 
110     /**
111      * Attempt the fix any issues in the given drawable, usually caused by platform bugs in the
112      * implementation. This method should be call after retrieval from
113      * {@link android.content.res.Resources} or a {@link android.content.res.TypedArray}.
114      */
fixDrawable(@onNull final Drawable drawable)115     static void fixDrawable(@NonNull final Drawable drawable) {
116         if (Build.VERSION.SDK_INT == 21
117                 && VECTOR_DRAWABLE_CLAZZ_NAME.equals(drawable.getClass().getName())) {
118             fixVectorDrawableTinting(drawable);
119         }
120     }
121 
122     /**
123      * Some drawable implementations have problems with mutation. This method returns false if
124      * there is a known issue in the given drawable's implementation.
125      */
canSafelyMutateDrawable(@onNull Drawable drawable)126     public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
127         if (Build.VERSION.SDK_INT < 15 && drawable instanceof InsetDrawable) {
128             return false;
129         }  else if (Build.VERSION.SDK_INT < 15 && drawable instanceof GradientDrawable) {
130             // GradientDrawable has a bug pre-ICS which results in mutate() resulting
131             // in loss of color
132             return false;
133         } else if (Build.VERSION.SDK_INT < 17 && drawable instanceof LayerDrawable) {
134             return false;
135         }
136 
137         if (drawable instanceof DrawableContainer) {
138             // If we have a DrawableContainer, let's traverse its child array
139             final Drawable.ConstantState state = drawable.getConstantState();
140             if (state instanceof DrawableContainer.DrawableContainerState) {
141                 final DrawableContainer.DrawableContainerState containerState =
142                         (DrawableContainer.DrawableContainerState) state;
143                 for (final Drawable child : containerState.getChildren()) {
144                     if (!canSafelyMutateDrawable(child)) {
145                         return false;
146                     }
147                 }
148             }
149         } else if (drawable instanceof android.support.v4.graphics.drawable.DrawableWrapper) {
150             return canSafelyMutateDrawable(
151                     ((android.support.v4.graphics.drawable.DrawableWrapper) drawable)
152                             .getWrappedDrawable());
153         } else if (drawable instanceof android.support.v7.graphics.drawable.DrawableWrapper) {
154             return canSafelyMutateDrawable(
155                     ((android.support.v7.graphics.drawable.DrawableWrapper) drawable)
156                             .getWrappedDrawable());
157         } else if (drawable instanceof ScaleDrawable) {
158             return canSafelyMutateDrawable(((ScaleDrawable) drawable).getDrawable());
159         }
160 
161         return true;
162     }
163 
164     /**
165      * VectorDrawable has an issue on API 21 where it sometimes doesn't create its tint filter.
166      * Fixed by toggling its state to force a filter creation.
167      */
fixVectorDrawableTinting(final Drawable drawable)168     private static void fixVectorDrawableTinting(final Drawable drawable) {
169         final int[] originalState = drawable.getState();
170         if (originalState == null || originalState.length == 0) {
171             // The drawable doesn't have a state, so set it to be checked
172             drawable.setState(ThemeUtils.CHECKED_STATE_SET);
173         } else {
174             // Else the drawable does have a state, so clear it
175             drawable.setState(ThemeUtils.EMPTY_STATE_SET);
176         }
177         // Now set the original state
178         drawable.setState(originalState);
179     }
180 
181     /**
182      * Parses tint mode.
183      */
parseTintMode(int value, PorterDuff.Mode defaultMode)184     public static PorterDuff.Mode parseTintMode(int value, PorterDuff.Mode defaultMode) {
185         switch (value) {
186             case 3: return PorterDuff.Mode.SRC_OVER;
187             case 5: return PorterDuff.Mode.SRC_IN;
188             case 9: return PorterDuff.Mode.SRC_ATOP;
189             case 14: return PorterDuff.Mode.MULTIPLY;
190             case 15: return PorterDuff.Mode.SCREEN;
191             case 16: return Build.VERSION.SDK_INT >= 11
192                     ? PorterDuff.Mode.valueOf("ADD")
193                     : defaultMode;
194             default: return defaultMode;
195         }
196     }
197 
198 }
199