-
Notifications
You must be signed in to change notification settings - Fork 6k
Add Spell Check Support for Android Engine #30858
Changes from 1 commit
dc93719
cd234ad
f463846
3dfae0a
546692b
5f43ff5
ee73219
624f68b
846b195
b27704c
64f0ac7
0cc3776
33f2f40
eb3c731
1877242
5372aaa
d9adbdc
34809db
10d154d
00c228e
e08171f
5ccc144
87ab60f
f4083d7
ce45427
6972b0b
3eba4de
963d9ae
6ea2ede
32c7702
e9ebd5b
8298436
993b6c8
4179f42
f999488
ade72bc
c20b62c
49583d4
49939aa
0cf00f3
48a4354
510f83a
d8e333b
61908d3
29b095f
ee28058
4f0a074
6c4d437
d73b915
66c24ed
04a2ce1
15d17f5
e6414b2
2882e3b
dfe4689
cdc6b9c
65b8850
036b15b
188c2ad
8198d4e
a2b6c90
2aac372
894f4e9
2a4ea66
c2a6550
6125e74
74d1477
cc8bf5e
5a175ec
1c02784
3e74d61
975673c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import android.view.textservice.TextInfo; | ||
| import android.view.textservice.TextServicesManager; | ||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.VisibleForTesting; | ||
| import io.flutter.embedding.engine.systemchannels.SpellCheckChannel; | ||
| import io.flutter.plugin.localization.LocalizationPlugin; | ||
| import java.util.ArrayList; | ||
|
|
@@ -21,12 +22,10 @@ | |
| * | ||
| * <p>The plugin handles requests for spell check sent by the {@link | ||
| * io.flutter.embedding.engine.systemchannels.SpellCheckChannel} via sending requests to the Android | ||
| * spell checker. It also receive the spell check results from the service and send them back to the | ||
| * framework through the {@link io.flutter.embedding.engine.systemchannels.SpellCheckChannel}. | ||
| * spell checker. It also receives the spell check results from the service and sends them back to | ||
| * the framework through the {@link io.flutter.embedding.engine.systemchannels.SpellCheckChannel}. | ||
| */ | ||
| public class SpellCheckPlugin | ||
| implements SpellCheckChannel.SpellCheckMethodHandler, | ||
| SpellCheckerSession.SpellCheckerSessionListener { | ||
| public class SpellCheckPlugin implements SpellCheckChannel.SpellCheckMethodHandler { | ||
|
|
||
| private final SpellCheckChannel mSpellCheckChannel; | ||
| private final TextServicesManager mTextServicesManager; | ||
|
|
@@ -62,6 +61,11 @@ public void destroy() { | |
| } | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| public SpellCheckPluginSessionListener createSpellCheckerSessionListener(String text) { | ||
| return new SpellCheckPluginSessionListener(text); | ||
| } | ||
|
|
||
| @Override | ||
| public void initiateSpellCheck(@NonNull String locale, @NonNull String text) { | ||
| performSpellCheck(locale, text); | ||
|
|
@@ -75,65 +79,85 @@ public void performSpellCheck(@NonNull String locale, @NonNull String text) { | |
| if (mSpellCheckerSession != null) { | ||
| mSpellCheckerSession.close(); | ||
|
||
| } | ||
|
|
||
| mSpellCheckerSession = | ||
|
||
| mTextServicesManager.newSpellCheckerSession( | ||
| null, | ||
| localeFromString, | ||
| this, | ||
| createSpellCheckerSessionListener(text), | ||
| /** referToSpellCheckerLanguageSettings= */ | ||
| true); | ||
|
|
||
| TextInfo[] textInfos = new TextInfo[] {new TextInfo(text)}; | ||
| mSpellCheckerSession.getSentenceSuggestions(textInfos, MAX_SPELL_CHECK_SUGGESTIONS); | ||
camsim99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /** | ||
| * Callback for Android spell check API that decomposes results and send results through the | ||
| * {@link SpellCheckChannel}. | ||
| * | ||
| * <p>Spell check results will be encoded as a string representing the span of that result, with | ||
| * the format [start_index.end_index.suggestion_1,suggestion_2,suggestion_3], where there may be | ||
| * up to 5 suggestions. | ||
| */ | ||
| @Override | ||
| public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { | ||
| ArrayList<String> spellCheckerSuggestionSpans = new ArrayList<String>(); | ||
| class SpellCheckPluginSessionListener implements SpellCheckerSession.SpellCheckerSessionListener { | ||
| private final String text; | ||
|
|
||
| if (results.length == 0) { | ||
| mSpellCheckChannel.updateSpellCheckResults(spellCheckerSuggestionSpans); | ||
| return; | ||
| public SpellCheckPluginSessionListener(String text) { | ||
| this.text = text; | ||
| } | ||
|
|
||
| SentenceSuggestionsInfo spellCheckResults = results[0]; | ||
| @VisibleForTesting | ||
| public SpellCheckChannel getSpellCheckChannel() { | ||
| return mSpellCheckChannel; | ||
| } | ||
|
|
||
| for (int i = 0; i < spellCheckResults.getSuggestionsCount(); i++) { | ||
| SuggestionsInfo suggestionsInfo = spellCheckResults.getSuggestionsInfoAt(i); | ||
| int suggestionsCount = suggestionsInfo.getSuggestionsCount(); | ||
| @VisibleForTesting | ||
| public String getText() { | ||
| return text; | ||
| } | ||
|
|
||
| if (suggestionsCount == 0) { | ||
| continue; | ||
| /** | ||
| * Callback for Android spell check API that decomposes results and send results through the | ||
| * {@link SpellCheckChannel}. | ||
| * | ||
| * <p>Spell check results will be encoded as a string representing the span of that result, with | ||
| * the format "start_index.end_index.suggestion_1/nsuggestion_2/nsuggestion_3", where there may | ||
| * be up to 5 suggestions. | ||
| */ | ||
| @Override | ||
| public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { | ||
| ArrayList<String> spellCheckerSuggestionSpans = new ArrayList<String>(); | ||
| SpellCheckChannel spellCheckChannel = getSpellCheckChannel(); | ||
|
|
||
| if (results.length == 0) { | ||
| spellCheckChannel.updateSpellCheckResults(spellCheckerSuggestionSpans, getText()); | ||
| return; | ||
| } | ||
|
|
||
| String spellCheckerSuggestionSpan = ""; | ||
| int start = spellCheckResults.getOffsetAt(i); | ||
| int end = start + spellCheckResults.getLengthAt(i) - 1; | ||
| SentenceSuggestionsInfo spellCheckResults = results[0]; | ||
|
|
||
| spellCheckerSuggestionSpan += String.valueOf(start) + "."; | ||
| spellCheckerSuggestionSpan += String.valueOf(end) + "."; | ||
| for (int i = 0; i < spellCheckResults.getSuggestionsCount(); i++) { | ||
| SuggestionsInfo suggestionsInfo = spellCheckResults.getSuggestionsInfoAt(i); | ||
| int suggestionsCount = suggestionsInfo.getSuggestionsCount(); | ||
|
|
||
| for (int j = 0; j < suggestionsCount; j++) { | ||
| spellCheckerSuggestionSpan += suggestionsInfo.getSuggestionAt(j) + "\n"; | ||
| if (suggestionsCount == 0) { | ||
| continue; | ||
| } | ||
|
|
||
| String spellCheckerSuggestionSpan = ""; | ||
| int start = spellCheckResults.getOffsetAt(i); | ||
| int end = start + spellCheckResults.getLengthAt(i) - 1; | ||
|
|
||
| spellCheckerSuggestionSpan += String.valueOf(start) + "."; | ||
| spellCheckerSuggestionSpan += String.valueOf(end) + "."; | ||
|
|
||
| for (int j = 0; j < suggestionsCount; j++) { | ||
| spellCheckerSuggestionSpan += suggestionsInfo.getSuggestionAt(j) + "\n"; | ||
| } | ||
|
|
||
| spellCheckerSuggestionSpans.add( | ||
| spellCheckerSuggestionSpan.substring(0, spellCheckerSuggestionSpan.length() - 1)); | ||
| } | ||
|
|
||
| spellCheckerSuggestionSpans.add( | ||
| spellCheckerSuggestionSpan.substring(0, spellCheckerSuggestionSpan.length() - 1)); | ||
| spellCheckChannel.updateSpellCheckResults(spellCheckerSuggestionSpans, getText()); | ||
| } | ||
|
|
||
| mSpellCheckChannel.updateSpellCheckResults(spellCheckerSuggestionSpans); | ||
| } | ||
|
|
||
| @Override | ||
| public void onGetSuggestions(SuggestionsInfo[] results) { | ||
| // Deprecated callback for Android spell check API; will not use. | ||
| @Override | ||
| public void onGetSuggestions(SuggestionsInfo[] results) { | ||
| // Deprecated callback for Android spell check API; will not use. | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle case when there's a pending
initiateSpellCheck. Maybe, callresult.errororresult.successwith an empty list. The general idea is that while the framework is waiting on a spellcheck request, a new request is issued, so the older one needs to be dropped.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The approach of dropping older requests has a similar fault to the approach I pursued previously of saving the result and text awaiting a response to that result in the
SpellCheckChannelclass, which is that we cannot cancel requests to the Android spell checker once they have been submitted.We already knew that, but our assumption was that if the
SpellCheckPluginhas detected a result awaiting a response when another request to initiate spell check has come in, then that request will not reach theonGetSentenceSuggestions(...)callback, meaning that the Android spell checker consumed the request. The consequence of assuming such is that sometimes the plugin will attempt to respond to a new request with old spell check results. Depending on how much the text has been modified between the time those old results come in and the time that the new request to spell check was submitted, those old results could lead to some hard-to-handle errors on the framework side.Given all of that, I decided to have the plugin only handle one request at a time by dropping new requests when an old one is still pending. This approach may lead to results lagging slightly behind, but (i) the spell check results in any given response will correspond to the text in that response, (ii) the spell check results will be returned in chronological order, and (iii) any impacts of these results lagging behind can be handled by the logic on the framework side. This is all because with this approach, we do not have to battle the unpredictable behavior of the Android spell checker. I believe also that in further iterations, other speedup techniques can be more easily explored given these guarantees.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this approach seem to work with decent performance in practice? Maybe this is the most solid approach in the real world even though it's not the fastest theoretically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, is there ever a risk of the spell checker never responding and locking up or is that another thing that we shouldn't worry about in practice?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, exactly. From playing around with it (paired with making the corrections on the framework side if the process of returning results lag behind), I don't see any difference. If you are open to playing around with it, too, that'd be great!
I think we can rely on Android for this. Android seems to have some handling for issues like that (see here).