1/** 2 * Copyright (c) 2022-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 { BaseStartAppHandler } from '@ohos/common'; 17import { StyleConstants } from '@ohos/common'; 18import { AppItemInfo } from '@ohos/common'; 19import { layoutConfigManager } from '@ohos/common'; 20import { Log } from '@ohos/common'; 21import { CheckEmptyUtils } from '@ohos/common'; 22import AppcenterConstants from '../common/constants/AppcenterConstants'; 23 24import AppCenterGridStyleConfig from './AppCenterGridStyleConfig'; 25 26const TAG = 'AppCenterStartAppHandler'; 27 28/** 29 * app center start app processing class 30 */ 31export default class AppCenterStartAppHandler extends BaseStartAppHandler { 32 private mAppCenterGridStyleConfig; 33 34 private constructor() { 35 super(); 36 this.mAppCenterGridStyleConfig = layoutConfigManager.getStyleConfig(AppCenterGridStyleConfig.APP_GRID_STYLE_CONFIG, 37 AppcenterConstants.FEATURE_NAME); 38 } 39 40 static getInstance(): AppCenterStartAppHandler { 41 if (globalThis.AppCenterStartAppHandler == null) { 42 globalThis.AppCenterStartAppHandler = new AppCenterStartAppHandler(); 43 } 44 return globalThis.AppCenterStartAppHandler; 45 } 46 47 protected calculateAppIconPosition(): void { 48 if (CheckEmptyUtils.isEmpty(this.mAppCenterGridStyleConfig)) { 49 Log.showError(TAG, `calculateAppIconPosition with invalid config`) 50 return; 51 } 52 53 const appItemInfo = AppStorage.get('startAppItemInfo'); 54 const index: number = this.getIndexInAppList(appItemInfo); 55 const appCenterMarginLeft = this.mAppCenterGridStyleConfig.mAppCenterMarginLeft; 56 const appCenterMarginTop = this.mAppCenterGridStyleConfig.mIconMarginVertical; 57 const appCenterWidth = this.mAppCenterGridStyleConfig.mGridWidth; 58 const appCenterHeight = this.mAppCenterGridStyleConfig.mGridHeight; 59 const appCenterItemWidth = this.mAppCenterGridStyleConfig.mAppItemSize; 60 const appCenterItemHeight = this.mAppCenterGridStyleConfig.mAppItemSize; 61 const appCenterSizeWidth = this.mAppCenterGridStyleConfig.mIconSize; 62 const appCenterSizeHeight = this.mAppCenterGridStyleConfig.mIconSize; 63 const appCenterColumns = this.mAppCenterGridStyleConfig.mColumns; 64 const appCenterRows = this.mAppCenterGridStyleConfig.mRows; 65 66 let row = Math.floor(index / appCenterColumns); 67 let column = index % appCenterColumns; 68 if (column != 0) { 69 row += 1; 70 } else { 71 column = appCenterColumns; 72 } 73 Log.showInfo(TAG, `calculateAppIconPosition index ${index} row ${row} column ${column}`) 74 75 const columnsGap = this.mAppCenterGridStyleConfig.mColumnsGap; 76 let itemLeftPadding = (appCenterItemWidth - appCenterSizeWidth) / 2; 77 let gapByFixed = (appCenterWidth - appCenterColumns * appCenterItemWidth - (appCenterColumns - 1) * columnsGap) / (2 * appCenterColumns); 78 this.mAppIconPositionX = appCenterMarginLeft + gapByFixed + (column - 1) * (appCenterItemWidth + 2 * gapByFixed + columnsGap) + itemLeftPadding; 79 80 const rowsGap = this.mAppCenterGridStyleConfig.mRowsGap; 81 const appCenterPadding = this.mAppCenterGridStyleConfig.mPadding; 82 this.mAppIconPositionY = StyleConstants.DEFAULT_28 + appCenterPadding + appCenterMarginTop + (row - 1) * (appCenterItemHeight + rowsGap); 83 } 84 85 private getIndexInAppList(appItemInfo): number { 86 let index: number = 0; 87 let listInfo: AppItemInfo[] = AppStorage.get('listInfo'); 88 for (var i = 0; i < listInfo.length; i++) { 89 if (typeof listInfo[i] !== 'undefined') { 90 if (appItemInfo.bundleName === listInfo[i].bundleName) { 91 index = i; 92 break; 93 } 94 } 95 } 96 97 return index + 1; 98 } 99} 100