Refactor: Pass layout as a KeyboardData instead of ID

Parse layouts sooner.
This commit is contained in:
Jules Aguillon 2022-11-26 22:12:40 +01:00
parent ebdacbc2b2
commit 69994a55c5
3 changed files with 80 additions and 67 deletions

View File

@ -24,8 +24,8 @@ final class Config
public final float sublabelTextSize;
// From preferences
public int layout; // Or '-1' for the system defaults
public int second_layout; // Or '-1' for none
public KeyboardData layout; // Or 'null' for the system defaults
public KeyboardData second_layout; // Or 'null' for none
public boolean show_numpad = false;
public float swipe_dist_px;
public boolean vibrateEnabled;
@ -64,8 +64,8 @@ final class Config
labelTextSize = 0.33f;
sublabelTextSize = 0.22f;
// default values
layout = -1;
second_layout = -1;
layout = null;
second_layout = null;
vibrateEnabled = true;
longPressTimeout = 600;
longPressInterval = 65;
@ -115,8 +115,8 @@ final class Config
{
keyboardHeightPercent = _prefs.getInt("keyboard_height", 35);
}
layout = layoutId_of_string(_prefs.getString("layout", "none"));
second_layout = layoutId_of_string(_prefs.getString("second_layout", "none"));
layout = layout_of_string(res, _prefs.getString("layout", "none"));
second_layout = layout_of_string(res, _prefs.getString("second_layout", "none"));
// 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.
@ -246,37 +246,38 @@ final class Config
}
}
public static int layoutId_of_string(String name)
public KeyboardData layout_of_string(Resources res, String name)
{
int id = R.xml.qwerty; // The config might store an invalid layout, don't crash
switch (name)
{
case "system": case "none": return -1;
case "azerty": return R.xml.azerty;
case "bangla": return R.xml.bangla;
case "bgph1": return R.xml.local_bgph1;
case "bone": return R.xml.bone;
case "colemak": return R.xml.colemak;
case "dvorak": return R.xml.dvorak;
case "hindi": return R.xml.hindi;
case "jcuken_ua": return R.xml.jcuken_ua;
case "neo2": return R.xml.neo2;
case "qwerty": return R.xml.qwerty;
case "qwerty_el": return R.xml.qwerty_el;
case "qwerty_es": return R.xml.qwerty_es;
case "qwerty_hu": return R.xml.qwerty_hu;
case "qwerty_ko": return R.xml.qwerty_ko;
case "qwerty_lv": return R.xml.qwerty_lv;
case "qwerty_no": return R.xml.qwerty_no;
case "qwerty_pt": return R.xml.qwerty_pt;
case "qwerty_sv_se": return R.xml.qwerty_sv_se;
case "qwerty_tr": return R.xml.qwerty_tr;
case "qwertz": return R.xml.qwertz;
case "qwertz_cs": return R.xml.qwertz_cs;
case "qwertz_de": return R.xml.qwertz_de;
case "qwertz_hu": return R.xml.qwertz_hu;
case "ru_jcuken": return R.xml.local_ru_jcuken;
default: return R.xml.qwerty; // The config might store an invalid layout, don't crash
case "system": case "none": return null;
case "azerty": id = R.xml.azerty; break;
case "bangla": id = R.xml.bangla; break;
case "bgph1": id = R.xml.local_bgph1; break;
case "bone": id = R.xml.bone; break;
case "colemak": id = R.xml.colemak; break;
case "dvorak": id = R.xml.dvorak; break;
case "hindi": id = R.xml.hindi; break;
case "jcuken_ua": id = R.xml.jcuken_ua; break;
case "neo2": id = R.xml.neo2; break;
case "qwerty": id = R.xml.qwerty; break;
case "qwerty_el": id = R.xml.qwerty_el; break;
case "qwerty_es": id = R.xml.qwerty_es; break;
case "qwerty_hu": id = R.xml.qwerty_hu; break;
case "qwerty_ko": id = R.xml.qwerty_ko; break;
case "qwerty_lv": id = R.xml.qwerty_lv; break;
case "qwerty_no": id = R.xml.qwerty_no; break;
case "qwerty_pt": id = R.xml.qwerty_pt; break;
case "qwerty_sv_se": id = R.xml.qwerty_sv_se; break;
case "qwerty_tr": id = R.xml.qwerty_tr; break;
case "qwertz": id = R.xml.qwertz; break;
case "qwertz_cs": id = R.xml.qwertz_cs; break;
case "qwertz_de": id = R.xml.qwertz_de; break;
case "qwertz_hu": id = R.xml.qwertz_hu; break;
case "ru_jcuken": id = R.xml.local_ru_jcuken; break;
}
return KeyboardData.load(res, id);
}
private static Config _globalConfig = null;

View File

