option for navbar to follow theme

fixes #4
This commit is contained in:
Helium314 2023-07-06 16:46:23 +02:00
parent 99075977ff
commit ae1a2ca183
7 changed files with 88 additions and 11 deletions

View file

@ -138,6 +138,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
private static final String SCHEME_PACKAGE = "package";
final Settings mSettings;
private int mOriginalNavBarColor = 0;
private int mOriginalNavBarFlags = 0;
private final DictionaryFacilitator mDictionaryFacilitator =
DictionaryFacilitatorProvider.getDictionaryFacilitator(
false /* isNeededForSpellChecking */);
@ -1067,7 +1069,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
@Override
public void onWindowShown() {
super.onWindowShown();
setNavigationBarVisibility(isInputViewShown());
if (isInputViewShown())
setNavigationBarColor();
}
@Override
@ -1077,7 +1080,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (mainKeyboardView != null) {
mainKeyboardView.closing();
}
setNavigationBarVisibility(false);
clearNavigationBarColor();
}
void onFinishInputInternal() {
@ -2014,12 +2017,62 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
return mSettings.getCurrent().isLanguageSwitchKeyEnabled();
}
private void setNavigationBarVisibility(final boolean visible) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
// For N and later, IMEs can specify Color.TRANSPARENT to make the navigation bar
// transparent. For other colors the system uses the default color.
getWindow().getWindow().setNavigationBarColor(
visible ? Color.BLACK : Color.TRANSPARENT);
// slightly modified from Simple Keyboard: https://github.com/rkkr/simple-keyboard/blob/master/app/src/main/java/rkr/simplekeyboard/inputmethod/latin/LatinIME.java
private void setNavigationBarColor() {
final SettingsValues settingsValues = mSettings.getCurrent();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP || !settingsValues.mNavBarColor)
return;
final int color;
if (settingsValues.mUserTheme) {
final int c = settingsValues.mBackgroundColor;
// slightly adjust so color is same as keyboard background
color = Color.rgb((int) (Color.red(c) * 0.925), (int) (Color.green(c) * 0.9379), (int) (Color.blue(c) * 0.945));
} else
color = settingsValues.mBackgroundColor;
final Window window = getWindow().getWindow();
if (window == null)
return;
mOriginalNavBarColor = window.getNavigationBarColor();
window.setNavigationBarColor(color);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
return;
final View view = window.getDecorView();
mOriginalNavBarFlags = view.getSystemUiVisibility();
if (isBrightColor(color)) {
view.setSystemUiVisibility(mOriginalNavBarFlags | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
} else {
view.setSystemUiVisibility(mOriginalNavBarFlags & ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
}
private void clearNavigationBarColor() {
final SettingsValues settingsValues = mSettings.getCurrent();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP || !settingsValues.mNavBarColor)
return;
final Window window = getWindow().getWindow();
if (window == null) {
return;
}
window.setNavigationBarColor(mOriginalNavBarColor);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
return;
final View view = window.getDecorView();
view.setSystemUiVisibility(mOriginalNavBarFlags);
}
private static boolean isBrightColor(int color) {
if (android.R.color.transparent == color) {
return true;
}
// See http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
boolean bright = false;
int[] rgb = {Color.red(color), Color.green(color), Color.blue(color)};
int brightness = (int) Math.sqrt(rgb[0] * rgb[0] * .241 + rgb[1] * rgb[1] * .691 + rgb[2] * rgb[2] * .068);
if (brightness >= 210) {
bright = true;
}
return bright;
}
}

View file

