From 2468e9134196de8308000ed7f7629607fc3eed7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Wed, 19 Jun 2024 19:55:37 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=90=8C=E6=AD=A5beta1=20Signed-off-by:=20?= =?UTF-8?q?=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 --- common/src/main/ets/util/FormatUtils.ts | 20 +++++++- .../src/main/ets/manager/OtaUpdateManager.ets | 8 +-- .../src/main/ets/ServiceExtAbility/service.ts | 36 ++++++++++++- .../main/ets/ServiceExtAbility/serviceStub.ts | 50 +++++++++++++++++++ 4 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts diff --git a/common/src/main/ets/util/FormatUtils.ts b/common/src/main/ets/util/FormatUtils.ts index 28ca9e0..9fcc3a2 100644 --- a/common/src/main/ets/util/FormatUtils.ts +++ b/common/src/main/ets/util/FormatUtils.ts @@ -117,6 +117,24 @@ export namespace FormatUtils { return ''; } } - 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 cf5c08c..9369571 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'; /** * 升级接口管理类 @@ -388,10 +389,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]; + if (typeof eventInfo === 'string') { + eventInfo = FormatUtils.parseJson(eventInfo); } return eventInfo; } diff --git a/product/oh/base/src/main/ets/ServiceExtAbility/service.ts b/product/oh/base/src/main/ets/ServiceExtAbility/service.ts index 446ff3c..e7cd991 100644 --- a/product/oh/base/src/main/ets/ServiceExtAbility/service.ts +++ b/product/oh/base/src/main/ets/ServiceExtAbility/service.ts @@ -19,6 +19,7 @@ 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. @@ -28,7 +29,11 @@ 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:' + FormatUtils.stringify(want)); @@ -45,7 +50,36 @@ export default class ServiceExtAbility extends Extension { onConnect(want: Want): rpc.RemoteObject { LogUtils.log(ServiceExtAbility.TAG, `onConnect , want: ${want?.abilityName}`); - return null; + 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: { + parameters: FormatUtils.parseJson(message) + } + }; + 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 From 6cfbee122e336baeb627c3f8f97fd535ba97a26d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Wed, 26 Jun 2024 20:10:01 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=90=8C=E6=AD=A5master=20=E5=88=B0beta1?= =?UTF-8?q?=20Signed-off-by:=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 --- AppScope/app.json5 | 4 ++-- common/src/main/ets/util/FormatUtils.ts | 6 +++--- feature/ota/src/main/ets/manager/OtaUpdateManager.ets | 4 +++- feature/ota/src/main/ets/manager/StateManager.ets | 1 + product/oh/base/src/main/ets/ServiceExtAbility/service.ts | 7 +++---- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/AppScope/app.json5 b/AppScope/app.json5 index b9626ec..2d0b59c 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": 2040000105, + "versionName": "204.0.0.105", "icon": "$media:app_icon", "label": "$string:app_name", "distributedNotificationEnabled": true diff --git a/common/src/main/ets/util/FormatUtils.ts b/common/src/main/ets/util/FormatUtils.ts index 9fcc3a2..bb30220 100644 --- a/common/src/main/ets/util/FormatUtils.ts +++ b/common/src/main/ets/util/FormatUtils.ts @@ -15,7 +15,7 @@ import type common from '@ohos.app.ability.common'; import { DeviceUtils } from '../util/DeviceUtils'; -import { Logutils } from '../LogUtils'; +import { LogUtils } from './LogUtils'; /** * 格式化工具 @@ -113,7 +113,7 @@ export namespace FormatUtils { try { return JSON.stringify(value); } catch (exception) { - Logutils.error('FormateUtils', 'JSON.stringify failed !!'); + LogUtils.error('FormateUtils', 'JSON.stringify failed !!'); return ''; } } @@ -133,7 +133,7 @@ export namespace FormatUtils { try { return JSON.parse(content) as T; } catch (exception) { - Logutils.error('FormateUtils', 'paramJson failed !!'); + LogUtils.error('FormateUtils', 'paramJson failed !!'); } return null; } diff --git a/feature/ota/src/main/ets/manager/OtaUpdateManager.ets b/feature/ota/src/main/ets/manager/OtaUpdateManager.ets index 9369571..a176b45 100644 --- a/feature/ota/src/main/ets/manager/OtaUpdateManager.ets +++ b/feature/ota/src/main/ets/manager/OtaUpdateManager.ets @@ -369,9 +369,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; @@ -389,7 +391,7 @@ export class OtaUpdateManager { } private wantParser(want: Want): update.EventInfo { - let eventInfo: update.EventInfo = want?.parameters?.[OtaUpdateManager.KEY]; + let eventInfo: update.EventInfo = want?.parameters?.[OtaUpdateManager.KEY] as update.EventInfo; if (typeof eventInfo === 'string') { eventInfo = FormatUtils.parseJson(eventInfo); } diff --git a/feature/ota/src/main/ets/manager/StateManager.ets b/feature/ota/src/main/ets/manager/StateManager.ets index 0697c26..ebdd7dd 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; diff --git a/product/oh/base/src/main/ets/ServiceExtAbility/service.ts b/product/oh/base/src/main/ets/ServiceExtAbility/service.ts index e7cd991..2171c11 100644 --- a/product/oh/base/src/main/ets/ServiceExtAbility/service.ts +++ b/product/oh/base/src/main/ets/ServiceExtAbility/service.ts @@ -70,14 +70,13 @@ export default class ServiceExtAbility extends Extension { }, this.connectTimeout * 1000); let want: Want = { - parameters: { - parameters: FormatUtils.parseJson(message) - } + parameters: FormatUtils.parseJson(message) }; + LogUtils.log(ServiceExtAbility.TAG, 'onConnect:' + FormatUtils.stringify(want)); OtaUpdateManager.getInstance().handleWant(want, globalThis.extensionContext); } - onDisconnect(want: want) { + onDisconnect(want: Want) { LogUtils.info(ServiceExtAbility.TAG, `onDisconnect, want: ${want?.abilityName}`); this.stopSelf(ServiceExtAbility.START_ID_CONNECT); } -- Gitee