5

Given a View, how can I determine if that View is being shown in a Window that has FLAG_SECURE?

In the simple case, where the View is shown directly in the window for an Activity, we can get that Window, then call getAttributes().flags and see if those flags include FLAG_SECURE.

However:

  • That may not be accurate if the View is being shown in some child window of the activity (the activity might have a secure window but the child window might not be secure)

  • It will not work for non-activity windows (e.g., a Service and SYSTEM_ALERT_WINDOW)

Is there a technique that can handle those edge cases as well?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • you might be looking for attribute flag `0x20000` (`FLAG_SECURE`). This [question](https://stackoverflow.com/questions/45928313/how-to-query-flag-secure-from-current-apk-screen-via-command-line) is similar, but it's `adb shell dumpsys`. – Martin Zeitler Nov 03 '19 at 14:28
  • @MartinZeitler: I do not know how to get to those flags from a `View`, other than by trying to look at the `Activity` associated with that `View`. That misses the edge cases I mention in the bullets. – CommonsWare Nov 03 '19 at 14:52
  • 1
    what they all have in common is a parent `Window`; even something displayed in a child-window has a parent `Window`. So one would need to find the immediate parent `Window` of a `View`. – Martin Zeitler Nov 03 '19 at 15:05
  • @CommonsWare If it's something important to be shown on screen. Why we cannot use custom toast with `View`, which would be shown on the secure `Window`, and can work within context only. – GensaGames Nov 06 '19 at 17:49

1 Answers1

3

The View.AttachInfo, which contains all relevant window information, only provides access to the window token, session and id. The window itself is nowhere referenced within View.

But as the flag itself is defined as Display.FLAG_SECURE, you would want to look for the display flags. You can easily access these with View.getDisplay().

Gets the logical display to which the view's window has been attached.

The Display.getFlags() itself should contain the FLAG_SECURE.

tynn
  • 38,113
  • 8
  • 108
  • 143
  • Note that `getDisplay()` only works sometime after `onCreate()` of the activity returns. `getDisplay()` will return `null` from inside of `onCreate()`. That could pose some problems in some situations, but the bulk of the time this approach should work. Thanks! – CommonsWare Nov 10 '19 at 18:20