From ce5194184d6f22e53c26bb5b8de4163580e69329 Mon Sep 17 00:00:00 2001 From: Bruno Leroux Date: Tue, 26 Sep 2023 22:39:05 +0200 Subject: [PATCH] [Android] Fix enableSuggestions set to false not honored (flutter/engine#46037) ## Description This PR fixes an issue where setting `TextField.enableSuggestions` to false was not honored on Android. Several Android devices (Samsung) and/or IMEs (including GBoard) does not disabled suggestions when `InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS `flag is set. The common solution is to rely on the following flag: ~~`InputType.TYPE_TEXT_VARIATION_PASSWORD`~~ `InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD`. Reference: - https://issuetracker.google.com/issues/36934423#comment4 - https://stackoverflow.com/questions/33148168/inputtype-type-text-flag-no-suggestions-in-samsung/33227237#33227237 - Existing comment on the codebase: https://github.com/flutter/engine/blob/195a31324587fffa002caaffa123c2cd5816381c/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java#L270 ## Related Issue Fixes https://github.com/flutter/flutter/issues/71679. ## Tests Adds 1 test. --- .../plugin/editing/TextInputPlugin.java | 6 +++- .../plugin/editing/TextInputPluginTest.java | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) 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 f337132b1c8..4897eaf2f13 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 @@ -272,7 +272,11 @@ public class TextInputPlugin implements ListenableEditingState.EditingStateWatch textType |= InputType.TYPE_TEXT_VARIATION_PASSWORD; } else { if (autocorrect) textType |= InputType.TYPE_TEXT_FLAG_AUTO_CORRECT; - if (!enableSuggestions) textType |= InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS; + if (!enableSuggestions) { + // Note: both required. Some devices ignore TYPE_TEXT_FLAG_NO_SUGGESTIONS. + textType |= InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS; + textType |= InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD; + } } if (textCapitalization == TextInputChannel.TextCapitalization.CHARACTERS) { 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 881dffc1888..c4eebd33b93 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 @@ -1306,6 +1306,42 @@ public class TextInputPluginTest { assertEquals(testImm.isSoftInputVisible(), false); } + @Test + public void inputConnection_textInputTypeMultilineAndSuggestionsDisabled() { + // Regression test for https://github.com/flutter/flutter/issues/71679. + View testView = new View(ctx); + DartExecutor dartExecutor = mock(DartExecutor.class); + TextInputChannel textInputChannel = new TextInputChannel(dartExecutor); + TextInputPlugin textInputPlugin = + new TextInputPlugin(testView, textInputChannel, mock(PlatformViewsController.class)); + textInputPlugin.setTextInputClient( + 0, + new TextInputChannel.Configuration( + false, + false, + false, // Disable suggestions. + true, + false, + TextInputChannel.TextCapitalization.NONE, + new TextInputChannel.InputType(TextInputChannel.TextInputType.MULTILINE, false, false), + null, + null, + null, + null, + null)); + + EditorInfo editorInfo = new EditorInfo(); + InputConnection connection = + textInputPlugin.createInputConnection(testView, mock(KeyboardManager.class), editorInfo); + + assertEquals( + editorInfo.inputType, + InputType.TYPE_CLASS_TEXT + | InputType.TYPE_TEXT_FLAG_MULTI_LINE + | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS + | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); + } + // -------- Start: Autofill Tests ------- @Test public void autofill_enabledByDefault() {