Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions srcs/juloo.keyboard2/DeviceLocales.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 11 additions & 2 deletions srcs/juloo.keyboard2/EditorConfig.java
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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)
Expand Down Expand Up @@ -72,9 +78,12 @@ public void refresh(EditorInfo info, Resources res)
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)
Expand Down
72 changes: 13 additions & 59 deletions srcs/juloo.keyboard2/Keyboard2.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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;
Expand All @@ -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;
Expand Down Expand Up @@ -119,6 +120,7 @@ public void onCreate()
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(); });
Expand All @@ -138,79 +140,31 @@ private void create_keyboard_view()
_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);
}

Expand All @@ -220,7 +174,6 @@ private void refresh_config()
{
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)
{
Expand Down Expand Up @@ -252,7 +205,7 @@ public void onStartInputView(EditorInfo info, boolean restarting)
{
_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);
Expand Down Expand Up @@ -339,6 +292,7 @@ private static void updateLayoutGravityOf(final View view, final int layoutGravi
public void onCurrentInputMethodSubtypeChanged(InputMethodSubtype subtype)
{
refreshSubtypeImm();
refresh_candidates_view();
_keyboardView.setKeyboard(current_layout());
}

Expand Down