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..46383ae405d0125ceb5b6dbd21777f7edbbe9f58 --- /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 0000000000000000000000000000000000000000..a4967620c9df1de0485d89b47bf20e6f803a59ba --- /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 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..e21a502dbc3831cb6e268e8e39b8c4cfa10d4949 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 0000000000000000000000000000000000000000..a2decd0359145c574714f8ffd243a831296fdaba --- /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' +