Merge "Only switch task focus to window's task that was tapped outside current focus" into qt-dev
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index e7dac9d..0f4e123 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -2362,33 +2362,6 @@
     }
 
     /**
-     * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
-     * current task in focus.
-     *
-     * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
-     * returns -1.
-     *
-     * @param x horizontal coordinate of the tap position
-     * @param y vertical coordinate of the tap position
-     * @return the task ID if a non-home task is found; -1 if not
-     */
-    int taskForTapOutside(int x, int y) {
-        for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
-            final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
-            if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
-                // We skip not only home stack, but also everything behind home because user can't
-                // see them when home stack is isn't in multi-window mode.
-                break;
-            }
-            final int taskId = stack.taskIdFromPoint(x, y);
-            if (taskId != -1) {
-                return taskId;
-            }
-        }
-        return -1;
-    }
-
-    /**
      * Returns true if the input point is within an app window.
      */
     boolean pointWithinAppWindow(int x, int y) {
diff --git a/services/core/java/com/android/server/wm/TaskPositioningController.java b/services/core/java/com/android/server/wm/TaskPositioningController.java
index cdcb857..3929a12 100644
--- a/services/core/java/com/android/server/wm/TaskPositioningController.java
+++ b/services/core/java/com/android/server/wm/TaskPositioningController.java
@@ -127,7 +127,6 @@
 
     void handleTapOutsideTask(DisplayContent displayContent, int x, int y) {
         mHandler.post(() -> {
-            int taskId = -1;
             synchronized (mService.mGlobalLock) {
                 final Task task = displayContent.findTaskForResizePoint(x, y);
                 if (task != null) {
@@ -135,15 +134,10 @@
                             task.preserveOrientationOnResize(), x, y)) {
                         return;
                     }
-                    taskId = task.mTaskId;
-                } else {
-                    taskId = displayContent.taskForTapOutside(x, y);
-                }
-            }
-            if (taskId >= 0) {
-                try {
-                    mActivityManager.setFocusedTask(taskId);
-                } catch (RemoteException e) {
+                    try {
+                        mActivityManager.setFocusedTask(task.mTaskId);
+                    } catch (RemoteException e) {
+                    }
                 }
             }
         });
diff --git a/services/core/java/com/android/server/wm/TaskStack.java b/services/core/java/com/android/server/wm/TaskStack.java
index e6b8112..09baf8c 100644
--- a/services/core/java/com/android/server/wm/TaskStack.java
+++ b/services/core/java/com/android/server/wm/TaskStack.java
@@ -1458,31 +1458,6 @@
         return false;
     }
 
-    int taskIdFromPoint(int x, int y) {
-        getBounds(mTmpRect);
-        if (!mTmpRect.contains(x, y) || isAdjustedForMinimizedDockedStack()) {
-            return -1;
-        }
-
-        for (int taskNdx = mChildren.size() - 1; taskNdx >= 0; --taskNdx) {
-            final Task task = mChildren.get(taskNdx);
-            final WindowState win = task.getTopVisibleAppMainWindow();
-            if (win == null) {
-                continue;
-            }
-            // We need to use the task's dim bounds (which is derived from the visible bounds of its
-            // apps windows) for any touch-related tests. Can't use the task's original bounds
-            // because it might be adjusted to fit the content frame. For example, the presence of
-            // the IME adjusting the windows frames when the app window is the IME target.
-            task.getDimBounds(mTmpRect);
-            if (mTmpRect.contains(x, y)) {
-                return task.mTaskId;
-            }
-        }
-
-        return -1;
-    }
-
     void findTaskForResizePoint(int x, int y, int delta,
             DisplayContent.TaskForResizePointSearchResult results) {
         if (!getWindowConfiguration().canResizeTask()) {
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index 8dfb02e..ce496f4 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -7587,12 +7587,36 @@
             return;
         }
 
-        final DisplayContent displayContent = touchedWindow.getDisplayContent();
+        handleTaskFocusChange(touchedWindow.getTask());
+        handleDisplayFocusChange(touchedWindow);
+    }
+
+    private void handleTaskFocusChange(Task task) {
+        if (task == null) {
+            return;
+        }
+
+        final TaskStack stack = task.mStack;
+        // We ignore home stack since we don't want home stack to move to front when touched.
+        // Specifically, in freeform we don't want tapping on home to cause the freeform apps to go
+        // behind home. See b/117376413
+        if (stack.isActivityTypeHome()) {
+            return;
+        }
+
+        try {
+            mActivityTaskManager.setFocusedTask(task.mTaskId);
+        } catch (RemoteException e) {
+        }
+    }
+
+    private void handleDisplayFocusChange(WindowState window) {
+        final DisplayContent displayContent = window.getDisplayContent();
         if (displayContent == null) {
             return;
         }
 
-        if (!touchedWindow.canReceiveKeys()) {
+        if (!window.canReceiveKeys()) {
             // If the window that received the input event cannot receive keys, don't move the
             // display it's on to the top since that window won't be able to get focus anyway.
             return;