From 67e75ee38a52595b9cccffc3eb876455732f8b01 Mon Sep 17 00:00:00 2001 From: Hasunie Date: Mon, 19 Dec 2016 08:06:36 +0530 Subject: [PATCH] adding DeviceInfo operation handler --- .../api/common/util/WindowsAPIUtils.java | 10 +- .../api/operations/util/OperationHandler.java | 94 +++++++++++++++++-- .../api/operations/util/OperationReply.java | 59 +++++++----- 3 files changed, 128 insertions(+), 35 deletions(-) diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/WindowsAPIUtils.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/WindowsAPIUtils.java index dad07f8d1..274c7ffae 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/WindowsAPIUtils.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/WindowsAPIUtils.java @@ -226,13 +226,17 @@ public class WindowsAPIUtils { } } - private static void updateDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) + /** + * This method is used to update Device Information. + * @param deviceId DeviceID to need to update. + * @param deviceInfo Device Info to be update/ + * @throws DeviceDetailsMgtException Error occurs while updating Device Info. + */ + public static void updateDeviceInfo(DeviceIdentifier deviceId, DeviceInfo deviceInfo) throws DeviceDetailsMgtException { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); DeviceInformationManager informationManager = (DeviceInformationManager) ctx.getOSGiService(DeviceInformationManager.class, null); - informationManager.addDeviceInfo(deviceId, deviceInfo); } } diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/operations/util/OperationHandler.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/operations/util/OperationHandler.java index f166f8197..99b9ef68a 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/operations/util/OperationHandler.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/operations/util/OperationHandler.java @@ -24,6 +24,7 @@ import org.json.JSONException; import org.json.JSONObject; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementService; @@ -165,7 +166,7 @@ public class OperationHandler { } } - /*** + /** * Update status of the ring operation. * * @param status Ring status of the device. @@ -193,7 +194,7 @@ public class OperationHandler { } } - /*** + /** * Update the status of the DataWipe operation. * * @param status Status of the data wipe. @@ -222,6 +223,23 @@ public class OperationHandler { } } + + public void updateDeviceInfoStatus(DeviceIdentifier deviceIdentifier) throws OperationManagementException { + List pendingDeviceInfoOperations; + try { + pendingDeviceInfoOperations = WindowsAPIUtils.getPendingOperations(deviceIdentifier); + } catch (DeviceManagementException e) { + throw new OperationManagementException("Error occurred in getting pending operation."); + } + for (Operation operation : pendingDeviceInfoOperations) { + if (PluginConstants.OperationCodes.DEVICE_INFO.equals(operation.getCode())) { + operation.setStatus(Operation.Status.COMPLETED); + updateStatus(deviceIdentifier.getId(), pendingDeviceInfoOperations); + } + } + } + + /** * Get pending operations. * @@ -231,13 +249,20 @@ public class OperationHandler { */ public List getPendingOperations(SyncmlDocument syncmlDocument) throws OperationManagementException, WindowsOperationException { - + SyncmlHeader syncmlHeader = syncmlDocument.getHeader(); + SyncmlBody syncmlBody = syncmlDocument.getBody(); List pendingOperations; - DeviceIdentifier deviceIdentifier = convertToDeviceIdentifierObject( - syncmlDocument.getHeader().getSource().getLocURI()); + DeviceIdentifier deviceIdentifier = convertToDeviceIdentifierObject(syncmlHeader.getSource().getLocURI()); + int sessionId = syncmlHeader.getSessionId(); + int msgId = syncmlHeader.getMsgID(); + if (!(PluginConstants.SyncML.SYNCML_FIRST_MESSAGE_ID == msgId && + PluginConstants.SyncML.SYNCML_FIRST_SESSION_ID == sessionId)) { + if ((syncmlBody.getResults() != null)) { + updateDeviceInfo(syncmlDocument); + } + } UpdateUriOperations(syncmlDocument); generateComplianceFeatureStatus(syncmlDocument); - pendingOperations = WindowsAPIUtils.getDeviceManagementService().getPendingOperations(deviceIdentifier); return pendingOperations; } @@ -486,4 +511,61 @@ public class OperationHandler { } } + + public void updateDeviceInfo(SyncmlDocument syncmlDocument) throws WindowsOperationException { + String softwareVersion; + String imsi; + String imei; + String model; + String vendor; + String totalRAM; + String deviceID = null; + String totalStorage; + + List deviceInformations = syncmlDocument.getBody().getResults().getItem(); + DeviceInfo deviceInfo = new DeviceInfo(); + for (ItemTag item : deviceInformations) { + String source = item.getSource().getLocURI(); + if (OperationCode.Info.SOFTWARE_VERSION.getCode().equals(source)) { + softwareVersion = item.getData(); + deviceInfo.setOsVersion(softwareVersion); + } + if (OperationCode.Info.IMSI.getCode().equals(source)) { + imsi = item.getData(); + deviceInfo.setIMSI(imsi); + } + if (OperationCode.Info.IMEI.getCode().equals(source)) { + imei = item.getData(); + deviceInfo.setIMEI(imei); + } + if (OperationCode.Info.DEVICE_MODEL.getCode().equals(source)) { + model = item.getData(); + deviceInfo.setDeviceModel(model); + } + if (OperationCode.Info.VENDOR.getCode().equals(source)) { + vendor = item.getData(); + deviceInfo.setVendor(vendor); + } + if (OperationCode.Info.TOTAL_RAM.getCode().equals(source)) { + totalRAM = item.getData(); + deviceInfo.setAvailableRAMMemory(Double.parseDouble(totalRAM)); + } + if (OperationCode.Info.TOTAL_STORAGE.getCode().equals(source)) { + totalStorage = item.getData(); + deviceInfo.setInternalAvailableMemory(Double.parseDouble(totalStorage)); + } + if (OperationCode.Info.DEV_ID.getCode().equals(source)) { + deviceID = item.getData(); + } + } + DeviceIdentifier deviceIdentifier = convertToDeviceIdentifierObject(deviceID); + try { + WindowsAPIUtils.updateDeviceInfo(deviceIdentifier, deviceInfo); + updateDeviceInfoStatus(deviceIdentifier); + } catch (org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException e) { + throw new WindowsOperationException("Error occurred while adding Device info."); + } catch (OperationManagementException e) { + throw new WindowsOperationException("Error occurred while updating Device info operation status."); + } + } } diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/operations/util/OperationReply.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/operations/util/OperationReply.java index 4c08ef623..658e7dc13 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/operations/util/OperationReply.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/operations/util/OperationReply.java @@ -247,10 +247,10 @@ public class OperationReply { switch (type) { case POLICY: if (this.syncmlDocument.getBody().getAlert() != null) { - if ((Constants.INITIAL_ALERT_DATA.equals(this.syncmlDocument.getBody() - .getAlert().getData())) || - Constants.INITIAL_WIN10_ALERT_DATA.equals(this.syncmlDocument.getBody() - .getAlert().getData())) { + if ((Constants.INITIAL_ALERT_DATA.equals(this.syncmlDocument.getBody().getAlert() + .getData())) || Constants.INITIAL_WIN10_ALERT_DATA. + equals(this.syncmlDocument.getBody() + .getAlert().getData())) { SequenceTag policySequence = new SequenceTag(); policySequence = buildSequence(operation, policySequence); syncmlBody.setSequence(policySequence); @@ -322,33 +322,40 @@ public class OperationReply { } } if (PluginConstants.OperationCodes.DEVICE_INFO.equals(operation.getCode())) { - HeartBeatDeviceInfo heartBeatDeviceInfo = new HeartBeatDeviceInfo(); - deviceInfoOperations = heartBeatDeviceInfo.getDeviceInfo(); - for (int x = 0; x > deviceInfoOperations.size(); x++) { - ItemTag deviceInfo = appendGetInfo(operation); - getElements.add(deviceInfo); + if (this.syncmlDocument.getBody().getAlert() != null) { + if ((Constants.INITIAL_ALERT_DATA.equals(this.syncmlDocument.getBody().getAlert() + .getData())) || Constants.INITIAL_WIN10_ALERT_DATA. + equals(this.syncmlDocument.getBody() + .getAlert().getData())) { + HeartBeatDeviceInfo heartBeatDeviceInfo = new HeartBeatDeviceInfo(); + deviceInfoOperations = heartBeatDeviceInfo.getDeviceInfo(); + for (Operation infoOperation : deviceInfoOperations) { + ItemTag deviceInfo = appendGetInfo(infoOperation); + getElements.add(deviceInfo); + } + } } + break; } - break; } } + if (!replaceItems.isEmpty()) { + replaceElement.setCommandId(Constants.SyncmlMessageCodes.replaceCommandId); + replaceElement.setItems(replaceItems); + } + if (!getElements.isEmpty()) { + getElement.setCommandId(Constants.SyncmlMessageCodes.elementCommandId); + getElement.setItems(getElements); + } + if (!addElements.isEmpty()) { + atomicTagElement.setCommandId(Constants.SyncmlMessageCodes.atomicCommandId); + atomicTagElement.setAdds(addElements); + } + syncmlBody.setGet(getElement); + syncmlBody.setExec(executeElements); + syncmlBody.setAtomicTag(atomicTagElement); + syncmlBody.setReplace(replaceElement); } - if (!replaceItems.isEmpty()) { - replaceElement.setCommandId(Constants.SyncmlMessageCodes.replaceCommandId); - replaceElement.setItems(replaceItems); - } - if (!getElements.isEmpty()) { - getElement.setCommandId(Constants.SyncmlMessageCodes.elementCommandId); - getElement.setItems(getElements); - } - if (!addElements.isEmpty()) { - atomicTagElement.setCommandId(Constants.SyncmlMessageCodes.atomicCommandId); - atomicTagElement.setAdds(addElements); - } - syncmlBody.setGet(getElement); - syncmlBody.setExec(executeElements); - syncmlBody.setAtomicTag(atomicTagElement); - syncmlBody.setReplace(replaceElement); } private ItemTag appendExecInfo(Operation operation) {