Skip to content

Commit b4b276a

Browse files
committed
Refactor: Load DeviceLocales less often
Previously, [DeviceLocales.load()] was called everytime the keyboard was shown on the screen. This operation is moderately costly and only need to be done when the IME subtype changes.
1 parent adb7e6e commit b4b276a

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

srcs/juloo.keyboard2/Keyboard2.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class Keyboard2 extends InputMethodService
4141
private KeyboardData _currentSpecialLayout;
4242
/** Layout associated with the currently selected locale. Not 'null'. */
4343
private KeyboardData _localeTextLayout;
44+
/** Installed and current locales. */
45+
private DeviceLocales _device_locales;
4446
private Dictionaries _dictionaries;
4547
private ViewGroup _emojiPane = null;
4648
private ViewGroup _clipboard_pane = null;
@@ -126,6 +128,7 @@ public void onCreate()
126128
_config.handler = _keyeventhandler;
127129
prefs.registerOnSharedPreferenceChangeListener(this);
128130
Logs.set_debug_logs(getResources().getBoolean(R.bool.debug_logs));
131+
refreshSubtypeImm();
129132
create_keyboard_view();
130133
ClipboardHistoryService.on_startup(this, _keyeventhandler);
131134
_foldStateTracker.setChangedCallback(() -> { refresh_config(); });
@@ -154,26 +157,26 @@ private void refreshSubtypeImm()
154157
{
155158
_config.shouldOfferVoiceTyping = true;
156159
KeyboardData default_layout = null;
157-
DeviceLocales locales = DeviceLocales.load(this);
158-
if (locales.default_ != null)
160+
_device_locales = DeviceLocales.load(this);
161+
if (_device_locales.default_ != null)
159162
{
160-
String layout_name = locales.default_.default_layout;
163+
String layout_name = _device_locales.default_.default_layout;
161164
if (layout_name != null)
162165
default_layout = LayoutsPreference.layout_of_string(getResources(), layout_name);
163166
}
164-
_config.extra_keys_subtype = locales.extra_keys();
167+
_config.extra_keys_subtype = _device_locales.extra_keys();
165168
if (default_layout == null)
166169
default_layout = loadLayout(R.xml.latn_qwerty_us);
167170
_localeTextLayout = default_layout;
168-
refresh_current_dictionary(locales.default_);
169171
}
170172

171-
private void refresh_current_dictionary(DeviceLocales.Loc current_locale)
173+
private void refresh_current_dictionary()
172174
{
173175
_config.current_dictionary = null;
174-
if (current_locale.dictionary == null)
176+
String current = _device_locales.default_.dictionary;
177+
if (current == null)
175178
return;
176-
Cdict[] dicts = _dictionaries.load(current_locale.dictionary);
179+
Cdict[] dicts = _dictionaries.load(current);
177180
if (dicts == null)
178181
return;
179182
_config.current_dictionary = Dictionaries.find_by_name(dicts, "main");
@@ -195,7 +198,7 @@ private void refresh_config()
195198
{
196199
int prev_theme = _config.theme;
197200
_config.refresh(getResources(), _foldStateTracker.isUnfolded(), _dictionaries);
198-
refreshSubtypeImm();
201+
refresh_current_dictionary();
199202
// Refreshing the theme config requires re-creating the views
200203
if (prev_theme != _config.theme)
201204
{

0 commit comments

Comments
 (0)