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 { Log } from '../utils/Log';
16import { SettingItemInfo } from '../bean/SettingItemInfo';
17import SettingItemOption from '../bean/SettingItemOption';
18import { SettingItemsConfig } from '../configs/SettingItemsConfig';
19import { SettingItemOptionsChecker } from './SettingItemOptionsChecker';
20
21const TAG = 'SettingItemsManager';
22
23/**
24 * Manager class for launcher settings item.
25 */
26export class SettingItemsManager {
27  private static readonly INVALID_INDEX: number = -1;
28
29  /**
30   * Mapping settings item to corresponding functions.
31   */
32  private settingName2CheckerFuncMap: Record<string, SettingItemOptionsChecker> = {};
33
34  /**
35   * constructor
36   */
37  constructor() {
38  }
39
40  /**
41   * Get corresponding functions for the given name.
42   */
43  withChecker(settingName: string, func: SettingItemOptionsChecker): SettingItemsManager {
44    Log.showInfo(TAG, `withChecker settingName: ${settingName}`);
45    this.settingName2CheckerFuncMap[settingName] = func;
46    return this;
47  }
48
49  /**
50   * Get all settings items for the given device type.
51   *
52   * @params deviceType device type constants like 'default', 'tablet'
53   * @params condition current settings.
54   * @return available settings items
55   */
56  get(deviceType: number, condition: number): SettingItemInfo[] {
57    const settingsList: SettingItemInfo[] = [];
58    for (const key in SettingItemsConfig.sSettingsMap) {
59      if (!SettingItemsConfig.sSettingsMap[key].isShowOptions) {
60        continue;
61      }
62      if (((SettingItemsConfig.sSettingsMap[key].deviceType & deviceType) != 0 &&
63      (SettingItemsConfig.sSettingsMap[key].condition & condition) != 0)) {
64        const settingItem = new SettingItemInfo();
65        settingItem.ida = SettingItemsConfig.sSettingsMap[key].index;
66        settingItem.settingName = SettingItemsConfig.sSettingsMap[key].description;
67        settingItem.settingType = SettingItemsConfig.sSettingsMap[key].settingType;
68        const checker = this.settingName2CheckerFuncMap[key];
69        const selectedOptionName = checker != undefined ? checker() : '';
70        const optionList = SettingItemsConfig.sSettingsMap[key].optionList;
71        if (optionList == undefined) {
72          settingsList.push(settingItem);
73          continue;
74        }
75        for (let i = 0; i < optionList.length; i++) {
76          const option = new SettingItemOption();
77          option.name = optionList[i].name;
78          option.value = optionList[i].name;
79
80          if (optionList[i].name == selectedOptionName) {
81            settingItem.settingValue = optionList[i].name;
82            option.checked = true;
83          } else {
84            option.checked = false;
85          }
86          settingItem.valueList.push(option);
87        }
88        settingsList.push(settingItem);
89      }
90    }
91
92    return settingsList;
93  }
94
95  private getGridLayoutIdx(gridLayout: string, optionList: any): number {
96    for (let i = 0; i < optionList.length; i++) {
97      if (gridLayout == optionList[i].name) {
98        return i;
99      }
100    }
101
102    return SettingItemsManager.INVALID_INDEX;
103  }
104
105  /**
106   * Convert layout constants to settings index.
107   *
108   * @params gridLayout layout constants like '4x4'
109   * @return settings index of the given layout
110   */
111  gridLayoutValue2Idx(gridLayout: string): number {
112    const phoneOptionList = SettingItemsConfig.sSettingsMap[SettingItemsConfig.SETTING_ITEM_PHONE_GRID_LAYOUT_OPTIONS].optionList;
113    const padOptionList = SettingItemsConfig.sSettingsMap[SettingItemsConfig.SETTING_ITEM_PAD_GRID_LAYOUT_OPTIONS].optionList;
114
115    const idxPhone = this.getGridLayoutIdx(gridLayout, phoneOptionList);
116    if (idxPhone != SettingItemsManager.INVALID_INDEX) {
117      return idxPhone;
118    }
119
120    const idxPad = this.getGridLayoutIdx(gridLayout, padOptionList);
121    if (idxPad != SettingItemsManager.INVALID_INDEX) {
122      return idxPad;
123    }
124
125    return 0;
126  }
127}