Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Undo custom serialization of spell check results on Android #34647

Merged
merged 4 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import androidx.annotation.Nullable;
import io.flutter.Log;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.JSONMethodCodec;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import org.json.JSONArray;
import org.json.JSONException;
import io.flutter.plugin.common.StandardMethodCodec;
import java.util.ArrayList;

/**
* {@link SpellCheckChannel} is a platform channel that is used by the framework to initiate spell
Expand All @@ -26,9 +25,8 @@
*
* <p>Once the spell check results are received by the {@link
* io.flutter.plugin.editing.SpellCheckPlugin}, it will send back to the framework the {@code
* ArrayList<String>} of encoded spell check results (see {@link
* io.flutter.plugin.editing.SpellCheckPlugin#onGetSentenceSuggestions} for details). For example,
* the argument may look like: {@code {"7.11.world\nword\nold"}}. The {@link
* ArrayList<HashMap<String,Object>>} of spell check results (see {@link
* io.flutter.plugin.editing.SpellCheckPlugin#onGetSentenceSuggestions} for details). The {@link
* io.flutter.plugin.editing.SpellCheckPlugin} only handles one request to fetch spell check results
* at a time; see {@link io.flutter.plugin.editing.SpellCheckPlugin#initiateSpellCheck} for details.
*
Expand Down Expand Up @@ -59,11 +57,11 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
switch (method) {
case "SpellCheck.initiateSpellCheck":
try {
final JSONArray argumentList = (JSONArray) args;
String locale = argumentList.getString(0);
String text = argumentList.getString(1);
final ArrayList<String> argumentList = (ArrayList<String>) args;
String locale = argumentList.get(0);
String text = argumentList.get(1);
spellCheckMethodHandler.initiateSpellCheck(locale, text, result);
} catch (JSONException exception) {
} catch (IllegalStateException exception) {
result.error("error", exception.getMessage(), null);
}
break;
Expand All @@ -75,7 +73,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
};

public SpellCheckChannel(@NonNull DartExecutor dartExecutor) {
channel = new MethodChannel(dartExecutor, "flutter/spellcheck", JSONMethodCodec.INSTANCE);
channel = new MethodChannel(dartExecutor, "flutter/spellcheck", StandardMethodCodec.INSTANCE);
channel.setMethodCallHandler(parsingMethodHandler);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.localization.LocalizationPlugin;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

/**
Expand All @@ -34,6 +35,10 @@ public class SpellCheckPlugin
private final TextServicesManager mTextServicesManager;
private SpellCheckerSession mSpellCheckerSession;

public static final String START_INDEX_KEY = "startIndex";
public static final String END_INDEX_KEY = "endIndex";
public static final String SUGGESTIONS_KEY = "suggestions";

@VisibleForTesting MethodChannel.Result pendingResult;

// The maximum number of suggestions that the Android spell check service is allowed to provide
Expand Down Expand Up @@ -105,19 +110,28 @@ public void performSpellCheck(@NonNull String locale, @NonNull String text) {
* 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.
* <p>Spell check results are encoded as dictionaries with a format that looks like
*
* <pre>{@code
* {
* startIndex: 0,
* endIndex: 5,
* suggestions: [hello, ...]
* }
* }</pre>
*
* where there may be up to 5 suggestions.
*/
@Override
public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
if (results.length == 0) {
pendingResult.success(new ArrayList<String>());
pendingResult.success(new ArrayList<HashMap<String, Object>>());
pendingResult = null;
return;
}

ArrayList<String> spellCheckerSuggestionSpans = new ArrayList<String>();
ArrayList<HashMap<String, Object>> spellCheckerSuggestionSpans =
new ArrayList<HashMap<String, Object>>();
SentenceSuggestionsInfo spellCheckResults = results[0];

for (int i = 0; i < spellCheckResults.getSuggestionsCount(); i++) {
Expand All @@ -128,19 +142,20 @@ public void onGetSentenceSuggestions(SentenceSuggestionsInfo[] results) {
continue;
}

String spellCheckerSuggestionSpan = "";
HashMap<String, Object> spellCheckerSuggestionSpan = new HashMap<String, Object>();
int start = spellCheckResults.getOffsetAt(i);
int end = start + spellCheckResults.getLengthAt(i) - 1;
int end = start + spellCheckResults.getLengthAt(i);

spellCheckerSuggestionSpan += String.valueOf(start) + ".";
spellCheckerSuggestionSpan += String.valueOf(end) + ".";
spellCheckerSuggestionSpan.put(START_INDEX_KEY, start);
spellCheckerSuggestionSpan.put(END_INDEX_KEY, end);

ArrayList<String> suggestions = new ArrayList<String>();
for (int j = 0; j < suggestionsCount; j++) {
spellCheckerSuggestionSpan += suggestionsInfo.getSuggestionAt(j) + "\n";
suggestions.add(suggestionsInfo.getSuggestionAt(j));
}

spellCheckerSuggestionSpans.add(
spellCheckerSuggestionSpan.substring(0, spellCheckerSuggestionSpan.length() - 1));
spellCheckerSuggestionSpan.put(SUGGESTIONS_KEY, suggestions);
spellCheckerSuggestionSpans.add(spellCheckerSuggestionSpan);
}

pendingResult.success(spellCheckerSuggestionSpans);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.systemchannels.SpellCheckChannel;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.JSONMethodCodec;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.StandardMethodCodec;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -38,7 +39,7 @@ public class SpellCheckPluginTest {
private static void sendToBinaryMessageHandler(
BinaryMessenger.BinaryMessageHandler binaryMessageHandler, String method, Object args) {
MethodCall methodCall = new MethodCall(method, args);
ByteBuffer encodedMethodCall = JSONMethodCodec.INSTANCE.encodeMethodCall(methodCall);
ByteBuffer encodedMethodCall = StandardMethodCodec.INSTANCE.encodeMethodCall(methodCall);
binaryMessageHandler.onMessage(
(ByteBuffer) encodedMethodCall.flip(), mock(BinaryMessenger.BinaryReply.class));
}
Expand Down Expand Up @@ -185,7 +186,7 @@ public void onGetSentenceSuggestionsResultsWithSuccessAndNoResultsProperly() {

spellCheckPlugin.onGetSentenceSuggestions(new SentenceSuggestionsInfo[] {});

verify(mockResult).success(new ArrayList<String>());
verify(mockResult).success(new ArrayList<HashMap<String, Object>>());
}

@Test
Expand All @@ -209,6 +210,16 @@ public void onGetSentenceSuggestionsResultsWithSuccessAndResultsProperly() {
new int[] {5})
});

verify(mockResult).success(new ArrayList<String>(Arrays.asList("7.11.world\nword\nold")));
ArrayList<HashMap<String, Object>> expectedResults = new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> expectedResult = new HashMap<String, Object>();

expectedResult.put(SpellCheckPlugin.START_INDEX_KEY, 7);
expectedResult.put(SpellCheckPlugin.END_INDEX_KEY, 12);
expectedResult.put(
SpellCheckPlugin.SUGGESTIONS_KEY,
new ArrayList<String>(Arrays.asList("world", "word", "old")));
expectedResults.add(expectedResult);

verify(mockResult).success(expectedResults);
}
}