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