From e745c812691462380695da2275757e3f29bbbdb2 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sat, 26 Nov 2022 22:30:45 +0100 Subject: [PATCH] Allow using a custom layout Add an option for specifying an XML layout description as a string. The option is a bit rough at the moment: - No documentation, users have to be aware of the keyboard's code to use this option. - No error are shown, the layout will fallback to qwerty on error. --- res/values/arrays.xml | 2 ++ res/values/strings.xml | 4 ++++ res/xml/settings.xml | 3 +++ srcs/juloo.keyboard2/Config.java | 4 ++++ srcs/juloo.keyboard2/KeyboardData.java | 23 ++++++++++++++++++++++- 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/res/values/arrays.xml b/res/values/arrays.xml index 17c4bd9..524da3d 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -26,6 +26,7 @@ jcuken_ua bangla hindi + custom None @@ -53,6 +54,7 @@ ЙЦУКЕН (Українська) বাংলা हिन्दी + @string/pref_layout_e_custom @string/pref_accents_e_all_installed diff --git a/res/values/strings.xml b/res/values/strings.xml index 63c55ab..a565b09 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -7,6 +7,7 @@ Adjust label brightness Change keyboard layout System settings + Custom layout Accents Show accents for all installed languages Only show accents for the selected language @@ -51,6 +52,9 @@ Very far Horizontal spacing between the keys Vertical spacing between the keys + Advanced + Custom layout + See the source code. This option is not meant to be used. Next Done Go diff --git a/res/xml/settings.xml b/res/xml/settings.xml index b585ba1..3f76983 100644 --- a/res/xml/settings.xml +++ b/res/xml/settings.xml @@ -54,4 +54,7 @@ + + + diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 5fb2ab5..8404946 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -26,6 +26,7 @@ final class Config // From preferences public KeyboardData layout; // Or 'null' for the system defaults public KeyboardData second_layout; // Or 'null' for none + public KeyboardData custom_layout; // Might be 'null' public boolean show_numpad = false; public float swipe_dist_px; public boolean vibrateEnabled; @@ -66,6 +67,7 @@ final class Config // default values layout = null; second_layout = null; + custom_layout = null; vibrateEnabled = true; longPressTimeout = 600; longPressInterval = 65; @@ -117,6 +119,7 @@ final class Config } layout = layout_of_string(res, _prefs.getString("layout", "none")); second_layout = layout_of_string(res, _prefs.getString("second_layout", "none")); + custom_layout = KeyboardData.load_string(_prefs.getString("custom_layout", "")); // The baseline for the swipe distance correspond to approximately the // width of a key in portrait mode, as most layouts have 10 columns. // Multipled by the DPI ratio because most swipes are made in the diagonals. @@ -252,6 +255,7 @@ final class Config switch (name) { case "system": case "none": return null; + case "custom": if (custom_layout != null) return custom_layout; break; case "azerty": id = R.xml.azerty; break; case "bangla": id = R.xml.bangla; break; case "bgph1": id = R.xml.local_bgph1; break; diff --git a/srcs/juloo.keyboard2/KeyboardData.java b/srcs/juloo.keyboard2/KeyboardData.java index 640ed3b..7b50f98 100644 --- a/srcs/juloo.keyboard2/KeyboardData.java +++ b/srcs/juloo.keyboard2/KeyboardData.java @@ -1,6 +1,9 @@ package juloo.keyboard2; import android.content.res.Resources; +import android.content.res.XmlResourceParser; +import android.util.Xml; +import java.io.StringReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -126,6 +129,7 @@ class KeyboardData return _pin_entry; } + /** Load a layout from a resource ID. Returns [null] on error. */ public static KeyboardData load(Resources res, int id) { KeyboardData l = _layoutCache.get(id); @@ -133,7 +137,9 @@ class KeyboardData { try { - l = parse_keyboard(res.getXml(id)); + XmlResourceParser parser = res.getXml(id); + l = parse_keyboard(parser); + parser.close(); _layoutCache.put(id, l); } catch (Exception e) @@ -144,6 +150,21 @@ class KeyboardData return l; } + /** Load a layout from a string. Returns [null] on error. */ + public static KeyboardData load_string(String src) + { + try + { + XmlPullParser parser = Xml.newPullParser(); + parser.setInput(new StringReader(src)); + return parse_keyboard(parser); + } + catch (Exception e) + { + return null; + } + } + private static KeyboardData parse_keyboard(XmlPullParser parser) throws Exception { if (!expect_tag(parser, "keyboard"))