From dbb61b39e4e8145c1342ea15b70631f071e22da5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E5=8F=8B=E6=9D=BE?= Date: Mon, 17 Jun 2024 19:25:56 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96hap=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 --- .../src/main/ets/ServiceExtAbility/service.ts | 30 ++++++++++- .../main/ets/ServiceExtAbility/serviceStub.ts | 50 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) 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 446ff3c..3f55b29 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,30 @@ 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; + return new ServiceExtStub(ServiceExtAbility.TAG, (message: string) => { + return this.remoteMessageCallback(message); + }); + } + + 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: { + 'EventInfo': JSON.parse(message) + } + }; + OtaUpdateManager.getInstance().handleWant(want, globalThis.extensionContext); } 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..f11fdcd --- /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; + } + } +} \ No newline at end of file -- Gitee From 207528889070af5db1f0a65003463848d14f8623 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:39:13 +0800 Subject: [PATCH 2/2] =?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 --- .idea/.gitignore | 3 +++ .idea/misc.xml | 6 ++++++ .idea/modules.xml | 8 ++++++++ .idea/update_update_app_0617_hap_opt.iml | 9 +++++++++ .idea/vcs.xml | 6 ++++++ common/src/main/ets/util/FormatUtils.ts | 20 ++++++++++++++++++- .../src/main/ets/manager/OtaUpdateManager.ets | 8 ++++---- .../src/main/ets/ServiceExtAbility/service.ts | 10 ++++++++-- .../main/ets/ServiceExtAbility/serviceStub.ts | 2 +- 9 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/update_update_app_0617_hap_opt.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1a4757a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/update_update_app_0617_hap_opt.iml b/.idea/update_update_app_0617_hap_opt.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/update_update_app_0617_hap_opt.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file 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 3f55b29..e7cd991 100644 --- a/product/oh/base/src/main/ets/ServiceExtAbility/service.ts +++ b/product/oh/base/src/main/ets/ServiceExtAbility/service.ts @@ -52,9 +52,10 @@ export default class ServiceExtAbility extends Extension { 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; - return new ServiceExtStub(ServiceExtAbility.TAG, (message: string) => { + let objectIpc: rpc.RemoteObject = new ServiceExtStub(ServiceExtAbility.TAG, (message: string) => { return this.remoteMessageCallback(message); }); + return objectIpc; } private remoteMessageCallback(message: string): void { @@ -70,12 +71,17 @@ export default class ServiceExtAbility extends Extension { let want: Want = { parameters: { - 'EventInfo': JSON.parse(message) + 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 { let isTerminal: boolean = OtaUpdateManager.getInstance().isTerminal(); return isTerminal; diff --git a/product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts b/product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts index f11fdcd..a95a63b 100644 --- a/product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts +++ b/product/oh/base/src/main/ets/ServiceExtAbility/serviceStub.ts @@ -18,7 +18,7 @@ import update from '@ohos.update'; import { LogUtils } from '@ohos/common'; /** - * serviceExtAbility 远端代理存根 + * ServiceExtAbility 远端代理存根 * * @since 2024-06-17 */ -- Gitee