Base input mode class
This commit is contained in:
parent
2fdb217e90
commit
fa02792649
153
app/src/main/java/net/mezimmah/wkt9/inputmode/BaseInputMode.kt
Normal file
153
app/src/main/java/net/mezimmah/wkt9/inputmode/BaseInputMode.kt
Normal file
@ -0,0 +1,153 @@
|
||||
package net.mezimmah.wkt9.inputmode
|
||||
|
||||
import net.mezimmah.wkt9.keypad.Key
|
||||
import net.mezimmah.wkt9.keypad.KeyCommandResolver
|
||||
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
|
||||
|
||||
override lateinit var mode: String
|
||||
protected set
|
||||
|
||||
override lateinit var status: Status
|
||||
protected set
|
||||
|
||||
override fun onKeyDown(key: Key, composing: Boolean): KeyEventResult {
|
||||
keyStats(key)
|
||||
|
||||
return KeyEventResult()
|
||||
}
|
||||
|
||||
override fun onKeyLongDown(key: Key, composing: Boolean): KeyEventResult {
|
||||
return KeyEventResult()
|
||||
}
|
||||
|
||||
override fun onKeyDownRepeatedly(key: Key, repeat: Int, composing: Boolean): KeyEventResult {
|
||||
return KeyEventResult()
|
||||
}
|
||||
|
||||
override fun afterKeyDown(key: Key, composing: Boolean): KeyEventResult {
|
||||
return KeyEventResult()
|
||||
}
|
||||
|
||||
override fun afterKeyLongDown(key: Key, keyDownMS: Long, composing: Boolean): KeyEventResult {
|
||||
return KeyEventResult()
|
||||
}
|
||||
|
||||
protected open fun commitNumber(key: Key, composing: Boolean): KeyEventResult {
|
||||
val number = KeyLayout.numeric[key] ?: return KeyEventResult(true)
|
||||
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
finishComposing = composing,
|
||||
commit = number.toString()
|
||||
)
|
||||
}
|
||||
|
||||
protected open fun deleteCharacter(repeat: Int = 0, composing: Boolean): KeyEventResult {
|
||||
return KeyEventResult(
|
||||
finishComposing = composing,
|
||||
deleteBeforeCursor = 1
|
||||
)
|
||||
}
|
||||
|
||||
protected open fun finalizeWordOrSentence(composing: Boolean): KeyEventResult {
|
||||
if (composing && !newKey) return navigateRight()
|
||||
|
||||
return KeyEventResult(
|
||||
finishComposing = composing,
|
||||
startComposing = true,
|
||||
candidates = listOf(" ", ". ", "? ", "! ", ", ", ": ", "; "),
|
||||
timeout = 700
|
||||
)
|
||||
}
|
||||
|
||||
protected fun focus(): KeyEventResult {
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
focus = true
|
||||
)
|
||||
}
|
||||
|
||||
protected fun functionMode(): KeyEventResult {
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
functionMode = true
|
||||
)
|
||||
}
|
||||
|
||||
protected open fun goBack(composing: Boolean): KeyEventResult {
|
||||
return KeyEventResult(
|
||||
consumed = false,
|
||||
finishComposing = composing
|
||||
)
|
||||
}
|
||||
|
||||
protected open fun goHome(repeat: Int, composing: Boolean): KeyEventResult {
|
||||
if (repeat > 1) return KeyEventResult(true)
|
||||
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
finishComposing = composing,
|
||||
goHome = true
|
||||
)
|
||||
}
|
||||
|
||||
protected open fun navigateLeft(): KeyEventResult {
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
left = true
|
||||
)
|
||||
}
|
||||
|
||||
protected open fun navigateRight(): KeyEventResult {
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
right = true
|
||||
)
|
||||
}
|
||||
|
||||
protected open fun record(composing: Boolean): KeyEventResult {
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
finishComposing = composing,
|
||||
record = true
|
||||
)
|
||||
}
|
||||
|
||||
protected open fun switchMode(composing: Boolean): KeyEventResult {
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
finishComposing = composing,
|
||||
switchInputMode = WKT9InputMode.ALPHA
|
||||
)
|
||||
}
|
||||
|
||||
protected fun transcribe(composing: Boolean): KeyEventResult {
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
finishComposing = composing,
|
||||
transcribe = true
|
||||
)
|
||||
}
|
||||
|
||||
private fun keyStats(key: Key) {
|
||||
when (key != lastKey) {
|
||||
true -> {
|
||||
newKey = true
|
||||
keyIndex = 0
|
||||
}
|
||||
false -> {
|
||||
newKey = false
|
||||
keyIndex++
|
||||
}
|
||||
}
|
||||
|
||||
lastKey = key
|
||||
}
|
||||
}
|
@ -3,30 +3,23 @@ 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 WordInputMode: InputMode {
|
||||
class WordInputMode: BaseInputMode() {
|
||||
private val tag = "WKT9"
|
||||
private val keyCommandResolver: KeyCommandResolver = KeyCommandResolver.getBasic()
|
||||
private val codeWord = StringBuilder()
|
||||
private var newKey = true
|
||||
private var keyIndex = 0
|
||||
private var lastKey: Key? = null
|
||||
|
||||
override val mode: String = "word"
|
||||
|
||||
override var status: Status = Status.CAP
|
||||
private set
|
||||
|
||||
init {
|
||||
Log.d(tag, "Started word input mode.")
|
||||
mode = "word"
|
||||
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)
|
||||
@ -73,6 +66,42 @@ class WordInputMode: InputMode {
|
||||
}
|
||||
}
|
||||
|
||||
override fun deleteCharacter(repeat: Int, composing: Boolean): KeyEventResult {
|
||||
if (repeat == 0) codeWord.clear()
|
||||
|
||||
return super.deleteCharacter(repeat, composing)
|
||||
}
|
||||
|
||||
override fun finalizeWordOrSentence(composing: Boolean): KeyEventResult {
|
||||
codeWord.clear()
|
||||
|
||||
return super.finalizeWordOrSentence(composing)
|
||||
}
|
||||
|
||||
override fun goBack(composing: Boolean): KeyEventResult {
|
||||
reset()
|
||||
|
||||
return super.goBack(composing)
|
||||
}
|
||||
|
||||
override fun goHome(repeat: Int, composing: Boolean): KeyEventResult {
|
||||
reset()
|
||||
|
||||
return super.goHome(repeat, composing)
|
||||
}
|
||||
|
||||
override fun record(composing: Boolean): KeyEventResult {
|
||||
codeWord.clear()
|
||||
|
||||
return super.record(composing)
|
||||
}
|
||||
|
||||
override fun switchMode(composing: Boolean): KeyEventResult {
|
||||
reset()
|
||||
|
||||
return super.switchMode(composing)
|
||||
}
|
||||
|
||||
private fun buildCodeWord(key: Key, composing: Boolean): KeyEventResult {
|
||||
val startComposing = codeWord.isEmpty()
|
||||
val code = KeyLayout.numeric[key]
|
||||
@ -86,109 +115,6 @@ class WordInputMode: InputMode {
|
||||
)
|
||||
}
|
||||
|
||||
private fun commitNumber(key: Key, composing: Boolean): KeyEventResult {
|
||||
val number = KeyLayout.numeric[key] ?: return KeyEventResult(true)
|
||||
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
finishComposing = composing,
|
||||
commit = number.toString()
|
||||
)
|
||||
}
|
||||
|
||||
private fun deleteCharacter(repeat: Int = 0, composing: Boolean): KeyEventResult {
|
||||
if (repeat == 0) codeWord.clear()
|
||||
|
||||
return KeyEventResult(
|
||||
finishComposing = composing,
|
||||
deleteBeforeCursor = 1
|
||||
)
|
||||
}
|
||||
|
||||
private fun finalizeWordOrSentence(composing: Boolean): KeyEventResult {
|
||||
if (composing && !newKey) return navigateRight()
|
||||
|
||||
val finishComposing = codeWord.isNotEmpty() && composing
|
||||
|
||||
codeWord.clear()
|
||||
|
||||
return KeyEventResult(
|
||||
finishComposing = finishComposing,
|
||||
startComposing = true,
|
||||
increaseWeight = true,
|
||||
candidates = listOf(" ", ". ", "? ", "! ", ", ", ": ", "; "),
|
||||
timeout = 700
|
||||
)
|
||||
}
|
||||
|
||||
private fun focus(): KeyEventResult {
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
focus = true
|
||||
)
|
||||
}
|
||||
|
||||
private fun functionMode(): KeyEventResult {
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
functionMode = true
|
||||
)
|
||||
}
|
||||
|
||||
private fun goBack(composing: Boolean): KeyEventResult {
|
||||
reset()
|
||||
|
||||
return KeyEventResult(
|
||||
consumed = false,
|
||||
finishComposing = composing
|
||||
)
|
||||
}
|
||||
|
||||
private fun goHome(repeat: Int, composing: Boolean): KeyEventResult {
|
||||
if (repeat > 1) return KeyEventResult(true)
|
||||
|
||||
reset()
|
||||
|
||||
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 {
|
||||
codeWord.clear()
|
||||
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
finishComposing = composing,
|
||||
record = true
|
||||
)
|
||||
}
|
||||
|
||||
private fun reset() {
|
||||
codeWord.clear()
|
||||
newKey = true
|
||||
@ -211,22 +137,4 @@ class WordInputMode: InputMode {
|
||||
updateWordStatus = composing
|
||||
)
|
||||
}
|
||||
|
||||
private fun switchMode(composing: Boolean): KeyEventResult {
|
||||
reset()
|
||||
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
finishComposing = composing,
|
||||
switchInputMode = WKT9InputMode.ALPHA
|
||||
)
|
||||
}
|
||||
|
||||
private fun transcribe(composing: Boolean): KeyEventResult {
|
||||
return KeyEventResult(
|
||||
consumed = true,
|
||||
finishComposing = composing,
|
||||
transcribe = true
|
||||
)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user