Skip to content

Commit

Permalink
Improvements for Work profile policy flow
Browse files Browse the repository at this point in the history
  • Loading branch information
charithag committed Jan 11, 2018
1 parent 3fee00c commit 439c4f9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private void revokeWorkProfile(Operation operation) throws AndroidAgentException
List<String> systemAppList = Arrays.asList(enableSystemAppsData.split(resources.getString(
R.string.split_delimiter)));
for (String packageName : systemAppList) {
hideSystemApp(packageName);
setApplicationHidden(packageName, true);
}
}
if (!profileData.isNull(resources.getString(R.string.intent_extra_hide_system_apps))) {
Expand All @@ -239,7 +239,7 @@ private void revokeWorkProfile(Operation operation) throws AndroidAgentException
List<String> systemAppList = Arrays.asList(hideSystemAppsData.split(resources.getString(
R.string.split_delimiter)));
for (String packageName : systemAppList) {
enableSystemApp(packageName);
setApplicationHidden(packageName, false);
}
}
if (!profileData.isNull(resources.getString(R.string.intent_extra_unhide_system_apps))) {
Expand All @@ -248,7 +248,7 @@ private void revokeWorkProfile(Operation operation) throws AndroidAgentException
List<String> systemAppList = Arrays.asList(unhideSystemAppsData.split(resources.getString(
R.string.split_delimiter)));
for (String packageName : systemAppList) {
hideSystemApp(packageName);
setApplicationHidden(packageName, true);
}
}
if (!profileData.isNull(resources.getString(R.string.intent_extra_enable_google_play_apps))) {
Expand All @@ -274,17 +274,8 @@ private void uninstallGooglePlayApps(String packageName) {
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void enableSystemApp(String packageName) {
try {
devicePolicyManager.enableSystemApp(deviceAdmin, packageName);
} catch (IllegalArgumentException e) {
Log.e(TAG, "App is not available on the device to enable. " + e.toString());
}
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void hideSystemApp(String packageName) {
devicePolicyManager.setApplicationHidden(deviceAdmin, packageName, true);
private void setApplicationHidden(String packageName, boolean isHidden) {
devicePolicyManager.setApplicationHidden(deviceAdmin, packageName, isHidden);
}

/**
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 439c4f9

Please sign in to comment.