wordpress-mobile / AztecEditor-Android

A reusable native Android rich text editor component.
Mozilla Public License 2.0
675 stars 112 forks source link

Fix predictive text issue with Samsung devices on Android 13 #1024

Closed khaykov closed 1 year ago

khaykov commented 1 year ago

Fixes #1023

This PR fixes the predictive text (Grammarly) issue present in default Samsung keyboard on devices with Android 13 (api 33).

Problem: When Grammarly kick-in and marks issues in the text, content of the editor is replaced with malformed content, where some of the existing spans are removed from editor, and cursor sometimes moves to the end of the text.

Video from the issue to illustrate it: https://user-images.githubusercontent.com/728822/211878346-1d296b47-8031-4f18-841a-23fb2a190172.mp4

In addition, we can't debug the Android source code, since it is different on Samsung.

Solution: I tried multiple approaches using InputFilters and TextWatchers to detect when the issue is happening, and remedy it. While they do work to some extent, they are not very reliable, and there is one problem I was not able to adress - cursor moving to the end of the text (and scrolling the editor as well).

My last resort was trying to implement a custom InputConnection, but I realized that I don't necessarily need to do it - I can make a wrapper around Samsung version, and direct/intercept all the calls to it to see what is up. This worked well, and If found out that Samsung is using Extracted Text (one you usually see in EditText when it expands in landscape mode) for Grammarly, and returning null from it (default behavior of BaseInputConnection) disables the Grammarly, but still keeps regular suggestions and spellchecker.

Unfortunately, I was not able to make Grammarly work without messing up content of the editor, but in my opinion that's fine.

The fix is disabled by default in a library, but enable in Demo App.

Test device targeting

  1. Launch the app on Samsung device on Android 13
  2. Tap in the editor to bring up the keyboard.
  3. In Logcat confirm that you see this line Overriding predictive text behavior on Samsung device with API 33
  4. Try same on other device (not Samsung or different Android version) and confirm that that line is missing from logcat.

Test the fix

  1. Launch the app on Samsung device on Android 13 with default Samsung keyboard (and predictive text enabled, which is default).
  2. On a new line, anywhere in demo content type "Hello my name is . "
  3. Notice that nothing happens - images are still present, and you are not scrolled to the bottom of the screen.
  4. Try typing other things, and confirm that nothing adverse is happening.
khaykov commented 1 year ago

I added a fix for paragraph spans (like heading) leaking on the next line.