From 283bf7de6bc6a439df306466d1cea4af08218c19 Mon Sep 17 00:00:00 2001 From: Dimo Karaivanov Date: Wed, 7 Dec 2022 17:33:06 +0200 Subject: [PATCH] no more unnecessary throtling of the hardware backspace key --- .../github/sspanak/tt9/ime/KeyPadHandler.java | 41 ++++--------------- .../sspanak/tt9/ime/SoftKeyHandler.java | 17 +++++++- .../github/sspanak/tt9/ime/TraditionalT9.java | 5 ++- 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/io/github/sspanak/tt9/ime/KeyPadHandler.java b/src/io/github/sspanak/tt9/ime/KeyPadHandler.java index 9ffec7ed..e8e26e16 100644 --- a/src/io/github/sspanak/tt9/ime/KeyPadHandler.java +++ b/src/io/github/sspanak/tt9/ime/KeyPadHandler.java @@ -24,6 +24,8 @@ abstract class KeyPadHandler extends InputMethodService { protected int mEditing = NON_EDIT; // temporal key handling + private boolean backspaceHandled = false; + private int ignoreNextKeyUp = 0; private int lastKeyCode = 0; @@ -32,10 +34,6 @@ abstract class KeyPadHandler extends InputMethodService { private int lastNumKeyCode = 0; private int numKeyRepeatCounter = 0; - // throttling - private static final int BACKSPACE_DEBOUNCE_TIME = 80; - private long lastBackspaceCall = 0; - /** * Main initialization of the input method component. Be sure to call to @@ -135,18 +133,11 @@ abstract class KeyPadHandler extends InputMethodService { // Logger.d("onKeyDown", "Key: " + event + " repeat?: " + event.getRepeatCount() + " long-time: " + event.isLongPress()); // "backspace" key must repeat its function, when held down, so we handle it in a special way - // Also dialer fields seem to handle backspace on their own and we must ignore it, - // otherwise, keyDown race condition occur for all keys. - if (mEditing != EDITING_DIALER && keyCode == settings.getKeyBackspace()) { - boolean isThereTextBefore = InputFieldHelper.isThereText(currentInputConnection); - boolean backspaceHandleStatus = handleBackspaceHold(); - - // Allow BACK key to function as back when there is no text - if (keyCode == KeyEvent.KEYCODE_BACK) { - return isThereTextBefore; - } else { - return backspaceHandleStatus; - } + if (keyCode == settings.getKeyBackspace()) { + // When there is no more text, allow "Back" key to function normally, not to block navigation. + // All other keys are blocked, unless it turns out it is annoying this way. + backspaceHandled = onBackspace() || keyCode != KeyEvent.KEYCODE_BACK; + return backspaceHandled; } // In numeric fields, we do not want to handle anything, but "backspace" @@ -249,11 +240,7 @@ abstract class KeyPadHandler extends InputMethodService { // Logger.d("onKeyUp", "Key: " + keyCode + " repeat?: " + event.getRepeatCount()); - if ( - mEditing != EDITING_DIALER // dialer fields seem to handle backspace on their own - && keyCode == settings.getKeyBackspace() - && InputFieldHelper.isThereText(currentInputConnection) - ) { + if (backspaceHandled && keyCode == settings.getKeyBackspace()) { return true; } @@ -300,18 +287,6 @@ abstract class KeyPadHandler extends InputMethodService { } - protected boolean handleBackspaceHold() { - if (System.currentTimeMillis() - lastBackspaceCall < BACKSPACE_DEBOUNCE_TIME) { - return true; - } - - boolean handled = onBackspace(); - lastBackspaceCall = System.currentTimeMillis(); - - return handled; - } - - private boolean handleSpecialFunctionKey(int keyCode, boolean hold) { if (keyCode == settings.getKeyAddWord() * (hold ? -1 : 1)) { return onKeyAddWord(); diff --git a/src/io/github/sspanak/tt9/ime/SoftKeyHandler.java b/src/io/github/sspanak/tt9/ime/SoftKeyHandler.java index 51aff3f2..01064ba1 100644 --- a/src/io/github/sspanak/tt9/ime/SoftKeyHandler.java +++ b/src/io/github/sspanak/tt9/ime/SoftKeyHandler.java @@ -17,6 +17,9 @@ class SoftKeyHandler implements View.OnTouchListener { private final TraditionalT9 tt9; private View view = null; + private static final int BACKSPACE_DEBOUNCE_TIME = 40; + private long lastBackspaceCall = 0; + public SoftKeyHandler(TraditionalT9 tt9) { this.tt9 = tt9; @@ -101,6 +104,18 @@ class SoftKeyHandler implements View.OnTouchListener { } + protected boolean handleBackspaceHold() { + if (System.currentTimeMillis() - lastBackspaceCall < BACKSPACE_DEBOUNCE_TIME) { + return true; + } + + boolean handled = tt9.onBackspace(); + lastBackspaceCall = System.currentTimeMillis(); + + return handled; + } + + @Override public boolean onTouch(View view, MotionEvent event) { int action = event.getAction(); @@ -117,7 +132,7 @@ class SoftKeyHandler implements View.OnTouchListener { } if (buttonId == R.id.main_right && action == MotionEvent.AXIS_PRESSURE) { - return tt9.handleBackspaceHold(); + return handleBackspaceHold(); } return false; diff --git a/src/io/github/sspanak/tt9/ime/TraditionalT9.java b/src/io/github/sspanak/tt9/ime/TraditionalT9.java index d997aee8..7f713e2a 100644 --- a/src/io/github/sspanak/tt9/ime/TraditionalT9.java +++ b/src/io/github/sspanak/tt9/ime/TraditionalT9.java @@ -130,7 +130,10 @@ public class TraditionalT9 extends KeyPadHandler { public boolean onBackspace() { - if (!InputFieldHelper.isThereText(currentInputConnection)) { + // 1. Dialer fields seem to handle backspace on their own and we must ignore it, + // otherwise, keyDown race condition occur for all keys. + // 2. Allow the assigned key to function normally, when there is no text (e.g. "Back" navigates back) + if (mEditing == EDITING_DIALER || !InputFieldHelper.isThereText(currentInputConnection)) { Logger.d("onBackspace", "backspace ignored"); mInputMode.reset(); return false;