mirror of
https://github.com/OpenFlutter/fluwx.git
synced 2025-05-17 15:25:59 +08:00
refactor android side
This commit is contained in:
@ -62,7 +62,7 @@ dependencies:
|
||||
> V4开始,iOS中的url_scheme,universal_link, LSApplicationQueriesSchemes可以不必开发者手动配动。只需在`pubspec.yaml`
|
||||
> 中填写即可。
|
||||
|
||||
- app_id. 必填. 它将用于生成iOS的url_scheme以及在Android端冷启动时,重新初始化WxApi。
|
||||
- app_id. 可选但推荐填写. 它将用于生成iOS的url_scheme以及在Android端冷启动时,重新初始化WxApi。
|
||||
- debug_logging. 可选. 把它设置成`true`可以开启日志。
|
||||
- flutter_activity. 可选. 这个通常是用于Android的冷启动。如果不设置任何值,`Fluwx`将尝试启动launcher activity.
|
||||
- universal_link. iOS 必填. 它将用自动配置universal_link。
|
||||
|
@ -33,14 +33,13 @@ android {
|
||||
compileSdk 31
|
||||
|
||||
sourceSets {
|
||||
main.java.srcDirs += ['src/main/kotlin',"${buildDir}/generated/src/kotlin"]
|
||||
main.java.srcDirs += ['src/main/kotlin', "${buildDir}/generated/src/kotlin"]
|
||||
test.java.srcDirs += 'src/test/kotlin'
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 16
|
||||
consumerProguardFiles 'consumer-proguard-rules.txt'
|
||||
manifestPlaceholders = loadManifestPlaceholder(projectYaml)
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@ -75,36 +74,6 @@ Map loadPubspec() {
|
||||
return projectConfig
|
||||
}
|
||||
|
||||
static def loadManifestPlaceholder(Map projectConfig) {
|
||||
String interruptWxRequest = "true"
|
||||
String flutterActivity = ""
|
||||
String debugLogging = "disabled"
|
||||
Map fluwx = (Map) projectConfig.get("fluwx")
|
||||
if (fluwx) {
|
||||
Map android = (Map) fluwx.get("android")
|
||||
if (android) {
|
||||
def iwr = android.get("interrupt_wx_request")
|
||||
if (iwr) {
|
||||
interruptWxRequest = (String) iwr
|
||||
}
|
||||
|
||||
def activity = android.get("flutter_activity")
|
||||
if (activity) {
|
||||
flutterActivity = (String) activity
|
||||
}
|
||||
}
|
||||
|
||||
def logging = fluwx.get("debug_logging")
|
||||
if (logging && logging == "true") {
|
||||
debugLogging = "true"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ["InterruptWeChatRequestByFluwx": interruptWxRequest,
|
||||
"FluwxFlutterActivity" : flutterActivity,
|
||||
"WeChatDebugLogging" : debugLogging]
|
||||
}
|
||||
|
||||
tasks.register("generateFluwxHelperFile") {
|
||||
Map config = loadPubspec()
|
||||
@ -116,16 +85,44 @@ tasks.register("generateFluwxHelperFile") {
|
||||
appId = ""
|
||||
}
|
||||
File generateFolder = new File("${buildDir}/generated/src/kotlin/com/jarvan/fluwx")
|
||||
String enableLogging = "false"
|
||||
String interruptWeChatRequestByFluwx = "true"
|
||||
String flutterActivity = ""
|
||||
Map android = (Map) fluwx.get("android")
|
||||
if (android) {
|
||||
def iwr = android.get("interrupt_wx_request")
|
||||
if (iwr && iwr == "true" || iwr == "false") {
|
||||
interruptWeChatRequestByFluwx = (String) iwr
|
||||
}
|
||||
|
||||
def activity = android.get("flutter_activity")
|
||||
if (activity) {
|
||||
flutterActivity = (String) activity
|
||||
}
|
||||
}
|
||||
|
||||
def logging = fluwx.get("debug_logging")
|
||||
if (logging && logging == "true" || logging == "false") {
|
||||
enableLogging = (String) logging
|
||||
}
|
||||
|
||||
|
||||
String template = "package com.jarvan.fluwx\n" +
|
||||
"\n" +
|
||||
"// auto generated\n" +
|
||||
"object FluwxHelper {\n" +
|
||||
" val appId:String =\"&&PLACEHOLDER&&\";\n" +
|
||||
"internal object FluwxConfigurations {\n" +
|
||||
" val flutterActivity: String = \"&&flutterActivity&&\"\n" +
|
||||
" val enableLogging: Boolean = &&enableLogging&&\n" +
|
||||
" val interruptWeChatRequestByFluwx: Boolean = &&interruptWeChatRequestByFluwx&&\n" +
|
||||
"}"
|
||||
if (!generateFolder.exists()) {
|
||||
generateFolder.mkdirs()
|
||||
}
|
||||
|
||||
file("${generateFolder.absolutePath}/FluwxHelper.kt").text = template.replace("&&PLACEHOLDER&&", appId)
|
||||
String source = template.replace("&&interruptWeChatRequestByFluwx&&", interruptWeChatRequestByFluwx)
|
||||
.replace("&&flutterActivity&&", flutterActivity)
|
||||
.replace("&&enableLogging&&", enableLogging)
|
||||
file("${generateFolder.absolutePath}/FluwxConfigurations.kt").text = source
|
||||
|
||||
}
|
||||
|
||||
|
32
android/gen.gradle.kts
Normal file
32
android/gen.gradle.kts
Normal file
@ -0,0 +1,32 @@
|
||||
abstract class GenFluwxHelperTask : DefaultTask() {
|
||||
@get:Incremental
|
||||
@get:PathSensitive(PathSensitivity.NAME_ONLY)
|
||||
@get:InputDirectory
|
||||
abstract val inputDir: DirectoryProperty
|
||||
|
||||
@get:OutputDirectory
|
||||
abstract val outputDir: DirectoryProperty
|
||||
|
||||
@get:Input
|
||||
abstract val inputProperty: Property<String>
|
||||
|
||||
@TaskAction
|
||||
fun execute(inputChanges: InputChanges) {
|
||||
println(
|
||||
if (inputChanges.isIncremental) "Executing incrementally"
|
||||
else "Executing non-incrementally"
|
||||
)
|
||||
|
||||
inputChanges.getFileChanges(inputDir).forEach { change ->
|
||||
if (change.fileType == FileType.DIRECTORY) return@forEach
|
||||
|
||||
println("${change.changeType}: ${change.normalizedPath}")
|
||||
val targetFile = outputDir.file(change.normalizedPath).get().asFile
|
||||
// if (change.changeType == ChangeType.REMOVED) {
|
||||
// targetFile.delete()
|
||||
// } else {
|
||||
// targetFile.writeText(change.file.readText().reversed())
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
@ -12,17 +12,6 @@
|
||||
|
||||
<application>
|
||||
|
||||
<meta-data
|
||||
android:name="InterruptWeChatRequestByFluwx"
|
||||
android:value="${InterruptWeChatRequestByFluwx}" />
|
||||
|
||||
<meta-data
|
||||
android:name="WeChatDebugLogging"
|
||||
android:value="${WeChatDebugLogging}" />
|
||||
<meta-data
|
||||
android:name="FluwxFlutterActivity"
|
||||
android:value="${FluwxFlutterActivity}" />
|
||||
|
||||
<activity
|
||||
android:name=".wxapi.FluwxWXEntryActivity"
|
||||
android:exported="false"
|
||||
|
@ -11,6 +11,9 @@ import com.jarvan.fluwx.handlers.PermissionHandler
|
||||
import com.jarvan.fluwx.handlers.WXAPiHandler
|
||||
import com.jarvan.fluwx.utils.KEY_FLUWX_REQUEST_INFO_EXT_MSG
|
||||
import com.jarvan.fluwx.utils.WXApiUtils
|
||||
import com.jarvan.fluwx.utils.readWeChatCallbackIntent
|
||||
import com.tencent.mm.opensdk.modelbase.BaseReq
|
||||
import com.tencent.mm.opensdk.modelbase.BaseResp
|
||||
import com.tencent.mm.opensdk.modelbiz.ChooseCardFromWXCardPackage
|
||||
import com.tencent.mm.opensdk.modelbiz.OpenRankList
|
||||
import com.tencent.mm.opensdk.modelbiz.OpenWebview
|
||||
@ -19,8 +22,15 @@ import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
|
||||
import com.tencent.mm.opensdk.modelbiz.WXOpenBusinessView
|
||||
import com.tencent.mm.opensdk.modelbiz.WXOpenBusinessWebview
|
||||
import com.tencent.mm.opensdk.modelbiz.WXOpenCustomerServiceChat
|
||||
import com.tencent.mm.opensdk.modelmsg.LaunchFromWX
|
||||
import com.tencent.mm.opensdk.modelmsg.SendAuth
|
||||
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
|
||||
import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX
|
||||
import com.tencent.mm.opensdk.modelpay.PayReq
|
||||
import com.tencent.mm.opensdk.modelpay.PayResp
|
||||
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
|
||||
import com.tencent.mm.opensdk.openapi.SendReqCallback
|
||||
import com.tencent.mm.opensdk.utils.ILog
|
||||
import io.flutter.embedding.engine.plugins.FlutterPlugin
|
||||
import io.flutter.embedding.engine.plugins.activity.ActivityAware
|
||||
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
|
||||
@ -34,14 +44,39 @@ import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
/** FluwxPlugin */
|
||||
class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
|
||||
PluginRegistry.NewIntentListener {
|
||||
PluginRegistry.NewIntentListener, IWXAPIEventHandler {
|
||||
companion object {
|
||||
var callingChannel: MethodChannel? = null
|
||||
|
||||
// 主动获取的启动参数
|
||||
var extMsg: String? = null
|
||||
}
|
||||
|
||||
private val errStr = "errStr"
|
||||
private val errCode = "errCode"
|
||||
private val openId = "openId"
|
||||
private val type = "type"
|
||||
|
||||
private val weChatLogger = object : ILog {
|
||||
|
||||
override fun d(p0: String?, p1: String?) {
|
||||
logToFlutter(p0, p1)
|
||||
}
|
||||
|
||||
override fun i(p0: String?, p1: String?) {
|
||||
logToFlutter(p0, p1)
|
||||
}
|
||||
|
||||
override fun e(p0: String?, p1: String?) {
|
||||
logToFlutter(p0, p1)
|
||||
}
|
||||
|
||||
override fun v(p0: String?, p1: String?) {
|
||||
logToFlutter(p0, p1)
|
||||
}
|
||||
|
||||
override fun w(p0: String?, p1: String?) {
|
||||
logToFlutter(p0, p1)
|
||||
}
|
||||
}
|
||||
private var shareHandler: FluwxShareHandler? = null
|
||||
|
||||
private var authHandler: FluwxAuthHandler? = null
|
||||
@ -53,16 +88,9 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
|
||||
|
||||
private var activityPluginBinding: ActivityPluginBinding? = null
|
||||
|
||||
private fun handelIntent(intent: Intent) {
|
||||
intent.getStringExtra(KEY_FLUWX_REQUEST_INFO_EXT_MSG)?.let {
|
||||
extMsg = it
|
||||
}
|
||||
}
|
||||
|
||||
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
|
||||
val channel = MethodChannel(flutterPluginBinding.binaryMessenger, "com.jarvanmo/fluwx")
|
||||
channel.setMethodCallHandler(this)
|
||||
val applicationContext = flutterPluginBinding.applicationContext
|
||||
fluwxChannel = channel
|
||||
context = flutterPluginBinding.applicationContext
|
||||
authHandler = FluwxAuthHandler(channel)
|
||||
@ -72,9 +100,14 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
|
||||
}
|
||||
|
||||
override fun onMethodCall(call: MethodCall, result: Result) {
|
||||
callingChannel = fluwxChannel
|
||||
when {
|
||||
call.method == "registerApp" -> WXAPiHandler.registerApp(call, result, context)
|
||||
call.method == "registerApp" -> {
|
||||
WXAPiHandler.registerApp(call, result, context)
|
||||
if (FluwxConfigurations.enableLogging) {
|
||||
WXAPiHandler.wxApi?.setLogImpl(weChatLogger)
|
||||
}
|
||||
}
|
||||
|
||||
call.method == "sendAuth" -> authHandler?.sendAuth(call, result)
|
||||
call.method == "authByQRCode" -> authHandler?.authByQRCode(call, result)
|
||||
call.method == "stopAuthByQRCode" -> authHandler?.stopAuthByQRCode(result)
|
||||
@ -109,7 +142,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
|
||||
private fun attemptToResumeMsgFromWx(result: Result) {
|
||||
if (attemptToResumeMsgFromWxFlag.compareAndSet(false, true)) {
|
||||
activityPluginBinding?.activity?.intent?.let {
|
||||
FluwxRequestHandler.handleRequestInfoFromIntent(it)
|
||||
letWeChatHandleIntent(it)
|
||||
}
|
||||
result.success(null)
|
||||
} else {
|
||||
@ -141,7 +174,7 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
|
||||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
|
||||
shareHandler?.onDestroy()
|
||||
authHandler?.removeAllListeners()
|
||||
activityPluginBinding = null
|
||||
@ -153,15 +186,12 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
|
||||
|
||||
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
|
||||
shareHandler?.permissionHandler = PermissionHandler(binding.activity)
|
||||
handelIntent(binding.activity.intent)
|
||||
FluwxRequestHandler.handleRequestInfoFromIntent(binding.activity.intent)
|
||||
}
|
||||
|
||||
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
|
||||
// WXAPiHandler.setContext(binding.activity.applicationContext)
|
||||
activityPluginBinding = binding
|
||||
handelIntent(binding.activity.intent)
|
||||
FluwxRequestHandler.handleRequestInfoFromIntent(binding.activity.intent)
|
||||
binding.addOnNewIntentListener(this)
|
||||
shareHandler?.permissionHandler = PermissionHandler(binding.activity)
|
||||
}
|
||||
|
||||
@ -317,8 +347,197 @@ class FluwxPlugin : FlutterPlugin, MethodCallHandler, ActivityAware,
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent): Boolean {
|
||||
handelIntent(intent)
|
||||
return false
|
||||
return letWeChatHandleIntent(intent)
|
||||
}
|
||||
|
||||
private fun letWeChatHandleIntent(intent: Intent): Boolean =
|
||||
intent.readWeChatCallbackIntent()?.let {
|
||||
WXAPiHandler.wxApi?.handleIntent(it, this) ?: false
|
||||
} ?: run {
|
||||
false
|
||||
}
|
||||
|
||||
override fun onReq(req: BaseReq?) {
|
||||
activityPluginBinding?.activity?.let { activity ->
|
||||
req?.let {
|
||||
if (FluwxConfigurations.interruptWeChatRequestByFluwx) {
|
||||
when (req) {
|
||||
is ShowMessageFromWX.Req -> handleWXShowMessageFromWX(req)
|
||||
is LaunchFromWX.Req -> handleWXLaunchFromWX(req)
|
||||
else -> {}
|
||||
}
|
||||
} else {
|
||||
FluwxRequestHandler.customOnReqDelegate?.invoke(req, activity)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun handleWXShowMessageFromWX(req: ShowMessageFromWX.Req) {
|
||||
val result = mapOf(
|
||||
"extMsg" to req.message.messageExt,
|
||||
"messageAction" to req.message.messageAction,
|
||||
"description" to req.message.description,
|
||||
"lang" to req.lang,
|
||||
"description" to req.country,
|
||||
)
|
||||
|
||||
extMsg = req.message.messageExt
|
||||
fluwxChannel?.invokeMethod("onWXShowMessageFromWX", result)
|
||||
}
|
||||
|
||||
private fun handleWXLaunchFromWX(req: LaunchFromWX.Req) {
|
||||
val result = mapOf(
|
||||
"extMsg" to req.messageExt,
|
||||
"messageAction" to req.messageAction,
|
||||
"lang" to req.lang,
|
||||
"country" to req.country,
|
||||
)
|
||||
|
||||
fluwxChannel?.invokeMethod("onWXLaunchFromWX", result)
|
||||
}
|
||||
|
||||
override fun onResp(response: BaseResp?) {
|
||||
when (response) {
|
||||
is SendAuth.Resp -> handleAuthResponse(response)
|
||||
is SendMessageToWX.Resp -> handleSendMessageResp(response)
|
||||
is PayResp -> handlePayResp(response)
|
||||
is WXLaunchMiniProgram.Resp -> handleLaunchMiniProgramResponse(response)
|
||||
is SubscribeMessage.Resp -> handleSubscribeMessage(response)
|
||||
is WXOpenBusinessWebview.Resp -> handlerWXOpenBusinessWebviewResponse(response)
|
||||
is WXOpenCustomerServiceChat.Resp -> handlerWXOpenCustomerServiceChatResponse(response)
|
||||
is WXOpenBusinessView.Resp -> handleWXOpenBusinessView(response)
|
||||
is ChooseCardFromWXCardPackage.Resp -> handleWXOpenInvoiceResponse(response)
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleWXOpenInvoiceResponse(response: ChooseCardFromWXCardPackage.Resp) {
|
||||
val result = mapOf(
|
||||
"cardItemList" to response.cardItemList,
|
||||
"transaction" to response.transaction,
|
||||
"openid" to response.openId,
|
||||
errStr to response.errStr,
|
||||
type to response.type,
|
||||
errCode to response.errCode
|
||||
)
|
||||
|
||||
fluwxChannel?.invokeMethod("onOpenWechatInvoiceResponse", result)
|
||||
}
|
||||
|
||||
private fun handleWXOpenBusinessView(response: WXOpenBusinessView.Resp) {
|
||||
val result = mapOf(
|
||||
"openid" to response.openId,
|
||||
"extMsg" to response.extMsg,
|
||||
"businessType" to response.businessType,
|
||||
errStr to response.errStr,
|
||||
type to response.type,
|
||||
errCode to response.errCode
|
||||
)
|
||||
|
||||
fluwxChannel?.invokeMethod("onOpenBusinessViewResponse", result)
|
||||
}
|
||||
|
||||
private fun handleSubscribeMessage(response: SubscribeMessage.Resp) {
|
||||
val result = mapOf(
|
||||
"openid" to response.openId,
|
||||
"templateId" to response.templateID,
|
||||
"action" to response.action,
|
||||
"reserved" to response.reserved,
|
||||
"scene" to response.scene,
|
||||
type to response.type
|
||||
)
|
||||
|
||||
fluwxChannel?.invokeMethod("onSubscribeMsgResp", result)
|
||||
}
|
||||
|
||||
private fun handleLaunchMiniProgramResponse(response: WXLaunchMiniProgram.Resp) {
|
||||
val result = mutableMapOf(
|
||||
errStr to response.errStr,
|
||||
type to response.type,
|
||||
errCode to response.errCode,
|
||||
openId to response.openId
|
||||
)
|
||||
|
||||
response.extMsg?.let {
|
||||
result["extMsg"] = response.extMsg
|
||||
}
|
||||
|
||||
fluwxChannel?.invokeMethod("onLaunchMiniProgramResponse", result)
|
||||
}
|
||||
|
||||
private fun handlePayResp(response: PayResp) {
|
||||
val result = mapOf(
|
||||
"prepayId" to response.prepayId,
|
||||
"returnKey" to response.returnKey,
|
||||
"extData" to response.extData,
|
||||
errStr to response.errStr,
|
||||
type to response.type,
|
||||
errCode to response.errCode
|
||||
)
|
||||
fluwxChannel?.invokeMethod("onPayResponse", result)
|
||||
}
|
||||
|
||||
private fun handleSendMessageResp(response: SendMessageToWX.Resp) {
|
||||
val result = mapOf(
|
||||
errStr to response.errStr,
|
||||
type to response.type,
|
||||
errCode to response.errCode,
|
||||
openId to response.openId
|
||||
)
|
||||
|
||||
fluwxChannel?.invokeMethod("onShareResponse", result)
|
||||
}
|
||||
|
||||
private fun handleAuthResponse(response: SendAuth.Resp) {
|
||||
val result = mapOf(
|
||||
errCode to response.errCode,
|
||||
"code" to response.code,
|
||||
"state" to response.state,
|
||||
"lang" to response.lang,
|
||||
"country" to response.country,
|
||||
errStr to response.errStr,
|
||||
openId to response.openId,
|
||||
"url" to response.url,
|
||||
type to response.type
|
||||
)
|
||||
|
||||
fluwxChannel?.invokeMethod("onAuthResponse", result)
|
||||
}
|
||||
|
||||
|
||||
private fun handlerWXOpenBusinessWebviewResponse(response: WXOpenBusinessWebview.Resp) {
|
||||
val result = mapOf(
|
||||
errCode to response.errCode,
|
||||
"businessType" to response.businessType,
|
||||
"resultInfo" to response.resultInfo,
|
||||
errStr to response.errStr,
|
||||
openId to response.openId,
|
||||
type to response.type
|
||||
)
|
||||
|
||||
fluwxChannel?.invokeMethod("onWXOpenBusinessWebviewResponse", result)
|
||||
}
|
||||
|
||||
private fun handlerWXOpenCustomerServiceChatResponse(response: WXOpenCustomerServiceChat.Resp) {
|
||||
val result = mapOf(
|
||||
errCode to response.errCode,
|
||||
errStr to response.errStr,
|
||||
openId to response.openId,
|
||||
type to response.type
|
||||
)
|
||||
|
||||
fluwxChannel?.invokeMethod("onWXOpenCustomerServiceChatResponse", result)
|
||||
}
|
||||
|
||||
private fun logToFlutter(tag: String?, message: String?) {
|
||||
fluwxChannel?.invokeMethod(
|
||||
"wechatLog", mapOf(
|
||||
"detail" to "$tag : $message"
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -16,119 +16,10 @@
|
||||
package com.jarvan.fluwx.handlers
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.core.content.ContextCompat.startActivity
|
||||
import com.jarvan.fluwx.FluwxPlugin
|
||||
import com.jarvan.fluwx.utils.KEY_FLUWX_REQUEST_INFO_BUNDLE
|
||||
import com.jarvan.fluwx.utils.KEY_FLUWX_REQUEST_INFO_EXT_MSG
|
||||
import com.jarvan.fluwx.utils.startFlutterActivity
|
||||
import com.tencent.mm.opensdk.modelbase.BaseReq
|
||||
import com.tencent.mm.opensdk.modelmsg.LaunchFromWX
|
||||
import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX
|
||||
import java.security.cert.Extension
|
||||
|
||||
|
||||
object FluwxRequestHandler {
|
||||
|
||||
var customOnReqDelegate: ((baseReq: BaseReq, activity: Activity) -> Unit)? = null
|
||||
|
||||
fun handleRequestInfoFromIntent(intent: Intent) {
|
||||
intent.getBundleExtra(KEY_FLUWX_REQUEST_INFO_BUNDLE)?.run {
|
||||
val type = getInt("_wxapi_command_type", -9999)
|
||||
if (type == 4) {
|
||||
handleShowMessageFromWXBundle(this)
|
||||
} else if (type == 6) {
|
||||
handleWXLaunchFromWXBundle(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleShowMessageFromWXBundle(bundle: Bundle) =
|
||||
handleWXShowMessageFromWX(ShowMessageFromWX.Req(bundle))
|
||||
|
||||
private fun handleWXLaunchFromWXBundle(bundle: Bundle) =
|
||||
handleWXLaunchFromWX(LaunchFromWX.Req(bundle))
|
||||
|
||||
private fun handleRequest(req: BaseReq) {
|
||||
when (req) {
|
||||
is ShowMessageFromWX.Req -> handleWXShowMessageFromWX(req)
|
||||
is LaunchFromWX.Req -> handleWXLaunchFromWX(req)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleWXShowMessageFromWX(req: ShowMessageFromWX.Req) {
|
||||
val result = mapOf(
|
||||
"extMsg" to req.message.messageExt,
|
||||
"messageAction" to req.message.messageAction,
|
||||
"description" to req.message.description,
|
||||
"lang" to req.lang,
|
||||
"description" to req.country,
|
||||
)
|
||||
|
||||
FluwxPlugin.extMsg = req.message.messageExt
|
||||
FluwxPlugin.callingChannel?.invokeMethod("onWXShowMessageFromWX", result)
|
||||
}
|
||||
|
||||
private fun handleWXLaunchFromWX(req: LaunchFromWX.Req) {
|
||||
val result = mapOf(
|
||||
"extMsg" to req.messageExt,
|
||||
"messageAction" to req.messageAction,
|
||||
"lang" to req.lang,
|
||||
"country" to req.country,
|
||||
)
|
||||
|
||||
FluwxPlugin.callingChannel?.invokeMethod("onWXLaunchFromWX", result)
|
||||
}
|
||||
|
||||
private fun defaultOnReqDelegate(baseReq: BaseReq, activity: Activity) {
|
||||
// FIXME: 可能是官方的Bug,从微信拉起APP的Intent类型不对,无法跳转回Flutter Activity
|
||||
// 稳定复现场景:微信版本为7.0.5,小程序SDK为2.7.7
|
||||
|
||||
// com.tencent.mm.opensdk.constants.ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX = 4
|
||||
if (!WXAPiHandler.coolBoot) {
|
||||
handleRequest(baseReq)
|
||||
activity.startFlutterActivity()
|
||||
} else {
|
||||
when (baseReq) {
|
||||
is ShowMessageFromWX.Req -> {
|
||||
activity.startFlutterActivity(
|
||||
wxRequestBundle = Bundle().apply {
|
||||
baseReq.toBundle(this)
|
||||
},
|
||||
bundle = Bundle().apply {
|
||||
putString(
|
||||
KEY_FLUWX_REQUEST_INFO_EXT_MSG,
|
||||
baseReq.message.messageExt
|
||||
)
|
||||
})
|
||||
WXAPiHandler.coolBoot = false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fun onReq(baseReq: BaseReq, activity: Activity) {
|
||||
try {
|
||||
val packageManager = activity.packageManager
|
||||
val appInfo = packageManager.getApplicationInfo(
|
||||
activity.packageName,
|
||||
PackageManager.GET_META_DATA
|
||||
)
|
||||
val defaultHandle = appInfo.metaData.getBoolean("InterruptWeChatRequestByFluwx", true)
|
||||
if (defaultHandle) {
|
||||
defaultOnReqDelegate(baseReq, activity)
|
||||
} else {
|
||||
customOnReqDelegate?.invoke(baseReq, activity)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.i("Fluwx", "can't load meta-data InterruptWeChatRequestByFluwx")
|
||||
}
|
||||
}
|
||||
}
|
@ -1,156 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The OpenFlutter Organization
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.jarvan.fluwx.handlers
|
||||
|
||||
import com.jarvan.fluwx.FluwxPlugin
|
||||
import com.tencent.mm.opensdk.modelbase.BaseResp
|
||||
import com.tencent.mm.opensdk.modelbiz.*
|
||||
import com.tencent.mm.opensdk.modelmsg.SendAuth
|
||||
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
|
||||
import com.tencent.mm.opensdk.modelpay.PayResp
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
|
||||
object FluwxResponseHandler {
|
||||
|
||||
private const val errStr = "errStr"
|
||||
private const val errCode = "errCode"
|
||||
private const val openId = "openId"
|
||||
private const val type = "type"
|
||||
|
||||
fun handleResponse(response: BaseResp) {
|
||||
when (response) {
|
||||
is SendAuth.Resp -> handleAuthResponse(response)
|
||||
is SendMessageToWX.Resp -> handleSendMessageResp(response)
|
||||
is PayResp -> handlePayResp(response)
|
||||
is WXLaunchMiniProgram.Resp -> handleLaunchMiniProgramResponse(response)
|
||||
is SubscribeMessage.Resp -> handleSubscribeMessage(response)
|
||||
is WXOpenBusinessWebview.Resp -> handlerWXOpenBusinessWebviewResponse(response)
|
||||
is WXOpenCustomerServiceChat.Resp -> handlerWXOpenCustomerServiceChatResponse(response)
|
||||
is WXOpenBusinessView.Resp -> handleWXOpenBusinessView(response)
|
||||
is ChooseCardFromWXCardPackage.Resp -> handleWXOpenInvoiceResponse(response)
|
||||
}
|
||||
}
|
||||
private fun handleWXOpenInvoiceResponse(response: ChooseCardFromWXCardPackage.Resp) {
|
||||
val result = mapOf(
|
||||
"cardItemList" to response.cardItemList,
|
||||
"transaction" to response.transaction,
|
||||
"openid" to response.openId,
|
||||
errStr to response.errStr,
|
||||
type to response.type,
|
||||
errCode to response.errCode)
|
||||
|
||||
FluwxPlugin.callingChannel?.invokeMethod("onOpenWechatInvoiceResponse", result)
|
||||
}
|
||||
|
||||
private fun handleWXOpenBusinessView(response: WXOpenBusinessView.Resp) {
|
||||
val result = mapOf(
|
||||
"openid" to response.openId,
|
||||
"extMsg" to response.extMsg,
|
||||
"businessType" to response.businessType,
|
||||
errStr to response.errStr,
|
||||
type to response.type,
|
||||
errCode to response.errCode)
|
||||
|
||||
FluwxPlugin.callingChannel?.invokeMethod("onOpenBusinessViewResponse", result)
|
||||
}
|
||||
|
||||
private fun handleSubscribeMessage(response: SubscribeMessage.Resp) {
|
||||
val result = mapOf(
|
||||
"openid" to response.openId,
|
||||
"templateId" to response.templateID,
|
||||
"action" to response.action,
|
||||
"reserved" to response.reserved,
|
||||
"scene" to response.scene,
|
||||
type to response.type)
|
||||
|
||||
FluwxPlugin.callingChannel?.invokeMethod("onSubscribeMsgResp", result)
|
||||
}
|
||||
|
||||
private fun handleLaunchMiniProgramResponse(response: WXLaunchMiniProgram.Resp) {
|
||||
val result = mutableMapOf(
|
||||
errStr to response.errStr,
|
||||
type to response.type,
|
||||
errCode to response.errCode,
|
||||
openId to response.openId
|
||||
)
|
||||
|
||||
response.extMsg?.let {
|
||||
result["extMsg"] = response.extMsg
|
||||
}
|
||||
|
||||
FluwxPlugin.callingChannel?.invokeMethod("onLaunchMiniProgramResponse", result)
|
||||
}
|
||||
|
||||
private fun handlePayResp(response: PayResp) {
|
||||
val result = mapOf(
|
||||
"prepayId" to response.prepayId,
|
||||
"returnKey" to response.returnKey,
|
||||
"extData" to response.extData,
|
||||
errStr to response.errStr,
|
||||
type to response.type,
|
||||
errCode to response.errCode
|
||||
)
|
||||
FluwxPlugin.callingChannel?.invokeMethod("onPayResponse", result)
|
||||
}
|
||||
|
||||
private fun handleSendMessageResp(response: SendMessageToWX.Resp) {
|
||||
val result = mapOf(
|
||||
errStr to response.errStr,
|
||||
type to response.type,
|
||||
errCode to response.errCode,
|
||||
openId to response.openId)
|
||||
|
||||
FluwxPlugin.callingChannel?.invokeMethod("onShareResponse", result)
|
||||
}
|
||||
|
||||
private fun handleAuthResponse(response: SendAuth.Resp) {
|
||||
val result = mapOf(
|
||||
errCode to response.errCode,
|
||||
"code" to response.code,
|
||||
"state" to response.state,
|
||||
"lang" to response.lang,
|
||||
"country" to response.country,
|
||||
errStr to response.errStr,
|
||||
openId to response.openId,
|
||||
"url" to response.url,
|
||||
type to response.type)
|
||||
|
||||
FluwxPlugin.callingChannel?.invokeMethod("onAuthResponse", result)
|
||||
}
|
||||
|
||||
|
||||
private fun handlerWXOpenBusinessWebviewResponse(response: WXOpenBusinessWebview.Resp) {
|
||||
val result = mapOf(
|
||||
errCode to response.errCode,
|
||||
"businessType" to response.businessType,
|
||||
"resultInfo" to response.resultInfo,
|
||||
errStr to response.errStr,
|
||||
openId to response.openId,
|
||||
type to response.type)
|
||||
|
||||
FluwxPlugin.callingChannel?.invokeMethod("onWXOpenBusinessWebviewResponse", result)
|
||||
}
|
||||
|
||||
private fun handlerWXOpenCustomerServiceChatResponse(response: WXOpenCustomerServiceChat.Resp) {
|
||||
val result = mapOf(
|
||||
errCode to response.errCode,
|
||||
errStr to response.errStr,
|
||||
openId to response.openId,
|
||||
type to response.type)
|
||||
|
||||
FluwxPlugin.callingChannel?.invokeMethod("onWXOpenCustomerServiceChatResponse", result)
|
||||
}
|
||||
}
|
@ -30,7 +30,7 @@ import com.tencent.mm.opensdk.utils.ILog
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
|
||||
object WXAPiHandler : ILog {
|
||||
object WXAPiHandler {
|
||||
|
||||
var wxApi: IWXAPI? = null
|
||||
|
||||
@ -57,16 +57,6 @@ object WXAPiHandler : ILog {
|
||||
|
||||
fun registerApp(call: MethodCall, result: MethodChannel.Result, context: Context?) {
|
||||
|
||||
context?.let {
|
||||
with(it) {
|
||||
val appInfo =
|
||||
packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
|
||||
val enableLogging = appInfo.metaData.getString("WeChatDebugLogging", "")
|
||||
if (enableLogging == "true" && BuildConfig.DEBUG) {
|
||||
startLog()
|
||||
}
|
||||
}
|
||||
}
|
||||
if (call.argument<Boolean?>("android") == false) {
|
||||
return
|
||||
}
|
||||
@ -118,49 +108,11 @@ object WXAPiHandler : ILog {
|
||||
}
|
||||
|
||||
private fun registerWxAPIInternal(appId: String, context: Context) {
|
||||
with(context) {
|
||||
val appInfo =
|
||||
packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
|
||||
val enableLogging = appInfo.metaData.getString("WeChatDebugLogging", "")
|
||||
if (enableLogging == "true" && BuildConfig.DEBUG) {
|
||||
startLog()
|
||||
}
|
||||
}
|
||||
|
||||
val api = WXAPIFactory.createWXAPI(context.applicationContext, appId)
|
||||
registered = api.registerApp(appId)
|
||||
wxApi = api
|
||||
}
|
||||
|
||||
fun startLog() {
|
||||
wxApi?.setLogImpl(this)
|
||||
}
|
||||
|
||||
override fun d(p0: String?, p1: String?) {
|
||||
logToFlutter(p0,p1)
|
||||
}
|
||||
|
||||
override fun i(p0: String?, p1: String?) {
|
||||
logToFlutter(p0,p1)
|
||||
}
|
||||
|
||||
override fun e(p0: String?, p1: String?) {
|
||||
logToFlutter(p0,p1)
|
||||
}
|
||||
|
||||
override fun v(p0: String?, p1: String?) {
|
||||
logToFlutter(p0,p1)
|
||||
}
|
||||
|
||||
override fun w(p0: String?, p1: String?) {
|
||||
logToFlutter(p0,p1)
|
||||
}
|
||||
|
||||
private fun logToFlutter(tag:String?,message:String?){
|
||||
FluwxPlugin.callingChannel?.invokeMethod("wechatLog", mapOf(
|
||||
"detail" to "$tag : $message"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -7,49 +7,48 @@ import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import com.jarvan.fluwx.FluwxConfigurations
|
||||
|
||||
internal const val KEY_FLUWX_REQUEST_INFO_EXT_MSG = "KEY_FLUWX_REQUEST_INFO_EXT_MSG"
|
||||
internal const val KEY_FLUWX_REQUEST_INFO_BUNDLE = "KEY_FLUWX_REQUEST_INFO_BUNDLE"
|
||||
internal const val KEY_FLUWX_EXTRA = "KEY_FLUWX_EXTRA"
|
||||
internal const val FLAG_PAYLOAD_FROM_WECHAT = "FLAG_PAYLOAD_FROM_WECHAT"
|
||||
|
||||
internal fun Activity.startFlutterActivity(
|
||||
wxRequestBundle: Bundle? = null,
|
||||
bundle: Bundle? = null,
|
||||
extra: Intent,
|
||||
) {
|
||||
flutterActivityIntent()?.also { intent ->
|
||||
intent.addFluwxExtras()
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
|
||||
bundle?.let {
|
||||
intent.putExtras(it)
|
||||
}
|
||||
|
||||
wxRequestBundle?.let {
|
||||
intent.putExtra(KEY_FLUWX_REQUEST_INFO_BUNDLE, it)
|
||||
}
|
||||
|
||||
intent.putExtra(KEY_FLUWX_EXTRA, extra)
|
||||
intent.putExtra(FLAG_PAYLOAD_FROM_WECHAT, true)
|
||||
try {
|
||||
startActivity(intent)
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
Log.w("fluwx", "Can not start activity for Intent: $intent")
|
||||
} finally {
|
||||
finish()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal fun Context.flutterActivityIntent(): Intent? {
|
||||
val appInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
|
||||
val flutterActivity = appInfo.metaData.getString("FluwxFlutterActivity", "")
|
||||
return if (flutterActivity.isBlank()) {
|
||||
return if (FluwxConfigurations.flutterActivity.isBlank()) {
|
||||
packageManager.getLaunchIntentForPackage(packageName)
|
||||
} else {
|
||||
Intent().also {
|
||||
it.setClassName(this, "${packageName}.$flutterActivity")
|
||||
it.setClassName(this, "${packageName}.${FluwxConfigurations.flutterActivity}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun Intent.addFluwxExtras() {
|
||||
putExtra("fluwx_payload_from_fluwx", true)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun Intent.readWeChatCallbackIntent(): Intent? {
|
||||
return if (getBooleanExtra(FLAG_PAYLOAD_FROM_WECHAT, false)) {
|
||||
getParcelableExtra(KEY_FLUWX_EXTRA)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
@ -17,67 +17,24 @@ package com.jarvan.fluwx.wxapi
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import com.jarvan.fluwx.FluwxHelper
|
||||
import com.jarvan.fluwx.handlers.FluwxResponseHandler
|
||||
import com.jarvan.fluwx.handlers.FluwxRequestHandler
|
||||
import com.jarvan.fluwx.handlers.WXAPiHandler
|
||||
import com.jarvan.fluwx.utils.flutterActivityIntent
|
||||
import com.jarvan.fluwx.utils.startFlutterActivity
|
||||
import com.tencent.mm.opensdk.modelbase.BaseReq
|
||||
import com.tencent.mm.opensdk.modelbase.BaseResp
|
||||
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
|
||||
import io.flutter.Log
|
||||
|
||||
|
||||
open class FluwxWXEntryActivity : Activity(), IWXAPIEventHandler {
|
||||
open class FluwxWXEntryActivity : Activity() {
|
||||
|
||||
// IWXAPI 是第三方app和微信通信的openapi接口
|
||||
|
||||
public override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
try {
|
||||
if (!WXAPiHandler.wxApiRegistered) {
|
||||
val wechatAppId = FluwxHelper.appId
|
||||
if (wechatAppId.isNotBlank()) {
|
||||
WXAPiHandler.setupWxApi(wechatAppId,this)
|
||||
WXAPiHandler.coolBoot = true
|
||||
} else {
|
||||
Log.w("fluwx","can't load meta-data weChatAppId")
|
||||
}
|
||||
}
|
||||
WXAPiHandler.wxApi?.handleIntent(intent, this)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
this.startFlutterActivity()
|
||||
}
|
||||
startFlutterActivity(intent)
|
||||
finish()
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent) {
|
||||
super.onNewIntent(intent)
|
||||
|
||||
setIntent(intent)
|
||||
|
||||
try {
|
||||
WXAPiHandler.wxApi?.handleIntent(intent, this)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
this.startFlutterActivity()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
override fun onReq(baseReq: BaseReq) {
|
||||
// FIXME: 可能是官方的Bug,从微信拉起APP的Intent类型不对,无法跳转回Flutter Activity
|
||||
// 稳定复现场景:微信版本为7.0.5,小程序SDK为2.7.7
|
||||
FluwxRequestHandler.onReq(baseReq,this)
|
||||
}
|
||||
|
||||
// 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法
|
||||
override fun onResp(resp: BaseResp) {
|
||||
FluwxResponseHandler.handleResponse(resp)
|
||||
startFlutterActivity(intent)
|
||||
finish()
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user