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.
This commit is contained in:
Jules Aguillon 2022-11-26 22:30:45 +01:00
parent 09d984a1ab
commit e745c81269
5 changed files with 35 additions and 1 deletions

View File

@ -26,6 +26,7 @@
<item>jcuken_ua</item>
<item>bangla</item>
<item>hindi</item>
<item>custom</item>
</string-array>
<string-array name="pref_layout_entries">
<item>None</item>
@ -53,6 +54,7 @@
<item>ЙЦУКЕН (Українська)</item>
<item>বাংলা</item>
<item>हिन्दी</item>
<item>@string/pref_layout_e_custom</item>
</string-array>
<string-array name="pref_accents_entries">
<item>@string/pref_accents_e_all_installed</item>

View File

@ -7,6 +7,7 @@
<string name="pref_label_brightness">Adjust label brightness</string>
<string name="pref_layout_title">Change keyboard layout</string>
<string name="pref_layout_e_system">System settings</string>
<string name="pref_layout_e_custom">Custom layout</string>
<string name="pref_accents_title">Accents</string>
<string name="pref_accents_e_all_installed">Show accents for all installed languages</string>
<string name="pref_accents_e_selected">Only show accents for the selected language</string>
@ -51,6 +52,9 @@
<string name="pref_swipe_dist_e_very_far">Very far</string>
<string name="pref_key_horizontal_space">Horizontal spacing between the keys</string>
<string name="pref_key_vertical_space">Vertical spacing between the keys</string>
<string name="pref_category_advanced">Advanced</string>
<string name="pref_custom_layout_title">Custom layout</string>
<string name="pref_custom_layout_summary">See the source code. This option is not meant to be used.</string>
<string name="key_action_next">Next</string>
<string name="key_action_done">Done</string>
<string name="key_action_go">Go</string>

View File

@ -54,4 +54,7 @@
<juloo.common.IntSlideBarPreference android:key="key_vertical_space" android:title="@string/pref_key_vertical_space" android:summary="%sdp" android:defaultValue="2" min="0" max="8"/>
<juloo.common.IntSlideBarPreference android:key="key_horizontal_space" android:title="@string/pref_key_horizontal_space" android:summary="%sdp" android:defaultValue="2" min="0" max="8"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_category_advanced">
<EditTextPreference android:key="custom_layout" android:title="@string/pref_custom_layout_title" android:summary="@string/pref_custom_layout_summary" android:inputType="text|textMultiLine|textLongMessage"/>
</PreferenceCategory>
</PreferenceScreen>

View File

@ -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;

View File

@ -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"))