Example usage for android.app.admin DevicePolicyManager isDeviceOwnerApp

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

Introduction

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

Prototype

public boolean isDeviceOwnerApp(String packageName) 

Source Link

Document

Used to determine if a particular package has been registered as a Device Owner app.

Usage

From source file:com.afwsamples.testdpc.common.Util.java

@TargetApi(VERSION_CODES.LOLLIPOP)
public static boolean isDeviceOwner(Context context) {
    final DevicePolicyManager dpm = getDevicePolicyManager(context);
    return dpm.isDeviceOwnerApp(context.getPackageName());
}

From source file:Main.java

/**
 * @return true if the device or profile is already owned by TestDPC
 *///from  w  w w .  j  av a 2s .co m
public static boolean isManagedByTestDPC(Context context) {
    DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context
            .getSystemService(Context.DEVICE_POLICY_SERVICE);
    String packageName = context.getPackageName();

    return devicePolicyManager.isProfileOwnerApp(packageName)
            || devicePolicyManager.isDeviceOwnerApp(packageName);
}

From source file:Main.java

/**
 * @return true if the device or profile is already owned
 *///from   www.  j  a  v a2 s  .  c  o  m
public static boolean isManaged(Context context) {
    DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context
            .getSystemService(Context.DEVICE_POLICY_SERVICE);

    List<ComponentName> admins = devicePolicyManager.getActiveAdmins();
    if (admins == null)
        return false;
    for (ComponentName admin : admins) {
        String adminPackageName = admin.getPackageName();
        if (devicePolicyManager.isDeviceOwnerApp(adminPackageName)
                || devicePolicyManager.isProfileOwnerApp(adminPackageName)) {
            return true;
        }
    }

    return false;
}

From source file:com.afwsamples.testdpc.common.preference.DpcPreferenceHelper.java

private int getCurrentAdmin() {
    final DevicePolicyManager dpm = (DevicePolicyManager) mContext
            .getSystemService(Context.DEVICE_POLICY_SERVICE);
    final String packageName = mContext.getPackageName();

    if (dpm.isDeviceOwnerApp(packageName)) {
        return ADMIN_DEVICE_OWNER;
    }/*from w w w .ja v  a  2 s  .c  o  m*/
    if (dpm.isProfileOwnerApp(packageName)) {
        return ADMIN_PROFILE_OWNER;
    }
    return ADMIN_NONE;
}

From source file:com.afwsamples.testdpc.policy.PolicyManagementFragment.java

@Override
public boolean isAvailable(Context context) {
    DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
    String packageName = context.getPackageName();
    return dpm.isProfileOwnerApp(packageName) || dpm.isDeviceOwnerApp(packageName);
}

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.
 *//*from   w  w w .  jav a2s .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..");
        }
    }
}