diff --git a/app/src/main/java/io/github/sspanak/tt9/ime/KeyPadHandler.java b/app/src/main/java/io/github/sspanak/tt9/ime/KeyPadHandler.java index 9c3a05ab..8ae0dccc 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ime/KeyPadHandler.java +++ b/app/src/main/java/io/github/sspanak/tt9/ime/KeyPadHandler.java @@ -6,6 +6,8 @@ import android.view.View; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputConnection; +import java.util.HashMap; + import io.github.sspanak.tt9.Logger; import io.github.sspanak.tt9.ime.helpers.Key; import io.github.sspanak.tt9.preferences.SettingsStore; @@ -14,6 +16,9 @@ import io.github.sspanak.tt9.preferences.SettingsStore; abstract class KeyPadHandler extends InputMethodService { protected SettingsStore settings; + // debounce handling + private final HashMap lastKeyTime = new HashMap<>(); + // temporal key handling private boolean isBackspaceHandled = false; @@ -112,6 +117,10 @@ abstract class KeyPadHandler extends InputMethodService { */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { + if (debounceKey(keyCode, event)) { + return true; + } + if (shouldBeOff()) { return false; } @@ -187,6 +196,10 @@ abstract class KeyPadHandler extends InputMethodService { */ @Override public boolean onKeyUp(int keyCode, KeyEvent event) { + if (debounceKey(keyCode, event)) { + return true; + } + if (shouldBeOff()) { return false; } @@ -273,6 +286,26 @@ abstract class KeyPadHandler extends InputMethodService { } + private boolean debounceKey(int keyCode, KeyEvent event) { + if (settings.getKeyPadDebounceTime() <= 0 || event.isLongPress()) { + return false; + } + + long now = System.currentTimeMillis(); + Long lastTime = lastKeyTime.get(keyCode); + + if (lastTime != null && now - lastTime < settings.getKeyPadDebounceTime()) { + return true; + } + + if (event.getAction() == KeyEvent.ACTION_UP) { + lastKeyTime.put(keyCode, now); + } + + return false; + } + + // hardware key handlers abstract protected boolean onBack(); abstract public boolean onBackspace(); diff --git a/app/src/main/java/io/github/sspanak/tt9/preferences/SettingsStore.java b/app/src/main/java/io/github/sspanak/tt9/preferences/SettingsStore.java index d5027592..3724163e 100644 --- a/app/src/main/java/io/github/sspanak/tt9/preferences/SettingsStore.java +++ b/app/src/main/java/io/github/sspanak/tt9/preferences/SettingsStore.java @@ -256,7 +256,7 @@ public class SettingsStore { /************* typing settings *************/ - public int getAbcAutoAcceptTimeout() { return prefs.getBoolean("abc_auto_accept", true) ? 800 : -1; } + public int getAbcAutoAcceptTimeout() { return prefs.getBoolean("abc_auto_accept", true) ? 800 + getKeyPadDebounceTime() : -1; } public boolean getAutoSpace() { return prefs.getBoolean("auto_space", true); } public boolean getAutoTextCase() { return prefs.getBoolean("auto_text_case", true); } public String getDoubleZeroChar() { @@ -275,7 +275,6 @@ public class SettingsStore { public final static int DICTIONARY_IMPORT_BATCH_SIZE = 5000; // words public final static int DICTIONARY_IMPORT_PROGRESS_UPDATE_TIME = 250; // ms public final static int DICTIONARY_MISSING_WARNING_INTERVAL = 30000; // ms - public final static int PREFERENCES_CLICK_DEBOUNCE_TIME = 250; // ms public final static byte SLOW_QUERY_TIME = 50; // ms public final static int SOFT_KEY_REPEAT_DELAY = 40; // ms public final static float SOFT_KEY_COMPLEX_LABEL_TITLE_SIZE = 0.55f; @@ -304,4 +303,20 @@ public class SettingsStore { public boolean getGoogleChatHack() { return prefs.getBoolean("pref_hack_google_chat", false); } + + /** + * Protection against faulty devices, that sometimes send two (or more) click events + * per a single key press, which absolutely undesirable side effects. + * There were reports about this on Kyocera KYF31 + * and on CAT S22. + */ + public final static int PREFERENCES_CLICK_DEBOUNCE_TIME = 250; // ms + + public int getKeyPadDebounceTime() { + try { + return Integer.parseInt(prefs.getString("pref_key_pad_debounce_time", "0")); + } catch (NumberFormatException e) { + return 0; + } + } } diff --git a/app/src/main/java/io/github/sspanak/tt9/preferences/items/ItemClickable.java b/app/src/main/java/io/github/sspanak/tt9/preferences/items/ItemClickable.java index d1cfec36..adfdbbce 100644 --- a/app/src/main/java/io/github/sspanak/tt9/preferences/items/ItemClickable.java +++ b/app/src/main/java/io/github/sspanak/tt9/preferences/items/ItemClickable.java @@ -57,16 +57,6 @@ abstract public class ItemClickable { } - - /** - * debounceClick - * Protection against faulty devices, that sometimes send two (or more) click events - * per a single key press. - * - * My smashed Qin F21 Pro+ occasionally does this, if I press the keys hard. - * There were reports the same happens on Kyocera KYF31, causing absolutely undesirable side effects. - * See: ... - */ protected boolean debounceClick(Preference p) { long now = System.currentTimeMillis(); if (now - lastClickTime < SettingsStore.PREFERENCES_CLICK_DEBOUNCE_TIME) { diff --git a/app/src/main/java/io/github/sspanak/tt9/preferences/items/ItemDropDown.java b/app/src/main/java/io/github/sspanak/tt9/preferences/items/ItemDropDown.java index e2440ddb..ef2edd4e 100644 --- a/app/src/main/java/io/github/sspanak/tt9/preferences/items/ItemDropDown.java +++ b/app/src/main/java/io/github/sspanak/tt9/preferences/items/ItemDropDown.java @@ -33,6 +33,20 @@ public class ItemDropDown { item.setEntries(this.values.values().toArray(new CharSequence[0])); } + public void disable() { + if (item != null) { + item.setEnabled(false); + item.setOnPreferenceChangeListener(null); + } + } + + public void enable() { + if (item != null) { + item.setEnabled(true); + enableClickHandler(); + } + } + public ItemDropDown enableClickHandler() { if (item == null) { Logger.w("SectionKeymap.populateItem", "Cannot set a click listener a NULL item. Ignoring."); @@ -63,7 +77,7 @@ public class ItemDropDown { public void preview() { try { setPreview(values.get(Integer.parseInt(item.getValue()))); - } catch (NumberFormatException e) { + } catch (Exception e) { setPreview(""); } } diff --git a/app/src/main/java/io/github/sspanak/tt9/preferences/items/ItemKeyPadDebounceTime.java b/app/src/main/java/io/github/sspanak/tt9/preferences/items/ItemKeyPadDebounceTime.java new file mode 100644 index 00000000..0c6c9c76 --- /dev/null +++ b/app/src/main/java/io/github/sspanak/tt9/preferences/items/ItemKeyPadDebounceTime.java @@ -0,0 +1,33 @@ +package io.github.sspanak.tt9.preferences.items; + +import android.content.Context; + +import androidx.preference.DropDownPreference; + +import java.util.LinkedHashMap; + +import io.github.sspanak.tt9.R; + +public class ItemKeyPadDebounceTime extends ItemDropDown { + public static final String NAME = "pref_key_pad_debounce_time"; + + private final Context context; + + public ItemKeyPadDebounceTime(Context context, DropDownPreference item) { + super(item); + this.context = context; + } + + public ItemDropDown populate() { + LinkedHashMap dropDownOptions = new LinkedHashMap<>(); + dropDownOptions.put(0, context.getString(R.string.pref_hack_key_pad_debounce_off)); + + int[] values = new int[] { 33, 50, 100, 150, 250, 350 }; + for (int value : values) { + dropDownOptions.put(value, value + " ms"); + } + super.populate(dropDownOptions); + + return this; + } +} diff --git a/app/src/main/java/io/github/sspanak/tt9/preferences/screens/SetupScreen.java b/app/src/main/java/io/github/sspanak/tt9/preferences/screens/SetupScreen.java index 134eb21c..b2e28b27 100644 --- a/app/src/main/java/io/github/sspanak/tt9/preferences/screens/SetupScreen.java +++ b/app/src/main/java/io/github/sspanak/tt9/preferences/screens/SetupScreen.java @@ -5,6 +5,7 @@ import androidx.preference.Preference; import io.github.sspanak.tt9.R; import io.github.sspanak.tt9.ime.helpers.SystemSettings; import io.github.sspanak.tt9.preferences.PreferencesActivity; +import io.github.sspanak.tt9.preferences.items.ItemKeyPadDebounceTime; import io.github.sspanak.tt9.preferences.items.ItemSelectGlobalKeyboard; import io.github.sspanak.tt9.preferences.items.ItemSetDefaultGlobalKeyboard; @@ -17,18 +18,18 @@ public class SetupScreen extends BaseScreenFragment { @Override public void onCreate() { - createKeyboardSection(); + boolean isTT9On = SystemSettings.isTT9Enabled(activity); + createKeyboardSection(isTT9On); + createHacksSection(isTT9On); } @Override public void onResume() { super.onResume(); - createKeyboardSection(); + onCreate(); } - private void createKeyboardSection() { - boolean isTT9On = SystemSettings.isTT9Enabled(activity); - + private void createKeyboardSection(boolean isTT9On) { Preference statusItem = findPreference("global_tt9_status"); if (statusItem != null) { statusItem.setSummary( @@ -43,4 +44,29 @@ public class SetupScreen extends BaseScreenFragment { new ItemSetDefaultGlobalKeyboard(defaultKeyboardItem, activity).enableClickHandler(); } } + + private void createHacksSection(boolean isTT9On) { + Preference altScrolling = findPreference("pref_alternative_suggestion_scrolling"); + if (altScrolling != null) { + altScrolling.setEnabled(isTT9On); + } + + Preference hackGoogleChat = findPreference("pref_hack_google_chat"); + if (hackGoogleChat != null) { + hackGoogleChat.setEnabled(isTT9On); + } + + Preference hackFBMessenger = findPreference("pref_hack_fb_messenger"); + if (hackFBMessenger != null) { + hackFBMessenger.setEnabled(isTT9On); + } + + ItemKeyPadDebounceTime item = new ItemKeyPadDebounceTime(activity, findPreference(ItemKeyPadDebounceTime.NAME)); + item.populate().preview(); + if (isTT9On) { + item.enable(); + } else { + item.disable(); + } + } } diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index f753ccee..7f4d1417 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -82,4 +82,6 @@ Не Автоматично Да се добави ли „%1$s“ към %2$s? + Защита от многократно натискане + Изключена diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index ca39a9c6..7592a23d 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -34,4 +34,6 @@ Wenn Ihnen %1$s gefällt, könnten Sie die Entwicklung auf %2$s unterstützen. Mit \"OK\" in Facebook Messenger senden Nachrichten mit \"OK\" in Google Chat senden + Mehrfacher Pressschutz + Aus diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index a1a1d6d5..55c3a7c3 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -78,4 +78,6 @@ Si te gusta %1$s, podrías apoyar su desarrollo en: %2$s. Enviar con «OK» en Facebook Messenger Enviar mensajes con «OK» en Google Chat + Protección con múltiples clics + Apagado diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 0da2264c..0f8f0181 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -76,4 +76,6 @@ Ajouter mot « %1$s » à %2$s? Donner Si vous aimez %1$s vous pouvez soutenir son développement à : %2$s + Protection multi-presse + Désactivée diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 30d92fb8..fe8b2028 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -43,5 +43,7 @@ Se ti piace %1$s, potresti supportarne lo sviluppo su: %2$s. Inviare con \"OK\" su Facebook Messenger Inviare messaggi con \"OK\" su Google Chat + Protezione multi-pressione + Spento diff --git a/app/src/main/res/values-iw/strings.xml b/app/src/main/res/values-iw/strings.xml index 763b2021..24d66161 100644 --- a/app/src/main/res/values-iw/strings.xml +++ b/app/src/main/res/values-iw/strings.xml @@ -71,4 +71,6 @@ אם אתה אוהב את %1$s, תוכל לתמוך בפיתוח שלו בכתובת: %2$s שלח עם \"OK\" ב-Facebook Messenger. שלח הודעות עם \"OK\" ב-Google Chat + הגנה מרובה לחיצות + כבוי diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index a881c27a..57c36e1e 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -34,4 +34,6 @@ Als je %1$s leuk vindt, zou je de ontwikkeling kunnen ondersteunen op: %2$s. Verstuur met \"OK\" in Facebook Messenger Stuur berichten met \"OK\" in Google Chat + Bescherming tegen meervoudig persen + Uit diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index a8120a3b..2b6da64b 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -67,4 +67,6 @@ Se você gosta de %1$s, você poderia apoiar o seu desenvolvimento em: %2$s. Enviar com \"OK\" no Facebook Messenger Enviar mensagens com \"OK\" no Google Chat + Proteção multiclique + Desligado diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 545ecb99..cf8b5d9b 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -82,4 +82,6 @@ Поддержать Если вам нравится %1$s, вы можете поддержать его разработку по: %2$s. Отправка сообщения с «ОК» в Google Chat + Защита от многократного нажатия + Выключена diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index 82e58f31..9585bdf1 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -82,4 +82,6 @@ Підтримуйте Якщо вам подобається %1$s, ви можете підтримати його розробку за: %2$s. Надсилати повідомлення з «ОК» до Google Chat + Захист від багаторазового натискання + Вимкнено diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 400850c9..93d4dd0d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -47,6 +47,8 @@ Character for Double 0-key Press Send messages with \"OK\" in Google Chat Send with \"OK\" in Facebook Messenger + Multi-Press Protection + Off Show On-Screen Keys Show On-Screen Numpad (BETA) diff --git a/app/src/main/res/xml/prefs_screen_setup.xml b/app/src/main/res/xml/prefs_screen_setup.xml index d409f017..6dfcc22d 100644 --- a/app/src/main/res/xml/prefs_screen_setup.xml +++ b/app/src/main/res/xml/prefs_screen_setup.xml @@ -1,6 +1,5 @@ - + +