Search code examples
androidsecurityuser-interfacesystem-alert-window

Prevent application with SYSTEM_ALERT_WINDOW from obscuring my application


Is there any way to assure that my application's window is not obscured by any other application's view using with SYSTEM_ALERT_WINDOW permission?

If not, then is there any better way to assure that my app is not obscured by such window apart from obtaining the same permission and refreshing/showing/whatever my own view (of course shown in alert window) every 100ms or so to keep it visible?

Eventual flickering, in case my application is obscured, is actually a good thing and an indicator to the user that something is wrong.

EDIT: It seems that there is no way to do it except from going through KNOX on Samsung or some other proprietary solution for Trusted UI. Accepted answer is enough for my purpose, but it is not an answer for the question asked.


Solution

  • Even if it's not exactly what you're asking, the closest replacement I know of is:

    Later can be implemented like so:

    override fun onFilterTouchEventForSecurity(event: MotionEvent): Boolean {
        if ((event.flags and MotionEvent.FLAG_WINDOW_IS_OBSCURED) == MotionEvent.FLAG_WINDOW_IS_OBSCURED) {
            Toast.makeText(context, "Screen overlay detected!", Toast.LENGTH_LONG).show()
            return false // touch event is cancelled
        }
        return super.onFilterTouchEventForSecurity(event)
    }
    

    See also the Security section of View class documentation.

    Notice that this functionality is available from API 9+. A workaround for older APIs can be found in this SO Question: Analogue of android:filterTouchesWhenObscured for API level below 9.