From 0b4fe92c7e61ec36da47747df711ac2044545a11 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 20 Feb 2020 16:01:08 +0530 Subject: [PATCH] Modify app manager payload for old android agent versions --- .../android/common/AndroidConstants.java | 11 +++ .../android/core/impl/AndroidServiceImpl.java | 2 +- .../core/util/MobileDeviceManagementUtil.java | 75 +++++++++++++++++-- 3 files changed, 82 insertions(+), 6 deletions(-) diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/AndroidConstants.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/AndroidConstants.java index be48dbe62..437646f03 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/AndroidConstants.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.common/src/main/java/org/wso2/carbon/device/mgt/mobile/android/common/AndroidConstants.java @@ -192,6 +192,14 @@ public final class AndroidConstants { public static final String IS_ACTIVE = "isActive"; public static final String FREE_SUB_METHOD = "FREE"; public static final String PAID_SUB_METHOD = "PAID"; + public static final String TYPE = "type"; + public static final String ENTERPRISE = "ENTERPRISE"; + public static final String WEB_CLIP = "WEB_CLIP"; + public static final String WEBAPP = "WEBAPP"; + public static final String PACKAGE_NAME = "packageName"; + public static final String APP_IDENTIFIER = "appIdentifier"; + public static final String AGENT_VERSION = "AGENT_VERSION"; + } public final class ApplicationInstall { @@ -220,6 +228,9 @@ public final class AndroidConstants { public static final String BEHAVIOUR_WHITELISTED_APPS_ONLY = "whitelist"; public static final String INSTALL = "INSTALL"; public static final String UNINSTALL = "UNINSTALL"; + public static final String INSTALL_APPLICATION = "INSTALL_APPLICATION"; + public static final String UNINSTALL_APPLICATION = "UNINSTALL_APPLICATION"; + } public final class ErrorMessages { diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/AndroidServiceImpl.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/AndroidServiceImpl.java index eae9eae0b..2b661dced 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/AndroidServiceImpl.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/impl/AndroidServiceImpl.java @@ -946,7 +946,7 @@ public class AndroidServiceImpl implements AndroidService { throw new DeviceManagementException(msg, e); } try { - return MobileDeviceManagementUtil.getPendingOperations(device); + return MobileDeviceManagementUtil.getPendingOperations(device, deviceIdentifier); } catch (OperationManagementException e) { String msg = "Issue in retrieving operation management service instance"; log.error(msg, e); diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/util/MobileDeviceManagementUtil.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/util/MobileDeviceManagementUtil.java index 996a63e59..a559c11f5 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/util/MobileDeviceManagementUtil.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.core/src/main/java/org/wso2/carbon/device/mgt/mobile/android/core/util/MobileDeviceManagementUtil.java @@ -43,6 +43,7 @@ import com.google.gson.JsonNull; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.apache.commons.httpclient.HttpException; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.client.methods.HttpPost; @@ -81,6 +82,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; +import org.wso2.carbon.device.mgt.core.operation.mgt.util.OperationIdComparator; import org.wso2.carbon.device.mgt.core.search.mgt.impl.Utils; import org.wso2.carbon.device.mgt.mobile.android.common.AndroidConstants; import org.wso2.carbon.device.mgt.mobile.android.common.bean.DeviceState; @@ -101,6 +103,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -138,10 +141,16 @@ public class MobileDeviceManagementUtil { } public static List getPendingOperations - (Device device) throws OperationManagementException { - List operations; - operations = AndroidDeviceManagementDataHolder.getInstance().getDeviceManagementProviderService() - .getPendingOperations(device); + (Device device, DeviceIdentifier deviceIdentifier) throws OperationManagementException { + List operations = AndroidDeviceManagementDataHolder.getInstance() + .getDeviceManagementProviderService().getPendingOperations(device); + if (operations != null) { + List pendingOperations = new ArrayList<>(operations); + if (!pendingOperations.isEmpty() && deviceIdentifier != null) { + handleAppManagerPayloadForOldAgent(deviceIdentifier, pendingOperations); + operations = pendingOperations; + } + } return operations; } @@ -227,6 +236,62 @@ public class MobileDeviceManagementUtil { } } + /** + * App Installation and App Un-installation payloads are modified as per old app manager payloads to + * support old android agent versions. Old android agent versions are detected on the unavailability of + * AGENT_VERSION value. + * + * @param deviceIdentifier of the Device + * @param operations list of pending operations + * @throws OperationManagementException when there is an error in retreiveing device information + */ + private static void handleAppManagerPayloadForOldAgent( + DeviceIdentifier deviceIdentifier, List operations) throws OperationManagementException { + List appManagerOperations = new ArrayList<>(); + Iterator operationIterator = operations.iterator(); + while (operationIterator.hasNext()) { + Operation op = operationIterator.next(); + if (AndroidConstants.ApplicationInstall.INSTALL_APPLICATION.equals(op.getCode()) + || AndroidConstants.ApplicationInstall.UNINSTALL_APPLICATION.equals(op.getCode())) { + DeviceInfo deviceInfo; + try { + deviceInfo = AndroidDeviceManagementDataHolder.getInstance().getDeviceInformationManager() + .getDeviceInfo(deviceIdentifier); + } catch (DeviceDetailsMgtException e) { + String msg = "Error occurred while retrieving device info from DeviceInformationManagerService " + + "of device " + deviceIdentifier; + log.error(msg); + throw new OperationManagementException(msg, e); + } + if (deviceInfo != null + && deviceInfo.getDeviceDetailsMap() != null + && !deviceInfo.getDeviceDetailsMap().isEmpty() + && StringUtils.isBlank(deviceInfo.getDeviceDetailsMap().get( + AndroidConstants.ApplicationProperties.AGENT_VERSION))) { + JSONObject appPayload = new JSONObject(op.getPayLoad().toString()); + String appType = appPayload.getString(AndroidConstants.ApplicationProperties.TYPE); + if (AndroidConstants.ApplicationProperties.ENTERPRISE.equals(appType)) { + appPayload.put(AndroidConstants.ApplicationProperties.PACKAGE_NAME, + appPayload.getString(AndroidConstants.ApplicationProperties.APP_IDENTIFIER)); + op.setPayLoad(appPayload.toString()); + appManagerOperations.add(op); + operationIterator.remove(); + } else if (AndroidConstants.ApplicationProperties.WEB_CLIP.equals(appType)) { + appPayload.put(AndroidConstants.ApplicationProperties.TYPE, + AndroidConstants.ApplicationProperties.WEBAPP); + op.setPayLoad(appPayload.toString()); + appManagerOperations.add(op); + operationIterator.remove(); + } + } + } + } + if (!appManagerOperations.isEmpty()) { + operations.addAll(appManagerOperations); + operations.sort(new OperationIdComparator()); + } + } + private static void updateApplicationList(Operation operation, Device device) throws org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException { // Parsing json string to get applications list. @@ -524,7 +589,7 @@ public class MobileDeviceManagementUtil { public static void updateDisEnrollOperationStatus(Device device) throws DeviceManagementException { try { - List pendingOperations = getPendingOperations(device); + List pendingOperations = getPendingOperations(device, null); if (pendingOperations != null && !pendingOperations.isEmpty()) { for (Operation operation : pendingOperations) { operation.setStatus(Operation.Status.ERROR);