termux-app

Форк
0
120 строк · 4.6 Кб
1
package com.termux.shared.activity.media;
2

3
import androidx.annotation.IdRes;
4
import androidx.annotation.NonNull;
5
import androidx.annotation.StyleRes;
6
import androidx.appcompat.app.ActionBar;
7
import androidx.appcompat.app.AppCompatActivity;
8
import androidx.appcompat.app.AppCompatDelegate;
9
import androidx.appcompat.widget.Toolbar;
10

11
import com.termux.shared.logger.Logger;
12
import com.termux.shared.theme.NightMode;
13

14
public class AppCompatActivityUtils {
15

16
    private static final String LOG_TAG = "AppCompatActivityUtils";
17

18

19
    /** Set activity night mode.
20
     *
21
     * @param activity The host {@link AppCompatActivity}.
22
     * @param name The {@link String} representing the name for a {@link NightMode}.
23
     * @param local If set to {@code true}, then a call to {@link AppCompatDelegate#setLocalNightMode(int)}
24
     *              will be made, otherwise to {@link AppCompatDelegate#setDefaultNightMode(int)}.
25
     */
26
    public static void setNightMode(AppCompatActivity activity, String name, boolean local) {
27
        if (name == null) return;
28
        NightMode nightMode = NightMode.modeOf(name);
29
        if (nightMode != null) {
30
            if (local) {
31
                if (activity != null) {
32
                    activity.getDelegate().setLocalNightMode(nightMode.getMode());
33
                }
34
            } else {
35
                AppCompatDelegate.setDefaultNightMode(nightMode.getMode());
36
            }
37
        }
38

39
    }
40

41
    /** Set activity toolbar.
42
     *
43
     * @param activity The host {@link AppCompatActivity}.
44
     * @param id The toolbar resource id.
45
     */
46
    public static void setToolbar(@NonNull AppCompatActivity activity, @IdRes int id) {
47
        Toolbar toolbar = activity.findViewById(id);
48
        if (toolbar != null)
49
            activity.setSupportActionBar(toolbar);
50
    }
51

52
    /** Set activity toolbar title.
53
     *
54
     * @param activity The host {@link AppCompatActivity}.
55
     * @param id The toolbar resource id.
56
     * @param title The toolbar title {@link String}.
57
     * @param titleAppearance The toolbar title TextAppearance resource id.
58
     */
59
    public static void setToolbarTitle(@NonNull AppCompatActivity activity, @IdRes int id,
60
                                       String title, @StyleRes int titleAppearance) {
61
        Toolbar toolbar = activity.findViewById(id);
62
        if (toolbar != null) {
63
            //toolbar.setTitle(title); // Does not work
64
            final ActionBar actionBar = activity.getSupportActionBar();
65
            if (actionBar != null)
66
                actionBar.setTitle(title);
67

68
            try {
69
                if (titleAppearance != 0)
70
                    toolbar.setTitleTextAppearance(activity, titleAppearance);
71
            } catch (Exception e) {
72
                Logger.logStackTraceWithMessage(LOG_TAG, "Failed to set toolbar title appearance to style resource id " + titleAppearance, e);
73
            }
74

75

76
        }
77
    }
78

79
    /** Set activity toolbar subtitle.
80
     *
81
     * @param activity The host {@link AppCompatActivity}.
82
     * @param id The toolbar resource id.
83
     * @param subtitle The toolbar subtitle {@link String}.
84
     * @param subtitleAppearance The toolbar subtitle TextAppearance resource id.
85
     */
86
    public static void setToolbarSubtitle(@NonNull AppCompatActivity activity, @IdRes int id,
87
                                          String subtitle, @StyleRes int subtitleAppearance) {
88
        Toolbar toolbar = activity.findViewById(id);
89
        if (toolbar != null) {
90
            toolbar.setSubtitle(subtitle);
91
            try {
92
                if (subtitleAppearance != 0)
93
                    toolbar.setSubtitleTextAppearance(activity, subtitleAppearance);
94
            } catch (Exception e) {
95
                Logger.logStackTraceWithMessage(LOG_TAG, "Failed to set toolbar subtitle appearance to style resource id " + subtitleAppearance, e);
96
            }
97
        }
98
    }
99

100

101
    /** Set whether back button should be shown in activity toolbar.
102
     *
103
     * @param activity The host {@link AppCompatActivity}.
104
     * @param showBackButtonInActionBar Set to {@code true} to enable and {@code false} to disable.
105
     */
106
    public static void setShowBackButtonInActionBar(@NonNull AppCompatActivity activity,
107
                                                    boolean showBackButtonInActionBar) {
108
        final ActionBar actionBar = activity.getSupportActionBar();
109
        if (actionBar != null) {
110
            if (showBackButtonInActionBar) {
111
                actionBar.setDisplayHomeAsUpEnabled(true);
112
                actionBar.setDisplayShowHomeEnabled(true);
113
            } else {
114
                actionBar.setDisplayHomeAsUpEnabled(false);
115
                actionBar.setDisplayShowHomeEnabled(false);
116
            }
117
        }
118
    }
119

120
}
121

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

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

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

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