From 39f619004e5283d74f80bb9ed2afac5c56c09628 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 15:46:30 +0800 Subject: [PATCH 1/3] =?UTF-8?q?hap=20=E4=BE=9D=E8=B5=96=E4=BC=98=E5=8C=96?= =?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 --- common/src/main/ets/util/FormatUtils.ts | 20 ++++++++++- .../src/main/ets/manager/OtaUpdateManager.ets | 8 ++--- .../src/main/ets/ServiceExtAbility/service.ts | 36 ++++++++++++++++++- 3 files changed, 58 insertions(+), 6 deletions(-) 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 { -- Gitee From 9684cfce95f5bbf7bb793b7f5228516328295b17 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:50:39 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9hap=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 --- .../main/ets/ServiceExtAbility/serviceStub.ts | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts 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..c07d080 --- /dev/null +++ b/product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts @@ -0,0 +1,45 @@ +/* + * 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 c5cdc24e3cfa0a99994167e510cac8033de31711 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 11:56:22 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9license?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 邹友松 --- .../oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts b/product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts index c07d080..49ca305 100644 --- a/product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts +++ b/product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts @@ -1,4 +1,9 @@ /* + * 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 -- Gitee