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