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

Commit 8f9233e

Browse files
android implementation
1 parent 5184d7f commit 8f9233e

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

shell/platform/android/io/flutter/embedding/engine/systemchannels/TextInputChannel.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
112112
textInputMethodHandler.clearClient();
113113
result.success(null);
114114
break;
115+
case "TextInput.AutofillContext.commit":
116+
textInputMethodHandler.finishAutofillContext(true);
117+
result.success(null);
118+
break;
119+
case "TextInput.AutofillContext.cancel":
120+
textInputMethodHandler.finishAutofillContext(false);
121+
result.success(null);
122+
break;
115123
default:
116124
result.notImplemented();
117125
break;
@@ -284,6 +292,18 @@ public interface TextInputMethodHandler {
284292
*/
285293
void requestAutofill();
286294

295+
/**
296+
* Requests that the {@link AutofillManager} cancel or commit the current
297+
* autofill context, depending on the value of {@code shouldSave}.
298+
*
299+
* The method calls {@link android.view.autofill.AutofillManager#commit()} when {@code shouldSave}
300+
* is true, and calls {@link android.view.autofill.AutofillManager#cancel()} otherwise.
301+
*
302+
* @param shouldSave whether the active autofill service should save the
303+
* current user input for future use.
304+
*/
305+
void finishAutofillContext(boolean shouldSave);
306+
287307
// TODO(mattcarroll): javadoc
288308
void setClient(int textInputClientId, @NonNull Configuration configuration);
289309

shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ public void requestAutofill() {
8282
notifyViewEntered();
8383
}
8484

85+
@Override
86+
public void finishAutofillContext(boolean shouldSave) {
87+
if (afm == null)
88+
return;
89+
if (shouldSave) {
90+
afm.commit();
91+
} else {
92+
afm.cancel();
93+
}
94+
}
95+
8596
@Override
8697
public void setClient(
8798
int textInputClientId, TextInputChannel.Configuration configuration) {

shell/platform/android/test/io/flutter/plugin/editing/TextInputPluginTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import io.flutter.plugin.common.BinaryMessenger;
3636
import io.flutter.plugin.common.JSONMethodCodec;
3737
import io.flutter.plugin.common.MethodCall;
38+
import io.flutter.plugin.common.MethodChannel;
3839
import io.flutter.plugin.platform.PlatformViewsController;
3940
import java.nio.ByteBuffer;
4041
import java.util.ArrayList;
@@ -70,6 +71,12 @@ private void verifyMethodCall(ByteBuffer buffer, String methodName, String[] exp
7071
}
7172
}
7273

74+
private static void sendToBinaryMessageHandler(BinaryMessenger.BinaryMessageHandler binaryMessageHandler, String method, Object args) {
75+
MethodCall methodCall = new MethodCall(method, args);
76+
ByteBuffer encodedMethodCall = JSONMethodCodec.INSTANCE.encodeMethodCall(methodCall).position(0);
77+
binaryMessageHandler.onMessage(encodedMethodCall, mock(BinaryMessenger.BinaryReply.class));
78+
}
79+
7380
@Test
7481
public void textInputPlugin_RequestsReattachOnCreation() throws JSONException {
7582
// Initialize a general TextInputPlugin.
@@ -531,6 +538,32 @@ public void autofill_onProvideVirtualViewStructure_single() {
531538
verify(children[0]).setDimens(anyInt(), anyInt(), anyInt(), anyInt(), geq(0), geq(0));
532539
}
533540

541+
@Test
542+
public void respondsToInputChannelMessages () {
543+
ArgumentCaptor<BinaryMessenger.BinaryMessageHandler> binaryMessageHandlerCaptor = ArgumentCaptor.forClass(BinaryMessenger.BinaryMessageHandler.class);
544+
DartExecutor mockBinaryMessenger = mock(DartExecutor.class);
545+
TextInputChannel.TextInputMethodHandler mockHandler = mock(TextInputChannel.TextInputMethodHandler.class);
546+
TextInputChannel textInputChannel = new TextInputChannel(mockBinaryMessenger);
547+
548+
textInputChannel.setTextInputMethodHandler(mockHandler);
549+
550+
verify(mockBinaryMessenger, times(1))
551+
.setMessageHandler(
552+
any(String.class),
553+
binaryMessageHandlerCaptor.capture());
554+
555+
BinaryMessenger.BinaryMessageHandler binaryMessageHandler = binaryMessageHandlerCaptor.getValue();
556+
557+
sendToBinaryMessageHandler(binaryMessageHandler, "TextInput.requestAutofill", null);
558+
verify(mockHandler, times(1)).requestAutofill();
559+
560+
sendToBinaryMessageHandler(binaryMessageHandler, "TextInput.AutofillContext.commit", null);
561+
verify(mockHandler, times(1)).finishAutofillContext(true);
562+
563+
sendToBinaryMessageHandler(binaryMessageHandler, "TextInput.AutofillContext.cancel", null);
564+
verify(mockHandler, times(1)).finishAutofillContext(false);
565+
}
566+
534567
@Implements(InputMethodManager.class)
535568
public static class TestImm extends ShadowInputMethodManager {
536569
private InputMethodSubtype currentInputMethodSubtype;

0 commit comments

Comments
 (0)