Call Filter from a separate function
The function 'processMotion' has gotten too long. To make it easier to
reason about, separate some code from the center of the function into a
new function.
Unfortunately, this new function will have side-effects, but it's still
easier to understand and reduces the number of local vars.
There's no functional change in this CL.
Bug: 241935838
Test: atest inputflinger_tests
Change-Id: I2157798b70299659791ae6ef85b2394e9130b4b3
diff --git a/services/inputflinger/UnwantedInteractionBlocker.cpp b/services/inputflinger/UnwantedInteractionBlocker.cpp
index 1c27a52..fc8dfe6 100644
--- a/services/inputflinger/UnwantedInteractionBlocker.cpp
+++ b/services/inputflinger/UnwantedInteractionBlocker.cpp
@@ -616,6 +616,48 @@
return touches;
}
+std::set<int32_t> PalmRejector::detectPalmPointers(const NotifyMotionArgs& args) {
+ std::bitset<::ui::kNumTouchEvdevSlots> slotsToHold;
+ std::bitset<::ui::kNumTouchEvdevSlots> slotsToSuppress;
+
+ // Store the slot state before we call getTouches and update it. This way, we can find
+ // the slots that have been removed due to the incoming event.
+ SlotState oldSlotState = mSlotState;
+ mSlotState.update(args);
+
+ std::vector<::ui::InProgressTouchEvdev> touches =
+ getTouches(args, mDeviceInfo, oldSlotState, mSlotState);
+ ::base::TimeTicks chromeTimestamp = toChromeTimestamp(args.eventTime);
+
+ if (DEBUG_MODEL) {
+ std::stringstream touchesStream;
+ for (const ::ui::InProgressTouchEvdev& touch : touches) {
+ touchesStream << touch.tracking_id << " : " << touch << "\n";
+ }
+ ALOGD("Filter: touches = %s", touchesStream.str().c_str());
+ }
+
+ mPalmDetectionFilter->Filter(touches, chromeTimestamp, &slotsToHold, &slotsToSuppress);
+
+ ALOGD_IF(DEBUG_MODEL, "Response: slotsToHold = %s, slotsToSuppress = %s",
+ slotsToHold.to_string().c_str(), slotsToSuppress.to_string().c_str());
+
+ // Now that we know which slots should be suppressed, let's convert those to pointer id's.
+ std::set<int32_t> newSuppressedIds;
+ for (size_t i = 0; i < args.pointerCount; i++) {
+ const int32_t pointerId = args.pointerProperties[i].id;
+ std::optional<size_t> slot = oldSlotState.getSlotForPointerId(pointerId);
+ if (!slot) {
+ slot = mSlotState.getSlotForPointerId(pointerId);
+ LOG_ALWAYS_FATAL_IF(!slot, "Could not find slot for pointer id %" PRId32, pointerId);
+ }
+ if (slotsToSuppress.test(*slot)) {
+ newSuppressedIds.insert(pointerId);
+ }
+ }
+ return newSuppressedIds;
+}
+
std::vector<NotifyMotionArgs> PalmRejector::processMotion(const NotifyMotionArgs& args) {
if (mPalmDetectionFilter == nullptr) {
return {args};
@@ -633,43 +675,10 @@
if (args.action == AMOTION_EVENT_ACTION_DOWN) {
mSuppressedPointerIds.clear();
}
- std::bitset<::ui::kNumTouchEvdevSlots> slotsToHold;
- std::bitset<::ui::kNumTouchEvdevSlots> slotsToSuppress;
- // Store the slot state before we call getTouches and update it. This way, we can find
- // the slots that have been removed due to the incoming event.
- SlotState oldSlotState = mSlotState;
- mSlotState.update(args);
- std::vector<::ui::InProgressTouchEvdev> touches =
- getTouches(args, mDeviceInfo, oldSlotState, mSlotState);
- ::base::TimeTicks chromeTimestamp = toChromeTimestamp(args.eventTime);
-
- if (DEBUG_MODEL) {
- std::stringstream touchesStream;
- for (const ::ui::InProgressTouchEvdev& touch : touches) {
- touchesStream << touch.tracking_id << " : " << touch << "\n";
- }
- ALOGD("Filter: touches = %s", touchesStream.str().c_str());
- }
- mPalmDetectionFilter->Filter(touches, chromeTimestamp, &slotsToHold, &slotsToSuppress);
-
- ALOGD_IF(DEBUG_MODEL, "Response: slotsToHold = %s, slotsToSuppress = %s",
- slotsToHold.to_string().c_str(), slotsToSuppress.to_string().c_str());
-
- // Now that we know which slots should be suppressed, let's convert those to pointer id's.
std::set<int32_t> oldSuppressedIds;
std::swap(oldSuppressedIds, mSuppressedPointerIds);
- for (size_t i = 0; i < args.pointerCount; i++) {
- const int32_t pointerId = args.pointerProperties[i].id;
- std::optional<size_t> slot = oldSlotState.getSlotForPointerId(pointerId);
- if (!slot) {
- slot = mSlotState.getSlotForPointerId(pointerId);
- LOG_ALWAYS_FATAL_IF(!slot, "Could not find slot for pointer id %" PRId32, pointerId);
- }
- if (slotsToSuppress.test(*slot)) {
- mSuppressedPointerIds.insert(pointerId);
- }
- }
+ mSuppressedPointerIds = detectPalmPointers(args);
std::vector<NotifyMotionArgs> argsWithoutUnwantedPointers =
cancelSuppressedPointers(args, oldSuppressedIds, mSuppressedPointerIds);