This commit is contained in:
Nehemiah of Zebulun 2023-08-30 15:57:54 +02:00
parent 113a3b994d
commit cb250e64de
2 changed files with 73 additions and 12 deletions

View File

@ -11,6 +11,11 @@ import android.view.View
import android.view.ViewConfiguration import android.view.ViewConfiguration
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.view.textservice.SentenceSuggestionsInfo
import android.view.textservice.SpellCheckerSession
import android.view.textservice.SuggestionsInfo
import android.view.textservice.TextInfo
import android.view.textservice.TextServicesManager
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import android.widget.Toast import android.widget.Toast
@ -38,8 +43,13 @@ import net.mezimmah.wkt9.voice.Whisper
import okio.IOException import okio.IOException
import java.io.File import java.io.File
import java.lang.StringBuilder import java.lang.StringBuilder
import java.util.Locale
class WKT9: InputMethodService() { //val info = arrayOf(TextInfo("banan#", 0, 6, 0, 0))
//
//spellCheckerSession?.getSentenceSuggestions(info, 10)
class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListener {
private val tag = "WKT9" private val tag = "WKT9"
// Dao - Database // Dao - Database
@ -76,6 +86,13 @@ class WKT9: InputMethodService() {
private var candidateIndex = 0 private var candidateIndex = 0
private var inputStatus: Status = Status.CAP private var inputStatus: Status = Status.CAP
private var timeout: Int? = null private var timeout: Int? = null
private var lastComposedString: String? = null
private val commitHistory: MutableList<String> = mutableListOf()
private var trackCommits = false
// Spell checker
private var locale: Locale? = null
private var spellCheckerSession: SpellCheckerSession? = null
// UI // UI
private lateinit var inputView: View private lateinit var inputView: View
@ -98,6 +115,9 @@ class WKT9: InputMethodService() {
numericInputMode = NumericInputMode() numericInputMode = NumericInputMode()
wordInputMode = WordInputMode() wordInputMode = WordInputMode()
longPressTimeout = ViewConfiguration.getLongPressTimeout() longPressTimeout = ViewConfiguration.getLongPressTimeout()
lastComposedString = null
commitHistory.clear()
trackCommits = false
t9.initializeWords(languageTag) t9.initializeWords(languageTag)
@ -114,9 +134,16 @@ class WKT9: InputMethodService() {
override fun onFinishInput() { override fun onFinishInput() {
super.onFinishInput() super.onFinishInput()
clearCandidates()
spellCheckerSession?.cancel()
spellCheckerSession?.close()
inputMode = null inputMode = null
cursorPosition = 0 cursorPosition = 0
inputStatus = Status.CAP inputStatus = Status.CAP
spellCheckerSession = null
locale = null
} }
override fun onFinishInputView(finishingInput: Boolean) { override fun onFinishInputView(finishingInput: Boolean) {
@ -168,8 +195,13 @@ class WKT9: InputMethodService() {
override fun onStartInput(attribute: EditorInfo?, restarting: Boolean) { override fun onStartInput(attribute: EditorInfo?, restarting: Boolean) {
val inputType = attribute?.inputType?.and(InputType.TYPE_MASK_CLASS) ?: 0 val inputType = attribute?.inputType?.and(InputType.TYPE_MASK_CLASS) ?: 0
val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
val inputMethodSubtype = inputMethodManager.currentInputMethodSubtype
val textServiceManager = getSystemService(TEXT_SERVICES_MANAGER_SERVICE) as TextServicesManager
cursorPosition = attribute?.initialSelEnd ?: 0 cursorPosition = attribute?.initialSelEnd ?: 0
locale = Locale.forLanguageTag(inputMethodSubtype.languageTag)
spellCheckerSession = textServiceManager.newSpellCheckerSession(null, locale, this, false)
when (inputType) { when (inputType) {
InputType.TYPE_CLASS_DATETIME, InputType.TYPE_CLASS_DATETIME,
@ -206,6 +238,22 @@ class WKT9: InputMethodService() {
) )
} }
override fun onGetSuggestions(p0: Array<out SuggestionsInfo>?) {
TODO("Not yet implemented")
}
override fun onGetSentenceSuggestions(suggestionsInfo: Array<out SentenceSuggestionsInfo>?) {
suggestionsInfo?.map {
val suggestions = it.getSuggestionsInfoAt(0)
for (index in 0 until suggestions.suggestionsCount) {
val suggestion = suggestions.getSuggestionAt(index)
Log.d(tag, "Suggestion: $suggestion")
}
}
}
private fun candidatesToLowerCase() { private fun candidatesToLowerCase() {
candidates.forEachIndexed { index, candidate -> candidates.forEachIndexed { index, candidate ->
candidates[index] = candidate.lowercase() candidates[index] = candidate.lowercase()
@ -244,6 +292,8 @@ class WKT9: InputMethodService() {
private fun composeText(text: CharSequence, cursorPosition: Int = 1): Boolean { private fun composeText(text: CharSequence, cursorPosition: Int = 1): Boolean {
if (!composing) return false if (!composing) return false
lastComposedString = text.toString()
return currentInputConnection?.setComposingText(text, cursorPosition) ?: false return currentInputConnection?.setComposingText(text, cursorPosition) ?: false
} }
@ -268,6 +318,10 @@ class WKT9: InputMethodService() {
return if (composing) { return if (composing) {
composing = false composing = false
val lastComposed = lastComposedString
if (trackCommits && !lastComposed.isNullOrEmpty()) commitHistory.add(lastComposed)
updateInputStatus() updateInputStatus()
currentInputConnection?.finishComposingText() ?: false currentInputConnection?.finishComposingText() ?: false
@ -276,18 +330,21 @@ class WKT9: InputMethodService() {
@SuppressLint("DiscouragedApi") @SuppressLint("DiscouragedApi")
private fun getIconResource(): Int { private fun getIconResource(): Int {
val name = inputMode?.let { val mode = inputMode?.mode
val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager val language = locale?.language
val inputMethodSubtype = inputMethodManager.currentInputMethodSubtype val country = locale?.country
it.mode if (mode == null ||language == null || country == null) {
.plus("_") return resources.getIdentifier("wkt9", "drawable", packageName)
.plus(inputMethodSubtype.languageTag) }
.plus("_")
.plus(inputStatus.toString()) val name = mode.plus("_")
.replace("-", "_") .plus(language)
.lowercase() .plus("_")
} ?: "wkt9" .plus(country)
.plus("_")
.plus(inputStatus.toString())
.lowercase()
return resources.getIdentifier(name, "drawable", packageName) return resources.getIdentifier(name, "drawable", packageName)
} }
@ -318,6 +375,9 @@ class WKT9: InputMethodService() {
private fun handleKeyEventResult(res: KeyEventResult): Boolean { private fun handleKeyEventResult(res: KeyEventResult): Boolean {
if (res.finishComposing) finishComposingText() if (res.finishComposing) finishComposingText()
if (res.startComposing) markComposingRegion() if (res.startComposing) markComposingRegion()
trackCommits = res.trackCommits
if (!res.codeWord.isNullOrEmpty()) onCodeWordUpdate(res.codeWord, res.timeout) if (!res.codeWord.isNullOrEmpty()) onCodeWordUpdate(res.codeWord, res.timeout)
if (!res.candidates.isNullOrEmpty()) onCandidates(res.candidates, res.timeout) if (!res.candidates.isNullOrEmpty()) onCandidates(res.candidates, res.timeout)
if (!res.commit.isNullOrEmpty()) onCommit(res.commit) if (!res.commit.isNullOrEmpty()) onCommit(res.commit)

View File

@ -7,6 +7,7 @@ data class KeyEventResult(
val consumed: Boolean = true, val consumed: Boolean = true,
val finishComposing: Boolean = false, val finishComposing: Boolean = false,
val startComposing: Boolean = false, val startComposing: Boolean = false,
val trackCommits: Boolean = false,
val codeWord: StringBuilder? = null, val codeWord: StringBuilder? = null,
val candidates: List<String>? = null, val candidates: List<String>? = null,
val commit: String? = null, val commit: String? = null,