Fix UiAutomation to correctly wait for idleness

Currently, everytime onAccessibilityEvent is called, "lastEventTime" is
updated with the event timestamp. However, accessibility framework
doesn't always sends events by the order of the timestamp, so it's
possible that the timestamp is updated with an older event's timestamp.

This fixes the issue by updating the variable with maximum of the
current value and the event time.

Test: CtsCompanionDeviceManagerUiAutomationTestCases (fixes flakiness)
Test: UiAutomationTest
Bug: 262184630
Bug: 268464095
Change-Id: If3306c6a6624a8b76a17d2c7b745143e5f9c1ed3
diff --git a/core/java/android/app/UiAutomation.java b/core/java/android/app/UiAutomation.java
index 249937a..cda2397 100644
--- a/core/java/android/app/UiAutomation.java
+++ b/core/java/android/app/UiAutomation.java
@@ -1623,7 +1623,9 @@
                         if (isGenerationChangedLocked()) {
                             return;
                         }
-                        mLastEventTimeMillis = event.getEventTime();
+                        // It is not guaranteed that the accessibility framework sends events by the
+                        // order of event timestamp.
+                        mLastEventTimeMillis = Math.max(mLastEventTimeMillis, event.getEventTime());
                         if (mWaitingForEventDelivery) {
                             mEventQueue.add(AccessibilityEvent.obtain(event));
                         }