kos
kos

Reputation: 1801

Detect devices running MDM (mobile device management)

I have a feature to pop up an app update prompt dialog in old app versions (the version is controlled via Firebase Remote Config).

However, turns out a lot (but not most) of my customers use MDM to lock down their phone, in which case the end users cannot directly update the app.

I'm using the normal detection of intent.resolveActivity(packageManager) to check if the play store activity can be started before showing the dialog. But that check passes - the Play Store is there, but updates are blocked.

I want to disable the update prompt for these end users.

Is there a way to detect MDMs? Or at least a way to detect that app updates have been blocked?

Upvotes: 5

Views: 3713

Answers (2)

Philippe Banwarth
Philippe Banwarth

Reputation: 17755

The Test DPC google sample contains a ProvisioningStateUtil with a few utility methods :

/**
 * @return true if the device or profile is already owned
 */
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;
}

This require at least Android 5.0 (API 21)

You can also directly check if the current user is allowed to install or update applications :

UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
boolean blocked = userManager.hasUserRestriction(UserManager.DISALLOW_INSTALL_APPS);

It also require API 21

Upvotes: 7

Paulo Ribeiro
Paulo Ribeiro

Reputation: 153

You can use UserManager:

UserManager user_manager = (UserManager) getSystemService(Context.USER_SERVICE);
    if (user_manager.hasUserRestriction("WORK_PROFILE_RESTRICTION")) {
        System.out.print("app under MDM");
     } else {
         System.out.print("app not under MDM");
     }

Although if the admin did not set-up the value you will receive false. If you get true it means that you are definitely under a managed profile, but if you get false it can be a false negative.

Upvotes: 1

Related Questions