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

@@ -27,7 +27,7 @@ public enum NightMode {
private static final String LOG_TAG = "NightMode";
private final String name;
private final int mode;
private final @AppCompatDelegate.NightMode int mode;
NightMode(final String name, int mode) {
this.name = name;

View File

@@ -1,10 +1,19 @@
package com.termux.shared.theme;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import androidx.appcompat.app.AppCompatActivity;
public class ThemeUtils {
public static final int ATTR_TEXT_COLOR_PRIMARY = android.R.attr.textColorPrimary;
public static final int ATTR_TEXT_COLOR_SECONDARY = android.R.attr.textColorSecondary;
public static final int ATTR_TEXT_COLOR = android.R.attr.textColor;
public static final int ATTR_TEXT_COLOR_LINK = android.R.attr.textColorLink;
/**
* Will return true if system has enabled night mode.
* https://developer.android.com/guide/topics/resources/providing-resources#NightQualifier
@@ -28,4 +37,50 @@ public class ThemeUtils {
return false;
}
}
/** Get {@link #ATTR_TEXT_COLOR_PRIMARY} value being used by current theme. */
public static int getTextColorPrimary(Context context) {
return getSystemAttrColor(context, ATTR_TEXT_COLOR_PRIMARY);
}
/** Get {@link #ATTR_TEXT_COLOR_SECONDARY} value being used by current theme. */
public static int getTextColorSecondary(Context context) {
return getSystemAttrColor(context, ATTR_TEXT_COLOR_SECONDARY);
}
/** Get {@link #ATTR_TEXT_COLOR} value being used by current theme. */
public static int getTextColor(Context context) {
return getSystemAttrColor(context, ATTR_TEXT_COLOR);
}
/** Get {@link #ATTR_TEXT_COLOR_LINK} value being used by current theme. */
public static int getTextColorLink(Context context) {
return getSystemAttrColor(context, ATTR_TEXT_COLOR_LINK);
}
/** Wrapper for {@link #getSystemAttrColor(Context, int, int)} with {@code def} value {@code 0}. */
public static int getSystemAttrColor(Context context, int attr) {
return getSystemAttrColor(context, attr, 0);
}
/**
* Get a values defined by the current heme listed in attrs.
*
* @param context The context for operations. It must be an instance of {@link Activity} or
* {@link AppCompatActivity} or one with which a theme attribute can be got.
* Do no use application context.
* @param attr The attr id.
* @param def The def value to return.
* @return Returns the {@code attr} value if found, otherwise {@code def}.
*/
public static int getSystemAttrColor(Context context, int attr, int def) {
TypedArray typedArray = context.getTheme().obtainStyledAttributes(new int[] { attr });
int color = typedArray.getColor(0, def);
typedArray.recycle();
return color;
}
}