package com.donut.wxf1a06dafa3350688 import android.app.Activity import android.app.Application import android.content.Intent import android.os.Bundle import android.os.Process import androidx.core.app.ActivityCompat import androidx.core.content.PermissionChecker import androidx.lifecycle.Observer import com.donut.plugin.activity.MainProcessActivity import com.donut.plugin.activity.PermissionsActivity import com.donut.plugin.bean.DataBean import com.donut.plugin.util.BaseConsts import com.donut.plugin.util.DeviceUuidFactory import com.donut.plugin.util.EfunboxUtil import com.donut.plugin.util.LiveEventBusKeys import com.donut.plugin.util.Logger import com.donut.plugin.util.UMengUtil import com.jeremyliao.liveeventbus.LiveEventBus import com.tencent.luggage.wxa.SaaA.plugin.AsyncJsApi import com.tencent.luggage.wxa.SaaA.plugin.NativePluginInterface import com.tencent.luggage.wxa.SaaA.plugin.SyncJsApi import org.json.JSONObject import java.lang.reflect.Method class TestNativePlugin : NativePluginInterface { private val TAG = "TestNativePlugin" var mPermissionsLEBCallBack: Observer? = null; override fun getPluginID(): String { return BuildConfig.PLUGIN_ID } /** * 初始化application * */ @AsyncJsApi(methodName = "initPlugin") fun initPluginMethod(data: JSONObject?, callback: (data: Any) -> Unit, activity: Activity) { try { var code = data!!.getString("channelCode"); val clazz = Class.forName("android.app.ActivityThread") val method: Method = clazz.getMethod("currentApplication") BaseConsts.mBaseApplication = method.invoke(null) as Application; BaseConsts.mTopActivity = activity; LiveEventBus .config() .autoClear(true) .lifecycleObserverAlwaysActive(true); Logger.e("BaseConsts.mTopActivity::${BaseConsts.mTopActivity.localClassName}") Logger.e("BaseConsts.mBaseApplication::${BaseConsts.mBaseApplication.javaClass.name}") Logger.e("渠道号::${code}") DeviceUuidFactory(BaseConsts.mBaseApplication); BaseConsts.mBaseApplication.registerActivityLifecycleCallbacks( activityLifecycleCallbacks ) //注释跳转主进程初始化友盟 // var intent = Intent( // activity, // MainProcessActivity::class.java // ) // intent.putExtra("operationCode", 2) // intent.putExtra("channelCode", code) // activity.startActivity(intent) callback("success") } catch (e: Exception) { callback("fail") e.printStackTrace() } } /** * 初始化权限 * */ @AsyncJsApi(methodName = "initPermissions") fun initPermission(data: JSONObject?, callback: (data: Any) -> Unit) { if (mPermissionsLEBCallBack == null) { mPermissionsLEBCallBack = Observer { t: DataBean? -> Logger.e("给js返回的:" + t!!.toJSON()) if (t != null) { callback(t.toJSON()) } else { callback("error") } //清空3个监听 mPermissionsLEBCallBack = clearLEBCallBackByKey( LiveEventBusKeys.PERMISSIONS_ACTIVITY_CALLBACK, mPermissionsLEBCallBack!! ) }; LiveEventBus .get(LiveEventBusKeys.PERMISSIONS_ACTIVITY_CALLBACK, DataBean::class.java) .observeForever(mPermissionsLEBCallBack!!) } var intent = Intent( BaseConsts.mTopActivity, PermissionsActivity::class.java ) intent.putExtra("permissionArray", data!!.getString("permissionArr")) BaseConsts.mTopActivity.startActivity(intent) } /** * 检查是否有权限 * */ @SyncJsApi(methodName = "checkPermission") fun checkPermission( data: JSONObject?, activity: Activity, ): Boolean { if (data!!.length() == 0) { Logger.e("未传输需要检测的权限") return false; } if (ActivityCompat.checkSelfPermission( activity, data!!.getString("permission") ) == PermissionChecker.PERMISSION_GRANTED ) { return true; } else if (ActivityCompat.checkSelfPermission( activity, data!!.getString("permission") ) == PermissionChecker.PERMISSION_DENIED ) { return false; } return false; } /** * 获取deviceUUID * */ @SyncJsApi(methodName = "getDeviceUuid") fun getDeviceUUID(data: JSONObject?): String { return DeviceUuidFactory.getUuid(); } /** * 获取SN * */ @SyncJsApi(methodName = "AndroidSN") fun getAndroidSN(data: JSONObject?, activity: Activity): String { return EfunboxUtil.getSerialNum(activity); } @AsyncJsApi(methodName = "initUmengSDK") fun initUmeng(data: JSONObject?, callback: (data: Any) -> Unit, activity: Activity) { var test = EfunboxUtil.getUengChannelCode(BaseConsts.mBaseApplication); Logger.e("test:" + test) // // var code = data!!.getString("channelCode"); // try { // var code = data!!.getString("channelCode"); // UMengUtil.init(activity, code) // callback("success") // } catch (e: Exception) { // e.printStackTrace(); // callback("error") // } } /** * 杀死所有进程关闭应用 * */ @AsyncJsApi(methodName = "closeApp") fun closeApp(data: JSONObject?, callback: (data: Any) -> Unit, activity: Activity) { EfunboxUtil.killAppProcess(activity); } //根据key移除leb回调,并且指向空 private fun clearLEBCallBackByKey( key: String, observer: Observer, ): Observer? { LiveEventBus .get(key, DataBean::class.java) .removeObserver(observer) return null; } var activityLifecycleCallbacks: Application.ActivityLifecycleCallbacks = object : Application.ActivityLifecycleCallbacks { override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle) { Logger.e("oncreate:activityName:" + activity.localClassName) } override fun onActivityStarted(activity: Activity) {} override fun onActivityResumed(activity: Activity) {} override fun onActivityPaused(activity: Activity) {} override fun onActivityStopped(activity: Activity) {} override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {} override fun onActivityDestroyed(activity: Activity) { //是退出的WxaContainerActivity,就调用umeng的destory,算是退出应用了 Logger.e("onActivityDestroyed:activityName:" + activity.localClassName) if (activity.localClassName.contains("WxaContainerActivity")) { Logger.e("跳转MainProcess---") //注释跳转主进程初始化关闭 // var intent = Intent( // activity, // MainProcessActivity::class.java // ) // intent.putExtra("operationCode", 1) // activity.startActivity(intent) // Logger.e("跳转MainProcess---end") } } } }