1 /*
2  * Copyright (C) 2017 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.launcher3.util;
18 
19 import android.content.Context;
20 import android.content.res.Configuration;
21 import android.content.res.TypedArray;
22 import android.graphics.Color;
23 import android.graphics.ColorMatrix;
24 import android.graphics.drawable.Drawable;
25 import android.util.AttributeSet;
26 import android.util.SparseArray;
27 import android.util.TypedValue;
28 
29 import com.android.launcher3.R;
30 import com.android.launcher3.Utilities;
31 import com.android.launcher3.uioverrides.WallpaperColorInfo;
32 
33 /**
34  * Various utility methods associated with theming.
35  */
36 public class Themes {
37 
getActivityThemeRes(Context context)38     public static int getActivityThemeRes(Context context) {
39         WallpaperColorInfo wallpaperColorInfo = WallpaperColorInfo.INSTANCE.get(context);
40         boolean darkTheme;
41         if (Utilities.ATLEAST_Q) {
42             Configuration configuration = context.getResources().getConfiguration();
43             int nightMode = configuration.uiMode & Configuration.UI_MODE_NIGHT_MASK;
44             darkTheme = nightMode == Configuration.UI_MODE_NIGHT_YES;
45         } else {
46             darkTheme = wallpaperColorInfo.isDark();
47         }
48 
49         if (darkTheme) {
50             return wallpaperColorInfo.supportsDarkText() ?
51                     R.style.AppTheme_Dark_DarkText : wallpaperColorInfo.isMainColorDark() ?
52                             R.style.AppTheme_Dark_DarkMainColor : R.style.AppTheme_Dark;
53         } else {
54             return wallpaperColorInfo.supportsDarkText() ?
55                     R.style.AppTheme_DarkText : wallpaperColorInfo.isMainColorDark() ?
56                             R.style.AppTheme_DarkMainColor : R.style.AppTheme;
57         }
58     }
59 
getDefaultBodyFont(Context context)60     public static String getDefaultBodyFont(Context context) {
61         TypedArray ta = context.obtainStyledAttributes(android.R.style.TextAppearance_DeviceDefault,
62                 new int[]{android.R.attr.fontFamily});
63         String value = ta.getString(0);
64         ta.recycle();
65         return value;
66     }
67 
getDialogCornerRadius(Context context)68     public static float getDialogCornerRadius(Context context) {
69         return getDimension(context, android.R.attr.dialogCornerRadius,
70                 context.getResources().getDimension(R.dimen.default_dialog_corner_radius));
71     }
72 
getDimension(Context context, int attr, float defaultValue)73     public static float getDimension(Context context, int attr, float defaultValue) {
74         TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
75         float value = ta.getDimension(0, defaultValue);
76         ta.recycle();
77         return value;
78     }
79 
getColorAccent(Context context)80     public static int getColorAccent(Context context) {
81         return getAttrColor(context, android.R.attr.colorAccent);
82     }
83 
getAttrColor(Context context, int attr)84     public static int getAttrColor(Context context, int attr) {
85         TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
86         int colorAccent = ta.getColor(0, 0);
87         ta.recycle();
88         return colorAccent;
89     }
90 
getAttrBoolean(Context context, int attr)91     public static boolean getAttrBoolean(Context context, int attr) {
92         TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
93         boolean value = ta.getBoolean(0, false);
94         ta.recycle();
95         return value;
96     }
97 
getAttrDrawable(Context context, int attr)98     public static Drawable getAttrDrawable(Context context, int attr) {
99         TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
100         Drawable value = ta.getDrawable(0);
101         ta.recycle();
102         return value;
103     }
104 
getAttrInteger(Context context, int attr)105     public static int getAttrInteger(Context context, int attr) {
106         TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
107         int value = ta.getInteger(0, 0);
108         ta.recycle();
109         return value;
110     }
111 
112     /**
113      * Returns the alpha corresponding to the theme attribute {@param attr}, in the range [0, 255].
114      */
getAlpha(Context context, int attr)115     public static int getAlpha(Context context, int attr) {
116         return (int) (255 * getFloat(context, attr, 0) + 0.5f);
117     }
118 
119     /**
120      * Returns the alpha corresponding to the theme attribute {@param attr}
121      */
getFloat(Context context, int attr, float defValue)122     public static float getFloat(Context context, int attr, float defValue) {
123         TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
124         float value = ta.getFloat(0, defValue);
125         ta.recycle();
126         return value;
127     }
128 
129     /**
130      * Scales a color matrix such that, when applied to color R G B A, it produces R' G' B' A' where
131      * R' = r * R
132      * G' = g * G
133      * B' = b * B
134      * A' = a * A
135      *
136      * The matrix will, for instance, turn white into r g b a, and black will remain black.
137      *
138      * @param color The color r g b a
139      * @param target The ColorMatrix to scale
140      */
setColorScaleOnMatrix(int color, ColorMatrix target)141     public static void setColorScaleOnMatrix(int color, ColorMatrix target) {
142         target.setScale(Color.red(color) / 255f, Color.green(color) / 255f,
143                 Color.blue(color) / 255f, Color.alpha(color) / 255f);
144     }
145 
146     /**
147      * Changes a color matrix such that, when applied to srcColor, it produces dstColor.
148      *
149      * Note that values on the last column of target ColorMatrix can be negative, and may result in
150      * negative values when applied on a color. Such negative values will be automatically shifted
151      * up to 0 by the framework.
152      *
153      * @param srcColor The color to start from
154      * @param dstColor The color to create by applying target on srcColor
155      * @param target The ColorMatrix to transform the color
156      */
setColorChangeOnMatrix(int srcColor, int dstColor, ColorMatrix target)157     public static void setColorChangeOnMatrix(int srcColor, int dstColor, ColorMatrix target) {
158         target.reset();
159         target.getArray()[4] = Color.red(dstColor) - Color.red(srcColor);
160         target.getArray()[9] = Color.green(dstColor) - Color.green(srcColor);
161         target.getArray()[14] = Color.blue(dstColor) - Color.blue(srcColor);
162         target.getArray()[19] = Color.alpha(dstColor) - Color.alpha(srcColor);
163     }
164 
165     /**
166      * Creates a map for attribute-name to value for all the values in {@param attrs} which can be
167      * held in memory for later use.
168      */
createValueMap(Context context, AttributeSet attrSet, IntArray keysToIgnore)169     public static SparseArray<TypedValue> createValueMap(Context context, AttributeSet attrSet,
170             IntArray keysToIgnore) {
171         int count = attrSet.getAttributeCount();
172         IntArray attrNameArray = new IntArray(count);
173         for (int i = 0; i < count; i++) {
174             attrNameArray.add(attrSet.getAttributeNameResource(i));
175         }
176         attrNameArray.removeAllValues(keysToIgnore);
177 
178         int[] attrNames = attrNameArray.toArray();
179         SparseArray<TypedValue> result = new SparseArray<>(attrNames.length);
180         TypedArray ta = context.obtainStyledAttributes(attrSet, attrNames);
181         for (int i = 0; i < attrNames.length; i++) {
182             TypedValue tv = new TypedValue();
183             ta.getValue(i, tv);
184             result.put(attrNames[i], tv);
185         }
186 
187         return result;
188     }
189 }
190