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