4

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.

Syzygy
  • 578
  • 7
  • 27

2 Answers2

2

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Unfortunately using SYSTEM_ALERT_WINDOW does not work. The code looks like this now dpm.setPermissionGrantState(componentName, "com.my.app", Manifest.permission.SYSTEM_ALERT_WINDOW, DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED); – Syzygy May 31 '17 at 13:53
  • @Syzygy: I have not used `setPermissionGrantState()`. What does "does not work" mean, specifically? Note that `SYSTEM_ALERT_WINDOW` is not a `dangerous` permission, and so if `setPermissionGrantState()` is for `dangerous` permissions, that would explain the problem. And, in my experience, it is automatically granted anyway, [much to my great irritation](https://commonsware.com/blog/2017/05/11/system_alert_window-updates.html). – CommonsWare May 31 '17 at 13:59
  • "does not work" means that this permission is not granted. In my case the permission is not granted automatically either. Not only that, but I also need to prevent the user from disabling this permission as well (I'm aware that this seems odd, but so does having device admin access (~COSU use case)). – Syzygy May 31 '17 at 14:15
1

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

tagy22
  • 1,353
  • 17
  • 26