1/* 2 * Copyright (c) 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 { CardItemInfo } from '../bean/CardItemInfo' 18import { CommonConstants } from '../constants/CommonConstants' 19import { Logger } from '../utils/Logger' 20 21const TAG: string = 'FormManager' 22 23/** 24 * Wrapper class for formManager interfaces. 25 */ 26class FormManagerModel { 27 private readonly CARD_SIZE_1x2: number[] = [1, 2] 28 private readonly CARD_SIZE_2x2: number[] = [2, 2] 29 private readonly CARD_SIZE_2x4: number[] = [2, 4] 30 private readonly CARD_SIZE_4x4: number[] = [4, 4] 31 32 /** 33 * get all form info 34 * 35 * @return Array<CardItemInfo> cardItemInfoList 36 */ 37 public async getAllFormsInfo(): Promise<CardItemInfo[]> { 38 const formList = await formManagerAbility.getAllFormsInfo() 39 const cardItemInfoList = new Array<CardItemInfo>() 40 for (const formItem of formList) { 41 const cardItemInfo = new CardItemInfo() 42 cardItemInfo.bundleName = formItem.bundleName 43 cardItemInfo.abilityName = formItem.abilityName 44 cardItemInfo.moduleName = formItem.moduleName 45 cardItemInfo.cardName = formItem.name 46 cardItemInfo.cardDimension = formItem.defaultDimension 47 cardItemInfo.description = formItem.description 48 cardItemInfo.formConfigAbility = formItem.formConfigAbility 49 cardItemInfo.supportDimensions = formItem.supportDimensions 50 cardItemInfo.area = this.getCardSize(cardItemInfo.cardDimension) 51 cardItemInfoList.push(cardItemInfo) 52 } 53 return cardItemInfoList 54 } 55 56 /** 57 * get card area by dimension 58 * 59 * @param dimension 60 * @return number[] 61 */ 62 public getCardSize(dimension: number): number[] { 63 if (dimension == CommonConstants.CARD_DIMENSION_1x2) { 64 return this.CARD_SIZE_1x2 65 } else if (dimension == CommonConstants.CARD_DIMENSION_2x2) { 66 return this.CARD_SIZE_2x2 67 } else if (dimension == CommonConstants.CARD_DIMENSION_2x4) { 68 return this.CARD_SIZE_2x4 69 } else { 70 return this.CARD_SIZE_4x4 71 } 72 } 73 74 /** 75 * get card dimension bty area 76 * 77 * @param dimension 78 * @return number[] 79 */ 80 public getCardDimension(area: number[]) { 81 if (area.toString() === this.CARD_SIZE_1x2.toString()) { 82 return CommonConstants.CARD_DIMENSION_1x2 83 } else if (area.toString() === this.CARD_SIZE_2x2.toString()) { 84 return CommonConstants.CARD_DIMENSION_2x2 85 } else if (area.toString() == this.CARD_SIZE_2x4.toString()) { 86 return CommonConstants.CARD_DIMENSION_2x4 87 } else { 88 return CommonConstants.CARD_DIMENSION_4x4 89 } 90 } 91 92 /** 93 * get form info by bundleName 94 * 95 * @param bundle 96 * @return Array<CardItemInfo> cardItemInfoList 97 */ 98 public async getFormsInfoByApp(bundle: string): Promise<CardItemInfo[]> { 99 Logger.info(TAG, `getFormsInfoByApp bundle: ${bundle}`) 100 const formList = await formManagerAbility.getFormsInfo(bundle) 101 const cardItemInfoList = new Array<CardItemInfo>() 102 for (const formItem of formList) { 103 const cardItemInfo = new CardItemInfo() 104 cardItemInfo.bundleName = formItem.bundleName 105 cardItemInfo.abilityName = formItem.abilityName 106 cardItemInfo.moduleName = formItem.moduleName 107 cardItemInfo.cardName = formItem.name 108 cardItemInfo.cardDimension = formItem.defaultDimension 109 cardItemInfo.area = this.getCardSize(cardItemInfo.cardDimension) 110 cardItemInfo.description = formItem.description 111 cardItemInfo.formConfigAbility = formItem.formConfigAbility 112 cardItemInfo.supportDimensions = formItem.supportDimensions 113 cardItemInfoList.push(cardItemInfo) 114 } 115 return cardItemInfoList 116 } 117} 118 119export let FormManager = new FormManagerModel() 120 121