termux-app
86 строк · 3.4 Кб
1package com.termux.shared.theme;
2
3import android.app.Activity;
4import android.content.Context;
5import android.content.res.Configuration;
6import android.content.res.TypedArray;
7
8import androidx.appcompat.app.AppCompatActivity;
9
10public class ThemeUtils {
11
12public static final int ATTR_TEXT_COLOR_PRIMARY = android.R.attr.textColorPrimary;
13public static final int ATTR_TEXT_COLOR_SECONDARY = android.R.attr.textColorSecondary;
14public static final int ATTR_TEXT_COLOR = android.R.attr.textColor;
15public static final int ATTR_TEXT_COLOR_LINK = android.R.attr.textColorLink;
16
17/**
18* Will return true if system has enabled night mode.
19* https://developer.android.com/guide/topics/resources/providing-resources#NightQualifier
20*/
21public static boolean isNightModeEnabled(Context context) {
22if (context == null) return false;
23return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
24
25}
26
27/** Will return true if mode is set to {@link NightMode#TRUE}, otherwise will return true if
28* mode is set to {@link NightMode#SYSTEM} and night mode is enabled by system. */
29public static boolean shouldEnableDarkTheme(Context context, String name) {
30if (NightMode.TRUE.getName().equals(name))
31return true;
32else if (NightMode.FALSE.getName().equals(name))
33return false;
34else if (NightMode.SYSTEM.getName().equals(name)) {
35return isNightModeEnabled(context);
36} else {
37return false;
38}
39}
40
41
42/** Get {@link #ATTR_TEXT_COLOR_PRIMARY} value being used by current theme. */
43public static int getTextColorPrimary(Context context) {
44return getSystemAttrColor(context, ATTR_TEXT_COLOR_PRIMARY);
45}
46
47/** Get {@link #ATTR_TEXT_COLOR_SECONDARY} value being used by current theme. */
48public static int getTextColorSecondary(Context context) {
49return getSystemAttrColor(context, ATTR_TEXT_COLOR_SECONDARY);
50}
51
52/** Get {@link #ATTR_TEXT_COLOR} value being used by current theme. */
53public static int getTextColor(Context context) {
54return getSystemAttrColor(context, ATTR_TEXT_COLOR);
55}
56
57/** Get {@link #ATTR_TEXT_COLOR_LINK} value being used by current theme. */
58public static int getTextColorLink(Context context) {
59return getSystemAttrColor(context, ATTR_TEXT_COLOR_LINK);
60}
61
62
63
64/** Wrapper for {@link #getSystemAttrColor(Context, int, int)} with {@code def} value {@code 0}. */
65public static int getSystemAttrColor(Context context, int attr) {
66return getSystemAttrColor(context, attr, 0);
67}
68
69/**
70* Get a values defined by the current heme listed in attrs.
71*
72* @param context The context for operations. It must be an instance of {@link Activity} or
73* {@link AppCompatActivity} or one with which a theme attribute can be got.
74* Do no use application context.
75* @param attr The attr id.
76* @param def The def value to return.
77* @return Returns the {@code attr} value if found, otherwise {@code def}.
78*/
79public static int getSystemAttrColor(Context context, int attr, int def) {
80TypedArray typedArray = context.getTheme().obtainStyledAttributes(new int[] { attr });
81int color = typedArray.getColor(0, def);
82typedArray.recycle();
83return color;
84}
85
86}
87