From 1fa1e7c6e64d227be1e2a766918bb47e18469e9a Mon Sep 17 00:00:00 2001 From: Dimo Karaivanov Date: Fri, 28 Apr 2023 09:31:50 +0300 Subject: [PATCH] Small fixes (#242) * fixed the main view not showing up in some cases * fixed crashing when changing the theme * fixed 1-key not working after using it in 123 mode, then switching to ABC mode * fixed auto-space not working with the on screen punctuation keys * fixed on-screen punctuation keys erasing the previously typed, but unaccepted word * fixed more auto-space issues * fixed key repeating being reset incorrectly when holding some key * fixed pressing 1-key accepted even if there are no more emoji, in Predictive Mode --- .../github/sspanak/tt9/ime/KeyPadHandler.java | 9 ++++++++- .../github/sspanak/tt9/ime/TraditionalT9.java | 19 +++++++++++------- .../sspanak/tt9/ime/modes/InputMode.java | 2 +- .../sspanak/tt9/ime/modes/ModePredictive.java | 5 +++-- .../tt9/ime/modes/helpers/AutoSpace.java | 20 +++++++++---------- .../tt9/ime/modes/helpers/Predictions.java | 4 ++++ .../tt9/preferences/PreferencesActivity.java | 9 ++++++++- .../screens/MainSettingsScreen.java | 1 + 8 files changed, 47 insertions(+), 22 deletions(-) diff --git a/src/io/github/sspanak/tt9/ime/KeyPadHandler.java b/src/io/github/sspanak/tt9/ime/KeyPadHandler.java index 62c4049e..8145af10 100644 --- a/src/io/github/sspanak/tt9/ime/KeyPadHandler.java +++ b/src/io/github/sspanak/tt9/ime/KeyPadHandler.java @@ -50,6 +50,7 @@ abstract class KeyPadHandler extends InputMethodService { @Override public boolean onEvaluateInputViewShown() { super.onEvaluateInputViewShown(); + onRestart(getCurrentInputEditorInfo()); return mEditing != EDITING_DIALER && mEditing != NON_EDIT; } @@ -170,8 +171,14 @@ abstract class KeyPadHandler extends InputMethodService { return true; } - resetKeyRepeat(); ignoreNextKeyUp = keyCode; + if (Key.isNumber(keyCode)) { + numKeyRepeatCounter = 0; + lastNumKeyCode = 0; + } else { + keyRepeatCounter = 0; + lastKeyCode = 0; + } if (handleHotkey(keyCode, true)) { return true; diff --git a/src/io/github/sspanak/tt9/ime/TraditionalT9.java b/src/io/github/sspanak/tt9/ime/TraditionalT9.java index d3cb7122..e3449571 100644 --- a/src/io/github/sspanak/tt9/ime/TraditionalT9.java +++ b/src/io/github/sspanak/tt9/ime/TraditionalT9.java @@ -213,7 +213,7 @@ public class TraditionalT9 extends KeyPadHandler { mInputMode.onAcceptSuggestion(word); commitCurrentSuggestion(); - autoCorrectSpace(word, true, -1, false, false); + autoCorrectSpace(word, true); resetKeyRepeat(); return true; @@ -287,7 +287,7 @@ public class TraditionalT9 extends KeyPadHandler { // Automatically accept the current word, when the next one is a space or punctuation, // instead of requiring "OK" before that. if (mInputMode.shouldAcceptCurrentSuggestion(key, hold, repeat > 0)) { - autoCorrectSpace(acceptIncompleteSuggestion(), false, key, hold, repeat > 0); + autoCorrectSpace(acceptIncompleteSuggestion(), false); currentWord = ""; } @@ -319,7 +319,7 @@ public class TraditionalT9 extends KeyPadHandler { return false; } - autoCorrectSpace(acceptIncompleteSuggestion(), false, -1, false, false); + autoCorrectSpace(acceptIncompleteSuggestion(), false); sendDownUpKeyEvents(keyCode); return true; } @@ -330,9 +330,13 @@ public class TraditionalT9 extends KeyPadHandler { return false; } - autoCorrectSpace(acceptIncompleteSuggestion(), false, -1, false, false); + // accept the previously typed word (if any) + autoCorrectSpace(acceptIncompleteSuggestion(), false); + + // "type" and accept the text + mInputMode.onAcceptSuggestion(text); textField.setText(text); - autoCorrectSpace(text, false, -1, false, false); + autoCorrectSpace(text, true); return true; } @@ -556,6 +560,7 @@ public class TraditionalT9 extends KeyPadHandler { mInputMode = InputMode.getInstance(settings, mLanguage, allowedInputModes.get(modeIndex)); mInputMode.defaultTextCase(); + resetKeyRepeat(); } // save the settings for the next time @@ -623,12 +628,12 @@ public class TraditionalT9 extends KeyPadHandler { } - private void autoCorrectSpace(String currentWord, boolean isWordAcceptedManually, int incomingKey, boolean hold, boolean repeat) { + private void autoCorrectSpace(String currentWord, boolean isWordAcceptedManually) { if (mInputMode.shouldDeletePrecedingSpace(inputType)) { textField.deletePrecedingSpace(currentWord); } - if (mInputMode.shouldAddAutoSpace(inputType, textField, isWordAcceptedManually, incomingKey, hold, repeat)) { + if (mInputMode.shouldAddAutoSpace(inputType, textField, isWordAcceptedManually)) { textField.setText(" "); } } diff --git a/src/io/github/sspanak/tt9/ime/modes/InputMode.java b/src/io/github/sspanak/tt9/ime/modes/InputMode.java index bf9ea330..56946bd8 100644 --- a/src/io/github/sspanak/tt9/ime/modes/InputMode.java +++ b/src/io/github/sspanak/tt9/ime/modes/InputMode.java @@ -84,7 +84,7 @@ abstract public class InputMode { // Interaction with the IME. Return "true" if it should perform the respective action. public boolean shouldAcceptCurrentSuggestion(int key, boolean hold, boolean repeat) { return false; } - public boolean shouldAddAutoSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually, int incomingKey, boolean hold, boolean repeat) { return false; } + public boolean shouldAddAutoSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually) { return false; } public boolean shouldDeletePrecedingSpace(InputType inputType) { return false; } public boolean shouldSelectNextSuggestion() { return false; } diff --git a/src/io/github/sspanak/tt9/ime/modes/ModePredictive.java b/src/io/github/sspanak/tt9/ime/modes/ModePredictive.java index fbc2e854..1bf96e1e 100644 --- a/src/io/github/sspanak/tt9/ime/modes/ModePredictive.java +++ b/src/io/github/sspanak/tt9/ime/modes/ModePredictive.java @@ -214,6 +214,7 @@ public class ModePredictive extends InputMode { private final Handler handleSuggestions = new Handler(Looper.getMainLooper()) { @Override public void handleMessage(Message m) { + digitSequence = predictions.getDigitSequence(); suggestions.clear(); suggestions.addAll(predictions.getList()); @@ -289,13 +290,13 @@ public class ModePredictive extends InputMode { @Override - public boolean shouldAddAutoSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually, int incomingKey, boolean hold, boolean repeat) { + public boolean shouldAddAutoSpace(InputType inputType, TextField textField, boolean isWordAcceptedManually) { return autoSpace .setLastWord(lastAcceptedWord) .setLastSequence(lastAcceptedSequence) .setInputType(inputType) .setTextField(textField) - .shouldAddAutoSpace(isWordAcceptedManually, incomingKey, hold, repeat); + .shouldAddAutoSpace(isWordAcceptedManually); } diff --git a/src/io/github/sspanak/tt9/ime/modes/helpers/AutoSpace.java b/src/io/github/sspanak/tt9/ime/modes/helpers/AutoSpace.java index 9311a71f..4a6147f3 100644 --- a/src/io/github/sspanak/tt9/ime/modes/helpers/AutoSpace.java +++ b/src/io/github/sspanak/tt9/ime/modes/helpers/AutoSpace.java @@ -2,13 +2,14 @@ package io.github.sspanak.tt9.ime.modes.helpers; import java.util.regex.Pattern; -import io.github.sspanak.tt9.Logger; import io.github.sspanak.tt9.ime.helpers.InputType; import io.github.sspanak.tt9.ime.helpers.TextField; import io.github.sspanak.tt9.preferences.SettingsStore; public class AutoSpace { - private final Pattern nextIsPunctuation = Pattern.compile("\\p{Punct}"); + private final Pattern isNumber = Pattern.compile("\\s*\\d+\\s*"); + private final Pattern isPunctuation = Pattern.compile("\\p{Punct}"); + private final SettingsStore settings; private InputType inputType; @@ -48,20 +49,19 @@ public class AutoSpace { * * See the helper functions for the list of rules. */ - public boolean shouldAddAutoSpace(boolean isWordAcceptedManually, int incomingKey, boolean hold, boolean repeat) { + public boolean shouldAddAutoSpace(boolean isWordAcceptedManually) { String previousChars = textField.getPreviousChars(2); String nextChars = textField.getNextChars(2); - Logger.d("shouldAddAutoSpace", "next chars: '" + nextChars + "'"); return settings.getAutoSpace() - && !hold + && !isNumber.matcher(previousChars).find() && ( - shouldAddAutoSpaceAfterPunctuation(previousChars, incomingKey, repeat) + shouldAddAutoSpaceAfterPunctuation(previousChars) || shouldAddAutoSpaceAfterWord(isWordAcceptedManually) ) && !nextChars.startsWith(" ") - && !nextIsPunctuation.matcher(nextChars).find(); + && !isPunctuation.matcher(nextChars).find(); } @@ -71,10 +71,10 @@ public class AutoSpace { * The rules are similar to the ones in the standard Android keyboard (with some exceptions, * because we are not using a QWERTY keyboard here). */ - private boolean shouldAddAutoSpaceAfterPunctuation(String previousChars, int incomingKey, boolean repeat) { + private boolean shouldAddAutoSpaceAfterPunctuation(String previousChars) { return - (incomingKey != 0 || repeat) - && !inputType.isSpecialized() + !inputType.isSpecialized() + && !previousChars.endsWith(" ") && !previousChars.endsWith("\n") && !previousChars.endsWith("\t") && ( previousChars.endsWith(".") || previousChars.endsWith(",") diff --git a/src/io/github/sspanak/tt9/ime/modes/helpers/Predictions.java b/src/io/github/sspanak/tt9/ime/modes/helpers/Predictions.java index 1f23a96a..714c60f6 100644 --- a/src/io/github/sspanak/tt9/ime/modes/helpers/Predictions.java +++ b/src/io/github/sspanak/tt9/ime/modes/helpers/Predictions.java @@ -58,6 +58,10 @@ public class Predictions { return this; } + public String getDigitSequence() { + return digitSequence; + } + public Predictions setIsStemFuzzy(boolean yes) { this.isStemFuzzy = yes; return this; diff --git a/src/io/github/sspanak/tt9/preferences/PreferencesActivity.java b/src/io/github/sspanak/tt9/preferences/PreferencesActivity.java index 04d9e5c0..5b203d02 100644 --- a/src/io/github/sspanak/tt9/preferences/PreferencesActivity.java +++ b/src/io/github/sspanak/tt9/preferences/PreferencesActivity.java @@ -8,6 +8,7 @@ import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; @@ -40,9 +41,15 @@ public class PreferencesActivity extends AppCompatActivity implements Preference DictionaryDb.normalizeWordFrequencies(settings); InputModeValidator.validateEnabledLanguages(settings, settings.getEnabledLanguageIds()); + validateFunctionKeys(); super.onCreate(savedInstanceState); - validateFunctionKeys(); + + // changing the theme causes onCreate(), which displays the MainSettingsScreen, + // but leaves the old "back" history, which is no longer valid, + // so we must reset it + getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); + buildLayout(); } diff --git a/src/io/github/sspanak/tt9/preferences/screens/MainSettingsScreen.java b/src/io/github/sspanak/tt9/preferences/screens/MainSettingsScreen.java index 95da07ca..57df9e5d 100644 --- a/src/io/github/sspanak/tt9/preferences/screens/MainSettingsScreen.java +++ b/src/io/github/sspanak/tt9/preferences/screens/MainSettingsScreen.java @@ -34,6 +34,7 @@ public class MainSettingsScreen extends BaseScreenFragment { @Override public void onResume() { + init(); // changing the theme recreates the PreferencesActivity, making "this.activity" NULL, so we reinitialize it. super.onResume(); createSettingsSection(); }