Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require a password before disabling application as admin in Android

I'm developing an application which runs as an Admin. I'm able to do it using the following code

DemoDeviceAdminReceiver.jav:

public class DemoDeviceAdminReceiver extends DeviceAdminReceiver {
    static final String TAG = "DemoDeviceAdminReceiver";

    /** Called when this application is approved to be a device administrator. */
    @Override
    public void onEnabled(Context context, Intent intent) {
        super.onEnabled(context, intent);

        Toast.makeText(context, R.string.device_admin_enabled, Toast.LENGTH_LONG).show();
        Log.d(TAG, "onEnabled");
    }

    /** Called when disabling device administrator power. */
    @Override
    public CharSequence onDisableRequested(Context context, Intent intent) {
        Toast.makeText(context, R.string.device_admin_requesting_disable, Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDisableRequested");

        return super.onDisableRequested(context, intent);
    }

    /** Called when this application is no longer the device administrator. */
    @Override
    public void onDisabled(Context context, Intent intent) {
        super.onDisabled(context, intent);
        Toast.makeText(context, R.string.device_admin_disabled, Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDisabled");
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        super.onPasswordChanged(context, intent);
        Log.d(TAG, "onPasswordChanged");
    }

    @Override
    public void onPasswordFailed(Context context, Intent intent) {
        super.onPasswordFailed(context, intent);
        Log.d(TAG, "onPasswordFailed");
    }

    @Override
    public void onPasswordSucceeded(Context context, Intent intent) {
        super.onPasswordSucceeded(context, intent);
        Log.d(TAG, "onPasswordSucceeded");
    }
}

Manifest:

<receiver
    android:name=".DemoDeviceAdminReceiver"
    android:permission="android.permission.BIND_DEVICE_ADMIN" >
    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
    </intent-filter>

    <meta-data
        android:name="android.app.device_admin"
        android:resource="@xml/device_admin_sample" />
</receiver>

res/xml/device_admin_sample.xml:

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <limit-password />
        <watch-login />
        <reset-password />
        <force-lock />
        <wipe-data />
        <expire-password />
        <encrypted-storage />
    </uses-policies>
</device-admin>

Every thing works fine, but I don't know how to ask for a password whenever the user clicks on disable the app from admin rights.

Please help to solve this big riddle. Thanks

like image 597
Chethan Shetty Avatar asked Nov 12 '22 18:11

Chethan Shetty


1 Answers

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 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.

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, the cancel button and an ok button. Only if they then press Ok will the permission be disabled.

like image 153
Syzygy Avatar answered Nov 15 '22 04:11

Syzygy