Add TextInput sendAppPrivateCommand to Flutter Engine (flutter/engine#20144)

Adds sendAppPrivateCommand for Crowdsource
This commit is contained in:
guolinaileen
2020-08-07 17:15:05 -07:00
committed by GitHub
parent 51a7c1d712
commit 3c958edb47
3 changed files with 118 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package io.flutter.embedding.engine.systemchannels;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import androidx.annotation.NonNull;
@@ -112,6 +113,22 @@ public class TextInputChannel {
textInputMethodHandler.clearClient();
result.success(null);
break;
case "TextInput.sendAppPrivateCommand":
try {
final JSONObject arguments = (JSONObject) args;
final String action = arguments.getString("action");
final String data = arguments.getString("data");
Bundle bundle = null;
if (data != null && !data.isEmpty()) {
bundle = new Bundle();
bundle.putString("data", data);
}
textInputMethodHandler.sendAppPrivateCommand(action, bundle);
result.success(null);
} catch (JSONException exception) {
result.error("error", exception.getMessage(), null);
}
break;
case "TextInput.finishAutofillContext":
textInputMethodHandler.finishAutofillContext((boolean) args);
result.success(null);
@@ -328,6 +345,17 @@ public class TextInputChannel {
// TODO(mattcarroll): javadoc
void clearClient();
/**
* Sends client app private command to the current text input client(input method). The app
* private command result will be informed through {@code performPrivateCommand}.
*
* @param action Name of the command to be performed. This must be a scoped name. i.e. prefixed
* with a package name you own, so that different developers will not create conflicting
* commands.
* @param data Any data to include with the command.
*/
void sendAppPrivateCommand(String action, Bundle data);
}
/** A text editing configuration. */

View File

@@ -8,6 +8,7 @@ import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.Editable;
import android.text.InputType;
@@ -119,6 +120,11 @@ public class TextInputPlugin {
public void clearClient() {
clearTextInputClient();
}
@Override
public void sendAppPrivateCommand(String action, Bundle data) {
sendTextInputAppPrivateCommand(action, data);
}
});
textInputChannel.requestExistingInputState();
@@ -303,6 +309,10 @@ public class TextInputPlugin {
}
}
public void sendTextInputAppPrivateCommand(String action, Bundle data) {
mImm.sendAppPrivateCommand(mView, action, data);
}
private void showTextInput(View view) {
view.requestFocus();
mImm.showSoftInput(view, 0);

View File

@@ -18,6 +18,7 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.SparseIntArray;
import android.view.KeyEvent;
@@ -40,6 +41,7 @@ import java.nio.ByteBuffer;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
@@ -566,6 +568,74 @@ public class TextInputPluginTest {
verify(mockHandler, times(1)).finishAutofillContext(false);
}
@Test
public void sendAppPrivateCommand_dataIsEmpty() throws JSONException {
ArgumentCaptor<BinaryMessenger.BinaryMessageHandler> binaryMessageHandlerCaptor =
ArgumentCaptor.forClass(BinaryMessenger.BinaryMessageHandler.class);
DartExecutor mockBinaryMessenger = mock(DartExecutor.class);
TextInputChannel textInputChannel = new TextInputChannel(mockBinaryMessenger);
EventHandler mockEventHandler = mock(EventHandler.class);
TestImm testImm =
Shadow.extract(
RuntimeEnvironment.application.getSystemService(Context.INPUT_METHOD_SERVICE));
testImm.setEventHandler(mockEventHandler);
View testView = new View(RuntimeEnvironment.application);
TextInputPlugin textInputPlugin =
new TextInputPlugin(testView, textInputChannel, mock(PlatformViewsController.class));
verify(mockBinaryMessenger, times(1))
.setMessageHandler(any(String.class), binaryMessageHandlerCaptor.capture());
JSONObject arguments = new JSONObject();
arguments.put("action", "actionCommand");
arguments.put("data", "");
BinaryMessenger.BinaryMessageHandler binaryMessageHandler =
binaryMessageHandlerCaptor.getValue();
sendToBinaryMessageHandler(binaryMessageHandler, "TextInput.sendAppPrivateCommand", arguments);
verify(mockEventHandler, times(1))
.sendAppPrivateCommand(any(View.class), eq("actionCommand"), eq(null));
}
@Test
public void sendAppPrivateCommand_hasData() throws JSONException {
ArgumentCaptor<BinaryMessenger.BinaryMessageHandler> binaryMessageHandlerCaptor =
ArgumentCaptor.forClass(BinaryMessenger.BinaryMessageHandler.class);
DartExecutor mockBinaryMessenger = mock(DartExecutor.class);
TextInputChannel textInputChannel = new TextInputChannel(mockBinaryMessenger);
EventHandler mockEventHandler = mock(EventHandler.class);
TestImm testImm =
Shadow.extract(
RuntimeEnvironment.application.getSystemService(Context.INPUT_METHOD_SERVICE));
testImm.setEventHandler(mockEventHandler);
View testView = new View(RuntimeEnvironment.application);
TextInputPlugin textInputPlugin =
new TextInputPlugin(testView, textInputChannel, mock(PlatformViewsController.class));
verify(mockBinaryMessenger, times(1))
.setMessageHandler(any(String.class), binaryMessageHandlerCaptor.capture());
JSONObject arguments = new JSONObject();
arguments.put("action", "actionCommand");
arguments.put("data", "actionData");
ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class);
BinaryMessenger.BinaryMessageHandler binaryMessageHandler =
binaryMessageHandlerCaptor.getValue();
sendToBinaryMessageHandler(binaryMessageHandler, "TextInput.sendAppPrivateCommand", arguments);
verify(mockEventHandler, times(1))
.sendAppPrivateCommand(any(View.class), eq("actionCommand"), bundleCaptor.capture());
assertEquals("actionData", bundleCaptor.getValue().getCharSequence("data"));
}
interface EventHandler {
void sendAppPrivateCommand(View view, String action, Bundle data);
}
@Implements(InputMethodManager.class)
public static class TestImm extends ShadowInputMethodManager {
private InputMethodSubtype currentInputMethodSubtype;
@@ -573,6 +643,7 @@ public class TextInputPluginTest {
private CursorAnchorInfo cursorAnchorInfo;
private ArrayList<Integer> selectionUpdateValues;
private boolean trackSelection = false;
private EventHandler handler;
public TestImm() {
selectionUpdateValues = new ArrayList<Integer>();
@@ -597,6 +668,15 @@ public class TextInputPluginTest {
return restartCounter.get(view.hashCode(), /*defaultValue=*/ 0);
}
public void setEventHandler(EventHandler eventHandler) {
handler = eventHandler;
}
@Implementation
public void sendAppPrivateCommand(View view, String action, Bundle data) {
handler.sendAppPrivateCommand(view, action, data);
}
@Implementation
public void updateCursorAnchorInfo(View view, CursorAnchorInfo cursorAnchorInfo) {
this.cursorAnchorInfo = cursorAnchorInfo;