From a83a19a0a895ae4b86b6185bac65004c1fb0ba4c Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sat, 3 Jun 2023 20:06:44 +0200 Subject: [PATCH] Refactor: Handle Event keys in Keyboard2 The `KeyEventHandler` class is intended to handle every keys and to call into the main class through a limited API. However, this is not true for `Event` keys, which in practice had each a corresponding API call. --- srcs/juloo.keyboard2/KeyEventHandler.java | 42 +----- srcs/juloo.keyboard2/Keyboard2.java | 151 ++++++++++++---------- 2 files changed, 82 insertions(+), 111 deletions(-) diff --git a/srcs/juloo.keyboard2/KeyEventHandler.java b/srcs/juloo.keyboard2/KeyEventHandler.java index 70b54b9..36f6819 100644 --- a/srcs/juloo.keyboard2/KeyEventHandler.java +++ b/srcs/juloo.keyboard2/KeyEventHandler.java @@ -10,8 +10,6 @@ class KeyEventHandler implements Config.IKeyEventHandler IReceiver _recv; Autocapitalisation _autocap; - public int actionId; // Action performed by the Action key. - public KeyEventHandler(Looper looper, IReceiver recv) { _recv = recv; @@ -40,28 +38,7 @@ class KeyEventHandler implements Config.IKeyEventHandler { case Char: send_text(String.valueOf(key.getChar())); break; case String: send_text(key.getString()); break; - case Event: - switch (key.getEvent()) - { - case CONFIG: _recv.showKeyboardConfig(); break; - case SWITCH_TEXT: _recv.set_layout(Layout.Current); break; - case SWITCH_NUMERIC: _recv.set_layout(Layout.Numeric); break; - case SWITCH_EMOJI: _recv.setPane_emoji(); break; - case SWITCH_BACK_EMOJI: _recv.setPane_normal(); break; - case CHANGE_METHOD: _recv.switchInputMethod(); break; - case CHANGE_METHOD_PREV: _recv.switchToPrevInputMethod(); break; - case ACTION: - InputConnection conn = _recv.getCurrentInputConnection(); - if (conn != null) - conn.performEditorAction(actionId); - break; - case SWITCH_SECOND: _recv.set_layout(Layout.Secondary); break; - case SWITCH_SECOND_BACK: _recv.set_layout(Layout.Primary); break; - case SWITCH_GREEKMATH: _recv.set_layout(Layout.Greekmath); break; - case CAPS_LOCK: _recv.set_shift_state(true, true); break; - case SWITCH_VOICE_TYPING: _recv.switch_voice_typing(); break; - } - break; + case Event: _recv.handle_event_key(key.getEvent()); break; case Keyevent: handleKeyUpWithModifier(key.getKeyevent(), mods); break; @@ -170,24 +147,9 @@ class KeyEventHandler implements Config.IKeyEventHandler conn.performContextMenuAction(id); } - public enum Layout - { - Current, // The primary or secondary layout - Primary, - Secondary, - Numeric, - Greekmath - } - public static interface IReceiver { - public void switchInputMethod(); - public void switchToPrevInputMethod(); - public void switch_voice_typing(); - public void setPane_emoji(); - public void setPane_normal(); - public void showKeyboardConfig(); - public void set_layout(Layout l); + public void handle_event_key(KeyValue.Event ev); public void set_shift_state(boolean state, boolean lock); public InputConnection getCurrentInputConnection(); } diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index 5394a6c..2742de7 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -36,6 +36,7 @@ public class Keyboard2 extends InputMethodService // Layout associated with the currently selected locale. private KeyboardData _localeTextLayout; private ViewGroup _emojiPane = null; + public int actionId; // Action performed by the Action key. private Config _config; @@ -150,9 +151,14 @@ public class Keyboard2 extends InputMethodService } } + InputMethodManager get_imm() + { + return (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); + } + private void refreshSubtypeImm() { - InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); + InputMethodManager imm = get_imm(); if (VERSION.SDK_INT < 28) _config.shouldOfferSwitchingToNextInputMethod = true; else @@ -213,14 +219,14 @@ public class Keyboard2 extends InputMethodService if (info.actionLabel != null) { _config.actionLabel = info.actionLabel.toString(); - _keyeventhandler.actionId = info.actionId; + actionId = info.actionId; _config.swapEnterActionKey = false; } else { int action = info.imeOptions & EditorInfo.IME_MASK_ACTION; _config.actionLabel = actionLabel_of_imeAction(action); // Might be null - _keyeventhandler.actionId = action; + actionId = action; _config.swapEnterActionKey = (info.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0; } @@ -398,46 +404,80 @@ public class Keyboard2 extends InputMethodService /** Not static */ public class Receiver implements KeyEventHandler.IReceiver { - public void switchInputMethod() + public void handle_event_key(KeyValue.Event ev) { - InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); - imm.showInputMethodPicker(); - } - - public void switchToPrevInputMethod() - { - if (VERSION.SDK_INT < 28) + switch (ev) { - InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); - imm.switchToLastInputMethod(getConnectionToken()); + case CONFIG: + Intent intent = new Intent(Keyboard2.this, SettingsActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(intent); + break; + + case SWITCH_TEXT: + _currentSpecialLayout = null; + _keyboardView.setKeyboard(current_layout()); + break; + + case SWITCH_NUMERIC: + setSpecialLayout(_config.modify_numpad(loadLayout(R.xml.numeric))); + break; + + case SWITCH_EMOJI: + if (_emojiPane == null) + _emojiPane = (ViewGroup)inflate_view(R.layout.emoji_pane); + setInputView(_emojiPane); + break; + + case SWITCH_BACK_EMOJI: + setInputView(_keyboardView); + break; + + case CHANGE_METHOD: + get_imm().showInputMethodPicker(); + break; + + case CHANGE_METHOD_PREV: + if (VERSION.SDK_INT < 28) + get_imm().switchToLastInputMethod(getConnectionToken()); + else + switchToPreviousInputMethod(); + break; + + case ACTION: + InputConnection conn = getCurrentInputConnection(); + if (conn != null) + conn.performEditorAction(actionId); + break; + + case SWITCH_SECOND: + if (_config.second_layout != null) + setTextLayout(Current_text_layout.SECONDARY); + break; + + case SWITCH_SECOND_BACK: + setTextLayout(Current_text_layout.PRIMARY); + break; + + case SWITCH_GREEKMATH: + setSpecialLayout(_config.modify_numpad(loadLayout(R.xml.greekmath))); + break; + + case CAPS_LOCK: + set_shift_state(true, true); + break; + + case SWITCH_VOICE_TYPING: + SimpleEntry im = get_voice_typing_im(get_imm()); + if (im == null) + return; + // Best-effort. Good enough for triggering Google's voice typing. + if (VERSION.SDK_INT < 28) + switchInputMethod(im.getKey()); + else + switchInputMethod(im.getKey(), im.getValue()); + break; } - else - Keyboard2.this.switchToPreviousInputMethod(); - } - - public void switch_voice_typing() - { - InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); - SimpleEntry im = get_voice_typing_im(imm); - if (im == null) - return; - // Best-effort. Good enough for triggering Google's voice typing. - if (VERSION.SDK_INT < 28) - Keyboard2.this.switchInputMethod(im.getKey()); - else - Keyboard2.this.switchInputMethod(im.getKey(), im.getValue()); - } - - public void setPane_emoji() - { - if (_emojiPane == null) - _emojiPane = (ViewGroup)inflate_view(R.layout.emoji_pane); - setInputView(_emojiPane); - } - - public void setPane_normal() - { - setInputView(_keyboardView); } public void set_shift_state(boolean state, boolean lock) @@ -445,37 +485,6 @@ public class Keyboard2 extends InputMethodService _keyboardView.set_shift_state(state, lock); } - public void set_layout(KeyEventHandler.Layout l) - { - switch (l) - { - case Current: - _currentSpecialLayout = null; - _keyboardView.setKeyboard(current_layout()); - break; - case Primary: - setTextLayout(Current_text_layout.PRIMARY); - break; - case Secondary: - if (_config.second_layout != null) - setTextLayout(Current_text_layout.SECONDARY); - break; - case Numeric: - setSpecialLayout(_config.modify_numpad(loadLayout(R.xml.numeric))); - break; - case Greekmath: - setSpecialLayout(_config.modify_numpad(loadLayout(R.xml.greekmath))); - break; - } - } - - public void showKeyboardConfig() - { - Intent intent = new Intent(Keyboard2.this, SettingsActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(intent); - } - public InputConnection getCurrentInputConnection() { return Keyboard2.this.getCurrentInputConnection();