diff options
| author | Jules Aguillon | 2026-01-18 17:26:41 +0100 |
|---|---|---|
| committer | GitHub | 2026-01-18 17:26:41 +0100 |
| commit | a848897586223b050e87367b48ed2eebd0870d1d (patch) | |
| tree | 7a4a68886a27ee9bf37815597ceebc38e1d45f29 /srcs/juloo.keyboard2/DeviceLocales.java | |
| parent | 4d4c22b4e21505000d92fa3b6c1f400379c51cd4 (diff) | |
| download | unexpected-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/juloo.keyboard2/DeviceLocales.java')
| -rw-r--r-- | srcs/juloo.keyboard2/DeviceLocales.java | 83 |
1 files changed, 83 insertions, 0 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; + } +} |
