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