1/**
2 * Copyright (c) 2022-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 { BaseStartAppHandler } from '@ohos/common';
17import { CommonConstants } from '@ohos/common';
18import { AppItemInfo } from '@ohos/common';
19import { layoutConfigManager } from '@ohos/common';
20import { LayoutViewModel } from '@ohos/common';
21import { Log } from '@ohos/common';
22import { CheckEmptyUtils } from '@ohos/common';
23import SmartDockConstants from '../common/constants/SmartDockConstants';
24import { SmartDockStyleConfig } from '../config/SmartDockStyleConfig';
25
26const TAG = 'SmartDockStartAppHandler';
27
28/**
29 * smartDock start app processing class
30 */
31export default class SmartDockStartAppHandler extends BaseStartAppHandler {
32  private mSmartDockStyleConfig;
33
34  private constructor() {
35    super();
36    this.mSmartDockStyleConfig = layoutConfigManager.getStyleConfig(SmartDockStyleConfig.APP_LIST_STYLE_CONFIG,
37      SmartDockConstants.FEATURE_NAME);
38  }
39
40  static getInstance(): SmartDockStartAppHandler {
41    if (globalThis.SmartDockStartAppHandler == null) {
42      globalThis.SmartDockStartAppHandler = new SmartDockStartAppHandler();
43    }
44    return globalThis.SmartDockStartAppHandler;
45  }
46
47  protected calculateAppIconPosition(): void {
48    if (CheckEmptyUtils.isEmpty(this.mSmartDockStyleConfig)) {
49      Log.showError(TAG, 'calculateAppIconPosition with invalid config')
50      return;
51    }
52    const appItemInfo = AppStorage.get('startAppItemInfo');
53    const residentList: AppItemInfo[] = AppStorage.get('residentList');
54    const recentList: AppItemInfo[] = AppStorage.get('recentList');
55    const screenWidth: number = AppStorage.get('screenWidth');
56    const workSpaceHeight: number = LayoutViewModel.getInstance().getWorkSpaceHeight();
57    this.mAppIconPositionY = workSpaceHeight + this.mSmartDockStyleConfig.mListItemGap;
58    const smartDockWidth: number = this.getListWidth(residentList) +
59    (recentList.length > 0 ? this.mSmartDockStyleConfig.mDockGap + this.getListWidth(recentList) : 0);
60    const smartDockStartPositionX: number = (screenWidth - smartDockWidth) / 2;
61    const startAppTypeFromPageDesktop: number = AppStorage.get('startAppTypeFromPageDesktop');
62    if (startAppTypeFromPageDesktop === CommonConstants.OVERLAY_TYPE_APP_RECENT) {
63      const indexInRecentList: number = this.getIndexInList(appItemInfo, recentList);
64      this.mAppIconPositionX = smartDockStartPositionX + this.getListWidth(residentList) + this.mSmartDockStyleConfig.mDockGap
65      + this.mSmartDockStyleConfig.mDockPadding + indexInRecentList
66      * (this.mSmartDockStyleConfig.mListItemWidth + this.mSmartDockStyleConfig.mListItemGap);
67      return;
68    }else {
69      if (CheckEmptyUtils.isEmptyArr(residentList)) {
70        Log.showError(TAG, 'residentList is empty');
71        return;
72      }
73      const indexInResidentList: number = this.getIndexInList(appItemInfo, residentList);
74      this.mAppIconPositionX = smartDockStartPositionX + this.mSmartDockStyleConfig.mDockPadding + indexInResidentList
75      * (this.mSmartDockStyleConfig.mListItemWidth + this.mSmartDockStyleConfig.mListItemGap)
76      - (recentList.length > 0 ? 0 : this.mSmartDockStyleConfig.mListItemGap / 2);
77      return;
78    }
79  }
80
81  private getIndexInList(appItemInfo, list) : number {
82    let index: number = CommonConstants.INVALID_VALUE;
83    if (CheckEmptyUtils.isEmptyArr(list)) {
84      Log.showError(TAG, 'getIndexInRecentList with invalid list')
85      return index;
86    }
87
88    for (var i = 0; i < list.length; i++) {
89      if (appItemInfo?.bundleName === list[i]?.bundleName) {
90        index = i;
91        break;
92      }
93    }
94
95    return index;
96  }
97
98  private getListWidth(itemList): number {
99    let width = 0;
100    if (CheckEmptyUtils.isEmptyArr(itemList)) {
101      return width;
102    } else {
103      let num = itemList.length;
104      if (num > this.mSmartDockStyleConfig.mMaxDockNum) {
105        num = this.mSmartDockStyleConfig.mMaxDockNum
106      }
107      width = this.mSmartDockStyleConfig.mDockPadding * 2 + num * (this.mSmartDockStyleConfig.mListItemWidth) + (num - 1)
108      * (this.mSmartDockStyleConfig.mListItemGap);
109    }
110    return width;
111  }
112}
113