Separate "handler" code

As with the previous commit, remove casts of the context.
The "handler" object is referenced in the "config" object for now.
This commit is contained in:
Jules Aguillon 2021-12-28 17:47:18 +01:00
parent 0190cfc29a
commit 15ce200ce3
6 changed files with 139 additions and 73 deletions

View File

@ -34,7 +34,9 @@ final class Config
public boolean shouldOfferSwitchingToNextInputMethod;
public int accent_flags_to_remove;
private Config(Context context)
public final IKeyEventHandler handler;
private Config(Context context, IKeyEventHandler h)
{
Resources res = context.getResources();
// static values
@ -61,6 +63,7 @@ final class Config
// initialized later
shouldOfferSwitchingToNextInputMethod = false;
accent_flags_to_remove = 0;
handler = h;
}
/*
@ -120,13 +123,18 @@ final class Config
private static Config _globalConfig = null;
public static void initGlobalConfig(Context context)
public static void initGlobalConfig(Context context, IKeyEventHandler handler)
{
_globalConfig = new Config(context);
_globalConfig = new Config(context, handler);
}
public static Config globalConfig()
{
return _globalConfig;
}
public static interface IKeyEventHandler
{
public void handleKeyUp(KeyValue value, int flags);
}
}

View File

@ -48,16 +48,15 @@ public class EmojiGridView extends GridView
public void setEmojiGroup(int group)
{
_emojiArray = (group == GROUP_LAST_USE) ? getLastEmojis() : Emoji.getEmojisByGroup(group);
setAdapter(new EmojiViewAdpater((Keyboard2)getContext(), _emojiArray));
setAdapter(new EmojiViewAdpater(getContext(), _emojiArray));
}
public void onItemClick(AdapterView<?> parent, View v, int pos, long id)
{
Keyboard2 main = (Keyboard2)getContext();
Config config = Config.globalConfig();
Integer used = _lastUsed.get(_emojiArray[pos]);
_lastUsed.put(_emojiArray[pos], (used == null) ? 1 : used.intValue() + 1);
main.handleKeyUp(_emojiArray[pos], 0);
config.handler.handleKeyUp(_emojiArray[pos], 0);
saveLastUsed(); // TODO: opti
}
@ -118,7 +117,7 @@ public class EmojiGridView extends GridView
private static class EmojiView extends TextView
{
public EmojiView(Keyboard2 context)
public EmojiView(Context context)
{
super(context);
setTextSize(EMOJI_SIZE);
@ -136,13 +135,13 @@ public class EmojiGridView extends GridView
private static class EmojiViewAdpater extends BaseAdapter
{
private Keyboard2 _main;
private Context _context;
private Emoji[] _emojiArray;
public EmojiViewAdpater(Keyboard2 main, Emoji[] emojiArray)
public EmojiViewAdpater(Context context, Emoji[] emojiArray)
{
_main = main;
_context = context;
_emojiArray = emojiArray;
}
@ -168,7 +167,7 @@ public class EmojiGridView extends GridView
EmojiView view = (EmojiView)convertView;
if (view == null)
view = new EmojiView(_main);
view = new EmojiView(_context);
view.setEmoji(_emojiArray[pos]);
return (view);
}

View File

@ -22,8 +22,7 @@ public class EmojiKeyButton extends Button
public void onClick(View v)
{
Keyboard2 main = (Keyboard2)getContext();
main.handleKeyUp(_key, 0);
Config config = Config.globalConfig();
config.handler.handleKeyUp(_key, 0);
}
}

View File

@ -0,0 +1,76 @@
package juloo.keyboard2;
import android.view.KeyEvent;
class KeyEventHandler implements Config.IKeyEventHandler
{
private IReceiver _recv;
public KeyEventHandler(IReceiver recv)
{
_recv = recv;
}
public void handleKeyUp(KeyValue key, int flags)
{
key = KeyModifier.handleFlags(key, flags);
switch (key.eventCode)
{
case KeyValue.EVENT_CONFIG: _recv.showKeyboardConfig(); return;
case KeyValue.EVENT_SWITCH_TEXT: _recv.setLayout(-1); return;
case KeyValue.EVENT_SWITCH_NUMERIC: _recv.setLayout(R.xml.numeric); return;
case KeyValue.EVENT_SWITCH_EMOJI: _recv.setPane_emoji(); return;
case KeyValue.EVENT_SWITCH_BACK_EMOJI: _recv.setPane_normal(); return;
case KeyValue.EVENT_CHANGE_METHOD: _recv.switchToNextInputMethod(); return;
default:
if ((flags & (KeyValue.FLAG_CTRL | KeyValue.FLAG_ALT)) != 0)
handleMetaKeyUp(key, flags);
else if (key.char_ != KeyValue.CHAR_NONE)
_recv.commitChar(key.char_);
else if (key.eventCode != KeyValue.EVENT_NONE)
handleMetaKeyUp(key, flags);
else
_recv.commitText(key.symbol);
}
}
// private void handleDelKey(int before, int after)
// {
// CharSequence selection = getCurrentInputConnection().getSelectedText(0);
// if (selection != null && selection.length() > 0)
// getCurrentInputConnection().commitText("", 1);
// else
// getCurrentInputConnection().deleteSurroundingText(before, after);
// }
private void handleMetaKeyUp(KeyValue key, int flags)
{
int meta = 0;
if (key.eventCode == KeyValue.EVENT_NONE)
return ;
if ((flags & KeyValue.FLAG_CTRL) != 0)
meta |= KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON;
if ((flags & KeyValue.FLAG_ALT) != 0)
meta |= KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_ON;
if ((flags & KeyValue.FLAG_SHIFT) != 0)
meta |= KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON;
_recv.sendKeyEvent(key.eventCode, meta);
}
public static interface IReceiver
{
public void switchToNextInputMethod();
public void setPane_emoji();
public void setPane_normal();
public void showKeyboardConfig();
/** 'res_id' is '-1' for the currently selected layout. */
public void setLayout(int res_id);
public void sendKeyEvent(int eventCode, int meta);
public void commitText(String text);
public void commitChar(char c);
}
}

