Repository navigation
android: report active text input target to IME - #189662
prefanatic wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request overrides onCheckIsTextEditor in FlutterView to determine if the view currently creates an input connection, relying on a new isInputConnectionTarget method in TextInputPlugin. It also includes corresponding unit tests. The review feedback suggests refactoring the FlutterViewTest to mock flutterEngine.getTextInputPlugin() instead of using reflection to set the private textInputPlugin field, which improves test robustness and readability.
camsim99
left a comment
There was a problem hiding this comment.
Besides avoiding reflection in the test, this change looks reasonable to me! Thanks for taking a look at the issue :) let's get a second review
…dding/android/FlutterViewTest.java Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
camsim99
left a comment
There was a problem hiding this comment.
No worries, thanks for the quick fix!
|
Hi @prefanatic, thank you for the contribution but after some though I think for now we are going to move forward with a similar change in #187778 that accomplishes the same functionality. While I'm closing this PR, thank you again for putting together this patch and working with us on reviews. The team would gladly welcome future contributions from you! |
…t is attached (flutter#193074) <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> `FlutterView` never overrides [`View.onCheckIsTextEditor()`](https://developer.android.com/reference/android/view/View#onCheckIsTextEditor()), so it always reports `false`. Android's `InputMethodManager` turns that into the `IS_TEXT_EDITOR` start-input flag ([`InputMethodManager.java#L2945`](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android14-release/core/java/android/view/inputmethod/InputMethodManager.java#2945)), and devices that ship `config_preventImeStartupUnlessTextEditor=true` (default `false` in [`config.xml#L3198`](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android14-release/core/res/res/values/config.xml#3198); set by the low-RAM Android TV overlay on Chromecast with Google TV, Philips/TCL Google TVs) answer every `startInput` without that flag with `NO_EDITOR` and unbind the IME, unless a show is already pending for the window ([`InputMethodManagerService.startInputUncheckedLocked`](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android14-release/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java#2598), [`shouldPreventImeStartupLocked`](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android14-release/services/core/java/com/android/server/inputmethod/InputMethodManagerService.java#2647)). On those devices the sequence for a Flutter `TextField` is: 1. Every Flutter-originated `startInput` returns `NO_EDITOR` and kills the IME service: window focus gain, the `restartInput` in [`TextInputPlugin.setTextInputEditingState`](https://github.com/flutter/flutter/blob/19946f91c8d9de18a4674460d015229cc0b2534f/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java#L528), and the `restartInput` in the [IME hide listener](https://github.com/flutter/flutter/blob/19946f91c8d9de18a4674460d015229cc0b2534f/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java#L98-L106). The client records no bind sequence (`mBindSequence=-1`). 2. [`showTextInput`](https://github.com/flutter/flutter/blob/19946f91c8d9de18a4674460d015229cc0b2534f/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java#L431-L440) → `showSoftInput` marks a show as requested, so IMMS binds the IME again (logcat shows `GoogleInputMethodService.onCreate()` ~30 ms after the show) and creates a session. 3. IMMS sends the client `onBindMethod` with the new sequence, which the client discards because its own sequence is `-1`: ``` W InputMethodManager: Ignoring onBind: cur seq=-1, given seq=626 ``` The keyboard is drawn (IME `mInputViewStarted=true`) but the app holds no session (`mCurMethod=null`, IME `mInputBinding=null`). 4. With no session, `ViewRootImpl`'s `ImeInputStage` returns `DISPATCH_NOT_HANDLED` for every key, so D-pad events fall through to the view hierarchy, Flutter moves focus, the field detaches, and the keyboard hides. That is the "keyboard disappears on any button" from the issue. This also explains both workarounds reported on the issue: a platform-view `EditText` works because `EditText.onCheckIsTextEditor()` is `true`, and a hot restart heals it (until Back closes the keyboard) because a `startInput` issued while a show is pending bypasses `shouldPreventImeStartupLocked` and records a real sequence. `dumpsys input_method` on a Chromecast 4K (Android 14, Gboard TV 17.3.09) while the keyboard was visible but dead: ``` # IMMS mCurClient=ClientState{... mPid=13034} mCurSeq=627 mCurId=.../LatinIME mHaveConnection=true mBoundToMethod=true mInputShown=true # app client (pid 13034) mBindSequence=-1 mCurImeId=null mCurMethod= null mServedView=io.flutter.embedding.android.FlutterView{...} mCurrentEditorInfo: inputType=0x80091 imeOptions=0x2000002 # IME service mInputBinding=null mInputConnection=null mInputStarted=true mInputViewStarted=true ``` Every `StartInput` history entry has reason `SESSION_CREATED_BY_IME` with a fresh `imeToken`. On a Google TV Streamer (4 GB, flag off, same Gboard build) the same flow shows `mBindSequence == mCurSeq`, `mCurMethod` populated, and reasons `WINDOW_FOCUS_GAIN` / `APP_CALLED_RESTART_INPUT_API` with a stable token. ### Change `FlutterView.onCheckIsTextEditor()` now returns `isAttachedToFlutterEngine() && textInputPlugin.isTextEditor()`, mirroring the guard in [`onCreateInputConnection`](https://github.com/flutter/flutter/blob/19946f91c8d9de18a4674460d015229cc0b2534f/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterView.java#L906-L914). The new `TextInputPlugin.isTextEditor()` is `true` when a framework client with an input type other than `TextInputType.none` is attached (the same condition `showTextInput` already uses to decide whether to show the keyboard), or when a virtual-display platform view is the input target (its `InputConnection` is [proxied through `FlutterView`](https://github.com/flutter/flutter/blob/19946f91c8d9de18a4674460d015229cc0b2534f/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java#L327-L336)). Hybrid-composition platform views own their `InputConnection`, so they stay `false`. Ordering is already right on the framework side: [`EditableText._openInputConnection`](https://github.com/flutter/flutter/blob/19946f91c8d9de18a4674460d015229cc0b2534f/packages/flutter/lib/src/widgets/editable_text.dart#L4131) calls `TextInput.attach` (`setClient`) before `setEditingState` and `show`, so the input target is `FRAMEWORK_CLIENT` by the time the flag is read. [`clearTextInputClient`](https://github.com/flutter/flutter/blob/19946f91c8d9de18a4674460d015229cc0b2534f/engine/src/flutter/shell/platform/android/io/flutter/plugin/editing/TextInputPlugin.java#L578) resets the target, so the flag drops when the field detaches, which matches `EditText` being a text editor for as long as it exists. This is the union of the two earlier attempts at this override: flutter#189662 (`FRAMEWORK_CLIENT || VIRTUAL_DISPLAY_PLATFORM_VIEW`) and flutter#187778 (`FRAMEWORK_CLIENT` with a non-`none` input type), which @Renzo-Olivares suggested combining in flutter#187778 (comment). Both were closed without landing; this PR only carries the `onCheckIsTextEditor` part, not the focus-restore logic from flutter#187778. ### Behavior to call out `IS_TEXT_EDITOR` (`hasEditorFocused()` in [`ImeVisibilityStateComputer.computeState`](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/android14-release/services/core/java/com/android/server/inputmethod/ImeVisibilityStateComputer.java#398)) feeds three IMMS decisions, and this change flips all of them for a `FlutterView` with an attached client: 1. The startup-prevention gate above, which is the bug. 2. `SOFT_INPUT_STATE_UNSPECIFIED` + window focus gain: today IMMS hides the IME (`HIDE_UNSPECIFIED_WINDOW`) because the focused view is not an editor. With this change, regaining window focus while a `TextField` is attached no longer hides the keyboard, same as a native `EditText`. This likely fixes a class of "keyboard closes when a system dialog/popup is dismissed" reports; [flutter#187778 measured](flutter#187778 (comment)) this override alone as the difference for keyboard restore after Home → Recents on a Pixel 7. 3. `SOFT_INPUT_STATE_UNSPECIFIED` + `adjustResize` (or a large layout) + `SOFT_INPUT_IS_FORWARD_NAVIGATION` + text editor: IMMS auto-shows the IME on window focus gain (`SHOW_AUTO_EDITOR_FORWARD_NAV`). `EditText` apps get this today; Flutter apps with an attached client start getting it too. Flutter's app template sets [`windowSoftInputMode="adjustResize"`](https://github.com/flutter/flutter/blob/19946f91c8d9de18a4674460d015229cc0b2534f/packages/flutter_tools/templates/app/android.tmpl/app/src/main/AndroidManifest.xml.tmpl), so this applies to most apps. The flag is computed by the framework from `onCheckIsTextEditor()` regardless of start reason, so it cannot be suppressed for `WINDOW_GAINED_FOCUS` only; flagging it here for reviewers to weigh. Not affected: key routing (`FlutterView.dispatchKeyEvent` is unchanged), `InputConnectionAdaptor`, IME insets sync, and autofill (`notifyViewEntered` already uses `mView`). ### Verification The affected Robolectric classes were run locally against this branch with the `shell/platform/android/test_runner` Gradle configuration (compileSdk 36, Robolectric 4.16, Mockito 5.17.0, embedding dependencies from `tools/androidx/files.json`) without a full engine build: | class | tests | result | |---|---|---| | `io.flutter.embedding.android.FlutterViewTest` | 89 | all pass | | `io.flutter.plugin.editing.TextInputPluginTest` | 117 | all pass | `FlutterViewTest.onCheckIsTextEditor_reflectsAttachedTextInputClient` fails at the assertion after `setClient` with the `FlutterView` override removed and passes with it. The Java files are formatted with google-java-format 1.7, the version pinned in `DEPS`. The same mechanism is what Plezy ships today as an app-side workaround on Android TV: a 1×1 focusable child of `FlutterView` that reports `onCheckIsTextEditor()` from a Dart-side flag, delegates `onCreateInputConnection` to `FlutterView`, and returns `true` from `checkInputConnectionProxy(flutterView)`; D-pad input in `TextField`s works on the affected Chromecast with that in place. This engine change makes the proxy view unnecessary. Fixes flutter#177360 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Camille Simon <[email protected]>
On Android, dismissing an autofill popup after the first character can also dismiss the keyboard. Android reports this as
HIDE_SAME_WINDOW_FOCUSED_WITHOUT_EDITOR.FlutterViewcreates input connections but inherits the defaultView.onCheckIsTextEditor()implementation, which returnsfalse. When the autofill UI closes and Android checks the focused window, it can therefore conclude that the window no longer contains a text editor and hide the IME.This change overrides
FlutterView.onCheckIsTextEditor()and reportstrueonly when:TextInputPlugintarget creates its input connection throughFlutterView.With this PR, we aim to resolve #159670.
An example application providing a reproducible case is available at:
https://github.com/prefanatic/flutter-autofill-keyboard-dismiss-repro
I've been testing personally on a Pixel 10 Pro running Android 16, using Gboard, and Bitwarden representing the autofill feature.
Before the fix, we can see the keyboard dismisses when a character is entered.
https://github.com/user-attachments/assets/78f3941f-be0c-432c-9071-d082f6ee4307
After the fix, the keyboard stays up.
https://github.com/user-attachments/assets/c0a22784-5562-4c6d-ab8c-e5f0e5e5f53c
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.