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