Example usage for android.app.admin DevicePolicyManager setApplicationHidden

List of usage examples for android.app.admin DevicePolicyManager setApplicationHidden

Introduction

In this page you can find the example usage for android.app.admin DevicePolicyManager setApplicationHidden.

Prototype

public boolean setApplicationHidden(@NonNull ComponentName admin, String packageName, boolean hidden) 

Source Link

Document

Hide or unhide packages.

Usage

From source file:com.example.android.apprestrictionenforcer.StatusFragment.java

/**
 * Unhides the AppRestrictionSchema sample in case it is hidden in this profile.
 *
 * @param activity The activity//w ww.  j  a  v a 2  s .c  o m
 */
private void unhideApp(Activity activity) {
    DevicePolicyManager devicePolicyManager = (DevicePolicyManager) activity
            .getSystemService(Activity.DEVICE_POLICY_SERVICE);
    devicePolicyManager.setApplicationHidden(EnforcerDeviceAdminReceiver.getComponentName(activity),
            Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA, false);
    Toast.makeText(activity, "Enabled the app", Toast.LENGTH_SHORT).show();
    mListener.onStatusUpdated();
}

From source file:org.wso2.iot.agent.events.listeners.ApplicationStateListener.java

/**
 * This method will check if the app just installed is allowed if a app white-listing policy is enforced.
 *//*  w  ww .  j  av  a 2 s. c  o  m*/
private void applyEnforcement(String packageName) {
    DevicePolicyManager devicePolicyManager;
    ComponentName cdmfDeviceAdmin;
    devicePolicyManager = (DevicePolicyManager) context.getApplicationContext()
            .getSystemService(Context.DEVICE_POLICY_SERVICE);
    cdmfDeviceAdmin = AgentDeviceAdminReceiver.getComponentName(context.getApplicationContext());
    String permittedPackageName;
    JSONObject permittedApp;
    String permissionName;
    Boolean isAllowed = false;
    String whiteListAppsPref = Preference.getString(context, Constants.AppRestriction.WHITE_LIST_APPS);
    String ownershipType = Preference.getString(context, Constants.DEVICE_TYPE);
    if (!whiteListAppsPref.equals("")) {
        try {
            JSONArray whiteListApps = new JSONArray(whiteListAppsPref);
            for (int i = 0; i < whiteListApps.length(); i++) {
                permittedApp = new JSONObject(whiteListApps.getString(i));
                permittedPackageName = permittedApp.getString(Constants.AppRestriction.PACKAGE_NAME);
                if (permittedPackageName.equals(packageName)) {
                    permissionName = permittedApp.getString(Constants.AppRestriction.RESTRICTION_TYPE);
                    if (permissionName.equals(Constants.AppRestriction.WHITE_LIST)) {
                        isAllowed = true;
                        break;
                    }
                }
            }
            if (!isAllowed) {
                String disallowedApps = Preference.getString(context, Constants.AppRestriction.DISALLOWED_APPS);
                disallowedApps = disallowedApps + context.getString(R.string.whitelist_package_split_regex)
                        + packageName;
                Preference.putString(context, Constants.AppRestriction.DISALLOWED_APPS, disallowedApps);
                //Calls devicePolicyManager if the agent is profile-owner or device-owner.
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
                        && (devicePolicyManager.isProfileOwnerApp(cdmfDeviceAdmin.getPackageName())
                                || devicePolicyManager.isDeviceOwnerApp(cdmfDeviceAdmin.getPackageName()))) {
                    devicePolicyManager.setApplicationHidden(cdmfDeviceAdmin, packageName, true);
                } else if (Constants.OWNERSHIP_COPE.equals(ownershipType)) {
                    CommonUtils.callSystemApp(context, Constants.Operation.APP_RESTRICTION, "false",
                            packageName);
                }
            }
        } catch (JSONException e) {
            Log.e(TAG, "Invalid JSON format..");
        }
    }
}