11package me.jbusdriver.common
22
3+ import android.app.Application
34import android.os.Environment
45import com.billy.cc.core.component.CC
56import com.orhanobut.logger.AndroidLogAdapter
67import com.orhanobut.logger.Logger
78import com.orhanobut.logger.PrettyFormatStrategy
89import com.squareup.leakcanary.LeakCanary
9- import com.tencent.tinker.loader.app.TinkerApplication
10- import com.tencent.tinker.loader.shareutil.ShareConstants
1110import com.umeng.analytics.MobclickAgent
11+ import com.wlqq.phantom.library.PhantomCore
12+ import com.wlqq.phantom.library.log.ILogReporter
1213import io.reactivex.plugins.RxJavaPlugins
1314import me.jbusdriver.BuildConfig
15+ import me.jbusdriver.base.GSON
1416import me.jbusdriver.base.JBusManager
1517import me.jbusdriver.base.arrayMapof
1618import me.jbusdriver.debug.stetho.initializeStetho
1719import me.jbusdriver.http.JAVBusService
1820import java.io.File
21+ import java.util.*
1922
2023
2124lateinit var JBus : AppContext
2225
2326
24- class AppContext : TinkerApplication (ShareConstants .TINKER_ENABLE_ALL , " me.jbusdriver.common.JBusApplicationLike" ,
25- " com.tencent.tinker.loader.TinkerLoader" , false ) {
27+ class AppContext : Application () {
2628
2729 val JBusServices by lazy { arrayMapof<String , JAVBusService >() }
30+ private val isDebug by lazy {
31+ BuildConfig .DEBUG || File (Environment .getExternalStorageDirectory().absolutePath + File .separator +
32+ packageName
33+ + File .separator + " debug"
34+
35+ ).exists()
36+ }
37+
38+ private val phantomHostConfig by lazy {
39+ PhantomCore .Config ()
40+ .setCheckSignature(! isDebug)
41+ .setCheckVersion(! BuildConfig .DEBUG )
42+ .setDebug(isDebug)
43+ .setLogLevel(if (isDebug) android.util.Log .VERBOSE else android.util.Log .WARN )
44+ .setLogReporter(LogReporterImpl ())
45+ }
46+
2847
2948 override fun onCreate () {
3049 super .onCreate()
50+ JBusManager .setContext(this )
51+ JBus = this
3152
3253 if (LeakCanary .isInAnalyzerProcess(this )) {
3354 // This process is dedicated to LeakCanary for heap analysis.
3455 // You should not init your app in this process.
3556 return
3657 }
58+ // 插件系统尽早初始化
59+ PhantomCore .getInstance().init (this , phantomHostConfig)
3760
38- if (BuildConfig . DEBUG ) {
61+ if (isDebug ) {
3962 LeakCanary .install(this )
4063
4164 initializeStetho(this ) // chrome://inspect/#devices
@@ -49,31 +72,27 @@ class AppContext : TinkerApplication(ShareConstants.TINKER_ENABLE_ALL, "me.jbusd
4972 .build()
5073
5174 Logger .addLogAdapter(object : AndroidLogAdapter (formatStrategy) {
52- override fun isLoggable (priority : Int , tag : String? ) = BuildConfig .DEBUG || File (Environment .getExternalStorageDirectory().absolutePath + File .separator +
53- packageName
54- + File .separator + " debug"
55-
56- ).exists()
75+ override fun isLoggable (priority : Int , tag : String? ) = isDebug
5776 })
5877
5978
60- CC .enableVerboseLog(true )
61- CC .enableDebug(true )
62- CC .enableRemoteCC(true )
79+ CC .enableVerboseLog(isDebug )
80+ CC .enableDebug(isDebug )
81+ CC .enableRemoteCC(isDebug )
6382 }
6483
65- MobclickAgent .setDebugMode(BuildConfig .DEBUG )
84+
85+ MobclickAgent .setDebugMode(isDebug)
6686
6787 RxJavaPlugins .setErrorHandler {
6888 try {
69- if (! BuildConfig . DEBUG ) MobclickAgent .reportError(this , it)
89+ if (! isDebug ) MobclickAgent .reportError(this , it)
7090 } catch (e: Exception ) {
7191 // ignore report error
7292 }
7393 }
7494
75- JBus = this
76- JBusManager .setContext(this )
95+
7796 this .registerActivityLifecycleCallbacks(JBusManager )
7897 }
7998
@@ -89,4 +108,23 @@ class AppContext : TinkerApplication(ShareConstants.TINKER_ENABLE_ALL, "me.jbusd
89108 }
90109
91110
111+ companion object {
112+
113+ private class LogReporterImpl : ILogReporter {
114+
115+ override fun reportException (throwable : Throwable , message : HashMap <String , Any >) {
116+ // 使用 Bugly 或其它异常监控平台上报 Phantom 内部捕获的异常
117+ MobclickAgent .reportError(JBus , throwable)
118+ MobclickAgent .reportError(JBus , GSON .toJson(message))
119+ }
120+
121+ override fun reportEvent (eventId : String , label : String , params : HashMap <String , Any >) {
122+ // 使用 talkingdata 或其它移动统计平台上报 Phantom 内部自定义事件
123+ }
124+
125+ override fun reportLog (tag : String , message : String ) {
126+ // 使用 Bugly 或其它异常监控平台上报 Phantom 内部输出的上下文相关日志
127+ }
128+ }
129+ }
92130}
0 commit comments