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 delete this.mDialogController; 72 this.mDialogController = null; 73 } 74 75 aboutToAppear(): void { 76 this.updateAppCenterScreen(); 77 } 78 updateAppCenterScreen(): void { 79 Log.showInfo(TAG, "aboutToAppear") 80 this.mAppGridViewModel = AppGridViewModel.getInstance(); 81 this.mAppGridViewModel.getAppList(); 82 this.mAppCenterGridStyleConfig = this.mAppGridViewModel.getAppGridStyleConfig(); 83 this.mAppCenterStartAppHandler = AppCenterStartAppHandler.getInstance(); 84 ResourceManager.getInstance().getStringByResource($r('app.string.isUninstall')).then((resName) => { 85 this.dialogName = resName; 86 }); 87 } 88 89 public buildMenuInfoList(appInfo) { 90 let menuInfoList = new Array<MenuInfo>(); 91 let shortcutInfo: any = this.mAppGridViewModel.getShortcutInfo(appInfo.bundleName); 92 if (shortcutInfo) { 93 let menu = null; 94 shortcutInfo.forEach((value) => { 95 menu = new MenuInfo(); 96 menu.menuType = CommonConstants.MENU_TYPE_DYNAMIC; 97 menu.menuImgSrc = value.icon; 98 menu.menuText = value.label; 99 menu.shortcutIconId = value.iconId; 100 menu.shortcutLabelId = value.labelId; 101 menu.bundleName = value.bundleName; 102 menu.moduleName = value.moduleName; 103 menu.onMenuClick = () => { 104 this.mAppGridViewModel.jumpTo(value.wants[0].targetClass, value.wants[0].targetBundle, value.wants[0].targetModule); 105 }; 106 value.bundleName == appInfo.bundleName && value.moduleName == appInfo.moduleName && menuInfoList.push(menu); 107 }); 108 } 109 110 let open = new MenuInfo(); 111 open.menuType = CommonConstants.MENU_TYPE_FIXED; 112 open.menuImgSrc = "/common/pics/ic_public_add_norm.svg"; 113 open.menuText = $r('app.string.app_menu_open'); 114 open.onMenuClick = () => { 115 Log.showInfo(TAG,`open.OnMenuClick--:${JSON.stringify(appInfo)}`); 116 Trace.start(Trace.CORE_METHOD_START_APP_ANIMATION); 117 this.setStartAppInfo(appInfo); 118 this.mAppGridViewModel.jumpTo(appInfo.abilityName, appInfo.bundleName, appInfo.moduleName); 119 windowManager.hideWindow(windowManager.APP_CENTER_WINDOW_NAME); 120 }; 121 menuInfoList.push(open); 122 123 let addToDockMenu = new MenuInfo(); 124 addToDockMenu.menuType = CommonConstants.MENU_TYPE_FIXED; 125 addToDockMenu.menuImgSrc = "/common/pics/ic_public_copy.svg"; 126 addToDockMenu.menuText = $r('app.string.app_center_menu_add_dock'); 127 addToDockMenu.onMenuClick = () => { 128 Log.showDebug(TAG, "Launcher click menu item add to smartDock entry"); 129 localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_DOCK_ITEM_ADD, appInfo); 130 }; 131 menuInfoList.push(addToDockMenu); 132 133 let addToDeskMenu = new MenuInfo(); 134 addToDeskMenu.menuType = CommonConstants.MENU_TYPE_FIXED; 135 addToDeskMenu.menuImgSrc = "/common/pics/ic_public_copy.svg"; 136 addToDeskMenu.menuText = $r('app.string.app_center_menu_add_desktop'); 137 addToDeskMenu.onMenuClick = () => { 138 Log.showDebug(TAG, 'click menu item add to pageDesk entry'); 139 localEventManager.sendLocalEventSticky(EventConstants.EVENT_REQUEST_PAGEDESK_ITEM_ADD, appInfo); 140 }; 141 menuInfoList.push(addToDeskMenu); 142 143 let uninstallMenu = new MenuInfo(); 144 uninstallMenu.menuType = CommonConstants.MENU_TYPE_FIXED; 145 uninstallMenu.menuImgSrc = "/common/pics/ic_public_delete.svg"; 146 uninstallMenu.menuText = $r('app.string.uninstall'); 147 uninstallMenu.onMenuClick = () => { 148 Log.showDebug(TAG, `click menu item uninstall: ${JSON.stringify(appInfo)}`); 149 let cacheKey = appInfo.appLabelId + appInfo.bundleName + appInfo.moduleName; 150 let appName = this.mAppGridViewModel.getAppName(cacheKey); 151 Log.showDebug(TAG, `buildMenuInfoList appName: ${appName}`); 152 if (appName != null) { 153 appInfo.appName = appName; 154 } 155 this.mSelectItem = appInfo; 156 this.mDialogController.open(); 157 AppStorage.SetOrCreate('uninstallAppInfo', appInfo); 158 }; 159 uninstallMenu.menuEnabled = appInfo.isUninstallAble; 160 menuInfoList.push(uninstallMenu); 161 return menuInfoList; 162 } 163 164 build() { 165 Column() { 166 Row() { 167 AppGrid({ 168 appGridList: $appGridList, 169 isScroll: true, 170 appGridStyleConfig: $mAppCenterGridStyleConfig, 171 onItemClick: (event, item) => { 172 Log.showDebug(TAG,`open.onAppGridClick--:${JSON.stringify(item)}`); 173 Trace.start(Trace.CORE_METHOD_START_APP_ANIMATION); 174 this.setStartAppInfo(item); 175 this.mAppGridViewModel.jumpTo(item.abilityName, item.bundleName, item.moduleName); 176 windowManager.hideWindow(windowManager.APP_CENTER_WINDOW_NAME); 177 }, 178 buildMenu: (item) => this.buildMenuInfoList(item) 179 }) 180 } 181 .width(this.mAppCenterGridStyleConfig.mGridWidth) 182 .height(this.mAppCenterGridStyleConfig.mGridHeight) 183 184 Row() { 185 186 }.width(216) 187 if (this.traceBuildEnd()) {} 188 } 189 .padding({ 190 top: this.mAppCenterGridStyleConfig.mPadding 191 }) 192 .backgroundImageSize(ImageSize.Cover) 193 .width(StyleConstants.PERCENTAGE_100) 194 .height(this.workSpaceHeight - StyleConstants.DEFAULT_28) 195 } 196 197 private traceBuildEnd() { 198 Trace.end(Trace.CORE_METHOD_START_APP_CENTER); 199 return true; 200 } 201 202 /** 203 * set start app info 204 */ 205 private setStartAppInfo(item) { 206 Log.showInfo(TAG, `appcenter StartAppInfo:${JSON.stringify(item)}`); 207 if (CheckEmptyUtils.isEmpty(item)) { 208 Log.showError(TAG, `setStartAppInfo with item`) 209 return; 210 } 211 AppStorage.SetOrCreate('startAppTypeFromPageDesktop', CommonConstants.OVERLAY_TYPE_APP_ICON); 212 item.icon = ResourceManager.getInstance().getCachedAppIcon(item.appIconId, item.bundleName, item.moduleName); 213 AppStorage.SetOrCreate('startAppItemInfo', item); 214 this.mAppCenterStartAppHandler.setAppIconSize(this.mAppCenterGridStyleConfig.mIconSize); 215 this.mAppCenterStartAppHandler.setAppIconInfo(); 216 } 217} 218