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

View File

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

View File

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