• 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 BaseDataSource from './BaseDataSource';
17import {
18  CardItemInfo,
19  FormListInfoCacheManager,
20  CommonConstants,
21  Log,
22  ObjectCopyUtil
23} from '@ohos/common';
24import ArrayList from '@ohos.util.ArrayList';
25
26const TAG = 'IncrementDataSource';
27
28/**
29 * 推荐卡片资源池数据源:最新安装的应用程序和最新更新的应用程序基类
30 */
31export default abstract class IncrementDataSource extends BaseDataSource {
32  private readonly mFormListInfoCacheManager: FormListInfoCacheManager = FormListInfoCacheManager.getInstance();
33
34  /**
35   * 获取缓存key
36   *
37   * @return 缓存key
38   */
39  public abstract getCacheKey(): string;
40
41  /**
42   * 获取卡片资源池的初始值列表
43   *
44   * @return 卡片资源池的初始值列表
45   */
46  protected getThresholdDataList(dataSourceList: string[],
47    allDataMap: Map<string, CardItemInfo>): ArrayList<CardItemInfo> {
48    let resDataList: ArrayList<CardItemInfo> = new ArrayList<CardItemInfo>();
49    let abilityFormBundleNameSet: Set<string> = this.mRecommendCardsDataPool.getPageDeskAllFormSet();
50    let cardItemInfoList = this.getCardItemInfoList();
51    for (let i: number = 0; i < cardItemInfoList.length; i++) {
52      if (abilityFormBundleNameSet.has(cardItemInfoList[i].bundleName)) {
53        continue;
54      }
55      resDataList.add(cardItemInfoList[i]);
56    }
57    return resDataList;
58  }
59
60  protected getSourceDataList(): string[] {
61    return [];
62  }
63
64  private getCardItemInfoList(): CardItemInfo[] {
65    let mDataList: CardItemInfo[] = [];
66    let mDataListTemp: ArrayList<CardItemInfo> = this.mFormListInfoCacheManager.getCache(this.getCacheKey());
67    for (let i: number = 0; i < mDataListTemp.length; i++) {
68      mDataList.push(ObjectCopyUtil.DeepClone(mDataListTemp[i]));
69    }
70    return mDataList;
71  }
72
73  /**
74   * 添加卡片信息到缓存
75   *
76   * @param 卡片信息
77   */
78  public onDataAdd(formInfo: CardItemInfo): void {
79    let mDataList: any = this.mFormListInfoCacheManager.getCache(this.getCacheKey());
80    if (mDataList === CommonConstants.INVALID_VALUE) {
81      mDataList = new ArrayList<CardItemInfo>();
82    }
83    Log.showInfo(TAG, `add card: ${JSON.stringify(formInfo)}`);
84    for (let i: number = 0; i < mDataList.length; i++) {
85      let cardInfo = mDataList[i];
86      if (cardInfo.bundleName === formInfo.bundleName) {
87        Log.showInfo(TAG, 'onDataAdd same data, return');
88        return;
89      }
90    }
91    if (mDataList.length === this.getThreshold()) {
92      mDataList.removeByIndex(0);
93    }
94    mDataList.add(formInfo);
95    this.mFormListInfoCacheManager.setCache(this.getCacheKey(), mDataList);
96  }
97
98  /**
99   * 根据bundleName从缓存中移除卡片信息
100   *
101   * @param bundleName bundleName
102   */
103  public onDataRemove(bundleName: string): void {
104    Log.showInfo(TAG, `remove card: ${bundleName}`);
105    let mDataList: ArrayList<CardItemInfo> = this.mFormListInfoCacheManager.getCache(this.getCacheKey());
106    if (mDataList === null || mDataList.length === 0) {
107      return;
108    }
109    let mUpateDataList: ArrayList<CardItemInfo> = new ArrayList();
110    for (let i: number = 0; i < mDataList.length; i++) {
111      if (bundleName === mDataList[i].bundleName) {
112        continue;
113      }
114      mUpateDataList.add(mDataList[i]);
115    }
116    this.mFormListInfoCacheManager.setCache(this.getCacheKey(), mUpateDataList);
117  }
118}
119