diff options
| author | 2022-09-06 12:20:37 +0000 | |
|---|---|---|
| committer | 2022-09-14 06:43:22 +0000 | |
| commit | 5fc73f8f2fc508f56813a4eb8d8d33e40b8f6346 (patch) | |
| tree | 582c5b4b9da419d89b894440ba9691ecd693b3b5 | |
| parent | 826ce0df09a32e32148940986de44fb574de3eee (diff) | |
Clear default focus highlight when lost window focus or re-enter touch mode
The default focus highlight will indicate the focused view in view
system when user navigate the focus without touch.
This CL will clear it if the window focus has lost or it re-entered the
touch mode, so we won't see the wrong highlights in different windows
while using multi-window mode.
Bug: 244273901
Test: atest DefaultFocusHighlightTest
Test: multi-window, use dpad to navigate focus
Change-Id: Iad07c77b4ae16f3c88797eddd749e0aedebdd4fc
| -rw-r--r-- | core/java/android/view/View.java | 3 | ||||
| -rw-r--r-- | core/java/android/view/ViewRootImpl.java | 15 |
2 files changed, 16 insertions, 2 deletions
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index 8fee4db458b3..cf087bcf7cfc 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -24490,8 +24490,9 @@ public class View implements Drawable.Callback, KeyEvent.Callback, /** * Set the current default focus highlight. * @param highlight the highlight drawable, or {@code null} if it's no longer needed. + * @hide */ - private void setDefaultFocusHighlight(Drawable highlight) { + void setDefaultFocusHighlight(Drawable highlight) { mDefaultFocusHighlight = highlight; mDefaultFocusHighlightSizeChanged = true; if (highlight != null) { diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index 074cbe5a6947..89a1557db482 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -3815,6 +3815,13 @@ public final class ViewRootImpl implements ViewParent, if (mAttachInfo.mTooltipHost != null) { mAttachInfo.mTooltipHost.hideTooltip(); } + if (!hasWindowFocus) { + // Clear focus highlight if its window lost focus. + final View focused = mView.findFocus(); + if (focused != null) { + focused.setDefaultFocusHighlight(null); + } + } } // Note: must be done after the focus change callbacks, @@ -5846,7 +5853,13 @@ public final class ViewRootImpl implements ViewParent, // be when the window is first being added, and mFocused isn't // set yet. final View focused = mView.findFocus(); - if (focused != null && !focused.isFocusableInTouchMode()) { + if (focused == null) { + return false; + } + + // Clear default focus highlight if it entered touch mode. + focused.setDefaultFocusHighlight(null); + if (!focused.isFocusableInTouchMode()) { final ViewGroup ancestorToTakeFocus = findAncestorToTakeFocusInTouchMode(focused); if (ancestorToTakeFocus != null) { // there is an ancestor that wants focus after its |