From 04410a88db20834f5508a0516f068e1f3d9bf810 Mon Sep 17 00:00:00 2001 From: jinyanyan Date: Tue, 1 Jul 2025 17:45:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=BD=93=E6=9F=A5=E7=9C=8B=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E4=BF=A1=E6=81=AF=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8C?= =?UTF-8?q?=E9=87=87=E7=94=A8=E5=90=8E=E5=8F=B0worker=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ets/default/model/SystemInfoWorker.ts | 128 ++++++++++++++++++ .../main/ets/MainAbility/DelayedImports.ts | 11 ++ .../src/main/ets/MainAbility/MainAbility.ts | 10 ++ .../phone/src/main/ets/pages/aboutDevice.ets | 38 +++++- .../src/main/ets/workers/systemInfoWorker.ts | 2 + 5 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 common/utils/src/main/ets/default/model/SystemInfoWorker.ts create mode 100644 product/phone/src/main/ets/MainAbility/DelayedImports.ts create mode 100644 product/phone/src/main/ets/workers/systemInfoWorker.ts 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 00000000..46383ae4 --- /dev/null +++ b/common/utils/src/main/ets/default/model/SystemInfoWorker.ts @@ -0,0 +1,128 @@ + +import Worker from '@ohos.worker' +import VolumeManager from '@ohos.file.volumeManager' + +// import volumeManager from '@ohos.file.volumeManager' +import LogUtil from '../baseUtil/LogUtil' + +import DeviceInfo from '@ohos.deviceInfo' +import ResourceMonitor from '@ohos.resourceMonitor' +import WifiManager from '@ohos.wifiManager' + +const WorkerPort = Worker.workerPort; + +const resourceMonitorProcedure = (() => { + // cpu内存信息,获取一次即可 + let response = {} + const refreshResponse = () => { + response = { + getMemInfo: ResourceMonitor.getMemInfo(), + getMemTotal: ResourceMonitor.getMemTotal(), + getCPURate: ResourceMonitor.getCPURate(), + getCPUInfo: ResourceMonitor.getCPUInfo(), + getCPULoad: ResourceMonitor.getCPULoad(), + getProcessNum: ResourceMonitor.getProcessNum(), + getThreadingNum: ResourceMonitor.getThreadingNum() + } + LogUtil.info(`systemInfoWorker resourceMonitor get response=${JSON.stringify(response)}`) + } + 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 refreshingSystemInfo = () => { + try { + systemInfo = { + getMemTotal: ResourceMonitor.getMemTotal(), + getStorageTotal: ResourceMonitor.getStorageTotal(), + getCPUInfo: ResourceMonitor.getCPUInfo(), + getCPUMasterCoreFrequency: ResourceMonitor.getCPUMasterCoreFrequency(), + getCPUSubCoreFrequency: ResourceMonitor.getCPUSubCoreFrequency(), + getKernelVersion: ResourceMonitor.getKernelVersion() + } + } + catch(error) { + LogUtil.error(`refreshingSystemInfo fail error=${error}`) + } + } + const refreshDeviceInfo = () => { + try { + deviceInfo = { + productModel: DeviceInfo.productModel, + manufacture: DeviceInfo.manufacture, + serial: DeviceInfo.serial, + displayVersion: DeviceInfo.displayVersion + } + } catch(error) { + LogUtil.error(`refreshingSystemInfo fail error=${error}`) + } + } + return async (page, isRefreshingCache = false) => { + if (Object.getOwnPropertyNames(systemInfo).length === 0 || isRefreshingCache) { + // 内存磁盘总量,cpu信息,获取一次即可,且避免app启动时就执行 + // 如果需要反复刷新,则令isRefreshingCache=true + // 对于需要反复刷新参数的场景下,则每次都刷新systemInfo + refreshingSystemInfo() + LogUtil.info(`systemInfoWorker aboutDevice get systemInfo=${JSON.stringify(systemInfo)}`) + } + if (Object.getOwnPropertyNames(deviceInfo).length === 0 || isRefreshingCache) { + // 设备序列号,制造商等信息,获取一次即可 + // 如果需要反复刷新,则令isRefreshingCache=true + // 对于需要反复刷新参数的场景下,则每次都刷新deviceInfo + refreshDeviceInfo() + LogUtil.info(`systemInfoWorker aboutDevice get deviceInfo=${JSON.stringify(deviceInfo)}`) + } + // wifi mac地址信息,可能因为开关wifi而更新或禁用 + try { + systemInfo["getDeviceMacAddress"] = WifiManager.getDeviceMacAddress() + } catch (error) { + LogUtil.info(`systemInfoWorker aboutDevice error 2`) + } + LogUtil.info(`systemInfoWorker aboutDevice 1`) + // volumes(sd卡信息)需要每次获取 + let volumes = [] + try { + await VolumeManager.getAllVolumes().then((internal_para_volumes)=> { + volumes = internal_para_volumes + }) + } catch (error) { + LogUtil.info(`systemInfoWorker aboutDevice error 1`) + } + LogUtil.info(`systemInfoWorker aboutDevice 2`) + 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=${JSON.stringify(event)}`) + // loadVolumeManager() + 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/src/main/ets/MainAbility/DelayedImports.ts b/product/phone/src/main/ets/MainAbility/DelayedImports.ts new file mode 100644 index 00000000..a4967620 --- /dev/null +++ b/product/phone/src/main/ets/MainAbility/DelayedImports.ts @@ -0,0 +1,11 @@ +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 +} \ 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 9bb5fbbe..d57e0b0c 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 7cb4cf91..e21a502d 100644 --- a/product/phone/src/main/ets/pages/aboutDevice.ets +++ b/product/phone/src/main/ets/pages/aboutDevice.ets @@ -20,7 +20,7 @@ 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 deviceInfo from '@ohos.deviceInfo'; import { BaseData } from '../../../../../../common/utils/src/main/ets/default/bean/BaseData'; /** @@ -30,7 +30,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 +94,35 @@ struct AboutDevice { .height(ConfigData.WH_100_100) } + requestDeviceInfo() { + const systemInfoWorker = globalThis.systemInfoWorker + const CurrentPage = "aboutDevice" + systemInfoWorker.onmessage = (output) => { + LogUtil.info(`systemInfoWorker aboutDevice output=${JSON.stringify(output)}`) + const data = output.data + if (data.page === CurrentPage) { + const response = data.response + if (response) { + const error = response.error + const volumes = response.volumes + const deviceInfo = response.deviceInfo + const systemInfo = response.systemInfo + + this.aboutDeviceList = AboutDeviceModel.getAboutDeviceInfoListener(); + this.getDeviceInfo(deviceInfo) + LogUtil.info(ConfigData.TAG + 'settings get device info' + + JSON.stringify(AboutDeviceModel.setOnAboutDeviceListener())); + + LogUtil.info(ConfigData.TAG + `deviceInfo=${JSON.stringify(deviceInfo)}, systemInfo=${JSON.stringify(systemInfo)}`); + } + } + } + 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 +131,8 @@ struct AboutDevice { AppStorage.SetOrCreate("systemName", AboutDeviceModel.getSystemName()) } - private getDeviceInfo(): void { + private getDeviceInfo(deviceInfo): 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 00000000..a2decd03 --- /dev/null +++ b/product/phone/src/main/ets/workers/systemInfoWorker.ts @@ -0,0 +1,2 @@ +import '../../../../../../common/utils/src/main/ets/default/model/SystemInfoWorker.ts' + -- Gitee From 0c870a50783ef0725047ec5f8504fd86a5d27bf9 Mon Sep 17 00:00:00 2001 From: jinyanyan Date: Tue, 1 Jul 2025 18:24:05 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=BD=93app=E5=88=87=E6=8D=A2=E5=88=B0?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8C=E5=8F=91?= =?UTF-8?q?=E9=80=81=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E5=B0=86=E7=82=B9=E4=B8=8B=E6=8C=89=E9=92=AE=E9=A2=9C=E8=89=B2?= =?UTF-8?q?=E5=8E=BB=E6=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/default/WatchAbilityEvent.ts | 39 +++++++++++++++++++ .../src/main/ets/default/entryComponent.ets | 22 +++++++++++ .../src/main/ets/MainAbility/MainAbility.ts | 5 +++ 3 files changed, 66 insertions(+) create mode 100644 common/component/src/main/ets/default/WatchAbilityEvent.ts diff --git a/common/component/src/main/ets/default/WatchAbilityEvent.ts b/common/component/src/main/ets/default/WatchAbilityEvent.ts new file mode 100644 index 00000000..e0bb292a --- /dev/null +++ b/common/component/src/main/ets/default/WatchAbilityEvent.ts @@ -0,0 +1,39 @@ + +import Emitter from '@ohos.events.emitter' +import LogUtil from '../../../../../utils/src/main/ets/default/baseUtil/LogUtil'; + +export const AppGoBackgoundEventId = 1314 +export const AppGoForegoundEventId = 1315 + +const TAG = "WatchAbilityEvent-> " + +export function addBackgroundEvent(ability) { + // app切换到后台的时候发一个消息 + const prevOnBackground = ability.onBackground + ability.onBackground = (function() { + prevOnBackground() + + const event: Emitter.InnerEvent = { + eventId: AppGoBackgoundEventId, + priority: Emitter.EventPriority.HIGH + } + Emitter.emit(event) + LogUtil.info(TAG + 'onBackground') + }).bind(ability) +} + + +export function addForegroundEvent(ability) { + // app切换到前台的时候发一个消息 + const prevOnBackground = ability.onForeground + ability.onForeground = (function() { + prevOnBackground() + + const event: Emitter.InnerEvent = { + eventId: AppGoForegoundEventId, + priority: Emitter.EventPriority.HIGH + } + Emitter.emit(event) + LogUtil.info(TAG + 'onBackground') + }).bind(ability) +} \ No newline at end of file diff --git a/common/component/src/main/ets/default/entryComponent.ets b/common/component/src/main/ets/default/entryComponent.ets index e8af23e0..a4b67e90 100644 --- a/common/component/src/main/ets/default/entryComponent.ets +++ b/common/component/src/main/ets/default/entryComponent.ets @@ -15,7 +15,11 @@ import ComponentConfig from './ComponentConfig'; import ConfigData from '../../../../../utils/src/main/ets/default/baseUtil/ConfigData'; +import Emitter from '@ohos.events.emitter' +import {AppGoBackgoundEventId, AppGoForegoundEventId} from './WatchAbilityEvent.ts' +import LogUtil from '../../../../../utils/src/main/ets/default/baseUtil/LogUtil'; +const TAG = "EntryComponent->" /** * item custom component */ @@ -39,6 +43,20 @@ export default struct EntryComponent { private fontSize ? = $r('sys.float.ohos_id_text_size_body1'); private valueFontSize ? = $r('sys.float.ohos_id_text_size_body2'); + appGoBackgoundEventCallback() { + // 退到后台时按钮避免按下的颜色 + this.isTouched = false; + LogUtil.info(TAG + 'bluetooth go background'); + } + + aboutToAppear(): void { + Emitter.on({eventId: AppGoBackgoundEventId}, this.appGoBackgoundEventCallback.bind(this)) + } + + aboutToDisappear(): void { + Emitter.off(AppGoBackgoundEventId, this.appGoBackgoundEventCallback.bind(this)) + } + build() { Row() { Row() { @@ -140,6 +158,10 @@ export default struct EntryComponent { if (event?.type === TouchType.Up) { this.isTouched = false; } + + if (event?.type === TouchType.Up) { + this.isTouched = false; + } }) } } \ 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 d57e0b0c..8738caa5 100644 --- a/product/phone/src/main/ets/MainAbility/MainAbility.ts +++ b/product/phone/src/main/ets/MainAbility/MainAbility.ts @@ -31,6 +31,11 @@ export default class MainAbility extends Ability { const module = await import('./DelayedImports.ts') module.createSystemWorkers(this) LogUtil.info('DelayedImports executed') + + const watchAbilityEvent = await import('../../../../../../common/component/src/main/ets/default/WatchAbilityEvent.ts') + watchAbilityEvent.addBackgroundEvent(this) + watchAbilityEvent.addForegroundEvent(this) + } catch(error) { LogUtil.error('import DelayedImports faild') } -- Gitee