This commit is contained in:
Nehemiah of Zebulun 2023-09-11 14:35:19 +02:00
parent c89d4f0204
commit d1bf1c44a1
2 changed files with 61 additions and 27 deletions

View File

@ -17,6 +17,8 @@ import android.view.inputmethod.InputMethodManager
import android.view.textservice.SentenceSuggestionsInfo import android.view.textservice.SentenceSuggestionsInfo
import android.view.textservice.SpellCheckerSession import android.view.textservice.SpellCheckerSession
import android.view.textservice.SuggestionsInfo 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
@ -47,11 +49,6 @@ import okio.IOException
import java.io.File import java.io.File
import java.util.Locale import java.util.Locale
//val info = arrayOf(TextInfo("banan#", 0, 6, 0, 0))
//
//spellCheckerSession?.getSentenceSuggestions(info, 10)
class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListener { class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListener {
private val tag = "WKT9" private val tag = "WKT9"
@ -210,10 +207,8 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
val inputClass = inputType?.and(InputType.TYPE_MASK_CLASS) ?: 0 val inputClass = inputType?.and(InputType.TYPE_MASK_CLASS) ?: 0
val typeVariation = inputType?.and(InputType.TYPE_MASK_VARIATION) ?: 0 val typeVariation = inputType?.and(InputType.TYPE_MASK_VARIATION) ?: 0
val typeFlags = inputType?.and(InputType.TYPE_MASK_FLAGS) ?: 0 val typeFlags = inputType?.and(InputType.TYPE_MASK_FLAGS) ?: 0
// val textServiceManager = getSystemService(TEXT_SERVICES_MANAGER_SERVICE) as TextServicesManager
cursorPosition = attribute?.initialSelEnd ?: 0 cursorPosition = attribute?.initialSelEnd ?: 0
// spellCheckerSession = textServiceManager.newSpellCheckerSession(null, locale, this, false)
val forceNumeric = resources.getStringArray(R.array.input_mode_numeric) val forceNumeric = resources.getStringArray(R.array.input_mode_numeric)
@ -259,14 +254,18 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
} }
override fun onGetSentenceSuggestions(suggestionsInfo: Array<out SentenceSuggestionsInfo>?) { override fun onGetSentenceSuggestions(suggestionsInfo: Array<out SentenceSuggestionsInfo>?) {
clearCandidates()
suggestionsInfo?.map { suggestionsInfo?.map {
val suggestions = it.getSuggestionsInfoAt(0) val suggestions = it.getSuggestionsInfoAt(0)
for (index in 0 until suggestions.suggestionsCount) { for (index in 0 until suggestions.suggestionsCount) {
val suggestion = suggestions.getSuggestionAt(index) val suggestion = suggestions.getSuggestionAt(index)
Log.d(tag, "Suggestion: $suggestion") candidates.add(suggestion)
} }
if (candidates.isNotEmpty()) loadCandidates()
} }
} }
@ -344,28 +343,41 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
InputType.TYPE_TEXT_VARIATION_PERSON_NAME InputType.TYPE_TEXT_VARIATION_PERSON_NAME
) )
if (letterVariations.contains(variation)) { val mode: WKT9InputMode = if (letterVariations.contains(variation)) {
allowSuggestions = false allowSuggestions = false
enableInputMode(WKT9InputMode.ALPHA) WKT9InputMode.ALPHA
} else if (lastInputMode == WKT9InputMode.ALPHA) { } else if (lastInputMode == WKT9InputMode.ALPHA) {
allowSuggestions = flags != InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS allowSuggestions = flags != InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
enableInputMode(WKT9InputMode.ALPHA) WKT9InputMode.ALPHA
} else enableInputMode(WKT9InputMode.WORD) } else {
allowSuggestions = flags != InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
WKT9InputMode.WORD
}
spellCheckerSession = if (allowSuggestions) {
val textServiceManager = getSystemService(TEXT_SERVICES_MANAGER_SERVICE) as TextServicesManager
textServiceManager.newSpellCheckerSession(null, locale, this, false)
} else {
spellCheckerSession?.apply {
cancel()
close()
}
null
}
enableInputMode(mode)
} }
private fun finishComposingText(): Boolean { private fun finishComposingText(): Boolean {
return if (composing) { return if (composing) {
composing = false composing = false
lastComposedString?.let { if (allowSuggestions && inputMode is AlphaInputMode) handleSuggestions()
commitHistory.add(it)
lastComposedString = null
}
if (allowSuggestions) Log.d(tag, "History: $commitHistory")
updateInputStatus() updateInputStatus()
@ -373,6 +385,28 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
} else false } else false
} }
private fun handleSuggestions() {
val lastComposed = lastComposedString ?: return
val lastChar = lastComposed.lowercase().last()
val code = keypad.codeForLetter(lastChar)
if (lastComposed.length != 1 || code == null) {
commitHistory.clear()
return
}
commitHistory.add(lastComposed)
loadSuggestions(commitHistory.joinToString(""))
}
private fun loadSuggestions(word: String) {
val info = arrayOf(TextInfo(word.plus("#"), 0, word.length + 1, 0, 0))
spellCheckerSession?.getSentenceSuggestions(info, 10)
}
@SuppressLint("DiscouragedApi") @SuppressLint("DiscouragedApi")
private fun getIconResource(): Int { private fun getIconResource(): Int {
val mode = inputMode?.mode ?: return resources.getIdentifier("wkt9", "drawable", packageName) val mode = inputMode?.mode ?: return resources.getIdentifier("wkt9", "drawable", packageName)
@ -447,11 +481,11 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
return false return false
} }
private fun loadCandidates(highLight: Int? = null) { private fun loadCandidates() {
val candidatesView = inputView?.findViewById<LinearLayout>(R.id.suggestions) ?: return val candidatesView = inputView?.findViewById<LinearLayout>(R.id.suggestions) ?: return
candidates.forEachIndexed { index, candidate -> candidates.forEachIndexed { index, candidate ->
val layout = if (index == highLight) R.layout.current_suggestion else R.layout.suggestion val layout = if (index == candidateIndex) R.layout.current_suggestion else R.layout.suggestion
val candidateView = layoutInflater.inflate(layout, null) val candidateView = layoutInflater.inflate(layout, null)
val textView = candidateView.findViewById<TextView>(R.id.suggestion_text) val textView = candidateView.findViewById<TextView>(R.id.suggestion_text)
@ -486,7 +520,7 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
this.candidates.add(candidate) this.candidates.add(candidate)
} }
loadCandidates(candidateIndex) loadCandidates()
composeText(this.candidates[candidateIndex]) composeText(this.candidates[candidateIndex])
handleComposeTimeout(timeout) handleComposeTimeout(timeout)
} }
@ -500,7 +534,7 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
if (!hasCandidates) return@launch if (!hasCandidates) return@launch
loadCandidates(candidateIndex) loadCandidates()
composeText(candidates[candidateIndex], 1) composeText(candidates[candidateIndex], 1)
handleComposeTimeout(timeout) handleComposeTimeout(timeout)
} }
@ -582,7 +616,7 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
if (candidateIndex < 0) candidateIndex = candidates.count() - 1 if (candidateIndex < 0) candidateIndex = candidates.count() - 1
clearCandidateUI() clearCandidateUI()
loadCandidates(candidateIndex) loadCandidates()
composeText(candidates[candidateIndex]) composeText(candidates[candidateIndex])
handleComposeTimeout(this.timeout) handleComposeTimeout(this.timeout)
} }
@ -632,7 +666,7 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
if (candidateIndex >= candidates.count()) candidateIndex = 0 if (candidateIndex >= candidates.count()) candidateIndex = 0
clearCandidateUI() clearCandidateUI()
loadCandidates(candidateIndex) loadCandidates()
composeText(candidates[candidateIndex]) composeText(candidates[candidateIndex])
handleComposeTimeout(this.timeout) handleComposeTimeout(this.timeout)
} }
@ -701,7 +735,7 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
} }
showStatusIcon(getIconResource()) showStatusIcon(getIconResource())
loadCandidates(candidateIndex) loadCandidates()
composeText(candidates[candidateIndex]) composeText(candidates[candidateIndex])
} }

View File

@ -44,7 +44,7 @@ class Keypad(
return builder.toString() return builder.toString()
} }
private fun codeForLetter(letter: Char): Int? { fun codeForLetter(letter: Char): Int? {
return letterCodeMap[letter] return letterCodeMap[letter]
} }
} }