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.getInstance(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         TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
117         float alpha = ta.getFloat(0, 0);
118         ta.recycle();
119         return (int) (255 * alpha + 0.5f);
120     }
121 
122     /**
123      * Scales a color matrix such that, when applied to color R G B A, it produces R' G' B' A' where
124      * R' = r * R
125      * G' = g * G
126      * B' = b * B
127      * A' = a * A
128      *
129      * The matrix will, for instance, turn white into r g b a, and black will remain black.
130      *
131      * @param color The color r g b a
132      * @param target The ColorMatrix to scale
133      */
setColorScaleOnMatrix(int color, ColorMatrix target)134     public static void setColorScaleOnMatrix(int color, ColorMatrix target) {
135         target.setScale(Color.red(color) / 255f, Color.green(color) / 255f,
136                 Color.blue(color) / 255f, Color.alpha(color) / 255f);
137     }
138 
139     /**
140      * Changes a color matrix such that, when applied to srcColor, it produces dstColor.
141      *
142      * Note that values on the last column of target ColorMatrix can be negative, and may result in
143      * negative values when applied on a color. Such negative values will be automatically shifted
144      * up to 0 by the framework.
145      *
146      * @param srcColor The color to start from
147      * @param dstColor The color to create by applying target on srcColor
148      * @param target The ColorMatrix to transform the color
149      */
setColorChangeOnMatrix(int srcColor, int dstColor, ColorMatrix target)150     public static void setColorChangeOnMatrix(int srcColor, int dstColor, ColorMatrix target) {
151         target.reset();
152         target.getArray()[4] = Color.red(dstColor) - Color.red(srcColor);
153         target.getArray()[9] = Color.green(dstColor) - Color.green(srcColor);
154         target.getArray()[14] = Color.blue(dstColor) - Color.blue(srcColor);
155         target.getArray()[19] = Color.alpha(dstColor) - Color.alpha(srcColor);
156     }
157 
158     /**
159      * Creates a map for attribute-name to value for all the values in {@param attrs} which can be
160      * held in memory for later use.
161      */
createValueMap(Context context, AttributeSet attrSet, IntArray keysToIgnore)162     public static SparseArray<TypedValue> createValueMap(Context context, AttributeSet attrSet,
163             IntArray keysToIgnore) {
164         int count = attrSet.getAttributeCount();
165         IntArray attrNameArray = new IntArray(count);
166         for (int i = 0; i < count; i++) {
167             attrNameArray.add(attrSet.getAttributeNameResource(i));
168         }
169         attrNameArray.removeAllValues(keysToIgnore);
170 
171         int[] attrNames = attrNameArray.toArray();
172         SparseArray<TypedValue> result = new SparseArray<>(attrNames.length);
173         TypedArray ta = context.obtainStyledAttributes(attrSet, attrNames);
174         for (int i = 0; i < attrNames.length; i++) {
175             TypedValue tv = new TypedValue();
176             ta.getValue(i, tv);
177             result.put(attrNames[i], tv);
178         }
179 
180         return result;
181     }
182 }
183