|
|
@@ -10,12 +10,7 @@ import com.atmob.keyboard_android.keyboard.ext.InputMethodLifecycleService
|
|
|
import com.atmob.keyboard_android.mvvm.ViewModelManager
|
|
|
import com.atmob.keyboard_android.mvvm.viewmodel.KeyboardViewModel
|
|
|
import com.atmob.keyboard_android.util.ClipboardHelper
|
|
|
-import com.atmob.keyboard_android.util.InputMethodUtil
|
|
|
import com.atmob.keyboard_android.util.KeyboardHolder
|
|
|
-import com.atmob.keyboard_android.util.LogUtil
|
|
|
-import io.flutter.embedding.engine.FlutterEngineCache
|
|
|
-import io.flutter.plugin.common.MethodCall
|
|
|
-import io.flutter.plugin.common.MethodChannel
|
|
|
|
|
|
/**
|
|
|
* 自定义键盘的输入法服务
|
|
|
@@ -23,21 +18,6 @@ import io.flutter.plugin.common.MethodChannel
|
|
|
class CustomKeyboardService : InputMethodLifecycleService(), ICustomKeyboardService,
|
|
|
ClipboardHelper.OnUserClipboardDataUpdateListener {
|
|
|
/**
|
|
|
- * 用于与 Flutter 端通信的 MethodChannel
|
|
|
- */
|
|
|
- private lateinit var mMethodChannel: MethodChannel
|
|
|
-
|
|
|
- /**
|
|
|
- * 保存键盘视图
|
|
|
- */
|
|
|
- private var vKeyboardView: View? = null
|
|
|
-
|
|
|
- /**
|
|
|
- * 存储按键映射
|
|
|
- */
|
|
|
- private var mKeyMappings: List<Pair<String, String>> = listOf()
|
|
|
-
|
|
|
- /**
|
|
|
* 键盘ViewModel
|
|
|
*/
|
|
|
private val mKeyboardViewModel by lazy {
|
|
|
@@ -53,49 +33,16 @@ class CustomKeyboardService : InputMethodLifecycleService(), ICustomKeyboardServ
|
|
|
super.onCreate()
|
|
|
// 保存输入法Service的实例
|
|
|
KeyboardHolder.attachKeyboardService(this)
|
|
|
-
|
|
|
- val flutterEngine = FlutterEngineCache.getInstance().get("my_engine_id")
|
|
|
- if (flutterEngine != null) {
|
|
|
- mMethodChannel =
|
|
|
- MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "keyboard_android")
|
|
|
-
|
|
|
- // 设置 MethodChannel 的回调
|
|
|
- mMethodChannel.setMethodCallHandler { call: MethodCall, result: MethodChannel.Result ->
|
|
|
- when (call.method) {
|
|
|
- "inputText" -> {
|
|
|
- val text = call.argument<String>("text")
|
|
|
- if (text != null) {
|
|
|
- InputMethodUtil.inputText(this.currentInputConnection, text)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- else -> {
|
|
|
- result.notImplemented()
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- LogUtil.e("FlutterEngine 未找到,MethodChannel 无法初始化")
|
|
|
- }
|
|
|
-
|
|
|
// 监听用户的剪切板
|
|
|
ClipboardHelper.registerClipboardListener(this)
|
|
|
}
|
|
|
|
|
|
override fun onCreateInputView(): View {
|
|
|
- val keyboardView = layoutInflater.inflate(R.layout.keyboard_layout, null)
|
|
|
- vKeyboardView = keyboardView
|
|
|
-
|
|
|
- // 获取按键映射
|
|
|
- fetchKeyMappings()
|
|
|
-
|
|
|
- return keyboardView
|
|
|
+ return layoutInflater.inflate(R.layout.keyboard_layout, null)
|
|
|
}
|
|
|
|
|
|
override fun onStartInputView(info: EditorInfo?, restarting: Boolean) {
|
|
|
super.onStartInputView(info, restarting)
|
|
|
- // 重新获取按键映射
|
|
|
- fetchKeyMappings()
|
|
|
}
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
@@ -104,57 +51,6 @@ class CustomKeyboardService : InputMethodLifecycleService(), ICustomKeyboardServ
|
|
|
ClipboardHelper.unRegisterClipboardListener(this)
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 通过 KeyboardAndroidPlugin 获取按键映射
|
|
|
- */
|
|
|
- private fun fetchKeyMappings() {
|
|
|
- // 通过 methodChannel 获取按键映射
|
|
|
- mMethodChannel.invokeMethod("getKeyMappings", null, object : MethodChannel.Result {
|
|
|
- override fun success(result: Any?) {
|
|
|
- // 如果获取成功,处理键映射
|
|
|
- if (result is List<*>) {
|
|
|
- mKeyMappings = result.filterIsInstance<Map<String, String>>().map {
|
|
|
- it["label"]!! to it["method"]!!
|
|
|
- }
|
|
|
- LogUtil.d("按键映射获取成功: $mKeyMappings")
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- override fun error(errorCode: String, errorMessage: String?, errorDetails: Any?) {
|
|
|
- LogUtil.d("获取按键映射失败: $errorMessage")
|
|
|
- }
|
|
|
-
|
|
|
- override fun notImplemented() {
|
|
|
- LogUtil.d("Flutter 端未实现 getKeyMappings 方法")
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 动态调用 Flutter 获取文本
|
|
|
- *
|
|
|
- * @param methodName 方法名
|
|
|
- * @param currentContent 当前输入连接
|
|
|
- */
|
|
|
- private fun sendDynamicTextRequest(methodName: String, currentContent: String) {
|
|
|
- mMethodChannel.invokeMethod(
|
|
|
- "sendDynamicTextRequest",
|
|
|
- mapOf("method" to methodName, "currentContent" to currentContent),
|
|
|
- object : MethodChannel.Result {
|
|
|
- override fun success(result: Any?) {
|
|
|
- LogUtil.d("sendDynamicTextRequest 请求已发送")
|
|
|
- }
|
|
|
-
|
|
|
- override fun error(errorCode: String, errorMessage: String?, errorDetails: Any?) {
|
|
|
- LogUtil.d("获取动态文本失败: $errorMessage")
|
|
|
- }
|
|
|
-
|
|
|
- override fun notImplemented() {
|
|
|
- LogUtil.d("Flutter 端未实现 sendDynamicTextRequest 方法")
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
-
|
|
|
override fun getKeyboardWindow(): Window {
|
|
|
return window!!.window!!
|
|
|
}
|