Skip to content

Commit

Permalink
Instead of input connection override disable auto-correct.
Browse files Browse the repository at this point in the history
  • Loading branch information
khaykov committed Oct 11, 2023
1 parent 29b449e commit 794e1bd
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import android.os.Parcelable
import android.provider.Settings
import android.text.Editable
import android.text.InputFilter
import android.text.InputType
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.Spanned
Expand Down Expand Up @@ -238,7 +239,6 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
private var bypassObservationQueue: Boolean = false
private var bypassMediaDeletedListener: Boolean = false
private var bypassCrashPreventerInputFilter: Boolean = false
private var overrideSamsungPredictiveBehavior: Boolean = false

var initialEditorContentParsedSHA256: ByteArray = ByteArray(0)

Expand Down Expand Up @@ -673,13 +673,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
}

override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
val baseInputConnection = requireNotNull(super.onCreateInputConnection(outAttrs)).wrapWithBackSpaceHandler()
return if (shouldOverridePredictiveTextBehavior()) {
AppLog.d(AppLog.T.EDITOR, "Overriding predictive text behavior on Samsung device with Samsung Keyboard with API 33")
SamsungInputConnection(this, baseInputConnection)
} else {
baseInputConnection
}
return requireNotNull(super.onCreateInputConnection(outAttrs)).wrapWithBackSpaceHandler()
}

private fun InputConnection.wrapWithBackSpaceHandler(): InputConnection {
Expand All @@ -705,7 +699,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
private fun shouldOverridePredictiveTextBehavior(): Boolean {
val currentKeyboard = Settings.Secure.getString(context.contentResolver, Settings.Secure.DEFAULT_INPUT_METHOD)
return Build.MANUFACTURER.lowercase(Locale.US) == "samsung" && Build.VERSION.SDK_INT >= 33 &&
(currentKeyboard !== null && currentKeyboard.startsWith("com.samsung.android.honeyboard")) && overrideSamsungPredictiveBehavior
(currentKeyboard !== null && currentKeyboard.startsWith("com.samsung.android.honeyboard"))
}

// Setup the keyListener(s) for Backspace and Enter key.
Expand Down Expand Up @@ -1778,11 +1772,13 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
bypassMediaDeletedListener = false
}

// removes Grammarly suggestions from default keyboard on Samsung devices on Android 13 (API 33)
// removes auto-correct from default keyboard on Samsung devices on Android 13 (API 33)
// Grammarly implementation is often messing spans and cursor position, as described here:
// https://github.com/wordpress-mobile/AztecEditor-Android/issues/1023
fun enableSamsungPredictiveBehaviorOverride() {
overrideSamsungPredictiveBehavior = true
if(shouldOverridePredictiveTextBehavior()){
inputType = inputType or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
}
}

fun isMediaDeletedListenerDisabled(): Boolean {
Expand Down

0 comments on commit 794e1bd

Please sign in to comment.