From b480597e8e0c0effd98818c2d8ba994c4df5aef3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Wed, 14 Aug 2024 21:36:21 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=BF=AE=E6=94=B94.1release=20=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E6=88=90=E5=8A=9F=E6=88=96=E5=A4=B1=E8=B4=A5=E5=BC=B9?= =?UTF-8?q?=E6=A1=86=E6=8F=90=E9=86=92=20Signed-off-by:=20=E9=82=B9?= =?UTF-8?q?=E5=8F=8B=E6=9D=BE=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/src/main/ets/util/FormatUtils.ts | 37 +++++++++++++++++++ .../src/main/ets/manager/OtaUpdateManager.ets | 11 ++++-- .../ota/src/main/ets/manager/StateManager.ets | 18 ++++++--- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/common/src/main/ets/util/FormatUtils.ts b/common/src/main/ets/util/FormatUtils.ts index cb46447..bb30220 100644 --- a/common/src/main/ets/util/FormatUtils.ts +++ b/common/src/main/ets/util/FormatUtils.ts @@ -15,6 +15,7 @@ import type common from '@ohos.app.ability.common'; import { DeviceUtils } from '../util/DeviceUtils'; +import { LogUtils } from './LogUtils'; /** * 格式化工具 @@ -100,4 +101,40 @@ export namespace FormatUtils { let numfmt: Intl.NumberFormat = new Intl.NumberFormat(language, {style:'percent', notation:'standard'}); return numfmt.format(num); } + + /** + * JSON stringify方法封装 + * + * @param value JS对象 + * @return json字符串 + */ + export function stringify(value: T): string { + if (value) { + try { + return JSON.stringify(value); + } catch (exception) { + LogUtils.error('FormateUtils', 'JSON.stringify failed !!'); + return ''; + } + } + return ''; + } + + /** + * json 字符串解析 + * + * @param content json 字符串 + * @return T 解析后返回值 + */ + export function parseJson(content: string): T | null { + if (!content) { + return null; + } + try { + return JSON.parse(content) as T; + } catch (exception) { + LogUtils.error('FormateUtils', 'paramJson failed !!'); + } + return null; + } } \ No newline at end of file diff --git a/feature/ota/src/main/ets/manager/OtaUpdateManager.ets b/feature/ota/src/main/ets/manager/OtaUpdateManager.ets index e617369..21b0e0e 100644 --- a/feature/ota/src/main/ets/manager/OtaUpdateManager.ets +++ b/feature/ota/src/main/ets/manager/OtaUpdateManager.ets @@ -32,6 +32,7 @@ import { StateManager } from '../manager/StateManager'; import { NotificationManager } from '../notify/NotificationManager'; import VersionUtils from '../util/VersionUtils'; import { UpgradeAdapter } from '../UpgradeAdapter'; +import { FormatUtils } from '@ohos/common/src/main/ets/util/FormatUtils'; /** * 升级接口管理类 @@ -322,6 +323,7 @@ export class OtaUpdateManager { await this.getNewVersion(); } + this.log('notifyUpdateStatus' + 'notify start'); await StateManager.createInstance(otaStatus).notify(context, eventId); } @@ -368,9 +370,11 @@ export class OtaUpdateManager { public async handleWant(want: Want, context: common.Context): Promise { let action: string = want?.action ?? ''; if (await NotificationManager.handleAction(action, context)) { + this.log('handleWant:' + FormatUtils.stringify(want)); return; } let eventInfo = this.wantParser(want); + this.log('handleWant: eventInfo is ' + FormatUtils.stringify(eventInfo)); if (!eventInfo?.eventId) { this.log('eventInfo?.eventId is null'); return; @@ -388,10 +392,9 @@ export class OtaUpdateManager { } private wantParser(want: Want): update.EventInfo { - let eventInfo: update.EventInfo; - let eventInfoStr = want.parameters[OtaUpdateManager.KEY]; - if (typeof eventInfoStr === 'string') { - eventInfo = JSON.parse(eventInfoStr); + let eventInfo: update.EventInfo = want?.parameters?.[OtaUpdateManager.KEY] as update.EventInfo; + if (typeof eventInfo === 'string') { + eventInfo = FormatUtils.parseJson(eventInfo); } return eventInfo; } diff --git a/feature/ota/src/main/ets/manager/StateManager.ets b/feature/ota/src/main/ets/manager/StateManager.ets index a98abf0..8773f6a 100644 --- a/feature/ota/src/main/ets/manager/StateManager.ets +++ b/feature/ota/src/main/ets/manager/StateManager.ets @@ -422,6 +422,7 @@ export class DownloadSuccess extends BaseState { async notify(context?: common.Context, eventId?: update.EventId): Promise { let isABInstall = await VersionUtils.isABInstall(); + LogUtils.info('StateManager:', 'notify ab flag ' + isABInstall + ',eventId:' + eventId); if (eventId == update.EventId.EVENT_DOWNLOAD_SUCCESS && isABInstall) { OtaUpdateManager.getInstance().upgrade(update.Order.INSTALL); return; @@ -576,12 +577,17 @@ export class UpgradeSuccess extends BaseState { this.isButtonClickable = false; } - async notify(context?: common.Context): Promise { - AppStorage.Set('installStatusRefresh', JSON.stringify(this.otaStatus)); - await UpgradeAdapter.getInstance().getNotifyInstance()?.cancelAll(); - let versionName = globalThis.lastVersionName; - LogUtils.log('StateManager', "UpgradeSuccess versionName is " + versionName); - await UpgradeAdapter.getInstance().getNotifyInstance()?.showUpgradeSuccess(versionName, context); + async notify(context?: common.Context, eventId?: update.EventId): Promise { + if (eventId == update.EventId.EVENT_UPGRADE_SUCCESS) { + LogUtils.log('StateManager', 'Upgrade success'); + AppStorage.Set('installStatusRefresh', JSON.stringify(this.otaStatus)); + await UpgradeAdapter.getInstance().getNotifyInstance()?.cancelAll(); + let versionName = globalThis.lastVersionName; + LogUtils.log('StateManager', 'UpgradeSuccess versionName is ' + versionName); + await UpgradeAdapter.getInstance().getNotifyInstance()?.showUpgradeSuccess(versionName, context); + } else { + LogUtils.log('StateManager', 'Upgrade EventId error'); + } } } -- Gitee From ec106fcc6e053586e1228f99ea016c84b2d7369d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Wed, 14 Aug 2024 13:46:00 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=BA=A7=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- feature/ota/src/main/ets/manager/StateManager.ets | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/feature/ota/src/main/ets/manager/StateManager.ets b/feature/ota/src/main/ets/manager/StateManager.ets index 8773f6a..98f3c43 100644 --- a/feature/ota/src/main/ets/manager/StateManager.ets +++ b/feature/ota/src/main/ets/manager/StateManager.ets @@ -579,14 +579,13 @@ export class UpgradeSuccess extends BaseState { async notify(context?: common.Context, eventId?: update.EventId): Promise { if (eventId == update.EventId.EVENT_UPGRADE_SUCCESS) { - LogUtils.log('StateManager', 'Upgrade success'); AppStorage.Set('installStatusRefresh', JSON.stringify(this.otaStatus)); await UpgradeAdapter.getInstance().getNotifyInstance()?.cancelAll(); let versionName = globalThis.lastVersionName; - LogUtils.log('StateManager', 'UpgradeSuccess versionName is ' + versionName); + LogUtils.info('StateManager', 'UpgradeSuccess versionName is ' + versionName); await UpgradeAdapter.getInstance().getNotifyInstance()?.showUpgradeSuccess(versionName, context); } else { - LogUtils.log('StateManager', 'Upgrade EventId error'); + LogUtils.error('StateManager', 'Upgrade EventId error'); } } } -- Gitee From 56b784b802a67b8c7076a44b74fde4584d1c544b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Fri, 16 Aug 2024 03:29:08 +0000 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7=EF=BC=8C=E4=B8=8D=E5=90=88=E7=90=86=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- AppScope/app.json5 | 4 ++-- feature/ota/src/main/ets/manager/OtaUpdateManager.ets | 1 - feature/ota/src/main/ets/manager/StateManager.ets | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/AppScope/app.json5 b/AppScope/app.json5 index b9626ec..e757ab9 100644 --- a/AppScope/app.json5 +++ b/AppScope/app.json5 @@ -3,8 +3,8 @@ "bundleName": "com.ohos.updateapp", "debug": false, "vendor": "example", - "versionCode": 2040000100, - "versionName": "204.0.0.100", + "versionCode": 2040000110, + "versionName": "204.0.0.110", "icon": "$media:app_icon", "label": "$string:app_name", "distributedNotificationEnabled": true diff --git a/feature/ota/src/main/ets/manager/OtaUpdateManager.ets b/feature/ota/src/main/ets/manager/OtaUpdateManager.ets index 21b0e0e..6c9661e 100644 --- a/feature/ota/src/main/ets/manager/OtaUpdateManager.ets +++ b/feature/ota/src/main/ets/manager/OtaUpdateManager.ets @@ -323,7 +323,6 @@ export class OtaUpdateManager { await this.getNewVersion(); } - this.log('notifyUpdateStatus' + 'notify start'); await StateManager.createInstance(otaStatus).notify(context, eventId); } diff --git a/feature/ota/src/main/ets/manager/StateManager.ets b/feature/ota/src/main/ets/manager/StateManager.ets index 98f3c43..116eb5f 100644 --- a/feature/ota/src/main/ets/manager/StateManager.ets +++ b/feature/ota/src/main/ets/manager/StateManager.ets @@ -579,6 +579,7 @@ export class UpgradeSuccess extends BaseState { async notify(context?: common.Context, eventId?: update.EventId): Promise { if (eventId == update.EventId.EVENT_UPGRADE_SUCCESS) { + LogUtils.error('StateManager', 'Upgrade success'); AppStorage.Set('installStatusRefresh', JSON.stringify(this.otaStatus)); await UpgradeAdapter.getInstance().getNotifyInstance()?.cancelAll(); let versionName = globalThis.lastVersionName; -- Gitee From 05074d018af88ebf91bcad1c3e9c918a6a2ea3dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Tue, 20 Aug 2024 09:05:35 +0000 Subject: [PATCH 4/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9hap=E5=8F=91=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E6=97=B6notification=E6=9C=8D=E5=8A=A1=E8=BF=98?= =?UTF-8?q?=E6=9C=AA=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- AppScope/app.json5 | 4 +-- .../src/main/ets/manager/UpgradeInterface.ets | 5 ++++ .../ota/src/main/ets/manager/StateManager.ets | 2 ++ .../main/ets/notify/NotificationHelper.ets | 27 +++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/AppScope/app.json5 b/AppScope/app.json5 index e757ab9..5ea08d8 100644 --- a/AppScope/app.json5 +++ b/AppScope/app.json5 @@ -3,8 +3,8 @@ "bundleName": "com.ohos.updateapp", "debug": false, "vendor": "example", - "versionCode": 2040000110, - "versionName": "204.0.0.110", + "versionCode": 2040000115, + "versionName": "204.0.0.115", "icon": "$media:app_icon", "label": "$string:app_name", "distributedNotificationEnabled": true diff --git a/common/src/main/ets/manager/UpgradeInterface.ets b/common/src/main/ets/manager/UpgradeInterface.ets index 4fd8ee7..f89f5a4 100644 --- a/common/src/main/ets/manager/UpgradeInterface.ets +++ b/common/src/main/ets/manager/UpgradeInterface.ets @@ -196,5 +196,10 @@ export interface INotify { * 取消所有通知 */ cancelAll(): Promise; + + /** + * 检查notification服务是否启动 + */ + isServiceReady: Promise; } diff --git a/feature/ota/src/main/ets/manager/StateManager.ets b/feature/ota/src/main/ets/manager/StateManager.ets index 116eb5f..b67e528 100644 --- a/feature/ota/src/main/ets/manager/StateManager.ets +++ b/feature/ota/src/main/ets/manager/StateManager.ets @@ -528,6 +528,7 @@ export class InstallFailed extends BaseState { async notify(context?: common.Context): Promise { AppStorage.Set('installStatusRefresh', JSON.stringify(this.otaStatus)); + await UpgradeAdapter.getInstance().getNotifyInstance()?.isServiceReady(); await UpgradeAdapter.getInstance().getNotifyInstance()?.cancelAll(); if (VersionUtils.isInNewVersionPage()) { DialogUtils.showUpgradeFailDialog(context, this.otaStatus); @@ -581,6 +582,7 @@ export class UpgradeSuccess extends BaseState { if (eventId == update.EventId.EVENT_UPGRADE_SUCCESS) { LogUtils.error('StateManager', 'Upgrade success'); AppStorage.Set('installStatusRefresh', JSON.stringify(this.otaStatus)); + await UpgradeAdapter.getInstance().getNotifyInstance()?.isServiceReady(); await UpgradeAdapter.getInstance().getNotifyInstance()?.cancelAll(); let versionName = globalThis.lastVersionName; LogUtils.info('StateManager', 'UpgradeSuccess versionName is ' + versionName); diff --git a/feature/ota/src/main/ets/notify/NotificationHelper.ets b/feature/ota/src/main/ets/notify/NotificationHelper.ets index 6536f3d..1c948e2 100644 --- a/feature/ota/src/main/ets/notify/NotificationHelper.ets +++ b/feature/ota/src/main/ets/notify/NotificationHelper.ets @@ -205,6 +205,33 @@ export class NotificationHelper implements INotify { }); } + /** + * 检查notification服务是否启动 + */ + async isServiceReady(): Promise { + const retryTimes: number = 10; + let count: number = 0; + let isReady: boolean = false; + while (count < retryTimes) { + try { + await notification.isDistributedEnabled().then(() => { + this.logInfo('notification service is ready'); + isReady = true; + }); + if (isReady) { + break; + } + count++; + await new Promise((resolve)) => setTimeout(() => resolve(), 1000)); + this.logError('notification service is not ready'); + } catch (err) { + count++; + this.logError('notification service throw abnormal'); + continue; + } + }; + } + /** * info级别日志打印 * -- Gitee From e646267a3037c9ec53d2bdf42d50c27567a77788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Tue, 20 Aug 2024 11:48:44 +0000 Subject: [PATCH 5/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- AppScope/app.json5 | 4 ++-- common/src/main/ets/manager/UpgradeInterface.ets | 2 +- feature/ota/src/main/ets/notify/NotificationHelper.ets | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/AppScope/app.json5 b/AppScope/app.json5 index 5ea08d8..f706bbb 100644 --- a/AppScope/app.json5 +++ b/AppScope/app.json5 @@ -3,8 +3,8 @@ "bundleName": "com.ohos.updateapp", "debug": false, "vendor": "example", - "versionCode": 2040000115, - "versionName": "204.0.0.115", + "versionCode": 2040000120, + "versionName": "204.0.0.120", "icon": "$media:app_icon", "label": "$string:app_name", "distributedNotificationEnabled": true diff --git a/common/src/main/ets/manager/UpgradeInterface.ets b/common/src/main/ets/manager/UpgradeInterface.ets index f89f5a4..66deb6d 100644 --- a/common/src/main/ets/manager/UpgradeInterface.ets +++ b/common/src/main/ets/manager/UpgradeInterface.ets @@ -200,6 +200,6 @@ export interface INotify { /** * 检查notification服务是否启动 */ - isServiceReady: Promise; + isServiceReady(): Promise; } diff --git a/feature/ota/src/main/ets/notify/NotificationHelper.ets b/feature/ota/src/main/ets/notify/NotificationHelper.ets index 1c948e2..cd8cd9f 100644 --- a/feature/ota/src/main/ets/notify/NotificationHelper.ets +++ b/feature/ota/src/main/ets/notify/NotificationHelper.ets @@ -222,7 +222,7 @@ export class NotificationHelper implements INotify { break; } count++; - await new Promise((resolve)) => setTimeout(() => resolve(), 1000)); + await new Promise((resolve) => setTimeout(() => resolve(), 1000)); this.logError('notification service is not ready'); } catch (err) { count++; -- Gitee From 2bca94d97090fbe7c4705a05a60583c06e92261d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Wed, 21 Aug 2024 08:26:02 +0000 Subject: [PATCH 6/7] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=BA=A7=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- feature/ota/src/main/ets/manager/StateManager.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/feature/ota/src/main/ets/manager/StateManager.ets b/feature/ota/src/main/ets/manager/StateManager.ets index b67e528..568c486 100644 --- a/feature/ota/src/main/ets/manager/StateManager.ets +++ b/feature/ota/src/main/ets/manager/StateManager.ets @@ -580,7 +580,7 @@ export class UpgradeSuccess extends BaseState { async notify(context?: common.Context, eventId?: update.EventId): Promise { if (eventId == update.EventId.EVENT_UPGRADE_SUCCESS) { - LogUtils.error('StateManager', 'Upgrade success'); + LogUtils.info('StateManager', 'Upgrade success'); AppStorage.Set('installStatusRefresh', JSON.stringify(this.otaStatus)); await UpgradeAdapter.getInstance().getNotifyInstance()?.isServiceReady(); await UpgradeAdapter.getInstance().getNotifyInstance()?.cancelAll(); -- Gitee From 2f388b83f00f5a3c3cc738004b89d70b3ae5b7cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Thu, 29 Aug 2024 14:59:43 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E6=A0=8F=E5=BC=B9=E6=A1=86=E9=97=AE=E9=A2=98=20Signed-off-by:?= =?UTF-8?q?=20=E9=82=B9=E5=8F=8B=E6=9D=BE=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/ServiceExtAbility/service.ts | 42 ++++++++++++++-- .../main/ets/ServiceExtAbility/serviceStub.ts | 50 +++++++++++++++++++ 2 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts diff --git a/product/oh/base/src/main/ets/ServiceExtAbility/service.ts b/product/oh/base/src/main/ets/ServiceExtAbility/service.ts index cc0fe00..2171c11 100644 --- a/product/oh/base/src/main/ets/ServiceExtAbility/service.ts +++ b/product/oh/base/src/main/ets/ServiceExtAbility/service.ts @@ -16,8 +16,10 @@ import Extension from '@ohos.app.ability.ServiceExtensionAbility'; import type Want from '@ohos.app.ability.Want'; import type rpc from '@ohos.rpc'; +import { FormatUtils } from '@ohos/common/src/main/ets/util/FormatUtils'; import { OtaUpdateManager } from '@ohos/ota/src/main/ets/manager/OtaUpdateManager'; import { LogUtils } from '@ohos/common/src/main/ets/util/LogUtils'; +import { ServiceExtStub } from './serviceStub'; /** * service extension ability. @@ -27,15 +29,19 @@ import { LogUtils } from '@ohos/common/src/main/ets/util/LogUtils'; */ export default class ServiceExtAbility extends Extension { private static readonly TAG = 'ServiceExtAbility'; + private static readonly CONNECT_TIMEOUT: string = 'Timeout'; + private static readonly START_ID_CONNECT = 10000; private startIdArray: number[] = []; + private connectTimeout: number = 15; + private connectTimeoutId: number | null = null; onCreate(want: Want): void { - LogUtils.log(ServiceExtAbility.TAG, 'onCreate:' + JSON.stringify(want)); + LogUtils.log(ServiceExtAbility.TAG, 'onCreate:' + FormatUtils.stringify(want)); globalThis.extensionContext = this.context; // when start ServiceExtAbility ,set context } async onRequest(want: Want, startId: number): Promise { - LogUtils.log(ServiceExtAbility.TAG, `onRequest, want: ${want.abilityName}`); + LogUtils.log(ServiceExtAbility.TAG, `onRequest, want: ${want?.abilityName}`); this.startIdArray.push(startId); globalThis.extensionContext = this.context; await OtaUpdateManager.getInstance().handleWant(want, globalThis.extensionContext); @@ -43,8 +49,36 @@ export default class ServiceExtAbility extends Extension { } onConnect(want: Want): rpc.RemoteObject { - LogUtils.log(ServiceExtAbility.TAG, `onConnect , want: ${want.abilityName}`); - return null; + LogUtils.log(ServiceExtAbility.TAG, `onConnect , want: ${want?.abilityName}`); + this.startIdArray.push(ServiceExtAbility.START_ID_CONNECT); + this.connectTimeout = want?.parameters?.[ServiceExtAbility.CONNECT_TIMEOUT] as number ?? this.connectTimeout; + let objectIpc: rpc.RemoteObject = new ServiceExtStub(ServiceExtAbility.TAG, (message: string) => { + return this.remoteMessageCallback(message); + }); + return objectIpc; + } + + private remoteMessageCallback(message: string): void { + LogUtils.info(ServiceExtAbility.TAG, + `remoteMessageCallback, timeout: ${this.connectTimeout}s, message: ${message}.`); + if (this.connectTimeoutId !== null) { + clearTimeout(this.connectTimeoutId); + } + this.connectTimeoutId = setTimeout(()=> { + this.connectTimeoutId = null; + this.stopSelf(ServiceExtAbility.START_ID_CONNECT); + }, this.connectTimeout * 1000); + + let want: Want = { + parameters: FormatUtils.parseJson(message) + }; + LogUtils.log(ServiceExtAbility.TAG, 'onConnect:' + FormatUtils.stringify(want)); + OtaUpdateManager.getInstance().handleWant(want, globalThis.extensionContext); + } + + onDisconnect(want: Want) { + LogUtils.info(ServiceExtAbility.TAG, `onDisconnect, want: ${want?.abilityName}`); + this.stopSelf(ServiceExtAbility.START_ID_CONNECT); } private isTerminal(): boolean { diff --git a/product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts b/product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts new file mode 100644 index 0000000..49ca305 --- /dev/null +++ b/product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import rpc from '@ohos.rpc'; +import update from '@ohos.update'; +import { LogUtils } from '@ohos/common'; + +/** + * ServiceExtAbility 远端代理存根 + * + * @since 2024-06-17 + */ +export class ServiceExtStub extends rpc.RemoteObject { + private static readonly TAG = 'ServiceExtStub'; + private static readonly MESSAGE_CODE = 5; + private remoteMessageCallBack: (message: string) => void; + + constructor(des: string, callBack: (message: string) => void) { + super(des); + this.remoteMessageCallBack = callBack; + } + + async onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, + options: rpc.MessageOption): Promise { + let dataMessage = data.readString(); + LogUtils.info(ServiceExtStub.TAG, `onRemoteMessageRequest, code: ${code}, dataMessage: ${dataMessage}.`); + switch (code) { + case ServiceExtStub.MESSAGE_CODE: + LogUtils.info(ServiceExtStub.TAG, `onRemoteMessageRequest, from ota update manager.`); + reply.writeInt(0); + this.remoteMessageCallBack(dataMessage); + return true; + default: + LogUtils.info(ServiceExtStub.TAG, `onRemoteMessageRequest, do nothing for error code, code: ${code}.`); + return true; + } + } +} -- Gitee