termux-app
120 строк · 4.6 Кб
1package com.termux.shared.activity.media;
2
3import androidx.annotation.IdRes;
4import androidx.annotation.NonNull;
5import androidx.annotation.StyleRes;
6import androidx.appcompat.app.ActionBar;
7import androidx.appcompat.app.AppCompatActivity;
8import androidx.appcompat.app.AppCompatDelegate;
9import androidx.appcompat.widget.Toolbar;
10
11import com.termux.shared.logger.Logger;
12import com.termux.shared.theme.NightMode;
13
14public class AppCompatActivityUtils {
15
16private 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*/
26public static void setNightMode(AppCompatActivity activity, String name, boolean local) {
27if (name == null) return;
28NightMode nightMode = NightMode.modeOf(name);
29if (nightMode != null) {
30if (local) {
31if (activity != null) {
32activity.getDelegate().setLocalNightMode(nightMode.getMode());
33}
34} else {
35AppCompatDelegate.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*/
46public static void setToolbar(@NonNull AppCompatActivity activity, @IdRes int id) {
47Toolbar toolbar = activity.findViewById(id);
48if (toolbar != null)
49activity.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*/
59public static void setToolbarTitle(@NonNull AppCompatActivity activity, @IdRes int id,
60String title, @StyleRes int titleAppearance) {
61Toolbar toolbar = activity.findViewById(id);
62if (toolbar != null) {
63//toolbar.setTitle(title); // Does not work
64final ActionBar actionBar = activity.getSupportActionBar();
65if (actionBar != null)
66actionBar.setTitle(title);
67
68try {
69if (titleAppearance != 0)
70toolbar.setTitleTextAppearance(activity, titleAppearance);
71} catch (Exception e) {
72Logger.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*/
86public static void setToolbarSubtitle(@NonNull AppCompatActivity activity, @IdRes int id,
87String subtitle, @StyleRes int subtitleAppearance) {
88Toolbar toolbar = activity.findViewById(id);
89if (toolbar != null) {
90toolbar.setSubtitle(subtitle);
91try {
92if (subtitleAppearance != 0)
93toolbar.setSubtitleTextAppearance(activity, subtitleAppearance);
94} catch (Exception e) {
95Logger.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*/
106public static void setShowBackButtonInActionBar(@NonNull AppCompatActivity activity,
107boolean showBackButtonInActionBar) {
108final ActionBar actionBar = activity.getSupportActionBar();
109if (actionBar != null) {
110if (showBackButtonInActionBar) {
111actionBar.setDisplayHomeAsUpEnabled(true);
112actionBar.setDisplayShowHomeEnabled(true);
113} else {
114actionBar.setDisplayHomeAsUpEnabled(false);
115actionBar.setDisplayShowHomeEnabled(false);
116}
117}
118}
119
120}
121