Skip to content

Commit

Permalink
js data ini (#490)
Browse files Browse the repository at this point in the history
* dev onDataInit

* [android] 合并JS改造代码

* 调整build和日志输出

* add logs

* fix onDataInit
  • Loading branch information
biezhihua authored Sep 23, 2024
1 parent f169ff5 commit 9df0a0f
Show file tree
Hide file tree
Showing 38 changed files with 1,098 additions and 196 deletions.
4 changes: 3 additions & 1 deletion GaiaXAnalyze/GXAnalyzeAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,13 @@ android {

buildTypes {
debug {
debuggable true
ndk {
abiFilters "x86", "x86_64", 'armeabi-v7a', 'arm64-v8a'
abiFilters 'arm64-v8a', 'armeabi-v7a', "x86", "x86_64"
}
}
release {
debuggable false
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
Expand Down
10 changes: 10 additions & 0 deletions GaiaXAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ android {
}
}

buildTypes {
debug {
debuggable true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

lintOptions {
abortOnError false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ data class GXTemplateInfo(
val js: String? = null
) {

var preload: Boolean = false

var expVersion: String? = null

var children: MutableList<GXTemplateInfo>? = null
Expand Down Expand Up @@ -222,6 +224,8 @@ data class GXTemplateInfo(
cssJson.getJSONObject(template.id)?.put(GXTemplateKey.GAIAX_LAYER_EDGE_INSETS, it)
}

val preload = layerJson.getBooleanValue(GXTemplateKey.GAIAX_LAYER_PRELOAD)

val expVersion: String? = layerJson.getString(GXTemplateKey.GAIAX_LAYER_EXP_VERSION)

val layer = GXLayer.create(layerJson)
Expand All @@ -240,6 +244,7 @@ data class GXTemplateInfo(
this.rawLayerJson = layerJson
this.rawConfigJson = configJson
this.expVersion = expVersion
this.preload = preload
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ object GXTemplateKey {

const val GAIAX_LAYER_EXP_VERSION = "exp-version"

const val GAIAX_LAYER_PRELOAD = "preload"

const val GAIAX_PX = "px"

const val GAIAX_PT = "pt"
Expand Down
4 changes: 4 additions & 0 deletions GaiaXAndroidAdapter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ android {
}

buildTypes {
debug {
debuggable true
}
release {
debuggable false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
Expand Down
4 changes: 4 additions & 0 deletions GaiaXAndroidClientToStudio/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ android {
}

buildTypes {
debug {
debuggable true
}
release {
debuggable false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,7 @@ class GXSocket : SocketListener {
gxSocketListener?.onStudioAddData(templateId, templateJson)
lastTemplateId = templateId
updateTask?.let { uiHandler.removeCallbacks(it) }
updateTask =
Runnable { gxSocketListener?.onStudioUpdate(templateId, templateJson) }
updateTask = Runnable { gxSocketListener?.onStudioUpdate(templateId, templateJson) }
updateTask?.let { uiHandler.postDelayed(it, 200) }
}
// 手动推送
Expand Down
15 changes: 11 additions & 4 deletions GaiaXAndroidDemo/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ android {
minSdk 21
targetSdk 33
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

ndk {
abiFilters "armeabi-v7a", "arm64-v8a", "x86"
}
}

sourceSets {
Expand All @@ -53,12 +49,23 @@ android {
}

buildTypes {
debug {
debuggable true
ndk {
abiFilters 'arm64-v8a', 'armeabi-v7a', "x86", "x86_64"
}
}
release {
debuggable false
ndk {
abiFilters 'arm64-v8a', 'armeabi-v7a'
}
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}


compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.alibaba.gaiax.demo.fastpreview

import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
Expand All @@ -16,6 +15,7 @@ import com.alibaba.gaiax.js.proxy.GXJSEngineProxy
import com.alibaba.gaiax.studio.GXStudioClient
import com.alibaba.gaiax.template.GXSize.Companion.dpToPx
import com.alibaba.gaiax.utils.GXScreenUtils
import kotlin.math.log


/**
Expand All @@ -26,7 +26,7 @@ import com.alibaba.gaiax.utils.GXScreenUtils
class GXFastPreviewActivity : AppCompatActivity(), GXStudioClient.IFastPreviewListener {

companion object {
private const val TAG = "[GaiaX]"
private const val TAG = "[GaiaX][FastPreview]"
const val GAIA_STUDIO_URL = "GAIA_STUDIO_URL"
const val GAIA_STUDIO_MODE = "GAIA_STUDIO_MODE"
const val GAIA_STUDIO_MODE_MULTI = "MULTI"
Expand Down Expand Up @@ -134,11 +134,7 @@ class GXFastPreviewActivity : AppCompatActivity(), GXStudioClient.IFastPreviewLi
gxView = GXTemplateEngine.instance.createView(gxTemplateItem, gxMeasureSize)

gxView?.let {
// 绑定数据
GXTemplateEngine.instance.bindData(gxView, gxTemplateData)

// 将数据加入页面中
fastPreviewRoot.addView(gxView, 0)

// 获取模板信息
val gxTemplateInfo = GXTemplateEngine.instance.getGXTemplateInfo(gxTemplateItem)
Expand All @@ -151,9 +147,24 @@ class GXFastPreviewActivity : AppCompatActivity(), GXStudioClient.IFastPreviewLi
Log.d(TAG, "exception() called with: data = $data")
}
}
GXJSEngineProxy.instance.registerComponent(gxView)

// 注册容器
GXJSEngineProxy.instance.registerComponentAndOnReady(gxView)
if (gxTemplateInfo.preload) {
GXJSEngineProxy.instance.onDataInit(gxView, gxTemplateData.data)?.let {
Log.d(TAG, "create: onDataInit changed data=${it}")
gxTemplateData.data = it
}
}

// 绑定数据
GXTemplateEngine.instance.bindData(gxView, gxTemplateData)

// 将数据加入页面中
fastPreviewRoot.addView(gxView, 0)

GXJSEngineProxy.instance.onReady(gxView)

Log.d(TAG, "create() called end")
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions GaiaXAndroidJS/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ android {
}

buildTypes {
debug {
debuggable true
}
release {
debuggable false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
Expand Down
2 changes: 1 addition & 1 deletion GaiaXAndroidJS/src/main/assets/bootstrap.js

Large diffs are not rendered by default.

112 changes: 109 additions & 3 deletions GaiaXAndroidJS/src/main/kotlin/com/alibaba/gaiax/js/GXJSEngine.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.alibaba.gaiax.js

import android.content.Context
import android.text.TextUtils
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.alibaba.gaiax.js.api.GXJSBaseModule
import com.alibaba.gaiax.js.api.IGXPage
import com.alibaba.gaiax.js.engine.GXHostContext
import com.alibaba.gaiax.js.engine.GXHostEngine
import com.alibaba.gaiax.js.impl.debug.DebugJSContext
import com.alibaba.gaiax.js.impl.debug.ISocketBridgeListener
Expand All @@ -27,7 +31,7 @@ class GXJSEngine {
}
}

internal enum class EngineType {
enum class EngineType {
QuickJS, DebugJS
}

Expand Down Expand Up @@ -96,7 +100,8 @@ class GXJSEngine {
}
if (file.startsWith(MODULE_PREFIX) && file.endsWith(MODULE_SUFFIX)) {
try {
val bizModules = JSONObject.parseObject(assetsOpen("$GAIAX_JS_MODULES/$file").bufferedReader(Charsets.UTF_8).use { it.readText() })
val bizModules = JSONObject.parseObject(
assetsOpen("$GAIAX_JS_MODULES/$file").bufferedReader(Charsets.UTF_8).use { it.readText() })
allModules.putAll(bizModules)
} catch (e: Exception) {
e.printStackTrace()
Expand All @@ -106,6 +111,9 @@ class GXJSEngine {
}
}
}
if (Log.isLog()) {
Log.d("registerAssetsModules() called with: allModules = $allModules")
}
allModules.forEach {
try {
Class.forName(it.value.toString())
Expand All @@ -127,7 +135,8 @@ class GXJSEngine {

private fun assetsOpen(file: String) = synchronized(context.assets) { context.assets.open(file) }

private fun assetsModules(path: String): Array<out String>? = synchronized(context.assets) { context.assets.list(path) }
private fun assetsModules(path: String): Array<out String>? =
synchronized(context.assets) { context.assets.list(path) }


fun startDefaultEngine(complete: (() -> Unit)? = null) {
Expand Down Expand Up @@ -225,14 +234,33 @@ class GXJSEngine {

fun onNativeEvent(componentId: Long, data: JSONObject) {
quickJSEngine?.runtime()?.context()?.getComponent(componentId)?.onNativeEvent(data)
if (data.getBooleanValue("isPage")) {
findPage(componentId, EngineType.QuickJS)?.let {
it.onNativeEvent(data)
}
}
debugEngine?.runtime()?.context()?.getComponent(componentId)?.onNativeEvent(data)
}

fun postAnimationMessage(data: JSONObject) {
quickJSEngine?.runtime()?.context()?.postAnimationMessage(data)
debugEngine?.runtime()?.context()?.postAnimationMessage(data)
}

fun postModalMessage(data: JSONObject) {
quickJSEngine?.runtime()?.context()?.postModalMessage(data)
debugEngine?.runtime()?.context()?.postModalMessage(data)
}

fun onReady(componentId: Long) {
quickJSEngine?.runtime()?.context()?.getComponent(componentId)?.onReady()
debugEngine?.runtime()?.context()?.getComponent(componentId)?.onReady()
}

fun onDataInit(componentId: Long, data: JSONObject): JSONObject? {
return quickJSEngine?.runtime()?.context()?.getComponent(componentId)?.onDataInit(data)
}

fun onReuse(componentId: Long) {
quickJSEngine?.runtime()?.context()?.getComponent(componentId)?.onReuse()
debugEngine?.runtime()?.context()?.getComponent(componentId)?.onReuse()
Expand All @@ -258,6 +286,8 @@ class GXJSEngine {
debugEngine?.runtime()?.context()?.getComponent(componentId)?.onLoadMore(data)
}



/**
* 为视图注册JS组件
*/
Expand All @@ -269,6 +299,20 @@ class GXJSEngine {
return componentId
}

fun generateUniqueInstanceId(): Long {
return IdGenerator.genLongId()
}

fun registerComponentWithId(
instanceId: Long, bizId: String, templateId: String, templateVersion: String, script: String?
) {
if (script != null) {
quickJSEngine?.runtime()?.context()
?.registerComponent(instanceId, bizId, templateId, templateVersion, script)
debugEngine?.runtime()?.context()?.registerComponent(instanceId, bizId, templateId, templateVersion, script)
}
}

/**
* 为视图解除JS组件
*/
Expand All @@ -277,6 +321,68 @@ class GXJSEngine {
debugEngine?.runtime()?.context()?.unregisterComponent(componentId)
}

fun registerPage(
bizId: String,
templateId: String,
templateVersion: String,
script: String,
nativePage: IGXPage
): Long {
// 页面instanceId从50000起
val pageId = IdGenerator.genLongId() + 50000
quickJSEngine?.runtime()?.context()
?.registerPage(pageId, bizId, templateId, templateVersion, script, nativePage)
debugEngine?.runtime()?.context()?.registerPage(pageId, bizId, templateId, templateVersion, script, nativePage)
return pageId
}

fun unregisterPage(id: Long) {
quickJSEngine?.runtime()?.context()?.unregisterPage(id)
debugEngine?.runtime()?.context()?.unregisterPage(id)
}

fun findPage(id: Long, engineType: EngineType): IGXPage? {
return when (engineType) {
EngineType.QuickJS -> quickJSEngine?.runtime()?.context()?.findPage(id)
EngineType.DebugJS -> debugEngine?.runtime()?.context()?.findPage(id)
}
}

fun onPageLoad(id: Long, data: JSONObject) {
quickJSEngine?.runtime()?.context()?.findPage(id)?.onLoad(data)
debugEngine?.runtime()?.context()?.findPage(id)?.onLoad(data)
}

fun onPageUnload(id: Long) {
quickJSEngine?.runtime()?.context()?.findPage(id)?.onUnload()
debugEngine?.runtime()?.context()?.findPage(id)?.onUnload()
}

fun onPageReady(id: Long) {
quickJSEngine?.runtime()?.context()?.findPage(id)?.onReady()
debugEngine?.runtime()?.context()?.findPage(id)?.onReady()
}

fun onPageShow(id: Long) {
quickJSEngine?.runtime()?.context()?.findPage(id)?.onShow()
debugEngine?.runtime()?.context()?.findPage(id)?.onShow()
}

fun onPageHide(id: Long) {
quickJSEngine?.runtime()?.context()?.findPage(id)?.onHide()
debugEngine?.runtime()?.context()?.findPage(id)?.onHide()
}

fun onPageScroll(id: Long, data: JSONObject) {
quickJSEngine?.runtime()?.context()?.findPage(id)?.onPageScroll(data)
debugEngine?.runtime()?.context()?.findPage(id)?.onPageScroll(data)
}

fun onPageReachBottom(id: Long) {
quickJSEngine?.runtime()?.context()?.findPage(id)?.onReachBottom()
debugEngine?.runtime()?.context()?.findPage(id)?.onReachBottom()
}

interface IJsExceptionListener {

/**
Expand Down
Loading

0 comments on commit 9df0a0f

Please sign in to comment.