diff options
| author | Jules Aguillon | 2021-04-18 23:28:49 +0200 |
|---|---|---|
| committer | Jules Aguillon | 2021-04-18 23:28:49 +0200 |
| commit | 81803c406a82862602aa5e350fcc07692e9f16a6 (patch) | |
| tree | 55d0a58f6c37f0707d1d0988d1e937c514bbf3e2 | |
| parent | 67d3a99a92a457e26a987bd722ce3b46e171b2b1 (diff) | |
| download | unexpected-keyboard-81803c406a82862602aa5e350fcc07692e9f16a6.tar.gz unexpected-keyboard-81803c406a82862602aa5e350fcc07692e9f16a6.zip | |
Separate handling of modifiers from KeyValue class
KeyValue defines an ADT and some of its values, it now contains public
final fields and no internal logic.
KeyModifier handles modifiers and accents and creates new instances of
KeyValue when needed.
This operation is now properly cached.
| -rw-r--r-- | srcs/juloo.keyboard2/EmojiGridView.java | 4 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/EmojiGroupButtonsBar.java | 2 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/EmojiKeyButton.java | 4 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/KeyModifier.java | 70 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/KeyValue.java | 159 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/Keyboard2.java | 28 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/Keyboard2View.java | 22 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/KeyboardData.java | 4 |
8 files changed, 155 insertions, 138 deletions
diff --git a/srcs/juloo.keyboard2/EmojiGridView.java b/srcs/juloo.keyboard2/EmojiGridView.java index 1bfceb3..58a0911 100644 --- a/srcs/juloo.keyboard2/EmojiGridView.java +++ b/srcs/juloo.keyboard2/EmojiGridView.java @@ -90,7 +90,7 @@ public class EmojiGridView extends GridView HashSet<String> set = new HashSet<String>(); for (Emoji emoji : _lastUsed.keySet()) - set.add(String.valueOf(_lastUsed.get(emoji)) + "-" + emoji.getName()); + set.add(String.valueOf(_lastUsed.get(emoji)) + "-" + emoji.name); edit.putStringSet(LAST_USE_PREF, set); edit.apply(); } @@ -130,7 +130,7 @@ public class EmojiGridView extends GridView public void setEmoji(Emoji emoji) { - setText(emoji.getSymbol(0)); + setText(emoji.symbol); } } diff --git a/srcs/juloo.keyboard2/EmojiGroupButtonsBar.java b/srcs/juloo.keyboard2/EmojiGroupButtonsBar.java index 3d6f8de..7729b77 100644 --- a/srcs/juloo.keyboard2/EmojiGroupButtonsBar.java +++ b/srcs/juloo.keyboard2/EmojiGroupButtonsBar.java @@ -15,7 +15,7 @@ public class EmojiGroupButtonsBar extends LinearLayout for (int i = 0; i < Emoji.num_groups; i++) { Emoji first = Emoji.getEmojisByGroup(i)[0]; - add_group(i, first.getSymbol(0)); + add_group(i, first.symbol); } } diff --git a/srcs/juloo.keyboard2/EmojiKeyButton.java b/srcs/juloo.keyboard2/EmojiKeyButton.java index 00a9a69..de9f206 100644 --- a/srcs/juloo.keyboard2/EmojiKeyButton.java +++ b/srcs/juloo.keyboard2/EmojiKeyButton.java @@ -15,8 +15,8 @@ public class EmojiKeyButton extends Button super(context, attrs); setOnClickListener(this); _key = KeyValue.getKeyByName(attrs.getAttributeValue(null, "key")); - setText(_key.getSymbol(0)); - if ((_key.getFlags() & KeyValue.FLAG_KEY_FONT) != 0) + setText(_key.symbol); + if ((_key.flags & KeyValue.FLAG_KEY_FONT) != 0) setTypeface(((Keyboard2)context).getSpecialKeyFont()); } diff --git a/srcs/juloo.keyboard2/KeyModifier.java b/srcs/juloo.keyboard2/KeyModifier.java new file mode 100644 index 0000000..d14d254 --- /dev/null +++ b/srcs/juloo.keyboard2/KeyModifier.java @@ -0,0 +1,70 @@ +package juloo.keyboard2; + +import android.util.SparseArray; +import android.view.KeyCharacterMap; +import java.util.HashMap; + +class KeyModifier +{ + /* Cache key is KeyValue's name */ + private static HashMap<String, SparseArray<KeyValue>> _cache = + new HashMap<String, SparseArray<KeyValue>>(); + + /* Modify a key according to modifiers. */ + public static KeyValue handleFlags(KeyValue k, int flags) + { + if (flags == 0) // No modifier + return k; + SparseArray<KeyValue> ks = cacheEntry(k); + KeyValue r = ks.get(flags); + if (r != null) // Found in cache + return r; + char c = handleChar(k.char_, flags); + if (c == k.char_) // Don't override the symbol if the char didn't change + r = k; + else + r = k.withCharAndSymbol(String.valueOf(c), c); + ks.put(flags, r); + return r; + } + + private static char handleChar(char c, int flags) + { + if (c == KeyValue.CHAR_NONE) + return c; + if ((flags & KeyValue.FLAG_SHIFT) != 0) // Shift + c = Character.toUpperCase(c); + if ((flags & KeyValue.FLAGS_ACCENTS) != 0) // Accents, after shift is applied + c = handleAccentChar(c, flags); + return c; + } + + private static char handleAccentChar(char c, int flags) + { + char accent; + switch ((flags & KeyValue.FLAGS_ACCENTS)) + { + case KeyValue.FLAG_ACCENT1: accent = '\u02CB'; break; + case KeyValue.FLAG_ACCENT2: accent = '\u00B4'; break; + case KeyValue.FLAG_ACCENT3: accent = '\u02C6'; break; + case KeyValue.FLAG_ACCENT4: accent = '\u02DC'; break; + case KeyValue.FLAG_ACCENT5: accent = '\u00B8'; break; + case KeyValue.FLAG_ACCENT6: accent = '\u00A8'; break; + default: return c; // Can't happen + } + char r = (char)KeyCharacterMap.getDeadChar(accent, (int)c); + return (r == 0) ? c : r; + } + + /* Lookup the cache entry for a key. Create it needed. */ + private static SparseArray<KeyValue> cacheEntry(KeyValue k) + { + SparseArray<KeyValue> ks = _cache.get(k.name); + if (ks == null) + { + ks = new SparseArray<KeyValue>(); + _cache.put(k.name, ks); + } + return ks; + } +} diff --git a/srcs/juloo.keyboard2/KeyValue.java b/srcs/juloo.keyboard2/KeyValue.java index 1f6d0a8..4c4a5a0 100644 --- a/srcs/juloo.keyboard2/KeyValue.java +++ b/srcs/juloo.keyboard2/KeyValue.java @@ -6,115 +6,62 @@ import java.util.HashMap; class KeyValue { - public static final int EVENT_NONE = -1; - public static final int EVENT_CONFIG = -2; - public static final int EVENT_SWITCH_TEXT = -3; - public static final int EVENT_SWITCH_NUMERIC = -4; - public static final int EVENT_SWITCH_EMOJI = -5; - public static final int EVENT_SWITCH_BACK_EMOJI = -6; - public static final int EVENT_CHANGE_METHOD = -7; - public static final char CHAR_NONE = '\0'; - - public static final int FLAG_KEEP_ON = 1; - public static final int FLAG_LOCK = (1 << 1); - public static final int FLAG_CTRL = (1 << 2); - public static final int FLAG_SHIFT = (1 << 3); - public static final int FLAG_ALT = (1 << 4); - public static final int FLAG_NOREPEAT = (1 << 5); - public static final int FLAG_NOCHAR = (1 << 6); - public static final int FLAG_LOCKED = (1 << 8); - - public static final int FLAG_KEY_FONT = (1 << 12); - - public static final int FLAG_ACCENT1 = (1 << 16); - public static final int FLAG_ACCENT2 = (1 << 17); - public static final int FLAG_ACCENT3 = (1 << 18); - public static final int FLAG_ACCENT4 = (1 << 19); - public static final int FLAG_ACCENT5 = (1 << 20); - public static final int FLAG_ACCENT6 = (1 << 21); + public static final int EVENT_NONE = -1; + public static final int EVENT_CONFIG = -2; + public static final int EVENT_SWITCH_TEXT = -3; + public static final int EVENT_SWITCH_NUMERIC = -4; + public static final int EVENT_SWITCH_EMOJI = -5; + public static final int EVENT_SWITCH_BACK_EMOJI = -6; + public static final int EVENT_CHANGE_METHOD = -7; + public static final char CHAR_NONE = '\0'; + + public static final int FLAG_KEEP_ON = 1; + public static final int FLAG_LOCK = (1 << 1); + public static final int FLAG_CTRL = (1 << 2); + public static final int FLAG_SHIFT = (1 << 3); + public static final int FLAG_ALT = (1 << 4); + public static final int FLAG_NOREPEAT = (1 << 5); + public static final int FLAG_NOCHAR = (1 << 6); + public static final int FLAG_LOCKED = (1 << 8); + + public static final int FLAG_KEY_FONT = (1 << 12); + + public static final int FLAG_ACCENT1 = (1 << 16); + public static final int FLAG_ACCENT2 = (1 << 17); + public static final int FLAG_ACCENT3 = (1 << 18); + public static final int FLAG_ACCENT4 = (1 << 19); + public static final int FLAG_ACCENT5 = (1 << 20); + public static final int FLAG_ACCENT6 = (1 << 21); public static final int FLAGS_ACCENTS = FLAG_ACCENT1 | FLAG_ACCENT2 | FLAG_ACCENT3 | FLAG_ACCENT4 | FLAG_ACCENT5 | FLAG_ACCENT6; - private final String _name; - private final String _symbol; - private final char _char; - private final int _eventCode; - private final int _flags; - - private int _cacheFlags; - private String _cacheSymbol; - - public String getName() - { - return (_name); - } - - public String getSymbol(int flags) - { - if (_symbol == null) - { - if (flags != _cacheFlags) - { - _cacheSymbol = String.valueOf(getChar(flags)); - _cacheFlags = flags; - } - return (_cacheSymbol); - } - return (_symbol); - } - - public char getChar(int flags) - { - if (flags != 0) - { - char c = _char; - if ((flags & FLAG_SHIFT) != 0) - c = Character.toUpperCase(_char); - if ((flags & FLAG_ACCENT1) != 0) - c = (char)KeyCharacterMap.getDeadChar('\u02CB', (int)c); - if ((flags & FLAG_ACCENT2) != 0) - c = (char)KeyCharacterMap.getDeadChar('\u00B4', (int)c); - if ((flags & FLAG_ACCENT3) != 0) - c = (char)KeyCharacterMap.getDeadChar('\u02C6', (int)c); - if ((flags & FLAG_ACCENT4) != 0) - c = (char)KeyCharacterMap.getDeadChar('\u02DC', (int)c); - if ((flags & FLAG_ACCENT5) != 0) - c = (char)KeyCharacterMap.getDeadChar('\u00B8', (int)c); - if ((flags & FLAG_ACCENT6) != 0) - c = (char)KeyCharacterMap.getDeadChar('\u00A8', (int)c); - if (c != 0) - return (c); - } - return (_char); - } - - public int getEventCode() - { - return (_eventCode); - } - - public int getFlags() - { - return (_flags); - } - - private static HashMap<String, KeyValue> keys = new HashMap<String, KeyValue>(); - - protected KeyValue(String name, String symbol, char c, int eventCode, int flags) - { - _name = name; - _symbol = symbol; - _char = c; - _eventCode = eventCode; - _flags = flags; - _cacheFlags = -1; - } - - public static KeyValue getKeyByName(String name) - { - return (KeyValue.keys.get(name)); - } + public final String name; + public final String symbol; + public final char char_; + public final int eventCode; + public final int flags; + + public KeyValue withCharAndSymbol(String s, char c) + { + return new KeyValue(name, s, c, eventCode, flags); + } + + private static HashMap<String, KeyValue> keys = new HashMap<String, KeyValue>(); + + protected KeyValue(String n, String s, char c, int e, int f) + { + name = n; + symbol = s; + char_ = c; + eventCode = e; + flags = f; + } + + public static KeyValue getKeyByName(String name) + { + return (KeyValue.keys.get(name)); + } private static void addKey(String name, String symbol, char c, int event, int flags) { @@ -215,6 +162,6 @@ class KeyValue addKey("insert", "Ins", CHAR_NONE, KeyEvent.KEYCODE_INSERT, 0); addKey("tab", "↹", '\t', KeyEvent.KEYCODE_TAB, 0); - addKey("space", null, ' ', KeyEvent.KEYCODE_SPACE, 0); + addKey("space", " ", ' ', KeyEvent.KEYCODE_SPACE, 0); } } diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index f94ef3a..0be7913 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -130,30 +130,28 @@ public class Keyboard2 extends InputMethodService public void handleKeyUp(KeyValue key, int flags) { - int eventCode = key.getEventCode(); - char keyChar = key.getChar(flags); - if (getCurrentInputConnection() == null) return ; - if (eventCode == KeyValue.EVENT_CONFIG) + key = KeyModifier.handleFlags(key, flags); + if (key.eventCode == KeyValue.EVENT_CONFIG) { Intent intent = new Intent(this, SettingsActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } - else if (eventCode == KeyValue.EVENT_SWITCH_TEXT) + else if (key.eventCode == KeyValue.EVENT_SWITCH_TEXT) _keyboardView.setKeyboard(getLayout(_currentTextLayout)); - else if (eventCode == KeyValue.EVENT_SWITCH_NUMERIC) + else if (key.eventCode == KeyValue.EVENT_SWITCH_NUMERIC) _keyboardView.setKeyboard(getLayout(R.xml.numeric)); - else if (eventCode == KeyValue.EVENT_SWITCH_EMOJI) + else if (key.eventCode == KeyValue.EVENT_SWITCH_EMOJI) { if (_emojiPane == null) _emojiPane = (ViewGroup)getLayoutInflater().inflate(R.layout.emoji_pane, null); setInputView(_emojiPane); } - else if (eventCode == KeyValue.EVENT_SWITCH_BACK_EMOJI) + else if (key.eventCode == KeyValue.EVENT_SWITCH_BACK_EMOJI) setInputView(_keyboardView); - else if (eventCode == KeyValue.EVENT_CHANGE_METHOD) + else if (key.eventCode == KeyValue.EVENT_CHANGE_METHOD) { InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); @@ -165,15 +163,15 @@ public class Keyboard2 extends InputMethodService // handleDelKey(1, 0); // else if (eventCode == KeyEvent.KEYCODE_FORWARD_DEL) // handleDelKey(0, 1); - else if (keyChar == KeyValue.CHAR_NONE) + else if (key.char_ == KeyValue.CHAR_NONE) { - if (eventCode != KeyValue.EVENT_NONE) + if (key.eventCode != KeyValue.EVENT_NONE) handleMetaKeyUp(key, flags); else - getCurrentInputConnection().commitText(key.getSymbol(flags), 1); + getCurrentInputConnection().commitText(key.symbol, 1); } else - sendKeyChar(keyChar); + sendKeyChar(key.char_); } // private void handleDelKey(int before, int after) @@ -191,7 +189,7 @@ public class Keyboard2 extends InputMethodService int metaState = 0; KeyEvent event; - if (key.getEventCode() == KeyValue.EVENT_NONE) + if (key.eventCode == KeyValue.EVENT_NONE) return ; if ((flags & KeyValue.FLAG_CTRL) != 0) metaState |= KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON; @@ -199,7 +197,7 @@ public class Keyboard2 extends InputMethodService 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.getEventCode(), 1, metaState); + event = new KeyEvent(1, 1, KeyEvent.ACTION_DOWN, key.eventCode, 1, metaState); getCurrentInputConnection().sendKeyEvent(event); getCurrentInputConnection().sendKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP)); } diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java index 1dbfb87..d8a24d8 100644 --- a/srcs/juloo.keyboard2/Keyboard2View.java +++ b/srcs/juloo.keyboard2/Keyboard2View.java @@ -172,11 +172,11 @@ public class Keyboard2View extends View if (key.timeoutWhat != -1) { _handler.removeMessages(key.timeoutWhat); - if ((newValue.getFlags() & KeyValue.FLAG_NOREPEAT) == 0) + if ((newValue.flags & KeyValue.FLAG_NOREPEAT) == 0) _handler.sendEmptyMessageDelayed(key.timeoutWhat, _config.longPressTimeout); } key.value = newValue; - key.flags = newValue.getFlags(); + key.flags = newValue.flags; updateFlags(); invalidate(); handleKeyDown(newValue); @@ -213,7 +213,7 @@ public class Keyboard2View extends View else { int what = _currentWhat++; - if (key.key0 != null && (key.key0.getFlags() & KeyValue.FLAG_NOREPEAT) == 0) + if (key.key0 != null && (key.key0.flags & KeyValue.FLAG_NOREPEAT) == 0) _handler.sendEmptyMessageDelayed(what, _config.longPressTimeout); _downKeys.add(new KeyDown(pointerId, key, touchX, touchY, what)); } @@ -376,18 +376,20 @@ public class Keyboard2View extends View private void drawLabel(Canvas canvas, KeyValue k, float x, float y, boolean locked) { - if ((k.getFlags() & KeyValue.FLAG_KEY_FONT) != 0) - canvas.drawText(k.getSymbol(_flags), x, y, locked ? _specialKeyLabelLockedPaint : _specialKeyLabelPaint); + k = KeyModifier.handleFlags(k, _flags); + if ((k.flags & KeyValue.FLAG_KEY_FONT) != 0) + canvas.drawText(k.symbol, x, y, locked ? _specialKeyLabelLockedPaint : _specialKeyLabelPaint); else - canvas.drawText(k.getSymbol(_flags), x, y, locked ? _keyLabelLockedPaint : _keyLabelPaint); + canvas.drawText(k.symbol, x, y, locked ? _keyLabelLockedPaint : _keyLabelPaint); } private void drawSubLabel(Canvas canvas, KeyValue k, float x, float y, boolean right) { - if ((k.getFlags() & KeyValue.FLAG_KEY_FONT) != 0) - canvas.drawText(k.getSymbol(_flags), x, y, right ? _specialKeySubLabelRightPaint : _specialKeySubLabelPaint); + k = KeyModifier.handleFlags(k, _flags); + if ((k.flags & KeyValue.FLAG_KEY_FONT) != 0) + canvas.drawText(k.symbol, x, y, right ? _specialKeySubLabelRightPaint : _specialKeySubLabelPaint); else - canvas.drawText(k.getSymbol(_flags), x, y, right ? _keySubLabelRightPaint : _keySubLabelPaint); + canvas.drawText(k.symbol, x, y, right ? _keySubLabelRightPaint : _keySubLabelPaint); } private static class KeyDown @@ -407,7 +409,7 @@ public class Keyboard2View extends View this.key = key; downX = x; downY = y; - flags = (value == null) ? 0 : value.getFlags(); + flags = (value == null) ? 0 : value.flags; timeoutWhat = what; } } diff --git a/srcs/juloo.keyboard2/KeyboardData.java b/srcs/juloo.keyboard2/KeyboardData.java index c137af2..2356954 100644 --- a/srcs/juloo.keyboard2/KeyboardData.java +++ b/srcs/juloo.keyboard2/KeyboardData.java @@ -168,7 +168,7 @@ class KeyboardData public KeyValue map(KeyValue k) { - return (k == null || (k.getFlags() & _flags) != 0) ? null : k; + return (k == null || (k.flags & _flags) != 0) ? null : k; } } @@ -180,7 +180,7 @@ class KeyboardData public KeyValue map(KeyValue k) { - return (k == null || k.getEventCode() == _eventCode) ? null : k; + return (k == null || k.eventCode == _eventCode) ? null : k; } } } |
