Search code examples
androidfacebook-messengerruntime-permissions

How do screen overlays detect (and disable themselves) when a run-time permission is requested?


If an app requests that the user grants a run-time permission while a screen overlay is active, the system gives a warning that a screen overlay has been detected - the user then needs to manually deactivate the screen overlay until they can actually grant the permission. This kind of sucks from an UX perspective.

However recently I've seen some apps - notably Facebook Messenger (Chat Heads) - avoid this by simply disabling the screen overlay automatically when a permission is requested and enabling itself again once it has been granted (or not).

How are they doing this and how can I reproduce it?

I've not been able to find any callbacks or broadcasts that seem related to this.


Solution

  • It seems like this was simply a configuration issue as this works automatically with the correct configuration.

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                PixelFormat.RGBA_8888);
    

    Previously I used TYPE_SYSTEM_OVERLAY, but also played around with TYPE_SYSTEM_ALERT. This gave me some issues where it would not be touchable and not "dodge" the permission request. Or it would be touchable, but prevent the onscreen keyboard.

    With this configuration, the view is touchable, the onscreen keyboard works and the overlay view is hidden and displayed automatically when a permission is requested, avoiding the dreaded screen overlay detected message.