DrawableCompatLollipop.java revision 2aabff2e355c96bf90d6047a613ce0fa2c1ffe45
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
17package android.support.v4.graphics.drawable;
18
19import android.content.res.ColorStateList;
20import android.content.res.Resources;
21import android.graphics.ColorFilter;
22import android.graphics.PorterDuff;
23import android.graphics.drawable.Drawable;
24import android.graphics.drawable.DrawableContainer;
25import android.graphics.drawable.InsetDrawable;
26import android.util.AttributeSet;
27
28import org.xmlpull.v1.XmlPullParser;
29import org.xmlpull.v1.XmlPullParserException;
30
31import java.io.IOException;
32
33/**
34 * Implementation of drawable compatibility that can call L APIs.
35 */
36class DrawableCompatLollipop {
37
38    public static void setHotspot(Drawable drawable, float x, float y) {
39        drawable.setHotspot(x, y);
40    }
41
42    public static void setHotspotBounds(Drawable drawable, int left, int top,
43            int right, int bottom) {
44        drawable.setHotspotBounds( left, top, right, bottom);
45    }
46
47    public static void setTint(Drawable drawable, int tint) {
48        drawable.setTint(tint);
49    }
50
51    public static void setTintList(Drawable drawable, ColorStateList tint) {
52        drawable.setTintList(tint);
53    }
54
55    public static void setTintMode(Drawable drawable, PorterDuff.Mode tintMode) {
56        drawable.setTintMode(tintMode);
57    }
58
59    public static Drawable wrapForTinting(final Drawable drawable) {
60        if (!(drawable instanceof TintAwareDrawable)) {
61            return new DrawableWrapperLollipop(drawable);
62        }
63        return drawable;
64    }
65
66    public static void applyTheme(Drawable drawable, Resources.Theme t) {
67        drawable.applyTheme(t);
68    }
69
70    public static boolean canApplyTheme(Drawable drawable) {
71        return drawable.canApplyTheme();
72    }
73
74    public static ColorFilter getColorFilter(Drawable drawable) {
75        return drawable.getColorFilter();
76    }
77
78    public static void clearColorFilter(Drawable drawable) {
79        drawable.clearColorFilter();
80
81        // API 21 + 22 have an issue where clearing a color filter on a DrawableContainer
82        // will not propagate to all of its children. To workaround this we unwrap the drawable
83        // to find any DrawableContainers, and then unwrap those to clear the filter on its
84        // children manually
85        if (drawable instanceof InsetDrawable) {
86            clearColorFilter(((InsetDrawable) drawable).getDrawable());
87        } else if (drawable instanceof DrawableWrapper) {
88            clearColorFilter(((DrawableWrapper) drawable).getWrappedDrawable());
89        } else if (drawable instanceof DrawableContainer) {
90            final DrawableContainer container = (DrawableContainer) drawable;
91            final DrawableContainer.DrawableContainerState state =
92                    (DrawableContainer.DrawableContainerState) container.getConstantState();
93            if (state != null) {
94                Drawable child;
95                for (int i = 0, count = state.getChildCount(); i < count; i++) {
96                    child = state.getChild(i);
97                    if (child != null) {
98                        clearColorFilter(child);
99                    }
100                }
101            }
102        }
103    }
104
105    public static void inflate(Drawable drawable, Resources res, XmlPullParser parser,
106                               AttributeSet attrs, Resources.Theme t)
107            throws IOException, XmlPullParserException {
108        drawable.inflate(res, parser, attrs, t);
109    }
110}
111