diff --git a/client/client/src/main/java/org/wso2/iot/agent/services/PolicyOperationsMapper.java b/client/client/src/main/java/org/wso2/iot/agent/services/PolicyOperationsMapper.java index dd0d8147..f34649c2 100644 --- a/client/client/src/main/java/org/wso2/iot/agent/services/PolicyOperationsMapper.java +++ b/client/client/src/main/java/org/wso2/iot/agent/services/PolicyOperationsMapper.java @@ -25,7 +25,7 @@ /** * This class is used to create specific operation with respect to - * recieved policy payload. + * received policy payload. */ public class PolicyOperationsMapper { diff --git a/client/client/src/main/java/org/wso2/iot/agent/services/PolicyRevokeHandler.java b/client/client/src/main/java/org/wso2/iot/agent/services/PolicyRevokeHandler.java index 2a5499dc..597dcc38 100644 --- a/client/client/src/main/java/org/wso2/iot/agent/services/PolicyRevokeHandler.java +++ b/client/client/src/main/java/org/wso2/iot/agent/services/PolicyRevokeHandler.java @@ -17,6 +17,7 @@ package org.wso2.iot.agent.services; +import android.annotation.TargetApi; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; @@ -38,13 +39,15 @@ import org.wso2.iot.agent.utils.Preference; import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; /** * This class is used to revoke the existing policy on the device. */ public class PolicyRevokeHandler { - private static final String TAG = PolicyOperationsMapper.class.getSimpleName(); + private static final String TAG = PolicyRevokeHandler.class.getSimpleName(); private Context context; private DevicePolicyManager devicePolicyManager; private Resources resources; @@ -204,6 +207,9 @@ public void revokeExistingPolicy(org.wso2.iot.agent.beans.Operation operation) case Constants.Operation.COSU_PROFILE_POLICY: revokeCOSUProfilePolicy(operation); break; + case Constants.Operation.WORK_PROFILE: + revokeWorkProfile(operation); + break; default: Log.e(TAG, "Operation code: " + operation.getCode() + " is not supported"); //throw new AndroidAgentException("Invalid operation code received"); @@ -211,6 +217,67 @@ public void revokeExistingPolicy(org.wso2.iot.agent.beans.Operation operation) } } + private void revokeWorkProfile(Operation operation) throws AndroidAgentException { + String enableSystemAppsData; + String hideSystemAppsData; + String unhideSystemAppsData; + String googlePlayAppsData; + try { + JSONObject profileData = new JSONObject(operation.getPayLoad().toString()); + if (!profileData.isNull(resources.getString(R.string.intent_extra_enable_system_apps))) { + enableSystemAppsData = (String) profileData.get(resources.getString( + R.string.intent_extra_enable_system_apps)); + List systemAppList = Arrays.asList(enableSystemAppsData.split(resources.getString( + R.string.split_delimiter))); + for (String packageName : systemAppList) { + setApplicationHidden(packageName, true); + } + } + if (!profileData.isNull(resources.getString(R.string.intent_extra_hide_system_apps))) { + hideSystemAppsData = (String) profileData.get(resources.getString( + R.string.intent_extra_hide_system_apps)); + List systemAppList = Arrays.asList(hideSystemAppsData.split(resources.getString( + R.string.split_delimiter))); + for (String packageName : systemAppList) { + setApplicationHidden(packageName, false); + } + } + if (!profileData.isNull(resources.getString(R.string.intent_extra_unhide_system_apps))) { + unhideSystemAppsData = (String) profileData.get(resources.getString( + R.string.intent_extra_unhide_system_apps)); + List systemAppList = Arrays.asList(unhideSystemAppsData.split(resources.getString( + R.string.split_delimiter))); + for (String packageName : systemAppList) { + setApplicationHidden(packageName, true); + } + } + if (!profileData.isNull(resources.getString(R.string.intent_extra_enable_google_play_apps))) { + googlePlayAppsData = (String) profileData.get(resources.getString( + R.string.intent_extra_enable_google_play_apps)); + List systemAppList = Arrays.asList(googlePlayAppsData.split(resources.getString( + R.string.split_delimiter))); + for (String packageName : systemAppList) { + uninstallGooglePlayApps(packageName); + } + } + } catch (JSONException e) { + Log.e(TAG, "Error in revoking work profile configuration policy.", e); + } + } + + private void uninstallGooglePlayApps(String packageName) { + try { + applicationManager.uninstallApplication(packageName, null); + } catch (AndroidAgentException e) { + Log.e(TAG, "Error while trying to uninstall app for revoke work profile policy.", e); + } + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + private void setApplicationHidden(String packageName, boolean isHidden) { + devicePolicyManager.setApplicationHidden(deviceAdmin, packageName, isHidden); + } + /** * Revokes camera policy on the device. * diff --git a/client/client/src/main/java/org/wso2/iot/agent/services/operation/OperationManagerWorkProfile.java b/client/client/src/main/java/org/wso2/iot/agent/services/operation/OperationManagerWorkProfile.java index 44205cca..0fe44813 100644 --- a/client/client/src/main/java/org/wso2/iot/agent/services/operation/OperationManagerWorkProfile.java +++ b/client/client/src/main/java/org/wso2/iot/agent/services/operation/OperationManagerWorkProfile.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except @@ -18,10 +18,7 @@ package org.wso2.iot.agent.services.operation; import android.annotation.TargetApi; -import android.app.AlarmManager; -import android.app.PendingIntent; import android.content.Context; -import android.content.Intent; import android.os.Build; import android.support.annotation.RequiresApi; import android.util.Log; @@ -35,7 +32,6 @@ import org.wso2.iot.agent.beans.ComplianceFeature; import org.wso2.iot.agent.beans.Notification; import org.wso2.iot.agent.beans.Operation; -import org.wso2.iot.agent.services.AppLockService; import org.wso2.iot.agent.services.NotificationService; import org.wso2.iot.agent.utils.CommonUtils; import org.wso2.iot.agent.utils.Constants; @@ -43,7 +39,6 @@ import java.util.ArrayList; import java.util.Arrays; -import java.util.Calendar; import java.util.List; import java.util.Objects; @@ -255,7 +250,7 @@ public void hideApp(Operation operation) throws AndroidAgentException { getResultBuilder().build(operation); if (packageName != null && !packageName.isEmpty()) { - getDevicePolicyManager().setApplicationHidden(getCdmDeviceAdmin(), packageName, true); + setApplicationHidden(packageName, true); } if (Constants.DEBUG_MODE_ENABLED) { @@ -283,7 +278,7 @@ public void unhideApp(Operation operation) throws AndroidAgentException { getResultBuilder().build(operation); if (packageName != null && !packageName.isEmpty()) { - getDevicePolicyManager().setApplicationHidden(getCdmDeviceAdmin(), packageName, false); + setApplicationHidden(packageName, false); } if (Constants.DEBUG_MODE_ENABLED) { @@ -405,6 +400,7 @@ public void configureWorkProfile(Operation operation) throws AndroidAgentExcepti R.string.split_delimiter))); for (String packageName : systemAppList) { enableSystemApp(packageName); + setApplicationHidden(packageName, false); } } if (!profileData.isNull(getContextResources().getString(R.string.intent_extra_hide_system_apps))) { @@ -415,7 +411,7 @@ public void configureWorkProfile(Operation operation) throws AndroidAgentExcepti List systemAppList = Arrays.asList(hideSystemAppsData.split(getContextResources().getString( R.string.split_delimiter))); for (String packageName : systemAppList) { - hideSystemApp(packageName); + setApplicationHidden(packageName, true); } } if (!profileData.isNull(getContextResources().getString(R.string.intent_extra_unhide_system_apps))) { @@ -426,7 +422,7 @@ public void configureWorkProfile(Operation operation) throws AndroidAgentExcepti List systemAppList = Arrays.asList(unhideSystemAppsData.split(getContextResources().getString( R.string.split_delimiter))); for (String packageName : systemAppList) { - enableSystemApp(packageName); + setApplicationHidden(packageName, false); } } if (!profileData.isNull(getContextResources().getString(R.string.intent_extra_enable_google_play_apps))) { @@ -438,14 +434,12 @@ public void configureWorkProfile(Operation operation) throws AndroidAgentExcepti enableGooglePlayApps(packageName); } } - } catch (JSONException e) { operation.setStatus(getContextResources().getString(R.string.operation_value_error)); operation.setOperationResponse("Error in parsing WORK_PROFILE payload."); getResultBuilder().build(operation); throw new AndroidAgentException("Invalid JSON format.", e); } - } @Override @@ -463,7 +457,7 @@ public void restrictAccessToApplications(Operation operation) throws AndroidAgen if (Constants.AppRestriction.BLACK_LIST.equals(appRestriction.getRestrictionType())) { String disallowedApps = ""; for (String packageName : appRestriction.getRestrictedList()) { - getDevicePolicyManager().setApplicationHidden(getCdmDeviceAdmin(), packageName, true); + setApplicationHidden(packageName, true); disallowedApps = disallowedApps + getContext().getString(R.string.whitelist_package_split_regex) + packageName; } Preference.putString(getContext(), @@ -520,7 +514,7 @@ private void validateInstalledApps() { } if (!isAllowed) { disallowedApps = disallowedApps + getContext().getString(R.string.whitelist_package_split_regex) + packageName; - getDevicePolicyManager().setApplicationHidden(getCdmDeviceAdmin(), packageName, true); + setApplicationHidden(packageName, true); } isAllowed = false; } @@ -545,11 +539,6 @@ private void enableSystemApp(String packageName) { } } - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - private void hideSystemApp(String packageName) { - getDevicePolicyManager().setApplicationHidden(getCdmDeviceAdmin(), packageName, true); - } - @Override public void setPolicyBundle(Operation operation) throws AndroidAgentException { getResultBuilder().build(operation); @@ -696,6 +685,11 @@ public ComplianceFeature checkWorkProfilePolicy(Operation operation, ComplianceF return policy; } + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + private void setApplicationHidden(String packageName, boolean isHidden) { + getDevicePolicyManager().setApplicationHidden(getCdmDeviceAdmin(), packageName, isHidden); + } + @Override public ComplianceFeature checkRuntimePermissionPolicy(Operation operation, ComplianceFeature policy) throws AndroidAgentException { policy.setCompliance(true);