Undo custom serialization of spell check results on Android (flutter/engine#34647)
This commit is contained in:
@@ -8,11 +8,10 @@ import androidx.annotation.NonNull;
|
||||
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
|
||||
@@ -26,9 +25,8 @@ import org.json.JSONException;
|
||||
*
|
||||
* <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.
|
||||
*
|
||||
@@ -59,11 +57,11 @@ public class SpellCheckChannel {
|
||||
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;
|
||||
@@ -75,7 +73,7 @@ public class SpellCheckChannel {
|
||||
};
|
||||
|
||||
public SpellCheckChannel(@NonNull DartExecutor dartExecutor) {
|
||||
channel = new MethodChannel(dartExecutor, "flutter/spellcheck", JSONMethodCodec.INSTANCE);
|
||||
channel = new MethodChannel(dartExecutor, "flutter/spellcheck", StandardMethodCodec.INSTANCE);
|
||||
channel.setMethodCallHandler(parsingMethodHandler);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ 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;
|
||||
|
||||
/**
|
||||
@@ -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
|
||||
@@ -105,19 +110,28 @@ public class SpellCheckPlugin
|
||||
* 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++) {
|
||||
@@ -128,19 +142,20 @@ public class SpellCheckPlugin
|
||||
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);
|
||||
|
||||
@@ -21,12 +21,13 @@ import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
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;
|
||||
@@ -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));
|
||||
}
|
||||
@@ -185,7 +186,7 @@ public class SpellCheckPluginTest {
|
||||
|
||||
spellCheckPlugin.onGetSentenceSuggestions(new SentenceSuggestionsInfo[] {});
|
||||
|
||||
verify(mockResult).success(new ArrayList<String>());
|
||||
verify(mockResult).success(new ArrayList<HashMap<String, Object>>());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -209,6 +210,16 @@ public class SpellCheckPluginTest {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user