From e5f12abfecd858725547820dec87c93cd94e27b5 Mon Sep 17 00:00:00 2001 From: Kohsuke Yatoh Date: Wed, 2 Dec 2020 05:47:08 +0000 Subject: [PATCH] Add RESULT_ATTR_DONT_SHOW_UI_FOR_SUGGESTIONS. Bug: 166304720 Test: atest CtsInputMethodTestCases:SpellCheckerTest Change-Id: I2f5796d899c79ea1cd6795c16f2011389ae7afea --- core/api/current.txt | 1 + core/java/android/text/style/SuggestionSpan.java | 8 ++++++++ .../java/android/view/textservice/SuggestionsInfo.java | 10 ++++++++++ core/java/android/widget/SpellChecker.java | 7 ++++++- 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/core/api/current.txt b/core/api/current.txt index 1c02f69ad8ece..07d935bb6c49b 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -52148,6 +52148,7 @@ package android.view.textservice { method public void setCookieAndSequence(int, int); method public void writeToParcel(android.os.Parcel, int); field @NonNull public static final android.os.Parcelable.Creator CREATOR; + field public static final int RESULT_ATTR_DONT_SHOW_UI_FOR_SUGGESTIONS = 16; // 0x10 field public static final int RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS = 4; // 0x4 field public static final int RESULT_ATTR_IN_THE_DICTIONARY = 1; // 0x1 field public static final int RESULT_ATTR_LOOKS_LIKE_GRAMMAR_ERROR = 8; // 0x8 diff --git a/core/java/android/text/style/SuggestionSpan.java b/core/java/android/text/style/SuggestionSpan.java index 1f15d462b9b1f..4a48832c10881 100644 --- a/core/java/android/text/style/SuggestionSpan.java +++ b/core/java/android/text/style/SuggestionSpan.java @@ -402,6 +402,10 @@ public class SuggestionSpan extends CharacterStyle implements ParcelableSpan { } } else if (autoCorrection) { tp.setUnderlineText(mAutoCorrectionUnderlineColor, mAutoCorrectionUnderlineThickness); + } else if (misspelled) { + tp.setUnderlineText(mMisspelledUnderlineColor, mMisspelledUnderlineThickness); + } else if (grammarError) { + tp.setUnderlineText(mGrammarErrorUnderlineColor, mGrammarErrorUnderlineThickness); } } @@ -425,6 +429,10 @@ public class SuggestionSpan extends CharacterStyle implements ParcelableSpan { } } else if (autoCorrection) { return mAutoCorrectionUnderlineColor; + } else if (misspelled) { + return mMisspelledUnderlineColor; + } else if (grammarError) { + return mGrammarErrorUnderlineColor; } return 0; } diff --git a/core/java/android/view/textservice/SuggestionsInfo.java b/core/java/android/view/textservice/SuggestionsInfo.java index ca529f626cfe0..1301c49c2ae02 100644 --- a/core/java/android/view/textservice/SuggestionsInfo.java +++ b/core/java/android/view/textservice/SuggestionsInfo.java @@ -53,6 +53,16 @@ public final class SuggestionsInfo implements Parcelable { */ public static final int RESULT_ATTR_LOOKS_LIKE_GRAMMAR_ERROR = 0x0008; + /** + * Flag of the attributes of the suggestions that can be obtained by + * {@link #getSuggestionsAttributes}: this tells that the text service has an alternative way to + * show UI for the list of correction suggestions to the user. When this flag is set, the + * receiver of the result suggestions should mark the erroneous part of the text with a text + * signifier (for example, underline), but should not show any UI for the list of correction + * suggestions to the user (for example, in a popup window). + */ + public static final int RESULT_ATTR_DONT_SHOW_UI_FOR_SUGGESTIONS = 0x0010; + private final int mSuggestionsAttributes; private final String[] mSuggestions; private final boolean mSuggestionsAvailable; diff --git a/core/java/android/widget/SpellChecker.java b/core/java/android/widget/SpellChecker.java index 0788635d1c68d..b06fa1a058d44 100644 --- a/core/java/android/widget/SpellChecker.java +++ b/core/java/android/widget/SpellChecker.java @@ -477,6 +477,8 @@ public class SpellChecker implements SpellCheckerSessionListener { mTextView.postDelayed(mSpellRunnable, SPELL_PAUSE_DURATION); } + // When calling this method, RESULT_ATTR_LOOKS_LIKE_TYPO or RESULT_ATTR_LOOKS_LIKE_GRAMMAR_ERROR + // (or both) should be set in suggestionsInfo. private void createMisspelledSuggestionSpan(Editable editable, SuggestionsInfo suggestionsInfo, SpellCheckSpan spellCheckSpan, int offset, int length) { final int spellCheckSpanStart = editable.getSpanStart(spellCheckSpan); @@ -506,7 +508,10 @@ public class SpellChecker implements SpellCheckerSessionListener { } final int suggestionsAttrs = suggestionsInfo.getSuggestionsAttributes(); - int flags = SuggestionSpan.FLAG_EASY_CORRECT; + int flags = 0; + if ((suggestionsAttrs & SuggestionsInfo.RESULT_ATTR_DONT_SHOW_UI_FOR_SUGGESTIONS) == 0) { + flags |= SuggestionSpan.FLAG_EASY_CORRECT; + } if ((suggestionsAttrs & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) != 0) { flags |= SuggestionSpan.FLAG_MISSPELLED; }