Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
[Android] Implement replaceText (#902)
Browse files Browse the repository at this point in the history
* Implement `replaceText` which is used in Android 13+ instead of `commitText` for replacing text with suggestions

* Create shared `replaceTextInternal` function for replacing text
  • Loading branch information
jmartinesp authored Dec 12, 2023
1 parent 3529e0d commit a87e5e6
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ internal class InterceptInputConnection(
// Called for suggestion from IME selected
override fun commitText(text: CharSequence?, newCursorPosition: Int): Boolean {
val (start, end) = getCurrentCompositionOrSelection()
return replaceTextInternal(start, end, text)
}

// In Android 13+, this is called instead of [commitText] when selecting suggestions from IME
override fun replaceText(
start: Int,
end: Int,
text: CharSequence,
newCursorPosition: Int,
textAttribute: TextAttribute?
): Boolean {
return replaceTextInternal(start, end, text)
}

private fun replaceTextInternal(
start: Int,
end: Int,
text: CharSequence?,
): Boolean {
val result = processTextEntry(text, start, end)

return if (result != null) {
Expand All @@ -137,7 +156,7 @@ internal class InterceptInputConnection(
endBatchEdit()
true
} else {
super.commitText(text, newCursorPosition)
false
}
}

Expand Down

0 comments on commit a87e5e6

Please sign in to comment.