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_ciimport { Log } from '../utils/Log';
166e80583aSopenharmony_ciimport { SettingItemInfo } from '../bean/SettingItemInfo';
176e80583aSopenharmony_ciimport SettingItemOption from '../bean/SettingItemOption';
186e80583aSopenharmony_ciimport { SettingItemsConfig } from '../configs/SettingItemsConfig';
196e80583aSopenharmony_ciimport { SettingItemOptionsChecker } from './SettingItemOptionsChecker';
206e80583aSopenharmony_ci
216e80583aSopenharmony_ciconst TAG = 'SettingItemsManager';
226e80583aSopenharmony_ci
236e80583aSopenharmony_ci/**
246e80583aSopenharmony_ci * Manager class for launcher settings item.
256e80583aSopenharmony_ci */
266e80583aSopenharmony_ciexport class SettingItemsManager {
276e80583aSopenharmony_ci  private static readonly INVALID_INDEX: number = -1;
286e80583aSopenharmony_ci
296e80583aSopenharmony_ci  /**
306e80583aSopenharmony_ci   * Mapping settings item to corresponding functions.
316e80583aSopenharmony_ci   */
326e80583aSopenharmony_ci  private settingName2CheckerFuncMap: Record<string, SettingItemOptionsChecker> = {};
336e80583aSopenharmony_ci
346e80583aSopenharmony_ci  /**
356e80583aSopenharmony_ci   * constructor
366e80583aSopenharmony_ci   */
376e80583aSopenharmony_ci  constructor() {
386e80583aSopenharmony_ci  }
396e80583aSopenharmony_ci
406e80583aSopenharmony_ci  /**
416e80583aSopenharmony_ci   * Get corresponding functions for the given name.
426e80583aSopenharmony_ci   */
436e80583aSopenharmony_ci  withChecker(settingName: string, func: SettingItemOptionsChecker): SettingItemsManager {
446e80583aSopenharmony_ci    Log.showInfo(TAG, `withChecker settingName: ${settingName}`);
456e80583aSopenharmony_ci    this.settingName2CheckerFuncMap[settingName] = func;
466e80583aSopenharmony_ci    return this;
476e80583aSopenharmony_ci  }
486e80583aSopenharmony_ci
496e80583aSopenharmony_ci  /**
506e80583aSopenharmony_ci   * Get all settings items for the given device type.
516e80583aSopenharmony_ci   *
526e80583aSopenharmony_ci   * @params deviceType device type constants like 'default', 'tablet'
536e80583aSopenharmony_ci   * @params condition current settings.
546e80583aSopenharmony_ci   * @return available settings items
556e80583aSopenharmony_ci   */
566e80583aSopenharmony_ci  get(deviceType: number, condition: number): SettingItemInfo[] {
576e80583aSopenharmony_ci    const settingsList: SettingItemInfo[] = [];
586e80583aSopenharmony_ci    for (const key in SettingItemsConfig.sSettingsMap) {
596e80583aSopenharmony_ci      if (!SettingItemsConfig.sSettingsMap[key].isShowOptions) {
606e80583aSopenharmony_ci        continue;
616e80583aSopenharmony_ci      }
626e80583aSopenharmony_ci      if (((SettingItemsConfig.sSettingsMap[key].deviceType & deviceType) != 0 &&
636e80583aSopenharmony_ci      (SettingItemsConfig.sSettingsMap[key].condition & condition) != 0)) {
646e80583aSopenharmony_ci        const settingItem = new SettingItemInfo();
656e80583aSopenharmony_ci        settingItem.ida = SettingItemsConfig.sSettingsMap[key].index;
666e80583aSopenharmony_ci        settingItem.settingName = SettingItemsConfig.sSettingsMap[key].description;
676e80583aSopenharmony_ci        settingItem.settingType = SettingItemsConfig.sSettingsMap[key].settingType;
686e80583aSopenharmony_ci        const checker = this.settingName2CheckerFuncMap[key];
696e80583aSopenharmony_ci        const selectedOptionName = checker != undefined ? checker() : '';
706e80583aSopenharmony_ci        const optionList = SettingItemsConfig.sSettingsMap[key].optionList;
716e80583aSopenharmony_ci        if (optionList == undefined) {
726e80583aSopenharmony_ci          settingsList.push(settingItem);
736e80583aSopenharmony_ci          continue;
746e80583aSopenharmony_ci        }
756e80583aSopenharmony_ci        for (let i = 0; i < optionList.length; i++) {
766e80583aSopenharmony_ci          const option = new SettingItemOption();
776e80583aSopenharmony_ci          option.name = optionList[i].name;
786e80583aSopenharmony_ci          option.value = optionList[i].name;
796e80583aSopenharmony_ci
806e80583aSopenharmony_ci          if (optionList[i].name == selectedOptionName) {
816e80583aSopenharmony_ci            settingItem.settingValue = optionList[i].name;
826e80583aSopenharmony_ci            option.checked = true;
836e80583aSopenharmony_ci          } else {
846e80583aSopenharmony_ci            option.checked = false;
856e80583aSopenharmony_ci          }
866e80583aSopenharmony_ci          settingItem.valueList.push(option);
876e80583aSopenharmony_ci        }
886e80583aSopenharmony_ci        settingsList.push(settingItem);
896e80583aSopenharmony_ci      }
906e80583aSopenharmony_ci    }
916e80583aSopenharmony_ci
926e80583aSopenharmony_ci    return settingsList;
936e80583aSopenharmony_ci  }
946e80583aSopenharmony_ci
956e80583aSopenharmony_ci  private getGridLayoutIdx(gridLayout: string, optionList: any): number {
966e80583aSopenharmony_ci    for (let i = 0; i < optionList.length; i++) {
976e80583aSopenharmony_ci      if (gridLayout == optionList[i].name) {
986e80583aSopenharmony_ci        return i;
996e80583aSopenharmony_ci      }
1006e80583aSopenharmony_ci    }
1016e80583aSopenharmony_ci
1026e80583aSopenharmony_ci    return SettingItemsManager.INVALID_INDEX;
1036e80583aSopenharmony_ci  }
1046e80583aSopenharmony_ci
1056e80583aSopenharmony_ci  /**
1066e80583aSopenharmony_ci   * Convert layout constants to settings index.
1076e80583aSopenharmony_ci   *
1086e80583aSopenharmony_ci   * @params gridLayout layout constants like '4x4'
1096e80583aSopenharmony_ci   * @return settings index of the given layout
1106e80583aSopenharmony_ci   */
1116e80583aSopenharmony_ci  gridLayoutValue2Idx(gridLayout: string): number {
1126e80583aSopenharmony_ci    const phoneOptionList = SettingItemsConfig.sSettingsMap[SettingItemsConfig.SETTING_ITEM_PHONE_GRID_LAYOUT_OPTIONS].optionList;
1136e80583aSopenharmony_ci    const padOptionList = SettingItemsConfig.sSettingsMap[SettingItemsConfig.SETTING_ITEM_PAD_GRID_LAYOUT_OPTIONS].optionList;
1146e80583aSopenharmony_ci
1156e80583aSopenharmony_ci    const idxPhone = this.getGridLayoutIdx(gridLayout, phoneOptionList);
1166e80583aSopenharmony_ci    if (idxPhone != SettingItemsManager.INVALID_INDEX) {
1176e80583aSopenharmony_ci      return idxPhone;
1186e80583aSopenharmony_ci    }
1196e80583aSopenharmony_ci
1206e80583aSopenharmony_ci    const idxPad = this.getGridLayoutIdx(gridLayout, padOptionList);
1216e80583aSopenharmony_ci    if (idxPad != SettingItemsManager.INVALID_INDEX) {
1226e80583aSopenharmony_ci      return idxPad;
1236e80583aSopenharmony_ci    }
1246e80583aSopenharmony_ci
1256e80583aSopenharmony_ci    return 0;
1266e80583aSopenharmony_ci  }
1276e80583aSopenharmony_ci}