Select theme depending on system settings

Automatically choose between the Dark and Light themes.
This commit is contained in:
Jules Aguillon 2022-01-30 23:29:50 +01:00
parent c9f7f2cfc8
commit 8631dfb723
7 changed files with 47 additions and 18 deletions

View File

@ -28,6 +28,7 @@
<string name="pref_character_size_title">Taille des labels</string> <string name="pref_character_size_title">Taille des labels</string>
<string name="pref_character_size_summary">Taille des caractères affichés sur les touches (%.2fx)</string> <string name="pref_character_size_summary">Taille des caractères affichés sur les touches (%.2fx)</string>
<string name="pref_theme">Thème</string> <string name="pref_theme">Thème</string>
<string name="pref_theme_e_system">Paramètre système</string>
<string name="pref_theme_e_dark">Sombre</string> <string name="pref_theme_e_dark">Sombre</string>
<string name="pref_theme_e_light">Clair</string> <string name="pref_theme_e_light">Clair</string>
<string name="pref_theme_e_black">Noir</string> <string name="pref_theme_e_black">Noir</string>

View File

@ -28,6 +28,7 @@
<string name="pref_character_size_title">Iezīmes izmērs</string> <string name="pref_character_size_title">Iezīmes izmērs</string>
<string name="pref_character_size_summary">Tastatūrā attēloto rakstzīmju izmērs (%.2fx)</string> <string name="pref_character_size_summary">Tastatūrā attēloto rakstzīmju izmērs (%.2fx)</string>
<string name="pref_theme">Izskats</string> <string name="pref_theme">Izskats</string>
<string name="pref_theme_e_system">Ierīces iestatījumi</string>
<string name="pref_theme_e_dark">Tumšs</string> <string name="pref_theme_e_dark">Tumšs</string>
<string name="pref_theme_e_light">Gaišs</string> <string name="pref_theme_e_light">Gaišs</string>
<string name="pref_theme_e_black">Melns</string> <string name="pref_theme_e_black">Melns</string>

View File

@ -29,11 +29,13 @@
<item>4</item> <item>4</item>
</string-array> </string-array>
<string-array name="pref_theme_entries"> <string-array name="pref_theme_entries">
<item>@string/pref_theme_e_system</item>
<item>@string/pref_theme_e_dark</item> <item>@string/pref_theme_e_dark</item>
<item>@string/pref_theme_e_light</item> <item>@string/pref_theme_e_light</item>
<item>@string/pref_theme_e_black</item> <item>@string/pref_theme_e_black</item>
</string-array> </string-array>
<string-array name="pref_theme_values"> <string-array name="pref_theme_values">
<item>system</item>
<item>dark</item> <item>dark</item>
<item>light</item> <item>light</item>
<item>black</item> <item>black</item>

View File

@ -28,6 +28,7 @@
<string name="pref_character_size_title">Label size</string> <string name="pref_character_size_title">Label size</string>
<string name="pref_character_size_summary">Size of characters displayed on the keyboard (%.2fx)</string> <string name="pref_character_size_summary">Size of characters displayed on the keyboard (%.2fx)</string>
<string name="pref_theme">Theme</string> <string name="pref_theme">Theme</string>
<string name="pref_theme_e_system">System settings</string>
<string name="pref_theme_e_dark">Dark</string> <string name="pref_theme_e_dark">Dark</string>
<string name="pref_theme_e_light">Light</string> <string name="pref_theme_e_light">Light</string>
<string name="pref_theme_e_black">Black</string> <string name="pref_theme_e_black">Black</string>

View File

@ -15,7 +15,7 @@
<juloo.common.IntSlideBarPreference android:key="vibrate_duration" android:title="@string/pref_vibrate_duration_title" android:summary="%sms" android:defaultValue="20" min="5" max="50"/> <juloo.common.IntSlideBarPreference android:key="vibrate_duration" android:title="@string/pref_vibrate_duration_title" android:summary="%sms" android:defaultValue="20" min="5" max="50"/>
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_style"> <PreferenceCategory android:title="@string/pref_category_style">
<ListPreference android:key="theme" android:title="@string/pref_theme" android:summary="%s" android:defaultValue="dark" android:entries="@array/pref_theme_entries" android:entryValues="@array/pref_theme_values"/> <ListPreference android:key="theme" android:title="@string/pref_theme" android:summary="%s" android:defaultValue="system" android:entries="@array/pref_theme_entries" android:entryValues="@array/pref_theme_values"/>
<juloo.common.IntSlideBarPreference android:key="margin_bottom" android:title="@string/pref_margin_bottom_title" android:summary="%sdp" android:defaultValue="5" min="0" max="100"/> <juloo.common.IntSlideBarPreference android:key="margin_bottom" android:title="@string/pref_margin_bottom_title" android:summary="%sdp" android:defaultValue="5" min="0" max="100"/>
<juloo.common.IntSlideBarPreference android:key="key_height" android:title="@string/pref_key_height_title" android:summary="%sdp" android:defaultValue="50" min="30" max="90"/> <juloo.common.IntSlideBarPreference android:key="key_height" android:title="@string/pref_key_height_title" android:summary="%sdp" android:defaultValue="50" min="30" max="90"/>
<juloo.common.IntSlideBarPreference android:key="horizontal_margin" android:title="@string/pref_horizontal_margin_title" android:summary="%sdp" android:defaultValue="3" min="0" max="20"/> <juloo.common.IntSlideBarPreference android:key="horizontal_margin" android:title="@string/pref_horizontal_margin_title" android:summary="%sdp" android:defaultValue="3" min="0" max="20"/>

