-
-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathOtherInputConnection.kt
More file actions
197 lines (177 loc) · 6.78 KB
/
OtherInputConnection.kt
File metadata and controls
197 lines (177 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.simplemobiletools.keyboard.helpers;
import android.os.Bundle
import android.text.Editable
import android.text.Spanned
import android.text.style.SuggestionSpan
import android.util.Log
import android.view.inputmethod.BaseInputConnection
import android.view.inputmethod.CompletionInfo
import android.view.inputmethod.CorrectionInfo
import android.view.inputmethod.ExtractedText
import android.view.inputmethod.ExtractedTextRequest
import android.widget.SearchView
import android.widget.TextView
/**
* Source: https://stackoverflow.com/a/39460124
*/
class OtherInputConnection(private val mTextView: androidx.appcompat.widget.AppCompatAutoCompleteTextView?) : BaseInputConnection(
mTextView!!, true
) {
// Keeps track of nested begin/end batch edit to ensure this connection always has a
// balanced impact on its associated TextView.
// A negative value means that this connection has been finished by the InputMethodManager.
private var mBatchEditNesting = 0
override fun getEditable(): Editable? {
val tv = mTextView
Log.i("heregotEditTEZT", tv!!.text.toString())
return tv?.editableText
}
override fun beginBatchEdit(): Boolean {
synchronized(this) {
if (mBatchEditNesting >= 0) {
mTextView!!.beginBatchEdit()
mBatchEditNesting++
return true
}
}
return false
}
override fun endBatchEdit(): Boolean {
synchronized(this) {
if (mBatchEditNesting > 0) {
// When the connection is reset by the InputMethodManager and reportFinish
// is called, some endBatchEdit calls may still be asynchronously received from the
// IME. Do not take these into account, thus ensuring that this IC's final
// contribution to mTextView's nested batch edit count is zero.
mTextView!!.endBatchEdit()
mBatchEditNesting--
return true
}
}
return false
}
//clear the meta key states means shift, alt, ctrl
override fun clearMetaKeyStates(states: Int): Boolean {
val content = editable ?: return false
val kl = mTextView!!.keyListener //listen keyevents like a, enter, space
if (kl != null) {
try {
kl.clearMetaKeyState(mTextView, content, states)
} catch (e: AbstractMethodError) {
// This is an old listener that doesn't implement the
// new method.
}
}
return true
}
//When a user selects a suggestion from an autocomplete or suggestion list, the input method may call commitCompletion
override fun commitCompletion(text: CompletionInfo): Boolean {
if (DEBUG) Log.v(
TAG,
"commitCompletion $text"
)
mTextView!!.beginBatchEdit()
mTextView.onCommitCompletion(text)
mTextView.endBatchEdit()
return true
}
/**
which is used to commit a correction to a previously entered text.
This correction could be suggested by the input method or obtained through some other means.
*/
override fun commitCorrection(correctionInfo: CorrectionInfo): Boolean {
if (DEBUG) Log.v(
TAG,
"commitCorrection$correctionInfo"
)
mTextView!!.beginBatchEdit()
mTextView.onCommitCorrection(correctionInfo)
mTextView.endBatchEdit()
return true
}
/* It's used to simulate the action associated with an editor action, typically triggered by pressing the "Done" or "Enter" key on the keyboard.*/
override fun performEditorAction(actionCode: Int): Boolean {
if (DEBUG) Log.v(
TAG,
"performEditorAction $actionCode"
)
mTextView!!.onEditorAction(actionCode)
return true
}
/*
handle actions triggered from the context menu associated with the search text.
This menu typically appears when you long-press on the search text field.
*/
override fun performContextMenuAction(id: Int): Boolean {
if (DEBUG) Log.v(
TAG,
"performContextMenuAction $id"
)
mTextView!!.beginBatchEdit()
mTextView.onTextContextMenuItem(id)
mTextView.endBatchEdit()
return true
}
/*It is used to retrieve information about the currently extracted text
* eg- selected text, the start and end offsets, the total number of characters, and more.*/
override fun getExtractedText(request: ExtractedTextRequest, flags: Int): ExtractedText? {
if (mTextView != null) {
val et = ExtractedText()
if (mTextView.extractText(request, et)) {
if (flags and GET_EXTRACTED_TEXT_MONITOR != 0) {
// mTextView.setExtracting(request);
}
return et
}
}
return null
}
// API to send private commands from an input method to its connected editor. This can be used to provide domain-specific features
override fun performPrivateCommand(action: String, data: Bundle): Boolean {
mTextView!!.onPrivateIMECommand(action, data)
return true
}
//send the text to the connected editor from the keyboard pressed
override fun commitText(
text: CharSequence,
newCursorPosition: Int
): Boolean {
if (mTextView == null) {
return super.commitText(text, newCursorPosition)
}
if (text is Spanned) {
val spans = text.getSpans(
0, text.length,
SuggestionSpan::class.java
)
// mIMM.registerSuggestionSpansForNotification(spans);
}
// mTextView.resetErrorChangedFlag();
// mTextView.hideErrorIfUnchanged();
return super.commitText(text, newCursorPosition)
}
override fun requestCursorUpdates(cursorUpdateMode: Int): Boolean {
if (DEBUG) Log.v(
TAG,
"requestUpdateCursorAnchorInfo $cursorUpdateMode"
)
// It is possible that any other bit is used as a valid flag in a future release.
// We should reject the entire request in such a case.
val KNOWN_FLAGS_MASK = CURSOR_UPDATE_IMMEDIATE or CURSOR_UPDATE_MONITOR
val unknownFlags = cursorUpdateMode and KNOWN_FLAGS_MASK.inv()
if (unknownFlags != 0) {
if (DEBUG) {
Log.d(
TAG,
"Rejecting requestUpdateCursorAnchorInfo due to unknown flags. cursorUpdateMode=$cursorUpdateMode unknownFlags=$unknownFlags"
)
}
return false
}
return false
}
companion object {
private const val DEBUG = false
private val TAG = "loool"
}
}