Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 546
Jędrzej Dudkiewicz Avatar asked Sep 01 '25 22:09

Jędrzej Dudkiewicz


1 Answers

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

  • Either setting android:filterTouchesWhenObscured="true" in your layout (touch events will be filtered and not reach your View if they are going through an overlay, regardless is transparent or opaque). See View#setFilterTouchesWhenObscured(boolean),
  • Or overriding View#onFilterTouchEventForSecurity(android.view.MotionEvent) and checking for FLAG_WINDOW_IS_OBSCURED. See View#onFilterTouchEventForSecurity(android.view.MotionEvent).

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.

like image 138
Xavier Rubio Jansana Avatar answered Sep 03 '25 16:09

Xavier Rubio Jansana