Add NO_FOCUS_CHANGE flag to pointer gestures to disallow focus changes
When using a multi-touch trackpad, it is the expected behavior in most
operating systems that the user is allowed to perform gestures (like
scroll, pinch, etc.) on an unfocused window without bringing it into
focus. The previous behavior in Android was that any DOWN event on an
unfocused window would bring the unfocused window into focus, including
any pointer gesture.
This change adds the NO_FOCUS_CHANGE flag to the MotionEvents generated
by certain pointer gestures so that it does not change window focus.
Gestures such as tap and tap drag are not affected.
Bug: 173733166
Test: atest inputflinger_tests
Test: manual: in multi-display scenario with freeform windows and a
trackpad: open two freeform windows so that they overlap, perform
gesture (scroll/pinch) on unfocused window, observe that the window is
not focused; perform tap on unfocused window, observe that the window is
focused.
Change-Id: I74e52f8daa13d4e6c047bc23982ec56942c555f6
diff --git a/services/inputflinger/tests/InputDispatcher_test.cpp b/services/inputflinger/tests/InputDispatcher_test.cpp
index 9687c83..bbf51f6 100644
--- a/services/inputflinger/tests/InputDispatcher_test.cpp
+++ b/services/inputflinger/tests/InputDispatcher_test.cpp
@@ -1225,6 +1225,11 @@
return *this;
}
+ MotionEventBuilder& addFlag(uint32_t flags) {
+ mFlags |= flags;
+ return *this;
+ }
+
MotionEvent build() {
std::vector<PointerProperties> pointerProperties;
std::vector<PointerCoords> pointerCoords;
@@ -1244,7 +1249,7 @@
MotionEvent event;
ui::Transform identityTransform;
event.initialize(InputEvent::nextId(), DEVICE_ID, mSource, mDisplayId, INVALID_HMAC,
- mAction, mActionButton, /* flags */ 0, /* edgeFlags */ 0, AMETA_NONE,
+ mAction, mActionButton, mFlags, /* edgeFlags */ 0, AMETA_NONE,
mButtonState, MotionClassification::NONE, identityTransform,
/* xPrecision */ 0, /* yPrecision */ 0, mRawXCursorPosition,
mRawYCursorPosition, mDisplayWidth, mDisplayHeight, mEventTime, mEventTime,
@@ -1260,6 +1265,7 @@
int32_t mDisplayId{ADISPLAY_ID_DEFAULT};
int32_t mActionButton{0};
int32_t mButtonState{0};
+ int32_t mFlags{0};
float mRawXCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
float mRawYCursorPosition{AMOTION_EVENT_INVALID_CURSOR_POSITION};
int32_t mDisplayWidth{AMOTION_EVENT_INVALID_DISPLAY_SIZE};
@@ -3105,6 +3111,25 @@
mFakePolicy->assertOnPointerDownWasNotCalled();
}
+// Have two windows, one with focus. Injecting a trusted DOWN MotionEvent with the flag
+// NO_FOCUS_CHANGE on the unfocused window should not call the onPointerDownOutsideFocus callback.
+TEST_F(InputDispatcherOnPointerDownOutsideFocus, NoFocusChangeFlag) {
+ const MotionEvent event =
+ MotionEventBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_MOUSE)
+ .eventTime(systemTime(SYSTEM_TIME_MONOTONIC))
+ .pointer(PointerBuilder(/* id */ 0, AMOTION_EVENT_TOOL_TYPE_FINGER).x(20).y(20))
+ .addFlag(AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE)
+ .build();
+ ASSERT_EQ(InputEventInjectionResult::SUCCEEDED, injectMotionEvent(mDispatcher, event))
+ << "Inject motion event should return InputEventInjectionResult::SUCCEEDED";
+ mUnfocusedWindow->consumeAnyMotionDown(ADISPLAY_ID_DEFAULT, AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE);
+
+ ASSERT_TRUE(mDispatcher->waitForIdle());
+ mFakePolicy->assertOnPointerDownWasNotCalled();
+ // Ensure that the unfocused window did not receive any FOCUS events.
+ mUnfocusedWindow->assertNoEvents();
+}
+
// These tests ensures we can send touch events to a single client when there are multiple input
// windows that point to the same client token.
class InputDispatcherMultiWindowSameTokenTests : public InputDispatcherTest {