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