Getting to a stable version...

This commit is contained in:
Nehemiah of Zebulun 2023-09-05 11:03:45 +02:00
parent fa02792649
commit 980aa88381
9 changed files with 104 additions and 284 deletions

View File

@ -100,7 +100,7 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
private var spellCheckerSession: SpellCheckerSession? = null
// UI
private lateinit var inputView: View
private var inputView: View? = null
private var toast: Toast? = null
// Whisper
@ -138,7 +138,7 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
}
@SuppressLint("InflateParams")
override fun onCreateInputView(): View {
override fun onCreateInputView(): View? {
inputView = layoutInflater.inflate(R.layout.suggestions, null)
return inputView
@ -290,7 +290,7 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
}
private fun clearCandidateUI() {
val candidatesView = inputView.findViewById<LinearLayout>(R.id.suggestions)
val candidatesView = inputView?.findViewById<LinearLayout>(R.id.suggestions) ?: return
candidatesView.removeAllViews()
}
@ -420,7 +420,7 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
if (res.updateWordStatus) onUpdateWordStatus()
if (res.focus) onFocus()
if (res.switchInputMode != null) onSwitchInputMode(res.switchInputMode)
if (res.functionMode) onFunctionMode()
if (res.toggleFunctionMode) onToggleFunctionMode()
if (res.increaseVolume) onIncreaseVolume()
if (res.decreaseVolume) onDecreaseVolume()
if (res.increaseBrightness) onIncreaseBrightness()
@ -442,7 +442,7 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
}
private fun loadCandidates(highLight: Int? = null) {
val candidatesView = inputView.findViewById<LinearLayout>(R.id.suggestions)
val candidatesView = inputView?.findViewById<LinearLayout>(R.id.suggestions) ?: return
candidates.forEachIndexed { index, candidate ->
val layout = if (index == highLight) R.layout.current_suggestion else R.layout.suggestion
@ -532,8 +532,10 @@ class WKT9: InputMethodService(), SpellCheckerSession.SpellCheckerSessionListene
requestShowSelf(InputMethodManager.SHOW_IMPLICIT)
}
private fun onFunctionMode() {
enableInputMode(WKT9InputMode.FN)
private fun onToggleFunctionMode() {
if (inputMode is FNInputMode) enableInputMode(lastInputMode)
else enableInputMode(WKT9InputMode.FN)
updateInputStatus()
}

View File

@ -3,29 +3,20 @@ package net.mezimmah.wkt9.inputmode
import android.util.Log
import net.mezimmah.wkt9.keypad.Command
import net.mezimmah.wkt9.keypad.Key
import net.mezimmah.wkt9.keypad.KeyCommandResolver
import net.mezimmah.wkt9.keypad.KeyEventResult
import net.mezimmah.wkt9.keypad.KeyLayout
import java.lang.StringBuilder
class AlphaInputMode: InputMode {
class AlphaInputMode: BaseInputMode() {
private val tag = "WKT9"
private val keyCommandResolver: KeyCommandResolver = KeyCommandResolver.getBasic()
private var newKey = true
private var keyIndex = 0
private var lastKey: Key? = null
override val mode: String = "alpha"
override var status: Status = Status.CAP
private set
init {
Log.d(tag, "Started alpha input mode.")
mode = "alpha"
status = Status.CAP
Log.d(tag, "Started $mode input mode.")
}
override fun onKeyDown(key: Key, composing: Boolean): KeyEventResult {
keyStats(key)
super.onKeyDown(key, composing)
return when(keyCommandResolver.getCommand(key)) {
Command.BACK -> KeyEventResult(consumed = false)
@ -41,7 +32,7 @@ class AlphaInputMode: InputMode {
return when(keyCommandResolver.getCommand(key, true)) {
Command.RECORD -> record(composing)
Command.NUMBER -> commitNumber(key, composing)
Command.SWITCH_MODE -> switchMode(composing)
Command.SWITCH_MODE -> switchMode(WKT9InputMode.NUMERIC, composing)
else -> KeyEventResult(true)
}
}
@ -58,6 +49,7 @@ class AlphaInputMode: InputMode {
return when(keyCommandResolver.getCommand(key, after = true)) {
Command.BACK -> goBack(composing)
Command.CHARACTER -> composeCharacter(key, composing)
Command.FN -> functionMode()
Command.SHIFT_MODE -> shiftMode()
Command.SPACE -> finalizeWordOrSentence(composing)
else -> KeyEventResult()
@ -71,102 +63,10 @@ class AlphaInputMode: InputMode {
}
}
private fun deleteCharacter(composing: Boolean): KeyEventResult {
return KeyEventResult(
finishComposing = composing,
deleteBeforeCursor = 1
)
}
private fun commitNumber(key: Key, composing: Boolean): KeyEventResult {
val code = KeyLayout.numeric[key] ?: return KeyEventResult(true)
return KeyEventResult(
consumed = true,
finishComposing = composing,
commit = code.toString()
)
}
private fun composeCharacter(key: Key, composing: Boolean): KeyEventResult {
override fun composeCharacter(key: Key, composing: Boolean): KeyEventResult {
if (composing && !newKey) return navigateRight()
val layout = KeyLayout.en_US[key] ?: return KeyEventResult(true)
val candidates = layout.map { it.toString() }
return KeyEventResult(
consumed = true,
finishComposing = composing,
startComposing = true,
candidates = candidates,
timeout = 400
)
}
private fun finalizeWordOrSentence(composing: Boolean): KeyEventResult {
if (composing && !newKey) return navigateRight()
return KeyEventResult(
finishComposing = composing,
startComposing = true,
candidates = listOf(" ", ". ", "? ", "! ", ", ", ": ", "; "),
timeout = 700
)
}
private fun focus(): KeyEventResult {
return KeyEventResult(
consumed = true,
focus = true
)
}
private fun goBack(composing: Boolean): KeyEventResult {
return KeyEventResult(
consumed = false,
finishComposing = composing
)
}
private fun goHome(repeat: Int, composing: Boolean): KeyEventResult {
if (repeat > 1) return KeyEventResult(true)
return KeyEventResult(
consumed = true,
finishComposing = composing,
goHome = true
)
}
private fun keyStats(key: Key) {
when (key != lastKey) {
true -> {
newKey = true
keyIndex = 0
}
false -> {
newKey = false
keyIndex++
}
}
lastKey = key
}
private fun navigateLeft(): KeyEventResult {
return KeyEventResult(left = true)
}
private fun navigateRight(): KeyEventResult {
return KeyEventResult(right = true)
}
private fun record(composing: Boolean): KeyEventResult {
return KeyEventResult(
consumed = true,
finishComposing = composing,
record = true
)
return super.composeCharacter(key, composing)
}
private fun shiftMode(): KeyEventResult {
@ -181,20 +81,4 @@ class AlphaInputMode: InputMode {
updateInputStatus = true
)
}
private fun switchMode(composing: Boolean): KeyEventResult {
return KeyEventResult(
consumed = true,
finishComposing = composing,
switchInputMode = WKT9InputMode.NUMERIC
)
}
private fun transcribe(composing: Boolean): KeyEventResult {
return KeyEventResult(
consumed = true,
finishComposing = composing,
transcribe = true
)
}
}

View File

@ -6,11 +6,12 @@ import net.mezimmah.wkt9.keypad.KeyEventResult
import net.mezimmah.wkt9.keypad.KeyLayout
open class BaseInputMode: InputMode {
protected val keyCommandResolver: KeyCommandResolver = KeyCommandResolver.getBasic()
protected var newKey = true
protected var keyIndex = 0
protected var lastKey: Key? = null
protected open val keyCommandResolver: KeyCommandResolver = KeyCommandResolver.getBasic()
override lateinit var mode: String
protected set
@ -49,7 +50,30 @@ open class BaseInputMode: InputMode {
)
}
protected open fun deleteCharacter(repeat: Int = 0, composing: Boolean): KeyEventResult {
protected open fun composeCharacter(key: Key, composing: Boolean): KeyEventResult {
val layout = KeyLayout.en_US[key] ?: return KeyEventResult(true)
val candidates = layout.map { it.toString() }
return KeyEventResult(
consumed = true,
finishComposing = composing,
startComposing = true,
candidates = candidates,
timeout = 1200
)
}
protected fun composeNumber(key: Key, composing: Boolean): KeyEventResult {
val code = KeyLayout.numeric[key] ?: return KeyEventResult(true)
return KeyEventResult(
consumed = true,
finishComposing = composing,
commit = code.toString()
)
}
protected open fun deleteCharacter(composing: Boolean): KeyEventResult {
return KeyEventResult(
finishComposing = composing,
deleteBeforeCursor = 1
@ -77,7 +101,7 @@ open class BaseInputMode: InputMode {
protected fun functionMode(): KeyEventResult {
return KeyEventResult(
consumed = true,
functionMode = true
toggleFunctionMode = true
)
}
@ -120,11 +144,11 @@ open class BaseInputMode: InputMode {
)
}
protected open fun switchMode(composing: Boolean): KeyEventResult {
protected open fun switchMode(mode: WKT9InputMode, composing: Boolean): KeyEventResult {
return KeyEventResult(
consumed = true,
finishComposing = composing,
switchInputMode = WKT9InputMode.ALPHA
switchInputMode = mode
)
}

View File

@ -6,11 +6,10 @@ import net.mezimmah.wkt9.keypad.Key
import net.mezimmah.wkt9.keypad.KeyCommandResolver
import net.mezimmah.wkt9.keypad.KeyEventResult
class FNInputMode: InputMode {
class FNInputMode: BaseInputMode() {
private val tag = "WKT9"
private val parentKeyCommandResolver: KeyCommandResolver = KeyCommandResolver.getBasic()
private val keyCommandResolver: KeyCommandResolver = KeyCommandResolver(
parent = parentKeyCommandResolver,
override val keyCommandResolver: KeyCommandResolver = KeyCommandResolver(
parent = super.keyCommandResolver,
onShort = HashMap(mapOf(
Key.UP to Command.VOL_UP,
@ -23,49 +22,55 @@ class FNInputMode: InputMode {
Key.UP to Command.VOL_UP,
Key.DOWN to Command.VOL_DOWN,
Key.LEFT to Command.BRIGHTNESS_DOWN,
Key.RIGHT to Command.BRIGHTNESS_UP
Key.RIGHT to Command.BRIGHTNESS_UP,
Key.BACK to Command.HOME
))
)
override val mode: String = "fn"
override var status: Status = Status.NA
private set
init {
mode = "fn"
status = Status.NA
Log.d(tag, "Started $mode input mode.")
}
override fun onKeyDown(key: Key, composing: Boolean): KeyEventResult {
return when(keyCommandResolver.getCommand(key)) {
Command.BACK -> KeyEventResult(false)
Command.VOL_UP -> volumeUp()
Command.VOL_DOWN -> volumeDown()
Command.BRIGHTNESS_DOWN -> brightnessDown()
Command.BRIGHTNESS_UP -> brightnessUp()
else -> KeyEventResult(false)
else -> KeyEventResult(true)
}
}
override fun onKeyLongDown(key: Key, composing: Boolean): KeyEventResult {
return KeyEventResult(false)
return KeyEventResult(true)
}
override fun onKeyDownRepeatedly(key: Key, repeat: Int, composing: Boolean): KeyEventResult {
return when(keyCommandResolver.getCommand(key, repeat = repeat)) {
return when(keyCommandResolver.getCommand(key, repeat = repeat)) {
Command.HOME -> goHome(repeat, composing)
Command.VOL_UP -> volumeUp()
Command.VOL_DOWN -> volumeDown()
Command.BRIGHTNESS_DOWN -> brightnessDown()
Command.BRIGHTNESS_UP -> brightnessUp()
else -> KeyEventResult(false)
else -> KeyEventResult(true)
}
}
override fun afterKeyDown(key: Key, composing: Boolean): KeyEventResult {
return KeyEventResult(false)
return when(keyCommandResolver.getCommand(key, after = true)) {
Command.BACK -> goBack(composing)
Command.FN -> functionMode()
else -> KeyEventResult(true)
}
}
override fun afterKeyLongDown(key: Key, keyDownMS: Long, composing: Boolean): KeyEventResult {
return KeyEventResult(false)
return KeyEventResult(true)
}
private fun brightnessDown(): KeyEventResult {

View File

@ -3,24 +3,21 @@ package net.mezimmah.wkt9.inputmode
import android.util.Log
import net.mezimmah.wkt9.keypad.Command
import net.mezimmah.wkt9.keypad.Key
import net.mezimmah.wkt9.keypad.KeyCommandResolver
import net.mezimmah.wkt9.keypad.KeyEventResult
class IdleInputMode : InputMode {
override val mode: String = "idle"
override var status: Status = Status.NA
private set
class IdleInputMode : BaseInputMode() {
private val tag = "WKT9"
private val keyCommandResolver: KeyCommandResolver = KeyCommandResolver.getBasic()
init {
mode = "idle"
status = Status.NA
Log.d(tag, "Started $mode input mode.")
}
override fun onKeyDown(key: Key, composing: Boolean): KeyEventResult {
return when(keyCommandResolver.getCommand(key)) {
Command.FN -> KeyEventResult(true)
else -> KeyEventResult(false)
}
}
@ -33,14 +30,15 @@ class IdleInputMode : InputMode {
override fun onKeyDownRepeatedly(key: Key, repeat: Int, composing: Boolean): KeyEventResult {
return when(keyCommandResolver.getCommand(key, repeat = repeat)) {
Command.HOME -> goHome(repeat)
Command.HOME -> goHome(repeat, composing)
else -> KeyEventResult(false)
}
}
override fun afterKeyDown(key: Key, composing: Boolean): KeyEventResult {
return when(keyCommandResolver.getCommand(key, after = true)) {
Command.BACK -> goBack()
Command.BACK -> goBack(composing)
Command.FN -> functionMode()
else -> KeyEventResult(false)
}
}
@ -50,19 +48,4 @@ class IdleInputMode : InputMode {
else -> KeyEventResult(false)
}
}
private fun goBack(): KeyEventResult {
return KeyEventResult(
consumed = false,
)
}
private fun goHome(repeat: Int): KeyEventResult {
if (repeat > 1) return KeyEventResult(true)
return KeyEventResult(
consumed = true,
goHome = true
)
}
}

View File

@ -5,13 +5,11 @@ import net.mezimmah.wkt9.keypad.Command
import net.mezimmah.wkt9.keypad.Key
import net.mezimmah.wkt9.keypad.KeyCommandResolver
import net.mezimmah.wkt9.keypad.KeyEventResult
import net.mezimmah.wkt9.keypad.KeyLayout
class NumericInputMode: InputMode {
class NumericInputMode: BaseInputMode() {
private val tag = "WKT9"
private val parentKeyCommandResolver: KeyCommandResolver = KeyCommandResolver.getBasic()
private val keyCommandResolver: KeyCommandResolver = KeyCommandResolver(
parent = parentKeyCommandResolver,
override val keyCommandResolver: KeyCommandResolver = KeyCommandResolver(
parent = super.keyCommandResolver,
onLong = HashMap(mapOf(
Key.N0 to Command.SPACE,
@ -37,23 +35,22 @@ class NumericInputMode: InputMode {
Key.N7 to Command.NUMBER,
Key.N8 to Command.NUMBER,
Key.N9 to Command.NUMBER
)),
onRepeat = HashMap(mapOf(
Key.BACK to Command.HOME
))
)
private var newKey = true
private var keyIndex = 0
private var lastKey: Key? = null
override val mode: String = "numeric"
override var status: Status = Status.NUM
private set
init {
Log.d(tag, "Started numeric input mode.")
mode = "numeric"
status = Status.NUM
Log.d(tag, "Started $mode input mode.")
}
override fun onKeyDown(key: Key, composing: Boolean): KeyEventResult {
keyStats(key)
super.onKeyDown(key, composing)
return when(keyCommandResolver.getCommand(key)) {
Command.BACK -> KeyEventResult(consumed = false)
@ -68,13 +65,13 @@ class NumericInputMode: InputMode {
return when(keyCommandResolver.getCommand(key, true)) {
Command.CHARACTER -> composeCharacter(key, composing)
Command.SPACE -> insertSpace(composing)
Command.SWITCH_MODE -> switchMode(composing)
Command.SWITCH_MODE -> switchMode(WKT9InputMode.WORD, composing)
else -> KeyEventResult(true)
}
}
override fun onKeyDownRepeatedly(key: Key, repeat: Int, composing: Boolean): KeyEventResult {
return when(keyCommandResolver.getCommand(key, repeat = repeat)) {
return when(keyCommandResolver.getCommand(key, repeat = repeat)) {
Command.HOME -> goHome(repeat, composing)
Command.DELETE -> deleteCharacter(composing)
else -> KeyEventResult()
@ -84,6 +81,7 @@ class NumericInputMode: InputMode {
override fun afterKeyDown(key: Key, composing: Boolean): KeyEventResult {
return when(keyCommandResolver.getCommand(key, after = true)) {
Command.BACK -> goBack(composing)
Command.FN -> functionMode()
Command.NUMBER -> composeNumber(key, composing)
else -> KeyEventResult()
}
@ -95,53 +93,6 @@ class NumericInputMode: InputMode {
}
}
private fun deleteCharacter(composing: Boolean): KeyEventResult {
return KeyEventResult(
finishComposing = composing,
deleteBeforeCursor = 1
)
}
private fun composeCharacter(key: Key, composing: Boolean): KeyEventResult {
val layout = KeyLayout.en_US[key] ?: return KeyEventResult(true)
val candidates = layout.map { it.toString() }
return KeyEventResult(
consumed = true,
finishComposing = composing,
startComposing = true,
candidates = candidates,
timeout = 1200
)
}
private fun composeNumber(key: Key, composing: Boolean): KeyEventResult {
val code = KeyLayout.numeric[key] ?: return KeyEventResult(true)
return KeyEventResult(
consumed = true,
finishComposing = composing,
commit = code.toString()
)
}
private fun goBack(composing: Boolean): KeyEventResult {
return KeyEventResult(
consumed = false,
finishComposing = composing
)
}
private fun goHome(repeat: Int, composing: Boolean): KeyEventResult {
if (repeat > 1) return KeyEventResult(true)
return KeyEventResult(
consumed = true,
finishComposing = composing,
goHome = true
)
}
private fun insertSpace(composing: Boolean): KeyEventResult {
return KeyEventResult(
consumed = true,
@ -149,35 +100,4 @@ class NumericInputMode: InputMode {
commit = " "
)
}
private fun keyStats(key: Key) {
when (key != lastKey) {
true -> {
newKey = true
keyIndex = 0
}
false -> {
newKey = false
keyIndex++
}
}
lastKey = key
}
private fun navigateLeft(): KeyEventResult {
return KeyEventResult(left = true)
}
private fun navigateRight(): KeyEventResult {
return KeyEventResult(right = true)
}
private fun switchMode(composing: Boolean): KeyEventResult {
return KeyEventResult(
consumed = true,
finishComposing = composing,
switchInputMode = WKT9InputMode.WORD
)
}
}

View File

@ -23,8 +23,7 @@ class WordInputMode: BaseInputMode() {
return when(keyCommandResolver.getCommand(key)) {
Command.BACK -> KeyEventResult(consumed = false)
Command.DELETE -> deleteCharacter(0, composing)
Command.FN -> functionMode()
Command.DELETE -> deleteCharacter(composing)
Command.LEFT -> navigateLeft()
Command.RIGHT -> navigateRight()
Command.SELECT -> focus()
@ -35,7 +34,7 @@ class WordInputMode: BaseInputMode() {
override fun onKeyLongDown(key: Key, composing: Boolean): KeyEventResult {
return when(keyCommandResolver.getCommand(key, true)) {
Command.RECORD -> record(composing)
Command.SWITCH_MODE -> switchMode(composing)
Command.SWITCH_MODE -> switchMode(WKT9InputMode.ALPHA, composing)
Command.NUMBER -> commitNumber(key, composing)
else -> KeyEventResult(true)
}
@ -44,7 +43,7 @@ class WordInputMode: BaseInputMode() {
override fun onKeyDownRepeatedly(key: Key, repeat: Int, composing: Boolean): KeyEventResult {
return when(keyCommandResolver.getCommand(key, repeat = repeat)) {
Command.HOME -> goHome(repeat, composing)
Command.DELETE -> deleteCharacter(repeat, composing)
Command.DELETE -> deleteCharacter(composing)
else -> KeyEventResult()
}
}
@ -53,6 +52,7 @@ class WordInputMode: BaseInputMode() {
return when(keyCommandResolver.getCommand(key, after = true)) {
Command.BACK -> goBack(composing)
Command.CHARACTER -> buildCodeWord(key, composing)
Command.FN -> functionMode()
Command.SHIFT_MODE -> shiftMode(composing)
Command.SPACE -> finalizeWordOrSentence(composing)
else -> KeyEventResult()
@ -66,10 +66,10 @@ class WordInputMode: BaseInputMode() {
}
}
override fun deleteCharacter(repeat: Int, composing: Boolean): KeyEventResult {
if (repeat == 0) codeWord.clear()
override fun deleteCharacter(composing: Boolean): KeyEventResult {
codeWord.clear()
return super.deleteCharacter(repeat, composing)
return super.deleteCharacter(composing)
}
override fun finalizeWordOrSentence(composing: Boolean): KeyEventResult {
@ -96,10 +96,10 @@ class WordInputMode: BaseInputMode() {
return super.record(composing)
}
override fun switchMode(composing: Boolean): KeyEventResult {
override fun switchMode(mode: WKT9InputMode, composing: Boolean): KeyEventResult {
reset()
return super.switchMode(composing)
return super.switchMode(mode, composing)
}
private fun buildCodeWord(key: Key, composing: Boolean): KeyEventResult {

View File

@ -71,6 +71,8 @@ class KeyCommandResolver (
Key.BACK to Command.BACK,
Key.POUND to Command.SHIFT_MODE,
Key.FN to Command.FN
)),
afterLong = HashMap(mapOf(

View File

@ -23,7 +23,7 @@ data class KeyEventResult(
val updateWordStatus: Boolean = false,
val focus: Boolean = false,
val switchInputMode: WKT9InputMode? = null,
val functionMode: Boolean = false,
val toggleFunctionMode: Boolean = false,
val increaseVolume: Boolean = false,
val decreaseVolume: Boolean = false,
val increaseBrightness: Boolean = false,