A11y support for windows in a re-parented display

Currently, accessibility only supports windows in default display.
The windows in other displays aren't recongnized by accessibility
even they're re-parented to default display. Besides, we need to
offset the bound after re-parented since the original is from its
own display.

Bug: 129098348
Test: atest WindowStateTests
Test: atest DisplayContentTests
Test: a11y CTS & unit tests
Change-Id: I41a84a4c02e3c1be1dab4bd420d504b85787c4fb
diff --git a/services/core/java/com/android/server/wm/AccessibilityController.java b/services/core/java/com/android/server/wm/AccessibilityController.java
index b6a5be8..4cfc1d1 100644
--- a/services/core/java/com/android/server/wm/AccessibilityController.java
+++ b/services/core/java/com/android/server/wm/AccessibilityController.java
@@ -1065,8 +1065,6 @@
 
         private final long mRecurringAccessibilityEventsIntervalMillis;
 
-        private int mTempLayer = 0;
-
         public WindowsForAccessibilityObserver(WindowManagerService windowManagerService,
                 WindowsForAccessibilityCallback callback) {
             mContext = windowManagerService.mContext;
@@ -1091,7 +1089,7 @@
         }
 
         /**
-         * Check if windows have changed, and send them to the accessibilty subsystem if they have.
+         * Check if windows have changed, and send them to the accessibility subsystem if they have.
          *
          * @param forceSend Send the windows the accessibility even if they haven't changed.
          */
@@ -1108,8 +1106,7 @@
                 // the window manager is still looking for where to put it.
                 // We will do the work when we get a focus change callback.
                 // TODO(b/112273690): Support multiple displays
-                // TODO(b/129098348): Support embedded displays
-                if (mService.getDefaultDisplayContentLocked().mCurrentFocus == null) {
+                if (!isCurrentFocusWindowOnDefaultDisplay()) {
                     return;
                 }
 
@@ -1395,21 +1392,35 @@
         }
 
         private void populateVisibleWindowsOnScreenLocked(SparseArray<WindowState> outWindows) {
+            final List<WindowState> tempWindowStatesList = new ArrayList<>();
             final DisplayContent dc = mService.getDefaultDisplayContentLocked();
-            mTempLayer = 0;
             dc.forAllWindows((w) -> {
                 if (w.isVisibleLw()) {
-                    outWindows.put(mTempLayer++, w);
+                    tempWindowStatesList.add(w);
                 }
             }, false /* traverseTopToBottom */);
+            // Insert the re-parented windows in another display on top of their parents in
+            // default display.
             mService.mRoot.forAllWindows(w -> {
-                final WindowState win = findRootDisplayParentWindow(w);
-                if (win != null && win.getDisplayContent().isDefaultDisplay && w.isVisibleLw()) {
-                    // TODO(b/129098348): insert windows on child displays into outWindows based on
-                    // root-display-parent window.
-                    outWindows.put(mTempLayer++, w);
+                final WindowState parentWindow = findRootDisplayParentWindow(w);
+                if (parentWindow == null) {
+                    return;
                 }
-            }, false /* traverseTopToBottom */);
+
+                // TODO: Use Region instead to get rid of this complicated logic.
+                // Check the tap exclude region of the parent window. If the tap exclude region
+                // is empty, it means there is another can-receive-pointer-event view on top of
+                // the region. Hence, we don't count the window as visible.
+                if (w.isVisibleLw() && parentWindow.getDisplayContent().isDefaultDisplay
+                        && parentWindow.hasTapExcludeRegion()
+                        && tempWindowStatesList.contains(parentWindow)) {
+                    tempWindowStatesList.add(
+                            tempWindowStatesList.lastIndexOf(parentWindow) + 1, w);
+                }
+            }, true /* traverseTopToBottom */);
+            for (int i = 0; i < tempWindowStatesList.size(); i++) {
+                outWindows.put(i, tempWindowStatesList.get(i));
+            }
         }
 
         private WindowState findRootDisplayParentWindow(WindowState win) {
@@ -1425,6 +1436,23 @@
             return displayParentWindow;
         }
 
+        private boolean isCurrentFocusWindowOnDefaultDisplay() {
+            final WindowState focusedWindow =
+                    mService.mRoot.getTopFocusedDisplayContent().mCurrentFocus;
+            if (focusedWindow == null) {
+                return false;
+            }
+
+            final WindowState rootDisplayParentWindow = findRootDisplayParentWindow(focusedWindow);
+            if (!focusedWindow.isDefaultDisplay()
+                    && (rootDisplayParentWindow == null
+                    || !rootDisplayParentWindow.isDefaultDisplay())) {
+                return false;
+            }
+
+            return true;
+        }
+
         private class MyHandler extends Handler {
             public static final int MESSAGE_COMPUTE_CHANGED_WINDOWS = 1;