Fix crash due to auto capitalisation

It is unclear how _autocap.started is not called first but nothing is
preventing to initialize things earlier.
This commit is contained in:
Jules Aguillon 2022-10-15 16:12:25 +02:00
parent 7f51cf001a
commit d9a8688237
2 changed files with 43 additions and 38 deletions

View File

@ -27,25 +27,10 @@ final class Autocapitalisation
InputType.TYPE_TEXT_FLAG_CAP_SENTENCES |
InputType.TYPE_TEXT_FLAG_CAP_WORDS;
Runnable delayed_callback = new Runnable()
public Autocapitalisation(Looper looper, Callback cb)
{
public void run()
{
if (_should_update_caps_mode)
{
_should_enable_shift = _enabled && (_ic.getCursorCapsMode(_caps_mode) != 0);
_should_update_caps_mode = false;
}
_callback.update_shift_state(_should_enable_shift, _should_disable_shift);
}
};
void callback(final boolean might_disable)
{
_should_disable_shift = might_disable;
// The callback must be delayed because [getCursorCapsMode] would sometimes
// be called before the editor finished handling the previous event.
_handler.postDelayed(delayed_callback, 1);
_handler = new Handler(looper);
_callback = cb;
}
/**
@ -53,11 +38,9 @@ final class Autocapitalisation
* [started] does initialisation work and must be called before any other
* event.
*/
public void started(Looper looper, Callback cb, EditorInfo info, InputConnection ic)
public void started(EditorInfo info, InputConnection ic)
{
_handler = new Handler(looper);
_ic = ic;
_callback = cb;
_caps_mode = info.inputType & TextUtils.CAP_MODE_SENTENCES;
if (!Config.globalConfig().autocapitalisation || _caps_mode == 0)
{
@ -82,15 +65,6 @@ final class Autocapitalisation
callback(false);
}
void type_one_char(char c)
{
_cursor++;
if (is_trigger_character(c))
_should_update_caps_mode = true;
else
_should_enable_shift = false;
}
public void event_sent(int code)
{
switch (code)
@ -103,12 +77,17 @@ final class Autocapitalisation
callback(true);
}
public static interface Callback
{
public void update_shift_state(boolean should_enable, boolean should_disable);
}
/** Returns [true] if shift might be disabled. */
public void selection_updated(int old_cursor, int new_cursor)
{
if (new_cursor == _cursor) // Just typing
return;
if (new_cursor == 0)
if (new_cursor == 0 && _ic != null)
{
// Detect whether the input box has been cleared
CharSequence t = _ic.getTextAfterCursor(1, 0);
@ -120,6 +99,36 @@ final class Autocapitalisation
callback(true);
}
Runnable delayed_callback = new Runnable()
{
public void run()
{
if (_should_update_caps_mode && _ic != null)
{
_should_enable_shift = _enabled && (_ic.getCursorCapsMode(_caps_mode) != 0);
_should_update_caps_mode = false;
}
_callback.update_shift_state(_should_enable_shift, _should_disable_shift);
}
};
void callback(final boolean might_disable)
{
_should_disable_shift = might_disable;
// The callback must be delayed because [getCursorCapsMode] would sometimes
// be called before the editor finished handling the previous event.
_handler.postDelayed(delayed_callback, 1);
}
void type_one_char(char c)
{
_cursor++;
if (is_trigger_character(c))
_should_update_caps_mode = true;
else
_should_enable_shift = false;
}
boolean is_trigger_character(char c)
{
switch (c)
@ -130,9 +139,4 @@ final class Autocapitalisation
return false;
}
}
public static interface Callback
{
public void update_shift_state(boolean should_enable, boolean should_disable);
}
}

View File

@ -36,7 +36,7 @@ public class Keyboard2 extends InputMethodService
private ViewGroup _emojiPane = null;
private Config _config;
private Autocapitalisation _autocap = new Autocapitalisation();
private Autocapitalisation _autocap;
private boolean _debug_logs = false;
@ -57,6 +57,7 @@ public class Keyboard2 extends InputMethodService
_keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard);
_keyboardView.reset();
_debug_logs = getResources().getBoolean(R.bool.debug_logs);
_autocap = new Autocapitalisation(getMainLooper(), this);
}
public void update_shift_state(boolean should_enable, boolean should_disable)
@ -233,7 +234,7 @@ public class Keyboard2 extends InputMethodService
_keyboardView.setKeyboard(getLayout(R.xml.numeric));
else
_keyboardView.setKeyboard(getLayout(_currentTextLayout));
_autocap.started(getMainLooper(), this, info, getCurrentInputConnection());
_autocap.started(info, getCurrentInputConnection());
setInputView(_keyboardView);
if (_debug_logs)
log_editor_info(info);