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