Skip to content

Commit 0ca13ce

Browse files
committed
added code to keep instance of InputConnection as long as parameters passed remain the same per each call
1 parent 929eaf4 commit 0ca13ce

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import android.view.MotionEvent
4848
import android.view.View
4949
import android.view.WindowManager
5050
import android.view.inputmethod.BaseInputConnection
51+
import android.view.inputmethod.EditorInfo
52+
import android.view.inputmethod.InputConnection
5153
import android.widget.CheckBox
5254
import android.widget.EditText
5355
import android.widget.Toast
@@ -70,6 +72,7 @@ import org.wordpress.aztec.handlers.ListHandler
7072
import org.wordpress.aztec.handlers.ListItemHandler
7173
import org.wordpress.aztec.handlers.PreformatHandler
7274
import org.wordpress.aztec.handlers.QuoteHandler
75+
import org.wordpress.aztec.ime.EditorInfoUtils
7376
import org.wordpress.aztec.plugins.IAztecPlugin
7477
import org.wordpress.aztec.plugins.IToolbarButton
7578
import org.wordpress.aztec.source.Format
@@ -291,6 +294,9 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
291294

292295
private var focusOnVisible = true
293296

297+
var inputConnection: InputConnection? = null
298+
var inputConnectionEditorInfo: EditorInfo? = null
299+
294300
interface OnSelectionChangedListener {
295301
fun onSelectionChanged(selStart: Int, selEnd: Int)
296302
}
@@ -660,6 +666,36 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
660666
}
661667
}
662668

669+
override fun onCreateInputConnection(outAttrs: EditorInfo) : InputConnection {
670+
// initialize inputConnectionEditorInfo
671+
if (inputConnectionEditorInfo == null) {
672+
inputConnectionEditorInfo = outAttrs
673+
}
674+
675+
// now init the InputConnection, or replace if EditorInfo contains anything different
676+
if (inputConnection == null || !EditorInfoUtils.areEditorInfosTheSame(outAttrs, inputConnectionEditorInfo!!)) {
677+
// we have a new InputConnection to create, save the new EditorInfo data and create it
678+
// we make a copy of the parameters being received, because super.onCreateInputConnection may make changes
679+
// to EditorInfo params being sent to it, and we want to preserve the same data we received in order
680+
// to compare.
681+
// (see https://android.googlesource.com/platform/frameworks/base/+/jb-mr0-release/core/java/android/widget/
682+
// TextView.java#5404)
683+
inputConnectionEditorInfo = EditorInfoUtils.copyEditorInfo(outAttrs)
684+
val localInputConnection = super.onCreateInputConnection(outAttrs)
685+
if (localInputConnection == null) {
686+
// in case super returns null, let's just observe the base implementation, no need to make
687+
// an InputConnectionWrapper of a null target
688+
return localInputConnection
689+
}
690+
// if non null, wrap the new InputConnection around our wrapper
691+
//inputConnection = AztecTextInputConnectionWrapper(localInputConnection, this)
692+
inputConnection = localInputConnection
693+
}
694+
695+
// returnn the existing inputConnection
696+
return inputConnection!!
697+
}
698+
663699
override fun onRestoreInstanceState(state: Parcelable?) {
664700
disableTextChangedListener()
665701

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.wordpress.aztec.ime
2+
3+
import android.os.Build
4+
import android.view.inputmethod.EditorInfo
5+
import java.util.Arrays
6+
7+
object EditorInfoUtils {
8+
@JvmStatic
9+
fun areEditorInfosTheSame(ed1: EditorInfo, ed2: EditorInfo): Boolean {
10+
if (ed1 == ed2) {
11+
return true
12+
}
13+
14+
if (ed1.actionId == ed2.actionId
15+
&& (ed1.actionLabel != null && ed1.actionLabel.equals(ed2.actionLabel) || ed1.actionLabel == null && ed2.actionLabel == null)
16+
&& ed1.inputType == ed2.inputType
17+
&& ed1.imeOptions == ed2.imeOptions
18+
&& (ed1.privateImeOptions != null && ed1.privateImeOptions.equals(ed2.privateImeOptions) || ed1.privateImeOptions == null && ed2.privateImeOptions == null)
19+
&& ed1.initialSelStart == ed2.initialSelStart
20+
&& ed1.initialSelEnd == ed2.initialSelEnd
21+
&& ed1.initialCapsMode == ed2.initialCapsMode
22+
&& ed1.fieldId == ed2.fieldId
23+
) {
24+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
25+
// specific comparisons here
26+
if (ed1.contentMimeTypes != null && ed2.contentMimeTypes != null) {
27+
return Arrays.equals(ed1.contentMimeTypes, ed2.contentMimeTypes)
28+
}
29+
}
30+
return true
31+
}
32+
return false
33+
}
34+
35+
@JvmStatic
36+
fun copyEditorInfo(ed1: EditorInfo) : EditorInfo {
37+
val copy = EditorInfo()
38+
copy.actionId = ed1.actionId
39+
copy.actionLabel = ed1.actionLabel?.toString()
40+
copy.inputType = ed1.inputType
41+
copy.imeOptions = ed1.imeOptions
42+
copy.privateImeOptions = ed1.privateImeOptions?.toString()
43+
copy.initialSelStart = ed1.initialSelStart
44+
copy.initialSelEnd = ed1.initialSelEnd
45+
copy.initialCapsMode = ed1.initialCapsMode
46+
copy.fieldId = ed1.fieldId
47+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
48+
// specific comparisons here
49+
if (ed1.contentMimeTypes != null) {
50+
copy.contentMimeTypes = Arrays.copyOf(ed1.contentMimeTypes, ed1.contentMimeTypes.size)
51+
}
52+
}
53+
return copy
54+
}
55+
}

0 commit comments

Comments
 (0)