Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect devices running MDM (mobile device management)

Tags:

android

mdm

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?

like image 670
kos Avatar asked Mar 04 '23 16:03

kos


1 Answers

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

like image 104
bwt Avatar answered Mar 17 '23 06:03

bwt