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
16
17import {
18    CommonConstants,
19    AppItemInfo,
20    layoutConfigManager,
21    LayoutViewModel,
22    Log,
23    CheckEmptyUtils,
24    BaseCloseAppHandler,
25    CloseAppManager
26} from '@ohos/common';
27import SmartDockConstants from '../common/constants/SmartDockConstants';
28import { SmartDockStyleConfig } from '../config/SmartDockStyleConfig';
29
30const TAG = 'SmartDockCloseAppHandler';
31
32/**
33 * smartDock close app processing class
34 */
35export default class SmartDockCloseAppHandler extends BaseCloseAppHandler {
36    private mSmartDockStyleConfig;
37    private mAppItemBundleName: string;
38
39    constructor() {
40        super();
41        this.mSmartDockStyleConfig = layoutConfigManager.getStyleConfig(SmartDockStyleConfig.APP_LIST_STYLE_CONFIG,
42            SmartDockConstants.FEATURE_NAME);
43    }
44
45    static getInstance(): SmartDockCloseAppHandler {
46        Log.showInfo(TAG, `SmartDockCloseAppHandler getInstance called!`)
47        if (globalThis.SmartDockCloseAppHandler == null) {
48            globalThis.SmartDockCloseAppHandler = new SmartDockCloseAppHandler();
49            Log.showInfo(TAG, `SmartDockCloseAppHandler getInstance constructor`);
50        }
51        return globalThis.SmartDockCloseAppHandler;
52    }
53
54    /**
55     * get app icon info
56     *
57     * @param windowTarget close window target
58     */
59    public getAppIconInfo(windowTarget): void {
60        Log.showInfo(TAG, `getAppIconInfo called and windowTarget is ${JSON.stringify(windowTarget)}`);
61        this.mAppItemBundleName = windowTarget.bundleName;
62        this.mAppIconSize = this.mSmartDockStyleConfig.mListItemWidth;
63        //    this.setAppIconInfo();
64        this.calculateAppIconPosition();
65        let appCloseIconInfo = {
66            appIconSize: this.mAppIconSize,
67            appIconHeight: this.mAppIconSize,
68            appIconPositionX: this.mAppIconPositionX,
69            appIconPositionY: this.mAppIconPositionY
70        };
71        let recentList: AppItemInfo[] = AppStorage.get('recentList');
72        CloseAppManager.getInstance().addSmartDockClosePosition(appCloseIconInfo, recentList[0]);
73        Log.showInfo(TAG, `getAppIconInfo addSmartDockClosePosition ${JSON.stringify(appCloseIconInfo)}`);
74
75    }
76
77    protected calculateAppIconPosition(): void {
78        if (CheckEmptyUtils.isEmpty(this.mSmartDockStyleConfig)) {
79            Log.showError(TAG, `calculateAppIconPosition with invalid config`)
80            return;
81        }
82
83        this.mAppIconPositionX = 0;
84        this.mAppIconPositionY = 0;
85        const residentList: AppItemInfo[] = AppStorage.get('residentList');
86        const recentList: AppItemInfo[] = AppStorage.get('recentList');
87        const screenWidth: number = AppStorage.get('screenWidth');
88        const workSpaceHeight: number = LayoutViewModel.getInstance().getWorkSpaceHeight();
89        this.mAppIconPositionY = workSpaceHeight + (this.mSmartDockStyleConfig.mDockHeight - this.mSmartDockStyleConfig.mIconSize) / 2;
90
91        const smartDockWidth: number = this.getListWidth(residentList) +
92        (recentList.length > 0 ? this.mSmartDockStyleConfig.mDockGap + this.getListWidth(recentList) : 0);
93        const smartDockStartPositionX: number = (screenWidth - smartDockWidth) / 2;
94        const startAppTypeFromPageDesktop: number = AppStorage.get('startAppTypeFromPageDesktop');
95        this.mAppIconPositionX = smartDockStartPositionX + this.getListWidth(residentList) + this.mSmartDockStyleConfig.mDockGap
96        + this.mSmartDockStyleConfig.mDockPadding;
97        //    if (startAppTypeFromPageDesktop === CommonConstants.OVERLAY_TYPE_APP_RECENT) {
98        //      const indexInRecentList: number = this.getIndexInList(recentList);
99        //      this.mAppIconPositionX = smartDockStartPositionX + this.getListWidth(residentList) + this.mSmartDockStyleConfig.mDockGap
100        //      + this.mSmartDockStyleConfig.mDockPadding + indexInRecentList
101        //      * (this.mSmartDockStyleConfig.mListItemWidth + this.mSmartDockStyleConfig.mListItemGap);
102        //      return;
103        //    }else{
104        //      const indexInResidentList: number = this.getIndexInList(residentList);
105        //      this.mAppIconPositionX = smartDockStartPositionX + this.mSmartDockStyleConfig.mDockPadding + indexInResidentList
106        //      * (this.mSmartDockStyleConfig.mListItemWidth + this.mSmartDockStyleConfig.mListItemGap)
107        //      - (recentList.length > 0 ? 0 : this.mSmartDockStyleConfig.mListItemGap / 2);
108        //      return;
109        //    }
110    }
111
112    private getIndexInList(list): number {
113        let index: number = CommonConstants.INVALID_VALUE;
114        if (CheckEmptyUtils.isEmptyArr(list)) {
115            Log.showError(TAG, `getIndexInRecentList with invalid list`)
116            return index;
117        }
118
119        for (var i = 0; i < list.length; i++) {
120            if (this.mAppItemBundleName === list[i].bundleName) {
121                AppStorage.setOrCreate('closeAppItemInfo', list[i]);
122                Log.showInfo(TAG, `getIndexInList  closeAppItemInfo ${JSON.stringify(list[i])} index ${index}`)
123                index = i;
124                break;
125            }
126        }
127
128        return index;
129    }
130
131    private getListWidth(itemList): number {
132        let width = 0;
133        if (CheckEmptyUtils.isEmptyArr(itemList)) {
134            return width;
135        } else {
136            let num = itemList.length;
137            if (num > this.mSmartDockStyleConfig.mMaxDockNum) {
138                num = this.mSmartDockStyleConfig.mMaxDockNum
139            }
140            width = this.mSmartDockStyleConfig.mDockPadding * 2 + num * (this.mSmartDockStyleConfig.mListItemWidth) + (num - 1)
141            * (this.mSmartDockStyleConfig.mListItemGap);
142        }
143        return width;
144    }
145}
146