@ -31,22 +31,18 @@ public class Keyboard2 extends InputMethodService
private Keyboard2View _keyboardView;
private KeyEventHandler _keyeventhandler;
private int _currentTextLayout;
private KeyboardData _currentTextLayout;
private ViewGroup _emojiPane = null;
private Config _config;
private boolean _debug_logs = false;
private KeyboardData getLayout(int resId)
{
return KeyboardData.load(getResources(), resId);
}
@Override
public void onCreate()
{
super.onCreate();
KeyboardData.init(getResources());
SharedPreferences prefs = DirectBootAwarePreferences.get_shared_preferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
_keyeventhandler = new KeyEventHandler(getMainLooper(), this.new Receiver());
@ -68,14 +64,13 @@ public class Keyboard2 extends InputMethodService
private void refreshSubtypeLayout(InputMethodSubtype subtype)
{
int l = _config.layout;
if (l == -1)
KeyboardData l = _config.layout;
if (l == null)
{
String s = subtype.getExtraValueOf("default_layout");
if (s != null)
l = Config.layoutId_of_string(s);
else
l = R.xml.qwerty;
l = (s != null)
? _config.layout_of_string(getResources(), s)
: KeyboardData.load(getResources(), R.xml.qwerty);
}
_currentTextLayout = l;
}
@ -122,7 +117,10 @@ public class Keyboard2 extends InputMethodService
case 4: _config.extra_keys_subtype = new HashSet<KeyValue>(); break;
}
// Fallback for the layout option: Use qwerty in the "system settings" case
_currentTextLayout = (_config.layout == -1) ? R.xml.qwerty : _config.layout;
if (_config.layout == null)
_currentTextLayout = KeyboardData.load(getResources(), R.xml.qwerty);
else
_currentTextLayout = _config.layout;
}
private void refreshSubtypeImm()
@ -152,7 +150,7 @@ public class Keyboard2 extends InputMethodService
}
}
_config.shouldOfferSwitchingToSecond =
_config.second_layout != -1 &&
_config.second_layout != null &&
_currentTextLayout != _config.second_layout;
}
@ -217,15 +215,14 @@ public class Keyboard2 extends InputMethodService
Log.d(TAG, "actionLabel: "+_config.actionLabel);
}
private int chooseLayout(EditorInfo info)
private KeyboardData chooseLayout(EditorInfo info)
{
switch (info.inputType & InputType.TYPE_MASK_CLASS)
{
case InputType.TYPE_CLASS_NUMBER:
return R.xml.pin;
case InputType.TYPE_CLASS_PHONE:
case InputType.TYPE_CLASS_DATETIME:
return R.xml.pin;
return KeyboardData.load_pin_entry(getResources());
default:
return _currentTextLayout;
}
@ -236,7 +233,7 @@ public class Keyboard2 extends InputMethodService
{
refreshConfig();
refresh_action_label(info);
_keyboardView.setKeyboard(getLayout(chooseLayout(info)));
_keyboardView.setKeyboard(chooseLayout(info));
_keyeventhandler.started(info);
setInputView(_keyboardView);
if (_debug_logs)
@ -256,7 +253,7 @@ public class Keyboard2 extends InputMethodService
public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype)
{
refreshSubtypeImm();
_keyboardView.setKeyboard(getLayout(_currentTextLayout));
_keyboardView.setKeyboard(_currentTextLayout);
}
@Override
@ -316,20 +313,20 @@ public class Keyboard2 extends InputMethodService
public void switch_main()
{
_keyboardView.setKeyboard(getLayout(_currentTextLayout));
_keyboardView.setKeyboard(_currentTextLayout);
}
public void switch_layout(int layout_id)
{
_keyboardView.setKeyboard(getLayout(layout_id));
_keyboardView.setKeyboard(KeyboardData.load(getResources(), layout_id));
}
public void switch_second()
{
if (_config.second_layout == -1)
if (_config.second_layout == null)
return;
KeyboardData layout =
getLayout(_config.second_layout).mapKeys(new KeyboardData.MapKeyValues() {
_config.second_layout.mapKeys(new KeyboardData.MapKeyValues() {
public KeyValue apply(KeyValue key, boolean localized)
{
if (key.getKind() == KeyValue.Kind.Event

View File

@ -53,10 +53,10 @@ class KeyboardData
public KeyboardData addNumPad()
{
if (!num_pad || _numPadKeyboardData == null)
if (!num_pad)
return this;
ArrayList<Row> extendedRows = new ArrayList<Row>();
Iterator<Row> iterNumPadRows = _numPadKeyboardData.rows.iterator();
Iterator<Row> iterNumPadRows = _num_pad.rows.iterator();
for (Row row : rows)
{
ArrayList<KeyboardData.Key> keys = new ArrayList<Key>(row.keys);
@ -101,10 +101,31 @@ class KeyboardData
}));
}
private static Row _bottomRow = null;
private static KeyboardData _numPadKeyboardData = null;
private static Row _bottom_row;
private static KeyboardData _num_pad;
private static KeyboardData _pin_entry;
private static Map<Integer, KeyboardData> _layoutCache = new HashMap<Integer, KeyboardData>();
public static void init(Resources res)
{
try
{
_bottom_row = parse_bottom_row(res.getXml(R.xml.bottom_row));
_num_pad = parse_keyboard(res.getXml(R.xml.numpad));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static KeyboardData load_pin_entry(Resources res)
{
if (_pin_entry == null)
_pin_entry = load(res, R.xml.pin);
return _pin_entry;
}
public static KeyboardData load(Resources res, int id)
{
KeyboardData l = _layoutCache.get(id);
@ -112,12 +133,6 @@ class KeyboardData
{
try
{
if (_bottomRow == null)
_bottomRow = parse_bottom_row(res.getXml(R.xml.bottom_row));
if (_numPadKeyboardData == null)
{
_numPadKeyboardData = parse_keyboard(res.getXml(R.xml.numpad));
}
l = parse_keyboard(res.getXml(id));
_layoutCache.put(id, l);
}
@ -142,7 +157,7 @@ class KeyboardData
rows.add(Row.parse(parser));
float kw = (specified_kw != 0f) ? specified_kw : compute_max_width(rows);
if (bottom_row)
rows.add(_bottomRow.updateWidth(kw));
rows.add(_bottom_row.updateWidth(kw));
return new KeyboardData(rows, kw, extra_keys, num_pad);
}