InputEventTimeline field fixes

Small fixes and removed isDown field from InputEventTimeline.
Modified LatencyAggregator to use inputEventActionType instead of isDown to distinguish between down and other types of motion events.

Bug: 270049345
Test: atest inputflinger_tests
Flag: EXEMPT bugfix
Change-Id: I3d0fd64baf7b3b5df412cfbd0525818667135b1c
diff --git a/services/inputflinger/dispatcher/InputEventTimeline.cpp b/services/inputflinger/dispatcher/InputEventTimeline.cpp
index 31ceb8d..6881964 100644
--- a/services/inputflinger/dispatcher/InputEventTimeline.cpp
+++ b/services/inputflinger/dispatcher/InputEventTimeline.cpp
@@ -66,12 +66,11 @@
     return !operator==(rhs);
 }
 
-InputEventTimeline::InputEventTimeline(bool isDown, nsecs_t eventTime, nsecs_t readTime,
-                                       uint16_t vendorId, uint16_t productId,
+InputEventTimeline::InputEventTimeline(nsecs_t eventTime, nsecs_t readTime, uint16_t vendorId,
+                                       uint16_t productId,
                                        const std::set<InputDeviceUsageSource>& sources,
                                        InputEventActionType inputEventActionType)
-      : isDown(isDown),
-        eventTime(eventTime),
+      : eventTime(eventTime),
         readTime(readTime),
         vendorId(vendorId),
         productId(productId),
@@ -91,8 +90,8 @@
             return false;
         }
     }
-    return isDown == rhs.isDown && eventTime == rhs.eventTime && readTime == rhs.readTime &&
-            vendorId == rhs.vendorId && productId == rhs.productId && sources == rhs.sources &&
+    return eventTime == rhs.eventTime && readTime == rhs.readTime && vendorId == rhs.vendorId &&
+            productId == rhs.productId && sources == rhs.sources &&
             inputEventActionType == rhs.inputEventActionType;
 }
 
