1 /*
2  * Copyright (C) 2016 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 com.android.deskclock;
18 
19 import android.content.Context;
20 import android.content.res.ColorStateList;
21 import android.content.res.TypedArray;
22 import android.graphics.Color;
23 import android.graphics.drawable.Drawable;
24 import android.support.annotation.AttrRes;
25 import android.support.annotation.ColorInt;
26 
27 public final class ThemeUtils {
28 
29     /** Temporary array used internally to resolve attributes. */
30     private static final int[] TEMP_ATTR = new int[1];
31 
ThemeUtils()32     private ThemeUtils() {
33         // Prevent instantiation.
34     }
35 
36     /**
37      * Convenience method for retrieving a themed color value.
38      *
39      * @param context the {@link Context} to resolve the theme attribute against
40      * @param attr    the attribute corresponding to the color to resolve
41      * @return the color value of the resolved attribute
42      */
43     @ColorInt
resolveColor(Context context, @AttrRes int attr)44     public static int resolveColor(Context context, @AttrRes int attr) {
45         return resolveColor(context, attr, null /* stateSet */);
46     }
47 
48     /**
49      * Convenience method for retrieving a themed color value.
50      *
51      * @param context  the {@link Context} to resolve the theme attribute against
52      * @param attr     the attribute corresponding to the color to resolve
53      * @param stateSet an array of {@link android.view.View} states
54      * @return the color value of the resolved attribute
55      */
56     @ColorInt
resolveColor(Context context, @AttrRes int attr, @AttrRes int[] stateSet)57     public static int resolveColor(Context context, @AttrRes int attr, @AttrRes int[] stateSet) {
58         final TypedArray a;
59         synchronized (TEMP_ATTR) {
60             TEMP_ATTR[0] = attr;
61             a = context.obtainStyledAttributes(TEMP_ATTR);
62         }
63 
64         try {
65             if (stateSet == null) {
66                 return a.getColor(0, Color.RED);
67             }
68 
69             final ColorStateList colorStateList = a.getColorStateList(0);
70             if (colorStateList != null) {
71                 return colorStateList.getColorForState(stateSet, Color.RED);
72             }
73             return Color.RED;
74         } finally {
75             a.recycle();
76         }
77     }
78 
79     /**
80      * Convenience method for retrieving a themed drawable.
81      *
82      * @param context the {@link Context} to resolve the theme attribute against
83      * @param attr    the attribute corresponding to the drawable to resolve
84      * @return the drawable of the resolved attribute
85      */
resolveDrawable(Context context, @AttrRes int attr)86     public static Drawable resolveDrawable(Context context, @AttrRes int attr) {
87         final TypedArray a;
88         synchronized (TEMP_ATTR) {
89             TEMP_ATTR[0] = attr;
90             a = context.obtainStyledAttributes(TEMP_ATTR);
91         }
92 
93         try {
94             return a.getDrawable(0);
95         } finally {
96             a.recycle();
97         }
98     }
99 }
100 
101