1 /* 2 * Copyright (c) 2021-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.ArrayList; 19 import java.util.List; 20 import java.util.HashMap; 21 import java.util.Map; 22 import java.util.Locale; 23 24 import com.alibaba.fastjson.JSON; 25 import com.alibaba.fastjson.JSONObject; 26 import com.alibaba.fastjson.JSONArray; 27 import com.alibaba.fastjson.JSONException; 28 29 /** 30 * Json Util. 31 * 32 */ 33 public class JsonUtil { 34 private static final String DEVICE_TYPE = "device-type"; 35 private static final String NAME = "name"; 36 private static final String VALUE = "value"; 37 private static final String RESOURCE = "resource"; 38 private static final String MODULE_TYPE = "module-type"; 39 private static final String INSTALL_FLAG = "delivery-with-install"; 40 private static final String DEVICE_TYPE_NEW = "deviceType"; 41 private static final String MODULE_TYPE_NEW = "moduleType"; 42 private static final String INSTALL_FLAG_NEW = "deliveryWithInstall"; 43 private static final String ASSETS_DIR_NAME = "assets/"; 44 private static final String PROFILE = "$profile:"; 45 private static final Log LOG = new Log(JsonUtil.class.toString()); 46 47 private static final String SUMMARY = "summary"; 48 private static final String APP = "app"; 49 private static final String VERSION = "version"; 50 private static final String LEGACY_VERSION_CODE = "legacyVersionCode"; 51 private static final String LEGACY_VERSION_NAME = "legacyVersionName"; 52 private static final String MULTI_FRAMEWORK_BUNDLE = "multiFrameworkBundle"; 53 private static final String ACTION_SYSTEM_HOME = "action.system.home"; 54 private static final String ENTITY_SYSTEM_HOME = "entity.system.home"; 55 private static final String MAIN_ABILITY = "mainAbility"; 56 private static final String MAIN_ELEMENT = "mainElement"; 57 private static final String PAGE = "page"; 58 private static final String SERVICE = "service"; 59 private static final String FORM = "form"; 60 private static final String PACKAGES = "packages"; 61 private static final String ABILITIES = "abilities"; 62 private static final String WHEN = "when"; 63 private static final String STRING_RESOURCE = "$string:"; 64 private static final String EMPTY = ""; 65 private static final String BUNDLENAME = "bundleName"; 66 private static final String BUNDLE_TYPE = "bundleType"; 67 private static final String VERSIONCODE = "versionCode"; 68 private static final String VERSIONNAME = "versionName"; 69 private static final String PATCH_VERSION_CODE = "patchVersionCode"; 70 private static final String PATCH_VERSION_NAME = "patchVersionName"; 71 private static final String ORIGINAL_MODULE_HASH = "originalModuleHash"; 72 private static final String MODULE = "module"; 73 private static final String DEVICE_TYPES = "deviceTypes"; 74 private static final String TYPE = "type"; 75 private static final String LABEL = "label"; 76 private static final String LABEL_ID = "labelId"; 77 private static final String DESCRIPTION = "description"; 78 private static final String DESCRIPTION_ID = "descriptionId"; 79 private static final String VERSION_CODE = "versionCode"; 80 private static final String VENDOR = "vendor"; 81 private static final String VERSION_NAME = "versionName"; 82 private static final String MIN_COMPATIBLE_VERSION_CODE = "minCompatibleVersionCode"; 83 private static final String DEBUG = "debug"; 84 private static final String MIN_API_VERSION = "minAPIVersion"; 85 private static final String TARGET_API_VERSION = "targetAPIVersion"; 86 private static final String API_RELEASE_TYPE = "apiReleaseType"; 87 private static final String DISTRIBUTED_NOTIFICATION_ENABLED = "distributedNotificationEnabled"; 88 private static final String ENTITY_TYPE = "entityType"; 89 private static final String UNSPECIFIED = "unspecified"; 90 private static final String SRC_ENTRANCE = "srcEntrance"; 91 private static final String SRC_ENTRY = "srcEntry"; 92 private static final String PROCESS = "process"; 93 private static final String PHONE = "phone"; 94 private static final String TABLET = "tablet"; 95 private static final String TV = "tv"; 96 private static final String WEARABLE = "wearable"; 97 private static final String AR = "ar"; 98 private static final String VR = "vr"; 99 private static final String CAR = "car"; 100 private static final String EARPHONES = "earphones"; 101 private static final String SPEAKER = "speaker"; 102 private static final String LINK_IOT = "linkIOT"; 103 private static final String ROUTER = "router"; 104 private static final String TWO_IN_ONE = "2in1"; 105 private static final String DELIVERY_WITH_INSTALL = "deliveryWithInstall"; 106 private static final String INSTALLATION_FREE = "installationFree"; 107 private static final String VIRTUAL_MACHINE = "virtualMachine"; 108 private static final String DEFAULT = "default"; 109 private static final String HML = "hml"; 110 private static final String UI_SYNTAX = "uiSyntax"; 111 private static final String PAGES = "pages"; 112 private static final String JSON_SUFFIX = ".json"; 113 private static final String META_DATA = "metadata"; 114 private static final String SRC = "src"; 115 private static final String EXTENSION_ABILITIES = "extensionAbilities"; 116 private static final String REQ_PERMISSIONS = "reqPermissions"; 117 private static final String REQUEST_PERMISSIONS = "requestPermissions"; 118 private static final String REASON = "reason"; 119 private static final String REASON_ID = "reasonId"; 120 private static final String USED_SCENE = "usedScene"; 121 private static final String DEFINE_PERMISSIONS = "definePermissions"; 122 private static final String DISTRO_FILTER = "distroFilter"; 123 private static final String DISTRIBUTION_FILTER = "distributionFilter"; 124 private static final String LAUNCH_TYPE = "launchType"; 125 private static final String STANDARD = "standard"; 126 private static final String PERMISSIONS = "permissions"; 127 private static final String READ_PERMISSION = "readPermission"; 128 private static final String WRITE_PERMISSION = "writePermission"; 129 private static final String VISIBLE = "visible"; 130 private static final String EXPORTED = "exported"; 131 private static final String CONTINUABLE = "continuable"; 132 private static final String SKILLS = "skills"; 133 private static final String BACKGROUND_MODES = "backgroundModes"; 134 private static final String URI = "uri"; 135 private static final String FORMS = "forms"; 136 private static final String WINDOW = "window"; 137 private static final String IS_DEFAULT = "isDefault"; 138 private static final String COLOR_MODE = "colorMode"; 139 private static final String SUPPORT_DIMENSIONS = "supportDimensions"; 140 private static final String DEFAULT_DIMENSION = "defaultDimension"; 141 private static final String UPDATE_ENABLED = "updateEnabled"; 142 private static final String SCHEDULE_UPDATE_TIME = "scheduledUpdateTime"; 143 private static final String UPDATE_DURATION = "updateDuration"; 144 private static final String FROM_CONFIG_ABILITY = "formConfigAbility"; 145 private static final String FORM_VISIBLE_NOTIFY = "formVisibleNotify"; 146 private static final String AUTO = "auto"; 147 private static final String DOT = "."; 148 private static final String ATOMIC_SERVICE = "atomicService"; 149 private static final String SPLIT = "split"; 150 private static final String MAIN = "main"; 151 private static final String PRELOADS = "preloads"; 152 private static final String MODULE_NAME = "moduleName"; 153 private static final int DEFAULT_VERSION_CODE = -1; 154 private static final String DEPENDENCY_BUNDLE_NAME = "bundleName"; 155 private static final String DEPENDENCY_MODULE_NAME = "moduleName"; 156 private static final String DEPENDENCIES = "dependencies"; 157 private static final String COMPILE_SDK_VERSION = "compileSdkVersion"; 158 private static final String COMPILE_SDK_TYPE = "compileSdkType"; 159 160 161 /** 162 * parse hap list by device type 163 * 164 * @param deviceType target deviceType 165 * @param jsonString uncompress json String 166 * @return the parseHapList result 167 * @throws BundleException Throws this exception if the json is not standard. 168 */ parseHapList(String deviceType, String jsonString)169 static List<PackInfo> parseHapList(String deviceType, String jsonString) throws BundleException { 170 List<PackInfo> packInfos = new ArrayList<PackInfo>(); 171 JSONObject jsonObject = JSONObject.parseObject(jsonString); 172 if (jsonObject == null || !jsonObject.containsKey("packages")) { 173 LOG.error("Uncompress::parseHapList exception: packages is null."); 174 throw new BundleException("Parse hap list failed, packages is null."); 175 } 176 JSONArray jsonList = JSONArray.parseArray(getJsonString(jsonObject, "packages")); 177 if (jsonList == null) { 178 LOG.error("Uncompress::parseHapList exception: packages is null."); 179 throw new BundleException("Parse hap list failed, packages is null."); 180 } 181 int jsonListSize = jsonList.size(); 182 for (int i = 0; i < jsonListSize; i++) { 183 JSONObject tmpObj = jsonList.getJSONObject(i); 184 String deviceTypes = getJsonString(tmpObj, DEVICE_TYPE); 185 if (deviceTypes == null || EMPTY.equals(deviceTypes)) { 186 deviceTypes = getJsonString(tmpObj, DEVICE_TYPE_NEW); 187 } 188 if (deviceTypes != null && deviceTypes.toLowerCase(Locale.ENGLISH).contains( 189 deviceType.toLowerCase(Locale.ENGLISH))) { 190 PackInfo packInfo = new PackInfo(); 191 packInfo.name = getJsonString(tmpObj, NAME); 192 packInfo.moduleType = getJsonString(tmpObj, MODULE_TYPE); 193 if (packInfo.moduleType == null || EMPTY.equals(packInfo.moduleType)) { 194 packInfo.moduleType = getJsonString(tmpObj, MODULE_TYPE_NEW); 195 } 196 packInfo.deviceType = JSONArray.parseArray(deviceTypes, String.class); 197 String deliveryWithInstall = getJsonString(tmpObj, INSTALL_FLAG); 198 if (deliveryWithInstall == null || EMPTY.equals(deliveryWithInstall)) { 199 deliveryWithInstall = getJsonString(tmpObj, INSTALL_FLAG_NEW); 200 } 201 packInfo.deliveryWithInstall = Boolean.parseBoolean(deliveryWithInstall); 202 packInfos.add(packInfo); 203 } 204 } 205 return packInfos; 206 } 207 parsePackInfos(String jsonString)208 static List<PackInfo> parsePackInfos(String jsonString) throws BundleException { 209 JSONObject jsonObject = JSONObject.parseObject(jsonString); 210 if (jsonObject == null || !jsonObject.containsKey("packages")) { 211 LOG.error("JsonUtil::parsePackInfos exception: packages is null."); 212 throw new BundleException("Parse hap list failed, packages is null."); 213 } 214 String packages = getJsonString(jsonObject, PACKAGES); 215 return JSONArray.parseArray(packages, PackInfo.class); 216 } 217 parseShellVersionInfoToAppInfo(String packInfoJsonStr, AppInfo appInfo)218 private static boolean parseShellVersionInfoToAppInfo(String packInfoJsonStr, AppInfo appInfo) 219 throws BundleException { 220 LOG.info("Uncompress::parseShellVersionInfoToAppInfo: begin."); 221 if (!appInfo.isMultiFrameworkBundle()) { 222 LOG.info("Uncompress::parseShellVersionInfoToAppInfo: is not a multi framewok bundle."); 223 return false; 224 } 225 try { 226 JSONObject jsonObject = JSONObject.parseObject(packInfoJsonStr); 227 if (jsonObject == null) { 228 LOG.error("Uncompress::parseShellVersionInfoToAppInfo error: summary is null."); 229 return false; 230 } 231 JSONObject summaryJson = jsonObject.getJSONObject(SUMMARY); 232 if (summaryJson == null) { 233 LOG.error("Uncompress::parseShellVersionInfoToAppInfo error: summary is null."); 234 return false; 235 } 236 JSONObject appJson = summaryJson.getJSONObject(APP); 237 if (appJson == null) { 238 LOG.error("Uncompress::parseShellVersionInfoToAppInfo error: app is null."); 239 return false; 240 } 241 JSONObject versionJson = appJson.getJSONObject(VERSION); 242 if (versionJson == null) { 243 LOG.error("Uncompress::parseShellVersionInfoToAppInfo error: version is null."); 244 return false; 245 } 246 if (!versionJson.containsKey(LEGACY_VERSION_CODE) || !versionJson.containsKey(LEGACY_VERSION_NAME)) { 247 LOG.error("Uncompress::parseShellVersionInfoToAppInfo no legacy version info."); 248 return false; 249 } 250 appInfo.setShellVersionCode(versionJson.getString(LEGACY_VERSION_CODE)); 251 appInfo.setShellVersionName(versionJson.getString(LEGACY_VERSION_NAME)); 252 return true; 253 } catch (JSONException msg) { 254 LOG.error("parseShellVersionInfoToAppInfo exception."); 255 throw new BundleException("parseShellVersionInfoToAppInfo exception."); 256 } 257 } 258 parseDeviceTypeToHapInfo(String packInfoJsonStr, HapInfo hapInfo, String hapName)259 private static void parseDeviceTypeToHapInfo(String packInfoJsonStr, HapInfo hapInfo, String hapName) { 260 LOG.info("Uncompress::parseDeviceTypeToHapInfo: begin."); 261 JSONObject jsonObject = JSONObject.parseObject(packInfoJsonStr); 262 if (jsonObject == null || !jsonObject.containsKey("packages")) { 263 LOG.error("Uncompress::parseDeviceTypeToHapInfo error: no packages."); 264 return; 265 } 266 JSONArray jsonList = JSONArray.parseArray(getJsonString(jsonObject, "packages")); 267 if (jsonList == null) { 268 LOG.error("Uncompress::parseDeviceTypeToHapInfo error: packages is null."); 269 return; 270 } 271 272 for (int i = 0; i < jsonList.size(); i++) { 273 JSONObject tmpObj = jsonList.getJSONObject(i); 274 if (tmpObj == null) { 275 LOG.error("Uncompress::parseDeviceTypeToHapInfo error: obj is null."); 276 continue; 277 } 278 String name = getJsonString(tmpObj, NAME); 279 if (name != null && name.equals(hapName)) { 280 String deviceTypes = getJsonString(tmpObj, DEVICE_TYPE_NEW); 281 if (deviceTypes == null || deviceTypes.isEmpty()) { 282 deviceTypes = getJsonString(tmpObj, DEVICE_TYPE); 283 } 284 if (deviceTypes != null && !deviceTypes.isEmpty()) { 285 hapInfo.deviceType = JSONArray.parseArray(deviceTypes 286 .replace(UncompressEntrance.DEVICE_TYPE_DEFAULT, UncompressEntrance.DEVICE_TYPE_PHONE), 287 String.class); 288 } 289 break; 290 } 291 } 292 } 293 294 /** 295 * parse hap profile info 296 * 297 * @param harmonyProfileJsonString uncompress json String 298 * @param data resource index data 299 * @param paclInfoJsonString pack.info json String 300 * @param hapName hap file name 301 * @return the parseProfileInfo result 302 * @throws BundleException Throws this exception if the json is not standard. 303 */ parseProfileInfo(String harmonyProfileJsonString, byte[] data, String paclInfoJsonString, String hapName)304 static ProfileInfo parseProfileInfo(String harmonyProfileJsonString, byte[] data, String paclInfoJsonString, 305 String hapName) throws BundleException { 306 ProfileInfo profileInfo = new ProfileInfo(); 307 JSONObject jsonObject = JSONObject.parseObject(harmonyProfileJsonString); 308 if (jsonObject == null || !jsonObject.containsKey(APP) || !jsonObject.containsKey("deviceConfig") 309 || !jsonObject.containsKey("module")) { 310 LOG.error("Uncompress::parseProfileInfo exception: app, deviceConfig or module is null."); 311 throw new BundleException("Parse profile info failed, app, deviceConfig or module is null."); 312 } 313 if (jsonObject.containsKey(APP)) { 314 JSONObject appJson = jsonObject.getJSONObject(APP); 315 profileInfo.appInfo = parseAppInfo(appJson, data); 316 } 317 if (jsonObject.containsKey("module")) { 318 JSONObject hapJson = jsonObject.getJSONObject("module"); 319 profileInfo.hapInfo = parseHapInfo(hapJson, data); 320 } 321 parseDeviceTypeToHapInfo(paclInfoJsonString, profileInfo.hapInfo, hapName); 322 if (jsonObject.containsKey("deviceConfig")) { 323 JSONObject deviceConfigJson = jsonObject.getJSONObject("deviceConfig"); 324 profileInfo.deviceConfig = parseDeviceConfigInfo(deviceConfigJson, profileInfo.hapInfo.deviceType); 325 } 326 if (!parseShellVersionInfoToAppInfo(paclInfoJsonString, profileInfo.appInfo)) { 327 profileInfo.appInfo.setDefaultShellVersion(); 328 } 329 if (!profileInfo.appInfo.appName.isEmpty()) { 330 return profileInfo; 331 } 332 333 if (profileInfo.hapInfo.abilities.size() == 1) { 334 profileInfo.appInfo.appName = profileInfo.hapInfo.abilities.get(0).label; 335 profileInfo.appInfo.appNameEN = profileInfo.hapInfo.abilities.get(0).label; 336 } else { 337 for (AbilityInfo abilityInfo : profileInfo.hapInfo.abilities) { 338 boolean isLabel = false; 339 for (SkillInfo skill : abilityInfo.skills) { 340 if (skill.actions.contains("action.system.home") && skill.entities.contains("entity.system.home")) { 341 isLabel = true; 342 break; 343 } 344 } 345 if (isLabel) { 346 profileInfo.appInfo.appName = abilityInfo.label; 347 profileInfo.appInfo.appNameEN = abilityInfo.label; 348 break; 349 } 350 } 351 } 352 return profileInfo; 353 } 354 355 /** 356 * parse app info 357 * 358 * @param appJson global json Object 359 * @param data resource index data 360 * @return the parseAppInfo result 361 * @throws BundleException Throws this exception if the json is not standard. 362 */ parseAppInfo(JSONObject appJson, byte[] data)363 static AppInfo parseAppInfo(JSONObject appJson, byte[] data) throws BundleException { 364 AppInfo appInfo = new AppInfo(); 365 if (appJson == null) { 366 LOG.error("Uncompress::parseAppInfo exception: appJson is null."); 367 throw new BundleException("Parse app info failed, appJson is null."); 368 } 369 appInfo.bundleName = getJsonString(appJson, "bundleName"); 370 appInfo.setBundleType(getJsonString(appJson, BUNDLE_TYPE, APP)); 371 appInfo.vendor = getJsonString(appJson, "vendor"); 372 appInfo.relatedBundleName = getJsonString(appJson, "relatedBundleName"); 373 if (appJson.containsKey(VERSION)) { 374 JSONObject version = appJson.getJSONObject(VERSION); 375 appInfo.versionName = getJsonString(version, "name"); 376 appInfo.versionCode = getJsonString(version, "code"); 377 } 378 if (appJson.containsKey("apiVersion")) { 379 JSONObject apiVersion = appJson.getJSONObject("apiVersion"); 380 appInfo.compatibleApiVersion = apiVersion.getIntValue("compatible"); 381 appInfo.targetApiVersion = apiVersion.getIntValue("target"); 382 appInfo.releaseType = getJsonString(apiVersion, "releaseType"); 383 appInfo.setCompileSdkType(getJsonString(apiVersion, COMPILE_SDK_TYPE)); 384 appInfo.setCompileSdkVersion(getJsonString(apiVersion, COMPILE_SDK_VERSION)); 385 } 386 String labelRes = ""; 387 if (appJson.containsKey("labelId")) { 388 int labelId = appJson.getIntValue("labelId"); 389 labelRes = ResourcesParser.getBaseResourceById(labelId, data); 390 } 391 if (labelRes != null && !labelRes.isEmpty()) { 392 appInfo.appName = labelRes; 393 appInfo.appNameEN = labelRes; 394 } else if (appJson.containsKey("label")) { 395 appInfo.appName = getJsonString(appJson, "label"); 396 appInfo.appNameEN = getJsonString(appJson, "label"); 397 } 398 appInfo.setMultiFrameworkBundle(appJson.getBooleanValue(MULTI_FRAMEWORK_BUNDLE)); 399 if (appJson.containsKey(COMPILE_SDK_TYPE)) { 400 appInfo.setCompileSdkType(appJson.getString(COMPILE_SDK_TYPE)); 401 } 402 if (appJson.containsKey(COMPILE_SDK_VERSION)) { 403 appInfo.setCompileSdkVersion(appJson.getString(COMPILE_SDK_VERSION)); 404 } 405 return appInfo; 406 } 407 408 /** 409 * parse module app info 410 * 411 * @param appJson global json Object 412 * @param data resource index data 413 * @return the moduleApp result 414 * @throws BundleException Throws this exception if the json is not standard. 415 */ parseModuleAppInfo(JSONObject appJson, byte[] data)416 static ModuleAppInfo parseModuleAppInfo(JSONObject appJson, byte[] data) throws BundleException { 417 ModuleAppInfo moduleAppInfo = new ModuleAppInfo(); 418 if (appJson == null) { 419 LOG.error("Uncompress::parseModuleAppInfo exception: appJson is null."); 420 throw new BundleException("Parse module app info failed, appJson is null."); 421 } 422 423 moduleAppInfo.bundleName = getJsonString(appJson, BUNDLENAME); 424 moduleAppInfo.debug = getJsonBooleanValue(appJson, DEBUG, false); 425 moduleAppInfo.icon = parseIconById(appJson, data); 426 moduleAppInfo.label = parseResourceByKey(appJson, data, LABEL, LABEL_ID); 427 moduleAppInfo.setLabels(parseResourceMapByKey(appJson, data, LABEL_ID)); 428 moduleAppInfo.description = parseResourceByKey(appJson, data, DESCRIPTION, DESCRIPTION_ID); 429 moduleAppInfo.setDescriptions(parseResourceMapByKey(appJson, data, DESCRIPTION_ID)); 430 431 moduleAppInfo.vendor = getJsonString(appJson, VENDOR); 432 moduleAppInfo.versionCode = getJsonIntValue(appJson, VERSION_CODE, DEFAULT_VERSION_CODE); 433 moduleAppInfo.versionName = getJsonString(appJson, VERSION_NAME); 434 435 if (appJson.containsKey(MIN_COMPATIBLE_VERSION_CODE)) { 436 moduleAppInfo.minCompatibleVersionCode = 437 getJsonIntValue(appJson, MIN_COMPATIBLE_VERSION_CODE, DEFAULT_VERSION_CODE); 438 } else { 439 moduleAppInfo.minCompatibleVersionCode = getJsonIntValue(appJson, VERSION_CODE, DEFAULT_VERSION_CODE); 440 } 441 moduleAppInfo.minAPIVersion = getJsonIntValue(appJson, MIN_API_VERSION, DEFAULT_VERSION_CODE); 442 moduleAppInfo.targetAPIVersion = getJsonIntValue(appJson, TARGET_API_VERSION, DEFAULT_VERSION_CODE); 443 moduleAppInfo.apiReleaseType = getJsonString(appJson, API_RELEASE_TYPE); 444 moduleAppInfo.distributedNotificationEnabled = 445 getJsonBooleanValue(appJson, DISTRIBUTED_NOTIFICATION_ENABLED, false); 446 moduleAppInfo.entityType = getJsonString(appJson, ENTITY_TYPE, UNSPECIFIED); 447 moduleAppInfo.setBundleType(getJsonString(appJson, BUNDLE_TYPE, APP)); 448 moduleAppInfo.setCompileSdkType(getJsonString(appJson, COMPILE_SDK_TYPE, EMPTY)); 449 moduleAppInfo.setCompileSdkVersion(getJsonString(appJson, COMPILE_SDK_VERSION, EMPTY)); 450 // parse device type 451 parseSpecifiedDeviceType(appJson, moduleAppInfo); 452 return moduleAppInfo; 453 } 454 parseSpecifiedDeviceType(JSONObject appJson, ModuleAppInfo moduleAppInfo)455 static void parseSpecifiedDeviceType(JSONObject appJson, ModuleAppInfo moduleAppInfo) throws BundleException { 456 if (appJson == null) { 457 LOG.error("JsonUtil::parseSpecifiedDeviceType exception: appJson is null."); 458 throw new BundleException("Parse app info failed, appJson is null."); 459 } 460 parseDeviceType(appJson, moduleAppInfo, PHONE); 461 parseDeviceType(appJson, moduleAppInfo, TABLET); 462 parseDeviceType(appJson, moduleAppInfo, TV); 463 parseDeviceType(appJson, moduleAppInfo, WEARABLE); 464 parseDeviceType(appJson, moduleAppInfo, AR); 465 parseDeviceType(appJson, moduleAppInfo, VR); 466 parseDeviceType(appJson, moduleAppInfo, CAR); 467 parseDeviceType(appJson, moduleAppInfo, EARPHONES); 468 parseDeviceType(appJson, moduleAppInfo, SPEAKER); 469 parseDeviceType(appJson, moduleAppInfo, LINK_IOT); 470 parseDeviceType(appJson, moduleAppInfo, ROUTER); 471 parseDeviceType(appJson, moduleAppInfo, TWO_IN_ONE); 472 } 473 474 /** 475 * parse deviceType 476 * 477 * @param appJson is the json Object of app 478 * @param moduleAppInfo the app in module 479 * @param deviceName the device name in deviceType array 480 * @return the parseDeviceConfigInfo result 481 * @throws BundleException Throws this exception if the json is not standard. 482 */ parseDeviceType(JSONObject appJson, ModuleAppInfo moduleAppInfo, String deviceName)483 static ModuleDeviceType parseDeviceType(JSONObject appJson, ModuleAppInfo moduleAppInfo, 484 String deviceName) throws BundleException { 485 if (appJson == null) { 486 LOG.error("JsonUtil::parseDeviceType exception: appJson is null."); 487 throw new BundleException("Parse app info failed, appJson is null."); 488 } 489 ModuleDeviceType moduleDeviceType = new ModuleDeviceType(); 490 if (appJson.containsKey(deviceName)) { 491 JSONObject deviceObj = appJson.getJSONObject(deviceName); 492 if (deviceObj.containsKey(MIN_API_VERSION)) { 493 moduleDeviceType.minAPIVersion = deviceObj.getIntValue(MIN_API_VERSION); 494 } else { 495 moduleDeviceType.minAPIVersion = moduleAppInfo.minAPIVersion; 496 } 497 498 if (deviceObj.containsKey(DISTRIBUTED_NOTIFICATION_ENABLED)) { 499 moduleDeviceType.distributedNotificationEnabled = 500 deviceObj.getBoolean(DISTRIBUTED_NOTIFICATION_ENABLED); 501 } else { 502 moduleDeviceType.distributedNotificationEnabled = moduleAppInfo.distributedNotificationEnabled; 503 } 504 505 moduleAppInfo.deviceTypes.put(deviceName, moduleDeviceType); 506 } 507 return moduleDeviceType; 508 } 509 510 /** 511 * parse deviceConfig infos 512 * 513 * @param deviceConfigInfoJson deviceConfig json Object 514 * @param deviceTypes the deviceTypes of the hap 515 * @return the parseDeviceConfigInfo result 516 * @throws BundleException Throws this exception if the json is not standard. 517 */ parseDeviceConfigInfo(JSONObject deviceConfigInfoJson, List<String> deviceTypes)518 static Map<String, DeviceConfig> parseDeviceConfigInfo(JSONObject deviceConfigInfoJson, 519 List<String> deviceTypes) throws BundleException { 520 Map<String, DeviceConfig> deviceConfigs = new HashMap<>(); 521 if (deviceConfigInfoJson == null) { 522 return deviceConfigs; 523 } 524 525 DeviceConfig defaultConfig = new DeviceConfig(); 526 if (deviceConfigInfoJson.containsKey("default")) { 527 defaultConfig = parseDeviceConfig(deviceConfigInfoJson.getJSONObject("default"), 528 new DeviceConfig()); 529 } 530 531 for (String deviceType : deviceTypes) { 532 getTargetDeviceConfig(deviceConfigs, deviceConfigInfoJson, defaultConfig, deviceType); 533 } 534 535 if (deviceConfigs.isEmpty()) { 536 deviceConfigs.put(UncompressEntrance.DEVICE_TYPE_PHONE, defaultConfig); 537 } 538 539 return deviceConfigs; 540 } 541 542 /** 543 * parse deviceConfig infos 544 * 545 * @param deviceConfigs the parseDeviceConfigInfo result 546 * @param deviceConfigInfoJson deviceConfig json Object 547 * @param defaultConfig the default deviceConfig 548 * @param targetDeviceType the target deviceType 549 * @throws BundleException Throws this exception if the json is not standard. 550 */ getTargetDeviceConfig(Map<String, DeviceConfig> deviceConfigs, JSONObject deviceConfigInfoJson, DeviceConfig defaultConfig, String targetDeviceType)551 static void getTargetDeviceConfig(Map<String, DeviceConfig> deviceConfigs, JSONObject deviceConfigInfoJson, 552 DeviceConfig defaultConfig, String targetDeviceType) throws BundleException { 553 if (deviceConfigInfoJson.containsKey(targetDeviceType)) { 554 DeviceConfig deviceConfig = parseDeviceConfig( 555 deviceConfigInfoJson.getJSONObject(targetDeviceType), defaultConfig); 556 deviceConfigs.put(targetDeviceType, deviceConfig); 557 } else { 558 deviceConfigs.put(targetDeviceType, defaultConfig); 559 } 560 } 561 562 /** 563 * parse deviceConfig info 564 * 565 * @param deviceConfigJson deviceConfig json Object 566 * @param defaultConfig default deviceConfig 567 * @return the parseDeviceConfigInfo result 568 * @throws BundleException Throws this exception if the json is not standard. 569 */ parseDeviceConfig(JSONObject deviceConfigJson, DeviceConfig defaultConfig)570 static DeviceConfig parseDeviceConfig(JSONObject deviceConfigJson, DeviceConfig defaultConfig) 571 throws BundleException { 572 DeviceConfig deviceConfig = new DeviceConfig(); 573 if (deviceConfigJson == null || defaultConfig == null) { 574 LOG.error("Uncompress::parseDeviceConfigInfo exception: deviceConfigJson or defaultConfig is null."); 575 throw new BundleException("Parse device config info failed, deviceConfigJson or defaultConfig is null."); 576 } 577 578 deviceConfig.jointUserid = getJsonString(deviceConfigJson, "jointUserid", defaultConfig.jointUserid); 579 deviceConfig.process = getJsonString(deviceConfigJson, "process", defaultConfig.process); 580 581 if (deviceConfigJson.containsKey("reqSdk")) { 582 JSONObject reqSdk = deviceConfigJson.getJSONObject("reqSdk"); 583 deviceConfig.compatibleReqSdk = getJsonString(reqSdk, "compatible", defaultConfig.compatibleReqSdk); 584 deviceConfig.targetReqSdk = getJsonString(reqSdk, "target", defaultConfig.targetReqSdk); 585 } 586 587 if (deviceConfigJson.containsKey("ark")) { 588 JSONObject ark = deviceConfigJson.getJSONObject("ark"); 589 deviceConfig.arkFlag = getJsonString(ark, "flag", defaultConfig.arkFlag); 590 if (ark.containsKey("reqVersion")) { 591 JSONObject arkVersion = ark.getJSONObject("reqVersion"); 592 deviceConfig.targetArkVersion = getJsonString(arkVersion, "target", defaultConfig.targetArkVersion); 593 deviceConfig.compatibleArkVersion = getJsonString(arkVersion, "compatible", 594 defaultConfig.compatibleArkVersion); 595 } 596 } 597 598 if (deviceConfigJson.containsKey("directLaunch")) { 599 deviceConfig.directLaunch = deviceConfigJson.getBoolean("directLaunch"); 600 } else { 601 deviceConfig.directLaunch = defaultConfig.directLaunch; 602 } 603 604 return deviceConfig; 605 } 606 607 /** 608 * parse hap info 609 * 610 * @param hapJson hap json Object 611 * @param data resource index data 612 * @return the parseHapInfo result 613 * @throws BundleException Throws this exception if the json is not standard. 614 */ parseHapInfo(JSONObject hapJson, byte[] data)615 static HapInfo parseHapInfo(JSONObject hapJson, byte[] data) throws BundleException { 616 HapInfo hapInfo = new HapInfo(); 617 if (hapJson == null) { 618 LOG.error("Uncompress::parseHapInfo exception: hapJson is null."); 619 throw new BundleException("Parse hap info failed, hapJson is null."); 620 } 621 hapInfo.packageStr = getJsonString(hapJson, "package"); 622 hapInfo.name = getJsonString(hapJson, "name"); 623 hapInfo.description = parseResourceByKey(hapJson, data, "description", "descriptionId"); 624 hapInfo.setDescriptions(parseResourceMapByKey(hapJson, data, DESCRIPTION_ID)); 625 626 if (hapJson.containsKey(MAIN_ABILITY)) { 627 hapInfo.mainElement = getJsonString(hapJson, MAIN_ABILITY); 628 } 629 630 if (hapJson.containsKey("supportedModes")) { 631 hapInfo.supportedModes = JSONObject.parseArray(getJsonString(hapJson, "supportedModes"), 632 String.class); 633 } 634 635 if (hapJson.containsKey("defPermissions")) { 636 hapInfo.defPermissions = parseDefPermissions(hapJson, data); 637 } 638 639 if (hapJson.containsKey("definePermissions")) { 640 hapInfo.definePermissions = parseDefinePermissions(hapJson, data); 641 } 642 643 if (hapJson.containsKey("defPermissiongroups")) { 644 hapInfo.defPermissionsGroups = JSONArray.parseArray(getJsonString(hapJson, "defPermissiongroups"), 645 DefPermissionGroup.class); 646 } 647 648 if (hapJson.containsKey("distro")) { 649 getDistro(hapJson, hapInfo); 650 } 651 652 if (hapJson.containsKey("reqCapabilities")) { 653 hapInfo.reqCapabilities = JSONArray.parseArray(getJsonString(hapJson, "reqCapabilities"), 654 String.class); 655 } 656 657 if (hapJson.containsKey("deviceType")) { 658 hapInfo.deviceType = JSONArray.parseArray(getJsonString(hapJson, "deviceType") 659 .replace(UncompressEntrance.DEVICE_TYPE_DEFAULT, UncompressEntrance.DEVICE_TYPE_PHONE), 660 String.class); 661 } 662 663 if (hapJson.containsKey("metaData")) { 664 JSONObject metaDataJson = hapJson.getJSONObject("metaData"); 665 hapInfo.metaData = parseMetaData(metaDataJson); 666 } 667 668 if (hapJson.containsKey("js")) { 669 List<JsInfo> jsInfos = JSONObject.parseArray(getJsonString(hapJson, "js"), JsInfo.class); 670 if (jsInfos != null && jsInfos.size() > 0) { 671 hapInfo.isJs = true; 672 } 673 } 674 675 if (hapJson.containsKey("reqPermissions")) { 676 hapInfo.reqPermissions = parseFAReqPermission(hapJson, data); 677 } 678 679 if (hapJson.containsKey("commonEvents")) { 680 hapInfo.commonEvents = JSONObject.parseArray(getJsonString(hapJson, "commonEvents"), 681 CommonEvent.class); 682 } 683 684 if (hapJson.containsKey("shortcuts")) { 685 hapInfo.shortcuts = parseShoruCuts(hapJson, data); 686 } 687 688 if (hapJson.containsKey("abilities")) { 689 JSONArray abilities = hapJson.getJSONArray("abilities"); 690 List<AbilityInfo> abilitieList = new ArrayList<AbilityInfo>(); 691 int abilitiesSize = abilities.size(); 692 for (int i = 0; i < abilitiesSize; i++) { 693 JSONObject tmpObj = abilities.getJSONObject(i); 694 abilitieList.add(parseAbility(tmpObj, data)); 695 } 696 hapInfo.abilities = abilitieList; 697 adaptAbilityName(hapInfo.abilities, hapInfo.packageStr); 698 setFAProviderAbility(hapJson, hapInfo, hapInfo.abilities); 699 } 700 701 if (hapJson.containsKey("distroFilter")) { 702 hapInfo.distroFilter = JSONObject.parseObject( 703 getJsonString(hapJson, "distroFilter"), DistroFilter.class); 704 } 705 return hapInfo; 706 } 707 parseFAReqPermission(JSONObject hapJson, byte[] data)708 static List<ReqPermission> parseFAReqPermission(JSONObject hapJson, byte[] data) throws BundleException { 709 List<ReqPermission> reqPermissions = new ArrayList<>(); 710 if (!hapJson.containsKey(REQ_PERMISSIONS)) { 711 return reqPermissions; 712 } 713 JSONArray reqPermissionObjs = hapJson.getJSONArray(REQ_PERMISSIONS); 714 for (int i = 0; i < reqPermissionObjs.size(); ++i) { 715 ReqPermission reqPermission = new ReqPermission(); 716 JSONObject requestPermission = reqPermissionObjs.getJSONObject(i); 717 reqPermission.name = getJsonString(requestPermission, NAME); 718 reqPermission.reason = parseResourceByKey(requestPermission, data, REASON, REASON_ID); 719 if (requestPermission.containsKey(REASON_ID)) { 720 reqPermission.setReasons(parseResourceMapByKey(requestPermission, data, REASON_ID)); 721 } 722 if (requestPermission.containsKey(USED_SCENE)) { 723 reqPermission.usedScene = parseModuleUsedScene(requestPermission.getJSONObject(USED_SCENE)); 724 } 725 reqPermissions.add(reqPermission); 726 } 727 return reqPermissions; 728 } 729 730 /** 731 * get distro 732 * 733 * @param hapJson hapJson json 734 * @param hapInfo hapInfo json object 735 */ getDistro(JSONObject hapJson, HapInfo hapInfo)736 private static void getDistro(JSONObject hapJson, HapInfo hapInfo) { 737 JSONObject distroObj = hapJson.getJSONObject("distro"); 738 Distro distro = new Distro(); 739 distro.moduleName = getJsonString(distroObj, "moduleName"); 740 distro.moduleType = getJsonString(distroObj, "moduleType"); 741 distro.deliveryWithInstall = distroObj.getBoolean("deliveryWithInstall"); 742 if (distroObj.containsKey("installationFree")) { 743 boolean isFreeInstall = distroObj.getBoolean("installationFree"); 744 if (isFreeInstall) { 745 distro.installationFree = 1; 746 }else { 747 distro.installationFree = 0; 748 } 749 } else { 750 distro.installationFree = 2; 751 } 752 if (distroObj.containsKey("virtualMachine")) { 753 distro.virtualMachine = getJsonString(distroObj, "virtualMachine"); 754 } else { 755 distro.virtualMachine = "default"; 756 } 757 hapInfo.distro = distro; 758 } 759 760 /** 761 * parse meta data 762 * 763 * @param metaDataJson meta data json 764 * @return the parseMetaData result 765 */ parseMetaData(JSONObject metaDataJson)766 private static MetaData parseMetaData(JSONObject metaDataJson) { 767 MetaData metaData = new MetaData(); 768 if (metaDataJson == null) { 769 LOG.error("Uncompress::parseMetaData : metaDataJson is null"); 770 return metaData; 771 } 772 773 if (metaDataJson.containsKey("parameters")) { 774 metaData.parameters = JSONObject.parseArray(getJsonString(metaDataJson, "parameters"), 775 MetaDataInfo.class); 776 } 777 if (metaDataJson.containsKey("results")) { 778 metaData.results = JSONObject.parseArray(getJsonString(metaDataJson, "results"), 779 MetaDataInfo.class); 780 } 781 if (metaDataJson.containsKey("customizeData")) { 782 metaData.customizeDatas = JSONObject.parseArray(getJsonString(metaDataJson, "customizeData"), 783 CustomizeData.class); 784 } 785 return metaData; 786 } 787 788 /** 789 * parse ability object. 790 * 791 * @param abilityJson ability json object 792 * @param data resource index data 793 * @return the parseAbility result 794 * @throws BundleException Throws this exception if the json is not standard. 795 */ parseAbility(JSONObject abilityJson, byte[] data)796 static AbilityInfo parseAbility(JSONObject abilityJson, byte[] data) throws BundleException { 797 if (abilityJson == null) { 798 LOG.error("Uncompress::parseAbility exception: abilityJson is null."); 799 throw new BundleException("Parse ability failed, abilityJson is null."); 800 } 801 AbilityInfo ability = new AbilityInfo(); 802 ability.name = getJsonString(abilityJson, "name"); 803 if (abilityJson.containsKey("iconId")) { 804 int iconId = abilityJson.getIntValue("iconId"); 805 String iconPath = ResourcesParser.getResourceById(iconId, data); 806 if (iconPath != null && !iconPath.isEmpty()) { 807 ability.iconPath = ASSETS_DIR_NAME + iconPath; 808 } 809 } 810 ability.icon = ability.iconPath != null && !ability.iconPath.isEmpty() ? ability.iconPath : 811 getJsonString(abilityJson, "icon"); 812 813 if (abilityJson.containsKey("descriptionId")) { 814 int descriptionId = abilityJson.getIntValue("descriptionId"); 815 ability.descriptionRes = ResourcesParser.getBaseResourceById(descriptionId, data); 816 } 817 ability.description = ability.descriptionRes != null && !ability.descriptionRes.isEmpty() ? 818 ability.descriptionRes : getJsonString(abilityJson, "description"); 819 ability.setDescriptions(parseResourceMapByKey(abilityJson, data, DESCRIPTION_ID)); 820 821 if (abilityJson.containsKey("labelId")) { 822 int labelId = abilityJson.getIntValue("labelId"); 823 ability.labelRes = ResourcesParser.getBaseResourceById(labelId, data); 824 } 825 if (ability.labelRes != null && !ability.labelRes.isEmpty()) { 826 ability.label = ability.labelRes; 827 } else if (!getJsonString(abilityJson, "label").isEmpty()) { 828 ability.label = getJsonString(abilityJson, "label"); 829 } else { 830 ability.label = ability.name; 831 } 832 ability.setLabels(parseResourceMapByKey(abilityJson, data, LABEL_ID)); 833 834 ability.type = getJsonString(abilityJson, "type"); 835 ability.launchType = getJsonString(abilityJson, "launchType"); 836 ability.orientation = getJsonString(abilityJson, "orientation"); 837 ability.uri = getJsonString(abilityJson, "uri"); 838 if (abilityJson.containsKey("formsEnabled")) { 839 ability.formsEnabled = abilityJson.getBoolean("formsEnabled"); 840 } 841 842 if (abilityJson.containsKey("metaData")) { 843 JSONObject metaDataJson = abilityJson.getJSONObject("metaData"); 844 ability.metaData = parseMetaData(metaDataJson); 845 } 846 847 if (abilityJson.containsKey("skills")) { 848 ability.skills = JSONObject.parseArray(getJsonString(abilityJson, "skills"), SkillInfo.class); 849 } 850 851 if (abilityJson.containsKey("backgroundModes")) { 852 ability.backgroundModes = JSONObject.parseArray(getJsonString(abilityJson, "backgroundModes"), 853 String.class); 854 } 855 856 if (abilityJson.containsKey("visible")) { 857 ability.visible = abilityJson.getBoolean("visible"); 858 } 859 860 if (abilityJson.containsKey("configChanges")) { 861 ability.configChanges = JSONObject.parseArray( 862 getJsonString(abilityJson, "configChanges"), String.class); 863 } 864 865 if (abilityJson.containsKey("srcLanguage")) { 866 ability.srcLanguage = getJsonString(abilityJson, "srcLanguage"); 867 } 868 869 if (abilityJson.containsKey("srcPath")) { 870 ability.srcPath = getJsonString(abilityJson, "srcPath"); 871 } 872 873 parseAbilityPermissions(abilityJson, ability); 874 875 if (abilityJson.containsKey("forms")) { 876 JSONArray forms = abilityJson.getJSONArray("forms"); 877 List<AbilityFormInfo> formList = new ArrayList<AbilityFormInfo>(); 878 int formSize = forms.size(); 879 for (int i = 0; i < formSize; i++) { 880 JSONObject tmpObj = forms.getJSONObject(i); 881 formList.add(parseAbilityFormInfo(tmpObj, data)); 882 } 883 ability.formInfos = formList; 884 } 885 886 return ability; 887 } 888 adaptAbilityName(List<AbilityInfo> abilityInfos, String packageName)889 private static void adaptAbilityName(List<AbilityInfo> abilityInfos, String packageName) { 890 if (abilityInfos == null) { 891 return; 892 } 893 for (AbilityInfo abilityInfo : abilityInfos) { 894 if (abilityInfo.name.startsWith(DOT)) { 895 abilityInfo.name = packageName + abilityInfo.name; 896 } 897 } 898 } 899 900 /** 901 * parse ability object. 902 * 903 * @param abilityJson ability json object 904 * @param ability the parseAbility result 905 * @return the parseAbility Permissions result 906 * @throws BundleException Throws this exception if the json is not standard. 907 */ parseAbilityPermissions(JSONObject abilityJson, AbilityInfo ability)908 static void parseAbilityPermissions(JSONObject abilityJson, AbilityInfo ability) throws BundleException { 909 if (abilityJson == null || ability == null) { 910 LOG.error("Uncompress::parseAbilityPermissions exception: abilityJson or ability is null."); 911 throw new BundleException("Parse ability permissions failed, abilityJson or ability is null."); 912 } 913 if (abilityJson.containsKey("permissions")) { 914 ability.permissions = JSONObject.parseArray(getJsonString(abilityJson, "permissions"), String.class); 915 } 916 917 if (abilityJson.containsKey("grantPermission")) { 918 ability.grantPermission = abilityJson.getBoolean("grantPermission"); 919 } 920 ability.readPermission = getJsonString(abilityJson, "readPermission"); 921 ability.writePermission = getJsonString(abilityJson, "writePermission"); 922 923 if (abilityJson.containsKey("uriPermission")) { 924 JSONObject uriPermission = abilityJson.getJSONObject("uriPermission"); 925 ability.uriPermissionMode = getJsonString(uriPermission, "mode"); 926 ability.uriPermissionPath = getJsonString(uriPermission, "path"); 927 } 928 929 ability.mission = getJsonString(abilityJson, "mission"); 930 ability.targetAbility = getJsonString(abilityJson, "targetAbility"); 931 932 if (abilityJson.containsKey("directLaunch")) { 933 ability.directLaunch = abilityJson.getBoolean("directLaunch"); 934 } 935 936 if (abilityJson.containsKey("multiUserShared")) { 937 ability.multiUserShared = abilityJson.getBoolean("multiUserShared"); 938 } 939 940 if (abilityJson.containsKey("supportPipMode")) { 941 ability.supportPipMode = abilityJson.getBoolean("supportPipMode"); 942 } 943 944 if (abilityJson.containsKey("form")) { 945 JSONObject form = abilityJson.getJSONObject("form"); 946 FormInfo formInfo = new FormInfo(); 947 if (form.containsKey("formEntity")) { 948 formInfo.formEntity = JSONObject.parseArray(getJsonString(form, "formEntity"), String.class); 949 } 950 formInfo.minWidth = getJsonString(form, "minWidth"); 951 formInfo.defaultWidth = getJsonString(form, "defaultWidth"); 952 formInfo.minHeight = getJsonString(form, "minHeight"); 953 formInfo.defaultHeight = getJsonString(form, "defaultHeight"); 954 ability.formInfo = formInfo; 955 } 956 } 957 /** 958 * parse ability forms object 959 * 960 * @param abilityFormJson ability form json object 961 * @return the ability form info result 962 */ parseAbilityFormInfo(JSONObject abilityFormJson, byte[] data)963 static AbilityFormInfo parseAbilityFormInfo(JSONObject abilityFormJson, byte[] data) throws BundleException { 964 AbilityFormInfo abilityForm = new AbilityFormInfo(); 965 if (abilityFormJson == null) { 966 LOG.error("Uncompress::parseAbilityFormInfo : abilityFormJson is null"); 967 return abilityForm; 968 } 969 abilityForm.name = getJsonString(abilityFormJson, "name"); 970 abilityForm.type = getJsonString(abilityFormJson, "type"); 971 abilityForm.description = parseResourceByKey(abilityFormJson, data, "description", "descriptionId"); 972 abilityForm.setDescriptions(parseResourceMapByKey(abilityFormJson, data, DESCRIPTION_ID)); 973 if (abilityFormJson.containsKey("isDefault")) { 974 abilityForm.isDefault = abilityFormJson.getBoolean("isDefault"); 975 } 976 if (abilityFormJson.containsKey("colorMode")) { 977 abilityForm.colorMode = getJsonString(abilityFormJson, "colorMode"); 978 } 979 if (abilityFormJson.containsKey("formConfigAbility")) { 980 abilityForm.formConfigAbility = getJsonString(abilityFormJson, "formConfigAbility"); 981 } 982 if (abilityFormJson.containsKey("updateEnabled")) { 983 abilityForm.updateEnabled = abilityFormJson.getBoolean("updateEnabled"); 984 } 985 abilityForm.scheduledUpdateTime = getJsonString(abilityFormJson, "scheduledUpdateTime"); 986 if (abilityFormJson.containsKey("updateDuration")) { 987 abilityForm.updateDuration = abilityFormJson.getIntValue("updateDuration"); 988 } 989 if (abilityFormJson.containsKey("supportDimensions")) { 990 abilityForm.supportDimensions = JSONObject.parseArray(getJsonString(abilityFormJson, "supportDimensions"), 991 String.class); 992 } 993 abilityForm.defaultDimension = getJsonString(abilityFormJson, "defaultDimension"); 994 if (abilityFormJson.containsKey("metaData")) { 995 JSONObject metaDataJson = abilityFormJson.getJSONObject("metaData"); 996 abilityForm.metaData = parseMetaData(metaDataJson); 997 } 998 return abilityForm; 999 } 1000 1001 /** 1002 * parse module hap info 1003 * 1004 * @param harmonyProfileJsonString uncompress json String 1005 * @param data resource index data 1006 * @param packInfoJsonString pack.info json String 1007 * @param hapName hap file name 1008 * @return the parseProfileInfo result 1009 * @throws BundleException Throws this exception if the json is not standard. 1010 */ parseModuleProfileInfo(String harmonyProfileJsonString, byte[] data, String packInfoJsonString, String hapName, HashMap<String, String> profileJsons)1011 static ModuleProfileInfo parseModuleProfileInfo(String harmonyProfileJsonString, byte[] data, String packInfoJsonString, 1012 String hapName, HashMap<String, String> profileJsons) throws BundleException { 1013 ModuleProfileInfo moduleProfileInfo = new ModuleProfileInfo(); 1014 JSONObject jsonObject = JSONObject.parseObject(harmonyProfileJsonString); 1015 if (jsonObject == null || !jsonObject.containsKey(APP) || !jsonObject.containsKey(MODULE)) { 1016 LOG.error("JsonUtil::parseModuleProfileInfo exception: app or module is null."); 1017 throw new BundleException("Parse module profile info failed, app or module is null."); 1018 } 1019 1020 JSONObject appJson = jsonObject.getJSONObject(APP); 1021 moduleProfileInfo.moduleAppInfo = parseModuleAppInfo(appJson, data); 1022 JSONObject moduleJson = jsonObject.getJSONObject(MODULE); 1023 moduleProfileInfo.moduleInfo = parseModuleHapInfo(moduleJson, data, 1024 moduleProfileInfo.moduleAppInfo.bundleName, profileJsons); 1025 moduleProfileInfo.moduleInfo.appModel = AppModel.STAGE; 1026 moduleProfileInfo.moduleInfo.dependenies.addAll(parseDenpendencies(appJson, moduleJson)); 1027 1028 // parse appName 1029 for (ModuleAbilityInfo abilityInfo : moduleProfileInfo.moduleInfo.abilities) { 1030 for (SkillInfo skill : abilityInfo.skills) { 1031 if (skill.actions.contains(ACTION_SYSTEM_HOME) && skill.entities.contains(ENTITY_SYSTEM_HOME)) { 1032 moduleProfileInfo.moduleAppInfo.appName = abilityInfo.label; 1033 moduleProfileInfo.moduleAppInfo.appNameEN = abilityInfo.label; 1034 break; 1035 } 1036 } 1037 } 1038 1039 return moduleProfileInfo; 1040 } 1041 1042 /** 1043 * parse module hap info 1044 * 1045 * @param moduleJson Json hap json Object 1046 * @param data resource index data 1047 * @param hapName is the name of the hap 1048 * @param profileJsons is the map of profile 1049 * @return the ModuleProfileInfo result 1050 * @throws BundleException Throws this exception if the json is not standard. 1051 */ parseModuleHapInfo(JSONObject moduleJson, byte[] data, String hapName, HashMap<String, String> profileJsons)1052 static ModuleInfo parseModuleHapInfo(JSONObject moduleJson, byte[] data, String hapName, 1053 HashMap<String, String> profileJsons) throws BundleException { 1054 ModuleInfo moduleInfo = new ModuleInfo(); 1055 if (moduleJson == null) { 1056 LOG.error("JsonUtil::parseModuleHapInfo exception: moduleJson is null."); 1057 throw new BundleException("Parse module hap info failed, moduleJson is null."); 1058 } 1059 1060 moduleInfo.name = getJsonString(moduleJson, NAME); 1061 moduleInfo.type = getJsonString(moduleJson, TYPE); 1062 if (moduleJson.containsKey(SRC_ENTRY)) { 1063 moduleInfo.srcEntrance = getJsonString(moduleJson, SRC_ENTRY); 1064 } else { 1065 moduleInfo.srcEntrance = getJsonString(moduleJson, SRC_ENTRANCE); 1066 } 1067 moduleInfo.description = parseResourceByKey(moduleJson, data, DESCRIPTION, DESCRIPTION_ID); 1068 moduleInfo.setDescriptions(parseResourceMapByKey(moduleJson, data, DESCRIPTION_ID)); 1069 if (moduleJson.containsKey(PROCESS)) { 1070 moduleInfo.process = getJsonString(moduleJson, PROCESS); 1071 } else { 1072 moduleInfo.process = hapName; 1073 } 1074 moduleInfo.mainElement = getJsonString(moduleJson, MAIN_ELEMENT); 1075 if (moduleJson.containsKey(DEVICE_TYPES)) { 1076 moduleInfo.deviceTypes = JSONObject.parseArray(getJsonString(moduleJson, DEVICE_TYPES), String.class); 1077 } 1078 moduleInfo.deliveryWithInstall = getJsonBooleanValue(moduleJson, DELIVERY_WITH_INSTALL, true); 1079 parseInstallationFree(moduleJson, moduleInfo); 1080 moduleInfo.virtualMachine = getJsonString(moduleJson, VIRTUAL_MACHINE, DEFAULT); 1081 moduleInfo.uiSyntax = getJsonString(moduleJson, UI_SYNTAX, HML); 1082 moduleInfo.pages = parseModulePages(moduleJson, profileJsons); 1083 moduleInfo.moduleMetadataInfos = parseModuleMetadataInfos(moduleJson, data, profileJsons); 1084 // parse shortcuts and distroFilter 1085 if (!moduleInfo.moduleMetadataInfos.isEmpty()) { 1086 moduleInfo.moduleShortcuts = parseModuleShortcut(moduleInfo.moduleMetadataInfos, data); 1087 moduleInfo.distroFilter = parseModuleDistrofilterFromMetadata(moduleInfo.moduleMetadataInfos); 1088 } 1089 // parse abilities 1090 parseModuleAbilityAndAddShortcut(moduleJson, moduleInfo, data, profileJsons); 1091 // parse extensionAbilities 1092 parseStageExtensionAbility(moduleJson, moduleInfo, data, profileJsons); 1093 // parse request permission 1094 moduleInfo.requestPermissions = parseReqPermission(moduleJson, data); 1095 // parse define permission 1096 moduleInfo.definePermissions = parseDefinePermissions(moduleJson, data); 1097 moduleInfo.moduleAtomicService = parseModuleAtomicService(moduleJson); 1098 return moduleInfo; 1099 } 1100 parseDenpendencies(JSONObject appJson, JSONObject moduleJson)1101 private static List<DependencyItem> parseDenpendencies(JSONObject appJson, JSONObject moduleJson) 1102 throws BundleException { 1103 if (appJson == null || moduleJson == null) { 1104 LOG.error("JsonUtil::parseModuleHapInfo exception: moduleJson or appJson is null."); 1105 throw new BundleException("Parse module hap info failed, moduleJson or appJson is null."); 1106 } 1107 String bundleName = getJsonString(appJson, BUNDLENAME); 1108 List<DependencyItem> dependencyItemList = new ArrayList<>(); 1109 if (!moduleJson.containsKey(DEPENDENCIES)) { 1110 return dependencyItemList; 1111 } 1112 JSONArray dependencyObjList = moduleJson.getJSONArray(DEPENDENCIES); 1113 for (int i = 0; i < dependencyObjList.size(); ++i) { 1114 JSONObject object = dependencyObjList.getJSONObject(i); 1115 DependencyItem item = new DependencyItem(); 1116 if (object.containsKey(DEPENDENCY_BUNDLE_NAME)) { 1117 item.setBundleName(object.getString(DEPENDENCY_BUNDLE_NAME)); 1118 } else { 1119 item.setBundleName(bundleName); 1120 } 1121 if (object.containsKey(DEPENDENCY_MODULE_NAME)) { 1122 item.setModuleName(object.getString(DEPENDENCY_MODULE_NAME)); 1123 } 1124 if (object.containsKey(VERSION_CODE)) { 1125 item.setVersionCode(object.getIntValue(VERSION_CODE)); 1126 } 1127 dependencyItemList.add(item); 1128 } 1129 return dependencyItemList; 1130 } 1131 parseInstallationFree(JSONObject moduleJson, ModuleInfo moduleInfo)1132 private static void parseInstallationFree(JSONObject moduleJson, ModuleInfo moduleInfo) { 1133 if (moduleJson.containsKey(INSTALLATION_FREE)) { 1134 boolean isFreeInstall = moduleJson.getBoolean(INSTALLATION_FREE); 1135 if (isFreeInstall) { 1136 moduleInfo.installationFree = 1; 1137 } else { 1138 moduleInfo.installationFree = 0; 1139 } 1140 } else { 1141 moduleInfo.installationFree = 2; 1142 } 1143 } 1144 1145 /** 1146 * parse module pages 1147 * 1148 * @param moduleJson is json object of modulejson 1149 * @param profileJsons is the profile map 1150 * @return the pages result 1151 */ parseModulePages( JSONObject moduleJson, HashMap<String, String> profileJsons)1152 static List<String> parseModulePages( 1153 JSONObject moduleJson, HashMap<String, String> profileJsons) throws BundleException { 1154 List<String> pages = new ArrayList<>(); 1155 String pageFile = getJsonString(moduleJson, PAGES); 1156 if (pageFile.isEmpty()) { 1157 return pages; 1158 } 1159 pageFile = pageFile.replace(PROFILE, ""); 1160 pageFile += JSON_SUFFIX; 1161 String fileContent = profileJsons.get(pageFile); 1162 if (fileContent != null) { 1163 JSONObject pageObj = JSONObject.parseObject(fileContent); 1164 if (pageObj != null) { 1165 pages = JSONObject.parseArray(getJsonString(pageObj, SRC), String.class); 1166 } 1167 } 1168 return pages; 1169 } 1170 1171 /** 1172 * parse module hap info 1173 * 1174 * @param moduleMetadataInfos metedata in moduleInfo 1175 * @return the parse result 1176 */ parseModuleDistrofilterFromMetadata(List<ModuleMetadataInfo> moduleMetadataInfos)1177 static DistroFilter parseModuleDistrofilterFromMetadata(List<ModuleMetadataInfo> moduleMetadataInfos) { 1178 for (ModuleMetadataInfo moduleMetadataInfo : moduleMetadataInfos) { 1179 String resource = moduleMetadataInfo.resource; 1180 if (resource.isEmpty()) { 1181 continue; 1182 } 1183 JSONObject distroFilter = JSONObject.parseObject(resource); 1184 if (distroFilter.containsKey(DISTRIBUTION_FILTER)) { 1185 return JSONObject.parseObject(getJsonString(distroFilter, DISTRIBUTION_FILTER), DistroFilter.class); 1186 } 1187 if (distroFilter.containsKey(DISTRO_FILTER)) { 1188 return JSONObject.parseObject(getJsonString(distroFilter, DISTRO_FILTER), DistroFilter.class); 1189 } 1190 } 1191 return null; 1192 } 1193 1194 /** 1195 * parse module hap info 1196 * 1197 * @param moduleJson is the json object of module object. 1198 * @param data is the resource of hap 1199 * @param profileJsons is the map of profile file name and file content 1200 * @return the parsed extensionAbilityInfos 1201 * @throws BundleException Throws this exception if the json is not standard. 1202 */ parseModuleExtensionAbilities( JSONObject moduleJson, byte[] data, HashMap<String, String> profileJsons)1203 static List<ExtensionAbilityInfo> parseModuleExtensionAbilities( 1204 JSONObject moduleJson, byte[] data, HashMap<String, String> profileJsons) throws BundleException { 1205 List<ExtensionAbilityInfo> extensionAbilityInfos = new ArrayList<>(); 1206 JSONArray extensionAbilities = moduleJson.getJSONArray(EXTENSION_ABILITIES); 1207 int size = extensionAbilities.size(); 1208 for (int i = 0; i < size; ++i) { 1209 JSONObject tmpObj = extensionAbilities.getJSONObject(i); 1210 extensionAbilityInfos.add(parseModuleExtensionAbility(tmpObj, data, profileJsons)); 1211 } 1212 return extensionAbilityInfos; 1213 } 1214 parseStageExtensionAbility(JSONObject moduleJson, ModuleInfo moduleInfo, byte[] data, HashMap<String, String> profileJsons)1215 private static void parseStageExtensionAbility(JSONObject moduleJson, ModuleInfo moduleInfo, byte[] data, 1216 HashMap<String, String> profileJsons) throws BundleException { 1217 if (!moduleJson.containsKey(EXTENSION_ABILITIES)) { 1218 return; 1219 } 1220 // parse service providerAbility 1221 String serviceProviderAbility = parseStageServiceProvider(moduleJson, moduleInfo.abilities); 1222 moduleInfo.extensionAbilityInfos = parseModuleExtensionAbilities(moduleJson, data, profileJsons); 1223 // parse abilityform 1224 moduleInfo.abilityFormInfos = parseModuleAbilityforms(moduleInfo.extensionAbilityInfos, 1225 data, serviceProviderAbility); 1226 // parse commonEvent 1227 moduleInfo.commonEvents = parseModuleCommonEvents(moduleInfo.extensionAbilityInfos); 1228 } 1229 1230 /** 1231 * parse module hap info 1232 * 1233 * @param extensionAbilityJson is the json object of extension ability 1234 * @param data is the resource of hap 1235 * @param profileJsons is the map of profile file name and file content 1236 * @return the parsed extensionAbilityInfo 1237 * @throws BundleException Throws this exception if the json is not standard. 1238 */ parseModuleExtensionAbility(JSONObject extensionAbilityJson, byte[] data, HashMap<String, String> profileJsons)1239 static ExtensionAbilityInfo parseModuleExtensionAbility(JSONObject extensionAbilityJson, byte[] data, 1240 HashMap<String, String> profileJsons) throws BundleException { 1241 if (extensionAbilityJson == null) { 1242 LOG.error("JsonUtil::parseModuleExtensionAbility exception: extensionAbilityJson is null."); 1243 throw new BundleException("Parse ability failed, abilityJson is null."); 1244 } 1245 ExtensionAbilityInfo moduleExtensionAbilityInfo = new ExtensionAbilityInfo(); 1246 moduleExtensionAbilityInfo.name = getJsonString(extensionAbilityJson, NAME); 1247 if (extensionAbilityJson.containsKey(SRC_ENTRY)) { 1248 moduleExtensionAbilityInfo.srcEntrance = getJsonString(extensionAbilityJson, SRC_ENTRY); 1249 } else { 1250 moduleExtensionAbilityInfo.srcEntrance = getJsonString(extensionAbilityJson, SRC_ENTRANCE); 1251 } 1252 moduleExtensionAbilityInfo.icon = parseIconById(extensionAbilityJson, data); 1253 moduleExtensionAbilityInfo.label = 1254 parseResourceByKey(extensionAbilityJson, data, LABEL, LABEL_ID); 1255 moduleExtensionAbilityInfo.setLabels(parseResourceMapByKey(extensionAbilityJson, data, LABEL_ID)); 1256 moduleExtensionAbilityInfo.description = 1257 parseResourceByKey(extensionAbilityJson, data, DESCRIPTION, DESCRIPTION_ID); 1258 moduleExtensionAbilityInfo.setDescriptions(parseResourceMapByKey(extensionAbilityJson, data, DESCRIPTION_ID)); 1259 moduleExtensionAbilityInfo.type = getJsonString(extensionAbilityJson, TYPE); 1260 1261 if (extensionAbilityJson.containsKey(PERMISSIONS)) { 1262 moduleExtensionAbilityInfo.permissions = 1263 JSONObject.parseArray(getJsonString(extensionAbilityJson, PERMISSIONS), String.class); 1264 } 1265 moduleExtensionAbilityInfo.uri = getJsonString(extensionAbilityJson, URI); 1266 moduleExtensionAbilityInfo.readPermission = getJsonString(extensionAbilityJson, READ_PERMISSION); 1267 moduleExtensionAbilityInfo.writePermission = getJsonString(extensionAbilityJson, WRITE_PERMISSION); 1268 if (extensionAbilityJson.containsKey(SKILLS)) { 1269 moduleExtensionAbilityInfo.skills = 1270 JSONObject.parseArray(getJsonString(extensionAbilityJson, SKILLS), SkillInfo.class); 1271 } 1272 1273 if (extensionAbilityJson.containsKey(META_DATA)) { 1274 moduleExtensionAbilityInfo.metadataInfos = 1275 parseModuleMetadataInfos(extensionAbilityJson, data, profileJsons); 1276 // convert to metadata 1277 ModuleAdaption adaption = new ModuleAdaption(); 1278 moduleExtensionAbilityInfo.metadata = adaption.convertToMetadata(moduleExtensionAbilityInfo.metadataInfos); 1279 } 1280 if (extensionAbilityJson.containsKey(EXPORTED)) { 1281 moduleExtensionAbilityInfo.visible = getJsonBooleanValue(extensionAbilityJson, EXPORTED, false); 1282 } else { 1283 moduleExtensionAbilityInfo.visible = getJsonBooleanValue(extensionAbilityJson, VISIBLE, false); 1284 } 1285 return moduleExtensionAbilityInfo; 1286 } 1287 1288 /** 1289 * parse abilities info 1290 * 1291 * @param moduleJson Json hap json Object 1292 * @param data resource index data 1293 * @return the List<ModuleAbilityInfo> result 1294 * @throws BundleException Throws this exception if the json is not standard. 1295 */ parseModuleAbilities(JSONObject moduleJson, byte[] data, HashMap<String, String> profileJsons)1296 static List<ModuleAbilityInfo> parseModuleAbilities(JSONObject moduleJson, byte[] data, 1297 HashMap<String, String> profileJsons) throws BundleException { 1298 List<ModuleAbilityInfo> abilities = new ArrayList<>(); 1299 if (!moduleJson.containsKey(ABILITIES)) { 1300 return abilities; 1301 } 1302 JSONArray abilityObjs = moduleJson.getJSONArray(ABILITIES); 1303 int size = abilityObjs.size(); 1304 for (int i = 0; i < size; ++i) { 1305 JSONObject tmpObj =abilityObjs.getJSONObject(i); 1306 abilities.add(parseModuleAbility(tmpObj, data, profileJsons)); 1307 } 1308 return abilities; 1309 } 1310 parseModuleAbilityAndAddShortcut(JSONObject moduleJson, ModuleInfo moduleInfo, byte[] data, HashMap<String, String> profileJsons)1311 private static void parseModuleAbilityAndAddShortcut(JSONObject moduleJson, ModuleInfo moduleInfo, byte[] data, 1312 HashMap<String, String> profileJsons) throws BundleException { 1313 moduleInfo.abilities = parseModuleAbilities(moduleJson, data, profileJsons); 1314 for (ModuleAbilityInfo abilityInfo : moduleInfo.abilities) { 1315 moduleInfo.moduleShortcuts.addAll(parseModuleShortcut(abilityInfo.metadata, data)); 1316 } 1317 } 1318 1319 /** 1320 * parse ability info 1321 * 1322 * @param abilityJson Json hap json Object 1323 * @param data resource index data 1324 * @return the ModuleAbilityInfo result 1325 * @throws BundleException Throws this exception if the json is not standard. 1326 */ parseModuleAbility(JSONObject abilityJson, byte[] data, HashMap<String, String> profileJsons)1327 static ModuleAbilityInfo parseModuleAbility(JSONObject abilityJson, byte[] data, 1328 HashMap<String, String> profileJsons) throws BundleException { 1329 if (abilityJson == null) { 1330 LOG.error("Uncompress::parseModuleAbility exception: abilityJson is null."); 1331 throw new BundleException("Parse ability failed, abilityJson is null."); 1332 } 1333 ModuleAbilityInfo moduleAbilityInfo = new ModuleAbilityInfo(); 1334 moduleAbilityInfo.name = getJsonString(abilityJson, NAME); 1335 if (abilityJson.containsKey(SRC_ENTRY)) { 1336 moduleAbilityInfo.srcEntrance = getJsonString(abilityJson, SRC_ENTRY); 1337 } else { 1338 moduleAbilityInfo.srcEntrance = getJsonString(abilityJson, SRC_ENTRANCE); 1339 } 1340 1341 moduleAbilityInfo.launchType = getJsonString(abilityJson, LAUNCH_TYPE, STANDARD); 1342 moduleAbilityInfo.description = parseResourceByKey(abilityJson, data, DESCRIPTION, DESCRIPTION_ID); 1343 moduleAbilityInfo.setDescriptions(parseResourceMapByKey(abilityJson, data, DESCRIPTION_ID)); 1344 1345 moduleAbilityInfo.icon = parseIconById(abilityJson, data); 1346 moduleAbilityInfo.label = parseResourceByKey(abilityJson, data, LABEL, LABEL_ID); 1347 moduleAbilityInfo.setLabels(parseResourceMapByKey(abilityJson, data, LABEL_ID)); 1348 if (abilityJson.containsKey(PERMISSIONS)) { 1349 moduleAbilityInfo.permissions = JSONObject.parseArray( 1350 getJsonString(abilityJson, PERMISSIONS), String.class); 1351 } 1352 moduleAbilityInfo.metadata = parseModuleMetadataInfos(abilityJson, data, profileJsons); 1353 if (abilityJson.containsKey(EXPORTED)) { 1354 moduleAbilityInfo.visible = getJsonBooleanValue(abilityJson, EXPORTED, false); 1355 } else { 1356 moduleAbilityInfo.visible = getJsonBooleanValue(abilityJson, VISIBLE, false); 1357 } 1358 moduleAbilityInfo.continuable = getJsonBooleanValue(abilityJson, CONTINUABLE, false); 1359 1360 if (abilityJson.containsKey(SKILLS)) { 1361 moduleAbilityInfo.skills = JSON.parseArray(getJsonString(abilityJson, SKILLS), SkillInfo.class); 1362 } 1363 1364 if (abilityJson.containsKey(BACKGROUND_MODES)) { 1365 moduleAbilityInfo.backgroundModes = 1366 JSONArray.parseArray(getJsonString(abilityJson, BACKGROUND_MODES), String.class); 1367 } 1368 return moduleAbilityInfo; 1369 } 1370 1371 /** 1372 * parse metadata array 1373 * 1374 * @param jsonObject indicates json Object. 1375 * @param data resource index data. 1376 * @return the List<ModuleMetadataInfo> result. 1377 * @throws BundleException Throws this exception if the json is not standard. 1378 */ parseModuleMetadataInfos( JSONObject jsonObject, byte[] data, HashMap<String, String> profileJsons)1379 static List<ModuleMetadataInfo> parseModuleMetadataInfos( 1380 JSONObject jsonObject, byte[] data, HashMap<String, String> profileJsons) throws BundleException { 1381 List<ModuleMetadataInfo> moduleMetadataInfos = new ArrayList<>(); 1382 if (!jsonObject.containsKey(META_DATA)) { 1383 return moduleMetadataInfos; 1384 } 1385 JSONArray metadatas = jsonObject.getJSONArray(META_DATA); 1386 for (int i = 0; i < metadatas.size(); ++i) { 1387 JSONObject metadata = metadatas.getJSONObject(i); 1388 moduleMetadataInfos.add(parseModuleMetadata(metadata, data, profileJsons)); 1389 } 1390 return moduleMetadataInfos; 1391 } 1392 1393 /** 1394 * parse metadata info 1395 * 1396 * @param jsonObject Json hap json Object 1397 * @param data resource index data 1398 * @return the ModuleMetadataInfo result 1399 * @throws BundleException Throws this exception if the json is not standard. 1400 */ parseModuleMetadata(JSONObject jsonObject, byte[] data, HashMap<String, String> profileJson)1401 static ModuleMetadataInfo parseModuleMetadata(JSONObject jsonObject, byte[] data, 1402 HashMap<String, String> profileJson) throws BundleException { 1403 if (jsonObject == null) { 1404 LOG.error("JsonUtil::parseModuleMetadata exception: jsonObject is null."); 1405 throw new BundleException("Parse ModuleMetadataInfo failed, jsonObject is null."); 1406 } 1407 ModuleMetadataInfo moduleMetadataInfo = new ModuleMetadataInfo(); 1408 moduleMetadataInfo.name = getJsonString(jsonObject, NAME); 1409 moduleMetadataInfo.value = getJsonString(jsonObject, VALUE); 1410 1411 if (jsonObject.containsKey(RESOURCE)) { 1412 moduleMetadataInfo.resource = getJsonString(jsonObject, RESOURCE); 1413 String fileName = moduleMetadataInfo.resource; 1414 fileName = fileName.replace(PROFILE, ""); 1415 fileName = fileName + JSON_SUFFIX; 1416 moduleMetadataInfo.resource = profileJson.get(fileName); 1417 } 1418 return moduleMetadataInfo; 1419 } 1420 1421 /** 1422 * parse abilityform in stage module. 1423 * 1424 * @param extensionAbilityInfos is extent ability in module object. 1425 * @param data is resource byte in hap. 1426 * @return the List<AbilityFormInfo> result 1427 * @throws BundleException Throws this exception if the json is not standard. 1428 */ parseModuleAbilityforms(List<ExtensionAbilityInfo> extensionAbilityInfos, byte[] data, String serviceProviderAbility)1429 static List<AbilityFormInfo> parseModuleAbilityforms(List<ExtensionAbilityInfo> extensionAbilityInfos, 1430 byte[] data, String serviceProviderAbility) 1431 throws BundleException { 1432 List<AbilityFormInfo> abilityFormInfos = new ArrayList<>(); 1433 if (extensionAbilityInfos.isEmpty()) { 1434 return abilityFormInfos; 1435 } 1436 for (ExtensionAbilityInfo extensionAbilityInfo : extensionAbilityInfos) { 1437 List<AbilityFormInfo> formInfos = 1438 parseModuleFormInfoInMetadata(data, extensionAbilityInfo.metadataInfos); 1439 if (FORM.equals(extensionAbilityInfo.type)) { 1440 for (AbilityFormInfo formInfo : formInfos) { 1441 formInfo.providerAbility = serviceProviderAbility; 1442 } 1443 } 1444 abilityFormInfos.addAll(formInfos); 1445 } 1446 return abilityFormInfos; 1447 } 1448 1449 /** 1450 * parse commonevents info 1451 * 1452 * @param extensionAbilityInfos is the list of extensionAbility. 1453 * @return the List<CommonEvent> result 1454 */ parseModuleCommonEvents(List<ExtensionAbilityInfo> extensionAbilityInfos)1455 static List<CommonEvent> parseModuleCommonEvents(List<ExtensionAbilityInfo> extensionAbilityInfos) { 1456 List<CommonEvent> allCommonEvent = new ArrayList<>(); 1457 if (extensionAbilityInfos.isEmpty()) { 1458 return allCommonEvent; 1459 } 1460 for (ExtensionAbilityInfo extensionAbilityInfo : extensionAbilityInfos) { 1461 List<CommonEvent> commonEvents = 1462 parseCommoneventsInMetadata(extensionAbilityInfo.metadataInfos); 1463 allCommonEvent.addAll(commonEvents); 1464 } 1465 return allCommonEvent; 1466 } 1467 1468 /** 1469 * parse commonevents info 1470 * 1471 * @param moduleMetadataInfos is the list of ModuleMetadataInfo 1472 * @return the List<CommonEvent> result 1473 */ parseCommoneventsInMetadata(List<ModuleMetadataInfo> moduleMetadataInfos)1474 static List<CommonEvent> parseCommoneventsInMetadata(List<ModuleMetadataInfo> moduleMetadataInfos) { 1475 List<CommonEvent> commonEvents = new ArrayList<>(); 1476 // find common events and parse in metadata 1477 for (ModuleMetadataInfo moduleMetadataInfo : moduleMetadataInfos) { 1478 String jsonStr = moduleMetadataInfo.resource; 1479 JSONObject jsonObj = JSONObject.parseObject(jsonStr); 1480 if (jsonObj != null && jsonObj.containsKey("commonEvents")) { 1481 commonEvents.addAll(parseModuleCommonEvents(jsonObj)); 1482 } 1483 } 1484 return commonEvents; 1485 } 1486 1487 /** 1488 * parse commonevents info 1489 * 1490 * @param jsonObject is the json objects of commonevent. 1491 * @return the List<CommonEvent> result 1492 */ parseModuleCommonEvents(JSONObject jsonObject)1493 static List<CommonEvent> parseModuleCommonEvents(JSONObject jsonObject) { 1494 List<CommonEvent> commonEvents = new ArrayList<>(); 1495 JSONArray commonEventObjs = jsonObject.getJSONArray("commonEvents"); 1496 for (int i = 0; i < commonEventObjs.size(); ++i) { 1497 JSONObject commonEventObj = commonEventObjs.getJSONObject(i); 1498 CommonEvent commonEvent = new CommonEvent(); 1499 if (commonEventObj.containsKey("name")) { 1500 commonEvent.name = getJsonString(commonEventObj, "name"); 1501 } 1502 if (commonEventObj.containsKey("permission")) { 1503 commonEvent.permission = getJsonString(commonEventObj, "permission"); 1504 } 1505 if (commonEventObj.containsKey("types")) { 1506 commonEvent.type = JSON.parseArray(getJsonString(commonEventObj, "types"), String.class); 1507 } 1508 if (commonEventObj.containsKey("events")) { 1509 commonEvent.events = JSON.parseArray(getJsonString(commonEventObj, "events"), String.class); 1510 } 1511 commonEvents.add(commonEvent); 1512 } 1513 return commonEvents; 1514 } 1515 1516 /** 1517 * parse stage shortcuts info 1518 * 1519 * @param moduleMetadataInfos is the list of ModuleMetadataInfo 1520 * @param data is resource byte in hap 1521 * @return the List<moduleShortcut> result 1522 * @throws BundleException Throws this exception when parse failed. 1523 */ parseModuleShortcut(List<ModuleMetadataInfo> moduleMetadataInfos, byte[] data)1524 static List<ModuleShortcut> parseModuleShortcut(List<ModuleMetadataInfo> moduleMetadataInfos, byte[] data) 1525 throws BundleException { 1526 List<ModuleShortcut> shortcuts = new ArrayList<>(); 1527 // find shortcut and parse in metadata 1528 for (ModuleMetadataInfo moduleMetadataInfo : moduleMetadataInfos) { 1529 String jsonStr = moduleMetadataInfo.resource; 1530 if (jsonStr.isEmpty()) { 1531 continue; 1532 } 1533 JSONObject jsonObj = JSON.parseObject(jsonStr); 1534 if (jsonObj.containsKey("shortcuts")) { 1535 JSONArray shortcutObjs = jsonObj.getJSONArray("shortcuts"); 1536 for (int j = 0; j < shortcutObjs.size(); ++j) { 1537 shortcuts.add(parseModuleShortcutObj(shortcutObjs.getJSONObject(j), data)); 1538 } 1539 } 1540 } 1541 return shortcuts; 1542 } 1543 1544 /** 1545 * parse stage shortcuts json object array 1546 * 1547 * @param shortcutObj is the objects of shortcut 1548 * @param data is resource byte in hap 1549 * @return the List<ModuleShortcuts> result 1550 * @throws BundleException Throws this exception when parse failed. 1551 */ parseModuleShortcutObj(JSONObject shortcutObj, byte[] data)1552 static ModuleShortcut parseModuleShortcutObj(JSONObject shortcutObj, byte[] data) throws BundleException { 1553 if (shortcutObj == null) { 1554 LOG.error("JsonUtil::parseModuleShortcutObj failed"); 1555 throw new BundleException("JsonUtil::parseModuleShortcutObj failed"); 1556 } 1557 ModuleShortcut moduleShortcut = new ModuleShortcut(); 1558 if (shortcutObj.containsKey("shortcutId")) { 1559 moduleShortcut.setShortcutId(shortcutObj.getString("shortcutId")); 1560 } 1561 if (shortcutObj.containsKey("label")) { 1562 moduleShortcut.setLabel(parseResourceByStringID(data, getJsonString(shortcutObj, "label"))); 1563 } 1564 moduleShortcut.setLabels(parseResourceMapByKey(shortcutObj, data, LABEL_ID)); 1565 1566 if (shortcutObj.containsKey("icon")) { 1567 String iconPath = parseResourceByStringID(data, getJsonString(shortcutObj, "icon")); 1568 moduleShortcut.setIcon(iconPath.substring(iconPath.indexOf("resources"))); 1569 } 1570 if (shortcutObj.containsKey("wants")) { 1571 moduleShortcut.setWants(JSON.parseArray(getJsonString(shortcutObj, "wants"), Want.class)); 1572 } 1573 return moduleShortcut; 1574 } 1575 1576 /** 1577 * parse fa shortcuts json object array 1578 * 1579 * @param moduleJson is the object of module 1580 * @param data is resource byte in hap 1581 * @return the List<ModuleShortcuts> result 1582 * @throws BundleException Throws this exception when parse failed. 1583 */ parseShoruCuts(JSONObject moduleJson, byte[] data)1584 static List<Shortcut> parseShoruCuts(JSONObject moduleJson, byte[] data) throws BundleException { 1585 List<Shortcut> shortcuts = new ArrayList<>(); 1586 if (moduleJson.containsKey("shortcuts")) { 1587 JSONArray shortcutObjs = moduleJson.getJSONArray("shortcuts"); 1588 for (int i = 0; i < shortcutObjs.size(); ++i) { 1589 shortcuts.add(parseShortObj(shortcutObjs.getJSONObject(i), data)); 1590 } 1591 } 1592 return shortcuts; 1593 } 1594 1595 /** 1596 * parse fa shortcuts json object array 1597 * 1598 * @param shortcutObj is the object of shortcut 1599 * @param data is resource byte in hap 1600 * @return the List<ModuleShortcuts> result 1601 * @throws BundleException Throws this exception when parse failed. 1602 */ parseShortObj(JSONObject shortcutObj, byte[] data)1603 static Shortcut parseShortObj(JSONObject shortcutObj, byte[] data) throws BundleException { 1604 if (shortcutObj == null) { 1605 LOG.error("JsonUtil::parseModuleShortcutObj failed"); 1606 throw new BundleException("JsonUtil::parseModuleShortcutObj failed"); 1607 } 1608 Shortcut shortcut = new Shortcut(); 1609 if (shortcutObj.containsKey("shortcutId")) { 1610 shortcut.shortcutId = shortcutObj.getString("shortcutId"); 1611 } 1612 if (shortcutObj.containsKey("label")) { 1613 shortcut.label = parseResourceByKey(shortcutObj, data, "label", "labelId"); 1614 } 1615 shortcut.setLabels(parseResourceMapByKey(shortcutObj, data, LABEL_ID)); 1616 if (shortcutObj.containsKey("icon")) { 1617 shortcut.icon = parseIconById(shortcutObj, data); 1618 } 1619 if (shortcutObj.containsKey("intents")) { 1620 shortcut.intents = JSON.parseArray(getJsonString(shortcutObj, "intents"), IntentInfo.class); 1621 } 1622 return shortcut; 1623 } 1624 1625 /** 1626 * parse form info 1627 * 1628 * @param data is resource byte in hap 1629 * @param moduleMetadataInfos is the list of ModuleMetadataInfo 1630 * @return the List<AbilityFormInfo> result 1631 * @throws BundleException Throws this exception if the json is not standard. 1632 */ parseModuleFormInfoInMetadata( byte[] data, List<ModuleMetadataInfo> moduleMetadataInfos)1633 static List<AbilityFormInfo> parseModuleFormInfoInMetadata( 1634 byte[] data, List<ModuleMetadataInfo> moduleMetadataInfos) throws BundleException { 1635 List<AbilityFormInfo> abilityFormInfos = new ArrayList<>(); 1636 if (moduleMetadataInfos.isEmpty()) { 1637 return abilityFormInfos; 1638 } 1639 // find form json and parse in metadata 1640 for (ModuleMetadataInfo moduleMetadataInfo : moduleMetadataInfos) { 1641 String jsonStr = moduleMetadataInfo.resource; 1642 JSONObject jsonObj = JSONObject.parseObject(jsonStr); 1643 if (jsonObj != null && jsonObj.containsKey(FORMS)) { 1644 JSONArray jsonForms = JSONObject.parseArray(getJsonString(jsonObj, FORMS)); 1645 int size = jsonForms.size(); 1646 for (int j = 0; j < size; ++j) { 1647 JSONObject tmpObj = jsonForms.getJSONObject(j); 1648 abilityFormInfos.add(parseModuleForm(tmpObj, data)); 1649 } 1650 } 1651 } 1652 return abilityFormInfos; 1653 } 1654 1655 /** 1656 * parse form object 1657 * 1658 * @param formObj is form json object 1659 * @param data is resource byte in hap 1660 * @throws BundleException Throws this exception if the json is not standard. 1661 */ parseModuleForm(JSONObject formObj, byte[] data)1662 static AbilityFormInfo parseModuleForm(JSONObject formObj, byte[] data) throws BundleException { 1663 if (formObj == null) { 1664 LOG.error("JsonUtil::parseModuleForm exception: formObj is null."); 1665 throw new BundleException("Parse parseModuleForm failed, formObj is null."); 1666 } 1667 AbilityFormInfo moduleFormInfo = new AbilityFormInfo(); 1668 moduleFormInfo.name = getJsonString(formObj, NAME); 1669 moduleFormInfo.description = parseFormDescription(formObj, data); 1670 moduleFormInfo.setDescriptions(parseFormDescriptions(formObj, data)); 1671 moduleFormInfo.src = getJsonString(formObj, SRC); 1672 1673 if (formObj.containsKey(WINDOW)) { 1674 moduleFormInfo.windowInfo = 1675 JSON.parseObject(getJsonString(formObj, WINDOW), AbilityFormInfo.ModuleWindowInfo.class); 1676 } 1677 moduleFormInfo.isDefault = getJsonBooleanValue(formObj, IS_DEFAULT, false); 1678 moduleFormInfo.colorMode = getJsonString(formObj, COLOR_MODE, AUTO); 1679 if (formObj.containsKey(SUPPORT_DIMENSIONS)) { 1680 moduleFormInfo.supportDimensions = 1681 JSONObject.parseArray(getJsonString(formObj, SUPPORT_DIMENSIONS), String.class); 1682 } 1683 moduleFormInfo.defaultDimension = getJsonString(formObj, DEFAULT_DIMENSION); 1684 moduleFormInfo.updateEnabled = getJsonBooleanValue(formObj, UPDATE_ENABLED, false); 1685 moduleFormInfo.scheduledUpdateTime = getJsonString(formObj, SCHEDULE_UPDATE_TIME); 1686 moduleFormInfo.updateDuration = getJsonIntValue(formObj, UPDATE_DURATION, 1); 1687 moduleFormInfo.formConfigAbility = getJsonString(formObj, FROM_CONFIG_ABILITY); 1688 moduleFormInfo.formVisibleNotify = getJsonBooleanValue(formObj, FORM_VISIBLE_NOTIFY, false); 1689 return moduleFormInfo; 1690 } 1691 parseFormDescription(JSONObject formObj, byte[] data)1692 private static String parseFormDescription(JSONObject formObj, byte[] data) throws BundleException { 1693 if (formObj.containsKey(DESCRIPTION)) { 1694 String descriptionStr = getJsonString(formObj, DESCRIPTION); 1695 if (descriptionStr.contains(STRING_RESOURCE)) { 1696 return parseResourceByStringID(data, descriptionStr); 1697 } else { 1698 return descriptionStr; 1699 } 1700 } 1701 return EMPTY; 1702 } 1703 parseFormDescriptions(JSONObject formObj, byte[] data)1704 private static HashMap<String, String> parseFormDescriptions(JSONObject formObj, byte[] data) { 1705 HashMap<String, String> descriptions = new HashMap<>(); 1706 if (!formObj.containsKey(DESCRIPTION)) { 1707 return descriptions; 1708 } 1709 String descriptionStr = getJsonString(formObj, DESCRIPTION); 1710 if (descriptionStr.contains(STRING_RESOURCE)) { 1711 int len = STRING_RESOURCE.length(); 1712 String descriptionId = descriptionStr.substring(len); 1713 try { 1714 int id = Integer.parseInt(descriptionId); 1715 descriptions = ResourcesParser.getResourceMapById(id, data); 1716 } catch (NumberFormatException e) { 1717 LOG.error("parseFormDescriptions failed: invalid descriptionId."); 1718 } 1719 return descriptions; 1720 } 1721 return descriptions; 1722 } 1723 1724 /** 1725 * parse definepermission objects 1726 * 1727 * @param moduleJson is module json object 1728 * @param data is resource byte in hap 1729 * @throws BundleException Throws this exception if the json is not standard. 1730 */ parseDefinePermissions(JSONObject moduleJson, byte[] data)1731 static List<DefinePermission> parseDefinePermissions(JSONObject moduleJson, byte[] data) throws BundleException { 1732 List<DefinePermission> definePermissions = new ArrayList<>(); 1733 if (!moduleJson.containsKey(DEFINE_PERMISSIONS)) { 1734 return definePermissions; 1735 } 1736 JSONArray definePermissionObjs = moduleJson.getJSONArray(DEFINE_PERMISSIONS); 1737 for (int i = 0; i < definePermissionObjs.size(); ++i) { 1738 definePermissions.add(parseDefinePermission(definePermissionObjs.getJSONObject(i), data)); 1739 } 1740 return definePermissions; 1741 } 1742 parseModuleAtomicService(JSONObject moduleJson)1743 static ModuleAtomicService parseModuleAtomicService(JSONObject moduleJson) { 1744 ModuleAtomicService moduleAtomicService = new ModuleAtomicService(); 1745 JSONObject atomicServiceObj = null; 1746 if (!moduleJson.containsKey(ATOMIC_SERVICE)) { 1747 return moduleAtomicService; 1748 } 1749 atomicServiceObj = moduleJson.getJSONObject(ATOMIC_SERVICE); 1750 JSONArray preloadObjs = atomicServiceObj.getJSONArray(PRELOADS); 1751 List<PreloadItem> preloadItems = new ArrayList<>(); 1752 for (int i = 0; i < preloadObjs.size(); ++i) { 1753 PreloadItem preloadItem = new PreloadItem(); 1754 JSONObject itemObj = preloadObjs.getJSONObject(i); 1755 if (itemObj.containsKey(MODULE_NAME)) { 1756 preloadItem.setModuleName(getJsonString(itemObj, MODULE_NAME)); 1757 } 1758 preloadItems.add(preloadItem); 1759 } 1760 moduleAtomicService.setPreloadItems(preloadItems); 1761 1762 return moduleAtomicService; 1763 } 1764 1765 /** 1766 * parse define permission objects 1767 * 1768 * @param definePermissionObj is def permission json object 1769 * @param data is resource byte in hap 1770 * @throws BundleException Throws this exception if the json is not standard. 1771 */ parseDefinePermission(JSONObject definePermissionObj, byte[] data)1772 static DefinePermission parseDefinePermission(JSONObject definePermissionObj, byte[] data) throws BundleException { 1773 DefinePermission definePermission = new DefinePermission(); 1774 definePermission.name = getJsonString(definePermissionObj, "name", definePermission.name); 1775 definePermission.grantMode = getJsonString(definePermissionObj, "grantMode", definePermission.grantMode); 1776 definePermission.availableLevel = 1777 getJsonString(definePermissionObj, "availableLevel", definePermission.availableLevel); 1778 if (definePermissionObj.containsKey("provisionEnable")) { 1779 definePermission.provisionEnable = definePermissionObj.getBoolean("provisionEnable"); 1780 } 1781 if (definePermissionObj.containsKey("distributedSceneEnable")) { 1782 definePermission.distributedSceneEnable = definePermissionObj.getBoolean("distributedSceneEnable"); 1783 } 1784 definePermission.label = 1785 parseResourceByKey(definePermissionObj, data, "label", "labelId"); 1786 definePermission.setLabels(parseResourceMapByKey(definePermissionObj, data, LABEL_ID)); 1787 1788 definePermission.description = 1789 parseResourceByKey(definePermissionObj, data, "description", "descriptionId"); 1790 definePermission.setDescriptions(parseResourceMapByKey(definePermissionObj, data, DESCRIPTION_ID)); 1791 1792 return definePermission; 1793 } 1794 1795 /** 1796 * parse defpermission objects 1797 * 1798 * @param moduleJson is module json object 1799 * @param data is resource byte in hap 1800 * @throws BundleException Throws this exception if the json is not standard. 1801 */ parseDefPermissions(JSONObject moduleJson, byte[] data)1802 static List<DefPermission> parseDefPermissions(JSONObject moduleJson, byte[] data) throws BundleException { 1803 List<DefPermission> defPermissions = new ArrayList<>(); 1804 if (moduleJson.containsKey("defPermissions")) { 1805 JSONArray defPermissionObjs = moduleJson.getJSONArray("defPermissions"); 1806 for (int i = 0; i < defPermissionObjs.size(); ++i) { 1807 defPermissions.add(parseDefPermission(defPermissionObjs.getJSONObject(i), data)); 1808 } 1809 } 1810 return defPermissions; 1811 } 1812 1813 /** 1814 * parse defpermission objects 1815 * 1816 * @param defPermissionObj is def permission json object 1817 * @param data is resource byte in hap 1818 * @throws BundleException Throws this exception if the json is not standard. 1819 */ parseDefPermission(JSONObject defPermissionObj, byte[] data)1820 static DefPermission parseDefPermission(JSONObject defPermissionObj, byte[] data) throws BundleException { 1821 DefPermission defPermission = new DefPermission(); 1822 if (defPermissionObj.containsKey("name")) { 1823 defPermission.name = getJsonString(defPermissionObj, "name"); 1824 } 1825 if (defPermissionObj.containsKey("grantMode")) { 1826 defPermission.grantMode = getJsonString(defPermissionObj, "grantMode"); 1827 } 1828 if (defPermissionObj.containsKey("availableScope")) { 1829 defPermission.availableScope = 1830 JSONObject.parseArray(getJsonString(defPermissionObj, "availableScope"), String.class); 1831 } 1832 defPermission.label = parseResourceByKey(defPermissionObj, data, "label", "labelId"); 1833 defPermission.setLabels(parseResourceMapByKey(defPermissionObj, data, LABEL_ID)); 1834 1835 defPermission.description = parseResourceByKey(defPermissionObj, data, "description", "descriptionId"); 1836 defPermission.setDescriptions(parseResourceMapByKey(defPermissionObj, data, DESCRIPTION_ID)); 1837 1838 if (defPermissionObj.containsKey("group")) { 1839 defPermission.group = getJsonString(defPermissionObj, "group"); 1840 } 1841 return defPermission; 1842 } 1843 1844 /** 1845 * get resource from JSONObject by the id. 1846 * 1847 * @param data is resource byte in hap. 1848 * @param id is the resource id. 1849 * @return the result 1850 * @throws BundleException Throws this exception when parse failed. 1851 */ parseResourceByStringID(byte[] data, String id)1852 static String parseResourceByStringID(byte[] data, String id) throws BundleException { 1853 String res = ""; 1854 int index = 0; 1855 while(id.charAt(index) < '0' || id.charAt(index) > '9') { 1856 index++; 1857 } 1858 try { 1859 int finalId = Integer.parseInt(id.substring(index)); 1860 res = ResourcesParser.getResourceStringById(finalId, data); 1861 } catch (NumberFormatException e) { 1862 LOG.error("parseResourceByStringID failed: input invalid of " + id + "."); 1863 } 1864 return res; 1865 } 1866 1867 /** 1868 * get resource from JSONObject by the key. 1869 * 1870 * @param jsonObject uncompress json object. 1871 * @param data is resource byte in hap. 1872 * @param key is the index of json object. 1873 * @param keyId is the index id of resource. 1874 * @return the result 1875 */ parseResourceByKey(JSONObject jsonObject, byte[] data, String key, String keyId)1876 static String parseResourceByKey(JSONObject jsonObject, byte[] data, String key, String keyId) { 1877 String res = ""; 1878 if (jsonObject.containsKey(keyId)) { 1879 int resId = jsonObject.getIntValue(keyId); 1880 res = ResourcesParser.getResourceStringById(resId, data); 1881 } 1882 if (res != null && !res.isEmpty()) { 1883 return res; 1884 } else if (jsonObject.containsKey(key)) { 1885 return getJsonString(jsonObject, key); 1886 } else { 1887 return ""; 1888 } 1889 } 1890 parseResourceMapByKey(JSONObject jsonObject, byte[] data, String keyId)1891 static HashMap<String, String> parseResourceMapByKey(JSONObject jsonObject, byte[] data, String keyId) { 1892 HashMap<String, String> map = new HashMap<>(); 1893 if (jsonObject.containsKey(keyId)) { 1894 int resId = jsonObject.getIntValue(keyId); 1895 map = ResourcesParser.getResourceMapById(resId, data); 1896 } 1897 return map; 1898 } 1899 1900 /** 1901 * get icon path from resource by the key. 1902 * 1903 * @param jsonObject uncompress json object. 1904 * @param data is resource byte in hap 1905 * @return the result 1906 * @throws BundleException Throws this exception when parse failed. 1907 */ parseIconById(JSONObject jsonObject, byte[] data)1908 static String parseIconById(JSONObject jsonObject, byte[] data) throws BundleException { 1909 String iconPath = ""; 1910 if (jsonObject.containsKey("iconId")) { 1911 int resId = jsonObject.getIntValue("iconId"); 1912 iconPath = ResourcesParser.getBaseResourceById(resId, data); 1913 iconPath = iconPath.substring(iconPath.indexOf("resources")); 1914 } 1915 if (!iconPath.isEmpty()) { 1916 return iconPath; 1917 } else if (jsonObject.containsKey("icon")) { 1918 return getJsonString(jsonObject, "icon"); 1919 } else { 1920 return ""; 1921 } 1922 } 1923 1924 /** 1925 * parse stage module reqPermissions. 1926 * 1927 * @param moduleJson module json object. 1928 * @param data is resource byte in hap. 1929 * @return the result 1930 */ parseReqPermission(JSONObject moduleJson, byte[] data)1931 static List<ReqPermission> parseReqPermission(JSONObject moduleJson, byte[] data) throws BundleException { 1932 List<ReqPermission> reqPermissions = new ArrayList<>(); 1933 if (!moduleJson.containsKey(REQUEST_PERMISSIONS)) { 1934 return reqPermissions; 1935 } 1936 JSONArray requestPermissionObjs = moduleJson.getJSONArray(REQUEST_PERMISSIONS); 1937 for (int i = 0; i < requestPermissionObjs.size(); ++i) { 1938 ReqPermission reqPermission = new ReqPermission(); 1939 JSONObject requestPermission = requestPermissionObjs.getJSONObject(i); 1940 reqPermission.name = getJsonString(requestPermission, NAME); 1941 reqPermission.reason = parseResourceByKey(requestPermission, data, REASON, REASON_ID); 1942 if (requestPermission.containsKey(REASON_ID)) { 1943 reqPermission.setReasons(parseResourceMapByKey(requestPermission, data, REASON_ID)); 1944 } 1945 if (requestPermission.containsKey(USED_SCENE)) { 1946 reqPermission.usedScene = parseModuleUsedScene(requestPermission.getJSONObject(USED_SCENE)); 1947 } 1948 reqPermissions.add(reqPermission); 1949 } 1950 return reqPermissions; 1951 } 1952 parseModuleUsedScene(JSONObject usedSceneObj)1953 private static UsedScene parseModuleUsedScene(JSONObject usedSceneObj) { 1954 UsedScene usedScene = new UsedScene(); 1955 if (usedSceneObj.containsKey(ABILITIES)) { 1956 usedScene.ability = JSON.parseArray(getJsonString(usedSceneObj, ABILITIES), String.class); 1957 } 1958 if (usedSceneObj.containsKey(WHEN)) { 1959 usedScene.when = getJsonString(usedSceneObj, WHEN); 1960 } 1961 return usedScene; 1962 } 1963 1964 /** 1965 * get the String from JSONObject by the key. 1966 * 1967 * @param jsonObject uncompress json object 1968 * @param key value key 1969 * @return the result 1970 */ getJsonString(JSONObject jsonObject, String key)1971 private static String getJsonString(JSONObject jsonObject, String key) { 1972 String value = ""; 1973 if (jsonObject != null && jsonObject.containsKey(key)) { 1974 value = jsonObject.get(key).toString(); 1975 } 1976 return value; 1977 } 1978 1979 /** 1980 * get the String from JSONObject by the key. 1981 * 1982 * @param jsonObject uncompress json object 1983 * @param key value key 1984 * @param defaultValue the default value 1985 * @return the result 1986 */ getJsonString(JSONObject jsonObject, String key, String defaultValue)1987 private static String getJsonString(JSONObject jsonObject, String key, String defaultValue) { 1988 String value = defaultValue; 1989 if (jsonObject != null && jsonObject.containsKey(key)) { 1990 value = jsonObject.get(key).toString(); 1991 } 1992 return value; 1993 } 1994 getJsonIntValue(JSONObject jsonObject, String key, int defaultValue)1995 private static int getJsonIntValue(JSONObject jsonObject, String key, int defaultValue) { 1996 int value = defaultValue; 1997 if (jsonObject != null && jsonObject.containsKey(key)) { 1998 value = jsonObject.getIntValue(key); 1999 } 2000 return value; 2001 } 2002 getJsonBooleanValue(JSONObject jsonObject, String key, boolean defaultValue)2003 private static boolean getJsonBooleanValue(JSONObject jsonObject, String key, boolean defaultValue) { 2004 boolean value = defaultValue; 2005 if (jsonObject != null && jsonObject.containsKey(key)) { 2006 value = jsonObject.getBooleanValue(key); 2007 } 2008 return value; 2009 } 2010 setFAProviderAbility(JSONObject moduleJson, HapInfo hapInfo, List<AbilityInfo> abilityInfos)2011 private static void setFAProviderAbility(JSONObject moduleJson, HapInfo hapInfo, 2012 List<AbilityInfo> abilityInfos) throws BundleException { 2013 if (abilityInfos.isEmpty()) { 2014 throw new BundleException("JsonUtil::setProviderAbility abilityInfo is empty."); 2015 } 2016 String serviceProviderAbility = parseFAServiceProviderAbility(moduleJson, abilityInfos); 2017 for (AbilityInfo abilityInfo : abilityInfos) { 2018 if (!abilityInfo.formInfos.isEmpty()) { 2019 if (SERVICE.equals(abilityInfo.type)) { 2020 setProviderAbilityForForm(abilityInfo.formInfos, serviceProviderAbility); 2021 } 2022 if (PAGE.equals(abilityInfo.type)) { 2023 setProviderAbilityForForm(abilityInfo.formInfos, abilityInfo.name); 2024 } 2025 hapInfo.formInfos.addAll(abilityInfo.formInfos); 2026 } 2027 } 2028 } 2029 setProviderAbilityForForm(List<AbilityFormInfo> abilityFormInfos, String providerAbility)2030 private static void setProviderAbilityForForm(List<AbilityFormInfo> abilityFormInfos, String providerAbility) { 2031 for (AbilityFormInfo abilityFormInfo : abilityFormInfos) { 2032 abilityFormInfo.providerAbility = providerAbility; 2033 } 2034 } 2035 parseStageServiceProvider(JSONObject moduleJson, List<ModuleAbilityInfo> moduleAbilityInfos)2036 private static String parseStageServiceProvider(JSONObject moduleJson, 2037 List<ModuleAbilityInfo> moduleAbilityInfos) throws BundleException { 2038 if (moduleJson.containsKey(MAIN_ELEMENT)) { 2039 return getJsonString(moduleJson, MAIN_ELEMENT); 2040 } 2041 if (!moduleAbilityInfos.isEmpty()) { 2042 for (ModuleAbilityInfo moduleAbilityInfo : moduleAbilityInfos) { 2043 if (isSystemHomeAbility(moduleAbilityInfo.skills)) { 2044 return moduleAbilityInfo.name; 2045 } 2046 } 2047 return moduleAbilityInfos.get(0).name; 2048 } 2049 return ""; 2050 } 2051 parseFAServiceProviderAbility(JSONObject moduleJson, List<AbilityInfo> abilityInfos)2052 private static String parseFAServiceProviderAbility(JSONObject moduleJson, 2053 List<AbilityInfo> abilityInfos) throws BundleException { 2054 if (abilityInfos.isEmpty()) { 2055 throw new BundleException("JsonUtil::parseServiceProviderAbility abilityInfos is empty."); 2056 } 2057 2058 if (moduleJson.containsKey(MAIN_ABILITY)) { 2059 return getJsonString(moduleJson, MAIN_ABILITY); 2060 } 2061 for (AbilityInfo abilityInfo : abilityInfos) { 2062 if (isSystemHomeAbility(abilityInfo.skills)) { 2063 return abilityInfo.name; 2064 } 2065 } 2066 for (AbilityInfo abilityInfo : abilityInfos) { 2067 if (abilityInfo.type.equals(PAGE)) { 2068 return abilityInfo.name; 2069 } 2070 } 2071 return ""; 2072 } 2073 isSystemHomeAbility(List<SkillInfo> skills)2074 private static boolean isSystemHomeAbility(List<SkillInfo> skills) { 2075 for (SkillInfo skillInfo : skills) { 2076 if (skillInfo.entities.contains(ENTITY_SYSTEM_HOME) && skillInfo.actions.contains(ACTION_SYSTEM_HOME)) { 2077 return true; 2078 } 2079 } 2080 return false; 2081 } 2082 2083 /** 2084 * parse patch.json form json string. 2085 * 2086 * @param jsonString is the file path of hqf file 2087 * @return HQFVerifyInfo 2088 */ parsePatch(String jsonString)2089 static HQFInfo parsePatch(String jsonString) throws BundleException { 2090 HQFInfo hqfVerifyInfo = new HQFInfo(); 2091 JSONObject jsonObject = JSON.parseObject(jsonString); 2092 JSONObject appObj = jsonObject.getJSONObject(APP); 2093 if (appObj == null) { 2094 LOG.error("parsePatch failed, input patch.json is invalid, patch.json has no app."); 2095 throw new BundleException("parsePatch failed, input patch.json is invalid."); 2096 } 2097 2098 if (appObj.containsKey(BUNDLENAME)) { 2099 hqfVerifyInfo.setBundleName(appObj.getString(BUNDLENAME)); 2100 } 2101 if (appObj.containsKey(VERSIONCODE)) { 2102 hqfVerifyInfo.setVersionCode(appObj.getIntValue(VERSIONCODE)); 2103 } 2104 if (appObj.containsKey(VERSIONNAME)) { 2105 hqfVerifyInfo.setVersionName(appObj.getString(VERSIONNAME)); 2106 } 2107 if (appObj.containsKey(PATCH_VERSION_CODE)) { 2108 hqfVerifyInfo.setPatchVersionCode(appObj.getIntValue(PATCH_VERSION_CODE)); 2109 } 2110 if (appObj.containsKey(PATCH_VERSION_NAME)) { 2111 hqfVerifyInfo.setPatchVersionName(appObj.getString(PATCH_VERSION_NAME)); 2112 } 2113 JSONObject moduleObj = jsonObject.getJSONObject(MODULE); 2114 if (moduleObj == null) { 2115 LOG.error("parse failed, input patch.json is invalid, patch.json has no module."); 2116 throw new BundleException("parse failed, input patch.json is invalid, patch.json has no module."); 2117 } 2118 if (moduleObj.containsKey(NAME)) { 2119 hqfVerifyInfo.setModuleName(moduleObj.getString(NAME)); 2120 } 2121 if (moduleObj.containsKey(TYPE)) { 2122 hqfVerifyInfo.setType(moduleObj.getString(TYPE)); 2123 } 2124 if (moduleObj.containsKey(DEVICE_TYPES)) { 2125 hqfVerifyInfo.setDeviceTypes(JSONObject.parseArray(getJsonString(moduleObj, DEVICE_TYPES), String.class)); 2126 } 2127 if (moduleObj.containsKey(ORIGINAL_MODULE_HASH)) { 2128 hqfVerifyInfo.setOriginalModuleHash(moduleObj.getString(ORIGINAL_MODULE_HASH)); 2129 } 2130 return hqfVerifyInfo; 2131 } 2132 } 2133