• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
17  BaseCloseAppHandler,
18  CloseAppManager,
19  CommonConstants,
20  Log,
21  CheckEmptyUtils,
22  StyleConstants
23} from '@ohos/common';
24import { BigFolderViewModel } from '@ohos/bigfolder';
25import PageDesktopViewModel from '../viewmodel/PageDesktopViewModel';
26
27const TAG = 'PageDesktopCloseAppHandler';
28
29/**
30 * Desktop workspace start app processing class
31 */
32export class PageDesktopCloseAppHandler extends BaseCloseAppHandler {
33  private mGridConfig;
34  private mPageDesktopStyleConfig;
35  private mFolderStyleConfig;
36  private mAppItemBundleName: string;
37  private mCloseAppType: boolean = true;
38  private mFindPagedesktopPosition: boolean = true;
39  private mFolderItem: any;
40
41  constructor() {
42    super();
43    this.mGridConfig = PageDesktopViewModel.getInstance().getGridConfig();
44    this.mPageDesktopStyleConfig = PageDesktopViewModel.getInstance().getPageDesktopStyleConfig();
45    this.mFolderStyleConfig = BigFolderViewModel.getInstance().getFolderStyleConfig();
46  }
47
48  static getInstance(): PageDesktopCloseAppHandler {
49    if (globalThis.PageDesktopCloseAppHandler == null) {
50      globalThis.PageDesktopCloseAppHandler = new PageDesktopCloseAppHandler();
51    }
52    return globalThis.PageDesktopCloseAppHandler;
53  }
54
55  /**
56   * get app icon info
57   *
58   * @param windowTarget close window target
59   */
60  public getAppIconInfo(windowTarget): void {
61    this.mAppItemBundleName = windowTarget.bundleName;
62    //        this.setAppIconInfo();
63    this.calculateAppIconPosition();
64    if (this.mFindPagedesktopPosition) {
65      let appCloseIconInfo = {
66        appIconSize: this.mCloseAppType ? StyleConstants.DEFAULT_ADD_APP_SIZE : StyleConstants.DEFAULT_FOLDER_APP_SIZE,
67        appIconHeight: this.mCloseAppType ? StyleConstants.DEFAULT_ADD_APP_SIZE : StyleConstants.DEFAULT_FOLDER_APP_SIZE,
68        appIconPositionX: this.mAppIconPositionX,
69        appIconPositionY: this.mAppIconPositionY
70      };
71      let appItem: any = this.getCloseAppItemInfo();
72      CloseAppManager.getInstance().addPagedesktopClosePosition(appCloseIconInfo, appItem);
73    } else {
74      CloseAppManager.getInstance().addPagedesktopClosePosition(null);
75    }
76  }
77
78  protected calculateAppIconPosition(): void {
79    if (CheckEmptyUtils.isEmpty(this.mGridConfig) || CheckEmptyUtils.isEmpty(this.mPageDesktopStyleConfig)) {
80      Log.showError(TAG, `calculateAppIconPosition with invalid config`);
81      return;
82    }
83
84    const gridWidth: number = this.mPageDesktopStyleConfig.mGridWidth;
85    const gridHeight: number = this.mPageDesktopStyleConfig.mGridHeight;
86    const column: number = this.mGridConfig.column;
87    const row: number = this.mGridConfig.row;
88    const columnsGap: number = this.mPageDesktopStyleConfig.mColumnsGap;
89    const rowGap: number = this.mPageDesktopStyleConfig.mRowsGap;
90    const gridItemHeight: number = row > 0 ? (gridHeight + rowGap) / row : 0;
91    const gridItemWidth: number = column > 0 ? (gridWidth + columnsGap) / column : 0;
92    const paddingTop: number = this.mPageDesktopStyleConfig.mPaddingTop;
93    let appItem: any = this.getCloseAppItemInfo();
94    if (CheckEmptyUtils.isEmpty(appItem)) {
95      Log.showError(TAG, `calculateAppIconPosition pagedesktop not has close app`);
96      this.mFindPagedesktopPosition = false;
97      return;
98    }
99    if (this.mCloseAppType) {
100      this.mAppIconPositionY = this.mPageDesktopStyleConfig.mDesktopMarginTop + paddingTop + appItem.row * gridItemHeight + 0.96;
101      let columnSize: number = (this.mPageDesktopStyleConfig.mGridWidth - (column - 1) * columnsGap) / column;
102      let iconLeftMargin: number = (columnSize - this.mPageDesktopStyleConfig.mIconSize) / 2;
103      this.mAppIconPositionX = this.mPageDesktopStyleConfig.mMargin + iconLeftMargin + appItem.column * (gridItemWidth);
104
105      if (CommonConstants.OVERLAY_TYPE_CARD !== AppStorage.Get('startAppTypeFromPageDesktop') && appItem.page != AppStorage.Get('pageIndex')) {
106        AppStorage.SetOrCreate('pageIndex', appItem.page);
107      }
108    } else {
109      let folderItem: any = this.mFolderItem;
110      const folderGridSize: number = this.mFolderStyleConfig.mGridSize;
111      const folderGridGap: number = this.mFolderStyleConfig.mFolderGridGap;
112      const folderAppSize: number = this.mFolderStyleConfig.mFolderAppSize;
113      let folderLeftMargin: number = (gridItemWidth * folderItem.area[0] - folderGridSize - columnsGap) / 2;
114      let folderRow: number = Math.floor((folderGridSize) / (folderGridGap + folderAppSize));
115      let folderColumn: number = folderRow;
116      let folderLeftPadding: number = (folderGridSize - folderColumn * (folderGridGap + folderAppSize) + folderGridGap) / 2;
117      let folderTopPadding: number = (folderGridSize - folderRow * (folderGridGap + folderAppSize) + folderGridGap) / 2;
118
119      let index: number = this.getIndexInFolderAppList(appItem, folderItem);
120      if (index >= folderRow * folderColumn) {
121        index = 9;
122      }
123      let row: number = Math.floor(index / folderColumn);
124      let column: number = index % folderColumn;
125      if (column != 0) {
126        row += 1;
127      } else {
128        column = folderColumn;
129      }
130      Log.showDebug(TAG, `calculateAppIconPosition index ${index} row ${row} column ${column}`);
131      this.mAppIconPositionY = this.mPageDesktopStyleConfig.mDesktopMarginTop + folderItem.row * (gridItemHeight) + paddingTop
132      + this.mPageDesktopStyleConfig.mIconMarginVertical + folderTopPadding + (row - 1) * (folderGridGap + folderAppSize);
133
134      this.mAppIconPositionX = this.mPageDesktopStyleConfig.mMargin + folderItem.column * (gridItemWidth) + folderLeftMargin
135      + folderLeftPadding + (column - 1) * (folderGridGap + folderAppSize);
136
137      if (folderItem.page != AppStorage.Get('pageIndex')) {
138        AppStorage.SetOrCreate('pageIndex', folderItem.page);
139      }
140    }
141    this.mFindPagedesktopPosition = true;
142  }
143
144  private getIndexInFolderAppList(appItem, folderItem): number {
145    let index: number = 0;
146    if (CheckEmptyUtils.isEmpty(appItem) || CheckEmptyUtils.isEmpty(folderItem)
147    || CheckEmptyUtils.isEmpty(folderItem.layoutInfo[0])) {
148      Log.showError(TAG, `getIndexInFolderAppList with invalid appItem or folderItem`);
149    }
150
151    for (var i = 0; i < folderItem.layoutInfo[0].length; i++) {
152      if (folderItem.layoutInfo[0][i]?.bundleName === appItem.bundleName) {
153        index = i;
154        break;
155      }
156    }
157
158    return index + 1;
159  }
160
161  private getCloseAppItemInfo(): any {
162    Log.showDebug(TAG, `getCloseAppItemInfo called!`);
163    let appListInfo: {
164      appGridInfo: [[]]
165    } = AppStorage.Get('appListInfo')
166    let appGridInfo = appListInfo.appGridInfo;
167    if (CheckEmptyUtils.isEmptyArr(appGridInfo)) {
168      Log.showError(TAG, `getCloseAppItemInfo appGridInfo is null`);
169      return null;
170    } else {
171      for (var i = 0; i < appGridInfo.length; i++) {
172        let swipeGridInfo: any[] = appGridInfo[i];
173        if (!CheckEmptyUtils.isEmptyArr(swipeGridInfo)) {
174          for (var j = 0; j < swipeGridInfo.length; j++) {
175            let item = swipeGridInfo[j];
176            switch (item.typeId) {
177              case CommonConstants.TYPE_APP:
178                if (this.mAppItemBundleName === item.bundleName) {
179                  this.mCloseAppType = true;
180                  const appInfo = {
181                    bundleName: item.bundleName,
182                    abilityName: item.abilityName,
183                    moduleName: item.moduleName,
184                    appIconSize: this.mCloseAppType ? StyleConstants.DEFAULT_ADD_APP_SIZE : StyleConstants.DEFAULT_FOLDER_APP_SIZE,
185                    appIconId: item.appIconId,
186                    icon: globalThis.ResourceManager
187                      .getCachedAppIcon(item.appIconId, item.bundleName, item.moduleName),
188                    row: item.row,
189                    column: item.column,
190                    page: item.page
191                  }
192                  return appInfo;
193                }
194                break;
195              case CommonConstants.TYPE_FOLDER:
196                Log.showDebug(TAG, `getCloseAppItemInfo foldItem case ${item.layoutInfo}`);
197                let foldItem = this.getFolderItem(item.layoutInfo);
198                if (CheckEmptyUtils.isEmpty(foldItem)) {
199                  Log.showError(TAG, `getCloseAppItemInfo foldItem is null`);
200                  break;
201                } else {
202                  this.mCloseAppType = false;
203                  this.mFolderItem = item;
204                  return foldItem;
205                }
206              default:
207                break;
208            }
209          }
210        }
211      }
212      return null;
213    }
214  }
215
216  private getFolderItem(layoutInfo: [[]]): any{
217    if (CheckEmptyUtils.isEmptyArr(layoutInfo)) {
218      Log.showError(TAG, `getFolderItem layoutInfo is null`);
219      return null;
220    } else {
221      let foldPage: [] = layoutInfo[0];
222      if (!CheckEmptyUtils.isEmptyArr(foldPage)) {
223        for (var j = 0; j < foldPage.length; j++) {
224          let appItem: any = foldPage[j];
225          Log.showDebug(TAG, `getFolderItem appItem is ${JSON.stringify(appItem)}`);
226          if (!CheckEmptyUtils.isEmpty(appItem) && this.mAppItemBundleName == appItem.bundleName) {
227            Log.showDebug(TAG, `getFolderItem return item is ${JSON.stringify(appItem)}`);
228            return appItem;
229          }
230        }
231      }
232      return null;
233    }
234  }
235}
236