From ca07bff1330024bd78e28d1bc83940daab678fbb Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Thu, 15 Apr 2021 23:56:34 +0200 Subject: [PATCH] Use subtypes to choose layout This removes the "layout" setting. Every layouts that the user could use will appear in the global settings. --- res/values/strings.xml | 9 ++-- res/xml/method.xml | 12 +++++ res/xml/settings.xml | 8 --- srcs/juloo.keyboard2/Keyboard2.java | 77 ++++++++++++++++++++--------- 4 files changed, 69 insertions(+), 37 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index db45f85..e53b4b6 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -5,13 +5,10 @@ Unexpected Keyboard Settings + %s AZERTY + %s QWERTY + Layout - Change keyboard layout - %s - - Azerty - Qwerty - Typing Precision diff --git a/res/xml/method.xml b/res/xml/method.xml index 63887e3..f13eecc 100644 --- a/res/xml/method.xml +++ b/res/xml/method.xml @@ -2,4 +2,16 @@ + + diff --git a/res/xml/settings.xml b/res/xml/settings.xml index a4cf7ff..8c37179 100644 --- a/res/xml/settings.xml +++ b/res/xml/settings.xml @@ -1,14 +1,6 @@ - _layoutCache = new HashMap(); + + private static final int DEFAULT_LAYOUT = R.xml.qwerty; + private static final Map LAYOUTS = new HashMap(); + + private static void add_layout(String lang, int resId) + { + LAYOUTS.put(new Locale(lang).getLanguage(), resId); + } + + static + { + add_layout("fr", R.xml.azerty); + } + + private KeyboardData getLayout(int resId) + { + KeyboardData l = _layoutCache.get(resId); + if (l == null) + { + l = KeyboardData.parse(getResources().getXml(resId)); + _layoutCache.put(resId, l); + } + return l; + } + @Override public void onCreate() { @@ -34,7 +64,6 @@ public class Keyboard2 extends InputMethodService PreferenceManager.setDefaultValues(this, R.xml.settings, false); PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); _config = new Config(this); - _numericKeyboard = KeyboardData.parse(getResources().getXml(R.xml.numeric)); _keyboardView = (Keyboard2View)getLayoutInflater().inflate(R.layout.keyboard, null); _keyboardView.reset(); } @@ -49,6 +78,14 @@ public class Keyboard2 extends InputMethodService return (_specialKeyFont); } + private void refreshSubtype(InputMethodSubtype subtype) + { + Integer l = LAYOUTS.get(subtype.getLanguageTag()); + if (l == null) + l = DEFAULT_LAYOUT; + _currentTextLayout = l; + } + @Override public View onCreateInputView() { @@ -62,17 +99,25 @@ public class Keyboard2 extends InputMethodService @Override public void onStartInputView(EditorInfo info, boolean restarting) { + InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); + refreshSubtype(imm.getCurrentInputMethodSubtype()); if ((info.inputType & InputType.TYPE_CLASS_NUMBER) != 0) - _keyboardView.setKeyboard(_numericKeyboard); - else - _keyboardView.setKeyboard(_textKeyboard); + _keyboardView.setKeyboard(getLayout(R.xml.numeric)); + else + _keyboardView.setKeyboard(getLayout(_currentTextLayout)); } + @Override + public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype) + { + refreshSubtype(subtype); + _keyboardView.setKeyboard(getLayout(_currentTextLayout)); + } + @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { _config.refresh(); - updateConfig(); _keyboardView.reset(); } @@ -82,20 +127,6 @@ public class Keyboard2 extends InputMethodService _keyboardView.reset(); } - private int _getKeyboardLayoutRes(SharedPreferences prefs) - { - // Not looking up using [getIdentifier] as it was intended because the - // [packageName] argument can't be passed reliably (eg. debug builds) - switch (prefs.getString("keyboard_layout", null)) - { - case "azerty": - return R.xml.azerty; - default: - case "qwerty": - return R.xml.qwerty; - } - } - public void handleKeyUp(KeyValue key, int flags) { int eventCode = key.getEventCode(); @@ -110,9 +141,9 @@ public class Keyboard2 extends InputMethodService startActivity(intent); } else if (eventCode == KeyValue.EVENT_SWITCH_TEXT) - _keyboardView.setKeyboard(_textKeyboard); + _keyboardView.setKeyboard(getLayout(_currentTextLayout)); else if (eventCode == KeyValue.EVENT_SWITCH_NUMERIC) - _keyboardView.setKeyboard(_numericKeyboard); + _keyboardView.setKeyboard(getLayout(R.xml.numeric)); else if (eventCode == KeyValue.EVENT_SWITCH_EMOJI) { if (_emojiPane == null)