@@ -48,6 +48,8 @@ import android.view.MotionEvent
4848import android.view.View
4949import android.view.WindowManager
5050import android.view.inputmethod.BaseInputConnection
51+ import android.view.inputmethod.EditorInfo
52+ import android.view.inputmethod.InputConnection
5153import android.widget.CheckBox
5254import android.widget.EditText
5355import android.widget.Toast
@@ -70,6 +72,7 @@ import org.wordpress.aztec.handlers.ListHandler
7072import org.wordpress.aztec.handlers.ListItemHandler
7173import org.wordpress.aztec.handlers.PreformatHandler
7274import org.wordpress.aztec.handlers.QuoteHandler
75+ import org.wordpress.aztec.ime.EditorInfoUtils
7376import org.wordpress.aztec.plugins.IAztecPlugin
7477import org.wordpress.aztec.plugins.IToolbarButton
7578import org.wordpress.aztec.source.Format
@@ -118,6 +121,7 @@ import org.wordpress.aztec.watchers.event.text.BeforeTextChangedEventData
118121import org.wordpress.aztec.watchers.event.text.OnTextChangedEventData
119122import org.wordpress.aztec.watchers.event.text.TextWatcherEvent
120123import org.xml.sax.Attributes
124+ import java.lang.ref.WeakReference
121125import java.security.MessageDigest
122126import java.security.NoSuchAlgorithmException
123127import java.util.ArrayList
@@ -291,6 +295,9 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
291295
292296 private var focusOnVisible = true
293297
298+ var inputConnectionRef: WeakReference <InputConnection >? = null
299+ var inputConnectionEditorInfo: EditorInfo ? = null
300+
294301 interface OnSelectionChangedListener {
295302 fun onSelectionChanged (selStart : Int , selEnd : Int )
296303 }
@@ -660,6 +667,45 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
660667 }
661668 }
662669
670+ override fun onCreateInputConnection (outAttrs : EditorInfo ) : InputConnection {
671+ // limiting the reuseInputConnection fix for Anroid 8.0.0 for now
672+ if (Build .VERSION .SDK_INT == Build .VERSION_CODES .O ) {
673+ return handleReuseInputConnection(outAttrs)
674+ }
675+
676+ return super .onCreateInputConnection(outAttrs)
677+ }
678+
679+ private fun handleReuseInputConnection (outAttrs : EditorInfo ) : InputConnection {
680+ // initialize inputConnectionEditorInfo
681+ if (inputConnectionEditorInfo == null ) {
682+ inputConnectionEditorInfo = outAttrs
683+ }
684+
685+ // now init the InputConnection, or replace if EditorInfo contains anything different
686+ if (inputConnectionRef?.get() == null || ! EditorInfoUtils .areEditorInfosTheSame(outAttrs, inputConnectionEditorInfo!! )) {
687+ // we have a new InputConnection to create, save the new EditorInfo data and create it
688+ // we make a copy of the parameters being received, because super.onCreateInputConnection may make changes
689+ // to EditorInfo params being sent to it, and we want to preserve the same data we received in order
690+ // to compare.
691+ // (see https://android.googlesource.com/platform/frameworks/base/+/jb-mr0-release/core/java/android/widget/
692+ // TextView.java#5404)
693+ inputConnectionEditorInfo = EditorInfoUtils .copyEditorInfo(outAttrs)
694+ val localInputConnection = super .onCreateInputConnection(outAttrs)
695+ if (localInputConnection == null ) {
696+ // in case super returns null, let's just observe the base implementation, no need to make
697+ // an InputConnectionWrapper of a null target
698+ return localInputConnection
699+ }
700+ // if non null, wrap the new InputConnection around our wrapper (used for logging purposes only)
701+ // inputConnection = AztecTextInputConnectionWrapper(localInputConnection, this)
702+ inputConnectionRef = WeakReference (localInputConnection)
703+ }
704+
705+ // return the existing inputConnection
706+ return inputConnectionRef?.get()!!
707+ }
708+
663709 override fun onRestoreInstanceState (state : Parcelable ? ) {
664710 disableTextChangedListener()
665711
0 commit comments