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 formManagerAbility from '@ohos.app.form.formHost'; 17import { FormConstants } from '../constants/FormConstants'; 18import { Log } from '../utils/Log'; 19import { CardItemInfo } from '../bean/CardItemInfo'; 20import { CommonConstants } from '../constants/CommonConstants'; 21import { launcherAbilityManager } from './LauncherAbilityManager'; 22 23const TAG = 'FormManager'; 24 25/** 26 * Wrapper class for formManager interfaces. 27 */ 28export class FormManager { 29 private readonly CARD_SIZE_1x2: number[] = [2, 1]; 30 private readonly CARD_SIZE_2x2: number[] = [2, 2]; 31 private readonly CARD_SIZE_2x4: number[] = [4, 2]; 32 private readonly CARD_SIZE_4x4: number[] = [4, 4]; 33 34 private constructor() { 35 } 36 /** 37 * form manager instance 38 * 39 * @return formManager instance 40 */ 41 static getInstance(): FormManager { 42 if (globalThis.FormManagerInstance == null) { 43 globalThis.FormManagerInstance = new FormManager(); 44 } 45 return globalThis.FormManagerInstance; 46 } 47 48 /** 49 * get all form info 50 * 51 * @return Array<CardItemInfo> cardItemInfoList 52 */ 53 async getAllFormsInfo(): Promise<CardItemInfo[]> { 54 const formList = await formManagerAbility.getAllFormsInfo(); 55 const cardItemInfoList = new Array<CardItemInfo>(); 56 for (const formItem of formList) { 57 const cardItemInfo = new CardItemInfo(); 58 cardItemInfo.bundleName = formItem.bundleName; 59 cardItemInfo.abilityName = formItem.abilityName; 60 cardItemInfo.moduleName = formItem.moduleName; 61 cardItemInfo.cardName = formItem.name; 62 cardItemInfo.cardDimension = formItem.defaultDimension; 63 cardItemInfo.area = this.getCardSize(cardItemInfo.cardDimension); 64 cardItemInfo.description = formItem.description; 65 cardItemInfo.formConfigAbility = formItem.formConfigAbility; 66 cardItemInfo.supportDimensions = formItem.supportDimensions; 67 cardItemInfoList.push(cardItemInfo); 68 } 69 return cardItemInfoList; 70 } 71 72 getCardSize(dimension: number): number[] { 73 if (dimension == CommonConstants.CARD_DIMENSION_1x2) { 74 return this.CARD_SIZE_1x2; 75 } else if (dimension == CommonConstants.CARD_DIMENSION_2x2) { 76 return this.CARD_SIZE_2x2; 77 } else if (dimension == CommonConstants.CARD_DIMENSION_2x4) { 78 return this.CARD_SIZE_2x4; 79 } else { 80 return this.CARD_SIZE_4x4; 81 } 82 } 83 84 /** 85 * get form info by bundleName 86 * 87 * @param bundle 88 */ 89 async getFormsInfoByApp(bundle: string): Promise<CardItemInfo[]> { 90 Log.showDebug(TAG, `getFormsInfoByApp bundle: ${bundle}`); 91 const formList = await formManagerAbility.getFormsInfo(bundle); 92 const cardItemInfoList = new Array<CardItemInfo>(); 93 for (const formItem of formList) { 94 const cardItemInfo = new CardItemInfo(); 95 cardItemInfo.bundleName = formItem.bundleName; 96 cardItemInfo.abilityName = formItem.abilityName; 97 cardItemInfo.moduleName = formItem.moduleName; 98 cardItemInfo.cardName = formItem.name; 99 cardItemInfo.cardDimension = formItem.defaultDimension; 100 cardItemInfo.area = this.getCardSize(cardItemInfo.cardDimension); 101 cardItemInfo.description = formItem.description; 102 cardItemInfo.formConfigAbility = formItem.formConfigAbility; 103 cardItemInfo.supportDimensions = formItem.supportDimensions; 104 cardItemInfoList.push(cardItemInfo); 105 } 106 return cardItemInfoList; 107 } 108 109 /** 110 * get formCardItem by want parameters 111 * 112 * @param parameters 113 * @return formCardItem 114 */ 115 async getFormCardItemByWant(params) { 116 Log.showDebug(TAG, 'getFormCardItemByWant'); 117 let formCardItem: any = {}; 118 formCardItem.id = params[FormConstants.ID_PARAM]; 119 formCardItem.name = params[FormConstants.NAME_PARAM]; 120 formCardItem.bundleName = params[FormConstants.BUNDLE_PARAM]; 121 formCardItem.abilityName = params[FormConstants.ABILITY_PARAM]; 122 formCardItem.moduleName = params[FormConstants.MODULE_PARAM]; 123 formCardItem.dimension = params[FormConstants.DIMENSION_PARAM]; 124 formCardItem.formConfigAbility = await this.getFormConfigAbility(params[FormConstants.BUNDLE_PARAM], 125 params[FormConstants.MODULE_PARAM], params[FormConstants.ABILITY_PARAM], params[FormConstants.NAME_PARAM]); 126 const appInfo = await launcherAbilityManager.getAppInfoByBundleName(params[FormConstants.BUNDLE_PARAM] , 127 params[FormConstants.ABILITY_PARAM]); 128 formCardItem.appLabelId = appInfo.appLabelId; 129 return formCardItem; 130 } 131 132 /** 133 * get formConfigAbility by bundleName moduleName abilityName and cardName 134 * 135 * @param bundle 136 * @param moduleName 137 * @param abilityName 138 * @param cardName 139 * @return formConfigAbility 140 */ 141 async getFormConfigAbility(bundle:string, moduleName:string, abilityName:string, cardName:string) : Promise<string> { 142 const formList = await formManagerAbility.getFormsInfo(bundle); 143 let formConfigAbility = ""; 144 for (const formItem of formList) { 145 if(formItem.moduleName == moduleName && formItem.abilityName == abilityName && formItem.name == cardName ) { 146 formConfigAbility = formItem.formConfigAbility; 147 break; 148 } 149 } 150 return formConfigAbility; 151 } 152 153 /** 154 * get form info by bundleName and moduleName 155 * 156 * @param bundle 157 * @param moduleName 158 */ 159 async getFormsInfoByModule(bundle: string, moduleName: string): Promise<CardItemInfo[]> { 160 const formList = await formManagerAbility.getFormsInfo(bundle, moduleName); 161 const cardItemInfoList = new Array<CardItemInfo>(); 162 for (const formItem of formList) { 163 const cardItemInfo = new CardItemInfo(); 164 cardItemInfo.bundleName = formItem.bundleName; 165 cardItemInfo.abilityName = formItem.abilityName; 166 cardItemInfo.moduleName = formItem.moduleName; 167 cardItemInfo.cardName = formItem.name; 168 cardItemInfo.cardDimension = formItem.defaultDimension; 169 cardItemInfo.area = this.getCardSize(cardItemInfo.cardDimension); 170 cardItemInfo.description = formItem.description; 171 cardItemInfo.formConfigAbility = formItem.formConfigAbility; 172 cardItemInfo.supportDimensions = formItem.supportDimensions; 173 cardItemInfoList.push(cardItemInfo); 174 } 175 return cardItemInfoList; 176 } 177 178 /** 179 * delete form info by formId 180 * 181 * @param formId 182 */ 183 async deleteCard(formId: string): Promise<void> { 184 return await formManagerAbility.deleteForm(formId); 185 } 186 187} 188 189