diff --git a/adapter/ohos/JsonUtil.java b/adapter/ohos/JsonUtil.java index 35de49dedec55c1a82938bc0ba1ea916a64e7933..0a2fedbb09b34acbf00d5b7a7bd37741630ec7c0 100644 --- a/adapter/ohos/JsonUtil.java +++ b/adapter/ohos/JsonUtil.java @@ -132,17 +132,54 @@ public class JsonUtil { return true; } + private static void parseDeviceTypeToHapInfo(String packInfoJsonStr, HapInfo hapInfo, String hapName) { + LOG.info("Uncompress::parseDeviceTypeToHapInfo: begin"); + JSONObject jsonObject = JSONObject.parseObject(packInfoJsonStr); + if (jsonObject == null || !jsonObject.containsKey("packages")) { + LOG.error("Uncompress::parseDeviceTypeToHapInfo error: no packages"); + return; + } + JSONArray jsonList = JSONArray.parseArray(getJsonString(jsonObject, "packages")); + if (jsonList == null) { + LOG.error("Uncompress::parseDeviceTypeToHapInfo error: packages is null"); + return; + } + + for (int i = 0; i < jsonList.size(); i++) { + JSONObject tmpObj = jsonList.getJSONObject(i); + if (tmpObj == null) { + LOG.error("Uncompress::parseDeviceTypeToHapInfo error: obj is null"); + continue; + } + String name = getJsonString(tmpObj, NAME); + if (name != null && name.equals(hapName)) { + String deviceTypes = getJsonString(tmpObj, DEVICE_TYPE_NEW); + if (deviceTypes == null || deviceTypes.isEmpty()) { + deviceTypes = getJsonString(tmpObj, DEVICE_TYPE); + } + if (deviceTypes != null && !deviceTypes.isEmpty()) { + hapInfo.deviceType = JSONArray.parseArray(deviceTypes + .replace(UncompressEntrance.DEVICE_TYPE_DEFAULT, UncompressEntrance.DEVICE_TYPE_PHONE), + String.class); + } + break; + } + } + return; + } + /** * parse hap profile info * * @param harmonyProfileJsonString uncompress json String * @param data resource index data * @param paclInfoJsonString pack.info json String + * @param hapName hap file name * @return the parseProfileInfo result * @throws BundleException Throws this exception if the json is not standard. */ - static ProfileInfo parseProfileInfo(String harmonyProfileJsonString, byte[] data, String paclInfoJsonString) - throws BundleException { + static ProfileInfo parseProfileInfo(String harmonyProfileJsonString, byte[] data, String paclInfoJsonString, + String hapName) throws BundleException { ProfileInfo profileInfo = new ProfileInfo(); JSONObject jsonObject = JSONObject.parseObject(harmonyProfileJsonString); if (jsonObject == null || !jsonObject.containsKey(APP) || !jsonObject.containsKey("deviceConfig") @@ -158,6 +195,7 @@ public class JsonUtil { JSONObject hapJson = jsonObject.getJSONObject("module"); profileInfo.hapInfo = parseHapInfo(hapJson, data); } + parseDeviceTypeToHapInfo(paclInfoJsonString, profileInfo.hapInfo, hapName); if (jsonObject.containsKey("deviceConfig")) { JSONObject deviceConfigJson = jsonObject.getJSONObject("deviceConfig"); profileInfo.deviceConfig = parseDeviceConfigInfo(deviceConfigJson, profileInfo.hapInfo.deviceType); diff --git a/adapter/ohos/Uncompress.java b/adapter/ohos/Uncompress.java index bd3d5d3e06845d780b7a48a8cbe593808be0cbce..1634114e1f8558cfb63cc20722273a62b69c8294 100644 --- a/adapter/ohos/Uncompress.java +++ b/adapter/ohos/Uncompress.java @@ -666,7 +666,7 @@ public class Uncompress { private static void uncompressProfileInfo(HapZipInfo hapZipInfo, UncomperssResult uncomperssResult) throws BundleException { ProfileInfo profileInfo = JsonUtil.parseProfileInfo(hapZipInfo.getHarmonyProfileJsonStr(), - hapZipInfo.getResDataBytes(), hapZipInfo.getPackInfoJsonStr()); + hapZipInfo.getResDataBytes(), hapZipInfo.getPackInfoJsonStr(), hapZipInfo.getHapFileName()); profileInfo.hapName = hapZipInfo.getHapFileName(); uncomperssResult.addProfileInfoStr(hapZipInfo.getHarmonyProfileJsonStr()); uncomperssResult.addProfileInfo(profileInfo);