diff options
| author | Jules Aguillon | 2022-11-13 16:18:08 +0100 |
|---|---|---|
| committer | Jules Aguillon | 2022-11-13 16:28:39 +0100 |
| commit | 078dbcd5ff7f0828f48d66d866ea49d3eb14cc6a (patch) | |
| tree | 961089cf4b15f266fa1b32aeb0f3605accf4e20f /srcs/juloo.keyboard2/KeyEventHandler.java | |
| parent | 22a7df6632bcd53ef96cc7e314637ae2ec4b8695 (diff) | |
| download | unexpected-keyboard-078dbcd5ff7f0828f48d66d866ea49d3eb14cc6a.tar.gz unexpected-keyboard-078dbcd5ff7f0828f48d66d866ea49d3eb14cc6a.zip | |
Refactor: Move editing code from to KeyEventHandler
Remove the code dealing with InputMethodConnection from 'Keyboard2' and
move it into 'KeyEventHandler', where more editing actions can now be
implemented.
Autocapitalisation is also moved, the IReceiver interface is simplified.
Diffstat (limited to 'srcs/juloo.keyboard2/KeyEventHandler.java')
| -rw-r--r-- | srcs/juloo.keyboard2/KeyEventHandler.java | 112 |
1 files changed, 80 insertions, 32 deletions
diff --git a/srcs/juloo.keyboard2/KeyEventHandler.java b/srcs/juloo.keyboard2/KeyEventHandler.java index 5d23903..ebf838c 100644 --- a/srcs/juloo.keyboard2/KeyEventHandler.java +++ b/srcs/juloo.keyboard2/KeyEventHandler.java @@ -1,42 +1,63 @@ package juloo.keyboard2; +import android.os.Looper; import android.view.KeyEvent; +import android.view.inputmethod.EditorInfo; +import android.view.inputmethod.InputConnection; class KeyEventHandler implements Config.IKeyEventHandler { - private IReceiver _recv; + IReceiver _recv; + Autocapitalisation _autocap; - public KeyEventHandler(IReceiver recv) + public int actionId; // Action performed by the Action key. + + public KeyEventHandler(Looper looper, IReceiver recv) { _recv = recv; + _autocap = new Autocapitalisation(looper, + this.new Autocapitalisation_callback()); + } + + /** Editing just started. */ + public void started(EditorInfo info) + { + _autocap.started(info, _recv.getCurrentInputConnection()); + } + + /** Selection has been updated. */ + public void selection_updated(int oldSelStart, int newSelStart) + { + _autocap.selection_updated(oldSelStart, newSelStart); } - public void handleKeyUp(KeyValue key, Pointers.Modifiers mods) + /** A key has been released. */ + public void key_up(KeyValue key, Pointers.Modifiers mods) { if (key == null) return; switch (key.getKind()) { - case Char: - _recv.commitChar(key.getChar()); - break; - case String: - _recv.commitText(key.getString()); - break; + 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: - case SWITCH_SECOND_BACK: _recv.switchMain(); break; - case SWITCH_NUMERIC: _recv.switchNumeric(); break; + case SWITCH_SECOND_BACK: _recv.switch_main(); break; + case SWITCH_NUMERIC: _recv.switch_layout(R.xml.numeric); break; case SWITCH_EMOJI: _recv.setPane_emoji(); break; case SWITCH_BACK_EMOJI: _recv.setPane_normal(); break; case CHANGE_METHOD: _recv.switchToNextInputMethod(); break; - case ACTION: _recv.performAction(); break; - case SWITCH_SECOND: _recv.switchSecond(); break; - case SWITCH_GREEKMATH: _recv.switchGreekmath(); break; - case CAPS_LOCK: _recv.enableCapsLock(); break; + case ACTION: + InputConnection conn = _recv.getCurrentInputConnection(); + if (conn != null) + conn.performEditorAction(actionId); + break; + case SWITCH_SECOND: _recv.switch_second(); break; + case SWITCH_GREEKMATH: _recv.switch_layout(R.xml.greekmath); break; + case CAPS_LOCK: _recv.set_shift_state(true, true); break; } break; case Keyevent: @@ -57,17 +78,17 @@ class KeyEventHandler implements Config.IKeyEventHandler // getCurrentInputConnection().deleteSurroundingText(before, after); // } - private int sendMetaKey(int eventCode, int metaFlags, int metaState, boolean down) + int sendMetaKey(int eventCode, int metaFlags, int metaState, boolean down) { int action; int updatedMetaState; if (down) { action = KeyEvent.ACTION_DOWN; updatedMetaState = metaState | metaFlags; } else { action = KeyEvent.ACTION_UP; updatedMetaState = metaState & ~metaFlags; } - _recv.sendKeyEvent(action, eventCode, metaState); + send_keyevent(action, eventCode, metaState); return updatedMetaState; } - private int sendMetaKeyForModifier(KeyValue.Modifier mod, int metaState, boolean down) + int sendMetaKeyForModifier(KeyValue.Modifier mod, int metaState, boolean down) { switch (mod) { @@ -86,16 +107,35 @@ class KeyEventHandler implements Config.IKeyEventHandler /* * Don't set KeyEvent.FLAG_SOFT_KEYBOARD. */ - private void handleKeyUpWithModifier(int keyCode, Pointers.Modifiers mods) - { + void handleKeyUpWithModifier(int keyCode, Pointers.Modifiers mods) + { int metaState = 0; for (int i = 0; i < mods.size(); i++) metaState = sendMetaKeyForModifier(mods.get(i), metaState, true); - _recv.sendKeyEvent(KeyEvent.ACTION_DOWN, keyCode, metaState); - _recv.sendKeyEvent(KeyEvent.ACTION_UP, keyCode, metaState); + send_keyevent(KeyEvent.ACTION_DOWN, keyCode, metaState); + send_keyevent(KeyEvent.ACTION_UP, keyCode, metaState); for (int i = mods.size() - 1; i >= 0; i--) metaState = sendMetaKeyForModifier(mods.get(i), metaState, false); - } + } + + void send_keyevent(int eventAction, int eventCode, int meta) + { + InputConnection conn = _recv.getCurrentInputConnection(); + if (conn == null) + return; + conn.sendKeyEvent(new KeyEvent(1, 1, eventAction, eventCode, 0, meta)); + if (eventAction == KeyEvent.ACTION_UP) + _autocap.event_sent(eventCode, meta); + } + + void send_text(CharSequence text) + { + InputConnection conn = _recv.getCurrentInputConnection(); + if (conn == null) + return; + conn.commitText(text, 1); + _autocap.typed(text); + } public static interface IReceiver { @@ -103,17 +143,25 @@ class KeyEventHandler implements Config.IKeyEventHandler public void setPane_emoji(); public void setPane_normal(); public void showKeyboardConfig(); - public void performAction(); - public void enableCapsLock(); - public void switchMain(); - public void switchNumeric(); - public void switchSecond(); - public void switchGreekmath(); + public void switch_main(); + public void switch_second(); + public void switch_layout(int layout_id); + + public void set_shift_state(boolean state, boolean lock); - public void sendKeyEvent(int eventAction, int eventCode, int meta); + public InputConnection getCurrentInputConnection(); + } - public void commitText(String text); - public void commitChar(char c); + class Autocapitalisation_callback implements Autocapitalisation.Callback + { + @Override + public void update_shift_state(boolean should_enable, boolean should_disable) + { + if (should_enable) + _recv.set_shift_state(true, false); + else if (should_disable) + _recv.set_shift_state(false, false); + } } } |
