4

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.

Jędrzej Dudkiewicz
  • 1,053
  • 8
  • 21
  • What are you trying to get? Your app being always on top? Or you're trying to make sure no app is interfering with yours for security reasons (e.g. a rogue app that overlays yours in order to capture a PIN number)? – Xavier Rubio Jansana Nov 08 '17 at 11:20
  • Mainly prevent "cloak and dagger" attack, and preventing overlays which could potentially hide important information for application user (and third parties, for example client of the user). – Jędrzej Dudkiewicz Nov 08 '17 at 11:36
  • Also, I'm aware that what I'm looking for is basically TrustedUI, but it seems that available solutions are basically prohibitively expensive. – Jędrzej Dudkiewicz Nov 08 '17 at 11:38

1 Answers1

2

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.

Xavier Rubio Jansana
  • 6,388
  • 1
  • 27
  • 50
  • Thanks a lot, is there any way to detect overlay without depending on user interaction? I can add requirement for user to physically touch the screen, but it would be great if it wasn't necessary. – Jędrzej Dudkiewicz Nov 08 '17 at 13:39
  • Apparently this answer promises that, but I couldn't make it really detect if there was an overlay on top of my app, but just the opposite (if my app is able to draw an overlay on top of others) https://stackoverflow.com/questions/42429635/how-to-detect-screen-overlay-in-android So, from my tests is not possible, but if you find a way I would love to know. :) – Xavier Rubio Jansana Nov 08 '17 at 13:42
  • Thanks a lot. I will keep this one open without accepting your answer for some time, and if no one will provide a better answer I'll accept it. – Jędrzej Dudkiewicz Nov 08 '17 at 13:49
  • Fair enough. Thanks! – Xavier Rubio Jansana Nov 08 '17 at 13:51