Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dd3a05a7 authored by Chenxi Ni's avatar Chenxi Ni Committed by Automerger Merge Worker
Browse files

Merge "Fix InputConnection#performSpellCheck logic:" into sc-dev am: f5c2cf83

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/13425349

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: Iae83b3764c3223711cc485d3d9d51bba60814870
parents 875c7258 f5c2cf83
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -858,7 +858,7 @@ public interface InputConnection {
    boolean reportFullscreenMode(boolean enabled);

    /**
     * Have the editor perform spell checking around the current selection.
     * Have the editor perform spell checking for the full content.
     *
     * <p>The editor can ignore this method call if it does not support spell checking.
     *
+35 −23
Original line number Diff line number Diff line
@@ -220,29 +220,26 @@ public class SpellChecker implements SpellCheckerSessionListener {
    }

    void onPerformSpellCheck() {
        final int selectionStart = mTextView.getSelectionStart();
        final int selectionEnd = mTextView.getSelectionEnd();
        final int selectionRangeStart;
        final int selectionRangeEnd;
        if (selectionStart < selectionEnd) {
            selectionRangeStart = selectionStart;
            selectionRangeEnd = selectionEnd;
        } else {
            selectionRangeStart = selectionEnd;
            selectionRangeEnd = selectionStart;
        }
        // Expand the range so that it (hopefully) includes the current sentence.
        final int start = Math.max(0, selectionRangeStart - MIN_SENTENCE_LENGTH);
        final int end = Math.min(mTextView.length(), selectionRangeEnd + MIN_SENTENCE_LENGTH);
        // Triggers full content spell check.
        final int start = 0;
        final int end = mTextView.length();
        if (DBG) {
            Log.d(TAG, "performSpellCheckAroundSelection: " + start + ", " + end);
        }
        spellCheck(start, end);
        spellCheck(start, end, /* forceCheckWhenEditingWord= */ true);
    }

    public void spellCheck(int start, int end) {
        spellCheck(start, end, /* forceCheckWhenEditingWord= */ false);
    }

    /**
     * Requests to do spell check for text in the range (start, end).
     */
    public void spellCheck(int start, int end, boolean forceCheckWhenEditingWord) {
        if (DBG) {
            Log.d(TAG, "Start spell-checking: " + start + ", " + end);
            Log.d(TAG, "Start spell-checking: " + start + ", " + end + ", "
                    + forceCheckWhenEditingWord);
        }
        final Locale locale = mTextView.getSpellCheckerLocale();
        final boolean isSessionActive = isSessionActive();
@@ -267,7 +264,7 @@ public class SpellChecker implements SpellCheckerSessionListener {
        for (int i = 0; i < length; i++) {
            final SpellParser spellParser = mSpellParsers[i];
            if (spellParser.isFinished()) {
                spellParser.parse(start, end);
                spellParser.parse(start, end, forceCheckWhenEditingWord);
                return;
            }
        }
@@ -282,10 +279,14 @@ public class SpellChecker implements SpellCheckerSessionListener {

        SpellParser spellParser = new SpellParser();
        mSpellParsers[length] = spellParser;
        spellParser.parse(start, end);
        spellParser.parse(start, end, forceCheckWhenEditingWord);
    }

    private void spellCheck() {
        spellCheck(/* forceCheckWhenEditingWord= */ false);
    }

    private void spellCheck(boolean forceCheckWhenEditingWord) {
        if (mSpellCheckerSession == null) return;

        Editable editable = (Editable) mTextView.getText();
@@ -295,6 +296,12 @@ public class SpellChecker implements SpellCheckerSessionListener {
        TextInfo[] textInfos = new TextInfo[mLength];
        int textInfosCount = 0;

        if (DBG) {
            Log.d(TAG, "forceCheckWhenEditingWord=" + forceCheckWhenEditingWord
                    + ", mLength=" + mLength + ", cookie = " + mCookie
                    + ", sel start = " + selectionStart + ", sel end = " + selectionEnd);
        }

        for (int i = 0; i < mLength; i++) {
            final SpellCheckSpan spellCheckSpan = mSpellCheckSpans[i];
            if (mIds[i] < 0 || spellCheckSpan.isSpellCheckInProgress()) continue;
@@ -319,7 +326,7 @@ public class SpellChecker implements SpellCheckerSessionListener {
            } else {
                isEditing = selectionEnd < start || selectionStart > end;
            }
            if (start >= 0 && end > start && isEditing) {
            if (start >= 0 && end > start && (forceCheckWhenEditingWord || isEditing)) {
                spellCheckSpan.setSpellCheckInProgress(true);
                final TextInfo textInfo = new TextInfo(editable, start, end, mCookie, mIds[i]);
                textInfos[textInfosCount++] = textInfo;
@@ -546,7 +553,11 @@ public class SpellChecker implements SpellCheckerSessionListener {
    private class SpellParser {
        private Object mRange = new Object();

        public void parse(int start, int end) {
        // Forces to do spell checker even user is editing the word.
        private boolean mForceCheckWhenEditingWord;

        public void parse(int start, int end, boolean forceCheckWhenEditingWord) {
            mForceCheckWhenEditingWord = forceCheckWhenEditingWord;
            final int max = mTextView.length();
            final int parseEnd;
            if (end > max) {
@@ -567,6 +578,7 @@ public class SpellChecker implements SpellCheckerSessionListener {

        public void stop() {
            removeRangeSpan((Editable) mTextView.getText());
            mForceCheckWhenEditingWord = false;
        }

        private void setRangeSpan(Editable editable, int start, int end) {
@@ -617,7 +629,7 @@ public class SpellChecker implements SpellCheckerSessionListener {
                if (DBG) {
                    Log.i(TAG, "No more spell check.");
                }
                removeRangeSpan(editable);
                stop();
                return;
            }

@@ -649,7 +661,7 @@ public class SpellChecker implements SpellCheckerSessionListener {
                    if (DBG) {
                        Log.i(TAG, "Incorrect range span.");
                    }
                    removeRangeSpan(editable);
                    stop();
                    return;
                }
                do {
@@ -778,7 +790,7 @@ public class SpellChecker implements SpellCheckerSessionListener {
                removeRangeSpan(editable);
            }

            spellCheck();
            spellCheck(mForceCheckWhenEditingWord);
        }

        private <T> void removeSpansAt(Editable editable, int offset, T[] spans) {