diff --git a/res/xml/azerty.xml b/res/xml/azerty.xml index f8aae93..2369539 100644 --- a/res/xml/azerty.xml +++ b/res/xml/azerty.xml @@ -34,11 +34,4 @@ - - - - - - - diff --git a/res/xml/bottom_row.xml b/res/xml/bottom_row.xml new file mode 100644 index 0000000..3f64a42 --- /dev/null +++ b/res/xml/bottom_row.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/res/xml/dvorak.xml b/res/xml/dvorak.xml index e1d38c0..c659925 100644 --- a/res/xml/dvorak.xml +++ b/res/xml/dvorak.xml @@ -34,11 +34,4 @@ - - - - - - - diff --git a/res/xml/local_bgph1.xml b/res/xml/local_bgph1.xml index e4537a8..fce2a30 100644 --- a/res/xml/local_bgph1.xml +++ b/res/xml/local_bgph1.xml @@ -34,11 +34,4 @@ - - - - - - - diff --git a/res/xml/numeric.xml b/res/xml/numeric.xml index 334aa35..abd31d8 100644 --- a/res/xml/numeric.xml +++ b/res/xml/numeric.xml @@ -1,5 +1,5 @@ - + diff --git a/res/xml/qwerty.xml b/res/xml/qwerty.xml index bbcc04e..47e1078 100644 --- a/res/xml/qwerty.xml +++ b/res/xml/qwerty.xml @@ -34,11 +34,4 @@ - - - - - - - diff --git a/res/xml/qwerty_lv.xml b/res/xml/qwerty_lv.xml index 78919d9..d181f5d 100644 --- a/res/xml/qwerty_lv.xml +++ b/res/xml/qwerty_lv.xml @@ -34,11 +34,4 @@ - - - - - - - diff --git a/res/xml/qwertz.xml b/res/xml/qwertz.xml index e69a0b4..28a0026 100644 --- a/res/xml/qwertz.xml +++ b/res/xml/qwertz.xml @@ -34,11 +34,4 @@ - - - - - - - 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 _layoutCache = new HashMap(); - 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,7 +15,58 @@ class KeyboardData /* Total height of the keyboard. Unit is abstract. */ public final float keysHeight; - public KeyboardData(List rows_) + public KeyboardData replaceKeys(MapKeys f) + { + ArrayList rows_ = new ArrayList(); + for (Row r : rows) + rows_.add(r.replaceKeys(f)); + return new KeyboardData(rows_); + } + + private static Row _bottomRow = null; + private static Map _layoutCache = new HashMap(); + + public static KeyboardData load(Resources res, int id) + { + KeyboardData l = _layoutCache.get(id); + if (l == null) + { + try + { + 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(); + } + } + 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 rows = new ArrayList(); + while (expect_tag(parser, "row")) + rows.add(Row.parse(parser)); + if (bottom_row) + rows.add(_bottomRow); + return new KeyboardData(rows); + } + + private static Row parse_bottom_row(XmlResourceParser parser) throws Exception + { + if (!expect_tag(parser, "row")) + throw new Exception("Failed to parse bottom row"); + return Row.parse(parser); + } + + protected KeyboardData(List rows_) { float kw = 0.f; float kh = 0.f; @@ -26,45 +80,6 @@ class KeyboardData keysHeight = kh; } - public static KeyboardData parse(XmlResourceParser parser) - { - ArrayList rows = new ArrayList(); - - try - { - 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) - { - 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); - } - } - } - catch (Exception e) - { - e.printStackTrace(); - } - return new KeyboardData(rows); - } - - public KeyboardData replaceKeys(MapKeys f) - { - ArrayList rows_ = new ArrayList(); - for (Row r : rows) - rows_.add(r.replaceKeys(f)); - return new KeyboardData(rows_); - } - public static class Row { public final List keys; @@ -75,7 +90,7 @@ class KeyboardData /* Total width of very keys. Unit is abstract. */ private final float keysWidth; - public Row(List keys_, float h, float s) + protected Row(List 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; + } }