Snap for 6864616 from c6f6acad769e2570b436187cbfd902fe9e720681 to rvc-qpr1-release

Change-Id: Ic5590bb40399b2bac87466648c92f7695fe877f8
diff --git a/src/com/android/car/rotary/RotaryService.java b/src/com/android/car/rotary/RotaryService.java
index d4facc4..be15728 100644
--- a/src/com/android/car/rotary/RotaryService.java
+++ b/src/com/android/car/rotary/RotaryService.java
@@ -1141,9 +1141,6 @@
         if (!isValidAction(action)) {
             return;
         }
-        if (initFocus()) {
-            return;
-        }
 
         // If the focused node is in direct manipulation mode, manipulate it directly.
         if (mInDirectManipulationMode) {
@@ -1161,6 +1158,14 @@
             return;
         }
 
+        // Don't call initFocus() when handling ACTION_UP nudge events as this event will typically
+        // arrive before the TYPE_VIEW_FOCUSED event when we delegate focusing to a FocusArea, and
+        // will cause us to focus a nearby view when we discover that mFocusedNode is no longer
+        // focused.
+        if (initFocus()) {
+            return;
+        }
+
         // If the focused node is not in direct manipulation mode, try to move the focus to another
         // node.
         List<AccessibilityWindowInfo> windows = getWindows();
@@ -1703,6 +1708,11 @@
             return false;
         }
         AccessibilityNodeInfo focusParkingView = findFocusParkingView(mFocusedNode);
+
+        // Refresh the node to ensure the focused state is up to date. The node came directly from
+        // the node tree but it could have been cached by the accessibility framework.
+        focusParkingView = Utils.refreshNode(focusParkingView);
+
         if (focusParkingView == null) {
             return false;
         }