浏览代码

[feat]键盘插件,对接调起Flutter页面,还有问题

hezihao 8 月之前
父节点
当前提交
ca809cb7c5

+ 28 - 1
android/app/src/main/kotlin/com/atmob/keyboard/MainActivity.kt

@@ -1,5 +1,32 @@
 package com.atmob.keyboard
 
+import android.content.Intent
+import android.os.Handler
+import android.os.Looper
+import com.atmob.keyboard.util.JumpAppPageUtil
 import io.flutter.embedding.android.FlutterActivity
+import io.flutter.embedding.engine.FlutterEngine
 
-class MainActivity : FlutterActivity()
+class MainActivity : FlutterActivity() {
+    private val mMainHandler = Handler(Looper.getMainLooper())
+
+    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
+        super.configureFlutterEngine(flutterEngine)
+        mMainHandler.postDelayed({
+            JumpAppPageUtil.jumpAppPage(intent, flutterEngine)
+        }, 150)
+    }
+
+    override fun onNewIntent(intent: Intent) {
+        super.onNewIntent(intent)
+        setIntent(intent)
+        flutterEngine?.let {
+            JumpAppPageUtil.jumpAppPage(intent, it)
+        }
+    }
+
+    override fun onDestroy() {
+        super.onDestroy()
+        mMainHandler.removeCallbacksAndMessages(null)
+    }
+}

+ 58 - 0
android/app/src/main/kotlin/com/atmob/keyboard/util/JumpAppPageUtil.kt

@@ -0,0 +1,58 @@
+package com.atmob.keyboard.util
+
+import android.content.Intent
+import io.flutter.embedding.engine.FlutterEngine
+import io.flutter.plugin.common.MethodChannel
+
+/**
+ * Flutter页面跳转工具类
+ */
+class JumpAppPageUtil private constructor() {
+    companion object {
+        /**
+         * 路由地址
+         */
+        private const val KEY_PATH = "path"
+
+        /**
+         * 路由跳转参数
+         */
+        private const val KEY_ARGS = "args"
+
+        /**
+         * 通道名称,和Flutter端要一一对应
+         */
+        private const val FLUTTER_METHOD_CHANNEL_NAME = "keyboard_android"
+
+        /**
+         * Flutter的路由跳转方法
+         */
+        private const val FLUTTER_JUMP_APP_PAGE_METHOD = "jumpAppPage"
+
+        /**
+         * 跳转到Flutter页面
+         */
+        fun jumpAppPage(intent: Intent, engine: FlutterEngine) {
+            // 路由地址
+            val path = intent.getStringExtra(KEY_PATH) ?: ""
+            // 路由跳转参数,Json格式
+            val args = intent.getStringExtra(KEY_ARGS) ?: ""
+
+            if (path.isBlank()) {
+                return
+            }
+
+            val arguments = mapOf(
+                KEY_PATH to path,
+                KEY_ARGS to args
+            )
+
+            val methodChannel = MethodChannel(
+                engine.dartExecutor.binaryMessenger,
+                FLUTTER_METHOD_CHANNEL_NAME
+            )
+
+            methodChannel.invokeMethod(FLUTTER_JUMP_APP_PAGE_METHOD, arguments)
+        }
+    }
+}

+ 28 - 0
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/constant/FlutterHostConstants.kt

@@ -0,0 +1,28 @@
+package com.atmob.keyboard_android.constant
+
+/**
+ * Flutter宿主的常量
+ */
+interface FlutterHostConstants {
+    companion object {
+        /**
+         * 路由地址
+         */
+        const val KEY_PATH = "path"
+
+        /**
+         * 路由跳转参数
+         */
+        const val KEY_ARGS = "args"
+
+        /**
+         * 自定义人设页
+         */
+        const val PAGE_CHARACTER_CUSTOM = "/characterCustom"
+
+        /**
+         * VIP商店
+         */
+        const val PAGE_STORE = "/store"
+    }
+}

+ 49 - 0
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/FlutterPageLaunchUtil.kt

