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.inputmethod.EditorInfo
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.TextView
import android.widget.Toast
@ -38,8 +43,13 @@ import net.mezimmah.wkt9.voice.Whisper
import okio.IOException
import java.io.File
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"
// Dao - Database
@ -76,6 +86,13 @@ class WKT9: InputMethodService() {
private var candidateIndex = 0
private var inputStatus: Status = Status.CAP
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
private lateinit var inputView: View
@ -98,6 +115,9 @@ class WKT9: InputMethodService() {
numericInputMode = NumericInputMode()
wordInputMode = WordInputMode()
longPressTimeout = ViewConfiguration.getLongPressTimeout()
lastComposedString = null
commitHistory.clear()
trackCommits = false
t9.initializeWords(languageTag)
@ -114,9 +134,16 @@ class WKT9: InputMethodService() {
override fun onFinishInput() {
super.onFinishInput()
clearCandidates()
spellCheckerSession?.cancel()
spellCheckerSession?.close()
inputMode = null
cursorPosition = 0
inputStatus = Status.CAP
spellCheckerSession = null
locale = null
}
override fun onFinishInputView(finishingInput: Boolean) {
@ -168,8 +195,13 @@ class WKT9: InputMethodService() {
override fun onStartInput(attribute: EditorInfo?, restarting: Boolean) {
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
locale = Locale.forLanguageTag(inputMethodSubtype.languageTag)
spellCheckerSession = textServiceManager.newSpellCheckerSession(null, locale, this, false)
when (inputType) {
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() {
candidates.forEachIndexed { index, candidate ->
candidates[index] = candidate.lowercase()
@ -244,6 +292,8 @@ class WKT9: InputMethodService() {
private fun composeText(text: CharSequence, cursorPosition: Int = 1): Boolean {
if (!composing) return false
lastComposedString = text.toString()
return currentInputConnection?.setComposingText(text, cursorPosition) ?: false
}
@ -268,6 +318,10 @@ class WKT9: InputMethodService() {
return if (composing) {
composing = false
val lastComposed = lastComposedString
if (trackCommits && !lastComposed.isNullOrEmpty()) commitHistory.add(lastComposed)
updateInputStatus()
currentInputConnection?.finishComposingText() ?: false
@ -276,18 +330,21 @@ class WKT9: InputMethodService() {
@SuppressLint("DiscouragedApi")
private fun getIconResource(): Int {
val name = inputMode?.let {
val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
val inputMethodSubtype = inputMethodManager.currentInputMethodSubtype
val mode = inputMode?.mode
val language = locale?.language
val country = locale?.country
it.mode
.plus("_")
.plus(inputMethodSubtype.languageTag)
.plus("_")
.plus(inputStatus.toString())
.replace("-", "_")
.lowercase()
} ?: "wkt9"
if (mode == null ||language == null || country == null) {
return resources.getIdentifier("wkt9", "drawable", packageName)
}
val name = mode.plus("_")
.plus(language)
.plus("_")
.plus(country)
.plus("_")
.plus(inputStatus.toString())
.lowercase()
return resources.getIdentifier(name, "drawable", packageName)
}
@ -318,6 +375,9 @@ class WKT9: InputMethodService() {
private fun handleKeyEventResult(res: KeyEventResult): Boolean {
if (res.finishComposing) finishComposingText()
if (res.startComposing) markComposingRegion()
trackCommits = res.trackCommits
if (!res.codeWord.isNullOrEmpty()) onCodeWordUpdate(res.codeWord, res.timeout)
if (!res.candidates.isNullOrEmpty()) onCandidates(res.candidates, res.timeout)
if (!res.commit.isNullOrEmpty()) onCommit(res.commit)

View File

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