• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15
16import launcherBundleManager from '@ohos.bundle.launcherBundleManager';
17import { AppItemInfo, CheckEmptyUtils } from '@ohos/common';
18import { Log } from '@ohos/common'
19import { Trace } from '@ohos/common';
20import { StyleConstants } from '@ohos/common';
21import { CommonConstants } from '@ohos/common';
22import { EventConstants } from '@ohos/common';
23import { AppGrid } from '@ohos/common/component';
24import { UninstallDialog } from '@ohos/common/component';
25import { MenuInfo } from '@ohos/common';
26import { ResourceManager } from '@ohos/common';
27import { localEventManager } from '@ohos/common';
28import { AppGridViewModel } from '../viewmodel/AppGridViewModel';
29import AppCenterGridStyleConfig from '../common/AppCenterGridStyleConfig';
30import AppCenterStartAppHandler from '../common/AppCenterStartAppHandler';
31import { windowManager } from '@ohos/common';
32
33const TAG = 'AppGridLayout';
34
35class StartAppInfo extends AppItemInfo {
36  icon?: ResourceStr;
37}
38
39@Component
40export struct AppGridLayout {
41  @StorageLink('workSpaceHeight')@Watch('updateAppCenterScreen') workSpaceHeight: number = 0;
42  @State mColumns: number = StyleConstants.DEFAULT_APP_GRID_COLUMN;
43  @State mRows: number = StyleConstants.DEFAULT_APP_GRID_ROW;
44  @StorageLink('listInfo') appGridList: AppItemInfo[] = [];
45  mAppGridViewModel: AppGridViewModel = AppGridViewModel.getInstance();
46  @StorageLink('appGridStyleConfig') mAppCenterGridStyleConfig: AppCenterGridStyleConfig
47    = this.mAppGridViewModel.getAppGridStyleConfig();
48  mSelectItem: AppItemInfo = new AppItemInfo();
49  private mAppCenterStartAppHandler: AppCenterStartAppHandler = AppCenterStartAppHandler.getInstance();
50  private dialogName: string = '';
51  mDialogController: CustomDialogController | null = new CustomDialogController({
52    builder: UninstallDialog({
53      cancel: () => {
54      },
55      confirm: () => {
56        this.onAccept()
57      },
58      dialogName: this.dialogName,
59      dialogContent: this.mSelectItem.appName + ' ?',
60    }),
61    cancel: () => {
62    },
63    autoCancel: false,
64    customStyle: true
65  });
66
67  onAccept() {
68    if (typeof this.mSelectItem.bundleName === 'undefined') {
69      return;
70    }
71    this.mAppGridViewModel.uninstallApp(this.mSelectItem.bundleName, this.mSelectItem.isUninstallAble as boolean);
72    this.mDialogController?.close();
73  }
74
75  aboutToDisappear(): void {
76    Log.showInfo(TAG, "aboutToDisappear");
77    this.mDialogController = null;
78  }
79
80  aboutToAppear(): void {
81    this.updateAppCenterScreen();
82  }
83  updateAppCenterScreen(): void {
84    Log.showInfo(TAG, "aboutToAppear")
85    this.mAppGridViewModel = AppGridViewModel.getInstance();
86    this.mAppGridViewModel.getAppList();
87    this.mAppCenterGridStyleConfig = this.mAppGridViewModel.getAppGridStyleConfig();
88    this.mAppCenterStartAppHandler = AppCenterStartAppHandler.getInstance();
89    ResourceManager.getInstance().getStringByResource($r('app.string.isUninstall')).then((resName) => {
90      this.dialogName = resName;
91    });
92  }
93
94  public buildMenuInfoList(appInfo: AppItemInfo) {
95    let menuInfoList = new Array<MenuInfo>();
96    let shortcutInfo = this.mAppGridViewModel.getShortcutInfo(appInfo.bundleName);
97    if (shortcutInfo) {
98      let menu: MenuInfo | null = null;
99      shortcutInfo.forEach((value: launcherBundleManager.ShortcutInfo) => {
100        menu = new MenuInfo();
101        menu.menuType = CommonConstants.MENU_TYPE_DYNAMIC;
102        menu.menuImgSrc = value.icon;
103        menu.menuText = value.label;
104        menu.shortcutIconId = value.iconId;
105        menu.shortcutLabelId =  value.labelId;
106        menu.bundleName = value.bundleName;
107        menu.moduleName = value.moduleName;
108        menu.onMenuClick = () => {
109          this.mAppGridViewModel.jumpTo(value.wants[0].targetAbility, value.wants[0].targetBundle, value.wants[0].targetModule);
110        };
111        value.bundleName == appInfo.bundleName && value.moduleName == appInfo.moduleName && menuInfoList.push(menu);
112      });
113    }
114
115    let open = new MenuInfo();
116    open.menuType = CommonConstants.MENU_TYPE_FIXED;
117    open.menuImgSrc = "/common/pics/ic_public_add_norm.svg";
118    open.menuText = $r('app.string.app_menu_open');
119    open.onMenuClick = () => {
120      Log.showInfo(TAG,`open.OnMenuClick--:${JSON.stringify(appInfo)}`);
121      Trace.start(Trace.CORE_METHOD_START_APP_ANIMATION);
122      this.setStartAppInfo(appInfo as StartAppInfo);
123      this.mAppGridViewModel.jumpTo(appInfo.abilityName, appInfo.bundleName, appInfo.moduleName);
124      windowManager.hideWindow(windowManager.APP_CENTER_WINDOW_NAME);
125    };
126    menuInfoList.push(open);
127
128    let addToDockMenu = new MenuInfo();
129    addToDockMenu.menuType = CommonConstants.MENU_TYPE_FIXED;
130    addToDockMenu.menuImgSrc = "/common/pics/ic_public_copy.svg";
131    addToDockMenu.menuText = $r('app.string.app_center_menu_add_dock');
132    addToDockMenu.onMenuClick = () => {
133      Log.showDebug(TAG, "Launcher click menu item add to smartDock entry");
134      localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_DOCK_ITEM_ADD, appInfo);
135    };
136    menuInfoList.push(addToDockMenu);
137
138    let addToDeskMenu = new MenuInfo();
139    addToDeskMenu.menuType = CommonConstants.MENU_TYPE_FIXED;
140    addToDeskMenu.menuImgSrc = "/common/pics/ic_public_copy.svg";
141    addToDeskMenu.menuText = $r('app.string.app_center_menu_add_desktop');
142    addToDeskMenu.onMenuClick = () => {
143      Log.showDebug(TAG, 'click menu item add to pageDesk entry');
144      localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_PAGEDESK_ITEM_ADD, appInfo);
145    };
146    menuInfoList.push(addToDeskMenu);
147
148    let uninstallMenu = new MenuInfo();
149    uninstallMenu.menuType = CommonConstants.MENU_TYPE_FIXED;
150    uninstallMenu.menuImgSrc = "/common/pics/ic_public_delete.svg";
151    uninstallMenu.menuText = $r('app.string.uninstall');
152    uninstallMenu.onMenuClick = () => {
153      Log.showDebug(TAG, `click menu item uninstall: ${JSON.stringify(appInfo)}`);
154      let cacheKey = '';
155      if (appInfo.appLabelId && appInfo.bundleName && appInfo.moduleName) {
156        cacheKey = appInfo.appLabelId + appInfo.bundleName + appInfo.moduleName;
157      }
158      let appName = this.mAppGridViewModel.getAppName(cacheKey);
159      Log.showDebug(TAG, `buildMenuInfoList appName: ${appName}`);
160      if (appName != null) {
161        appInfo.appName = appName;
162      }
163      this.mSelectItem = appInfo;
164      this.mDialogController?.open();
165      AppStorage.setOrCreate('uninstallAppInfo', appInfo);
166    };
167    uninstallMenu.menuEnabled = appInfo.isUninstallAble as boolean;
168    menuInfoList.push(uninstallMenu);
169    return menuInfoList;
170  }
171
172  build() {
173    Column() {
174      Row() {
175        AppGrid({
176          appGridList: $appGridList,
177          isScroll: true,
178          appGridStyleConfig: $mAppCenterGridStyleConfig,
179          onItemClick: (event: ClickEvent, item: AppItemInfo) => {
180            Log.showDebug(TAG,`open.onAppGridClick--:${JSON.stringify(item)}`);
181            Trace.start(Trace.CORE_METHOD_START_APP_ANIMATION);
182            this.setStartAppInfo(item as StartAppInfo);
183            this.mAppGridViewModel.jumpTo(item.abilityName, item.bundleName, item.moduleName);
184            windowManager.hideWindow(windowManager.APP_CENTER_WINDOW_NAME);
185          },
186          buildMenu: (item: AppItemInfo): MenuInfo[] => this.buildMenuInfoList(item)
187        })
188      }
189      .width(this.mAppCenterGridStyleConfig.mGridWidth)
190      .height(this.mAppCenterGridStyleConfig.mGridHeight)
191
192      Row() {
193
194      }.width(216)
195      if (this.traceBuildEnd()) {}
196    }
197    .padding({
198      top: this.mAppCenterGridStyleConfig.mPadding
199    })
200    .backgroundImageSize(ImageSize.Cover)
201    .width(StyleConstants.PERCENTAGE_100)
202    .height(this.workSpaceHeight - StyleConstants.DEFAULT_28)
203  }
204
205  private traceBuildEnd() {
206    Trace.end(Trace.CORE_METHOD_START_APP_CENTER);
207    return true;
208  }
209
210  /**
211   * set start app info
212   */
213  private setStartAppInfo(item: StartAppInfo) {
214    Log.showInfo(TAG, `appcenter StartAppInfo:${JSON.stringify(item)}`);
215    if (CheckEmptyUtils.isEmpty(item)) {
216      Log.showError(TAG, `setStartAppInfo with item`)
217      return;
218    }
219    AppStorage.setOrCreate('startAppTypeFromPageDesktop', CommonConstants.OVERLAY_TYPE_APP_ICON);
220    item.icon = ResourceManager.getInstance().getCachedAppIcon(item.appIconId, item.bundleName, item.moduleName);
221    AppStorage.setOrCreate('startAppItemInfo', item);
222    this.mAppCenterStartAppHandler.setAppIconSize(this.mAppCenterGridStyleConfig.mIconSize);
223    this.mAppCenterStartAppHandler.setAppIconInfo();
224  }
225}
226