abouttreesummaryrefslogcommitdiff
diff options
context:
space:
mode:
authorJules Aguillon2021-04-15 23:23:31 +0200
committerJules Aguillon2021-04-15 23:23:31 +0200
commitc22ca7302c16773dbe1971323034dd6e23d9611b (patch)
treea5b84667fc1a8dbee93c75d5935246663f7705a9
parenteac74d3f2283ec9f36e595261ab21716d910c15a (diff)
downloadunexpected-keyboard-c22ca7302c16773dbe1971323034dd6e23d9611b.tar.gz
unexpected-keyboard-c22ca7302c16773dbe1971323034dd6e23d9611b.zip
Stateless KeyboardData class
It was a pain to use. It was also a pain to write this though.
-rw-r--r--srcs/juloo.keyboard2/Keyboard2.java16
-rw-r--r--srcs/juloo.keyboard2/Keyboard2View.java9
-rw-r--r--srcs/juloo.keyboard2/KeyboardData.java178
3 files changed, 117 insertions, 86 deletions
diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java
index e036a96..cb1def8 100644
--- a/srcs/juloo.keyboard2/Keyboard2.java
+++ b/srcs/juloo.keyboard2/Keyboard2.java
@@ -34,7 +34,7 @@ public class Keyboard2 extends InputMethodService
PreferenceManager.setDefaultValues(this, R.xml.settings, false);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
_config = new Config(this);
- updateConfig();
+ _numericKeyboard = KeyboardData.parse(getResources().getXml(R.xml.numeric));
_keyboardView = (Keyboard2View)getLayoutInflater().inflate(R.layout.keyboard, null);
_keyboardView.reset();
}
@@ -96,20 +96,6 @@ public class Keyboard2 extends InputMethodService
}
}
- /*
- ** TODO: move this to Config object
- */
- private void updateConfig()
- {
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
-
- _textKeyboard = new KeyboardData(getResources().getXml(_getKeyboardLayoutRes(prefs)));
- if (_config.disableAccentKeys)
- _textKeyboard.removeKeysByFlag(KeyValue.FLAGS_ACCENTS);
- _numericKeyboard = new KeyboardData(getResources().getXml(R.xml.numeric));
- _emojiPane = null;
- }
-
public void handleKeyUp(KeyValue key, int flags)
{
int eventCode = key.getEventCode();
diff --git a/srcs/juloo.keyboard2/Keyboard2View.java b/srcs/juloo.keyboard2/Keyboard2View.java
index 041b881..84159b2 100644
--- a/srcs/juloo.keyboard2/Keyboard2View.java
+++ b/srcs/juloo.keyboard2/Keyboard2View.java
@@ -87,7 +87,10 @@ public class Keyboard2View extends View
public void setKeyboard(KeyboardData keyboard)
{
- _keyboard = keyboard;
+ if (_config.disableAccentKeys)
+ _keyboard = keyboard.removeKeys(new KeyboardData.RemoveKeysByFlags(KeyValue.FLAGS_ACCENTS));
+ else
+ _keyboard = keyboard;
reset();
}
@@ -195,7 +198,7 @@ public class Keyboard2View extends View
if (touchY < y || touchY >= (y + _config.keyHeight))
continue ;
x = (KEY_PER_ROW * _keyWidth - row.getWidth(_keyWidth)) / 2 + _config.horizontalMargin;
- for (KeyboardData.Key key : row)
+ for (KeyboardData.Key key : row.getKeys())
{
keyW = _keyWidth * key.width;
if (touchX >= x && touchX < (x + keyW))
@@ -343,7 +346,7 @@ public class Keyboard2View extends View
for (KeyboardData.Row row : _keyboard.getRows())
{
x = (KEY_PER_ROW * _keyWidth - row.getWidth(_keyWidth)) / 2f + _config.horizontalMargin;
- for (KeyboardData.Key k : row)
+ for (KeyboardData.Key k : row.getKeys())
{
float keyW = _keyWidth * k.width;
KeyDown keyDown = getKeyDown(k);
diff --git a/srcs/juloo.keyboard2/KeyboardData.java b/srcs/juloo.keyboard2/KeyboardData.java
index b52a172..4bf4ec0 100644
--- a/srcs/juloo.keyboard2/KeyboardData.java
+++ b/srcs/juloo.keyboard2/KeyboardData.java
@@ -2,12 +2,18 @@ package juloo.keyboard2;
import android.content.res.XmlResourceParser;
import java.util.ArrayList;
+import java.util.List;
class KeyboardData
{
- private ArrayList<Row> _rows;
+ private final List<Row> _rows;
- public KeyboardData(XmlResourceParser parser)
+ public KeyboardData(ArrayList<Row> rows)
+ {
+ _rows = rows;
+ }
+
+ public static KeyboardData parse(XmlResourceParser parser)
{
ArrayList<Row> rows = new ArrayList<Row>();
@@ -25,110 +31,146 @@ class KeyboardData
{
String tag = parser.getName();
if (tag.equals("row"))
- rows.add(new Row(parser));
+ rows.add(Row.parse(parser));
else
throw new Exception("Unknow keyboard tag: " + tag);
}
}
- _rows = rows;
+ return new KeyboardData(rows);
}
catch (Exception e)
{
e.printStackTrace();
}
+ return new KeyboardData(rows);
}
- public ArrayList<Row> getRows()
+ public List<Row> getRows()
{
return (_rows);
}
- // Remove every keys that has the given flags.
- public void removeKeysByFlag(int flags)
+ public KeyboardData removeKeys(MapKeys f)
{
+ ArrayList<Row> rows = new ArrayList<Row>();
for (Row r : _rows)
- {
- for (Key k : r)
- {
- k.key0 = _removeKeyValueFlag(k.key0, flags);
- k.key1 = _removeKeyValueFlag(k.key1, flags);
- k.key2 = _removeKeyValueFlag(k.key2, flags);
- k.key3 = _removeKeyValueFlag(k.key3, flags);
- k.key4 = _removeKeyValueFlag(k.key4, flags);
- }
- }
- }
-
- private KeyValue _removeKeyValueFlag(KeyValue v, int flags)
- {
- return (v != null && (v.getFlags() & flags) != 0) ? null : v;
+ rows.add(r.removeKeys(f));
+ return new KeyboardData(rows);
}
- public class Row extends ArrayList<Key>
+ public static class Row
{
- private float _keysWidth;
+ private final List<Key> _keys;
+ private final float _keysWidth;
- public Row(XmlResourceParser parser) throws Exception
- {
- super();
+ public Row(List<Key> keys)
+ {
+ float kw = 0.f;
+ for (Key k : keys) kw += k.width;
+ _keys = keys;
+ _keysWidth = kw;
+ }
+ public static Row parse(XmlResourceParser parser) throws Exception
+ {
+ ArrayList<Key> keys = new ArrayList<Key>();
int status;
- _keysWidth = 0;
while ((status = parser.next()) != XmlResourceParser.END_TAG)
{
if (status == XmlResourceParser.START_TAG)
{
String tag = parser.getName();
if (tag.equals("key"))
- {
- Key k = new Key(parser);
- _keysWidth += k.width;
- add(k);
- }
+ keys.add(Key.parse(parser));
else
throw new Exception("Unknow row tag: " + tag);
}
}
+ return new Row(keys);
}
- public float getWidth(float keyWidth)
+ public List<Key> getKeys() { return _keys; }
+
+ public float getWidth(float keyWidth)
{
return (keyWidth * _keysWidth);
}
- }
- public class Key
- {
- /*
- ** 1 2
- ** 0
- ** 3 4
- */
- public KeyValue key0;
- public KeyValue key1;
- public KeyValue key2;
- public KeyValue key3;
- public KeyValue key4;
-
- public float width;
-
- public Key(XmlResourceParser parser) throws Exception
- {
- key0 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key0"));
- key1 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key1"));
- key2 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key2"));
- key3 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key3"));
- key4 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key4"));
- try
- {
- width = parser.getAttributeFloatValue(null, "width", 1f);
- }
- catch (Exception e)
- {
- width = 1f;
- }
- while (parser.next() != XmlResourceParser.END_TAG)
- continue ;
- }
+ public Row removeKeys(MapKeys f)
+ {
+ ArrayList<Key> keys = new ArrayList<Key>();
+ for (Key k : _keys)
+ keys.add(k.removeKeys(f));
+ return new Row(keys);
+ }
}
+
+ public static class Key
+ {
+ /*
+ ** 1 2
+ ** 0
+ ** 3 4
+ */
+ public final KeyValue key0;
+ public final KeyValue key1;
+ public final KeyValue key2;
+ public final KeyValue key3;
+ public final KeyValue key4;
+
+ public final float width;
+
+ public Key(KeyValue k0, KeyValue k1, KeyValue k2, KeyValue k3, KeyValue k4, float w)
+ {
+ key0 = k0;
+ key1 = k1;
+ key2 = k2;
+ key3 = k3;
+ key4 = k4;
+ width = w;
+ }
+
+ public static Key parse(XmlResourceParser parser) throws Exception
+ {
+ KeyValue k0 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key0"));
+ KeyValue k1 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key1"));
+ KeyValue k2 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key2"));
+ KeyValue k3 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key3"));
+ KeyValue k4 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key4"));
+ float width;
+ try
+ {
+ width = parser.getAttributeFloatValue(null, "width", 1f);
+ }
+ catch (Exception e)
+ {
+ width = 1f;
+ }
+ while (parser.next() != XmlResourceParser.END_TAG)
+ continue ;
+ return new Key(k0, k1, k2, k3, k4, width);
+ }
+
+ public Key removeKeys(MapKeys f)
+ {
+ return new Key(f.map(key0), f.map(key1), f.map(key2), f.map(key3), f.map(key4), width);
+ }
+ }
+
+ public static abstract interface MapKeys
+ {
+ public abstract KeyValue map(KeyValue k);
+ }
+
+ public static class RemoveKeysByFlags implements MapKeys
+ {
+ private final int _flags;
+
+ public RemoveKeysByFlags(int flags) { _flags = flags; }
+
+ public KeyValue map(KeyValue k)
+ {
+ return (k == null || (k.getFlags() & _flags) != 0) ? null : k;
+ }
+ }
}