From cb250e64de5a6e90a0cef743037970bfa3618eba Mon Sep 17 00:00:00 2001 From: Nehemiah of Zebulun Date: Wed, 30 Aug 2023 15:57:54 +0200 Subject: [PATCH] Progrss --- app/src/main/java/net/mezimmah/wkt9/WKT9.kt | 84 ++++++++++++++++--- .../mezimmah/wkt9/keypad/KeyEventResult.kt | 1 + 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/net/mezimmah/wkt9/WKT9.kt b/app/src/main/java/net/mezimmah/wkt9/WKT9.kt index 22ff629..aa56298 100644 --- a/app/src/main/java/net/mezimmah/wkt9/WKT9.kt +++ b/app/src/main/java/net/mezimmah/wkt9/WKT9.kt @@ -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 = 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?) { + TODO("Not yet implemented") + } + + override fun onGetSentenceSuggestions(suggestionsInfo: Array?) { + 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) diff --git a/app/src/main/java/net/mezimmah/wkt9/keypad/KeyEventResult.kt b/app/src/main/java/net/mezimmah/wkt9/keypad/KeyEventResult.kt index 9348c99..79ba026 100644 --- a/app/src/main/java/net/mezimmah/wkt9/keypad/KeyEventResult.kt +++ b/app/src/main/java/net/mezimmah/wkt9/keypad/KeyEventResult.kt @@ -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? = null, val commit: String? = null,