1/*
2 * Copyright (C) 2015 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.Outline;
22import android.graphics.PorterDuff;
23import android.graphics.Rect;
24import android.graphics.drawable.Drawable;
25import android.graphics.drawable.DrawableContainer;
26import android.graphics.drawable.GradientDrawable;
27import android.graphics.drawable.InsetDrawable;
28import android.os.Build;
29import android.support.annotation.NonNull;
30import android.support.annotation.Nullable;
31
32class DrawableWrapperLollipop extends DrawableWrapperKitKat {
33
34    DrawableWrapperLollipop(Drawable drawable) {
35        super(drawable);
36    }
37
38    DrawableWrapperLollipop(DrawableWrapperState state, Resources resources) {
39        super(state, resources);
40    }
41
42    @Override
43    public void setHotspot(float x, float y) {
44        mDrawable.setHotspot(x, y);
45    }
46
47    @Override
48    public void setHotspotBounds(int left, int top, int right, int bottom) {
49        mDrawable.setHotspotBounds(left, top, right, bottom);
50    }
51
52    @Override
53    public void getOutline(Outline outline) {
54        mDrawable.getOutline(outline);
55    }
56
57    @Override
58    public Rect getDirtyBounds() {
59        return mDrawable.getDirtyBounds();
60    }
61
62    @Override
63    public void setTintList(ColorStateList tint) {
64        if (isCompatTintEnabled()) {
65            super.setTintList(tint);
66        } else {
67            mDrawable.setTintList(tint);
68        }
69    }
70
71    @Override
72    public void setTint(int tintColor) {
73        if (isCompatTintEnabled()) {
74            super.setTint(tintColor);
75        } else {
76            mDrawable.setTint(tintColor);
77        }
78    }
79
80    @Override
81    public void setTintMode(PorterDuff.Mode tintMode) {
82        if (isCompatTintEnabled()) {
83            super.setTintMode(tintMode);
84        } else {
85            mDrawable.setTintMode(tintMode);
86        }
87    }
88
89    @Override
90    public boolean setState(int[] stateSet) {
91        if (super.setState(stateSet)) {
92            // Manually invalidate because the framework doesn't currently force an invalidation
93            // on a state change
94            invalidateSelf();
95            return true;
96        }
97        return false;
98    }
99
100    @Override
101    protected boolean isCompatTintEnabled() {
102        if (Build.VERSION.SDK_INT == 21) {
103            final Drawable drawable = mDrawable;
104            return drawable instanceof GradientDrawable || drawable instanceof DrawableContainer
105                    || drawable instanceof InsetDrawable;
106        }
107        return false;
108    }
109
110    @NonNull
111    @Override
112    DrawableWrapperState mutateConstantState() {
113        return new DrawableWrapperStateLollipop(mState, null);
114    }
115
116    private static class DrawableWrapperStateLollipop extends DrawableWrapperState {
117        DrawableWrapperStateLollipop(@Nullable DrawableWrapperState orig,
118                @Nullable Resources res) {
119            super(orig, res);
120        }
121
122        @Override
123        public Drawable newDrawable(@Nullable Resources res) {
124            return new DrawableWrapperLollipop(this, res);
125        }
126    }
127}
128