1/** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { Log } from '../utils/Log'; 17import FileUtils from '../utils/FileUtils'; 18import GridLayoutUtil from '../utils/GridLayoutUtil'; 19import { CommonConstants } from '../constants/CommonConstants'; 20import { RecentsModeConfig } from '../layoutconfig/RecentsModeConfig'; 21import { layoutConfigManager } from '../layoutconfig/LayoutConfigManager'; 22import { settingsDataManager } from '../manager/SettingsDataManager'; 23import { PageDesktopModeConfig } from '../layoutconfig/PageDesktopModeConfig'; 24import { PageDesktopLayoutConfig } from '../layoutconfig/PageDesktopLayoutConfig'; 25import { PageDesktopAppModeConfig } from '../layoutconfig/PageDesktopAppModeConfig'; 26import { SettingsModelObserver } from './SettingsModelObserver'; 27import GridLayoutConfigs from '../configs/GridLayoutConfigs'; 28import dataShare from '@ohos.data.dataShare'; 29import { GridLayoutInfo } from '../interface'; 30 31const TAG = 'SettingsModel'; 32 33/** 34 * Data model for launcher settings ability. 35 */ 36export class SettingsModel { 37 static readonly EVENT_FORCE_RELOAD: number = 1; 38 private static readonly DEFAULT_VALUE: string = '1'; 39 private readonly mPageDesktopModeConfig: PageDesktopModeConfig; 40 private readonly mPageDesktopLayoutConfig: PageDesktopLayoutConfig; 41 private readonly mRecentsModeConfig: RecentsModeConfig; 42 private readonly mPageDesktopAppModeConfig: PageDesktopAppModeConfig; 43 private mGridConfig = 1; 44 private mGridLayoutTable = GridLayoutConfigs.GridLayoutTable; 45 private readonly uri: string = ''; 46 private helper: dataShare.DataShareHelper | null = null; 47 private readonly mObserverList: SettingsModelObserver[] = []; 48 49 private constructor() { 50 this.mPageDesktopModeConfig = PageDesktopModeConfig.getInstance(); 51 const deviceType = this.mPageDesktopModeConfig.getDeviceType(); 52 if (deviceType == CommonConstants.DEFAULT_DEVICE_TYPE) { 53 this.mGridLayoutTable = GridLayoutConfigs.GridLayoutTable; 54 } else if (deviceType == CommonConstants.PAD_DEVICE_TYPE) { 55 this.mGridLayoutTable = GridLayoutConfigs.PadGridLayoutTableHorizontal; 56 } else { 57 this.mGridLayoutTable = GridLayoutConfigs.GridLayoutTableHorizontal; 58 } 59 this.mPageDesktopLayoutConfig = layoutConfigManager.getFunctionConfig<PageDesktopLayoutConfig>(PageDesktopLayoutConfig.GRID_LAYOUT_INFO); 60 this.mRecentsModeConfig = layoutConfigManager.getModeConfig(RecentsModeConfig.RECENT_MISSIONS_MODE_CONFIG); 61 this.mPageDesktopAppModeConfig = layoutConfigManager.getModeConfig(PageDesktopAppModeConfig.DESKTOP_APPLICATION_INFO); 62 this.uri = settingsDataManager.getUri(CommonConstants.NAVIGATION_BAR_STATUS_KEY); 63 this.helper = settingsDataManager.getHelper(globalThis.desktopContext, this.uri); 64 } 65 66 static getInstance(): SettingsModel { 67 if (globalThis.SettingsModelInstance == null) { 68 globalThis.SettingsModelInstance = new SettingsModel(); 69 } 70 return globalThis.SettingsModelInstance; 71 } 72 73 addObserver(observer: SettingsModelObserver): void { 74 Log.showDebug(TAG, 'addObserver'); 75 this.mObserverList.push(observer); 76 } 77 78 private notifyObservers(event: number): void { 79 Log.showDebug(TAG, 'notifyObservers'); 80 for (let i = 0; i < this.mObserverList.length; i++) { 81 this.mObserverList[i](event); 82 } 83 } 84 85 /** 86 * force reload all config from disk. 87 */ 88 forceReloadConfig(): void { 89 if (this.mPageDesktopModeConfig) { 90 this.mPageDesktopModeConfig.forceReloadConfig(); 91 } 92 if (this.mPageDesktopLayoutConfig) { 93 this.mPageDesktopLayoutConfig.forceReloadConfig(); 94 } 95 if (this.mPageDesktopAppModeConfig) { 96 this.mPageDesktopAppModeConfig.forceReloadConfig(); 97 } 98 if (this.mRecentsModeConfig) { 99 this.mRecentsModeConfig.forceReloadConfig(); 100 } 101 this.notifyObservers(1); 102 } 103 104 /** 105 * Get the grid view presetting collection of layout config information table. 106 * 107 * @return {object} Grid view presetting collection object. 108 */ 109 getGridLayoutTable(): any { 110 return this.mGridLayoutTable; 111 } 112 113 /** 114 * Get default layout information of grid view. 115 * 116 * @return {object} Default layout information of grid view. 117 */ 118 getDefaultLayoutInfo(): any { 119 let defaultLayoutInfoFilePath = globalThis.desktopContext.filesDir + '/layoutInfo.json'; 120 return FileUtils.readJsonFile(defaultLayoutInfoFilePath); 121 } 122 123 /** 124 * Get layout config of grid view. 125 * 126 * @return {object} Layout config of grid view. 127 */ 128 getGridConfig(): any { 129 this.mGridConfig = this.mPageDesktopModeConfig.getGridConfig(); 130 let gridLayout = this.mGridLayoutTable[0]; 131 for (let i = 0; i < this.mGridLayoutTable.length; i++) { 132 if (this.mGridLayoutTable[i].id == this.mGridConfig) { 133 gridLayout = this.mGridLayoutTable[i]; 134 break; 135 } 136 } 137 return gridLayout; 138 } 139 140 /** 141 * Set layout config id of grid view. 142 * 143 * @param gridConfig - Layout config id of grid view. 144 */ 145 setGridConfig(gridConfig) { 146 this.mPageDesktopModeConfig.updateGridConfig(gridConfig); 147 148 const config = this.getGridConfig(); 149 const gridLayoutInfo = this.mPageDesktopLayoutConfig.getGridLayoutInfo(); 150 this.mPageDesktopLayoutConfig.updateGridLayoutInfo(GridLayoutUtil.updateGridLayoutInfo( 151 gridLayoutInfo, config.row, config.column 152 )); 153 this.forceReloadConfig(); 154 } 155 156 /** 157 * Get appList config of workspace view. 158 * 159 * @return {object} appList config of workspace view. 160 */ 161 getAppListInfo(): any { 162 return this.mPageDesktopAppModeConfig.getAppListInfo(); 163 } 164 165 /** 166 * Determine if there is an application in the workspace. 167 * 168 * @return {boolean} true(exist). 169 */ 170 isAppListInfoExist(): boolean { 171 return this.mPageDesktopAppModeConfig.isConfigExist(); 172 } 173 174 /** 175 * Set layout config id of grid view. 176 * 177 * @param gridConfig - Layout config id of grid view. 178 */ 179 setAppListInfo(appList): void { 180 this.mPageDesktopAppModeConfig.updateAppListInfo(appList); 181 } 182 183 /** 184 * Get the layout view type. 185 * 186 * @return {string} Layout view type, should one of 'Grid' or 'List' which is stored in LayoutConstants class. 187 */ 188 getAppPageStartConfig(): any { 189 return this.mPageDesktopModeConfig.getAppStartPageType(); 190 } 191 192 /** 193 * Set the layout view type. 194 * 195 * @param {string} type - Layout view type, should one of 'Grid' or 'List' which is stored in LayoutConstants class. 196 */ 197 setAppPageStartConfig(type): void { 198 this.mPageDesktopModeConfig.updateAppStartPageType(type); 199 } 200 201 /** 202 * Set the device type. 203 * 204 * @param {string} deviceType - device type. 205 */ 206 setDevice(deviceType): void { 207 Log.showDebug(TAG, `setDevice ${deviceType}`); 208 if (deviceType == CommonConstants.DEFAULT_DEVICE_TYPE) { 209 this.mGridLayoutTable = GridLayoutConfigs.GridLayoutTable; 210 } else if (deviceType == CommonConstants.PAD_DEVICE_TYPE) { 211 this.mGridLayoutTable = GridLayoutConfigs.PadGridLayoutTableHorizontal; 212 } else { 213 this.mGridLayoutTable = GridLayoutConfigs.GridLayoutTableHorizontal; 214 } 215 this.mPageDesktopModeConfig.updateDeviceType(deviceType); 216 } 217 218 /** 219 * get the device type. 220 * 221 * @return {string} device type 222 */ 223 getDevice(): string { 224 return this.mPageDesktopModeConfig.getDeviceType(); 225 } 226 227 /** 228 * Get layout information of grid view. 229 * 230 * @return {object} layout information. 231 */ 232 getLayoutInfo(): GridLayoutInfo { 233 this.updateMenuId(); 234 return this.mPageDesktopLayoutConfig.getGridLayoutInfo(); 235 } 236 237 /** 238 * Set layout information of grid view. 239 */ 240 setLayoutInfo(layoutInfo: GridLayoutInfo): void { 241 this.mPageDesktopLayoutConfig.updateGridLayoutInfo(layoutInfo); 242 } 243 244 /** 245 * Remove layout information of grid view. 246 */ 247 deleteLayoutInfo(): void { 248 this.mPageDesktopLayoutConfig.deleteConfig(); 249 } 250 251 /** 252 * Get recent missions max limit. 253 * 254 * @return {number} recent missions max limit. 255 */ 256 getRecentMissionsLimit(): any { 257 return this.mRecentsModeConfig.getRecentMissionsLimit(); 258 } 259 260 /** 261 * Set recent missions max limit. 262 * 263 * @param {number} num - Recent missions max limit. 264 */ 265 setRecentMissionsLimit(num): void { 266 this.mRecentsModeConfig.updateRecentMissionsLimit(num); 267 } 268 269 /** 270 * Update settingData by settingDataKey. 271 */ 272 setValue(value: string): void { 273 settingsDataManager.setValue(this.helper, CommonConstants.NAVIGATION_BAR_STATUS_KEY, value); 274 } 275 276 /** 277 * get settingDataValue by settingDataKey. 278 * 279 * @return settingsDataValue by settingDataKey. 280 */ 281 getValue() { 282 return settingsDataManager.getValue(this.helper, CommonConstants.NAVIGATION_BAR_STATUS_KEY, SettingsModel.DEFAULT_VALUE); 283 } 284 285 /** 286 * Monitor data changes. 287 * @param callback 288 */ 289 registerListenForDataChanges(callback): void { 290 this.helper = settingsDataManager.getHelper(globalThis.desktopContext, this.uri); 291 if (this.helper !== null) { 292 this.helper.on('dataChange', this.uri, callback); 293 } 294 } 295 296 private updateMenuId(): void { 297 let currentId: number = AppStorage.get('menuId') as number ?? 0; 298 currentId++; 299 AppStorage.setOrCreate('menuId', currentId % 100); 300 } 301}