1
1
package com.shiqi.testquickjs
2
+ import android.content.Context
3
+ import android.content.res.Configuration
4
+ import android.os.Build
5
+ import android.os.Handler
6
+ import android.os.Looper
7
+ import android.os.Message
8
+ import android.text.TextUtils
9
+ import android.util.Log
10
+ import com.tencent.mtt.hippy.*
11
+ import com.tencent.mtt.hippy.bridge.HippyBridge
12
+ import com.tencent.mtt.hippy.bridge.HippyBridgeImpl
13
+ import com.tencent.mtt.hippy.bridge.HippyBridgeManager
14
+ import com.tencent.mtt.hippy.bridge.NativeCallback
15
+ import com.tencent.mtt.hippy.bridge.libraryloader.LibraryLoader
16
+ import com.tencent.mtt.hippy.common.HippyArray
17
+ import com.tencent.mtt.hippy.common.HippyMap
18
+ import com.tencent.mtt.hippy.common.ThreadExecutor
19
+ import com.tencent.mtt.hippy.devsupport.DevSupportManager
20
+ import com.tencent.mtt.hippy.dom.DomManager
21
+ import com.tencent.mtt.hippy.modules.HippyModuleManager
22
+ import com.tencent.mtt.hippy.uimanager.RenderManager
23
+ import com.tencent.mtt.hippy.utils.*
24
+ import java.nio.charset.StandardCharsets
25
+ import java.util.concurrent.atomic.AtomicBoolean
2
26
3
- class HippyJsEngine {
4
- }
27
+ /* *
28
+ * Description :
29
+ *
30
+ * @Author : robertrchen
31
+ * @Date : 2022/7/26
32
+ */
33
+ class HippyJsEngine (private val context : Context ) : HippyBridge.BridgeCallback {
34
+ companion object {
35
+
36
+ private const val TAG = " QuickJs HippyJsEngine"
37
+
38
+ private const val URI_SCHEME_FILE = " file:"
39
+ private const val URI_SCHEME_ASSETS = " asset:"
40
+
41
+ private val loaded = AtomicBoolean (false )
42
+
43
+ private fun loadHippy () {
44
+ if (loaded.compareAndSet(false , true )) {
45
+ LibraryLoader .loadLibraryIfNeed()
46
+ }
47
+ }
48
+ }
49
+
50
+ private var bridge: HippyBridgeImpl ? = null
51
+
52
+ private fun getCurrentHandler (): Handler {
53
+ return Handler (Looper .myLooper() ? : Looper .getMainLooper())
54
+ }
55
+
56
+ fun init () {
57
+ loadHippy()
58
+ val hippyContext = HippyContext (context)
59
+ ContextHolder .initAppContext(hippyContext.getContext())
60
+ val v8InitParams = HippyEngine .V8InitParams ()
61
+ bridge = HippyBridgeImpl (
62
+ hippyContext,
63
+ this ,
64
+ false ,
65
+ false ,
66
+ false ,
67
+ " " ,
68
+ v8InitParams
69
+ ).also { bridge ->
70
+ bridge.initJSBridge(
71
+ getGlobalConfigs(hippyContext),
72
+ object : NativeCallback (getCurrentHandler()) {
73
+ override fun Call (
74
+ result : Long ,
75
+ message : Message ? ,
76
+ action : String? ,
77
+ reason : String?
78
+ ) {
79
+ Log .i(TAG , " hippy init: ${result == 0L } " )
80
+
81
+ runJsFile(" asset:/sonic.js" , true )
82
+ }
83
+ }, - 1
84
+ )
85
+ }
86
+ }
87
+
88
+ fun runJsFile (
89
+ jsFilePath : String ,
90
+ isAssetFile : Boolean ,
91
+ ) {
92
+ bridge?.let {
93
+ val startTime = System .currentTimeMillis()
94
+ val nativeCallback = object : NativeCallback (getCurrentHandler()) {
95
+ override fun Call (
96
+ result : Long ,
97
+ message : Message ? ,
98
+ action : String? ,
99
+ reason : String?
100
+ ) {
101
+ Log .i(TAG , " hippy v8 eval, cost: ${System .currentTimeMillis() - startTime} ms ${result == 0L } " )
102
+ }
103
+ }
104
+ if (isAssetFile) {
105
+ val uri =
106
+ if (jsFilePath.startsWith(URI_SCHEME_ASSETS )) jsFilePath else URI_SCHEME_ASSETS + jsFilePath
107
+ it.runScriptFromUri(uri, context.assets, false , " " , nativeCallback)
108
+
109
+ } else {
110
+ val uri =
111
+ if (jsFilePath.startsWith(URI_SCHEME_FILE )) jsFilePath else URI_SCHEME_FILE + jsFilePath
112
+ it.runScriptFromUri(uri, null , false , " " , nativeCallback)
113
+ }
114
+ }
115
+ }
116
+ private fun getGlobalConfigs (hippyContext : HippyContext ): String {
117
+ val context: Context = hippyContext.getContext()
118
+ val globalParams = HippyMap ()
119
+ val dimensionMap = DimensionsUtil .getDimensions(- 1 , - 1 , context, false )
120
+ globalParams.pushMap(" Dimensions" , dimensionMap)
121
+ var packageName = " "
122
+ var versionName = " "
123
+ val extraDataMap = HippyMap ()
124
+ try {
125
+ val packageManager = context.packageManager
126
+ val packageInfo = packageManager.getPackageInfo(context.packageName, 0 )
127
+ if (TextUtils .isEmpty(packageName)) {
128
+ packageName = packageInfo.packageName
129
+ }
130
+ if (TextUtils .isEmpty(versionName)) {
131
+ versionName = packageInfo.versionName
132
+ }
133
+ } catch (var11: Exception ) {
134
+ var11.printStackTrace()
135
+ }
136
+ val platformParams = HippyMap ()
137
+ platformParams.pushString(" OS" , " android" )
138
+ platformParams.pushString(" PackageName" , packageName)
139
+ platformParams.pushString(" VersionName" , versionName)
140
+ platformParams.pushInt(" APILevel" , Build .VERSION .SDK_INT )
141
+ platformParams.pushBoolean(" NightMode" , this .getNightMode())
142
+ val Localization = HippyMap ()
143
+ Localization .pushString(" language" , I18nUtil .getLanguage())
144
+ Localization .pushString(" country" , I18nUtil .getCountry())
145
+ Localization .pushInt(" direction" , I18nUtil .getLayoutDirection())
146
+ platformParams.pushMap(" Localization" , Localization )
147
+ globalParams.pushMap(" Platform" , platformParams)
148
+ val tkd = HippyMap ()
149
+ tkd.pushString(" appName" , packageName ? : " " )
150
+ tkd.pushString(" appVersion" , versionName ? : " " )
151
+ tkd.pushMap(" extra" , extraDataMap)
152
+ globalParams.pushMap(" tkd" , tkd)
153
+ return ArgumentUtils .objectToJson(globalParams)
154
+ }
155
+
156
+ private fun getNightMode (): Boolean {
157
+ return when ((context.resources.configuration.uiMode and Configuration .UI_MODE_NIGHT_MASK )) {
158
+ Configuration .UI_MODE_NIGHT_UNDEFINED ->
159
+ false
160
+ Configuration .UI_MODE_NIGHT_NO ->
161
+ false
162
+ Configuration .UI_MODE_NIGHT_YES ->
163
+ true
164
+ else -> false
165
+ }
166
+ }
167
+
168
+ class HippyContext (private val context : Context ) : HippyEngineContext {
169
+
170
+ private val config: HippyGlobalConfigs
171
+
172
+ init {
173
+ val initParams = HippyEngine .EngineInitParams ()
174
+ initParams.context = context
175
+ config = HippyGlobalConfigs (initParams)
176
+ }
177
+
178
+ fun getContext () = context
179
+
180
+ override fun getComponentName (): String {
181
+ return " "
182
+ }
183
+
184
+ override fun getGlobalConfigs (): HippyGlobalConfigs {
185
+ return config
186
+ }
187
+
188
+ override fun getModuleManager (): HippyModuleManager ? {
189
+ return null
190
+ }
191
+
192
+ override fun getBridgeManager (): HippyBridgeManager ? {
193
+ return null
194
+ }
195
+
196
+ override fun getDevSupportManager (): DevSupportManager ? {
197
+ return null
198
+ }
199
+
200
+ override fun getThreadExecutor (): ThreadExecutor ? {
201
+ return null
202
+ }
203
+
204
+ override fun getDomManager (): DomManager ? {
205
+ return null
206
+ }
207
+
208
+ override fun getRenderManager (): RenderManager ? {
209
+ return null
210
+ }
211
+
212
+ override fun getInstance (id : Int ): HippyRootView ? {
213
+ return null
214
+ }
215
+
216
+ override fun addInstanceLifecycleEventListener (listener : HippyInstanceLifecycleEventListener ? ) {
217
+ }
218
+
219
+ override fun removeInstanceLifecycleEventListener (listener : HippyInstanceLifecycleEventListener ? ) {
220
+ }
221
+
222
+ override fun addEngineLifecycleEventListener (listener : HippyEngineLifecycleEventListener ? ) {
223
+ }
224
+
225
+ override fun removeEngineLifecycleEventListener (listener : HippyEngineLifecycleEventListener ? ) {
226
+ }
227
+
228
+ override fun handleException (throwable : Throwable ? ) {
229
+ }
230
+
231
+ override fun getStartTimeMonitor (): TimeMonitor ? {
232
+ return null
233
+ }
234
+
235
+ override fun getEngineId (): Int {
236
+ return 0
237
+ }
238
+ }
239
+
240
+ override fun callNatives (
241
+ moduleName : String? ,
242
+ moduleFunc : String? ,
243
+ callId : String? ,
244
+ params : HippyArray ?
245
+ ) {
246
+ TODO (" Not yet implemented" )
247
+ }
248
+
249
+ override fun reportException (message : String? , stackTrace : String? ) {
250
+ TODO (" Not yet implemented" )
251
+ }
252
+
253
+ override fun reportException (e : Throwable ? ) {
254
+ TODO (" Not yet implemented" )
255
+ }
256
+
257
+ }
0 commit comments