• 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  public 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: 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(
110            value.wants[0].targetAbility, value.wants[0].targetBundle, value.wants[0].targetModule
111          );
112        };
113        value.bundleName == appInfo.bundleName && value.moduleName == appInfo.moduleName && menuInfoList.push(menu);
114      });
115    }
116
117    let open = new MenuInfo();
118    open.menuType = CommonConstants.MENU_TYPE_FIXED;
119    open.menuImgSrc = '/common/pics/ic_public_add_norm.svg';
120    open.menuText = $r('app.string.app_menu_open');
121    open.onMenuClick = () => {
122      Log.showInfo(TAG,`open.OnMenuClick--:${JSON.stringify(appInfo)}`);
123      Trace.start(Trace.CORE_METHOD_START_APP_ANIMATION);
124      this.setStartAppInfo(appInfo as StartAppInfo);
125      this.mAppGridViewModel.jumpTo(appInfo.abilityName, appInfo.bundleName, appInfo.moduleName);
126      windowManager.hideWindow(windowManager.APP_CENTER_WINDOW_NAME);
127    };
128    menuInfoList.push(open);
129
130    let addToDockMenu = new MenuInfo();
131    addToDockMenu.menuType = CommonConstants.MENU_TYPE_FIXED;
132    addToDockMenu.menuImgSrc = '/common/pics/ic_public_copy.svg';
133    addToDockMenu.menuText = $r('app.string.app_center_menu_add_dock');
134    addToDockMenu.onMenuClick = () => {
135      Log.showDebug(TAG, 'Launcher click menu item add to smartDock entry');
136      localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_DOCK_ITEM_ADD, appInfo);
137    };
138    menuInfoList.push(addToDockMenu);
139
140    let addToDeskMenu = new MenuInfo();
141    addToDeskMenu.menuType = CommonConstants.MENU_TYPE_FIXED;
142    addToDeskMenu.menuImgSrc = '/common/pics/ic_public_copy.svg';
143    addToDeskMenu.menuText = $r('app.string.app_center_menu_add_desktop');
144    addToDeskMenu.onMenuClick = () => {
145      Log.showDebug(TAG, 'click menu item add to pageDesk entry');
146      localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_PAGEDESK_ITEM_ADD, appInfo);
147    };
148    menuInfoList.push(addToDeskMenu);
149
150    let uninstallMenu = new MenuInfo();
151    uninstallMenu.menuType = CommonConstants.MENU_TYPE_FIXED;
152    uninstallMenu.menuImgSrc = '/common/pics/ic_public_delete.svg';
153    uninstallMenu.menuText = $r('app.string.uninstall');
154    uninstallMenu.onMenuClick = () => {
155      Log.showDebug(TAG, `click menu item uninstall: ${JSON.stringify(appInfo)}`);
156      let cacheKey = '';
157      if (appInfo.appLabelId && appInfo.bundleName && appInfo.moduleName) {
158        cacheKey = appInfo.appLabelId + appInfo.bundleName + appInfo.moduleName;
159      }
160      let appName = this.mAppGridViewModel.getAppName(cacheKey);
161      Log.showDebug(TAG, `buildMenuInfoList appName: ${appName}`);
162      if (appName != null) {
163        appInfo.appName = appName;
164      }
165      this.mSelectItem = appInfo;
166      this.mDialogController?.open();
167      AppStorage.setOrCreate('uninstallAppInfo', appInfo);
168    };
169    uninstallMenu.menuEnabled = appInfo.isUninstallAble as boolean;
170    menuInfoList.push(uninstallMenu);
171    return menuInfoList;
172  }
173
174  build() {
175    Column() {
176      Row() {
177        AppGrid({
178          appGridList: $appGridList,
179          isScroll: true,
180          appGridStyleConfig: $mAppCenterGridStyleConfig,
181          onItemClick: (event: ClickEvent, item: AppItemInfo) => {
182            Log.showDebug(TAG,`open.onAppGridClick--:${JSON.stringify(item)}`);
183            Trace.start(Trace.CORE_METHOD_START_APP_ANIMATION);
184            this.setStartAppInfo(item as StartAppInfo);
185            this.mAppGridViewModel.jumpTo(item.abilityName, item.bundleName, item.moduleName);
186            windowManager.hideWindow(windowManager.APP_CENTER_WINDOW_NAME);
187          },
188          buildMenu: (item: AppItemInfo): MenuInfo[] => this.buildMenuInfoList(item)
189        })
190      }
191      .width(this.mAppCenterGridStyleConfig.mGridWidth)
192      .height(this.mAppCenterGridStyleConfig.mGridHeight)
193
194      Row() {
195
196      }.width(216)
197      if (this.traceBuildEnd()) {}
198    }
199    .padding({
200      top: this.mAppCenterGridStyleConfig.mPadding
201    })
202    .backgroundImageSize(ImageSize.Cover)
203    .width(StyleConstants.PERCENTAGE_100)
204    .height(this.workSpaceHeight - StyleConstants.DEFAULT_28)
205  }
206
207  private traceBuildEnd() {
208    Trace.end(Trace.CORE_METHOD_START_APP_CENTER);
209    return true;
210  }
211
212  /**
213   * set start app info
214   */
215  private setStartAppInfo(item: StartAppInfo) {
216    Log.showInfo(TAG, `appcenter StartAppInfo:${JSON.stringify(item)}`);
217    if (CheckEmptyUtils.isEmpty(item)) {
218      Log.showError(TAG, 'setStartAppInfo with item')
219      return;
220    }
221    AppStorage.setOrCreate('startAppTypeFromPageDesktop', CommonConstants.OVERLAY_TYPE_APP_ICON);
222    item.icon = ResourceManager.getInstance().getCachedAppIcon(item.appIconId, item.bundleName, item.moduleName);
223    AppStorage.setOrCreate('startAppItemInfo', item);
224    this.mAppCenterStartAppHandler.setAppIconSize(this.mAppCenterGridStyleConfig.mIconSize);
225    this.mAppCenterStartAppHandler.setAppIconInfo();
226  }
227}
228