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.design.widget;
18
19import android.graphics.drawable.Drawable;
20import android.graphics.drawable.DrawableContainer;
21import android.os.Build;
22import android.util.Log;
23
24import java.lang.reflect.Field;
25import java.lang.reflect.Method;
26
27/**
28 * Caution. Gross hacks ahead.
29 */
30class DrawableUtils {
31
32    private static final String LOG_TAG = "DrawableUtils";
33
34    private static Method sSetConstantStateMethod;
35    private static boolean sSetConstantStateMethodFetched;
36
37    private static Field sDrawableContainerStateField;
38    private static boolean sDrawableContainerStateFieldFetched;
39
40    private DrawableUtils() {}
41
42    static boolean setContainerConstantState(DrawableContainer drawable,
43            Drawable.ConstantState constantState) {
44        if (Build.VERSION.SDK_INT >= 9) {
45            // We can use getDeclaredMethod() on v9+
46            return setContainerConstantStateV9(drawable, constantState);
47        } else {
48            // Else we'll just have to set the field directly
49            return setContainerConstantStateV7(drawable, constantState);
50        }
51    }
52
53    private static boolean setContainerConstantStateV9(DrawableContainer drawable,
54            Drawable.ConstantState constantState) {
55        if (!sSetConstantStateMethodFetched) {
56            try {
57                sSetConstantStateMethod = DrawableContainer.class.getDeclaredMethod(
58                        "setConstantState", DrawableContainer.DrawableContainerState.class);
59                sSetConstantStateMethod.setAccessible(true);
60            } catch (NoSuchMethodException e) {
61                Log.e(LOG_TAG, "Could not fetch setConstantState(). Oh well.");
62            }
63            sSetConstantStateMethodFetched = true;
64        }
65        if (sSetConstantStateMethod != null) {
66            try {
67                sSetConstantStateMethod.invoke(drawable, constantState);
68                return true;
69            } catch (Exception e) {
70                Log.e(LOG_TAG, "Could not invoke setConstantState(). Oh well.");
71            }
72        }
73        return false;
74    }
75
76    private static boolean setContainerConstantStateV7(DrawableContainer drawable,
77            Drawable.ConstantState constantState) {
78        if (!sDrawableContainerStateFieldFetched) {
79            try {
80                sDrawableContainerStateField = DrawableContainer.class
81                        .getDeclaredField("mDrawableContainerStateField");
82                sDrawableContainerStateField.setAccessible(true);
83            } catch (NoSuchFieldException e) {
84                Log.e(LOG_TAG, "Could not fetch mDrawableContainerStateField. Oh well.");
85            }
86            sDrawableContainerStateFieldFetched = true;
87        }
88        if (sDrawableContainerStateField != null) {
89            try {
90                sDrawableContainerStateField.set(drawable, constantState);
91                return true;
92            } catch (Exception e) {
93                Log.e(LOG_TAG, "Could not set mDrawableContainerStateField. Oh well.");
94            }
95        }
96        return false;
97    }
98
99}
100