Compare commits
3 Commits
3b0ce64b09
...
23cccf2455
Author | SHA1 | Date | |
---|---|---|---|
23cccf2455 | |||
48a9a2e822 | |||
0ec4f4b262 |
@ -31,7 +31,6 @@ interface IME {
|
||||
|
||||
fun onDeleteText(beforeCursor: Int = 0, afterCursor: Int = 0, finishComposing: Boolean = false)
|
||||
|
||||
fun onGetTextBeforeCursor(n: Int): CharSequence?
|
||||
|
||||
fun onSwitchInputHandler(inputMode: InputMode)
|
||||
|
||||
|
@ -112,10 +112,6 @@ class WKT9IME: IME, InputMethodService() {
|
||||
inputHandler?.onDeleteWord(word)
|
||||
}
|
||||
|
||||
override fun onGetTextBeforeCursor(n: Int): CharSequence? {
|
||||
return this.currentInputConnection?.getTextBeforeCursor(n, 0)
|
||||
}
|
||||
|
||||
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
|
||||
if (keyDownStats.keyCode != keyCode) {
|
||||
keyDownStats.keyCode = keyCode
|
||||
@ -318,7 +314,7 @@ class WKT9IME: IME, InputMethodService() {
|
||||
|
||||
private fun switchInputMode(mode: InputMode) {
|
||||
inputHandler = when(mode) {
|
||||
InputMode.Word -> WordInputHandler(this, this, locale)
|
||||
InputMode.Word -> WordInputHandler(this, this, locale)
|
||||
|
||||
InputMode.Letter -> {
|
||||
val prefs: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
|
||||
|
@ -19,14 +19,18 @@ open class DefaultInputHandler(
|
||||
private var currentCapMode: Int? = null
|
||||
|
||||
protected val punctuationMarks = listOf(" ", ". ", "? ", "! ", ", ", ": ", "; ")
|
||||
protected val sentenceDelimiters = listOf('.', '?', '!')
|
||||
protected val wordDelimiters = listOf('\t', '\n', ' ', ',', ':', ';')
|
||||
|
||||
protected val tag = "WKT9"
|
||||
protected val keypad: Keypad = Keypad()
|
||||
|
||||
protected var wordStart: Boolean = true
|
||||
protected var sentenceStart: Boolean = true
|
||||
protected var wordStart: Boolean = false
|
||||
protected var sentenceStart: Boolean = false
|
||||
|
||||
init {
|
||||
setCursorPositionStatus()
|
||||
|
||||
wkt9.currentInputEditorInfo?.let {
|
||||
val inputType = it.inputType
|
||||
val typeFlags = inputType.and(InputType.TYPE_MASK_FLAGS)
|
||||
@ -83,4 +87,27 @@ open class DefaultInputHandler(
|
||||
protected fun triggerOriginalKeyEvent(key: Key) {
|
||||
triggerKeyEvent(key.keyCode)
|
||||
}
|
||||
|
||||
protected fun setCursorPositionStatus() {
|
||||
val textBeforeCursor = getTextBeforeCursor()
|
||||
|
||||
if (
|
||||
textBeforeCursor.isNullOrEmpty() ||
|
||||
textBeforeCursor.trim().isEmpty() ||
|
||||
sentenceDelimiters.contains(textBeforeCursor.trim().last())
|
||||
) {
|
||||
sentenceStart = true
|
||||
wordStart = true
|
||||
} else if (wordDelimiters.contains(textBeforeCursor.last())) {
|
||||
sentenceStart = false
|
||||
wordStart = true
|
||||
} else {
|
||||
sentenceStart = false
|
||||
wordStart = false
|
||||
}
|
||||
}
|
||||
|
||||
private fun getTextBeforeCursor(): CharSequence? {
|
||||
return wkt9.currentInputConnection?.getTextBeforeCursor(15, 0)
|
||||
}
|
||||
}
|
@ -30,7 +30,7 @@ class LetterInputHandler(
|
||||
val ime: IME,
|
||||
private var wkt9: WKT9IME,
|
||||
private var locale: Locale,
|
||||
private var composeTimeout: Long
|
||||
private var composeTimeout: Long,
|
||||
): SpellCheckerSession.SpellCheckerSessionListener, DefaultInputHandler(wkt9), InputHandler {
|
||||
private val db = AppDatabase.getInstance(wkt9)
|
||||
private val wordDao = db.getWordDao()
|
||||
@ -59,64 +59,22 @@ class LetterInputHandler(
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun finalizeWordOrSentence(stats: KeyEventStat) {
|
||||
if (word.isNotEmpty()) storeWord()
|
||||
override fun onDeleteWord(word: Word) {}
|
||||
|
||||
timeoutJob?.cancel()
|
||||
override fun onGetSuggestions(results: Array<out SuggestionsInfo>?) {}
|
||||
|
||||
val index = stats.repeats % punctuationMarks.count()
|
||||
val lastIndex = if (stats.repeats > 0) (stats.repeats - 1) % punctuationMarks.count() else null
|
||||
var beforeCursor = 0
|
||||
|
||||
if (lastIndex != null) beforeCursor += punctuationMarks[lastIndex].length
|
||||
|
||||
wordStart = true
|
||||
sentenceStart = index in 1..3
|
||||
|
||||
wkt9.onCommit(punctuationMarks[index], beforeCursor)
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun storeWord() {
|
||||
val str = word.toString()
|
||||
val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
|
||||
word.clear()
|
||||
|
||||
scope.launch {
|
||||
val word = Word(
|
||||
word = str,
|
||||
code = keypad.getCodeForWord(str),
|
||||
weight = 0,
|
||||
length = str.length,
|
||||
locale = locale.language
|
||||
)
|
||||
val result = wordDao.selectWord(str, locale.language)
|
||||
|
||||
if (result == null) wordDao.insert(word)
|
||||
}
|
||||
}
|
||||
override fun onFinishComposing() {}
|
||||
|
||||
override fun onGetSentenceSuggestions(results: Array<out SentenceSuggestionsInfo>?) {}
|
||||
override fun onSwitchLocale(locale: Locale) {
|
||||
this.locale = locale
|
||||
}
|
||||
|
||||
override fun onDeleteWord(word: Word) {}
|
||||
override fun onGetSuggestions(results: Array<out SuggestionsInfo>?) {}
|
||||
override fun onGetSentenceSuggestions(results: Array<out SentenceSuggestionsInfo>?) {}
|
||||
override fun onFinishComposing() {}
|
||||
|
||||
override fun toggleCapMode(key: Key) {
|
||||
super.toggleCapMode(key)
|
||||
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
override fun onRunCommand(command: Command, key: Key, event: KeyEvent, stats: KeyEventStat) {
|
||||
when (command) {
|
||||
Command.CAP_MODE -> toggleCapMode(key)
|
||||
Command.CHARACTER -> composeCharacter(key)
|
||||
Command.DELETE -> delete()
|
||||
Command.FINISH_DELETE -> finishDelete()
|
||||
Command.INPUT_MODE -> inputMode(key)
|
||||
Command.NUMBER -> triggerOriginalKeyEvent(key)
|
||||
// Command.RECORD -> wkt9.onRecord(true)
|
||||
@ -125,9 +83,14 @@ class LetterInputHandler(
|
||||
else -> Log.d(tag, "Command not implemented: $command")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onWordSelected(word: Word) {}
|
||||
|
||||
override fun toggleCapMode(key: Key) {
|
||||
super.toggleCapMode(key)
|
||||
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun composeCharacter(key: Key) {
|
||||
val composing = timeoutJob?.isActive ?: false
|
||||
|
||||
@ -157,15 +120,55 @@ class LetterInputHandler(
|
||||
setComposeTimeout()
|
||||
}
|
||||
|
||||
private fun finalizeWordOrSentence(stats: KeyEventStat) {
|
||||
if (word.isNotEmpty()) storeWord()
|
||||
|
||||
timeoutJob?.cancel()
|
||||
|
||||
val index = stats.repeats % punctuationMarks.count()
|
||||
val lastIndex = if (stats.repeats > 0) (stats.repeats - 1) % punctuationMarks.count() else null
|
||||
var beforeCursor = 0
|
||||
|
||||
if (lastIndex != null) beforeCursor += punctuationMarks[lastIndex].length
|
||||
|
||||
wordStart = true
|
||||
sentenceStart = index in 1..3
|
||||
|
||||
wkt9.onCommit(punctuationMarks[index], beforeCursor)
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun finishDelete() {
|
||||
setCursorPositionStatus()
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun resetKey(key: Key? = null) {
|
||||
lastKey = key
|
||||
repeats = 0
|
||||
}
|
||||
|
||||
private fun finishComposingChar() {
|
||||
val wordDelimiters = listOf(' ', ',', ':', ';')
|
||||
val sentenceDelimiters = listOf('.', '?', '!')
|
||||
private fun storeWord() {
|
||||
val str = word.toString()
|
||||
val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
|
||||
word.clear()
|
||||
|
||||
scope.launch {
|
||||
val word = Word(
|
||||
word = str,
|
||||
code = keypad.getCodeForWord(str),
|
||||
weight = 0,
|
||||
length = str.length,
|
||||
locale = locale.language
|
||||
)
|
||||
val result = wordDao.selectWord(str, locale.language)
|
||||
|
||||
if (result == null) wordDao.insert(word)
|
||||
}
|
||||
}
|
||||
|
||||
private fun finishComposingChar() {
|
||||
wkt9.onCommit()
|
||||
|
||||
if (sentenceDelimiters.contains(lastComposeChar)) {
|
||||
|
@ -23,7 +23,7 @@ import java.util.Locale
|
||||
class WordInputHandler(
|
||||
val ime: IME,
|
||||
private var wkt9: WKT9IME,
|
||||
private var locale: Locale,
|
||||
private var locale: Locale
|
||||
) : DefaultInputHandler(wkt9), InputHandler {
|
||||
private val codeword = StringBuilder()
|
||||
private var staleCodeword = false
|
||||
@ -90,6 +90,7 @@ class WordInputHandler(
|
||||
Command.CHARACTER -> buildCodeword(key)
|
||||
Command.DELETE -> delete(event.repeatCount)
|
||||
Command.ENTER -> enter(key)
|
||||
Command.FINISH_DELETE -> finishDelete()
|
||||
Command.INPUT_MODE -> inputMode(key)
|
||||
Command.MOVE_CURSOR -> moveCursor(key)
|
||||
Command.NUMBER -> triggerOriginalKeyEvent(key)
|
||||
@ -131,6 +132,11 @@ class WordInputHandler(
|
||||
}
|
||||
}
|
||||
|
||||
private fun finishDelete() {
|
||||
setCursorPositionStatus()
|
||||
updateIcon()
|
||||
}
|
||||
|
||||
private fun enter(key: Key) {
|
||||
if (codeword.isNotEmpty()) wkt9.onCommit("")
|
||||
else triggerOriginalKeyEvent(key)
|
||||
@ -158,6 +164,8 @@ class WordInputHandler(
|
||||
}
|
||||
|
||||
private fun inputMode(key: Key) {
|
||||
if (codeword.isNotEmpty()) wkt9.onCommit()
|
||||
|
||||
if (key == Key.B4) wkt9.onSwitchInputHandler(InputMode.Number)
|
||||
else wkt9.onSwitchInputHandler(InputMode.Letter)
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ enum class Command {
|
||||
DELETE,
|
||||
DIAL,
|
||||
ENTER,
|
||||
FINISH_DELETE,
|
||||
INPUT_MODE,
|
||||
MOVE_CURSOR,
|
||||
NUMBER,
|
||||
|
@ -388,6 +388,12 @@ enum class Key(
|
||||
command = Command.DELETE
|
||||
),
|
||||
|
||||
CommandMapping(
|
||||
events = listOf(Event.afterShortDown, Event.afterLongDown),
|
||||
inputModes = listOf(InputMode.Word, InputMode.Letter),
|
||||
command = Command.FINISH_DELETE
|
||||
),
|
||||
|
||||
CommandMapping(
|
||||
inputModes = listOf(InputMode.Number),
|
||||
overrideConsume = true,
|
||||
|
Loading…
x
Reference in New Issue
Block a user