View File

@ -12,6 +12,7 @@ import android.os.IBinder;
import android.text.InputType;
import android.preference.PreferenceManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
@ -54,7 +55,7 @@ public class Keyboard2 extends InputMethodService
_specialKeyFont = Typeface.createFromAsset(getAssets(), "fonts/keys.ttf");
PreferenceManager.setDefaultValues(this, R.xml.settings, false);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
Config.initGlobalConfig(this);
Config.initGlobalConfig(this, new KeyEventHandler(this.new Receiver()));
_config = Config.globalConfig();
_keyboardView = (Keyboard2View)getLayoutInflater().inflate(R.layout.keyboard, null);
_keyboardView.reset();
@ -193,77 +194,60 @@ public class Keyboard2 extends InputMethodService
_keyboardView.reset();
}
public void handleKeyUp(KeyValue key, int flags)
/** Not static */
public class Receiver implements KeyEventHandler.IReceiver
{
if (getCurrentInputConnection() == null)
return ;
key = KeyModifier.handleFlags(key, flags);
if (key.eventCode == KeyValue.EVENT_CONFIG)
public void switchToNextInputMethod()
{
Intent intent = new Intent(this, SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.switchToNextInputMethod(getConnectionToken(), false);
}
else if (key.eventCode == KeyValue.EVENT_SWITCH_TEXT)
_keyboardView.setKeyboard(getLayout(_currentTextLayout));
else if (key.eventCode == KeyValue.EVENT_SWITCH_NUMERIC)
_keyboardView.setKeyboard(getLayout(R.xml.numeric));
else if (key.eventCode == KeyValue.EVENT_SWITCH_EMOJI)
public void setPane_emoji()
{
if (_emojiPane == null)
_emojiPane = (ViewGroup)getLayoutInflater().inflate(R.layout.emoji_pane, null);
setInputView(_emojiPane);
}
else if (key.eventCode == KeyValue.EVENT_SWITCH_BACK_EMOJI)
public void setPane_normal()
{
setInputView(_keyboardView);
else if (key.eventCode == KeyValue.EVENT_CHANGE_METHOD)
{
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.switchToNextInputMethod(getConnectionToken(), false);
}
else if ((flags & (KeyValue.FLAG_CTRL | KeyValue.FLAG_ALT)) != 0)
handleMetaKeyUp(key, flags);
// else if (eventCode == KeyEvent.KEYCODE_DEL)
// handleDelKey(1, 0);
// else if (eventCode == KeyEvent.KEYCODE_FORWARD_DEL)
// handleDelKey(0, 1);
else if (key.char_ == KeyValue.CHAR_NONE)
{
if (key.eventCode != KeyValue.EVENT_NONE)
handleMetaKeyUp(key, flags);
else
getCurrentInputConnection().commitText(key.symbol, 1);
}
else
sendKeyChar(key.char_);
}
// private void handleDelKey(int before, int after)
// {
// CharSequence selection = getCurrentInputConnection().getSelectedText(0);
// if (selection != null && selection.length() > 0)
// getCurrentInputConnection().commitText("", 1);
// else
// getCurrentInputConnection().deleteSurroundingText(before, after);
// }
private void handleMetaKeyUp(KeyValue key, int flags)
public void setLayout(int res_id)
{
int metaState = 0;
KeyEvent event;
if (res_id == -1)
res_id = _currentTextLayout;
_keyboardView.setKeyboard(getLayout(res_id));
}
if (key.eventCode == KeyValue.EVENT_NONE)
public void sendKeyEvent(int eventCode, int meta)
{
InputConnection conn = getCurrentInputConnection();
if (conn == null)
return;
if ((flags & KeyValue.FLAG_CTRL) != 0)
metaState |= KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON;
if ((flags & KeyValue.FLAG_ALT) != 0)
metaState |= KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_ON;
if ((flags & KeyValue.FLAG_SHIFT) != 0)
metaState |= KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON;
event = new KeyEvent(1, 1, KeyEvent.ACTION_DOWN, key.eventCode, 0, metaState);
getCurrentInputConnection().sendKeyEvent(event);
getCurrentInputConnection().sendKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP));
KeyEvent event = new KeyEvent(1, 1, KeyEvent.ACTION_DOWN, eventCode, 0, meta);
conn.sendKeyEvent(event);
conn.sendKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP));
}
public void showKeyboardConfig()
{
Intent intent = new Intent(Keyboard2.this, SettingsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
public void commitText(String text)
{
getCurrentInputConnection().commitText(text, 1);
}
public void commitChar(char c)
{
sendKeyChar(c);
}
}
private IBinder getConnectionToken()

View File

@ -266,7 +266,7 @@ public class Keyboard2View extends View
private void handleKeyUp(KeyDown key)
{
if (key.value != null && (key.flags & (KeyValue.FLAG_LOCKED | KeyValue.FLAG_NOCHAR)) == 0)
((Keyboard2)getContext()).handleKeyUp(key.value, _flags);
_config.handler.handleKeyUp(key.value, _flags);
}
private void handleKeyDown(KeyValue key)
@ -317,7 +317,7 @@ public class Keyboard2View extends View
nextInterval = (long)((float)nextInterval / accel);
}
_handler.sendEmptyMessageDelayed(msg.what, nextInterval);
((Keyboard2)getContext()).handleKeyUp(key.value, _flags);
_config.handler.handleKeyUp(key.value, _flags);
return (true);
}
}