@@ -0,0 +1,49 @@
+package com.atmob.keyboard_android.util
+
+import android.content.Intent
+import com.atmob.keyboard_android.constant.FlutterHostConstants
+import com.blankj.utilcode.util.IntentUtils
+import com.blankj.utilcode.util.Utils
+import java.io.Serializable
+
+/**
+ * Flutter页面跳转工具类,插件调起某个Flutter页面
+ */
+class FlutterPageLaunchUtil private constructor() {
+    companion object {
+        /**
+         * 调起Flutter的某个页面
+         *
+         * @param path Flutter的页面路由
+         * @param args 页面跳转参数
+         */
+        fun jumpFlutterPage(
+            path: String,
+            args: Map<String, Serializable?> = mapOf<String, Serializable>()
+        ) {
+            val params = mutableMapOf<String, Serializable?>()
+            // 添加Flutter路由路径
+            params.put(FlutterHostConstants.KEY_PATH, path)
+            // 添加跳转参数
+            params.put(FlutterHostConstants.KEY_ARGS, JsonUtil.toJson(args))
+            startLaunchActivity(params)
+        }
+
+        /**
+         * 跳转到App的启动页
+         *
+         * @param params 跳转参数
+         */
+        private fun startLaunchActivity(params: Map<String, Serializable?> = mapOf<String, Serializable>()) {
+            val intent: Intent? = IntentUtils.getLaunchAppIntent(Utils.getApp().packageName)
+            if (intent == null) {
+                return
+            }
+            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+            for (entry in params) {
+                intent.putExtra(entry.key, entry.value)
+            }
+            Utils.getApp().startActivity(intent)
+        }
+    }
+}

+ 0 - 30
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/LaunchUtil.kt

@@ -1,30 +0,0 @@
-package com.atmob.keyboard_android.util
-
-import android.content.Intent
-import com.blankj.utilcode.util.IntentUtils
-import com.blankj.utilcode.util.Utils
-import java.io.Serializable
-
-/**
- * 跳转工具类
- */
-class LaunchUtil private constructor() {
-    companion object {
-        /**
-         * 跳转到App的启动页
-         *
-         * @param args 跳转参数
-         */
-        fun startLaunchActivity(args: Map<String, Serializable?> = mapOf<String, Serializable>()) {
-            val intent: Intent? = IntentUtils.getLaunchAppIntent(Utils.getApp().packageName)
-            if (intent == null) {
-                return
-            }
-            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
-            for (entry in args) {
-                intent.putExtra(entry.key, entry.value)
-            }
-            Utils.getApp().startActivity(intent)
-        }
-    }
-}

+ 7 - 6
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/util/bridge/FlutterBridgeManager.kt

@@ -1,8 +1,9 @@
 package com.atmob.keyboard_android.util.bridge
 
+import com.atmob.keyboard_android.constant.FlutterHostConstants
 import com.atmob.keyboard_android.constant.PluginConfig
 import com.atmob.keyboard_android.enums.FlutterMethod
-import com.atmob.keyboard_android.util.LaunchUtil
+import com.atmob.keyboard_android.util.FlutterPageLaunchUtil
 import com.atmob.keyboard_android.util.bridge.model.base.EmptyResp
 import com.atmob.keyboard_android.util.bridge.model.req.SuperReplyReq
 import com.atmob.keyboard_android.util.bridge.model.req.SuperSpeakReq
@@ -61,23 +62,23 @@ object FlutterBridgeManager : IBridgeApi {
     }
 
     override fun jump2LoginPage() {
-        LaunchUtil.startLaunchActivity()
+        FlutterPageLaunchUtil.jumpFlutterPage("/login")
     }
 
     override fun jump2CustomCharacterPage() {
-        LaunchUtil.startLaunchActivity()
+        FlutterPageLaunchUtil.jumpFlutterPage(FlutterHostConstants.PAGE_CHARACTER_CUSTOM)
     }
 
     override fun jump2CharacterMarketPage() {
-        LaunchUtil.startLaunchActivity()
+        FlutterPageLaunchUtil.jumpFlutterPage("/characterMarket")
     }
 
     override fun jump2VipStore() {
-        LaunchUtil.startLaunchActivity()
+        FlutterPageLaunchUtil.jumpFlutterPage(FlutterHostConstants.PAGE_STORE)
     }
 
     override fun jump2IntimacyPage() {
-        LaunchUtil.startLaunchActivity()
+        FlutterPageLaunchUtil.jumpFlutterPage("/intimacy")
     }
 
     override fun getKeyboardList(