Search code examples
androidpermissionsadmin

Android: How to ask for only a subset of DEVICE_ADMIN permissions


I got these two in my Manifest.xml:

<android:permission="android.permission.BIND_DEVICE_ADMIN">
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>

Meta-data:

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

And this in my MainActivity:

Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Click");
startActivityForResult(intent, REQUEST_CODE);

This works nicely, but in the Activity where the user should grant admin rights, the system lists a lot of things I don't want to do, like "Erase all data". Can I ask for only a subset of the admin rights, like "Lock Screen"?

Thanks.


Solution

  • The <receiver> element for your subclass of DeviceAdminReceiver should have a <meta-data> element, with a name of android.app.device_admin. This will point to an XML resource, listing the specific policies that you want. My interpretation of your question and comment is that you started with a resource that listed a bunch of policies, such as:

    <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 />
        <disable-camera />
      </uses-policies>
    </device-admin>
    

    You can trim back the list to just the policies that you need. Anything else will be unavailable to you, even if the user makes your app be a device administrator. Unfortunately, the roster of possible XML elements seems to be undocumented at the present time.