|
|
|
@ -4,6 +4,7 @@ import android.content.Context;
|
|
|
|
|
import android.content.res.Resources;
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
import androidx.annotation.AttrRes;
|
|
|
|
|
import androidx.annotation.ColorInt;
|
|
|
|
|
import androidx.annotation.DrawableRes;
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.annotation.StyleRes;
|
|
|
|
@ -19,25 +20,41 @@ public class ThemeUtil {
|
|
|
|
|
private static final String TAG = ThemeUtil.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
public static boolean isDarkTheme(@NonNull Context context) {
|
|
|
|
|
return getAttribute(context, R.attr.theme_type, "light").equals("dark");
|
|
|
|
|
return getAttributeText(context, R.attr.theme_type, "light").equals("dark");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ColorInt
|
|
|
|
|
public static int getThemedColor(@NonNull Context context, @AttrRes int attr) {
|
|
|
|
|
TypedValue typedValue = new TypedValue();
|
|
|
|
|
Resources.Theme theme = context.getTheme();
|
|
|
|
|
|
|
|
|
|
if (theme.resolveAttribute(attr, typedValue, true)) {
|
|
|
|
|
return typedValue.data;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Couldn't find a color attribute with id: " + attr);
|
|
|
|
|
return Color.RED;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DrawableRes
|
|
|
|
|
public static int getThemedDrawableResId(@NonNull Context context, @AttrRes int attr) {
|
|
|
|
|
TypedValue typedValue = new TypedValue();
|
|
|
|
|
Resources.Theme theme = context.getTheme();
|
|
|
|
|
|
|
|
|
|
if (theme.resolveAttribute(attr, typedValue, true)) {
|
|
|
|
|
return typedValue.resourceId;
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Couldn't find a drawable attribute with id: " + attr);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static LayoutInflater getThemedInflater(@NonNull Context context, @NonNull LayoutInflater inflater, @StyleRes int theme) {
|
|
|
|
|
Context contextThemeWrapper = new ContextThemeWrapper(context, theme);
|
|
|
|
|
return inflater.cloneInContext(contextThemeWrapper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String getAttribute(Context context, int attribute, String defaultValue) {
|
|
|
|
|
private static String getAttributeText(Context context, int attribute, String defaultValue) {
|
|
|
|
|
TypedValue outValue = new TypedValue();
|
|
|
|
|
|
|
|
|
|
if (context.getTheme().resolveAttribute(attribute, outValue, true)) {
|
|
|
|
@ -49,15 +66,4 @@ public class ThemeUtil {
|
|
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DrawableRes
|
|
|
|
|
public static int getDrawableResWithAttribute(Context context, @AttrRes int attributeId) {
|
|
|
|
|
TypedValue resolvedValue = new TypedValue();
|
|
|
|
|
context.getTheme().resolveAttribute(attributeId, resolvedValue, true);
|
|
|
|
|
if (resolvedValue.type != TypedValue.TYPE_STRING) {
|
|
|
|
|
Log.e(TAG, "Cannot resolve a drawable resource from an attribute ID: " + attributeId);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return resolvedValue.resourceId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|