Added: Add support for shared day/night theming across termux apps

With this commit, activities will automatically change theme between day/night if `night-mode` `termux.properties` is not set or is set to `system` without requiring app restart.

Dialog theming will be fully added in a later commit and may currently be in an inconsistent state or have crashes.

The `uiMode` has been removed from `configChanges` of `TermuxActivity`, this may cause termux app to restart if samsung DEX mode is changed, if it does, then users should report it so that it can be fixed by re-adding the value and ignoring the change inside `TermuxActivity.onConfigurationChanged()`. The docs don't state if its necessary. Check related pull request #1446.

Running `termux-reload-settings` will also restart `TermuxActivity`, the activity data should be preserved.
This commit is contained in:
agnostic-apollo
2022-01-23 00:18:33 +05:00
parent f3f434af92
commit 6631599fb6
30 changed files with 586 additions and 140 deletions

View File

@@ -0,0 +1,120 @@
package com.termux.shared.activity.media;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;
import com.termux.shared.logger.Logger;
import com.termux.shared.theme.NightMode;
public class AppCompatActivityUtils {
private static final String LOG_TAG = "AppCompatActivityUtils";
/** Set activity night mode.
*
* @param activity The host {@link AppCompatActivity}.
* @param name The {@link String} representing the name for a {@link NightMode}.
* @param local If set to {@code true}, then a call to {@link AppCompatDelegate#setLocalNightMode(int)}
* will be made, otherwise to {@link AppCompatDelegate#setDefaultNightMode(int)}.
*/
public static void setNightMode(AppCompatActivity activity, String name, boolean local) {
if (name == null) return;
NightMode nightMode = NightMode.modeOf(name);
if (nightMode != null) {
if (local) {
if (activity != null) {
activity.getDelegate().setLocalNightMode(nightMode.getMode());
}
} else {
AppCompatDelegate.setDefaultNightMode(nightMode.getMode());
}
}
}
/** Set activity toolbar.
*
* @param activity The host {@link AppCompatActivity}.
* @param id The toolbar resource id.
*/
public static void setToolbar(@NonNull AppCompatActivity activity, @IdRes int id) {
Toolbar toolbar = activity.findViewById(id);
if (toolbar != null)
activity.setSupportActionBar(toolbar);
}
/** Set activity toolbar title.
*
* @param activity The host {@link AppCompatActivity}.
* @param id The toolbar resource id.
* @param title The toolbar title {@link String}.
* @param titleAppearance The toolbar title TextAppearance resource id.
*/
public static void setToolbarTitle(@NonNull AppCompatActivity activity, @IdRes int id,
String title, @StyleRes int titleAppearance) {
Toolbar toolbar = activity.findViewById(id);
if (toolbar != null) {
//toolbar.setTitle(title); // Does not work
final ActionBar actionBar = activity.getSupportActionBar();
if (actionBar != null)
actionBar.setTitle(title);
try {
if (titleAppearance != 0)
toolbar.setTitleTextAppearance(activity, titleAppearance);
} catch (Exception e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to set toolbar title appearance to style resource id " + titleAppearance, e);
}
}
}
/** Set activity toolbar subtitle.
*
* @param activity The host {@link AppCompatActivity}.
* @param id The toolbar resource id.
* @param subtitle The toolbar subtitle {@link String}.
* @param subtitleAppearance The toolbar subtitle TextAppearance resource id.
*/
public static void setToolbarSubtitle(@NonNull AppCompatActivity activity, @IdRes int id,
String subtitle, @StyleRes int subtitleAppearance) {
Toolbar toolbar = activity.findViewById(id);
if (toolbar != null) {
toolbar.setSubtitle(subtitle);
try {
if (subtitleAppearance != 0)
toolbar.setSubtitleTextAppearance(activity, subtitleAppearance);
} catch (Exception e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Failed to set toolbar subtitle appearance to style resource id " + subtitleAppearance, e);
}
}
}
/** Set whether back button should be shown in activity toolbar.
*
* @param activity The host {@link AppCompatActivity}.
* @param showBackButtonInActionBar Set to {@code true} to enable and {@code false} to disable.
*/
public static void setShowBackButtonInActionBar(@NonNull AppCompatActivity activity,
boolean showBackButtonInActionBar) {
final ActionBar actionBar = activity.getSupportActionBar();
if (actionBar != null) {
if (showBackButtonInActionBar) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
} else {
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
}
}
}
}