@ -51,6 +51,9 @@ class AppearanceSettingsFragment : SubScreenFragment(), Preference.OnPreferenceC
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
removePreference(Settings.PREF_THEME_DAY_NIGHT)
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
removePreference(Settings.PREF_NAVBAR_COLOR)
}
setupTheme()
if (!ProductionFlags.IS_SPLIT_KEYBOARD_SUPPORTED ||

View file

@ -137,6 +137,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
public static final String PREF_SECONDARY_LOCALES = "pref_secondary_locales";
public static final String PREF_ADD_TO_PERSONAL_DICTIONARY = "add_to_personal_dictionary";
public static final String PREF_NAVBAR_COLOR = "navbar_color";
// This preference key is deprecated. Use {@link #PREF_SHOW_LANGUAGE_SWITCH_KEY} instead.
// This is being used only for the backward compatibility.

View file

@ -111,6 +111,7 @@ public class SettingsValues {
public final int mScreenMetrics;
public final boolean mAddToPersonalDictionary;
public final boolean mUseContactsDictionary;
public final boolean mNavBarColor;
// From the input box
@Nonnull
@ -263,13 +264,14 @@ public class SettingsValues {
mOneHandedModeGravity = Settings.readOneHandedModeGravity(prefs);
mSecondaryLocale = Settings.getSecondaryLocale(prefs, RichInputMethodManager.getInstance().getCurrentSubtypeLocale().toString());
mUserTheme = KeyboardTheme.getIsUser(KeyboardTheme.getThemeForParameters(
final int keyboardThemeId = KeyboardTheme.getThemeForParameters(
prefs.getString(Settings.PREF_THEME_FAMILY, ""),
prefs.getString(Settings.PREF_THEME_VARIANT, ""),
prefs.getBoolean(Settings.PREF_THEME_KEY_BORDERS, false),
prefs.getBoolean(Settings.PREF_THEME_DAY_NIGHT, false),
prefs.getBoolean(Settings.PREF_THEME_AMOLED_MODE, false)
));
);
mUserTheme = KeyboardTheme.getIsUser(keyboardThemeId);
mUserThemeColorAccent = prefs.getInt(Settings.PREF_THEME_USER_COLOR_ACCENT, Color.BLUE);
final int keyBgColor;
if (prefs.getBoolean(Settings.PREF_THEME_KEY_BORDERS, false))
@ -280,11 +282,21 @@ public class SettingsValues {
mHintTextColorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(prefs.getInt(Settings.PREF_THEME_USER_COLOR_HINT_TEXT, Color.WHITE), BlendModeCompat.SRC_ATOP);
mKeyTextColor = prefs.getInt(Settings.PREF_THEME_USER_COLOR_TEXT, Color.WHITE);
mKeyTextColorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(mKeyTextColor, BlendModeCompat.SRC_ATOP);
mBackgroundColor = prefs.getInt(Settings.PREF_THEME_USER_COLOR_BACKGROUND, Color.DKGRAY);
if (mUserTheme) {
mBackgroundColor = prefs.getInt(Settings.PREF_THEME_USER_COLOR_BACKGROUND, Color.DKGRAY);
} else if (KeyboardTheme.THEME_VARIANT_LIGHT.equals(KeyboardTheme.getThemeVariant(keyboardThemeId))) {
mBackgroundColor = Color.rgb(236, 239, 241);
} else if (keyboardThemeId == KeyboardTheme.THEME_ID_LXX_DARK) {
mBackgroundColor = Color.rgb(38, 50, 56);
} else {
// dark border is 13/13/13, but that's ok
mBackgroundColor = Color.BLACK;
}
mBackgroundColorFilter = BlendModeColorFilterCompat.createBlendModeColorFilterCompat(mBackgroundColor, BlendModeCompat.MODULATE);
mAddToPersonalDictionary = prefs.getBoolean(Settings.PREF_ADD_TO_PERSONAL_DICTIONARY, false);
mUseContactsDictionary = prefs.getBoolean(AndroidSpellCheckerService.PREF_USE_CONTACTS_KEY, false);
mNavBarColor = prefs.getBoolean(Settings.PREF_NAVBAR_COLOR, false);
}
public boolean isMetricsLoggingEnabled() {