• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// @ts-nocheck
2/*
3 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import Log from '../../default/Log';
18import SourceLoader from './SourceLoader';
19import Constants, { PluginType, ItemComponentData, LoaderConfigInfo } from '../common/Constants';
20import { AbilityInfoWithId, filterAbilityInfo, PluginData } from '../common/BundleParseUtil';
21import { parseEventString } from '../../default/event/EventUtil';
22
23const TAG = 'MetaSourceLoader';
24
25export default class MetaSourceLoader extends SourceLoader {
26  mPluginFilter = '';
27  mPermission = '';
28  mItemDatas: ItemComponentData[] = [];
29
30  constructor(config: LoaderConfigInfo) {
31    super(config);
32    this.mPluginFilter = config.action as string;
33    this.mPermission = config.permission as string;
34    Log.showDebug(TAG, `init loader, mPluginFilter: ${this.mPluginFilter}, mPermission: ${this.mPermission}`);
35  }
36
37  onAbilityAdd(abilityInfo: AbilityInfoWithId): void {
38    let pluginData: PluginData | undefined = filterAbilityInfo(abilityInfo, this.mPluginFilter);
39    if (pluginData) {
40      let itemData = parseData(abilityInfo, pluginData);
41      if (!itemData) {
42        return;
43      }
44      this.mItemDatas.push(itemData);
45      this.addItem(itemData);
46      Log.showDebug(TAG, `item[${itemData.id}] add success, name: ${abilityInfo.name}`);
47      return;
48    }
49    Log.showDebug(TAG, `Can't filter ${abilityInfo.name}.`);
50  }
51
52  onBundleRemove(bundleName: string): void {
53    for (let i = this.mItemDatas.length - 1; i >= 0; i--) {
54      if (bundleName == this.mItemDatas[i].bundleName) {
55        Log.showDebug(TAG, `remove item index: ${i}, abilityname: ${this.mItemDatas[i].abilityName}`);
56        this.removeItem(this.mItemDatas[i]);
57        this.mItemDatas.splice(i, 1);
58      }
59    }
60  }
61
62  clearData(): void {
63    Log.showDebug(TAG, `clear all, size: ${this.mItemDatas.length}`);
64    this.mItemDatas.forEach((data) => this.removeItem(data));
65    this.mItemDatas.length = 0;
66  }
67
68  reloadData(userId: number): void {
69    Log.showDebug(TAG, `reloadData userId: ${userId}`);
70  }
71}
72
73function parseData(info: AbilityInfoWithId, data: PluginData): ItemComponentData {
74  let { label, pluginType, icon, template, clickAction, longClickAction, launchType, ...extra } = data;
75  if (pluginType == undefined || pluginType == null || pluginType.toString() != PluginType.META.toString()) {
76    return undefined;
77  }
78  let itemData: ItemComponentData = {
79    id: info.itemId,
80    pluginType: PluginType.META,
81    deviceId: Constants.LOCAL_DEVICE,
82    bundleName: info.bundleName,
83    moduleName: info.moduleName,
84    abilityName: info.name,
85    abilityLabelId: info.labelId,
86    abilityIconId: info.iconId,
87    label: label,
88    iconUrl: icon,
89    template: template,
90    actionData: {
91      clickAction: parseEventString(clickAction),
92      longClickAction: parseEventString(longClickAction),
93      launchType: launchType,
94      extra: extra,
95    },
96  };
97  return itemData;
98}