1/** 2 * Copyright (c) 2023-2023 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 CardsDataPool from './CardsDataPool'; 17import { 18 CardItemInfo, 19 Log 20} from '@ohos/common'; 21import FormConstants from '../common/constants/FormConstants'; 22 23const TAG = 'RecommendCardsDataPool'; 24 25/** 26 * Recommendation module card data pool 27 */ 28export default class RecommendCardsDataPool extends CardsDataPool { 29 private mAllDataMap: Map<string, CardItemInfo> = new Map(); 30 private mAllBundleFormCount: Map<string, number> = new Map(); 31 private mFormSet: Set<string> = new Set<string>(); 32 33 constructor() { 34 super(); 35 } 36 37 static getInstance(): RecommendCardsDataPool { 38 if (globalThis.RecommendCardsDataPool == null) { 39 globalThis.RecommendCardsDataPool = new RecommendCardsDataPool(); 40 } 41 return globalThis.RecommendCardsDataPool; 42 } 43 44 /** 45 * 获取卡片数据map 46 */ 47 async initAllDataMap(): Promise<void> { 48 Log.showInfo(TAG, 'initAllDataMap'); 49 this.mAllDataMap = new Map(); 50 51 // 取到全量的卡片数据 52 let allFormMap: Map<string, CardItemInfo[]> = await this.getAllFormsInfo(); 53 Log.showInfo(TAG, `The number of all cards is ${allFormMap.size}`); 54 55 // 遍历过滤出没有添加到桌面应用对应的卡片 56 let abilityFormBundleNameSet: Set<string> = await this.getAbilityFormSet(); 57 for (let [cardItemBundleName, cardItemInfos] of allFormMap.entries()) { 58 if (abilityFormBundleNameSet.has(cardItemBundleName)) { 59 continue; 60 } 61 62 // 设置应用的卡片总数 63 this.setFormCount(cardItemBundleName, cardItemInfos); 64 65 // 从应用中选取一张卡片,优先选择默认卡片 66 let cardItemInfo: CardItemInfo = this.getRecommendForm(cardItemInfos); 67 if (cardItemInfo != null) { 68 this.mAllDataMap.set(cardItemBundleName, cardItemInfo); 69 } 70 } 71 Log.showInfo(TAG, `The number of remain show cards is ${this.mAllDataMap.size}`); 72 } 73 74 private async getAllFormsInfo(): Promise<Map<string, CardItemInfo[]>> { 75 let allFormMap: Map<string, CardItemInfo[]> = new Map(); 76 let formInfoList: CardItemInfo[] = await this.mFormModel.getAllFormsInfo(); 77 formInfoList.forEach(item => { 78 let forms: CardItemInfo[] = new Array(); 79 if (allFormMap.has(item.bundleName)) { 80 forms = allFormMap.get(item.bundleName); 81 } 82 forms.push(item); 83 allFormMap.set(item.bundleName, forms); 84 }); 85 Log.showInfo(TAG, `The number of all cards is ${allFormMap.size}`); 86 return allFormMap; 87 } 88 89 /** 90 * 设置应用的卡片总数 91 * 92 * @param bundle名称 93 * @param 卡片信息集 94 */ 95 private setFormCount(cardItemBundleName: string, cardItemInfos: CardItemInfo[]): void { 96 Log.showInfo(TAG, `enter setFormCount, cardItemInfo is ${JSON.stringify(cardItemInfos)}`); 97 if (cardItemInfos.length === 0) { 98 Log.showInfo(TAG, 'exit setFormCount, card item is empty'); 99 this.mAllBundleFormCount.set(cardItemBundleName, 0); 100 } 101 let dimensions: number[] = []; 102 cardItemInfos.forEach(formInfo => { 103 formInfo.supportDimensions.forEach(item => dimensions.push(item)); 104 }); 105 Log.showInfo(TAG, `exit setFormCount, totle count is ${dimensions.length}}`); 106 this.mAllBundleFormCount.set(cardItemBundleName, dimensions.length); 107 } 108 109 /** 110 * 获取应用的卡片总数 111 * 112 * @param 应用名称 113 * @return 应用的卡片总数 114 */ 115 getFormCount(bundleName: string): number { 116 return this.mAllBundleFormCount.get(bundleName); 117 } 118 119 /** 120 * 获取推荐卡片:SmallForm-->MediumForm 121 * 122 * @param 卡片信息集 123 * @return 推荐卡片 124 */ 125 getRecommendForm(cardItemInfos: CardItemInfo[]): CardItemInfo { 126 if (cardItemInfos.length === 0) { 127 return null; 128 } 129 let dimensions: number[] = []; 130 cardItemInfos.forEach(formInfo => { 131 formInfo.supportDimensions.forEach(item => dimensions.push(item)); 132 }); 133 dimensions = Array.from(new Set(dimensions)); 134 for (let i: number = 0; i < cardItemInfos.length; i++) { 135 let recommendForm: CardItemInfo = cardItemInfos[i]; 136 let supportDimensions: number[] = recommendForm.supportDimensions; 137 for (let j: number = 0; j < supportDimensions.length; j++) { 138 recommendForm.supportDimensions = dimensions; 139 if (supportDimensions[j] === FormConstants.SMALL_SIZE_SERVICE_FORM) { 140 recommendForm.cardDimension = FormConstants.SMALL_SIZE_SERVICE_FORM; 141 return recommendForm; 142 } 143 if (supportDimensions[j] === FormConstants.MEDIUM_SIZE_SERVICE_FORM) { 144 recommendForm.cardDimension = FormConstants.MEDIUM_SIZE_SERVICE_FORM; 145 return recommendForm; 146 } 147 } 148 } 149 return null; 150 } 151 152 private async getAbilityFormSet(): Promise<Set<string>> { 153 let abilityFormBundleNameSet: Set<string> = new Set<string>(); 154 let abilityFormList: CardItemInfo[] = await this.mFormModel.getAllFormsInfoFromRdb(); 155 abilityFormList.forEach(item => { 156 abilityFormBundleNameSet.add(item.bundleName); 157 }); 158 this.mFormSet = abilityFormBundleNameSet; 159 Log.showInfo(TAG, `The number of desktop cards is ${abilityFormBundleNameSet.size}`); 160 return abilityFormBundleNameSet; 161 } 162 163 /** 164 * 获取所有卡片map 165 * 166 * @return 所有卡片map 167 */ 168 getAllDataMap(): Map<string, CardItemInfo> { 169 return this.mAllDataMap; 170 } 171 172 /** 173 * 获取所有添加到桌面的卡片set. 174 * 175 * @return 所有添加到桌面的卡片set. 176 */ 177 getPageDeskAllFormSet(): Set<string> { 178 return this.mFormSet; 179 } 180} 181