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);
}
public void typed(char c)
{
type_one_char(c);
callback(false);
}
public void event_sent(int code, int meta)
{
if (meta != 0)

View File

@ -1,16 +1,16 @@
package juloo.keyboard2;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.Configuration;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.KeyEvent;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.HashSet;
final class Config
{
@ -294,6 +294,6 @@ final class Config
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();
Integer used = _lastUsed.get(_emojiArray[pos]);
_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
}

View File

@ -24,6 +24,6 @@ public class EmojiKeyButton extends Button
public void onClick(View v)
{
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;
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());
}
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)
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 sendKeyEvent(int eventAction, int eventCode, int meta);
public void set_shift_state(boolean state, boolean lock);
public void commitText(String text);
public void commitChar(char c);
public InputConnection getCurrentInputConnection();
}
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;
public class Keyboard2 extends InputMethodService
implements SharedPreferences.OnSharedPreferenceChangeListener,
Autocapitalisation.Callback
implements SharedPreferences.OnSharedPreferenceChangeListener
{
static private final String TAG = "Keyboard2";
private Keyboard2View _keyboardView;
private KeyEventHandler _keyeventhandler;
private int _currentTextLayout;
private ViewGroup _emojiPane = null;
private Config _config;
private Autocapitalisation _autocap;
private boolean _debug_logs = false;
@ -50,20 +49,12 @@ public class Keyboard2 extends InputMethodService
super.onCreate();
SharedPreferences prefs = DirectBootAwarePreferences.get_shared_preferences(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();
_keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard);
_keyboardView.reset();
_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)
@ -187,14 +178,14 @@ public class Keyboard2 extends InputMethodService
if (info.actionLabel != null)
{
_config.actionLabel = info.actionLabel.toString();
_config.actionId = info.actionId;
_keyeventhandler.actionId = info.actionId;
_config.swapEnterActionKey = false;
}
else
{
int action = info.imeOptions & EditorInfo.IME_MASK_ACTION;
_config.actionLabel = actionLabel_of_imeAction(action); // Might be null
_config.actionId = action;
_keyeventhandler.actionId = action;
_config.swapEnterActionKey =
(info.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0;
}
@ -243,7 +234,7 @@ public class Keyboard2 extends InputMethodService
refreshConfig();
refresh_action_label(info);
_keyboardView.setKeyboard(getLayout(chooseLayout(info)));
_autocap.started(info, getCurrentInputConnection());
_keyeventhandler.started(info);
setInputView(_keyboardView);
if (_debug_logs)
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)
{
super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd, candidatesStart, candidatesEnd);
_autocap.selection_updated(oldSelStart, newSelStart);
_keyeventhandler.selection_updated(oldSelStart, newSelStart);
}
@Override
@ -315,35 +306,22 @@ public class Keyboard2 extends InputMethodService
setInputView(_keyboardView);
}
public void performAction()
public void set_shift_state(boolean state, boolean lock)
{
InputConnection conn = getCurrentInputConnection();
if (conn == null)
return;
conn.performEditorAction(_config.actionId);
_keyboardView.set_shift_state(state, lock);
}
public void enableCapsLock()
{
_keyboardView.set_shift_state(true, true);
}
public void switchMain()
public void switch_main()
{
_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()
{
_keyboardView.setKeyboard(getLayout(R.xml.greekmath));
}
public void switchSecond()
public void switch_second()
{
if (_config.second_layout == -1)
return;
@ -360,16 +338,6 @@ public class Keyboard2 extends InputMethodService
_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()
{
Intent intent = new Intent(Keyboard2.this, SettingsActivity.class);
@ -377,16 +345,9 @@ public class Keyboard2 extends InputMethodService
startActivity(intent);
}
public void commitText(String text)
public InputConnection getCurrentInputConnection()
{
getCurrentInputConnection().commitText(text, 1);
_autocap.typed(text);
}
public void commitChar(char c)
{
sendKeyChar(c);
_autocap.typed(c);
return Keyboard2.this.getCurrentInputConnection();
}
}

View File

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