diff --git a/entry/src/main/ets/adapters/NativeRuntimeAdapter.ts b/entry/src/main/ets/adapters/NativeRuntimeAdapter.ts index 9a63dbc..8fdda74 100644 --- a/entry/src/main/ets/adapters/NativeRuntimeAdapter.ts +++ b/entry/src/main/ets/adapters/NativeRuntimeAdapter.ts @@ -1,23 +1,43 @@ -const sleep = (ms: number): Promise => new Promise((resolve) => setTimeout(resolve, ms)) +import nativeMrp from 'libmrp_napi.so' + +function assertOk(result: { ok: boolean; errorCode?: number; errorMessage?: string }, action: string): void { + if (!result.ok) { + throw new Error(`${action} failed: ${result.errorCode ?? -1} ${result.errorMessage ?? ''}`.trim()) + } +} export class NativeRuntimeAdapter { - async startSession(_appId: string): Promise { - await sleep(600) + async startSession(appId: string): Promise { + assertOk(nativeMrp.init({ + workDir: '/data/storage/el2/base/files/mrp', + width: 240, + height: 320, + debug: true, + }), 'init') + assertOk(nativeMrp.loadPackage(appId), 'loadPackage') + assertOk(nativeMrp.start(), 'start') } - async restartSession(_appId: string): Promise { - await sleep(400) + async restartSession(appId: string): Promise { + await this.stopSession() + await this.startSession(appId) } async stopSession(): Promise { - return + assertOk(nativeMrp.release(), 'release') } async notifyPageShow(): Promise { - return + const result = nativeMrp.resume() + if (!result.ok) { + return + } } async notifyPageHide(): Promise { - return + const result = nativeMrp.pause() + if (!result.ok) { + return + } } } diff --git a/entry/src/main/ets/components/runtime/RuntimeContainer.ets b/entry/src/main/ets/components/runtime/RuntimeContainer.ets index 25274ac..0902391 100644 --- a/entry/src/main/ets/components/runtime/RuntimeContainer.ets +++ b/entry/src/main/ets/components/runtime/RuntimeContainer.ets @@ -1,10 +1,19 @@ @Component export struct RuntimeContainer { + @Prop hint: string = 'Runtime Surface Placeholder' + build() { - Column() { - Text('Runtime Surface Placeholder') + Column({ space: 12 }) { + Text('MRP Runtime Surface Placeholder') + .fontSize(18) + .fontWeight(FontWeight.Medium) + Text(this.hint) + .fontSize(14) + .fontColor('#666666') } + .justifyContent(FlexAlign.Center) + .alignItems(HorizontalAlign.Center) .width('100%') - .height('100%') + .layoutWeight(1) } } diff --git a/entry/src/main/ets/pages/RunPage.ets b/entry/src/main/ets/pages/RunPage.ets index 5bb44df..b381a8d 100644 --- a/entry/src/main/ets/pages/RunPage.ets +++ b/entry/src/main/ets/pages/RunPage.ets @@ -59,6 +59,7 @@ export struct RunPage { if (this.appId) { StatusBanner({ text: `当前应用:${this.appId}` }) } + StatusBanner({ text: this.store.runtimeHint }) if (this.store.error) { StatusBanner({ text: this.store.error.message }) } @@ -67,7 +68,7 @@ export struct RunPage { onRestart: () => this.onRestart(), onExit: () => this.onExit() }) - RuntimeContainer() + RuntimeContainer({ hint: this.store.runtimeHint }) if (this.store.phase !== 'running') { RuntimeStatusOverlay({ text: `当前状态:${this.store.phase}` }) } diff --git a/entry/src/main/ets/store/RuntimeSessionStore.ts b/entry/src/main/ets/store/RuntimeSessionStore.ts index 0456e54..0fd5f4c 100644 --- a/entry/src/main/ets/store/RuntimeSessionStore.ts +++ b/entry/src/main/ets/store/RuntimeSessionStore.ts @@ -9,6 +9,7 @@ export class RuntimeSessionStore extends StoreObserver { error: UiError | null = null canRestart: boolean = false canExit: boolean = false + runtimeHint: string = '等待启动' constructor(private readonly service: RuntimeService = new RuntimeService()) { super() @@ -20,15 +21,18 @@ export class RuntimeSessionStore extends StoreObserver { this.error = null this.canRestart = false this.canExit = false + this.runtimeHint = '正在初始化 Native/NAPI...' this.notify() try { await this.service.start(appId) this.phase = 'running' this.canRestart = true this.canExit = true - } catch (_error) { + this.runtimeHint = 'Native/NAPI 已连通,可进入后续拉帧联调' + } catch (error) { this.phase = 'error' this.error = { message: '启动失败', retryable: true } + this.runtimeHint = error instanceof Error ? error.message : 'Native/NAPI 启动失败' } this.notify() } @@ -40,15 +44,18 @@ export class RuntimeSessionStore extends StoreObserver { this.phase = 'starting' this.error = null this.canRestart = false + this.runtimeHint = '正在重启 Native/NAPI...' this.notify() try { await this.service.restart(this.currentAppId) this.phase = 'running' this.canRestart = true this.canExit = true - } catch (_error) { + this.runtimeHint = '重启完成,Native/NAPI 仍可用' + } catch (error) { this.phase = 'error' this.error = { message: '重启失败', retryable: true } + this.runtimeHint = error instanceof Error ? error.message : '重启失败' } this.notify() } @@ -58,6 +65,7 @@ export class RuntimeSessionStore extends StoreObserver { this.phase = 'stopped' this.canExit = false this.canRestart = false + this.runtimeHint = '运行会话已释放' this.notify() }