Skip to content

Commit

Permalink
Merge pull request #134 from charithag/master
Browse files Browse the repository at this point in the history
  • Loading branch information
inoshperera authored Jan 11, 2018
2 parents 6d52583 + 439c4f9 commit fb85166
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

/**
* This class is used to create specific operation with respect to
* recieved policy payload.
* received policy payload.
*/
public class PolicyOperationsMapper {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -204,13 +207,77 @@ 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");
}
}
}

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<String> 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<String> 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<String> 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<String> 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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -35,15 +32,13 @@
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;
import org.wso2.iot.agent.utils.Preference;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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))) {
Expand All @@ -415,7 +411,7 @@ public void configureWorkProfile(Operation operation) throws AndroidAgentExcepti
List<String> 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))) {
Expand All @@ -426,7 +422,7 @@ public void configureWorkProfile(Operation operation) throws AndroidAgentExcepti
List<String> 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))) {
Expand All @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fb85166

Please sign in to comment.