From 15c608b8cd663bf9267ade94d689164762c40ed9 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sun, 25 Jun 2023 16:34:05 +0200 Subject: [PATCH] Use generated arrays in Config.layout_of_string This function is no longer an hardcoded list of layout ids. It's replaced by a linear scan of the previously generated array and a new corresponding array of resource ids. --- gen_layouts.py | 20 +++++------ res/values/layouts.xml | 40 +++++++++++++++++++++ srcs/juloo.keyboard2/Config.java | 62 ++++++++++++-------------------- 3 files changed, 73 insertions(+), 49 deletions(-) diff --git a/gen_layouts.py b/gen_layouts.py index 2b86883..bea9ba1 100644 --- a/gen_layouts.py +++ b/gen_layouts.py @@ -40,22 +40,22 @@ def sort_layouts(layouts): # Write the XML arrays used in the preferences. def generate_arrays(out, layouts): - def add_items(parent, strings): - for s in strings: + def mk_array(tag, name, strings_items): + elem = XML.Element(tag, name=name) + for s in strings_items: item = XML.Element("item") item.text = s - parent.append(item) + elem.append(item) + return elem none_item = [ ("none", "None") ] custom_item = [ ("custom", "@string/pref_layout_e_custom") ] - lids, names = zip(*(none_item + layouts + custom_item)) # unzip - values = XML.Element("string-array", name="pref_layout_values") - add_items(values, lids) - entries = XML.Element("string-array", name="pref_layout_entries") - add_items(entries, names) + values_items, entries_items = zip(*(none_item + layouts + custom_item)) # unzip + ids_items = map(lambda s: "@xml/%s" % s if s not in ["none", "custom"] else "-1", values_items) root = XML.Element("resources") root.append(XML.Comment(text="DO NOT EDIT. This file is generated, see gen_layouts.py.")) - root.append(values) - root.append(entries) + root.append(mk_array("string-array", "pref_layout_values", values_items)) + root.append(mk_array("string-array", "pref_layout_entries", entries_items)) + root.append(mk_array("integer-array", "layout_ids", ids_items)) XML.indent(root) XML.ElementTree(element=root).write(out, encoding="unicode", xml_declaration=True) diff --git a/res/values/layouts.xml b/res/values/layouts.xml index 6042437..139d42e 100644 --- a/res/values/layouts.xml +++ b/res/values/layouts.xml @@ -81,4 +81,44 @@ QWERTZ (Slovak) @string/pref_layout_e_custom + + -1 + @xml/latn_qwerty_us + @xml/latn_colemak + @xml/latn_dvorak + @xml/arab_alt + @xml/arab_pc + @xml/arab_pc_ckb + @xml/arab_pc_ir + @xml/beng_national + @xml/beng_provat + @xml/cyrl_jcuken_ru + @xml/cyrl_jcuken_uk + @xml/cyrl_yaverti + @xml/deva_alt + @xml/deva_inscript + @xml/grek_qwerty + @xml/hang_dubeolsik_kr + @xml/hebr_1_il + @xml/hebr_2_il + @xml/latn_azerty_fr + @xml/latn_bone + @xml/latn_neo2 + @xml/latn_qwerty_br + @xml/latn_qwerty_es + @xml/latn_qwerty_hu + @xml/latn_qwerty_lv + @xml/latn_qwerty_no + @xml/latn_qwerty_pl + @xml/latn_qwerty_ro + @xml/latn_qwerty_se + @xml/latn_qwerty_tr + @xml/latn_qwerty_vi + @xml/latn_qwertz + @xml/latn_qwertz_cz + @xml/latn_qwertz_de + @xml/latn_qwertz_hu + @xml/latn_qwertz_sk + -1 + \ No newline at end of file diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index ddcd587..2c48b3f 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -3,11 +3,14 @@ package juloo.keyboard2; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Resources; +import android.content.res.TypedArray; import android.os.Build; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.KeyEvent; +import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; final class Config @@ -335,53 +338,34 @@ final class Config } } + /** Obtained from XML. */ + static List layout_ids_str = null; + static TypedArray layout_ids_res = null; + /** Might return [null] if the selected layout is "system", "custom" or if the name is not recognized. */ public KeyboardData layout_of_string(Resources res, String name) { - int id; + if (layout_ids_str == null) + { + layout_ids_str = Arrays.asList(res.getStringArray(R.array.pref_layout_values)); + layout_ids_res = res.obtainTypedArray(R.array.layout_ids); + } + int i = layout_ids_str.indexOf(name); + if (i >= 0) + { + int id = layout_ids_res.getResourceId(i, 0); + if (id > 0) + return KeyboardData.load(res, id); + // Fallthrough + } switch (name) { - case "arab_pc_ckb": id = R.xml.arab_pc_ckb; break; - case "arab_pc_ir": id = R.xml.arab_pc_ir; break; - case "arab_pc": id = R.xml.arab_pc; break; - case "arab_alt": id = R.xml.arab_alt; break; - case "beng_national": id = R.xml.beng_national; break; - case "beng_provat": id = R.xml.beng_provat; break; - case "cyrl_jcuken_ru": id = R.xml.cyrl_jcuken_ru; break; - case "cyrl_jcuken_uk": id = R.xml.cyrl_jcuken_uk; break; - case "cyrl_yaverti": id = R.xml.cyrl_yaverti; break; - case "deva_inscript": id = R.xml.deva_inscript; break; - case "deva_alt": id = R.xml.deva_alt; break; - case "grek_qwerty": id = R.xml.grek_qwerty; break; - case "hang_dubeolsik_kr": id = R.xml.hang_dubeolsik_kr; break; - case "hebr_1_il": id = R.xml.hebr_1_il; break; - case "hebr_2_il": id = R.xml.hebr_2_il; break; - case "latn_azerty_fr": id = R.xml.latn_azerty_fr; break; - case "latn_bone": id = R.xml.latn_bone; break; - case "latn_colemak": id = R.xml.latn_colemak; break; - case "latn_dvorak": id = R.xml.latn_dvorak; break; - case "latn_neo2": id = R.xml.latn_neo2; break; - case "latn_qwerty_hu": id = R.xml.latn_qwerty_hu; break; - case "latn_qwerty_br": id = R.xml.latn_qwerty_br; break; - case "latn_qwerty_es": id = R.xml.latn_qwerty_es; break; - case "latn_qwerty_lv": id = R.xml.latn_qwerty_lv; break; - case "latn_qwerty_no": id = R.xml.latn_qwerty_no; break; - case "latn_qwerty_pl": id = R.xml.latn_qwerty_pl; break; - case "latn_qwerty_ro": id = R.xml.latn_qwerty_ro; break; - case "latn_qwerty_se": id = R.xml.latn_qwerty_se; break; - case "latn_qwerty_tr": id = R.xml.latn_qwerty_tr; break; - case "latn_qwerty_us": id = R.xml.latn_qwerty_us; break; - case "latn_qwerty_vi": id = R.xml.latn_qwerty_vi; break; - case "latn_qwertz": id = R.xml.latn_qwertz; break; - case "latn_qwertz_cz": id = R.xml.latn_qwertz_cz; break; - case "latn_qwertz_de": id = R.xml.latn_qwertz_de; break; - case "latn_qwertz_hu": id = R.xml.latn_qwertz_hu; break; - case "latn_qwertz_sk": id = R.xml.latn_qwertz_sk; break; case "custom": return custom_layout; - case "system": case "none": default: return null; + case "system": + case "none": + default: return null; } - return KeyboardData.load(res, id); } char inverse_numpad_char(char c)