diff --git a/services/inputflinger/dispatcher/InputEventTimeline.h b/services/inputflinger/dispatcher/InputEventTimeline.h
index 6668399..951fcc8 100644
--- a/services/inputflinger/dispatcher/InputEventTimeline.h
+++ b/services/inputflinger/dispatcher/InputEventTimeline.h
@@ -97,10 +97,9 @@
 };
 
 struct InputEventTimeline {
-    InputEventTimeline(bool isDown, nsecs_t eventTime, nsecs_t readTime, uint16_t vendorId,
-                       uint16_t productId, const std::set<InputDeviceUsageSource>& sources,
+    InputEventTimeline(nsecs_t eventTime, nsecs_t readTime, uint16_t vendorId, uint16_t productId,
+                       const std::set<InputDeviceUsageSource>& sources,
                        InputEventActionType inputEventActionType);
-    const bool isDown; // True if this is an ACTION_DOWN event
     const nsecs_t eventTime;
     const nsecs_t readTime;
     const uint16_t vendorId;
diff --git a/services/inputflinger/dispatcher/LatencyAggregator.cpp b/services/inputflinger/dispatcher/LatencyAggregator.cpp
index e09d97a..4ddd2e9 100644
--- a/services/inputflinger/dispatcher/LatencyAggregator.cpp
+++ b/services/inputflinger/dispatcher/LatencyAggregator.cpp
@@ -134,7 +134,9 @@
     mNumSketchEventsProcessed++;
 
     std::array<std::unique_ptr<KllQuantile>, SketchIndex::SIZE>& sketches =
-            timeline.isDown ? mDownSketches : mMoveSketches;
+            timeline.inputEventActionType == InputEventActionType::MOTION_ACTION_DOWN
+            ? mDownSketches
+            : mMoveSketches;
 
     // Process common ones first
     const nsecs_t eventToRead = timeline.readTime - timeline.eventTime;
@@ -242,7 +244,9 @@
         const nsecs_t consumeToGpuComplete = gpuCompletedTime - connectionTimeline.consumeTime;
         const nsecs_t gpuCompleteToPresent = presentTime - gpuCompletedTime;
 
-        android::util::stats_write(android::util::SLOW_INPUT_EVENT_REPORTED, timeline.isDown,
+        android::util::stats_write(android::util::SLOW_INPUT_EVENT_REPORTED,
+                                   timeline.inputEventActionType ==
+                                           InputEventActionType::MOTION_ACTION_DOWN,
                                    static_cast<int32_t>(ns2us(eventToRead)),
                                    static_cast<int32_t>(ns2us(readToDeliver)),
                                    static_cast<int32_t>(ns2us(deliverToConsume)),
diff --git a/services/inputflinger/dispatcher/LatencyTracker.cpp b/services/inputflinger/dispatcher/LatencyTracker.cpp
index 721d009..69024b3 100644
--- a/services/inputflinger/dispatcher/LatencyTracker.cpp
+++ b/services/inputflinger/dispatcher/LatencyTracker.cpp
@@ -70,7 +70,7 @@
 void LatencyTracker::trackListener(int32_t inputEventId, nsecs_t eventTime, nsecs_t readTime,
                                    DeviceId deviceId,
                                    const std::set<InputDeviceUsageSource>& sources,
-                                   int inputEventAction, InputEventType inputEventType) {
+                                   int32_t inputEventAction, InputEventType inputEventType) {
     reportAndPruneMatureRecords(eventTime);
     const auto it = mTimelines.find(inputEventId);
     if (it != mTimelines.end()) {
@@ -105,7 +105,7 @@
     const InputEventActionType inputEventActionType = [&]() {
         switch (inputEventType) {
             case InputEventType::MOTION: {
-                switch (inputEventAction) {
+                switch (MotionEvent::getActionMasked(inputEventAction)) {
                     case AMOTION_EVENT_ACTION_DOWN:
                         return InputEventActionType::MOTION_ACTION_DOWN;
                     case AMOTION_EVENT_ACTION_MOVE:
@@ -134,10 +134,8 @@
         }
     }();
 
-    bool isDown = inputEventType == InputEventType::MOTION &&
-            inputEventAction == AMOTION_EVENT_ACTION_DOWN;
     mTimelines.emplace(inputEventId,
-                       InputEventTimeline(isDown, eventTime, readTime, identifier->vendor,
+                       InputEventTimeline(eventTime, readTime, identifier->vendor,
                                           identifier->product, sources, inputEventActionType));
     mEventTimes.emplace(eventTime, inputEventId);
 }
diff --git a/services/inputflinger/dispatcher/LatencyTracker.h b/services/inputflinger/dispatcher/LatencyTracker.h
index 532f422..b4053ba 100644
--- a/services/inputflinger/dispatcher/LatencyTracker.h
+++ b/services/inputflinger/dispatcher/LatencyTracker.h
@@ -53,7 +53,7 @@
      * must drop all duplicate data.
      */
     void trackListener(int32_t inputEventId, nsecs_t eventTime, nsecs_t readTime, DeviceId deviceId,
-                       const std::set<InputDeviceUsageSource>& sources, int inputEventActionType,
+                       const std::set<InputDeviceUsageSource>& sources, int32_t inputEventAction,
                        InputEventType inputEventType);
     void trackFinishedEvent(int32_t inputEventId, const sp<IBinder>& connectionToken,
                             nsecs_t deliveryTime, nsecs_t consumeTime, nsecs_t finishTime);
diff --git a/services/inputflinger/tests/LatencyTracker_test.cpp b/services/inputflinger/tests/LatencyTracker_test.cpp
index 5650286..0f92833 100644
--- a/services/inputflinger/tests/LatencyTracker_test.cpp
+++ b/services/inputflinger/tests/LatencyTracker_test.cpp
@@ -61,7 +61,6 @@
 
 InputEventTimeline getTestTimeline() {
     InputEventTimeline t(
-            /*isDown=*/false,
             /*eventTime=*/2,
             /*readTime=*/3,
             /*vendorId=*/0,
@@ -174,7 +173,7 @@
                             AMOTION_EVENT_ACTION_CANCEL, InputEventType::MOTION);
     triggerEventReporting(/*eventTime=*/2);
     assertReceivedTimeline(
-            InputEventTimeline{/*isDown=*/false, /*eventTime=*/2,
+            InputEventTimeline{/*eventTime=*/2,
                                /*readTime=*/3, /*vendorId=*/0, /*productID=*/0,
                                /*sources=*/{InputDeviceUsageSource::UNKNOWN},
                                /*inputEventActionType=*/InputEventActionType::UNKNOWN_INPUT_EVENT});
@@ -226,7 +225,6 @@
 TEST_F(LatencyTrackerTest, WhenDuplicateEventsAreReported_DoesNotCrash) {
     constexpr nsecs_t inputEventId = 1;
     constexpr nsecs_t readTime = 3; // does not matter for this test
-    constexpr bool isDown = false;  // does not matter for this test
 
     // In the following 2 calls to trackListener, the inputEventId's are the same, but event times
     // are different.
@@ -246,7 +244,6 @@
 TEST_F(LatencyTrackerTest, MultipleEvents_AreReportedConsistently) {
     constexpr int32_t inputEventId1 = 1;
     InputEventTimeline timeline1(
-            /*isDown*/ false,
             /*eventTime*/ 2,
             /*readTime*/ 3,
             /*vendorId=*/0,
@@ -264,7 +261,6 @@
 
     constexpr int32_t inputEventId2 = 10;
     InputEventTimeline timeline2(
-            /*isDown=*/false,
             /*eventTime=*/20,
             /*readTime=*/30,
             /*vendorId=*/0,
@@ -317,9 +313,9 @@
                                 /*deviceId=*/DEVICE_ID,
                                 /*sources=*/{InputDeviceUsageSource::UNKNOWN},
                                 AMOTION_EVENT_ACTION_CANCEL, InputEventType::MOTION);
-        expectedTimelines.push_back(InputEventTimeline{timeline.isDown, timeline.eventTime,
-                                                       timeline.readTime, timeline.vendorId,
-                                                       timeline.productId, timeline.sources,
+        expectedTimelines.push_back(InputEventTimeline{timeline.eventTime, timeline.readTime,
+                                                       timeline.vendorId, timeline.productId,
+                                                       timeline.sources,
                                                        timeline.inputEventActionType});
     }
     // Now, complete the first event that was sent.
@@ -350,10 +346,9 @@
                             {InputDeviceUsageSource::UNKNOWN}, AMOTION_EVENT_ACTION_CANCEL,
                             InputEventType::MOTION);
     triggerEventReporting(expected.eventTime);
-    assertReceivedTimeline(InputEventTimeline{expected.isDown, expected.eventTime,
-                                              expected.readTime, expected.vendorId,
-                                              expected.productId, expected.sources,
-                                              expected.inputEventActionType});
+    assertReceivedTimeline(InputEventTimeline{expected.eventTime, expected.readTime,
+                                              expected.vendorId, expected.productId,
+                                              expected.sources, expected.inputEventActionType});
 }
 
 /**
@@ -364,7 +359,7 @@
 TEST_F(LatencyTrackerTest, TrackListenerCheck_DeviceInfoFieldsInputEventTimeline) {
     constexpr int32_t inputEventId = 1;
     InputEventTimeline timeline(
-            /*isDown*/ false, /*eventTime*/ 2, /*readTime*/ 3,
+            /*eventTime*/ 2, /*readTime*/ 3,
             /*vendorId=*/50, /*productId=*/60,
             /*sources=*/
             {InputDeviceUsageSource::TOUCHSCREEN, InputDeviceUsageSource::STYLUS_DIRECT},
@@ -390,37 +385,37 @@
     constexpr int32_t inputEventId = 1;
     // Create timelines for different event types (Motion, Key)
     InputEventTimeline motionDownTimeline(
-            /*isDown*/ true, /*eventTime*/ 2, /*readTime*/ 3,
+            /*eventTime*/ 2, /*readTime*/ 3,
             /*vendorId*/ 0, /*productId*/ 0,
             /*sources*/ {InputDeviceUsageSource::UNKNOWN},
             /*inputEventActionType*/ InputEventActionType::MOTION_ACTION_DOWN);
 
     InputEventTimeline motionMoveTimeline(
-            /*isDown*/ false, /*eventTime*/ 4, /*readTime*/ 5,
+            /*eventTime*/ 4, /*readTime*/ 5,
             /*vendorId*/ 0, /*productId*/ 0,
             /*sources*/ {InputDeviceUsageSource::UNKNOWN},
             /*inputEventActionType*/ InputEventActionType::MOTION_ACTION_MOVE);
 
     InputEventTimeline motionUpTimeline(
-            /*isDown*/ false, /*eventTime*/ 6, /*readTime*/ 7,
+            /*eventTime*/ 6, /*readTime*/ 7,
             /*vendorId*/ 0, /*productId*/ 0,
             /*sources*/ {InputDeviceUsageSource::UNKNOWN},
             /*inputEventActionType*/ InputEventActionType::MOTION_ACTION_UP);
 
     InputEventTimeline keyDownTimeline(
-            /*isDown*/ false, /*eventTime*/ 8, /*readTime*/ 9,
+            /*eventTime*/ 8, /*readTime*/ 9,
             /*vendorId*/ 0, /*productId*/ 0,
             /*sources*/ {InputDeviceUsageSource::UNKNOWN},
             /*inputEventActionType*/ InputEventActionType::KEY);
 
     InputEventTimeline keyUpTimeline(
-            /*isDown*/ false, /*eventTime*/ 10, /*readTime*/ 11,
+            /*eventTime*/ 10, /*readTime*/ 11,
             /*vendorId*/ 0, /*productId*/ 0,
             /*sources*/ {InputDeviceUsageSource::UNKNOWN},
             /*inputEventActionType*/ InputEventActionType::KEY);
 
     InputEventTimeline unknownTimeline(
-            /*isDown*/ false, /*eventTime*/ 12, /*readTime*/ 13,
+            /*eventTime*/ 12, /*readTime*/ 13,
             /*vendorId*/ 0, /*productId*/ 0,
             /*sources*/ {InputDeviceUsageSource::UNKNOWN},
             /*inputEventActionType*/ InputEventActionType::UNKNOWN_INPUT_EVENT);
diff --git a/services/inputflinger/tests/fuzzers/LatencyTrackerFuzzer.cpp b/services/inputflinger/tests/fuzzers/LatencyTrackerFuzzer.cpp
index 80c2213..695eb3c 100644
--- a/services/inputflinger/tests/fuzzers/LatencyTrackerFuzzer.cpp
+++ b/services/inputflinger/tests/fuzzers/LatencyTrackerFuzzer.cpp
@@ -71,7 +71,7 @@
                     const DeviceId deviceId = fdp.ConsumeIntegral<int32_t>();
                     std::set<InputDeviceUsageSource> sources = {
                             fdp.ConsumeEnum<InputDeviceUsageSource>()};
-                    int32_t inputEventActionType = fdp.ConsumeIntegral<int32_t>();
+                    const int32_t inputEventActionType = fdp.ConsumeIntegral<int32_t>();
                     const InputEventType inputEventType = fdp.ConsumeEnum<InputEventType>();
                     tracker.trackListener(inputEventId, eventTime, readTime, deviceId, sources,
                                           inputEventActionType, inputEventType);