From 0b15fe0a1ecbf12fd2cdda9a4c90f6e50fedea15 Mon Sep 17 00:00:00 2001 From: Helium Date: Tue, 22 Mar 2022 22:28:35 +0100 Subject: [PATCH] reduce amount of unwanted automatic space insertions --- .../inputmethod/latin/RichInputConnection.java | 8 ++++++++ .../latin/inputlogic/InputLogic.java | 18 +++++++++++++++++- .../latin/utils/InputTypeUtils.java | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/RichInputConnection.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/RichInputConnection.java index 46dea18bd..96127dbd6 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/RichInputConnection.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/RichInputConnection.java @@ -896,6 +896,14 @@ public boolean textBeforeCursorLooksLikeURL() { return StringUtils.lastPartLooksLikeURL(mCommittedTextBeforeComposingText); } + public boolean spaceBeforeCursor() { + return mCommittedTextBeforeComposingText.indexOf(" ") != -1; + } + + public boolean wordBeforeCursorMayBeEmail() { + return mCommittedTextBeforeComposingText.lastIndexOf(" ") < mCommittedTextBeforeComposingText.lastIndexOf("@"); + } + /** * Looks at the text just before the cursor to find out if we are inside a double quote. * diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/inputlogic/InputLogic.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/inputlogic/InputLogic.java index 920364b9c..3f8d94cb1 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/inputlogic/InputLogic.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/inputlogic/InputLogic.java @@ -18,6 +18,7 @@ import android.graphics.Color; import android.os.SystemClock; +import android.text.InputType; import android.text.SpannableString; import android.text.Spanned; import android.text.TextUtils; @@ -2045,11 +2046,26 @@ private void sendKeyCodePoint(final SettingsValues settingsValues, final int cod private void insertAutomaticSpaceIfOptionsAndTextAllow(final SettingsValues settingsValues) { if (settingsValues.shouldInsertSpacesAutomatically() && settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces - && !mConnection.textBeforeCursorLooksLikeURL()) { + && !textBeforeCursorMayBeURL() + && !(mConnection.getCodePointBeforeCursor() == Constants.CODE_PERIOD && mConnection.wordBeforeCursorMayBeEmail())) { sendKeyCodePoint(settingsValues, Constants.CODE_SPACE); } } + private boolean textBeforeCursorMayBeURL() { + if (mConnection.textBeforeCursorLooksLikeURL()) return true; + // doesn't look like URL, but we may be in URL field and user may want to enter example.com + if (mConnection.getCodePointBeforeCursor() != Constants.CODE_PERIOD && mConnection.getCodePointBeforeCursor() != ':') + return false; + final EditorInfo ei = getCurrentInputEditorInfo(); + if (ei == null) return false; + int inputType = ei.inputType; + if ((inputType & InputType.TYPE_TEXT_VARIATION_URI) != 0) + return !mConnection.spaceBeforeCursor(); + else + return false; + } + /** * Do the final processing after a batch input has ended. This commits the word to the editor. * @param settingsValues the current values of the settings. diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/utils/InputTypeUtils.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/utils/InputTypeUtils.java index 16babf595..edda78fa8 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/utils/InputTypeUtils.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/utils/InputTypeUtils.java @@ -31,6 +31,7 @@ public final class InputTypeUtils implements InputType { private static final int TEXT_VISIBLE_PASSWORD_INPUT_TYPE = TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD; private static final int[] SUPPRESSING_AUTO_SPACES_FIELD_VARIATION = { + InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS, InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS, InputType.TYPE_TEXT_VARIATION_PASSWORD, InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,