Reputation: 137
so here's the problem:
I'm trying to create a Device Admin app where when the user attempts to disable the admin functionality of the app, he is prompted for a password, and if he fails to input the correct password, the app keeps admin privileges.
I've seen an app called AppLock do just this, and it works great! I have experimented with locking the screen from the onDisableRequested() method as suggested in this SO question, but the admin app is disabled anyway.
I'm assuming AppLock locks the screen with a password activity when the user tries to disable the app as admin, but I don't understand how it can stop the app from being disabled on a wrong password, since the app is already in the process of being disabled when the password activity is launched. Is there a hook in a password activity to halt certain processes if the password is incorrect?
I'm also confused about how to implement the custom password lock screen that AppLock is using. How can I temporarily change the lock screen type with a custom password lock screen, then revert to the user's original lock screen type?
These are some other SO questions which were similar to my question:
How to prevent a user from disabling my device admin app, through the settings menu?
Require a password before disabling application as admin in Android
Upvotes: 4
Views: 4991
Reputation: 608
Couple of years late, but I just ran into the same problem and was able to solve it. Maybe the OP won't need the answer, but others - like me - might stumble over this in search for a solution.
Keep in mind that this method only works if the user can NOT unlock the screen.
All you need to do is lock the screen in DeviceAdminReceiver's onDisableRequested method, so that the user will have to enter their password for unlocking. Of course this only works if the phone has some form of screen lock(pin, password, fingerprint,...). Be wary of smart lock.
Keep in mind that this method is intended to display a warning explaining why the user should not disable permissions. If null (the default from super) is returned, nothing is displayed and the permission is disabled anyway. So if we return any text, a dialog with that text as well as an ok and a cancel button will be displayed.
If you combine that with the screen lock like this:
public CharSequence onDisableRequested(Context context, Intent intent) {
DevicePolicyManager deviceManger = (DevicePolicyManager)context.getSystemService(
Context.DEVICE_POLICY_SERVICE);
deviceManger.lockNow();
return "Your warning";
}
the screen will turn off when the user tries to disable the permission, when they unlock the screen, they're required to enter their password. Once they did that they see a dialog with "Your warning", a cancel button and an ok button. Only if they then press Ok will the permission be disabled.
Upvotes: 2