92 lines
4.2 KiB
Java
92 lines
4.2 KiB
Java
package io.flutter.plugin.editing;
|
|
|
|
import android.view.textservice.SentenceSuggestionsInfo;
|
|
import android.view.textservice.SpellCheckerSession;
|
|
import android.view.textservice.SuggestionsInfo;
|
|
import android.view.textservice.TextInfo;
|
|
import android.view.textservice.TextServicesManager;
|
|
import io.flutter.embedding.engine.systemchannels.SpellCheckChannel;
|
|
import io.flutter.plugin.common.MethodChannel;
|
|
import io.flutter.plugin.localization.LocalizationPlugin;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Locale;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public class SpellCheckPlugin implements SpellCheckChannel.SpellCheckMethodHandler, SpellCheckerSession.SpellCheckerSessionListener {
|
|
public static final String END_INDEX_KEY = "endIndex";
|
|
private static final int MAX_SPELL_CHECK_SUGGESTIONS = 5;
|
|
public static final String START_INDEX_KEY = "startIndex";
|
|
public static final String SUGGESTIONS_KEY = "suggestions";
|
|
private final SpellCheckChannel mSpellCheckChannel;
|
|
private SpellCheckerSession mSpellCheckerSession;
|
|
private final TextServicesManager mTextServicesManager;
|
|
MethodChannel.Result pendingResult;
|
|
|
|
@Override // android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener
|
|
public void onGetSuggestions(SuggestionsInfo[] suggestionsInfoArr) {
|
|
}
|
|
|
|
public SpellCheckPlugin(TextServicesManager textServicesManager, SpellCheckChannel spellCheckChannel) {
|
|
this.mTextServicesManager = textServicesManager;
|
|
this.mSpellCheckChannel = spellCheckChannel;
|
|
spellCheckChannel.setSpellCheckMethodHandler(this);
|
|
}
|
|
|
|
public void destroy() {
|
|
this.mSpellCheckChannel.setSpellCheckMethodHandler(null);
|
|
SpellCheckerSession spellCheckerSession = this.mSpellCheckerSession;
|
|
if (spellCheckerSession != null) {
|
|
spellCheckerSession.close();
|
|
}
|
|
}
|
|
|
|
@Override // io.flutter.embedding.engine.systemchannels.SpellCheckChannel.SpellCheckMethodHandler
|
|
public void initiateSpellCheck(String str, String str2, MethodChannel.Result result) {
|
|
if (this.pendingResult != null) {
|
|
result.error("error", "Previous spell check request still pending.", null);
|
|
} else {
|
|
this.pendingResult = result;
|
|
performSpellCheck(str, str2);
|
|
}
|
|
}
|
|
|
|
public void performSpellCheck(String str, String str2) {
|
|
Locale localeFromString = LocalizationPlugin.localeFromString(str);
|
|
if (this.mSpellCheckerSession == null) {
|
|
this.mSpellCheckerSession = this.mTextServicesManager.newSpellCheckerSession(null, localeFromString, this, true);
|
|
}
|
|
this.mSpellCheckerSession.getSentenceSuggestions(new TextInfo[]{new TextInfo(str2)}, 5);
|
|
}
|
|
|
|
@Override // android.view.textservice.SpellCheckerSession.SpellCheckerSessionListener
|
|
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] sentenceSuggestionsInfoArr) {
|
|
if (sentenceSuggestionsInfoArr.length == 0) {
|
|
this.pendingResult.success(new ArrayList());
|
|
this.pendingResult = null;
|
|
return;
|
|
}
|
|
ArrayList arrayList = new ArrayList();
|
|
SentenceSuggestionsInfo sentenceSuggestionsInfo = sentenceSuggestionsInfoArr[0];
|
|
for (int i = 0; i < sentenceSuggestionsInfo.getSuggestionsCount(); i++) {
|
|
SuggestionsInfo suggestionsInfoAt = sentenceSuggestionsInfo.getSuggestionsInfoAt(i);
|
|
int suggestionsCount = suggestionsInfoAt.getSuggestionsCount();
|
|
if (suggestionsCount > 0) {
|
|
HashMap hashMap = new HashMap();
|
|
int offsetAt = sentenceSuggestionsInfo.getOffsetAt(i);
|
|
int lengthAt = sentenceSuggestionsInfo.getLengthAt(i);
|
|
hashMap.put(START_INDEX_KEY, Integer.valueOf(offsetAt));
|
|
hashMap.put(END_INDEX_KEY, Integer.valueOf(lengthAt + offsetAt));
|
|
ArrayList arrayList2 = new ArrayList();
|
|
for (int i2 = 0; i2 < suggestionsCount; i2++) {
|
|
arrayList2.add(suggestionsInfoAt.getSuggestionAt(i2));
|
|
}
|
|
hashMap.put(SUGGESTIONS_KEY, arrayList2);
|
|
arrayList.add(hashMap);
|
|
}
|
|
}
|
|
this.pendingResult.success(arrayList);
|
|
this.pendingResult = null;
|
|
}
|
|
}
|