1 /* 2 * Copyright (c) 2022-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 16 package ohos; 17 18 import java.util.HashMap; 19 import java.util.List; 20 import java.util.ArrayList; 21 import java.util.Map; 22 23 /** 24 * collection of method for ModuleAdaption. 25 * 26 */ 27 class ModuleAdaption { 28 29 private static final Log LOG = new Log(ModuleAdaption.class.toString()); 30 /** 31 * convert moduleResult to uncompressResult function. 32 * 33 * @param moduleResult for module.json result 34 * @return UncomperssResult 35 */ convertToUncompressResult(ModuleResult moduleResult)36 UncompressResult convertToUncompressResult(ModuleResult moduleResult) { 37 UncompressResult uncomperssResult = new UncompressResult(); 38 uncomperssResult.setResult(moduleResult.getResult()); 39 uncomperssResult.setMessage(moduleResult.getMessage()); 40 for (String moduelProfile : moduleResult.moduleProfileStr) { 41 uncomperssResult.addProfileInfoStr(moduelProfile); 42 } 43 // adapt pack.info 44 uncomperssResult.setPackInfos(moduleResult.packInfos); 45 // adapt profileInfos 46 for (int i = 0; i < moduleResult.moduleProfileInfos.size(); ++i) { 47 ModuleProfileInfo moduleProfileInfo = moduleResult.moduleProfileInfos.get(i); 48 ProfileInfo profileInfo = new ProfileInfo(); 49 profileInfo.hapName = moduleProfileInfo.hapName; 50 // adapt appInfo 51 profileInfo.appInfo = convertToAppInfo(moduleProfileInfo.moduleAppInfo); 52 //adapt deviceconfig 53 profileInfo.deviceConfig = convertToDeviceConfig(moduleProfileInfo.moduleAppInfo); 54 // adapt hapInfo 55 profileInfo.hapInfo = convertToHapInfo(moduleProfileInfo.moduleInfo); 56 uncomperssResult.addProfileInfo(profileInfo); 57 } 58 for (ModuleProfileInfo profileInfo : moduleResult.moduleProfileInfos) { 59 if (profileInfo.moduleAppInfo != null) { 60 uncomperssResult.setLabel(profileInfo.moduleAppInfo.label); 61 uncomperssResult.setIcon(profileInfo.moduleAppInfo.icon); 62 } 63 } 64 return uncomperssResult; 65 } 66 67 /** 68 * convert ModuleAppInfo to AppInfo function. 69 * 70 * @param moduleAppInfo for module.json app result 71 * @return AppInfo 72 */ convertToAppInfo(ModuleAppInfo moduleAppInfo)73 AppInfo convertToAppInfo(ModuleAppInfo moduleAppInfo) { 74 AppInfo appInfo = new AppInfo(); 75 if (moduleAppInfo == null) { 76 LOG.error("convertToAppInfo failed: moduleAppInfo is null"); 77 return appInfo; 78 } 79 appInfo.bundleName = moduleAppInfo.bundleName; 80 appInfo.debug = moduleAppInfo.debug; 81 appInfo.icon = moduleAppInfo.icon; 82 appInfo.label = moduleAppInfo.label; 83 appInfo.description = moduleAppInfo.description; 84 appInfo.vendor = moduleAppInfo.vendor; 85 appInfo.versionCode = String.valueOf(moduleAppInfo.versionCode); 86 appInfo.versionName = moduleAppInfo.versionName; 87 appInfo.minCompatibleVersionCode = moduleAppInfo.minCompatibleVersionCode; 88 appInfo.compatibleApiVersion = moduleAppInfo.minAPIVersion; 89 appInfo.targetApiVersion = moduleAppInfo.targetAPIVersion; 90 appInfo.releaseType = moduleAppInfo.apiReleaseType; 91 appInfo.distributedNotificationEnabled = moduleAppInfo.distributedNotificationEnabled; 92 appInfo.appName = moduleAppInfo.appName; 93 appInfo.appNameEN = moduleAppInfo.appNameEN; 94 appInfo.setBundleType(moduleAppInfo.getBundleType()); 95 return appInfo; 96 } 97 98 /** 99 * convert moduleAppInfo deviceTypes to DeviceConfig function. 100 * 101 * @param moduleAppInfo for module.json app result 102 * @return Map<String, DeviceConfig> 103 */ convertToDeviceConfig(ModuleAppInfo moduleAppInfo)104 Map<String, DeviceConfig> convertToDeviceConfig(ModuleAppInfo moduleAppInfo) { 105 Map<String, DeviceConfig> deviceConfigMap = new HashMap<>(); 106 if (moduleAppInfo == null) { 107 return deviceConfigMap; 108 } 109 for(Map.Entry<String, ModuleDeviceType> entrty : moduleAppInfo.deviceTypes.entrySet()) { 110 DeviceConfig deviceConfig = new DeviceConfig(); 111 deviceConfig.distributedNotificationEnabled = entrty.getValue().distributedNotificationEnabled; 112 deviceConfigMap.put(entrty.getKey(), deviceConfig); 113 } 114 return deviceConfigMap; 115 } 116 117 /** 118 * convert moduleInfo to HapInfo function. 119 * 120 * @param moduleInfo for module.json module result 121 * @return HapInfo 122 */ convertToHapInfo(ModuleInfo moduleInfo)123 HapInfo convertToHapInfo(ModuleInfo moduleInfo) { 124 HapInfo hapInfo = new HapInfo(); 125 if (moduleInfo == null) { 126 LOG.error("convertToHapInfo failed: moduleInfo is null"); 127 return hapInfo; 128 } 129 hapInfo.name = moduleInfo.name; 130 hapInfo.distro = new Distro(); 131 hapInfo.distro.moduleName = moduleInfo.name; 132 hapInfo.distro.moduleType = moduleInfo.type; 133 hapInfo.distro.installationFree = moduleInfo.installationFree; 134 hapInfo.distro.virtualMachine = moduleInfo.virtualMachine; 135 hapInfo.distro.deliveryWithInstall = moduleInfo.deliveryWithInstall; 136 hapInfo.srcEntrance = moduleInfo.srcEntrance; 137 hapInfo.description = moduleInfo.description; 138 hapInfo.process = moduleInfo.process; 139 hapInfo.mainElement = moduleInfo.mainElement; 140 hapInfo.deviceType = moduleInfo.deviceTypes; 141 hapInfo.uiSyntax = moduleInfo.uiSyntax; 142 // convert pages 143 hapInfo.pages = moduleInfo.pages; 144 // convert moduleshortcut to short 145 hapInfo.shortcuts = convertToShortcut(moduleInfo.moduleShortcuts); 146 // convert metadata 147 hapInfo.metaData = convertToMetadata(moduleInfo.moduleMetadataInfos); 148 // convert abilities 149 hapInfo.abilities = convertToAbilityInfo(moduleInfo.abilities); 150 // convert extension abilities to abilities 151 hapInfo.extensionAbilityInfos = moduleInfo.extensionAbilityInfos; 152 // convert request permissions 153 hapInfo.reqPermissions = moduleInfo.requestPermissions; 154 // convert distrofilter 155 hapInfo.distroFilter = moduleInfo.distroFilter; 156 hapInfo.formInfos = moduleInfo.abilityFormInfos; 157 hapInfo.commonEvents = moduleInfo.commonEvents; 158 hapInfo.definePermissions = moduleInfo.definePermissions; 159 hapInfo.moduleAtomicService = moduleInfo.moduleAtomicService; 160 hapInfo.setAppModel(moduleInfo.getAppModel()); 161 hapInfo.setDependencies(moduleInfo.getDependenies()); 162 return hapInfo; 163 } 164 165 /** 166 * convert moduleShortcuts to HapInfo Shortcut. 167 * 168 * @param moduleShortcuts for module.json shortcut result 169 * @return List<Shortcut> 170 */ convertToShortcut(List<ModuleShortcut> moduleShortcuts)171 List<Shortcut> convertToShortcut(List<ModuleShortcut> moduleShortcuts) { 172 List<Shortcut> shortcuts = new ArrayList<>(); 173 for (ModuleShortcut value : moduleShortcuts) { 174 Shortcut shortcut = new Shortcut(); 175 shortcut.shortcutId = value.getShortcutId(); 176 shortcut.label = value.getLabel(); 177 shortcut.icon = value.getIcon(); 178 shortcut.intents = new ArrayList<>(); 179 for (int j = 0; j < value.getWants().size(); ++j) { 180 Want want = value.getWants().get(j); 181 IntentInfo intentInfo = new IntentInfo(); 182 intentInfo.targetBundle = want.bundleName; 183 intentInfo.targetClass = want.abilityName; 184 shortcut.intents.add(intentInfo); 185 } 186 shortcuts.add(shortcut); 187 } 188 return shortcuts; 189 } 190 191 /** 192 * convert metadataInfos to MetaData function. 193 * 194 * @param metadataInfos for module.json metadata result 195 * @return MetaData 196 */ convertToMetadata(List<ModuleMetadataInfo> metadataInfos)197 MetaData convertToMetadata(List<ModuleMetadataInfo> metadataInfos) { 198 MetaData metaData = new MetaData(); 199 List<CustomizeData> customizeDatas = new ArrayList<>(); 200 for (ModuleMetadataInfo metadataInfo : metadataInfos) { 201 CustomizeData customizeData = new CustomizeData(); 202 customizeData.name = metadataInfo.name; 203 customizeData.value = metadataInfo.value; 204 customizeData.extra = metadataInfo.resource; 205 customizeDatas.add(customizeData); 206 } 207 metaData.customizeDatas = customizeDatas; 208 return metaData; 209 } 210 211 /** 212 * convert ModuleAbilityInfo to AbilityInfo function. 213 * 214 * @param moduleAbilityInfos for module.json ability result 215 * @return List<AbilityInfo> 216 */ convertToAbilityInfo(List<ModuleAbilityInfo> moduleAbilityInfos)217 List<AbilityInfo> convertToAbilityInfo(List<ModuleAbilityInfo> moduleAbilityInfos) { 218 List<AbilityInfo> abilityInfos = new ArrayList<>(); 219 for (ModuleAbilityInfo info : moduleAbilityInfos) { 220 AbilityInfo abilityInfo = new AbilityInfo(); 221 abilityInfo.type = "page"; 222 abilityInfo.name = info.name; 223 abilityInfo.srcEntrance = info.srcEntrance; 224 abilityInfo.description = info.description; 225 abilityInfo.launchType = info.launchType; 226 abilityInfo.icon = info.icon; 227 abilityInfo.iconPath = info.icon; 228 abilityInfo.label = info.label; 229 abilityInfo.labelRes = info.label; 230 abilityInfo.permissions = info.permissions; 231 abilityInfo.visible = info.visible; 232 abilityInfo.continuable = info.continuable; 233 // convert metadata 234 abilityInfo.metaData = convertToMetadata(info.metadata); 235 // convert skillInfo 236 abilityInfo.skills = info.skills; 237 // convert configchanges 238 abilityInfo.backgroundModes = info.backgroundModes; 239 abilityInfos.add(abilityInfo); 240 } 241 return abilityInfos; 242 } 243 } 244