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.
This commit is contained in:
Jules Aguillon 2022-11-13 16:18:08 +01:00
parent 22a7df6632
commit 078dbcd5ff
7 changed files with 104 additions and 101 deletions

View File

@ -59,12 +59,6 @@ final class Autocapitalisation
callback(false); callback(false);
} }
public void typed(char c)
{
type_one_char(c);
callback(false);
}
public void event_sent(int code, int meta) public void event_sent(int code, int meta)
{ {
if (meta != 0) if (meta != 0)

View File

@ -1,16 +1,16 @@
package juloo.keyboard2; package juloo.keyboard2;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.content.res.Configuration;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build; import android.os.Build;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.KeyEvent; import android.view.KeyEvent;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
import java.util.HashSet;
final class Config final class Config
{ {
@ -294,6 +294,6 @@ final class Config
public static interface IKeyEventHandler public static interface IKeyEventHandler
{ {
public void handleKeyUp(KeyValue value, Pointers.Modifiers flags); public void key_up(KeyValue value, Pointers.Modifiers flags);
} }
} }

View File

@ -54,7 +54,7 @@ public class EmojiGridView extends GridView
Config config = Config.globalConfig(); Config config = Config.globalConfig();
Integer used = _lastUsed.get(_emojiArray[pos]); Integer used = _lastUsed.get(_emojiArray[pos]);
_lastUsed.put(_emojiArray[pos], (used == null) ? 1 : used.intValue() + 1); _lastUsed.put(_emojiArray[pos], (used == null) ? 1 : used.intValue() + 1);
config.handler.handleKeyUp(_emojiArray[pos].kv(), Pointers.Modifiers.EMPTY); config.handler.key_up(_emojiArray[pos].kv(), Pointers.Modifiers.EMPTY);
saveLastUsed(); // TODO: opti saveLastUsed(); // TODO: opti
} }

View File

@ -24,6 +24,6 @@ public class EmojiKeyButton extends Button
public void onClick(View v) public void onClick(View v)
{ {
Config config = Config.globalConfig(); Config config = Config.globalConfig();
config.handler.handleKeyUp(_key, Pointers.Modifiers.EMPTY); config.handler.key_up(_key, Pointers.Modifiers.EMPTY);
} }
} }

View File

