diff options
Diffstat (limited to 'srcs/juloo.keyboard2')
| -rw-r--r-- | srcs/juloo.keyboard2/Keyboard2.java | 21 | ||||
| -rw-r--r-- | srcs/juloo.keyboard2/KeyboardData.java | 133 |
2 files changed, 79 insertions, 75 deletions
diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java index 54fe587..7f02ea7 100644 --- a/srcs/juloo.keyboard2/Keyboard2.java +++ b/srcs/juloo.keyboard2/Keyboard2.java @@ -1,31 +1,24 @@ package juloo.keyboard2; import android.content.Context; -import android.content.res.Configuration; import android.content.Intent; import android.content.SharedPreferences; -import android.graphics.Typeface; import android.inputmethodservice.InputMethodService; import android.os.Build.VERSION; -import android.os.Bundle; import android.os.IBinder; -import android.text.InputType; import android.preference.PreferenceManager; +import android.text.InputType; +import android.view.ContextThemeWrapper; 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; -import android.view.ContextThemeWrapper; import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; -import android.util.Log; -import java.util.HashMap; import java.util.List; -import java.util.Locale; -import java.util.Map; public class Keyboard2 extends InputMethodService implements SharedPreferences.OnSharedPreferenceChangeListener @@ -36,17 +29,9 @@ public class Keyboard2 extends InputMethodService private Config _config; - private Map<Integer, KeyboardData> _layoutCache = new HashMap<Integer, KeyboardData>(); - private KeyboardData getLayout(int resId) { - KeyboardData l = _layoutCache.get(resId); - if (l == null) - { - l = KeyboardData.parse(getResources().getXml(resId)); - _layoutCache.put(resId, l); - } - return l; + return KeyboardData.load(getResources(), resId); } @Override diff --git a/srcs/juloo.keyboard2/KeyboardData.java b/srcs/juloo.keyboard2/KeyboardData.java index 2153534..fe1b2e4 100644 --- a/srcs/juloo.keyboard2/KeyboardData.java +++ b/srcs/juloo.keyboard2/KeyboardData.java @@ -1,8 +1,11 @@ package juloo.keyboard2; +import android.content.res.Resources; import android.content.res.XmlResourceParser; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; class KeyboardData { @@ -12,57 +15,69 @@ class KeyboardData /* Total height of the keyboard. Unit is abstract. */ public final float keysHeight; - public KeyboardData(List<Row> rows_) + public KeyboardData replaceKeys(MapKeys f) { - float kw = 0.f; - float kh = 0.f; - for (Row r : rows_) - { - kw = Math.max(kw, r.keysWidth); - kh += r.height + r.shift; - } - rows = rows_; - keysWidth = kw; - keysHeight = kh; + ArrayList<Row> rows_ = new ArrayList<Row>(); + for (Row r : rows) + rows_.add(r.replaceKeys(f)); + return new KeyboardData(rows_); } - public static KeyboardData parse(XmlResourceParser parser) - { - ArrayList<Row> rows = new ArrayList<Row>(); + private static Row _bottomRow = null; + private static Map<Integer, KeyboardData> _layoutCache = new HashMap<Integer, KeyboardData>(); - try + public static KeyboardData load(Resources res, int id) + { + KeyboardData l = _layoutCache.get(id); + if (l == null) { - int status; - - while (parser.next() != XmlResourceParser.START_TAG) - continue ; - if (!parser.getName().equals("keyboard")) - throw new Exception("Unknow tag: " + parser.getName()); - while ((status = parser.next()) != XmlResourceParser.END_DOCUMENT) + try { - if (status == XmlResourceParser.START_TAG) - { - String tag = parser.getName(); - if (tag.equals("row")) - rows.add(Row.parse(parser)); - else - throw new Exception("Unknow keyboard tag: " + tag); - } + if (_bottomRow == null) + _bottomRow = parse_bottom_row(res.getXml(R.xml.bottom_row)); + l = parse_keyboard(res.getXml(id)); + _layoutCache.put(id, l); + } + catch (Exception e) + { + e.printStackTrace(); } } - catch (Exception e) - { - e.printStackTrace(); - } + return l; + } + + private static KeyboardData parse_keyboard(XmlResourceParser parser) throws Exception + { + if (!expect_tag(parser, "keyboard")) + throw new Exception("Empty layout file"); + boolean bottom_row = parser.getAttributeBooleanValue(null, "bottom_row", true); + ArrayList<Row> rows = new ArrayList<Row>(); + while (expect_tag(parser, "row")) + rows.add(Row.parse(parser)); + if (bottom_row) + rows.add(_bottomRow); return new KeyboardData(rows); } - public KeyboardData replaceKeys(MapKeys f) + private static Row parse_bottom_row(XmlResourceParser parser) throws Exception { - ArrayList<Row> rows_ = new ArrayList<Row>(); - for (Row r : rows) - rows_.add(r.replaceKeys(f)); - return new KeyboardData(rows_); + if (!expect_tag(parser, "row")) + throw new Exception("Failed to parse bottom row"); + return Row.parse(parser); + } + + protected KeyboardData(List<Row> rows_) + { + float kw = 0.f; + float kh = 0.f; + for (Row r : rows_) + { + kw = Math.max(kw, r.keysWidth); + kh += r.height + r.shift; + } + rows = rows_; + keysWidth = kw; + keysHeight = kh; } public static class Row @@ -75,7 +90,7 @@ class KeyboardData /* Total width of very keys. Unit is abstract. */ private final float keysWidth; - public Row(List<Key> keys_, float h, float s) + protected Row(List<Key> keys_, float h, float s) { float kw = 0.f; for (Key k : keys_) kw += k.width + k.shift; @@ -91,17 +106,8 @@ class KeyboardData int status; float h = parser.getAttributeFloatValue(null, "height", 1f); float shift = parser.getAttributeFloatValue(null, "shift", 0f); - while ((status = parser.next()) != XmlResourceParser.END_TAG) - { - if (status == XmlResourceParser.START_TAG) - { - String tag = parser.getName(); - if (tag.equals("key")) - keys.add(Key.parse(parser)); - else - throw new Exception("Unknow row tag: " + tag); - } - } + while (expect_tag(parser, "key")) + keys.add(Key.parse(parser)); return new Row(keys, h, shift); } @@ -134,7 +140,7 @@ class KeyboardData /* Put keys 1 to 4 on the edges instead of the corners. */ public final boolean edgekeys; - public Key(KeyValue k0, KeyValue k1, KeyValue k2, KeyValue k3, KeyValue k4, float w, float s, boolean e) + protected Key(KeyValue k0, KeyValue k1, KeyValue k2, KeyValue k3, KeyValue k4, float w, float s, boolean e) { key0 = k0; key1 = k1; @@ -146,11 +152,6 @@ class KeyboardData edgekeys = e; } - public Key(KeyValue k0, KeyValue k1, KeyValue k2, KeyValue k3, KeyValue k4, float w, float s) - { - this(k0, k1, k2, k3, k4, w, s, false); - } - public static Key parse(XmlResourceParser parser) throws Exception { KeyValue k0 = KeyValue.getKeyByName(parser.getAttributeValue(null, "key0")); @@ -236,4 +237,22 @@ class KeyboardData return k; } } + + /** Parsing utils */ + + /** Returns [false] on [END_DOCUMENT] or [END_TAG], [true] otherwise. */ + private static boolean expect_tag(XmlResourceParser parser, String name) throws Exception + { + int status; + do + { + status = parser.next(); + if (status == XmlResourceParser.END_DOCUMENT || status == XmlResourceParser.END_TAG) + return false; + } + while (status != XmlResourceParser.START_TAG); + if (!parser.getName().equals(name)) + throw new Exception("Unknow tag: " + parser.getName()); + return true; + } } |
