2017-02-10 16:52:16 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher3.util;
|
|
|
|
|
|
2021-04-26 09:52:47 -07:00
|
|
|
import static android.app.WallpaperColors.HINT_SUPPORTS_DARK_TEXT;
|
|
|
|
|
import static android.app.WallpaperColors.HINT_SUPPORTS_DARK_THEME;
|
|
|
|
|
|
2021-04-30 14:18:08 -07:00
|
|
|
import android.app.WallpaperColors;
|
2021-04-26 09:52:47 -07:00
|
|
|
import android.app.WallpaperManager;
|
2017-02-10 16:52:16 -08:00
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.res.TypedArray;
|
2017-02-27 13:30:20 -08:00
|
|
|
import android.graphics.Color;
|
|
|
|
|
import android.graphics.ColorMatrix;
|
2017-05-31 14:48:19 -07:00
|
|
|
import android.graphics.drawable.Drawable;
|
2019-01-03 16:47:18 -08:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
|
import android.util.SparseArray;
|
|
|
|
|
import android.util.TypedValue;
|
2017-02-10 16:52:16 -08:00
|
|
|
|
2019-04-04 17:05:26 -07:00
|
|
|
import com.android.launcher3.R;
|
2019-05-01 13:15:29 -07:00
|
|
|
import com.android.launcher3.Utilities;
|
2021-05-17 13:23:08 -07:00
|
|
|
import com.android.launcher3.config.FeatureFlags;
|
2021-03-24 15:21:39 -07:00
|
|
|
import com.android.launcher3.icons.GraphicsUtils;
|
2019-04-04 17:05:26 -07:00
|
|
|
|
2017-02-10 16:52:16 -08:00
|
|
|
/**
|
|
|
|
|
* Various utility methods associated with theming.
|
|
|
|
|
*/
|
2021-04-26 09:52:47 -07:00
|
|
|
@SuppressWarnings("NewApi")
|
2017-02-10 16:52:16 -08:00
|
|
|
public class Themes {
|
|
|
|
|
|
2021-05-17 13:23:08 -07:00
|
|
|
public static final String KEY_THEMED_ICONS = "themed_icons";
|
|
|
|
|
|
2019-05-01 13:15:29 -07:00
|
|
|
public static int getActivityThemeRes(Context context) {
|
2021-04-30 14:18:08 -07:00
|
|
|
final int colorHints;
|
|
|
|
|
if (Utilities.ATLEAST_P) {
|
|
|
|
|
WallpaperColors colors = context.getSystemService(WallpaperManager.class)
|
|
|
|
|
.getWallpaperColors(WallpaperManager.FLAG_SYSTEM);
|
|
|
|
|
colorHints = colors == null ? 0 : colors.getColorHints();
|
|
|
|
|
} else {
|
|
|
|
|
colorHints = 0;
|
|
|
|
|
}
|
2021-04-26 09:52:47 -07:00
|
|
|
return getActivityThemeRes(context, colorHints);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int getActivityThemeRes(Context context, int wallpaperColorHints) {
|
|
|
|
|
boolean supportsDarkText = Utilities.ATLEAST_S
|
|
|
|
|
&& (wallpaperColorHints & HINT_SUPPORTS_DARK_TEXT) != 0;
|
|
|
|
|
boolean isMainColorDark = Utilities.ATLEAST_S
|
|
|
|
|
&& (wallpaperColorHints & HINT_SUPPORTS_DARK_THEME) != 0;
|
2019-05-01 13:15:29 -07:00
|
|
|
|
2021-05-03 18:44:13 +00:00
|
|
|
if (Utilities.isDarkTheme(context)) {
|
2021-04-26 09:52:47 -07:00
|
|
|
return supportsDarkText ? R.style.AppTheme_Dark_DarkText
|
|
|
|
|
: isMainColorDark ? R.style.AppTheme_Dark_DarkMainColor : R.style.AppTheme_Dark;
|
2019-05-01 13:15:29 -07:00
|
|
|
} else {
|
2021-04-26 09:52:47 -07:00
|
|
|
return supportsDarkText ? R.style.AppTheme_DarkText
|
|
|
|
|
: isMainColorDark ? R.style.AppTheme_DarkMainColor : R.style.AppTheme;
|
2019-05-01 13:15:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 13:23:08 -07:00
|
|
|
/**
|
|
|
|
|
* Returns true if workspace icon theming is enabled
|
|
|
|
|
*/
|
|
|
|
|
public static boolean isThemedIconEnabled(Context context) {
|
|
|
|
|
return FeatureFlags.ENABLE_THEMED_ICONS.get()
|
|
|
|
|
&& Utilities.getPrefs(context).getBoolean(KEY_THEMED_ICONS, false);
|
|
|
|
|
}
|
2021-04-26 09:52:47 -07:00
|
|
|
|
2019-04-11 16:58:50 -07:00
|
|
|
public static String getDefaultBodyFont(Context context) {
|
|
|
|
|
TypedArray ta = context.obtainStyledAttributes(android.R.style.TextAppearance_DeviceDefault,
|
|
|
|
|
new int[]{android.R.attr.fontFamily});
|
|
|
|
|
String value = ta.getString(0);
|
|
|
|
|
ta.recycle();
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-05 16:22:55 -07:00
|
|
|
public static float getDialogCornerRadius(Context context) {
|
|
|
|
|
return getDimension(context, android.R.attr.dialogCornerRadius,
|
|
|
|
|
context.getResources().getDimension(R.dimen.default_dialog_corner_radius));
|
2019-04-04 17:05:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static float getDimension(Context context, int attr, float defaultValue) {
|
|
|
|
|
TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
|
|
|
|
|
float value = ta.getDimension(0, defaultValue);
|
|
|
|
|
ta.recycle();
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 16:52:16 -08:00
|
|
|
public static int getColorAccent(Context context) {
|
|
|
|
|
return getAttrColor(context, android.R.attr.colorAccent);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-06 22:54:40 -07:00
|
|
|
/** Returns the background color attribute. */
|
2021-02-11 10:52:06 -08:00
|
|
|
public static int getColorBackground(Context context) {
|
|
|
|
|
return getAttrColor(context, android.R.attr.colorBackground);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-06 11:02:39 -05:00
|
|
|
/** Returns the floating background color attribute. */
|
|
|
|
|
public static int getColorBackgroundFloating(Context context) {
|
|
|
|
|
return getAttrColor(context, android.R.attr.colorBackgroundFloating);
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 16:52:16 -08:00
|
|
|
public static int getAttrColor(Context context, int attr) {
|
2021-03-24 15:21:39 -07:00
|
|
|
return GraphicsUtils.getAttrColor(context, attr);
|
2017-02-10 16:52:16 -08:00
|
|
|
}
|
|
|
|
|
|
2017-05-24 13:19:15 -07:00
|
|
|
public static boolean getAttrBoolean(Context context, int attr) {
|
|
|
|
|
TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
|
|
|
|
|
boolean value = ta.getBoolean(0, false);
|
|
|
|
|
ta.recycle();
|
2017-05-31 14:48:19 -07:00
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Drawable getAttrDrawable(Context context, int attr) {
|
|
|
|
|
TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
|
|
|
|
|
Drawable value = ta.getDrawable(0);
|
|
|
|
|
ta.recycle();
|
2017-05-24 13:19:15 -07:00
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-30 12:00:43 -07:00
|
|
|
public static int getAttrInteger(Context context, int attr) {
|
|
|
|
|
TypedArray ta = context.obtainStyledAttributes(new int[]{attr});
|
|
|
|
|
int value = ta.getInteger(0, 0);
|
|
|
|
|
ta.recycle();
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 13:30:20 -08:00
|
|
|
/**
|
|
|
|
|
* Scales a color matrix such that, when applied to color R G B A, it produces R' G' B' A' where
|
|
|
|
|
* R' = r * R
|
|
|
|
|
* G' = g * G
|
|
|
|
|
* B' = b * B
|
|
|
|
|
* A' = a * A
|
|
|
|
|
*
|
|
|
|
|
* The matrix will, for instance, turn white into r g b a, and black will remain black.
|
|
|
|
|
*
|
|
|
|
|
* @param color The color r g b a
|
|
|
|
|
* @param target The ColorMatrix to scale
|
|
|
|
|
*/
|
|
|
|
|
public static void setColorScaleOnMatrix(int color, ColorMatrix target) {
|
|
|
|
|
target.setScale(Color.red(color) / 255f, Color.green(color) / 255f,
|
|
|
|
|
Color.blue(color) / 255f, Color.alpha(color) / 255f);
|
|
|
|
|
}
|
2018-04-30 11:15:39 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Changes a color matrix such that, when applied to srcColor, it produces dstColor.
|
|
|
|
|
*
|
|
|
|
|
* Note that values on the last column of target ColorMatrix can be negative, and may result in
|
|
|
|
|
* negative values when applied on a color. Such negative values will be automatically shifted
|
|
|
|
|
* up to 0 by the framework.
|
|
|
|
|
*
|
|
|
|
|
* @param srcColor The color to start from
|
|
|
|
|
* @param dstColor The color to create by applying target on srcColor
|
|
|
|
|
* @param target The ColorMatrix to transform the color
|
|
|
|
|
*/
|
|
|
|
|
public static void setColorChangeOnMatrix(int srcColor, int dstColor, ColorMatrix target) {
|
|
|
|
|
target.reset();
|
|
|
|
|
target.getArray()[4] = Color.red(dstColor) - Color.red(srcColor);
|
|
|
|
|
target.getArray()[9] = Color.green(dstColor) - Color.green(srcColor);
|
|
|
|
|
target.getArray()[14] = Color.blue(dstColor) - Color.blue(srcColor);
|
|
|
|
|
target.getArray()[19] = Color.alpha(dstColor) - Color.alpha(srcColor);
|
|
|
|
|
}
|
2019-01-03 16:47:18 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a map for attribute-name to value for all the values in {@param attrs} which can be
|
|
|
|
|
* held in memory for later use.
|
|
|
|
|
*/
|
2019-01-07 15:13:39 -08:00
|
|
|
public static SparseArray<TypedValue> createValueMap(Context context, AttributeSet attrSet,
|
|
|
|
|
IntArray keysToIgnore) {
|
2019-01-03 16:47:18 -08:00
|
|
|
int count = attrSet.getAttributeCount();
|
2019-01-07 15:13:39 -08:00
|
|
|
IntArray attrNameArray = new IntArray(count);
|
2019-01-03 16:47:18 -08:00
|
|
|
for (int i = 0; i < count; i++) {
|
2019-01-07 15:13:39 -08:00
|
|
|
attrNameArray.add(attrSet.getAttributeNameResource(i));
|
2019-01-03 16:47:18 -08:00
|
|
|
}
|
2019-01-07 15:13:39 -08:00
|
|
|
attrNameArray.removeAllValues(keysToIgnore);
|
2019-01-03 16:47:18 -08:00
|
|
|
|
2019-01-07 15:13:39 -08:00
|
|
|
int[] attrNames = attrNameArray.toArray();
|
|
|
|
|
SparseArray<TypedValue> result = new SparseArray<>(attrNames.length);
|
2019-01-03 16:47:18 -08:00
|
|
|
TypedArray ta = context.obtainStyledAttributes(attrSet, attrNames);
|
2019-01-07 15:13:39 -08:00
|
|
|
for (int i = 0; i < attrNames.length; i++) {
|
2019-01-03 16:47:18 -08:00
|
|
|
TypedValue tv = new TypedValue();
|
|
|
|
|
ta.getValue(i, tv);
|
|
|
|
|
result.put(attrNames[i], tv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2017-02-10 16:52:16 -08:00
|
|
|
}
|