5package io.flutter.plugin.editing;
7import android.view.textservice.SentenceSuggestionsInfo;
8import android.view.textservice.SpellCheckerSession;
9import android.view.textservice.SuggestionsInfo;
10import android.view.textservice.TextInfo;
11import android.view.textservice.TextServicesManager;
12import androidx.annotation.NonNull;
13import androidx.annotation.VisibleForTesting;
14import io.flutter.embedding.engine.systemchannels.SpellCheckChannel;
15import io.flutter.plugin.common.MethodChannel;
16import io.flutter.plugin.localization.LocalizationPlugin;
17import java.util.ArrayList;
18import java.util.HashMap;
19import java.util.Locale;
32 SpellCheckerSession.SpellCheckerSessionListener {
35 private final TextServicesManager mTextServicesManager;
36 private SpellCheckerSession mSpellCheckerSession;
46 private static final int MAX_SPELL_CHECK_SUGGESTIONS = 5;
49 @NonNull TextServicesManager textServicesManager,
51 mTextServicesManager = textServicesManager;
52 mSpellCheckChannel = spellCheckChannel;
68 if (mSpellCheckerSession !=
null) {
69 mSpellCheckerSession.close();
81 result.error(
"error",
"Previous spell check request still pending.",
null);
94 if (mSpellCheckerSession ==
null) {
95 mSpellCheckerSession =
96 mTextServicesManager.newSpellCheckerSession(
104 TextInfo[] textInfos =
new TextInfo[] {
new TextInfo(
text)};
105 mSpellCheckerSession.getSentenceSuggestions(textInfos, MAX_SPELL_CHECK_SUGGESTIONS);
126 if (results.length == 0) {
127 pendingResult.success(
new ArrayList<HashMap<String, Object>>());
132 ArrayList<HashMap<String, Object>> spellCheckerSuggestionSpans =
133 new ArrayList<HashMap<String, Object>>();
134 SentenceSuggestionsInfo spellCheckResults = results[0];
135 if (spellCheckResults ==
null) {
136 pendingResult.success(
new ArrayList<HashMap<String, Object>>());
141 for (
int i = 0;
i < spellCheckResults.getSuggestionsCount();
i++) {
142 SuggestionsInfo suggestionsInfo = spellCheckResults.getSuggestionsInfoAt(
i);
143 int suggestionsCount = suggestionsInfo.getSuggestionsCount();
145 if (suggestionsCount <= 0) {
149 HashMap<String, Object> spellCheckerSuggestionSpan =
new HashMap<String, Object>();
150 int start = spellCheckResults.getOffsetAt(
i);
151 int end =
start + spellCheckResults.getLengthAt(
i);
156 ArrayList<String> suggestions =
new ArrayList<String>();
157 boolean validSuggestionsFound =
false;
158 for (
int j = 0; j < suggestionsCount; j++) {
159 String suggestion = suggestionsInfo.getSuggestionAt(j);
162 if (!suggestion.equals(
"")) {
163 validSuggestionsFound =
true;
164 suggestions.add(suggestion);
168 if (!validSuggestionsFound) {
172 spellCheckerSuggestionSpans.add(spellCheckerSuggestionSpan);
void setSpellCheckMethodHandler( @Nullable SpellCheckMethodHandler spellCheckMethodHandler)
static final String SUGGESTIONS_KEY
void onGetSuggestions(SuggestionsInfo[] results)
void performSpellCheck(@NonNull String locale, @NonNull String text)
SpellCheckPlugin( @NonNull TextServicesManager textServicesManager, @NonNull SpellCheckChannel spellCheckChannel)
static final String START_INDEX_KEY
static final String END_INDEX_KEY
void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results)
void initiateSpellCheck( @NonNull String locale, @NonNull String text, @NonNull MethodChannel.Result result)
MethodChannel.Result pendingResult
static Locale localeFromString(@NonNull String localeString)