@ -1,42 +1,63 @@
package juloo.keyboard2; package juloo.keyboard2;
import android.os.Looper;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
class KeyEventHandler implements Config.IKeyEventHandler 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; _recv = recv;
_autocap = new Autocapitalisation(looper,
this.new Autocapitalisation_callback());
} }
public void handleKeyUp(KeyValue key, Pointers.Modifiers mods) /** 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);
}
/** A key has been released. */
public void key_up(KeyValue key, Pointers.Modifiers mods)
{ {
if (key == null) if (key == null)
return; return;
switch (key.getKind()) switch (key.getKind())
{ {
case Char: case Char: send_text(String.valueOf(key.getChar())); break;
_recv.commitChar(key.getChar()); case String: send_text(key.getString()); break;
break;
case String:
_recv.commitText(key.getString());
break;
case Event: case Event:
switch (key.getEvent()) switch (key.getEvent())
{ {
case CONFIG: _recv.showKeyboardConfig(); break; case CONFIG: _recv.showKeyboardConfig(); break;
case SWITCH_TEXT: case SWITCH_TEXT:
case SWITCH_SECOND_BACK: _recv.switchMain(); break; case SWITCH_SECOND_BACK: _recv.switch_main(); break;
case SWITCH_NUMERIC: _recv.switchNumeric(); break; case SWITCH_NUMERIC: _recv.switch_layout(R.xml.numeric); break;
case SWITCH_EMOJI: _recv.setPane_emoji(); break; case SWITCH_EMOJI: _recv.setPane_emoji(); break;
case SWITCH_BACK_EMOJI: _recv.setPane_normal(); break; case SWITCH_BACK_EMOJI: _recv.setPane_normal(); break;
case CHANGE_METHOD: _recv.switchToNextInputMethod(); break; case CHANGE_METHOD: _recv.switchToNextInputMethod(); break;
case ACTION: _recv.performAction(); break; case ACTION:
case SWITCH_SECOND: _recv.switchSecond(); break; InputConnection conn = _recv.getCurrentInputConnection();
case SWITCH_GREEKMATH: _recv.switchGreekmath(); break; if (conn != null)
case CAPS_LOCK: _recv.enableCapsLock(); break; 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; break;
case Keyevent: case Keyevent:
@ -57,17 +78,17 @@ class KeyEventHandler implements Config.IKeyEventHandler
// getCurrentInputConnection().deleteSurroundingText(before, after); // 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 action;
int updatedMetaState; int updatedMetaState;
if (down) { action = KeyEvent.ACTION_DOWN; updatedMetaState = metaState | metaFlags; } if (down) { action = KeyEvent.ACTION_DOWN; updatedMetaState = metaState | metaFlags; }
else { action = KeyEvent.ACTION_UP; updatedMetaState = metaState & ~metaFlags; } else { action = KeyEvent.ACTION_UP; updatedMetaState = metaState & ~metaFlags; }
_recv.sendKeyEvent(action, eventCode, metaState); send_keyevent(action, eventCode, metaState);
return updatedMetaState; return updatedMetaState;
} }
private int sendMetaKeyForModifier(KeyValue.Modifier mod, int metaState, boolean down) int sendMetaKeyForModifier(KeyValue.Modifier mod, int metaState, boolean down)
{ {
switch (mod) switch (mod)
{ {
@ -86,34 +107,61 @@ class KeyEventHandler implements Config.IKeyEventHandler
/* /*
* Don't set KeyEvent.FLAG_SOFT_KEYBOARD. * 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; int metaState = 0;
for (int i = 0; i < mods.size(); i++) for (int i = 0; i < mods.size(); i++)
metaState = sendMetaKeyForModifier(mods.get(i), metaState, true); metaState = sendMetaKeyForModifier(mods.get(i), metaState, true);
_recv.sendKeyEvent(KeyEvent.ACTION_DOWN, keyCode, metaState); send_keyevent(KeyEvent.ACTION_DOWN, keyCode, metaState);
_recv.sendKeyEvent(KeyEvent.ACTION_UP, keyCode, metaState); send_keyevent(KeyEvent.ACTION_UP, keyCode, metaState);
for (int i = mods.size() - 1; i >= 0; i--) for (int i = mods.size() - 1; i >= 0; i--)
metaState = sendMetaKeyForModifier(mods.get(i), metaState, false); 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 public static interface IReceiver
{ {
public void switchToNextInputMethod(); public void switchToNextInputMethod();
public void setPane_emoji(); public void setPane_emoji();
public void setPane_normal(); public void setPane_normal();
public void showKeyboardConfig(); public void showKeyboardConfig();
public void performAction();
public void enableCapsLock();
public void switchMain(); public void switch_main();
public void switchNumeric(); public void switch_second();
public void switchSecond(); public void switch_layout(int layout_id);
public void switchGreekmath();
public void sendKeyEvent(int eventAction, int eventCode, int meta); public void set_shift_state(boolean state, boolean lock);
public void commitText(String text); public InputConnection getCurrentInputConnection();
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);
}
} }
} }

View File

@ -25,17 +25,16 @@ import java.util.List;
import java.util.Set; import java.util.Set;
public class Keyboard2 extends InputMethodService public class Keyboard2 extends InputMethodService
implements SharedPreferences.OnSharedPreferenceChangeListener, implements SharedPreferences.OnSharedPreferenceChangeListener
Autocapitalisation.Callback
{ {
static private final String TAG = "Keyboard2"; static private final String TAG = "Keyboard2";
private Keyboard2View _keyboardView; private Keyboard2View _keyboardView;
private KeyEventHandler _keyeventhandler;
private int _currentTextLayout; private int _currentTextLayout;
private ViewGroup _emojiPane = null; private ViewGroup _emojiPane = null;
private Config _config; private Config _config;
private Autocapitalisation _autocap;
private boolean _debug_logs = false; private boolean _debug_logs = false;
@ -50,20 +49,12 @@ public class Keyboard2 extends InputMethodService
super.onCreate(); super.onCreate();
SharedPreferences prefs = DirectBootAwarePreferences.get_shared_preferences(this); SharedPreferences prefs = DirectBootAwarePreferences.get_shared_preferences(this);
prefs.registerOnSharedPreferenceChangeListener(this); prefs.registerOnSharedPreferenceChangeListener(this);
Config.initGlobalConfig(prefs, getResources(), new KeyEventHandler(this.new Receiver())); _keyeventhandler = new KeyEventHandler(getMainLooper(), this.new Receiver());
Config.initGlobalConfig(prefs, getResources(), _keyeventhandler);
_config = Config.globalConfig(); _config = Config.globalConfig();
_keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard); _keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard);
_keyboardView.reset(); _keyboardView.reset();
_debug_logs = getResources().getBoolean(R.bool.debug_logs); _debug_logs = getResources().getBoolean(R.bool.debug_logs);
_autocap = new Autocapitalisation(getMainLooper(), this);
}
public void update_shift_state(boolean should_enable, boolean should_disable)
{
if (should_enable)
_keyboardView.set_shift_state(true, false);
else if (should_disable)
_keyboardView.set_shift_state(false, false);
} }
private List<InputMethodSubtype> getEnabledSubtypes(InputMethodManager imm) private List<InputMethodSubtype> getEnabledSubtypes(InputMethodManager imm)
@ -187,14 +178,14 @@ public class Keyboard2 extends InputMethodService
if (info.actionLabel != null) if (info.actionLabel != null)
{ {
_config.actionLabel = info.actionLabel.toString(); _config.actionLabel = info.actionLabel.toString();
_config.actionId = info.actionId; _keyeventhandler.actionId = info.actionId;
_config.swapEnterActionKey = false; _config.swapEnterActionKey = false;
} }
else else
{ {
int action = info.imeOptions & EditorInfo.IME_MASK_ACTION; int action = info.imeOptions & EditorInfo.IME_MASK_ACTION;
_config.actionLabel = actionLabel_of_imeAction(action); // Might be null _config.actionLabel = actionLabel_of_imeAction(action); // Might be null
_config.actionId = action; _keyeventhandler.actionId = action;
_config.swapEnterActionKey = _config.swapEnterActionKey =
(info.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0; (info.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0;
} }
@ -243,7 +234,7 @@ public class Keyboard2 extends InputMethodService
refreshConfig(); refreshConfig();
refresh_action_label(info); refresh_action_label(info);
_keyboardView.setKeyboard(getLayout(chooseLayout(info))); _keyboardView.setKeyboard(getLayout(chooseLayout(info)));
_autocap.started(info, getCurrentInputConnection()); _keyeventhandler.started(info);
setInputView(_keyboardView); setInputView(_keyboardView);
if (_debug_logs) if (_debug_logs)
log_editor_info(info); log_editor_info(info);
@ -269,7 +260,7 @@ public class Keyboard2 extends InputMethodService
public void onUpdateSelection(int oldSelStart, int oldSelEnd, int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd) public void onUpdateSelection(int oldSelStart, int oldSelEnd, int newSelStart, int newSelEnd, int candidatesStart, int candidatesEnd)
{ {
super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart, candidatesEnd); super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart, candidatesEnd);
_autocap.selection_updated(oldSelStart, newSelStart); _keyeventhandler.selection_updated(oldSelStart, newSelStart);
} }
@Override @Override
@ -315,35 +306,22 @@ public class Keyboard2 extends InputMethodService
setInputView(_keyboardView); setInputView(_keyboardView);
} }
public void performAction() public void set_shift_state(boolean state, boolean lock)
{ {
InputConnection conn = getCurrentInputConnection(); _keyboardView.set_shift_state(state, lock);
if (conn == null)
return;
conn.performEditorAction(_config.actionId);
} }
public void enableCapsLock() public void switch_main()
{
_keyboardView.set_shift_state(true, true);
}
public void switchMain()
{ {
_keyboardView.setKeyboard(getLayout(_currentTextLayout)); _keyboardView.setKeyboard(getLayout(_currentTextLayout));
} }
public void switchNumeric() public void switch_layout(int layout_id)
{ {
_keyboardView.setKeyboard(getLayout(R.xml.numeric)); _keyboardView.setKeyboard(getLayout(layout_id));
} }
public void switchGreekmath() public void switch_second()
{
_keyboardView.setKeyboard(getLayout(R.xml.greekmath));
}
public void switchSecond()
{ {
if (_config.second_layout == -1) if (_config.second_layout == -1)
return; return;
@ -360,16 +338,6 @@ public class Keyboard2 extends InputMethodService
_keyboardView.setKeyboard(layout); _keyboardView.setKeyboard(layout);
} }
public void sendKeyEvent(int eventAction, int eventCode, int meta)
{
InputConnection conn = 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);
}
public void showKeyboardConfig() public void showKeyboardConfig()
{ {
Intent intent = new Intent(Keyboard2.this, SettingsActivity.class); Intent intent = new Intent(Keyboard2.this, SettingsActivity.class);
@ -377,16 +345,9 @@ public class Keyboard2 extends InputMethodService
startActivity(intent); startActivity(intent);
} }
public void commitText(String text) public InputConnection getCurrentInputConnection()
{ {
getCurrentInputConnection().commitText(text, 1); return Keyboard2.this.getCurrentInputConnection();
_autocap.typed(text);
}
public void commitChar(char c)
{
sendKeyChar(c);
_autocap.typed(c);
} }
} }

View File

@ -126,13 +126,13 @@ public class Keyboard2View extends View
public void onPointerUp(KeyValue k, Pointers.Modifiers mods) public void onPointerUp(KeyValue k, Pointers.Modifiers mods)
{ {
_config.handler.handleKeyUp(k, mods); _config.handler.key_up(k, mods);
invalidate(); invalidate();
} }
public void onPointerHold(KeyValue k, Pointers.Modifiers mods) public void onPointerHold(KeyValue k, Pointers.Modifiers mods)
{ {
_config.handler.handleKeyUp(k, mods); _config.handler.key_up(k, mods);
} }
public void onPointerFlagsChanged(boolean shouldVibrate) public void onPointerFlagsChanged(boolean shouldVibrate)