View File

@ -2,7 +2,9 @@ package juloo.keyboard2;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.content.res.Configuration;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.TypedValue; import android.util.TypedValue;
@ -77,7 +79,8 @@ final class Config
public void refresh(Context context) public void refresh(Context context)
{ {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
DisplayMetrics dm = context.getResources().getDisplayMetrics(); Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
layout = layoutId_of_string(prefs.getString("layout", "system")); layout = layoutId_of_string(prefs.getString("layout", "system"));
swipe_dist_dp = Float.valueOf(prefs.getString("swipe_dist", "15")); swipe_dist_dp = Float.valueOf(prefs.getString("swipe_dist", "15"));
swipe_dist_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, swipe_dist_dp, dm); swipe_dist_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, swipe_dist_dp, dm);
@ -93,7 +96,7 @@ final class Config
preciseRepeat = prefs.getBoolean("precise_repeat", preciseRepeat); preciseRepeat = prefs.getBoolean("precise_repeat", preciseRepeat);
characterSize = prefs.getFloat("character_size", characterSize); characterSize = prefs.getFloat("character_size", characterSize);
accents = Integer.valueOf(prefs.getString("accents", "1")); accents = Integer.valueOf(prefs.getString("accents", "1"));
theme = themeId_of_string(prefs.getString("theme", "")); theme = getThemeId(res, prefs.getString("theme", ""));
} }
private float getDipPref(DisplayMetrics dm, SharedPreferences prefs, String pref_name, float def) private float getDipPref(DisplayMetrics dm, SharedPreferences prefs, String pref_name, float def)
@ -104,6 +107,25 @@ final class Config
return (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, dm)); return (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, dm));
} }
private int getThemeId(Resources res, String theme_name)
{
switch (theme_name)
{
case "light": return R.style.Light;
case "black": return R.style.Black;
case "dark": return R.style.Dark;
default:
case "system":
if (Build.VERSION.SDK_INT >= 8)
{
int night_mode = res.getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
if ((night_mode & Configuration.UI_MODE_NIGHT_NO) != 0)
return R.style.Light;
}
return R.style.Dark;
}
}
public static int layoutId_of_string(String name) public static int layoutId_of_string(String name)
{ {
switch (name) switch (name)

View File

@ -57,6 +57,7 @@ public class Keyboard2 extends InputMethodService
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
Config.initGlobalConfig(this, new KeyEventHandler(this.new Receiver())); Config.initGlobalConfig(this, new KeyEventHandler(this.new Receiver()));
_config = Config.globalConfig(); _config = Config.globalConfig();
_config.refresh(this);
_keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard); _keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard);
_keyboardView.reset(); _keyboardView.reset();
} }
@ -177,17 +178,27 @@ public class Keyboard2 extends InputMethodService
} }
} }
private void refreshConfig()
{
int prev_theme = _config.theme;
_config.refresh(this);
refreshSubtypeImm();
// Refreshing the theme config requires re-creating the views
if (prev_theme != _config.theme)
{
_keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard);
_emojiPane = null;
}
_keyboardView.setKeyboard(getLayout(_currentTextLayout));
}
@Override @Override
public void onStartInputView(EditorInfo info, boolean restarting) public void onStartInputView(EditorInfo info, boolean restarting)
{ {
// Update '_config' before calling 'KeyboardView.setKeyboard' refreshConfig();
refreshSubtypeImm();
refreshEditorInfo(info); refreshEditorInfo(info);
if ((info.inputType & InputType.TYPE_CLASS_NUMBER) != 0) if ((info.inputType & InputType.TYPE_CLASS_NUMBER) != 0)
_keyboardView.setKeyboard(getLayout(R.xml.numeric)); _keyboardView.setKeyboard(getLayout(R.xml.numeric));
else
_keyboardView.setKeyboard(getLayout(_currentTextLayout));
_keyboardView.reset(); // Layout might need to change due to rotation
setInputView(_keyboardView); setInputView(_keyboardView);
} }
@ -217,16 +228,7 @@ public class Keyboard2 extends InputMethodService
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{ {
int prev_theme = _config.theme; refreshConfig();
_config.refresh(this);
refreshSubtypeImm();
_keyboardView.setKeyboard(getLayout(_currentTextLayout));
// Refreshing the theme config requires re-creating the views
if (prev_theme != _config.theme)
{
_keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard);
_emojiPane = null;
}
} }
/** Not static */ /** Not static */