Syzygy
Syzygy

Reputation: 608

Automatically grant ACTION_MANAGE_OVERLAY_PERMISSION via setPermissionGrantState or similar

I'm trying to grant some runtime permissions to my app automatically, these include ACCESS_FINE_LOCATION, READ_PHONE_STATE as well as ACTION_MANAGE_OVERLAY_PERMISSION. Do note that this requires at least Device Administrator access rights, which I have.

While the first two work flawlessly via

  dpm.setPermissionGrantState(componentName, "com.my.app", Manifest.permission.READ_PHONE_STATE, DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
  dpm.setPermissionGrantState(componentName, "com.my.app", Manifest.permission.ACCESS_FINE_LOCATION, DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);

ACTION_MANAGE_OVERLAY_PERMISSION does not work at all. I believe this might be due to the fact that it's not part of Manifest.permission but instead android.settings.action.MANAGE_OVERLAY_PERMISSION. However it still is a permission that I want to be granted automatically.

Edit: While it seems that this permission is granted automatically to any app that requests it, this only is the case for apps distributed via the playstore. Unfortunately my App is NOT distributed that way.

Upvotes: 4

Views: 3349

Answers (2)

tagy22
tagy22

Reputation: 1360

I was able to automatically grant this permission, from my device owner app:

        devicePolicyManager.setPermissionGrantState(compName, this.packageName, Manifest.permission.SYSTEM_ALERT_WINDOW, DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED)

You also need in your manifest:

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

After this Settings.canDrawOverlays() returns true, and the permission is granted in the settings app. Although unlike other permissions granted this way, it seems like the user can choose to disable it in settings. My targetSdkVersion is 26

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007474

include ACCESS_FINE_LOCATION, READ_PHONE_STATE as well as ACTION_MANAGE_OVERLAY_PERMISSION

ACTION_MANAGE_OVERLAY_PERMISSION is not a permission.

but instead android.settings.action.MANAGE_OVERLAY_PERMISSION

That is not a permission. That is an Intent action.

The permission that you probably want is SYSTEM_ALERT_WINDOW.

Upvotes: 2

Related Questions