From 03df85e0593e6ac17f802b29a9045a4628019441 Mon Sep 17 00:00:00 2001 From: chaviw Date: Tue, 3 May 2022 11:37:25 -0500 Subject: [PATCH] Set focus to WMS computed focus when embedded window drops focus If an internal system window wants to drop focus from an embedded window, requestFocusTransfer doesn't need to be called and instead we can directly call setFocusedWindow. This fixes the case where transfer focus fails if the old focused window loses visibility by the time the transfer request arrives. The transfer won't be allowed because the old window isn't focused anymore so we can't honor the transfer request. This fix is fine for internal system windows because there's no security issue with transferring focus from embedded to something WMS computes. However, there's still a race condition for cases where apps want to transfer focus from embedded back to host when they are setting visibility on the embedded window since the embedded window can become invisible before the transfer goes through. Test: Pip Menu focus lost Fixes: 230851770 Change-Id: I09db0bbdf4db6eeaffa30275233811b13ea31132 --- data/etc/services.core.protolog.json | 12 ++++++------ .../com/android/server/wm/WindowManagerService.java | 12 +++++++----- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/data/etc/services.core.protolog.json b/data/etc/services.core.protolog.json index 58e13116f1edb..97ce67c9baf95 100644 --- a/data/etc/services.core.protolog.json +++ b/data/etc/services.core.protolog.json @@ -2581,12 +2581,6 @@ "group": "WM_DEBUG_ANIM", "at": "com\/android\/server\/wm\/WindowContainer.java" }, - "397105698": { - "message": "grantEmbeddedWindowFocus remove request for win=%s dropped since no candidate was found", - "level": "VERBOSE", - "group": "WM_DEBUG_FOCUS", - "at": "com\/android\/server\/wm\/WindowManagerService.java" - }, "397382873": { "message": "Moving to PAUSED: %s %s", "level": "VERBOSE", @@ -3109,6 +3103,12 @@ "group": "WM_DEBUG_LOCKTASK", "at": "com\/android\/server\/wm\/LockTaskController.java" }, + "958338552": { + "message": "grantEmbeddedWindowFocus win=%s dropped focus so setting focus to null since no candidate was found", + "level": "VERBOSE", + "group": "WM_DEBUG_FOCUS", + "at": "com\/android\/server\/wm\/WindowManagerService.java" + }, "959486822": { "message": "setSyncGroup #%d on %s", "level": "VERBOSE", diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java index 5d5944a74b672..7a5efa3949dd9 100644 --- a/services/core/java/com/android/server/wm/WindowManagerService.java +++ b/services/core/java/com/android/server/wm/WindowManagerService.java @@ -8857,16 +8857,18 @@ public class WindowManagerService extends IWindowManager.Stub WindowState newFocusTarget = displayContent == null ? null : displayContent.findFocusedWindow(); if (newFocusTarget == null) { - ProtoLog.v(WM_DEBUG_FOCUS, "grantEmbeddedWindowFocus remove request for " - + "win=%s dropped since no candidate was found", + t.setFocusedWindow(null, null, displayId).apply(); + ProtoLog.v(WM_DEBUG_FOCUS, "grantEmbeddedWindowFocus win=%s" + + " dropped focus so setting focus to null since no candidate" + + " was found", embeddedWindow); return; } - t.requestFocusTransfer(newFocusTarget.mInputChannelToken, newFocusTarget.getName(), - inputToken, embeddedWindow.toString(), + t.setFocusedWindow(newFocusTarget.mInputChannelToken, newFocusTarget.getName(), displayId).apply(); + EventLog.writeEvent(LOGTAG_INPUT_FOCUS, - "Transfer focus request " + newFocusTarget, + "Focus request " + newFocusTarget, "reason=grantEmbeddedWindowFocus(false)"); } ProtoLog.v(WM_DEBUG_FOCUS, "grantEmbeddedWindowFocus win=%s grantFocus=%s",