• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Log from '../../default/Log';
17import SourceLoader from './SourceLoader';
18import Constants, { PluginType, ItemComponentData, LoaderConfigInfo } from '../common/Constants';
19import { AbilityInfoWithId, filterAbilityInfo, PluginData } from '../common/BundleParseUtil';
20import { parseEventString } from '../../default/event/EventUtil';
21
22const TAG = 'MetaSourceLoader';
23
24export default class MetaSourceLoader extends SourceLoader {
25  mPluginFilter = '';
26  mPermission = '';
27  mItemDatas: ItemComponentData[] = [];
28
29  constructor(config: LoaderConfigInfo) {
30    super(config);
31    this.mPluginFilter = config.action as string;
32    this.mPermission = config.permission as string;
33    Log.showDebug(TAG, `init loader, mPluginFilter: ${this.mPluginFilter}, mPermission: ${this.mPermission}`);
34  }
35
36  onAbilityAdd(abilityInfo: AbilityInfoWithId): void {
37    let pluginData: PluginData | undefined = filterAbilityInfo(abilityInfo, this.mPluginFilter);
38    if (pluginData) {
39      let itemData = parseData(abilityInfo, pluginData);
40      if (!itemData) {
41        return;
42      }
43      this.mItemDatas.push(itemData);
44      this.addItem(itemData);
45      Log.showDebug(TAG, `item[${itemData.id}] add success, name: ${abilityInfo.name}`);
46      return;
47    }
48    Log.showDebug(TAG, `Can't filter ${abilityInfo.name}.`);
49  }
50
51  onBundleRemove(bundleName: string): void {
52    for (let i = this.mItemDatas.length - 1; i >= 0; i--) {
53      if (bundleName == this.mItemDatas[i].bundleName) {
54        Log.showDebug(TAG, `remove item index: ${i}, abilityname: ${this.mItemDatas[i].abilityName}`);
55        this.removeItem(this.mItemDatas[i]);
56        this.mItemDatas.splice(i, 1);
57      }
58    }
59  }
60
61  clearData(): void {
62    Log.showDebug(TAG, `clear all, size: ${this.mItemDatas.length}`);
63    this.mItemDatas.forEach((data) => this.removeItem(data));
64    this.mItemDatas.length = 0;
65  }
66
67  reloadData(userId: number): void {
68    Log.showDebug(TAG, `reloadData userId: ${userId}`);
69  }
70}
71
72function parseData(info: AbilityInfoWithId, data: PluginData): ItemComponentData {
73  let { label, pluginType, icon, template, clickAction, longClickAction, launchType, ...extra } = data;
74  if (pluginType == undefined || pluginType == null || pluginType.toString() != PluginType.META.toString()) {
75    return undefined;
76  }
77  let itemData: ItemComponentData = {
78    id: info.itemId,
79    pluginType: PluginType.META,
80    deviceId: Constants.LOCAL_DEVICE,
81    bundleName: info.bundleName,
82    moduleName: info.moduleName,
83    abilityName: info.name,
84    abilityLabelId: info.labelId,
85    abilityIconId: info.iconId,
86    label: label,
87    iconUrl: icon,
88    template: template,
89    actionData: {
90      clickAction: parseEventString(clickAction),
91      longClickAction: parseEventString(longClickAction),
92      launchType: launchType,
93      extra: extra,
94    },
95  };
96  return itemData;
97}