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.
This commit is contained in:
Jules Aguillon 2023-06-03 20:06:44 +02:00
parent bd9e25d298
commit a83a19a0a8
2 changed files with 82 additions and 111 deletions

View File

@ -10,8 +10,6 @@ class KeyEventHandler implements Config.IKeyEventHandler
IReceiver _recv; IReceiver _recv;
Autocapitalisation _autocap; Autocapitalisation _autocap;
public int actionId; // Action performed by the Action key.
public KeyEventHandler(Looper looper, IReceiver recv) public KeyEventHandler(Looper looper, IReceiver recv)
{ {
_recv = recv; _recv = recv;
@ -40,28 +38,7 @@ class KeyEventHandler implements Config.IKeyEventHandler
{ {
case Char: send_text(String.valueOf(key.getChar())); break; case Char: send_text(String.valueOf(key.getChar())); break;
case String: send_text(key.getString()); break; case String: send_text(key.getString()); break;
case Event: case Event: _recv.handle_event_key(key.getEvent()); break;
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 Keyevent: case Keyevent:
handleKeyUpWithModifier(key.getKeyevent(), mods); handleKeyUpWithModifier(key.getKeyevent(), mods);
break; break;
@ -170,24 +147,9 @@ class KeyEventHandler implements Config.IKeyEventHandler
conn.performContextMenuAction(id); conn.performContextMenuAction(id);
} }
public enum Layout
{
Current, // The primary or secondary layout
Primary,
Secondary,
Numeric,
Greekmath
}
public static interface IReceiver public static interface IReceiver
{ {
public void switchInputMethod(); public void handle_event_key(KeyValue.Event ev);
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 set_shift_state(boolean state, boolean lock); public void set_shift_state(boolean state, boolean lock);
public InputConnection getCurrentInputConnection(); public InputConnection getCurrentInputConnection();
} }

View File

@ -36,6 +36,7 @@ public class Keyboard2 extends InputMethodService
// Layout associated with the currently selected locale. // Layout associated with the currently selected locale.
private KeyboardData _localeTextLayout; private KeyboardData _localeTextLayout;
private ViewGroup _emojiPane = null; private ViewGroup _emojiPane = null;
public int actionId; // Action performed by the Action key.
private Config _config; private Config _config;
@ -150,9 +151,14 @@ public class Keyboard2 extends InputMethodService
} }
} }
InputMethodManager get_imm()
{
return (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
}
private void refreshSubtypeImm() private void refreshSubtypeImm()
{ {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = get_imm();
if (VERSION.SDK_INT < 28) if (VERSION.SDK_INT < 28)
_config.shouldOfferSwitchingToNextInputMethod = true; _config.shouldOfferSwitchingToNextInputMethod = true;
else else
@ -213,14 +219,14 @@ public class Keyboard2 extends InputMethodService
if (info.actionLabel != null) if (info.actionLabel != null)
{ {
_config.actionLabel = info.actionLabel.toString(); _config.actionLabel = info.actionLabel.toString();
_keyeventhandler.actionId = info.actionId; 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
_keyeventhandler.actionId = action; actionId = action;
_config.swapEnterActionKey = _config.swapEnterActionKey =
(info.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0; (info.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0;
} }
@ -398,46 +404,80 @@ public class Keyboard2 extends InputMethodService
/** Not static */ /** Not static */
public class Receiver implements KeyEventHandler.IReceiver public class Receiver implements KeyEventHandler.IReceiver
{ {
public void switchInputMethod() public void handle_event_key(KeyValue.Event ev)
{ {
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); switch (ev)
imm.showInputMethodPicker(); {
} case CONFIG:
Intent intent = new Intent(Keyboard2.this, SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
break;
public void switchToPrevInputMethod() 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) if (VERSION.SDK_INT < 28)
{ get_imm().switchToLastInputMethod(getConnectionToken());
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.switchToLastInputMethod(getConnectionToken());
}
else else
Keyboard2.this.switchToPreviousInputMethod(); switchToPreviousInputMethod();
} break;
public void switch_voice_typing() case ACTION:
{ InputConnection conn = getCurrentInputConnection();
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); if (conn != null)
SimpleEntry<String, InputMethodSubtype> im = get_voice_typing_im(imm); 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<String, InputMethodSubtype> im = get_voice_typing_im(get_imm());
if (im == null) if (im == null)
return; return;
// Best-effort. Good enough for triggering Google's voice typing. // Best-effort. Good enough for triggering Google's voice typing.
if (VERSION.SDK_INT < 28) if (VERSION.SDK_INT < 28)
Keyboard2.this.switchInputMethod(im.getKey()); switchInputMethod(im.getKey());
else else
Keyboard2.this.switchInputMethod(im.getKey(), im.getValue()); switchInputMethod(im.getKey(), im.getValue());
break;
} }
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) public void set_shift_state(boolean state, boolean lock)
@ -445,37 +485,6 @@ public class Keyboard2 extends InputMethodService
_keyboardView.set_shift_state(state, lock); _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() public InputConnection getCurrentInputConnection()
{ {
return Keyboard2.this.getCurrentInputConnection(); return Keyboard2.this.getCurrentInputConnection();