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/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp
index 16cb7d7..1bbdcbb 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.cpp
+++ b/services/inputflinger/dispatcher/InputDispatcher.cpp
@@ -2907,6 +2907,12 @@
                 ATRACE_NAME(message.c_str());
             }
 
+            if ((motionEntry.flags & AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE) &&
+                (motionEntry.policyFlags & POLICY_FLAG_TRUSTED)) {
+                // Skip reporting pointer down outside focus to the policy.
+                break;
+            }
+
             dispatchPointerDownOutsideFocus(motionEntry.source, dispatchEntry->resolvedAction,
                                             inputTarget.inputChannel->getConnectionToken());