4

Is there any way to detect whether there are any other apps drawing over your app (such as a screen filter) while your app is open programmatically? I want to be able to detect them so I can inform the user to turn them off in my app.

The one thing I thought of doing was using something similar to this solution https://stackoverflow.com/a/27140347/6854141 which uses the ActivityManager methods to detect the current foreground app/service and see if it isn't my application's package or potentially look for certain packages that I know would cause this issue.

However, this method involves various accommodations for pre Lollipop, Lollipop, Android M, and currently does not work for Android N--it's essential that the solution I implement works for Android N. I'm also not sure where something like a screen filter would fall in terms of importance status.

Cœur
  • 37,241
  • 25
  • 195
  • 267
jcarnide
  • 113
  • 6
  • "which uses the ActivityManager methods to detect the current foreground app/service" -- first, there may be many foreground app/services, none of which necessarily represent an app drawing over yours. Even the concept of "drawing over yours" will have issues in multi-window environments (Chrome OS, future Android freeform multi-window devices). – CommonsWare Oct 19 '16 at 15:20
  • @CommonsWare: Forgetting the multi-window environments for a second, do you have a suggestion for the "drawing over" case? – jcarnide Oct 19 '16 at 15:24
  • I know of no reliable means of detecting active drawing over your app, and all approaches to reliably detect other running apps should be broken now, for privacy and security reasons. You can determine what apps have requested the `SYSTEM_ALERT_WINDOW` permission, but that's about it. – CommonsWare Oct 19 '16 at 15:27
  • @CommonsWare: Thank you for the suggestion, I'll look into it :) – jcarnide Oct 21 '16 at 20:00
  • 1
    FWIW, [this sample app](https://github.com/commonsguy/cw-omnibus/tree/master/Introspection/SAWMonitor) raises a `Notification` whenever an app is installed or updated and that app has requested `SYSTEM_ALERT_WINDOW`. – CommonsWare Oct 21 '16 at 20:03
  • @CommonsWare: I had a look at the sample application you sent me. Something I found weird was that using `PackageManager`'s `checkPermission()` function returns `PERMISSION_GRANTED` for the `SYSTEM_ALERT_WINDOW` permission for all packages that initially had it granted but then had that permission revoked manually via the `Draw over apps` section of the Android settings. Is there a reason for this? If so, is there a way I can get a more accurate list of apps that CURRENTLY have the ability to draw over other apps? – jcarnide Oct 31 '16 at 18:09
  • "Is there a reason for this?" -- I would not expect that to work for that permission. "If so, is there a way I can get a more accurate list of apps that CURRENTLY have the ability to draw over other apps?" -- off the cuff, no. You can try `Settings.canDrawOverlays()`, with a `Context` created via `createPackageContext()`. I'm not convinced that this will work, and you would need to iterate over all candidate package names. – CommonsWare Oct 31 '16 at 18:26
  • @CommonsWare: Thanks for the prompt response. No cigar with `Settings.canDrawOverlays()`. I would've though that would work, seeing as that's what applications would use to currently check if they have the ability to draw over other apps. I even tried `Context.checkSelfPermission()` and `ContextCompat.checkSelfPermission()` with no luck. – jcarnide Oct 31 '16 at 19:37
  • Well, the `checkSelfPermission()` calls would only check your app, not others, so that does not surprise me. – CommonsWare Oct 31 '16 at 19:39
  • @CommonsWare: I was invoking `checkSelfPermission()` from the `Context` object returned by `createPackageContext()`, not my own. – jcarnide Oct 31 '16 at 19:41
  • Ah. I suspect that both of those suffer from the same problem: the APIs probably use the process ID (and look up the package from there), not the package name in the `Context`. Leastways, that was my fear with the implementation of `canDrawOverlays()`, which is why I was unconvinced that it would work. – CommonsWare Oct 31 '16 at 19:46

0 Answers0