-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathSpaceInputHandler.kt
More file actions
317 lines (289 loc) · 16.3 KB
/
Copy pathSpaceInputHandler.kt
File metadata and controls
317 lines (289 loc) · 16.3 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package com.urik.keyboard.service
import com.urik.keyboard.KeyboardConstants.TextProcessingConstants
import com.urik.keyboard.settings.KeyboardSettings
import com.urik.keyboard.ui.keyboard.components.SwipeDetector
import com.urik.keyboard.utils.ErrorLogger
import com.urik.keyboard.utils.UrlEmailDetector
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
class SpaceInputHandler(
private val inputState: InputStateManager,
private val outputBridge: OutputBridge,
private val suggestionPipeline: SuggestionPipeline,
private val autoCorrectionEngine: AutoCorrectionEngine,
private val textInputProcessor: TextInputProcessor,
private val swipeSpaceManager: SwipeSpaceManager,
private val swipeDetector: SwipeDetector,
private val candidateBarController: CandidateBarController,
private val languageManager: LanguageManager,
private val serviceScope: CoroutineScope,
private val onGetCurrentSettings: () -> KeyboardSettings,
private val onCheckAutoCapitalization: (textBefore: String) -> Unit,
private val onJapaneseSpaceNextCandidate: () -> Unit = {}
) {
fun handle() {
serviceScope.launch {
try {
if (inputState.requiresDirectCommit) {
outputBridge.sendSpace()
return@launch
}
if (inputState.isSuggestionsDisabled) {
outputBridge.sendSpace()
return@launch
}
if (suggestionPipeline.isJapaneseLayout && inputState.displayBuffer.isNotEmpty()) {
onJapaneseSpaceNextCandidate()
return@launch
}
swipeSpaceManager.clearAutoSpaceFlag()
if (inputState.displayBuffer.isNotEmpty()) {
val actualTextBefore = outputBridge.safeGetTextBeforeCursor(1)
val actualTextAfter = outputBridge.safeGetTextAfterCursor(1)
if (actualTextBefore.isEmpty() && actualTextAfter.isEmpty()) {
inputState.clearInternalStateOnly()
outputBridge.finishComposingText()
}
}
val currentTime = System.currentTimeMillis()
val timeSinceLastSpace = currentTime - inputState.lastSpaceTime
if (handleDoubleSpacePeriod(timeSinceLastSpace)) return@launch
inputState.lastSpaceTime = currentTime
if (inputState.spellConfirmationState == SpellConfirmationState.AWAITING_CONFIRMATION) {
suggestionPipeline.confirmAndLearnWord(onCheckAutoCapitalization)
return@launch
}
if (inputState.displayBuffer.isNotEmpty() &&
onGetCurrentSettings().spellCheckEnabled &&
inputState.displayBuffer.length >= TextProcessingConstants.MIN_SPELL_CHECK_LENGTH
) {
val textBeforeForUrlCheck = outputBridge.safeGetTextBeforeCursor(100)
val isUrlOrEmail =
UrlEmailDetector.isUrlOrEmailContext(
currentWord = inputState.displayBuffer,
textBeforeCursor = textBeforeForUrlCheck,
nextChar = " "
)
if (!isUrlOrEmail) {
suggestionPipeline.cancelDebounceJob()
val decision = autoCorrectionEngine.decide(
buffer = inputState.displayBuffer,
spellCheckEnabled = onGetCurrentSettings().spellCheckEnabled,
autocorrectionEnabled = onGetCurrentSettings().autocorrectionEnabled,
pauseOnMisspelledWord = onGetCurrentSettings().pauseOnMisspelledWord,
lastAutocorrection = inputState.lastAutocorrection,
textBeforeCursor = textBeforeForUrlCheck,
nextChar = " "
)
when (decision) {
is AutocorrectDecision.None -> {
inputState.isActivelyEditing = true
suggestionPipeline.recordWordUsage(inputState.displayBuffer)
outputBridge.beginBatchEdit()
try {
applyPronounCorrectionIfNeeded()
swipeDetector.updateLastCommittedWord(inputState.displayBuffer)
outputBridge.finishComposingText()
outputBridge.commitText(" ", 1)
inputState.clearInternalStateOnly()
suggestionPipeline.showBigramPredictions()
val textBefore = outputBridge.safeGetTextBeforeCursor(50)
onCheckAutoCapitalization(textBefore)
} finally {
outputBridge.endBatchEdit()
}
return@launch
}
is AutocorrectDecision.ContractionBypass -> {
val suggestions = textInputProcessor.getSuggestions(inputState.displayBuffer)
val displaySuggestions =
suggestionPipeline.storeAndCapitalizeSuggestions(
suggestions,
inputState.isCurrentWordAtSentenceStart
)
val originalWord = inputState.displayBuffer
inputState.isActivelyEditing = true
suggestionPipeline.learnWordAndInvalidateCache(originalWord, InputMethod.TYPED)
outputBridge.beginBatchEdit()
try {
applyPronounCorrectionIfNeeded()
swipeDetector.updateLastCommittedWord(originalWord)
outputBridge.finishComposingText()
outputBridge.commitText(" ", 1)
inputState.clearInternalStateOnly()
if (displaySuggestions.isNotEmpty()) {
inputState.postCommitReplacementState =
PostCommitReplacementState(
originalWord = originalWord,
committedWord = originalWord
)
inputState.pendingSuggestions = displaySuggestions
candidateBarController.updateSuggestions(displaySuggestions)
} else {
suggestionPipeline.showBigramPredictions()
}
val textBefore = outputBridge.safeGetTextBeforeCursor(50)
onCheckAutoCapitalization(textBefore)
} finally {
outputBridge.endBatchEdit()
}
return@launch
}
is AutocorrectDecision.Pause -> {
val suggestions = textInputProcessor.getSuggestions(inputState.displayBuffer)
val displaySuggestions =
suggestionPipeline.storeAndCapitalizeSuggestions(
suggestions,
inputState.isCurrentWordAtSentenceStart
)
inputState.spellConfirmationState = SpellConfirmationState.AWAITING_CONFIRMATION
inputState.pendingWordForLearning = inputState.displayBuffer
outputBridge.highlightCurrentWord()
inputState.pendingSuggestions = displaySuggestions
if (displaySuggestions.isNotEmpty()) {
candidateBarController.updateSuggestions(displaySuggestions)
} else {
candidateBarController.clearSuggestions()
}
return@launch
}
is AutocorrectDecision.Correct -> {
val originalWord = inputState.displayBuffer
val rawCorrected = decision.suggestion
val correctedWord = if (inputState.isCurrentWordAtSentenceStart) {
rawCorrected.replaceFirstChar { it.uppercaseChar() }
} else {
rawCorrected
}
inputState.isActivelyEditing = true
suggestionPipeline.recordWordUsage(correctedWord)
outputBridge.beginBatchEdit()
try {
applyPronounCorrectionIfNeeded()
outputBridge.commitText("$correctedWord ", 1)
swipeDetector.updateLastCommittedWord(correctedWord)
inputState.clearInternalStateOnly()
inputState.postCommitReplacementState =
PostCommitReplacementState(
originalWord = originalWord,
committedWord = correctedWord
)
inputState.lastAutocorrection =
LastAutocorrection(
originalTypedWord = originalWord,
correctedWord = correctedWord
)
inputState.pendingSuggestions = listOf(originalWord)
candidateBarController.updateSuggestions(inputState.pendingSuggestions)
val textBefore = outputBridge.safeGetTextBeforeCursor(50)
onCheckAutoCapitalization(textBefore)
} finally {
outputBridge.endBatchEdit()
}
return@launch
}
is AutocorrectDecision.Suggestions -> {
val suggestions = textInputProcessor.getSuggestions(inputState.displayBuffer)
val displaySuggestions =
suggestionPipeline.storeAndCapitalizeSuggestions(
suggestions,
inputState.isCurrentWordAtSentenceStart
)
val originalWord = inputState.displayBuffer
inputState.isActivelyEditing = true
suggestionPipeline.learnWordAndInvalidateCache(originalWord, InputMethod.TYPED)
outputBridge.beginBatchEdit()
try {
applyPronounCorrectionIfNeeded()
swipeDetector.updateLastCommittedWord(originalWord)
outputBridge.finishComposingText()
outputBridge.commitText(" ", 1)
inputState.clearInternalStateOnly()
if (displaySuggestions.isNotEmpty()) {
inputState.postCommitReplacementState =
PostCommitReplacementState(
originalWord = originalWord,
committedWord = originalWord
)
inputState.pendingSuggestions = displaySuggestions
candidateBarController.updateSuggestions(displaySuggestions)
} else {
suggestionPipeline.showBigramPredictions()
}
val textBefore = outputBridge.safeGetTextBeforeCursor(50)
onCheckAutoCapitalization(textBefore)
} finally {
outputBridge.endBatchEdit()
}
return@launch
}
}
}
}
outputBridge.beginBatchEdit()
try {
applyPronounCorrectionIfNeeded()
if (inputState.displayBuffer.isNotEmpty()) {
swipeDetector.updateLastCommittedWord(
inputState.displayBuffer
)
}
outputBridge.finishComposingText()
outputBridge.commitText(" ", 1)
inputState.clearInternalStateOnly()
suggestionPipeline.showBigramPredictions()
val textBefore = outputBridge.safeGetTextBeforeCursor(50)
onCheckAutoCapitalization(textBefore)
} finally {
outputBridge.endBatchEdit()
}
} catch (e: Exception) {
ErrorLogger.logException(
component = "UrikInputMethodService",
severity = ErrorLogger.Severity.HIGH,
exception = e,
context = mapOf("operation" to "handleSpace")
)
outputBridge.finishComposingText()
outputBridge.commitText(" ", 1)
inputState.clearInternalStateOnly()
}
}
}
private fun handleDoubleSpacePeriod(timeSinceLastSpace: Long): Boolean {
if (timeSinceLastSpace > DOUBLE_TAP_SPACE_THRESHOLD_MS ||
inputState.spellConfirmationState != SpellConfirmationState.NORMAL ||
!onGetCurrentSettings().doubleSpacePeriod
) {
return false
}
outputBridge.beginBatchEdit()
try {
if (inputState.wordState.hasContent && !inputState.requiresDirectCommit) {
inputState.clearInternalStateOnly()
outputBridge.finishComposingText()
}
outputBridge.deleteSurroundingText(1, 0)
outputBridge.commitText(". ", 1)
val textBefore = outputBridge.safeGetTextBeforeCursor(50)
onCheckAutoCapitalization(textBefore)
} finally {
outputBridge.endBatchEdit()
}
inputState.lastSpaceTime = 0
return true
}
private fun applyPronounCorrectionIfNeeded() {
val pronounLang = languageManager.currentLanguage.value.split("-").first()
if (pronounLang == "en" && inputState.displayBuffer.isNotEmpty()) {
val corrected = EnglishPronounCorrection.capitalize(inputState.displayBuffer.lowercase())
if (corrected != null && corrected != inputState.displayBuffer) {
inputState.onPronounCapitalized(corrected)
outputBridge.setComposingText(corrected, 1)
}
}
}
private companion object {
const val DOUBLE_TAP_SPACE_THRESHOLD_MS = 250L
}
}