Skip to content

Commit

Permalink
refactor: tidy the stuffs of input method service
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Mar 8, 2024
1 parent 318d65b commit db10498
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.osfans.trime.ime.lifecycle
package com.osfans.trime.ime.core

import android.inputmethodservice.InputMethodService
import androidx.annotation.CallSuper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import com.osfans.trime.ime.keyboard.Key
import com.osfans.trime.ime.keyboard.KeyboardSwitcher
import com.osfans.trime.ime.keyboard.KeyboardView
import com.osfans.trime.ime.keyboard.KeyboardWindow
import com.osfans.trime.ime.lifecycle.LifecycleInputMethodService
import com.osfans.trime.ime.symbol.TabManager
import com.osfans.trime.ime.symbol.TabView
import com.osfans.trime.ime.text.Candidate
Expand Down Expand Up @@ -472,7 +471,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS,
InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD,
-> {
Timber.i(
Timber.d(
"EditorInfo: private;" +
" packageName=" +
attribute.packageName +
Expand All @@ -493,7 +492,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
}

else -> {
Timber.i(
Timber.d(
"EditorInfo: normal;" +
" packageName=" +
attribute.packageName +
Expand Down Expand Up @@ -639,15 +638,15 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
keyCode: Int,
event: KeyEvent,
): Boolean {
Timber.i("\t<TrimeInput>\tonKeyDown()\tkeycode=%d, event=%s", keyCode, event.toString())
Timber.d("\t<TrimeInput>\tonKeyDown()\tkeycode=%d, event=%s", keyCode, event.toString())
return if (composeEvent(event) && onKeyEvent(event) && isWindowShown) true else super.onKeyDown(keyCode, event)
}

override fun onKeyUp(
keyCode: Int,
event: KeyEvent,
): Boolean {
Timber.i("\t<TrimeInput>\tonKeyUp()\tkeycode=%d, event=%s", keyCode, event.toString())
Timber.d("\t<TrimeInput>\tonKeyUp()\tkeycode=%d, event=%s", keyCode, event.toString())
if (composeEvent(event) && textInputManager!!.needSendUpRimeKey) {
textInputManager!!.onRelease(keyCode)
if (isWindowShown) return true
Expand All @@ -662,7 +661,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
* @return 是否成功處理
*/
private fun onKeyEvent(event: KeyEvent): Boolean {
Timber.i("\t<TrimeInput>\tonKeyEvent()\tRealKeyboard event=%s", event.toString())
Timber.d("\t<TrimeInput>\tonKeyEvent()\tRealKeyboard event=%s", event.toString())
var keyCode = event.keyCode
textInputManager!!.needSendUpRimeKey = Rime.isComposing
if (!this.isComposing) {
Expand Down Expand Up @@ -698,11 +697,8 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
switchToPreviousInputMethod()
} else {
val window = window.window
if (window != null) {
inputMethodManager
.switchToLastInputMethod(window.attributes.token)
}
@Suppress("DEPRECATION")
inputMethodManager.switchToLastInputMethod(window.window!!.attributes.token)
}
} catch (e: Exception) {
Timber.e(e, "Unable to switch to the previous IME.")
Expand All @@ -715,11 +711,8 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
switchToNextInputMethod(false)
} else {
val window = window.window
if (window != null) {
inputMethodManager
.switchToNextInputMethod(window.attributes.token, false)
}
@Suppress("DEPRECATION")
inputMethodManager.switchToNextInputMethod(window.window!!.attributes.token, false)
}
} catch (e: Exception) {
Timber.e(e, "Unable to switch to the next IME.")
Expand Down Expand Up @@ -808,7 +801,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
if (et.selectionEnd - et.selectionStart > 0) return ic.performContextMenuAction(android.R.id.cut)
}
}
Timber.i("hookKeyboard cut fail")
Timber.w("hookKeyboard cut fail")
return false
}

Expand All @@ -822,7 +815,7 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
if (et.selectionEnd - et.selectionStart > 0) return ic.performContextMenuAction(android.R.id.copy)
}
}
Timber.i("hookKeyboard copy fail")
Timber.w("hookKeyboard copy fail")
return false
}

Expand Down Expand Up @@ -996,22 +989,16 @@ open class TrimeInputMethodService : LifecycleInputMethodService() {
interface EventListener {
fun onCreate() {}

fun onInitializeInputUi(inputView: InputView) {}

fun onDestroy() {}

fun onStartInputView(
instance: EditorInstance,
restarting: Boolean,
) {}

fun osFinishInputView(finishingInput: Boolean) {}

fun onWindowShown() {}

fun onWindowHidden() {}

fun onUpdateSelection() {}
}

companion object {
Expand Down

0 comments on commit db10498

Please sign in to comment.