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