Skip to content

Commit

Permalink
Add logic to address Gboard's unusual invocation of TextWatcher methods
Browse files Browse the repository at this point in the history
Note: This is probably not enough, since this only addresses when the
caret is at the end of the word, but Gboard may exhibit the unusual
behavior also when the caret is in the middle or start of the word.
  • Loading branch information
mkevins committed Mar 30, 2021
1 parent adc0b38 commit 5581743
Showing 1 changed file with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter)
private var selEnd: Int = 0
var done = false

private var gboardReplacement: CharSequence? = null


override fun beforeTextChanged(text: CharSequence, start: Int, count: Int, after: Int) {
val aztecText = aztecTextRef.get()
if (aztecText?.getAztecKeyListener() != null && !aztecText.isTextChangedListenerDisabled()) {
Expand All @@ -32,6 +35,13 @@ class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter)
this.start = start
this.selStart = aztecText.selectionStart
this.selEnd = aztecText.selectionEnd

if (selStart == selEnd && 0 < count && count < after) {
// possible gboard replacement detected
gboardReplacement = text.subSequence(start, start + count)
} else {
gboardReplacement = null
}
}
}

Expand All @@ -40,14 +50,25 @@ class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter)
val aztecKeyListener = aztecText?.getAztecKeyListener()
if (aztecText != null && !aztecText.isTextChangedListenerDisabled() && aztecKeyListener != null) {
val newTextCopy = SpannableStringBuilder(text)

var gboardOffset = this.start
// If gboard replacement is happening, we offset the start position by the length
// of the gboard replacement
if (gboardReplacement != null) {
val gboardRestored = newTextCopy.subSequence(start, start + before)
if (gboardRestored.toString() == gboardReplacement.toString()) {
gboardOffset += gboardRestored.length
}
}

// if new text length is longer than original text by 1
if (textBefore?.length == newTextCopy.length - 1) {
// now check that the inserted character is actually a NEWLINE
if (newTextCopy[this.start] == Constants.NEWLINE) {
if (newTextCopy[gboardOffset] == Constants.NEWLINE) {
done = false
aztecText.editableText.setSpan(EnterPressedUnderway(), 0, 0, Spanned.SPAN_USER)
aztecKeyListener.onEnterKey(
newTextCopy.replace(this.start, this.start + 1, ""),
newTextCopy.replace(gboardOffset, gboardOffset + 1, ""),
true,
selStart,
selEnd
Expand All @@ -61,9 +82,12 @@ class EnterPressedWatcher(aztecText: AztecText, var enterDeleter: EnterDeleter)
aztecTextRef.get()?.editableText?.getSpans(0, 0, EnterPressedUnderway::class.java)?.forEach {
if (!done) {
done = true
if (enterDeleter.shouldDeleteEnter())
text.replace(start, start + 1, "")
if (enterDeleter.shouldDeleteEnter()) {
var gboardOffset = start
gboardReplacement?.let { replacement -> gboardOffset += replacement.length }
text.replace(gboardOffset, gboardOffset + 1, "")
}
}
aztecTextRef.get()?.editableText?.removeSpan(it)
}
}
Expand Down

0 comments on commit 5581743

Please sign in to comment.