From 7dffa0cc7fbb9bda6a4146b64449e6030c7fa131 Mon Sep 17 00:00:00 2001 From: Chavi Weingarten Date: Mon, 27 Mar 2023 23:18:41 +0000 Subject: [PATCH] Request embedded focus if a SurfacePackage was added to SV If the SV was already focus when a SurfacePackage is attached, it should request focus to the embedded. This allows focus to work better for the developer perspective since they don't have to understand the timing of when a SurfacePackage is attached vs when a view gains focus. Test: SurfaceControlViewHostTests Bug: 230340812 Change-Id: If55b33e92769e37794bd258e7f41da40decbef8c --- core/java/android/view/SurfaceView.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/core/java/android/view/SurfaceView.java b/core/java/android/view/SurfaceView.java index cdea97ce9b7ff..0e4cf89e7772b 100644 --- a/core/java/android/view/SurfaceView.java +++ b/core/java/android/view/SurfaceView.java @@ -1854,6 +1854,10 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall applyTransactionOnVriDraw(transaction); } mSurfacePackage = p; + + if (isFocused()) { + requestEmbeddedFocus(true); + } invalidate(); } @@ -1947,8 +1951,12 @@ public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCall @Override protected void onFocusChanged(boolean gainFocus, @FocusDirection int direction, - @Nullable Rect previouslyFocusedRect) { + @Nullable Rect previouslyFocusedRect) { super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); + requestEmbeddedFocus(gainFocus); + } + + private void requestEmbeddedFocus(boolean gainFocus) { final ViewRootImpl viewRoot = getViewRootImpl(); if (mSurfacePackage == null || viewRoot == null) { return;