Example usage for android.app.admin DevicePolicyManager isProfileOwnerApp

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

Introduction

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

Prototype

public boolean isProfileOwnerApp(String packageName) 

Source Link

Document

Used to determine if a particular package is registered as the profile owner for the user.

Usage

From source file:Main.java

/**
 * @return true if the device or profile is already owned by TestDPC
 *//*from   w w  w.  j  a v  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:com.afwsamples.testdpc.common.Util.java

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

From source file:Main.java

/**
 * @return true if the device or profile is already owned
 *///from  www  . ja  va2  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.example.android.apprestrictionenforcer.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_real);
    if (null == savedInstanceState) {
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(
                Context.DEVICE_POLICY_SERVICE);
        PackageManager packageManager = getPackageManager();
        if (!devicePolicyManager.isProfileOwnerApp(getApplicationContext().getPackageName())) {
            // If the managed profile is not yet set up, we show the setup screen.
            showSetupProfile();/*from ww w.  ja  va2s.  c  o  m*/
        } else {
            try {
                ApplicationInfo info = packageManager.getApplicationInfo(
                        Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA, PackageManager.GET_UNINSTALLED_PACKAGES);
                if (0 == (info.flags & ApplicationInfo.FLAG_INSTALLED)) {
                    // Need to reinstall the sample app
                    showStatusProfile();
                } else if (devicePolicyManager.isApplicationHidden(
                        EnforcerDeviceAdminReceiver.getComponentName(this),
                        Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA)) {
                    // The app is installed but hidden in this profile
                    showStatusProfile();
                } else {
                    // Everything is clear; show the main screen
                    showMainFragment();
                }
            } catch (PackageManager.NameNotFoundException e) {
                showStatusProfile();
            }
        }
    }
}

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  www .ja  va  2  s.c om*/
    if (dpm.isProfileOwnerApp(packageName)) {
        return ADMIN_PROFILE_OWNER;
    }
    return ADMIN_NONE;
}

From source file:org.wso2.iot.agent.activities.AuthenticationActivity.java

/**
 * Start device admin activation request.
 *
 *///from  ww  w. j av a  2s .  co  m
private void startDeviceAdminPrompt() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP
            && devicePolicyManager.isProfileOwnerApp(getPackageName())) {
        checkManifestPermissions();
        CommonUtils.callSystemApp(context, null, null, null);
        Log.i("onActivityResult", "Administration enabled!");
    } else {
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(
                Context.DEVICE_POLICY_SERVICE);
        ComponentName cdmDeviceAdmin = new ComponentName(AuthenticationActivity.this,
                AgentDeviceAdminReceiver.class);
        if (!devicePolicyManager.isAdminActive(cdmDeviceAdmin)) {
            Intent deviceAdminIntent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            deviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cdmDeviceAdmin);
            deviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                    getResources().getString(R.string.device_admin_enable_alert));
            startActivityForResult(deviceAdminIntent, ACTIVATION_REQUEST);
        }
    }
}

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.
 *///w  ww  . j a va  2s  . co 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..");
        }
    }
}