Files
termux-app/termux-shared/src/main/java/com/termux/shared/theme/ThemeUtils.java
agnostic-apollo 6631599fb6 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.
2022-01-23 01:42:26 +05:00

87 lines
3.4 KiB
Java

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
*/
public static boolean isNightModeEnabled(Context context) {
if (context == null) return false;
return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}
/** Will return true if mode is set to {@link NightMode#TRUE}, otherwise will return true if
* mode is set to {@link NightMode#SYSTEM} and night mode is enabled by system. */
public static boolean shouldEnableDarkTheme(Context context, String name) {
if (NightMode.TRUE.getName().equals(name))
return true;
else if (NightMode.FALSE.getName().equals(name))
return false;
else if (NightMode.SYSTEM.getName().equals(name)) {
return isNightModeEnabled(context);
} else {
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;
}
}