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