Skip to content

Bug: Crash on Android 13+ due to unsafe cast in EditorInputConnection.getSurroundingText #905

@KEFIRKA01

Description

@KEFIRKA01

Bug: Crash on Android 13+ due to unsafe cast in EditorInputConnection.getSurroundingText

Description

On Android 13 and newer (observed on Android 14/15), the application crashes with a ClassCastException when the IME requests surrounding text.
The crash happens immediately after the activity is resumed, before any user input is performed.

This issue occurs in the editor component (io.github.rosemoe.sora) and makes the editor unusable on affected devices.


Steps to Reproduce

  1. Install the app on an Android 13+ device
  2. Launch the app
  3. Activity resumes and the keyboard interacts with the editor
  4. App crashes immediately

No typing is required to trigger the crash.


Expected Behavior

The editor should safely handle surrounding text returned by the IME, regardless of whether it is a Spanned or a plain CharSequence, and must not crash.


Actual Behavior

The app crashes with the following exception:

java.lang.ClassCastException: java.lang.String cannot be cast to android.text.Spanned
    at io.github.rosemoe.sora.widget.EditorInputConnection.getSurroundingText(EditorInputConnection.java:588)
    at android.view.inputmethod.RemoteInputConnectionImpl.lambda$getSurroundingText$11(RemoteInputConnectionImpl.java:560)
    ...

Root Cause

EditorInputConnection.getSurroundingText(...) assumes that the surrounding text returned by the Android framework is always of type Spanned and performs an unsafe cast.

On modern Android versions (13+), InputMethodManager / RemoteInputConnectionImpl do not guarantee that surrounding text is a Spanned.
Depending on the IME (Gboard, OEM keyboards, etc.), a plain String may be returned, which leads to a ClassCastException.

This violates the current Android IME contract.


Why This Is Important

  • The crash happens on app launch, making the editor unusable.
  • Android 13+ devices represent a rapidly growing majority of users.
  • The issue is especially common on OEM systems (e.g. Xiaomi / MIUI / HyperOS).
  • It blocks real-world usage of the editor in IDEs, code editors, and automation tools where text input stability is critical.

Environment

  • Device: Xiaomi 23124RA7EO
  • Android version: 14 / 15
  • App build: 0.2.9 ALPHA
  • IME: Gboard / OEM keyboard
  • Editor: io.github.rosemoe.sora

Suggested Fix

Safely handle non-Spanned CharSequence values returned by the IME instead of assuming Spanned.
This aligns with Android framework recommendations and prevents crashes on modern Android versions.


Additional Context

The issue is reproducible without any special configuration and occurs consistently on affected devices.

Please let me know if additional logs, testing, or device information are needed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions