Reset no focused window timeout when window gets focus

If a window is about to receive focus, then we should stop the "no
focused window" timeout.

Otherwise, we would report an ANR, even though the focused window was
added within the "dispatching timeout" grace period of the application.

Bug: 302326393
Test: TEST=inputflinger_tests; m $TEST && adb sync data && adb shell -t data/nativetest64/$TEST/$TEST --gtest_filter="*FocusedWindowWithoutSetFocusedApplication_NoAnr*" --gtest_repeat=3000 --gtest_break_on_failure
Test: TEST=inputflinger_tests; m $TEST && $ANDROID_HOST_OUT/nativetest64/$TEST/$TEST --gtest_filter="*FocusedWindowWithoutSetFocusedApplication_NoAnr" --gtest_repeat=1000 --gtest_break_on_failure
Change-Id: Ie272545e1be23ff5ef4d1bf24507a8ba7e6d170b
diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp
index 4251682..6226a19 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.cpp
+++ b/services/inputflinger/dispatcher/InputDispatcher.cpp
@@ -6596,6 +6596,7 @@
         }
     }
     if (changes.newFocus) {
+        resetNoFocusedWindowTimeoutLocked();
         enqueueFocusEventLocked(changes.newFocus, /*hasFocus=*/true, changes.reason);
     }
 
diff --git a/services/inputflinger/tests/InputDispatcher_test.cpp b/services/inputflinger/tests/InputDispatcher_test.cpp
index 215989d..703c3f7 100644
--- a/services/inputflinger/tests/InputDispatcher_test.cpp
+++ b/services/inputflinger/tests/InputDispatcher_test.cpp
@@ -7988,7 +7988,7 @@
 TEST_F(InputDispatcherMultiWindowAnr, FocusedWindowWithoutSetFocusedApplication_NoAnr) {
     std::shared_ptr<FakeApplicationHandle> focusedApplication =
             std::make_shared<FakeApplicationHandle>();
-    focusedApplication->setDispatchingTimeout(200ms);
+    focusedApplication->setDispatchingTimeout(300ms);
     mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, focusedApplication);
     // The application that owns 'mFocusedWindow' and 'mUnfocusedWindow' is not focused.
     mFocusedWindow->setFocusable(false);