diff --git a/common/utils/src/main/ets/default/model/SystemInfoWorker.ts b/common/utils/src/main/ets/default/model/SystemInfoWorker.ts new file mode 100644 index 0000000000000000000000000000000000000000..3a4615ba8add6e5d5b14425ee0af2d72c0379173 --- /dev/null +++ b/common/utils/src/main/ets/default/model/SystemInfoWorker.ts @@ -0,0 +1,108 @@ +/** + * Copyright (c) 2021-2022 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 Worker from '@ohos.worker' +import VolumeManager from '@ohos.file.volumeManager' + +import LogUtil from '../baseUtil/LogUtil' + +import DeviceInfo from '@ohos.deviceInfo' +import WifiManager from '@ohos.wifiManager' + +const WorkerPort = Worker.workerPort; + +const resourceMonitorProcedure = (() => { + // cpu内存信息,获取一次即可 + let response = {} + const refreshResponse = () => { + LogUtil.info(`refreshResponse`) + } + return (page, isRefreshingCache = false) => { + if (Object.getOwnPropertyNames(response).length === 0 || isRefreshingCache) { + // 避免app启动时就执行,但同时又保证只执行一次 + // 如果需要反复刷新,则令isRefreshingCache=true + refreshResponse() + } + WorkerPort.postMessage({ page: page, response: response }) + } +})() + +const aboutDeviceProcedure = (() => { + let systemInfo = {} + let deviceInfo = {} + const refreshDeviceInfo = () => { + try { + deviceInfo = { + productModel: DeviceInfo.productModel, + manufacture: DeviceInfo.manufacture, + serial: DeviceInfo.serial, + displayVersion: DeviceInfo.displayVersion + } + } catch(error) { + LogUtil.error(`refreshDeviceInfo fail, code=${error?.code}, message=${error?.message}`) + } + } + return async (page, isRefreshingCache = false) => { + if (Object.getOwnPropertyNames(deviceInfo).length === 0 || isRefreshingCache) { + // 设备序列号,制造商等信息,获取一次即可 + // 如果需要反复刷新,则令isRefreshingCache=true + // 对于需要反复刷新参数的场景下,则每次都刷新deviceInfo + refreshDeviceInfo() + LogUtil.info(`systemInfoWorker aboutDevice get deviceInfo`) + } + // wifi mac地址信息,可能因为开关wifi而更新或禁用 + try { + systemInfo["getDeviceMacAddress"] = WifiManager.getDeviceMacAddress() + } catch (error) { + LogUtil.error(`getDeviceMacAddress fail, code=${error?.code}, message=${error?.message}`) + } + LogUtil.info('SystemInfoWorker aboutDevice get mac address end.') + // volumes(sd卡信息)需要每次获取 + let volumes = [] + try { + await VolumeManager.getAllVolumes().then((internal_para_volumes)=> { + volumes = internal_para_volumes + }) + } catch (error) { + LogUtil.error(`getAllVolumes fail, code=${error?.code}, message=${error?.message}`) + } + LogUtil.info('SystemInfoWorker aboutDevice get all volumes end.') + WorkerPort.postMessage({ + page: page, + response: { + volumes: volumes, + deviceInfo: deviceInfo, + systemInfo: systemInfo + } + }) + LogUtil.info(`systemInfoWorker aboutDevice end`) + } +})() + +export async function onMessageEvent(event) { + const data = event.data + const page = data.page + LogUtil.info(`systemInfoWorker onmessage page=${page}`) + if (page === "resourceMonitor") { + // resource-monitor的内容每次都需要刷新 + resourceMonitorProcedure(page, true) + } else if (page === "aboutDevice") { + aboutDeviceProcedure(page) + } else { + LogUtil.error(`systemInfoWorker default else, page=${page}`) + } +} + +WorkerPort.onmessage = onMessageEvent diff --git a/product/phone/package.json b/product/phone/package.json new file mode 100644 index 0000000000000000000000000000000000000000..1b845dbb47a8cf90f77c9d0b1bbfa4f3039f4ef7 --- /dev/null +++ b/product/phone/package.json @@ -0,0 +1,9 @@ +{ + "license": "ISC", + "devDependencies": {}, + "name": "phone", + "description": "example description", + "repository": {}, + "version": "1.0.0", + "dependencies": {} +} \ No newline at end of file diff --git a/product/phone/src/main/ets/MainAbility/DelayedImports.ts b/product/phone/src/main/ets/MainAbility/DelayedImports.ts new file mode 100644 index 0000000000000000000000000000000000000000..e598ddb3860125bf4ce50a23e1fa697df53dee18 --- /dev/null +++ b/product/phone/src/main/ets/MainAbility/DelayedImports.ts @@ -0,0 +1,30 @@ +/** + * Copyright (c) 2021-2022 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 Worker from '@ohos.worker'; + +import Emitter from '@ohos.events.emitter' +import LogUtil from '../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil' + +const TAG = 'MainAbility->DelayedImports' +export async function createSystemWorkers(ability) { + // 创建systemInfoWorker(关于,系统资源监控页面) + const systemInfoWorker = new Worker.ThreadWorker("phone/ets/workers/systemInfoWorker.ts") + globalThis.systemInfoWorker = systemInfoWorker +} + +export function getSystemWorker(): Worker.ThreadWorker { + return globalThis.systemInfoWorker as Worker.ThreadWorker +} \ No newline at end of file diff --git a/product/phone/src/main/ets/MainAbility/MainAbility.ts b/product/phone/src/main/ets/MainAbility/MainAbility.ts index 9bb5fbbed4c4b8575881188b96fbe9dbcee7b135..d57e0b0c7cbdf4be244b23dc5cd10be9334243e6 100644 --- a/product/phone/src/main/ets/MainAbility/MainAbility.ts +++ b/product/phone/src/main/ets/MainAbility/MainAbility.ts @@ -25,6 +25,16 @@ export default class MainAbility extends Ability { this.funcAbilityWant = want; GlobalContext.getContext().setObject(GlobalContext.globalKeyAbilityWant, want); GlobalContext.getContext().setObject(GlobalContext.globalKeySettingsAbilityContext, this.context); + + setTimeout(async () => { + try { + const module = await import('./DelayedImports.ts') + module.createSystemWorkers(this) + LogUtil.info('DelayedImports executed') + } catch(error) { + LogUtil.error('import DelayedImports faild') + } + }, 500) } onDestroy() { diff --git a/product/phone/src/main/ets/pages/aboutDevice.ets b/product/phone/src/main/ets/pages/aboutDevice.ets index 7cb4cf91c8e529693421b8ac9f9b4dbdaf716fbf..6a8019e24f27ed601c9a3a3507d2e4440e7915b8 100644 --- a/product/phone/src/main/ets/pages/aboutDevice.ets +++ b/product/phone/src/main/ets/pages/aboutDevice.ets @@ -20,8 +20,26 @@ import HeadComponent from '../../../../../../common/component/src/main/ets/defau import { SubEntryComponentWithEndText } from '../../../../../../common/component/src/main/ets/default/subEntryComponent'; -import deviceInfo from '@ohos.deviceInfo'; import { BaseData } from '../../../../../../common/utils/src/main/ets/default/bean/BaseData'; +import {getSystemWorker} from '../MainAbility/DelayedImports'; +import Worker from '@ohos.worker' +import {MessageEvents} from '@ohos.worker'; + + +interface DeviceInfoType { + productModel: string, + manufacture: string, + serial: string, + displayVersion: string +} + +interface ResponseType { + deviceInfo: DeviceInfoType +} +interface DataType { + page: string, + response: ResponseType +} /** * about phone @@ -30,7 +48,7 @@ import { BaseData } from '../../../../../../common/utils/src/main/ets/default/be @Component struct AboutDevice { @StorageLink("systemName") systemName: string = AboutDeviceModel.getSystemName(); - private aboutDeviceList: BaseData[] = []; + @State aboutDeviceList: BaseData[] = []; build() { Column() { @@ -94,12 +112,33 @@ struct AboutDevice { .height(ConfigData.WH_100_100) } + requestDeviceInfo() { + const systemInfoWorker = getSystemWorker() + if (!systemInfoWorker) { + return + } + const currentPage = 'aboutDevice' + systemInfoWorker.onmessage = (output: MessageEvents) => { + LogUtil.info(`systemInfoWorker aboutDevice output=${JSON.stringify(output)}`) + const data = output.data as DataType + if (data.page === currentPage) { + const response = data.response + if (response) { + const deviceInfo = response.deviceInfo + + this.aboutDeviceList = AboutDeviceModel.getAboutDeviceInfoListener(); + this.getDeviceInfo(deviceInfo) + + LogUtil.info(ConfigData.TAG + `deviceInfo=${JSON.stringify(deviceInfo)}}`); + } + } + } + systemInfoWorker.postMessage({ page: currentPage, request: 'none' }) + } + aboutToAppear(): void { LogUtil.info(ConfigData.TAG + 'settings get device info come in'); - this.aboutDeviceList = AboutDeviceModel.getAboutDeviceInfoListener(); - this.getDeviceInfo(); - LogUtil.info(ConfigData.TAG + 'settings get device info' + - JSON.stringify(AboutDeviceModel.setOnAboutDeviceListener())); + this.requestDeviceInfo() LogUtil.info(ConfigData.TAG + 'settings get device info end in'); } @@ -108,7 +147,8 @@ struct AboutDevice { AppStorage.SetOrCreate("systemName", AboutDeviceModel.getSystemName()) } - private getDeviceInfo(): void { + private getDeviceInfo(deviceInfo: DeviceInfoType): void { + LogUtil.info(ConfigData.TAG + `getDeviceInfo deviceInfo=${JSON.stringify(deviceInfo)}`); for (let item of this.aboutDeviceList) { let value = item.settingAlias; diff --git a/product/phone/src/main/ets/workers/systemInfoWorker.ts b/product/phone/src/main/ets/workers/systemInfoWorker.ts new file mode 100644 index 0000000000000000000000000000000000000000..8d0df8ed86958ea512bac66e9b0f775f4fc6bae0 --- /dev/null +++ b/product/phone/src/main/ets/workers/systemInfoWorker.ts @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2021-2022 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 '../../../../../../common/utils/src/main/ets/default/model/SystemInfoWorker.ts' +