termux-app

Форк
0
86 строк · 3.4 Кб
1
package com.termux.shared.theme;
2

3
import android.app.Activity;
4
import android.content.Context;
5
import android.content.res.Configuration;
6
import android.content.res.TypedArray;
7

8
import androidx.appcompat.app.AppCompatActivity;
9

10
public class ThemeUtils {
11

12
    public static final int ATTR_TEXT_COLOR_PRIMARY = android.R.attr.textColorPrimary;
13
    public static final int ATTR_TEXT_COLOR_SECONDARY = android.R.attr.textColorSecondary;
14
    public static final int ATTR_TEXT_COLOR = android.R.attr.textColor;
15
    public 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
     */
21
    public static boolean isNightModeEnabled(Context context) {
22
        if (context == null) return false;
23
        return (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. */
29
    public static boolean shouldEnableDarkTheme(Context context, String name) {
30
        if (NightMode.TRUE.getName().equals(name))
31
            return true;
32
        else if (NightMode.FALSE.getName().equals(name))
33
            return false;
34
        else if (NightMode.SYSTEM.getName().equals(name)) {
35
            return isNightModeEnabled(context);
36
        } else {
37
            return false;
38
        }
39
    }
40

41

42
    /** Get {@link #ATTR_TEXT_COLOR_PRIMARY} value being used by current theme. */
43
    public static int getTextColorPrimary(Context context) {
44
        return getSystemAttrColor(context, ATTR_TEXT_COLOR_PRIMARY);
45
    }
46

47
    /** Get {@link #ATTR_TEXT_COLOR_SECONDARY} value being used by current theme. */
48
    public static int getTextColorSecondary(Context context) {
49
        return getSystemAttrColor(context, ATTR_TEXT_COLOR_SECONDARY);
50
    }
51

52
    /** Get {@link #ATTR_TEXT_COLOR} value being used by current theme. */
53
    public static int getTextColor(Context context) {
54
        return getSystemAttrColor(context, ATTR_TEXT_COLOR);
55
    }
56

57
    /** Get {@link #ATTR_TEXT_COLOR_LINK} value being used by current theme. */
58
    public static int getTextColorLink(Context context) {
59
        return getSystemAttrColor(context, ATTR_TEXT_COLOR_LINK);
60
    }
61

62

63

64
    /** Wrapper for {@link #getSystemAttrColor(Context, int, int)} with {@code def} value {@code 0}. */
65
    public static int getSystemAttrColor(Context context, int attr) {
66
        return 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
     */
79
    public static int getSystemAttrColor(Context context, int attr, int def) {
80
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(new int[] { attr });
81
        int color = typedArray.getColor(0, def);
82
        typedArray.recycle();
83
        return color;
84
    }
85

86
}
87

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.