abouttreesummaryrefslogcommitdiff
path: root/srcs
diff options
context:
space:
mode:
authorJules Aguillon2026-01-18 17:26:41 +0100
committerGitHub2026-01-18 17:26:41 +0100
commita848897586223b050e87367b48ed2eebd0870d1d (patch)
tree7a4a68886a27ee9bf37815597ceebc38e1d45f29 /srcs
parent4d4c22b4e21505000d92fa3b6c1f400379c51cd4 (diff)
downloadunexpected-keyboard-a848897586223b050e87367b48ed2eebd0870d1d.tar.gz
unexpected-keyboard-a848897586223b050e87367b48ed2eebd0870d1d.zip
Refactor: Split DeviceLocales out of Keyboard2 (#1154)
* Refactor: Split DeviceLocales out of Keyboard2 This moves the code from Keyboard2 that handles the active IME subtypes and the current one. The goal is to access this information from the dictionary activity while simplifying the code of Keyboard2. * Refresh the candidates view when subtype changes This fixes the status message being inconsistently shown. * 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. * EditorConfig: Fix crash on Android 9
Diffstat (limited to 'srcs')
-rw-r--r--srcs/juloo.keyboard2/DeviceLocales.java83
-rw-r--r--srcs/juloo.keyboard2/EditorConfig.java13
-rw-r--r--srcs/juloo.keyboard2/Keyboard2.java72
3 files changed, 107 insertions, 61 deletions
diff --git a/srcs/juloo.keyboard2/DeviceLocales.java b/srcs/juloo.keyboard2/DeviceLocales.java
new file mode 100644
index 0000000..a6cfdaf
--- /dev/null
+++ b/srcs/juloo.keyboard2/DeviceLocales.java
@@ -0,0 +1,83 @@
+package juloo.keyboard2;
+
+import android.content.Context;
+import android.os.Build.VERSION;
+import android.view.inputmethod.InputMethodInfo;
+import android.view.inputmethod.InputMethodManager;
+import android.view.inputmethod.InputMethodSubtype;
+import java.util.ArrayList;
+import java.util.List;
+
+public final class DeviceLocales
+{
+ public final List<Loc> installed;
+ public final Loc default_;
+
+ public static DeviceLocales load(Context ctx)
+ {
+ InputMethodManager imm =
+ (InputMethodManager)ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
+ List<Loc> locs = get_installed_locales(ctx.getPackageName(), imm);
+ return new DeviceLocales(locs, current_locale(imm, locs));
+ }
+
+ /** Extra keys required by all the installed locales. */
+ public ExtraKeys extra_keys()
+ {
+ List<ExtraKeys> extra_keys = new ArrayList<ExtraKeys>();
+ for (Loc l : installed)
+ extra_keys.add(l.extra_keys);
+ return ExtraKeys.merge(extra_keys);
+ }
+
+ public static final class Loc
+ {
+ public final String lang_tag;
+ public final String script;
+ public final String default_layout; // Might be [null]
+ public final ExtraKeys extra_keys;
+
+ public Loc(InputMethodSubtype st)
+ {
+ lang_tag = st.getLanguageTag();
+ script = st.getExtraValueOf("script");
+ default_layout = st.getExtraValueOf("default_layout");
+ String extra_keys_s = st.getExtraValueOf("extra_keys");
+ extra_keys = (extra_keys_s != null) ?
+ ExtraKeys.parse(script, extra_keys_s) : ExtraKeys.EMPTY;
+ }
+ }
+
+ private DeviceLocales(List<Loc> locs, Loc def)
+ { installed = locs; default_ = def; }
+
+ private static List<Loc> get_installed_locales(String pkg, InputMethodManager imm)
+ {
+ List<Loc> locs = new ArrayList<Loc>();
+ for (InputMethodInfo imi : imm.getEnabledInputMethodList())
+ if (imi.getPackageName().equals(pkg))
+ {
+ for (InputMethodSubtype subtype :
+ imm.getEnabledInputMethodSubtypeList(imi, true))
+ locs.add(new Loc(subtype));
+ break;
+ }
+ return locs;
+ }
+
+ private static Loc current_locale(InputMethodManager imm, List<Loc> installed)
+ {
+ // Android might return a random subtype, for example, the first in the
+ // list alphabetically.
+ InputMethodSubtype current_subtype = imm.getCurrentInputMethodSubtype();
+ if (current_subtype == null)
+ return null;
+ if (VERSION.SDK_INT < 24)
+ return new Loc(current_subtype);
+ String default_lang_tag = current_subtype.getLanguageTag();
+ for (Loc l : installed)
+ if (l.lang_tag.equals(default_lang_tag))
+ return l;
+ return null;
+ }
+}
diff --git a/srcs/juloo.keyboard2/EditorConfig.java b/srcs/juloo.keyboard2/EditorConfig.java
index bdfab3c..2bfbcef 100644
--- a/srcs/juloo.keyboard2/EditorConfig.java
+++ b/srcs/juloo.keyboard2/EditorConfig.java
@@ -1,9 +1,11 @@
package juloo.keyboard2;
import android.content.res.Resources;
+import android.os.Build.VERSION;
import android.text.InputType;
import android.text.TextUtils;
import android.view.inputmethod.EditorInfo;
+import juloo.keyboard2.CandidatesView;
public final class EditorConfig
{
@@ -27,10 +29,14 @@ public final class EditorConfig
public boolean caps_initially_updated = false;
/** CurrentlyTypedWord. */
- public CharSequence initial_text_before_cursor = null;
+ public CharSequence initial_text_before_cursor = null; // Might be [null].
public int initial_sel_start;
public int initial_sel_end;
+ /** Suggestions. */
+ // Doesn't override [_config.suggestions_enabled].
+ public boolean should_show_candidates_view;
+
public EditorConfig() {}
public void refresh(EditorInfo info, Resources res)
@@ -72,9 +78,12 @@ public final class EditorConfig
caps_initially_enabled = (info.initialCapsMode != 0);
caps_initially_updated = caps_should_update_state(info);
/* CurrentlyTypedWord */
- initial_text_before_cursor = info.getInitialTextBeforeCursor(10, 0);
+ if (VERSION.SDK_INT >= 30)
+ initial_text_before_cursor = info.getInitialTextBeforeCursor(10, 0);
initial_sel_start = info.initialSelStart;
initial_sel_end = info.initialSelEnd;
+ /* Suggestions */
+ should_show_candidates_view = CandidatesView.should_show(info);
}
String actionLabel_of_imeAction(int action, Resources res)
diff --git a/srcs/juloo.keyboard2/Keyboard2.java b/srcs/juloo.keyboard2/Keyboard2.java
index a2940d5..a919405 100644
--- a/srcs/juloo.keyboard2/Keyboard2.java
+++ b/srcs/juloo.keyboard2/Keyboard2.java
@@ -13,7 +13,6 @@ import android.util.LogPrinter;
import android.view.*;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
-import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.widget.FrameLayout;
@@ -38,6 +37,8 @@ public class Keyboard2 extends InputMethodService
private KeyboardData _currentSpecialLayout;
/** Layout associated with the currently selected locale. Not 'null'. */
private KeyboardData _localeTextLayout;
+ /** Installed and current locales. */
+ private DeviceLocales _device_locales;
private ViewGroup _emojiPane = null;
private ViewGroup _clipboard_pane = null;
private Handler _handler;
@@ -119,6 +120,7 @@ public class Keyboard2 extends InputMethodService
prefs.registerOnSharedPreferenceChangeListener(this);
_config = Config.globalConfig();
Logs.set_debug_logs(getResources().getBoolean(R.bool.debug_logs));
+ refreshSubtypeImm();
create_keyboard_view();
ClipboardHistoryService.on_startup(this, _keyeventhandler);
_foldStateTracker.setChangedCallback(() -> { refresh_config(); });
@@ -138,79 +140,31 @@ public class Keyboard2 extends InputMethodService
_candidates_view = (CandidatesView)_container_view.findViewById(R.id.candidates_view);
}
- private List<InputMethodSubtype> getEnabledSubtypes(InputMethodManager imm)
- {
- String pkg = getPackageName();
- for (InputMethodInfo imi : imm.getEnabledInputMethodList())
- if (imi.getPackageName().equals(pkg))
- return imm.getEnabledInputMethodSubtypeList(imi, true);
- return Arrays.asList();
- }
-
- private ExtraKeys extra_keys_of_subtype(InputMethodSubtype subtype)
- {
- String extra_keys = subtype.getExtraValueOf("extra_keys");
- String script = subtype.getExtraValueOf("script");
- if (extra_keys != null)
- return ExtraKeys.parse(script, extra_keys);
- return ExtraKeys.EMPTY;
- }
-
- private void refreshAccentsOption(InputMethodManager imm, List<InputMethodSubtype> enabled_subtypes)
- {
- List<ExtraKeys> extra_keys = new ArrayList<ExtraKeys>();
- for (InputMethodSubtype s : enabled_subtypes)
- extra_keys.add(extra_keys_of_subtype(s));
- _config.extra_keys_subtype = ExtraKeys.merge(extra_keys);
- }
-
InputMethodManager get_imm()
{
return (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
}
- private InputMethodSubtype defaultSubtypes(InputMethodManager imm, List<InputMethodSubtype> enabled_subtypes)
- {
- if (VERSION.SDK_INT < 24)
- return imm.getCurrentInputMethodSubtype();
- // Android might return a random subtype, for example, the first in the
- // list alphabetically.
- InputMethodSubtype current_subtype = imm.getCurrentInputMethodSubtype();
- if (current_subtype == null)
- return null;
- for (InputMethodSubtype s : enabled_subtypes)
- if (s.getLanguageTag().equals(current_subtype.getLanguageTag()))
- return s;
- return null;
- }
-
private void refreshSubtypeImm()
{
- InputMethodManager imm = get_imm();
_config.shouldOfferVoiceTyping = true;
KeyboardData default_layout = null;
- _config.extra_keys_subtype = null;
- if (VERSION.SDK_INT >= 12)
+ _device_locales = DeviceLocales.load(this);
+ if (_device_locales.default_ != null)
{
- List<InputMethodSubtype> enabled_subtypes = getEnabledSubtypes(imm);
- InputMethodSubtype subtype = defaultSubtypes(imm, enabled_subtypes);
- if (subtype != null)
- {
- String s = subtype.getExtraValueOf("default_layout");
- if (s != null)
- default_layout = LayoutsPreference.layout_of_string(getResources(), s);
- refreshAccentsOption(imm, enabled_subtypes);
- }
+ String layout_name = _device_locales.default_.default_layout;
+ if (layout_name != null)
+ default_layout = LayoutsPreference.layout_of_string(getResources(), layout_name);
}
+ _config.extra_keys_subtype = _device_locales.extra_keys();
if (default_layout == null)
default_layout = loadLayout(R.xml.latn_qwerty_us);
_localeTextLayout = default_layout;
}
- private void refresh_candidates_view(EditorInfo info)
+ private void refresh_candidates_view()
{
- boolean should_show = CandidatesView.should_show(info);
- _config.should_show_candidates_view = should_show;
+ boolean should_show = _config.editor_config.should_show_candidates_view;
_candidates_view.setVisibility(should_show ? View.VISIBLE : View.GONE);
}
@@ -220,7 +174,6 @@ public class Keyboard2 extends InputMethodService
{
int prev_theme = _config.theme;
_config.refresh(getResources(), _foldStateTracker.isUnfolded());
- refreshSubtypeImm();
// Refreshing the theme config requires re-creating the views
if (prev_theme != _config.theme)
{
@@ -252,7 +205,7 @@ public class Keyboard2 extends InputMethodService
{
_config.editor_config.refresh(info, getResources());
refresh_config();
- refresh_candidates_view(info);
+ refresh_candidates_view();
_currentSpecialLayout = refresh_special_layout();
_keyboardView.setKeyboard(current_layout());
_keyeventhandler.started(_config);
@@ -339,6 +292,7 @@ public class Keyboard2 extends InputMethodService
public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype)
{
refreshSubtypeImm();
+ refresh_candidates_view();
_keyboardView.setKeyboard(current_layout());
}