16e80583aSopenharmony_ci/** 26e80583aSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 36e80583aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 46e80583aSopenharmony_ci * you may not use this file except in compliance with the License. 56e80583aSopenharmony_ci * You may obtain a copy of the License at 66e80583aSopenharmony_ci * 76e80583aSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 86e80583aSopenharmony_ci * 96e80583aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 106e80583aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 116e80583aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 126e80583aSopenharmony_ci * See the License for the specific language governing permissions and 136e80583aSopenharmony_ci * limitations under the License. 146e80583aSopenharmony_ci */ 156e80583aSopenharmony_ci 166e80583aSopenharmony_ciimport { Log } from '../utils/Log'; 176e80583aSopenharmony_ciimport FileUtils from '../utils/FileUtils'; 186e80583aSopenharmony_ciimport GridLayoutUtil from '../utils/GridLayoutUtil'; 196e80583aSopenharmony_ciimport { CommonConstants } from '../constants/CommonConstants'; 206e80583aSopenharmony_ciimport { RecentsModeConfig } from '../layoutconfig/RecentsModeConfig'; 216e80583aSopenharmony_ciimport { layoutConfigManager } from '../layoutconfig/LayoutConfigManager'; 226e80583aSopenharmony_ciimport { settingsDataManager } from '../manager/SettingsDataManager'; 236e80583aSopenharmony_ciimport { PageDesktopModeConfig } from '../layoutconfig/PageDesktopModeConfig'; 246e80583aSopenharmony_ciimport { PageDesktopLayoutConfig } from '../layoutconfig/PageDesktopLayoutConfig'; 256e80583aSopenharmony_ciimport { PageDesktopAppModeConfig } from '../layoutconfig/PageDesktopAppModeConfig'; 266e80583aSopenharmony_ciimport { SettingsModelObserver } from './SettingsModelObserver'; 276e80583aSopenharmony_ciimport GridLayoutConfigs from '../configs/GridLayoutConfigs'; 286e80583aSopenharmony_ciimport dataShare from '@ohos.data.dataShare'; 296e80583aSopenharmony_ciimport { GridLayoutInfo } from '../interface'; 306e80583aSopenharmony_ci 316e80583aSopenharmony_ciconst TAG = 'SettingsModel'; 326e80583aSopenharmony_ci 336e80583aSopenharmony_ci/** 346e80583aSopenharmony_ci * Data model for launcher settings ability. 356e80583aSopenharmony_ci */ 366e80583aSopenharmony_ciexport class SettingsModel { 376e80583aSopenharmony_ci static readonly EVENT_FORCE_RELOAD: number = 1; 386e80583aSopenharmony_ci private static readonly DEFAULT_VALUE: string = '1'; 396e80583aSopenharmony_ci private readonly mPageDesktopModeConfig: PageDesktopModeConfig; 406e80583aSopenharmony_ci private readonly mPageDesktopLayoutConfig: PageDesktopLayoutConfig; 416e80583aSopenharmony_ci private readonly mRecentsModeConfig: RecentsModeConfig; 426e80583aSopenharmony_ci private readonly mPageDesktopAppModeConfig: PageDesktopAppModeConfig; 436e80583aSopenharmony_ci private mGridConfig = 1; 446e80583aSopenharmony_ci private mGridLayoutTable = GridLayoutConfigs.GridLayoutTable; 456e80583aSopenharmony_ci private readonly uri: string = ''; 466e80583aSopenharmony_ci private helper: dataShare.DataShareHelper | null = null; 476e80583aSopenharmony_ci private readonly mObserverList: SettingsModelObserver[] = []; 486e80583aSopenharmony_ci 496e80583aSopenharmony_ci private constructor() { 506e80583aSopenharmony_ci this.mPageDesktopModeConfig = PageDesktopModeConfig.getInstance(); 516e80583aSopenharmony_ci const deviceType = this.mPageDesktopModeConfig.getDeviceType(); 526e80583aSopenharmony_ci if (deviceType == CommonConstants.DEFAULT_DEVICE_TYPE) { 536e80583aSopenharmony_ci this.mGridLayoutTable = GridLayoutConfigs.GridLayoutTable; 546e80583aSopenharmony_ci } else if (deviceType == CommonConstants.PAD_DEVICE_TYPE) { 556e80583aSopenharmony_ci this.mGridLayoutTable = GridLayoutConfigs.PadGridLayoutTableHorizontal; 566e80583aSopenharmony_ci } else { 576e80583aSopenharmony_ci this.mGridLayoutTable = GridLayoutConfigs.GridLayoutTableHorizontal; 586e80583aSopenharmony_ci } 596e80583aSopenharmony_ci this.mPageDesktopLayoutConfig = layoutConfigManager.getFunctionConfig<PageDesktopLayoutConfig>(PageDesktopLayoutConfig.GRID_LAYOUT_INFO); 606e80583aSopenharmony_ci this.mRecentsModeConfig = layoutConfigManager.getModeConfig(RecentsModeConfig.RECENT_MISSIONS_MODE_CONFIG); 616e80583aSopenharmony_ci this.mPageDesktopAppModeConfig = layoutConfigManager.getModeConfig(PageDesktopAppModeConfig.DESKTOP_APPLICATION_INFO); 626e80583aSopenharmony_ci this.uri = settingsDataManager.getUri(CommonConstants.NAVIGATION_BAR_STATUS_KEY); 636e80583aSopenharmony_ci this.helper = settingsDataManager.getHelper(globalThis.desktopContext, this.uri); 646e80583aSopenharmony_ci } 656e80583aSopenharmony_ci 666e80583aSopenharmony_ci static getInstance(): SettingsModel { 676e80583aSopenharmony_ci if (globalThis.SettingsModelInstance == null) { 686e80583aSopenharmony_ci globalThis.SettingsModelInstance = new SettingsModel(); 696e80583aSopenharmony_ci } 706e80583aSopenharmony_ci return globalThis.SettingsModelInstance; 716e80583aSopenharmony_ci } 726e80583aSopenharmony_ci 736e80583aSopenharmony_ci addObserver(observer: SettingsModelObserver): void { 746e80583aSopenharmony_ci Log.showDebug(TAG, 'addObserver'); 756e80583aSopenharmony_ci this.mObserverList.push(observer); 766e80583aSopenharmony_ci } 776e80583aSopenharmony_ci 786e80583aSopenharmony_ci private notifyObservers(event: number): void { 796e80583aSopenharmony_ci Log.showDebug(TAG, 'notifyObservers'); 806e80583aSopenharmony_ci for (let i = 0; i < this.mObserverList.length; i++) { 816e80583aSopenharmony_ci this.mObserverList[i](event); 826e80583aSopenharmony_ci } 836e80583aSopenharmony_ci } 846e80583aSopenharmony_ci 856e80583aSopenharmony_ci /** 866e80583aSopenharmony_ci * force reload all config from disk. 876e80583aSopenharmony_ci */ 886e80583aSopenharmony_ci forceReloadConfig(): void { 896e80583aSopenharmony_ci if (this.mPageDesktopModeConfig) { 906e80583aSopenharmony_ci this.mPageDesktopModeConfig.forceReloadConfig(); 916e80583aSopenharmony_ci } 926e80583aSopenharmony_ci if (this.mPageDesktopLayoutConfig) { 936e80583aSopenharmony_ci this.mPageDesktopLayoutConfig.forceReloadConfig(); 946e80583aSopenharmony_ci } 956e80583aSopenharmony_ci if (this.mPageDesktopAppModeConfig) { 966e80583aSopenharmony_ci this.mPageDesktopAppModeConfig.forceReloadConfig(); 976e80583aSopenharmony_ci } 986e80583aSopenharmony_ci if (this.mRecentsModeConfig) { 996e80583aSopenharmony_ci this.mRecentsModeConfig.forceReloadConfig(); 1006e80583aSopenharmony_ci } 1016e80583aSopenharmony_ci this.notifyObservers(1); 1026e80583aSopenharmony_ci } 1036e80583aSopenharmony_ci 1046e80583aSopenharmony_ci /** 1056e80583aSopenharmony_ci * Get the grid view presetting collection of layout config information table. 1066e80583aSopenharmony_ci * 1076e80583aSopenharmony_ci * @return {object} Grid view presetting collection object. 1086e80583aSopenharmony_ci */ 1096e80583aSopenharmony_ci getGridLayoutTable(): any { 1106e80583aSopenharmony_ci return this.mGridLayoutTable; 1116e80583aSopenharmony_ci } 1126e80583aSopenharmony_ci 1136e80583aSopenharmony_ci /** 1146e80583aSopenharmony_ci * Get default layout information of grid view. 1156e80583aSopenharmony_ci * 1166e80583aSopenharmony_ci * @return {object} Default layout information of grid view. 1176e80583aSopenharmony_ci */ 1186e80583aSopenharmony_ci getDefaultLayoutInfo(): any { 1196e80583aSopenharmony_ci let defaultLayoutInfoFilePath = globalThis.desktopContext.filesDir + '/layoutInfo.json'; 1206e80583aSopenharmony_ci return FileUtils.readJsonFile(defaultLayoutInfoFilePath); 1216e80583aSopenharmony_ci } 1226e80583aSopenharmony_ci 1236e80583aSopenharmony_ci /** 1246e80583aSopenharmony_ci * Get layout config of grid view. 1256e80583aSopenharmony_ci * 1266e80583aSopenharmony_ci * @return {object} Layout config of grid view. 1276e80583aSopenharmony_ci */ 1286e80583aSopenharmony_ci getGridConfig(): any { 1296e80583aSopenharmony_ci this.mGridConfig = this.mPageDesktopModeConfig.getGridConfig(); 1306e80583aSopenharmony_ci let gridLayout = this.mGridLayoutTable[0]; 1316e80583aSopenharmony_ci for (let i = 0; i < this.mGridLayoutTable.length; i++) { 1326e80583aSopenharmony_ci if (this.mGridLayoutTable[i].id == this.mGridConfig) { 1336e80583aSopenharmony_ci gridLayout = this.mGridLayoutTable[i]; 1346e80583aSopenharmony_ci break; 1356e80583aSopenharmony_ci } 1366e80583aSopenharmony_ci } 1376e80583aSopenharmony_ci return gridLayout; 1386e80583aSopenharmony_ci } 1396e80583aSopenharmony_ci 1406e80583aSopenharmony_ci /** 1416e80583aSopenharmony_ci * Set layout config id of grid view. 1426e80583aSopenharmony_ci * 1436e80583aSopenharmony_ci * @param gridConfig - Layout config id of grid view. 1446e80583aSopenharmony_ci */ 1456e80583aSopenharmony_ci setGridConfig(gridConfig) { 1466e80583aSopenharmony_ci this.mPageDesktopModeConfig.updateGridConfig(gridConfig); 1476e80583aSopenharmony_ci 1486e80583aSopenharmony_ci const config = this.getGridConfig(); 1496e80583aSopenharmony_ci const gridLayoutInfo = this.mPageDesktopLayoutConfig.getGridLayoutInfo(); 1506e80583aSopenharmony_ci this.mPageDesktopLayoutConfig.updateGridLayoutInfo(GridLayoutUtil.updateGridLayoutInfo( 1516e80583aSopenharmony_ci gridLayoutInfo, config.row, config.column 1526e80583aSopenharmony_ci )); 1536e80583aSopenharmony_ci this.forceReloadConfig(); 1546e80583aSopenharmony_ci } 1556e80583aSopenharmony_ci 1566e80583aSopenharmony_ci /** 1576e80583aSopenharmony_ci * Get appList config of workspace view. 1586e80583aSopenharmony_ci * 1596e80583aSopenharmony_ci * @return {object} appList config of workspace view. 1606e80583aSopenharmony_ci */ 1616e80583aSopenharmony_ci getAppListInfo(): any { 1626e80583aSopenharmony_ci return this.mPageDesktopAppModeConfig.getAppListInfo(); 1636e80583aSopenharmony_ci } 1646e80583aSopenharmony_ci 1656e80583aSopenharmony_ci /** 1666e80583aSopenharmony_ci * Determine if there is an application in the workspace. 1676e80583aSopenharmony_ci * 1686e80583aSopenharmony_ci * @return {boolean} true(exist). 1696e80583aSopenharmony_ci */ 1706e80583aSopenharmony_ci isAppListInfoExist(): boolean { 1716e80583aSopenharmony_ci return this.mPageDesktopAppModeConfig.isConfigExist(); 1726e80583aSopenharmony_ci } 1736e80583aSopenharmony_ci 1746e80583aSopenharmony_ci /** 1756e80583aSopenharmony_ci * Set layout config id of grid view. 1766e80583aSopenharmony_ci * 1776e80583aSopenharmony_ci * @param gridConfig - Layout config id of grid view. 1786e80583aSopenharmony_ci */ 1796e80583aSopenharmony_ci setAppListInfo(appList): void { 1806e80583aSopenharmony_ci this.mPageDesktopAppModeConfig.updateAppListInfo(appList); 1816e80583aSopenharmony_ci } 1826e80583aSopenharmony_ci 1836e80583aSopenharmony_ci /** 1846e80583aSopenharmony_ci * Get the layout view type. 1856e80583aSopenharmony_ci * 1866e80583aSopenharmony_ci * @return {string} Layout view type, should one of 'Grid' or 'List' which is stored in LayoutConstants class. 1876e80583aSopenharmony_ci */ 1886e80583aSopenharmony_ci getAppPageStartConfig(): any { 1896e80583aSopenharmony_ci return this.mPageDesktopModeConfig.getAppStartPageType(); 1906e80583aSopenharmony_ci } 1916e80583aSopenharmony_ci 1926e80583aSopenharmony_ci /** 1936e80583aSopenharmony_ci * Set the layout view type. 1946e80583aSopenharmony_ci * 1956e80583aSopenharmony_ci * @param {string} type - Layout view type, should one of 'Grid' or 'List' which is stored in LayoutConstants class. 1966e80583aSopenharmony_ci */ 1976e80583aSopenharmony_ci setAppPageStartConfig(type): void { 1986e80583aSopenharmony_ci this.mPageDesktopModeConfig.updateAppStartPageType(type); 1996e80583aSopenharmony_ci } 2006e80583aSopenharmony_ci 2016e80583aSopenharmony_ci /** 2026e80583aSopenharmony_ci * Set the device type. 2036e80583aSopenharmony_ci * 2046e80583aSopenharmony_ci * @param {string} deviceType - device type. 2056e80583aSopenharmony_ci */ 2066e80583aSopenharmony_ci setDevice(deviceType): void { 2076e80583aSopenharmony_ci Log.showDebug(TAG, `setDevice ${deviceType}`); 2086e80583aSopenharmony_ci if (deviceType == CommonConstants.DEFAULT_DEVICE_TYPE) { 2096e80583aSopenharmony_ci this.mGridLayoutTable = GridLayoutConfigs.GridLayoutTable; 2106e80583aSopenharmony_ci } else if (deviceType == CommonConstants.PAD_DEVICE_TYPE) { 2116e80583aSopenharmony_ci this.mGridLayoutTable = GridLayoutConfigs.PadGridLayoutTableHorizontal; 2126e80583aSopenharmony_ci } else { 2136e80583aSopenharmony_ci this.mGridLayoutTable = GridLayoutConfigs.GridLayoutTableHorizontal; 2146e80583aSopenharmony_ci } 2156e80583aSopenharmony_ci this.mPageDesktopModeConfig.updateDeviceType(deviceType); 2166e80583aSopenharmony_ci } 2176e80583aSopenharmony_ci 2186e80583aSopenharmony_ci /** 2196e80583aSopenharmony_ci * get the device type. 2206e80583aSopenharmony_ci * 2216e80583aSopenharmony_ci * @return {string} device type 2226e80583aSopenharmony_ci */ 2236e80583aSopenharmony_ci getDevice(): string { 2246e80583aSopenharmony_ci return this.mPageDesktopModeConfig.getDeviceType(); 2256e80583aSopenharmony_ci } 2266e80583aSopenharmony_ci 2276e80583aSopenharmony_ci /** 2286e80583aSopenharmony_ci * Get layout information of grid view. 2296e80583aSopenharmony_ci * 2306e80583aSopenharmony_ci * @return {object} layout information. 2316e80583aSopenharmony_ci */ 2326e80583aSopenharmony_ci getLayoutInfo(): GridLayoutInfo { 2336e80583aSopenharmony_ci this.updateMenuId(); 2346e80583aSopenharmony_ci return this.mPageDesktopLayoutConfig.getGridLayoutInfo(); 2356e80583aSopenharmony_ci } 2366e80583aSopenharmony_ci 2376e80583aSopenharmony_ci /** 2386e80583aSopenharmony_ci * Set layout information of grid view. 2396e80583aSopenharmony_ci */ 2406e80583aSopenharmony_ci setLayoutInfo(layoutInfo: GridLayoutInfo): void { 2416e80583aSopenharmony_ci this.mPageDesktopLayoutConfig.updateGridLayoutInfo(layoutInfo); 2426e80583aSopenharmony_ci } 2436e80583aSopenharmony_ci 2446e80583aSopenharmony_ci /** 2456e80583aSopenharmony_ci * Remove layout information of grid view. 2466e80583aSopenharmony_ci */ 2476e80583aSopenharmony_ci deleteLayoutInfo(): void { 2486e80583aSopenharmony_ci this.mPageDesktopLayoutConfig.deleteConfig(); 2496e80583aSopenharmony_ci } 2506e80583aSopenharmony_ci 2516e80583aSopenharmony_ci /** 2526e80583aSopenharmony_ci * Get recent missions max limit. 2536e80583aSopenharmony_ci * 2546e80583aSopenharmony_ci * @return {number} recent missions max limit. 2556e80583aSopenharmony_ci */ 2566e80583aSopenharmony_ci getRecentMissionsLimit(): any { 2576e80583aSopenharmony_ci return this.mRecentsModeConfig.getRecentMissionsLimit(); 2586e80583aSopenharmony_ci } 2596e80583aSopenharmony_ci 2606e80583aSopenharmony_ci /** 2616e80583aSopenharmony_ci * Set recent missions max limit. 2626e80583aSopenharmony_ci * 2636e80583aSopenharmony_ci * @param {number} num - Recent missions max limit. 2646e80583aSopenharmony_ci */ 2656e80583aSopenharmony_ci setRecentMissionsLimit(num): void { 2666e80583aSopenharmony_ci this.mRecentsModeConfig.updateRecentMissionsLimit(num); 2676e80583aSopenharmony_ci } 2686e80583aSopenharmony_ci 2696e80583aSopenharmony_ci /** 2706e80583aSopenharmony_ci * Update settingData by settingDataKey. 2716e80583aSopenharmony_ci */ 2726e80583aSopenharmony_ci setValue(value: string): void { 2736e80583aSopenharmony_ci settingsDataManager.setValue(this.helper, CommonConstants.NAVIGATION_BAR_STATUS_KEY, value); 2746e80583aSopenharmony_ci } 2756e80583aSopenharmony_ci 2766e80583aSopenharmony_ci /** 2776e80583aSopenharmony_ci * get settingDataValue by settingDataKey. 2786e80583aSopenharmony_ci * 2796e80583aSopenharmony_ci * @return settingsDataValue by settingDataKey. 2806e80583aSopenharmony_ci */ 2816e80583aSopenharmony_ci getValue() { 2826e80583aSopenharmony_ci return settingsDataManager.getValue(this.helper, CommonConstants.NAVIGATION_BAR_STATUS_KEY, SettingsModel.DEFAULT_VALUE); 2836e80583aSopenharmony_ci } 2846e80583aSopenharmony_ci 2856e80583aSopenharmony_ci /** 2866e80583aSopenharmony_ci * Monitor data changes. 2876e80583aSopenharmony_ci * @param callback 2886e80583aSopenharmony_ci */ 2896e80583aSopenharmony_ci registerListenForDataChanges(callback): void { 2906e80583aSopenharmony_ci this.helper = settingsDataManager.getHelper(globalThis.desktopContext, this.uri); 2916e80583aSopenharmony_ci if (this.helper !== null) { 2926e80583aSopenharmony_ci this.helper.on('dataChange', this.uri, callback); 2936e80583aSopenharmony_ci } 2946e80583aSopenharmony_ci } 2956e80583aSopenharmony_ci 2966e80583aSopenharmony_ci private updateMenuId(): void { 2976e80583aSopenharmony_ci let currentId: number = AppStorage.get('menuId') as number ?? 0; 2986e80583aSopenharmony_ci currentId++; 2996e80583aSopenharmony_ci AppStorage.setOrCreate('menuId', currentId % 100); 3006e80583aSopenharmony_ci } 3016e80583aSopenharmony_ci}