Add focusTransferTarget in WindowInfo
A transfer focus request was originally added on FocusRequest object.
This meant when a host wanted to provide focus to an embedded window, it
would request to transfer focus, but provide its own token as the
focusedToken in the request. In FocusResolver, it would ensure that the
focusedToken was current focus before allow the request to proceed. This
worked in some cases, but created races where clients had to understand
timing in order to properly transfer focus.
The new code creates a persistent focusTransferTarget value on the
WindowInfo object. The host will add the embedded token as part of its
own WindowInfo#focusTransferTarget. When FocusResolver determines that
a window should gain focus, it will check to see if there is any
focusTransferTarget value set. If there is, it will attempt to give focus
to that window, if possible. Otherwise, it will fallback to the previous
window in the chain.
This solves a few issues with embedded windows.
1. Apps can request focus to the embedded by requesting focus to the SV
that hosts the embedded. However, if they request focus too early, it
will drop the request since the host has not yet gained focus. With
the current code, it will transfer focus to the embedded once the
host could have gained focus. If the host wants to revoke, the
focusTransferTarget can be set to null, which will give the host focus
again.
2. This fixes the issue if another window becomes focus. When WM gives
focus back to the host window, it should automatically give focus to
the embedded if it was last requested. The app shouldn't be required
to maintain the last state of the embedded focus to see if it needs
to transfer it again. It's actually not even possible once focus can
be given to embedded via touch since only WM and Input know about
this.
By adding focusTransferTarget to WindowInfo, the focusedToken value in
FocusRequest can be removed, as well.
Test: InputDispatcher_test
Test: FocusResolver_test
Test: WindowInfo_test
Bug: 230340812
Change-Id: I252403184f7060c37c82353638ac6ee25a0979fc
diff --git a/services/inputflinger/dispatcher/FocusResolver.cpp b/services/inputflinger/dispatcher/FocusResolver.cpp
index 4da846b..0e4e79e 100644
--- a/services/inputflinger/dispatcher/FocusResolver.cpp
+++ b/services/inputflinger/dispatcher/FocusResolver.cpp
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+#include <optional>
#define LOG_TAG "InputDispatcher"
#define ATRACE_TAG ATRACE_TAG_INPUT
@@ -25,6 +26,7 @@
#include <binder/Binder.h>
#include <ftl/enum.h>
#include <gui/WindowInfo.h>
+#include <unordered_set>
#include "DebugConfig.h"
#include "FocusResolver.h"
@@ -34,6 +36,11 @@
namespace android::inputdispatcher {
+template <typename T>
+struct SpHash {
+ size_t operator()(const sp<T>& k) const { return std::hash<T*>()(k.get()); }
+};
+
sp<IBinder> FocusResolver::getFocusedWindowToken(int32_t displayId) const {
auto it = mFocusedWindowTokenByDisplay.find(displayId);
return it != mFocusedWindowTokenByDisplay.end() ? it->second.second : nullptr;
@@ -54,30 +61,30 @@
int32_t displayId, const std::vector<sp<WindowInfoHandle>>& windows) {
std::string removeFocusReason;
- // Check if the currently focused window is still focusable.
- const sp<IBinder> currentFocus = getFocusedWindowToken(displayId);
- if (currentFocus) {
- Focusability result = isTokenFocusable(currentFocus, windows);
- if (result == Focusability::OK) {
- return std::nullopt;
- }
- removeFocusReason = ftl::enum_string(result);
- }
-
- // We don't have a focused window or the currently focused window is no longer focusable. Check
- // to see if we can grant focus to the window that previously requested focus.
const std::optional<FocusRequest> request = getFocusRequest(displayId);
+ const sp<IBinder> currentFocus = getFocusedWindowToken(displayId);
+
+ // Find the next focused token based on the latest FocusRequest. If the requested focus window
+ // cannot be focused, focus will be removed.
if (request) {
sp<IBinder> requestedFocus = request->token;
- const Focusability result = isTokenFocusable(requestedFocus, windows);
+ sp<WindowInfoHandle> resolvedFocusWindow;
+ Focusability result = getResolvedFocusWindow(requestedFocus, windows, resolvedFocusWindow);
+ if (result == Focusability::OK && resolvedFocusWindow->getToken() == currentFocus) {
+ return std::nullopt;
+ }
const Focusability previousResult = mLastFocusResultByDisplay[displayId];
mLastFocusResultByDisplay[displayId] = result;
if (result == Focusability::OK) {
+ LOG_ALWAYS_FATAL_IF(!resolvedFocusWindow,
+ "Focused window should be non-null when result is OK!");
return updateFocusedWindow(displayId,
"Window became focusable. Previous reason: " +
ftl::enum_string(previousResult),
- requestedFocus, request->windowName);
+ resolvedFocusWindow->getToken(),
+ resolvedFocusWindow->getName());
}
+ removeFocusReason = ftl::enum_string(result);
}
// Focused window is no longer focusable and we don't have a suitable focus request to grant.
@@ -96,35 +103,18 @@
return std::nullopt;
}
- // Handle conditional focus requests, i.e. requests that have a focused token. These requests
- // are not persistent. If the window is no longer focusable, we expect focus to go back to the
- // previously focused window.
- if (request.focusedToken) {
- if (currentFocus != request.focusedToken) {
- ALOGW("setFocusedWindow %s on display %" PRId32
- " ignored, reason: focusedToken %s is not focused",
- request.windowName.c_str(), displayId, request.focusedWindowName.c_str());
- return std::nullopt;
- }
- Focusability result = isTokenFocusable(request.token, windows);
- if (result == Focusability::OK) {
- return updateFocusedWindow(displayId, "setFocusedWindow with focus check",
- request.token, request.windowName);
- }
- ALOGW("setFocusedWindow %s on display %" PRId32 " ignored, reason: %s",
- request.windowName.c_str(), displayId, ftl::enum_string(result).c_str());
- return std::nullopt;
- }
-
- Focusability result = isTokenFocusable(request.token, windows);
+ sp<WindowInfoHandle> resolvedFocusWindow;
+ Focusability result = getResolvedFocusWindow(request.token, windows, resolvedFocusWindow);
// Update focus request. The focus resolver will always try to handle this request if there is
// no focused window on the display.
mFocusRequestByDisplay[displayId] = request;
mLastFocusResultByDisplay[displayId] = result;
if (result == Focusability::OK) {
- return updateFocusedWindow(displayId, "setFocusedWindow", request.token,
- request.windowName);
+ LOG_ALWAYS_FATAL_IF(!resolvedFocusWindow,
+ "Focused window should be non-null when result is OK!");
+ return updateFocusedWindow(displayId, "setFocusedWindow", resolvedFocusWindow->getToken(),
+ resolvedFocusWindow->getName());
}
// The requested window is not currently focusable. Wait for the window to become focusable
@@ -134,11 +124,43 @@
nullptr);
}
+FocusResolver::Focusability FocusResolver::getResolvedFocusWindow(
+ const sp<IBinder>& token, const std::vector<sp<WindowInfoHandle>>& windows,
+ sp<WindowInfoHandle>& outFocusableWindow) {
+ sp<IBinder> curFocusCandidate = token;
+ bool focusedWindowFound = false;
+
+ // Keep track of all windows reached to prevent a cyclical transferFocus request.
+ std::unordered_set<sp<IBinder>, SpHash<IBinder>> tokensReached;
+
+ while (curFocusCandidate != nullptr && tokensReached.count(curFocusCandidate) == 0) {
+ tokensReached.emplace(curFocusCandidate);
+ Focusability result = isTokenFocusable(curFocusCandidate, windows, outFocusableWindow);
+ if (result == Focusability::OK) {
+ LOG_ALWAYS_FATAL_IF(!outFocusableWindow,
+ "Focused window should be non-null when result is OK!");
+ focusedWindowFound = true;
+ // outFocusableWindow has been updated by isTokenFocusable to contain
+ // the window info for curFocusCandidate. See if we can grant focus
+ // to the token that it wants to transfer its focus to.
+ curFocusCandidate = outFocusableWindow->getInfo()->focusTransferTarget;
+ }
+
+ // If the initial token is not focusable, return early with the failed result.
+ if (!focusedWindowFound) {
+ return result;
+ }
+ }
+
+ return focusedWindowFound ? Focusability::OK : Focusability::NO_WINDOW;
+}
+
FocusResolver::Focusability FocusResolver::isTokenFocusable(
- const sp<IBinder>& token, const std::vector<sp<WindowInfoHandle>>& windows) {
+ const sp<IBinder>& token, const std::vector<sp<WindowInfoHandle>>& windows,
+ sp<WindowInfoHandle>& outFocusableWindow) {
bool allWindowsAreFocusable = true;
- bool visibleWindowFound = false;
bool windowFound = false;
+ sp<WindowInfoHandle> visibleWindowHandle = nullptr;
for (const sp<WindowInfoHandle>& window : windows) {
if (window->getToken() != token) {
continue;
@@ -146,7 +168,7 @@
windowFound = true;
if (!window->getInfo()->inputConfig.test(gui::WindowInfo::InputConfig::NOT_VISIBLE)) {
// Check if at least a single window is visible.
- visibleWindowFound = true;
+ visibleWindowHandle = window;
}
if (window->getInfo()->inputConfig.test(gui::WindowInfo::InputConfig::NOT_FOCUSABLE)) {
// Check if all windows with the window token are focusable.
@@ -161,10 +183,12 @@
if (!allWindowsAreFocusable) {
return Focusability::NOT_FOCUSABLE;
}
- if (!visibleWindowFound) {
+ if (!visibleWindowHandle) {
return Focusability::NOT_VISIBLE;
}
+ // Only set the outFoundWindow if the window can be focused
+ outFocusableWindow = visibleWindowHandle;
return Focusability::OK;
}