diff options
| author | Jules Aguillon | 2021-04-15 23:23:31 +0200 |
|---|---|---|
| committer | Jules Aguillon | 2021-04-15 23:23:31 +0200 |
| commit | c22ca7302c16773dbe1971323034dd6e23d9611b (patch) | |
| tree | a5b84667fc1a8dbee93c75d5935246663f7705a9 /srcs/juloo.keyboard2/KeyboardData.java | |
| parent | eac74d3f2283ec9f36e595261ab21716d910c15a (diff) | |
| download | unexpected-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.
Diffstat (limited to 'srcs/juloo.keyboard2/KeyboardData.java')
| -rw-r--r-- | srcs/juloo.keyboard2/KeyboardData.java | 178 |
1 files changed, 110 insertions, 68 deletions
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; + } + } } |
