-
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 |
|---|---|---|
|
|
@@ -11,8 +11,10 @@ | |
| import android.view.textservice.TextServicesManager; | ||
| import androidx.annotation.NonNull; | ||
| 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.Arrays; | ||
| import java.util.Locale; | ||
|
|
||
| /** | ||
|
|
@@ -32,9 +34,11 @@ public class SpellCheckPlugin | |
| private final TextServicesManager mTextServicesManager; | ||
| private SpellCheckerSession mSpellCheckerSession; | ||
|
|
||
| private MethodChannel.Result pendingResult; | ||
| private String pendingResultText; | ||
|
|
||
| // The maximum number of suggestions that the Android spell check service is allowed to provide | ||
| // per word. | ||
| // Same number that is used by default for Android's TextViews. | ||
| // per word. Same number that is used by default for Android's TextViews. | ||
| private static final int MAX_SPELL_CHECK_SUGGESTIONS = 5; | ||
|
|
||
| public SpellCheckPlugin( | ||
|
|
@@ -62,8 +66,21 @@ public void destroy() { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Initiates call to native spell checker to spell check specified text if there is no result | ||
| * awaiting a response. | ||
| */ | ||
| @Override | ||
| public void initiateSpellCheck(@NonNull String locale, @NonNull String text) { | ||
| public void initiateSpellCheck( | ||
| @NonNull String locale, @NonNull String text, @NonNull MethodChannel.Result result) { | ||
| if (pendingResult != null) { | ||
| result.error("error", "Previous spell check request still pending.", null); | ||
| return; | ||
| } | ||
|
|
||
| pendingResult = result; | ||
| pendingResultText = text; | ||
|
|
||
| performSpellCheck(locale, text); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle case when there's a pending
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 We already knew that, but our assumption was that if the 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
||
| } | ||
|
|
||
|
|
@@ -96,13 +113,13 @@ public void performSpellCheck(@NonNull String locale, @NonNull String text) { | |
| */ | ||
| @Override | ||
| public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { | ||
| ArrayList<String> spellCheckerSuggestionSpans = new ArrayList<String>(); | ||
|
|
||
| if (results.length == 0) { | ||
| mSpellCheckChannel.updateSpellCheckResults(spellCheckerSuggestionSpans); | ||
| pendingResult.success(new ArrayList<>(Arrays.asList(pendingResultText, ""))); | ||
| pendingResult = null; | ||
| return; | ||
| } | ||
|
|
||
| ArrayList<String> spellCheckerSuggestionSpans = new ArrayList<String>(); | ||
| SentenceSuggestionsInfo spellCheckResults = results[0]; | ||
|
|
||
camsim99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (int i = 0; i < spellCheckResults.getSuggestionsCount(); i++) { | ||
|
|
@@ -128,7 +145,9 @@ public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) { | |
| spellCheckerSuggestionSpan.substring(0, spellCheckerSuggestionSpan.length() - 1)); | ||
| } | ||
|
|
||
| mSpellCheckChannel.updateSpellCheckResults(spellCheckerSuggestionSpans); | ||
| spellCheckerSuggestionSpans.add(0, pendingResultText); | ||
| pendingResult.success(spellCheckerSuggestionSpans); | ||
| pendingResult = null; | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.