buildAllWindowTrees

fun buildAllWindowTrees(): List<ApiUiNode>

Returns one ApiUiNode root per interactive window currently visible on screen (app + system overlays: dialogs, IME, autofill, status/nav bar).

Ordering: root with packageName == targetPackage comes first; remaining roots follow in the order returned by UiAutomation.getWindows() (Android's z-order). This keeps the app root in slot 0 across frames so older desktops that render only the first root remain functional.

Per-window failures are tolerated: if one window throws or yields null root (e.g. secure window, dead window), it is skipped and the rest are returned.

Fallback: if getWindows() returned empty or every window yielded null root, falls back to rootInActiveWindow() to preserve some output on legacy devices where windows enumeration is unreliable.

Recovery flow after UiAutomation dies (unchanged from pre-multi-window):

  1. Initial grace period (2s) — complete silence, no IPC calls at all.

  2. Poll phase — check serviceInfo every 500ms.

  3. Once serviceInfo != null — re-apply FLAG and attempt full tree build.