diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index 2dbf42f93e62b..f459bcaf48cef 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -802,6 +802,7 @@ public final class ViewRootImpl implements ViewParent, private final ViewRootRectTracker mUnrestrictedKeepClearRectsTracker = new ViewRootRectTracker(v -> v.collectUnrestrictedPreferKeepClearRects()); private boolean mHasPendingKeepClearAreaChange; + private Rect mKeepClearAccessibilityFocusRect; private IAccessibilityEmbeddedConnection mAccessibilityEmbeddedConnection; @@ -4824,13 +4825,27 @@ public final class ViewRootImpl implements ViewParent, mHandler.sendEmptyMessage(MSG_KEEP_CLEAR_RECTS_CHANGED); } - void keepClearRectsChanged() { + private void updateKeepClearForAccessibilityFocusRect() { + if (mViewConfiguration.isPreferKeepClearForFocusEnabled()) { + if (mKeepClearAccessibilityFocusRect == null) { + mKeepClearAccessibilityFocusRect = new Rect(); + } + boolean hasAccessibilityFocus = + getAccessibilityFocusedRect(mKeepClearAccessibilityFocusRect); + if (!hasAccessibilityFocus) { + mKeepClearAccessibilityFocusRect.setEmpty(); + } + mHandler.obtainMessage(MSG_KEEP_CLEAR_RECTS_CHANGED, 1, 0).sendToTarget(); + } + } + + void keepClearRectsChanged(boolean accessibilityFocusRectChanged) { boolean restrictedKeepClearRectsChanged = mKeepClearRectsTracker.computeChanges(); boolean unrestrictedKeepClearRectsChanged = mUnrestrictedKeepClearRectsTracker.computeChanges(); - if ((restrictedKeepClearRectsChanged || unrestrictedKeepClearRectsChanged) - && mView != null) { + if ((restrictedKeepClearRectsChanged || unrestrictedKeepClearRectsChanged + || accessibilityFocusRectChanged) && mView != null) { mHasPendingKeepClearAreaChange = true; // Only report keep clear areas immediately if they have not been reported recently if (!mHandler.hasMessages(MSG_REPORT_KEEP_CLEAR_RECTS)) { @@ -4847,10 +4862,16 @@ public final class ViewRootImpl implements ViewParent, } mHasPendingKeepClearAreaChange = false; - final List restrictedKeepClearRects = mKeepClearRectsTracker.getLastComputedRects(); + List restrictedKeepClearRects = mKeepClearRectsTracker.getLastComputedRects(); final List unrestrictedKeepClearRects = mUnrestrictedKeepClearRectsTracker.getLastComputedRects(); + if (mKeepClearAccessibilityFocusRect != null + && !mKeepClearAccessibilityFocusRect.isEmpty()) { + restrictedKeepClearRects = new ArrayList<>(restrictedKeepClearRects); + restrictedKeepClearRects.add(mKeepClearAccessibilityFocusRect); + } + try { mWindowSession.reportKeepClearAreasChanged(mWindow, restrictedKeepClearRects, unrestrictedKeepClearRects); @@ -5050,6 +5071,7 @@ public final class ViewRootImpl implements ViewParent, // Set the new focus host and node. mAccessibilityFocusedHost = view; mAccessibilityFocusedVirtualView = node; + updateKeepClearForAccessibilityFocusRect(); if (mAttachInfo.mThreadedRenderer != null) { mAttachInfo.mThreadedRenderer.invalidateRoot(); @@ -5638,7 +5660,7 @@ public final class ViewRootImpl implements ViewParent, systemGestureExclusionChanged(); } break; case MSG_KEEP_CLEAR_RECTS_CHANGED: { - keepClearRectsChanged(); + keepClearRectsChanged(/* accessibilityFocusRectChanged= */ msg.arg1 == 1); } break; case MSG_REPORT_KEEP_CLEAR_RECTS: { reportKeepClearAreasChanged();