Detect devices running MDM (mobile device management)
Asked Answered
P

2

5

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?

Propitiatory answered 26/7, 2019 at 4:4 Comment(0)
M
6

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

Mcclure answered 30/7, 2019 at 10:18 Comment(0)
H
1

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.

Hautemarne answered 30/7, 2019 at 5:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.