abouttreesummaryrefslogcommitdiff
diff options
context:
space:
mode:
authorJules Aguillon2021-12-28 17:47:18 +0100
committerJules Aguillon2021-12-28 17:47:18 +0100
commit15ce200ce3f9d19f1a1f1fb43f176bf511f14271 (patch)
tree9a001552834b4f70f04bea80d1f3bdaaed935eef
parent0190cfc29a07a5a281f0fd1bdac4999ba65c19ba (diff)
downloadunexpected-keyboard-15ce200ce3f9d19f1a1f1fb43f176bf511f14271.tar.gz
unexpected-keyboard-15ce200ce3f9d19f1a1f1fb43f176bf511f14271.zip
Separate "handler" code
As with the previous commit, remove casts of the context. The "handler" object is referenced in the "config" object for now.
-rw-r--r--srcs/juloo.keyboard2/Config.java14
-rw-r--r--srcs/juloo.keyboard2/EmojiGridView.java17
-rw-r--r--srcs/juloo.keyboard2/EmojiKeyButton.java5
-rw-r--r--srcs/juloo.keyboard2/KeyEventHandler.java76
-rw-r--r--srcs/juloo.keyboard2/Keyboard2.java96
-rw-r--r--srcs/juloo.keyboard2/Keyboard2View.java4
6 files changed, 139 insertions, 73 deletions
diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java
index e7a793a..73c43be 100644
--- a/srcs/juloo.keyboard2/Config.java
+++ b/srcs/juloo.keyboard2/Config.java
@@ -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);
+ }
}
diff --git a/srcs/juloo.keyboard2/EmojiGridView.java b/srcs/juloo.keyboard2/EmojiGridView.java
index 4f7ca05..9fc08be 100644
--- a/srcs/juloo.keyboard2/EmojiGridView.java
+++ b/srcs/juloo.keyboard2/EmojiGridView.java
@@ -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);
}
diff --git a/srcs/juloo.keyboard2/EmojiKeyButton.java b/srcs/juloo.keyboard2/EmojiKeyButton.java
index 3f86b1b..6f662b4 100644
--- a/srcs/juloo.keyboard2/EmojiKeyButton.java
+++ b/srcs/juloo.keyboard2/EmojiKeyButton.java
@@ -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);
}
}
diff --git a/srcs/juloo.keyboard2/KeyEventHandler.java b/srcs/juloo.keyboard2/KeyEventHandler.java
new file mode 100644
index 0000000..cba10dc
--- /dev/null
+++ b/srcs/juloo.keyboard2/KeyEventHandler.java
@@ -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);
+ }
+}
diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java
index 7a57def..0eb2a23 100644
--- a/srcs/juloo.keyboard2/Keyboard2.java
+++ b/srcs/juloo.keyboard2/Keyboard2.java
@@ -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)
- setInputView(_keyboardView);
- else if (key.eventCode == KeyValue.EVENT_CHANGE_METHOD)
+
+ public void setPane_normal()
{
- InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
- imm.switchToNextInputMethod(getConnectionToken(), false);
+ setInputView(_keyboardView);
}
- 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)
+
+ public void setLayout(int res_id)
{
- if (key.eventCode != KeyValue.EVENT_NONE)
- handleMetaKeyUp(key, flags);
- else
- getCurrentInputConnection().commitText(key.symbol, 1);
+ if (res_id == -1)
+ res_id = _currentTextLayout;
+ _keyboardView.setKeyboard(getLayout(res_id));
}
- else
- sendKeyChar(key.char_);
- }
- // private void handleDelKey(int before, int after)
- // {
- // CharSequence selection = getCurrentInputConnection().getSelectedText(0);
+ public void sendKeyEvent(int eventCode, int meta)
+ {
+ InputConnection conn = getCurrentInputConnection();
+ if (conn == null)
+ return;
+ KeyEvent event = new KeyEvent(1, 1, KeyEvent.ACTION_DOWN, eventCode, 0, meta);
+ conn.sendKeyEvent(event);
+ conn.sendKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP));
+ }
- // if (selection != null && selection.length() > 0)
- // getCurrentInputConnection().commitText("", 1);
- // else
- // getCurrentInputConnection().deleteSurroundingText(before, after);
- // }
+ public void showKeyboardConfig()
+ {
+ Intent intent = new Intent(Keyboard2.this, SettingsActivity.class);
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ startActivity(intent);
+ }
- private void handleMetaKeyUp(KeyValue key, int flags)
- {
- int metaState = 0;
- KeyEvent event;
+ public void commitText(String text)
+ {
+ getCurrentInputConnection().commitText(text, 1);
+ }
- if (key.eventCode == KeyValue.EVENT_NONE)
- 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));
+ public void commitChar(char c)
+ {
+ sendKeyChar(c);
+ }
}
private IBinder getConnectionToken()
diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java
index 4430df4..2edb745 100644
--- a/srcs/juloo.keyboard2/Keyboard2View.java
+++ b/srcs/juloo.keyboard2/Keyboard2View.java
@@ -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);
}
}