diff --git a/AppScope/app.json5 b/AppScope/app.json5 index b9626ec0b82881515db9379a792e0a17316e2cb0..2d0b59c916b3c5a6c463c4cff3223587e61bae2e 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 28ca9e05c2af3ee4e4c58ac5c8fdba0354157b70..bb302209ad90fc83ab8454ca879f5f908d559d8e 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,10 +113,28 @@ export namespace FormatUtils { try { return JSON.stringify(value); } catch (exception) { - Logutils.error('FormateUtils', 'JSON.stringify failed !!'); + LogUtils.error('FormateUtils', 'JSON.stringify failed !!'); 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 cf5c08c332b9cba5baee9a27403496855fac8780..a176b455e68a6a35d0471a339aa9a0a7c8fbf609 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'; /** * 升级接口管理类 @@ -368,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; @@ -388,10 +391,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 0697c260fbfd32066021f580e6d642e7501036e4..ebdd7dda52481967739980cfa6dedb6e84703276 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 446ff3ccd3a8b2d97a3501d1a5f3715154cb68c7..2171c1154bffe0d6ac6bc510f934fb7a0cd2e2a4 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,35 @@ 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: 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 0000000000000000000000000000000000000000..49ca3055b2769d94b91b879862050482c3661d5b --- /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; + } + } +}