diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/TextInputChannel.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/TextInputChannel.java index 8cb778daf1..78f6ec86e6 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/TextInputChannel.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/TextInputChannel.java @@ -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. */ diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java index 1aea8fbb27..1a635f13ac 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java @@ -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); diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java index bd4eb6f4b3..1a6b53c6b7 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java @@ -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 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 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 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 selectionUpdateValues; private boolean trackSelection = false; + private EventHandler handler; public TestImm() { selectionUpdateValues = new ArrayList(); @@ -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;