Log when a pointer is rejected

The previous log condition was incorrect.
To simplify the logic, we only need to check whether the set has
changed. When pointers are removed, the suppressed slots will be removed
accordingly. We can guard for this condition by requiring that the new
set of pointers is larger than the previous set.

It's still possible that the new set is smaller in size, and at the same
time, some of the pointers have changed. Such a condition should not
occur in practice because once a slot becomes palm, it should remain a
palm. We can consider improving this logic in the future.

Sample logs:
UnwantedInteractionBlocker: Palm detected, removing pointer ids {0, 1} after 109ms from NotifyMotionArgs(id=258638844, eventTime=7856563228000, deviceId=4, source=TOUCHSCREEN, action=MOVE, pointerCount=2 pointers={0: id=0 x=1847.0 y=729.0, pressure=1.0}, {1: id=1 x=1549.0 y=527.0, pressure=0.4}, flags=0x00000000)

Example dump:
UnwantedInteractionBlocker:
  mPreferStylusOverTouchBlocker:
    mActiveStyli: {}
    mLastTouchEvents: 4:NotifyMotionArgs(id=477963177, eventTime=67984771964000, deviceId=4, source=TOUCHSCREEN, action=MOVE, pointerCount=1 pointers={0: id=0 x=2078.0 y=740.0, pressure=1.0}, flags=0x00000000)
    mDevicesWithMixedToolType: {}
    mCanceledDevices: {}
  mEnablePalmRejection: 1
  isPalmRejectionEnabled (flag value): 1
  mPalmRejectors:
    deviceId = 4:
      mDeviceInfo:
        PalmFilterDeviceInfo(max_x=2559, max_y=1599, x_res=11, y_res=11, major_radius_res=1, minor_radius_res=1, minor_radius_supported=1), touch_major_res=1, touch_minor_res=1
      mSlotState:
        mSlotsByPointerId:
          0:0
        mPointerIdsBySlot:
          0:0
      mSuppressedPointerIds: {}
      mSharedPalmState: SharedPalmDetectionFilterState(
        latest_stylus_touch_time = 0 bogo-microseconds (67984.9 s from now)
        latest_finger_touch_time = -1657600374977818 bogo-microseconds
        active_finger_touches = 1
        active_palm_touches = 0
        latest_palm_touch_time = -1657600427078938 bogo-microseconds
        Now() = 67984909970 bogo-microseconds
      )
      mPalmDetectionFilter:
        NeuralStylusPalmDetectionFilter(
          is_palm_ = 00000000000000000000
          is_delay_ = 00000000000000000000
          strokes_ =
            0 : PalmFilterStroke(
              GetCentroid() = PointF(188.909, 67.2389)
              BiggestSize() = 576
              MaxMajorRadius() = 24
              samples (11 total):
                PalmFilterSample(major=16, minor=16, pressure=0.726562, edge=43.7273, tracking_id=0, point=PointF(188.909, 67.1818), time=-1657600374977818 bogo-microseconds)
                PalmFilterSample(major=17, minor=17, pressure=0.894531, edge=43.7273, tracking_id=0, point=PointF(188.909, 67.1818), time=-1657600374969818 bogo-microseconds)
                PalmFilterSample(major=20, minor=20, pressure=0.996094, edge=43.7273, tracking_id=0, point=PointF(188.909, 67.1818), time=-1657600374961818 bogo-microseconds)
                PalmFilterSample(major=22, minor=22, pressure=0.996094, edge=43.7273, tracking_id=0, point=PointF(188.909, 67.1818), time=-1657600374953818 bogo-microseconds)
                PalmFilterSample(major=22, minor=22, pressure=0.996094, edge=43.7273, tracking_id=0, point=PointF(188.909, 67.2642), time=-1657600374945818 bogo-microseconds)
                PalmFilterSample(major=23, minor=23, pressure=0.996094, edge=43.7273, tracking_id=0, point=PointF(188.909, 67.2727), time=-1657600374937818 bogo-microseconds)
                PalmFilterSample(major=24, minor=24, pressure=0.996094, edge=43.7273, tracking_id=0, point=PointF(188.909, 67.2727), time=-1657600374929818 bogo-microseconds)
                PalmFilterSample(major=24, minor=24, pressure=0.996094, edge=43.7273, tracking_id=0, point=PointF(188.909, 67.2727), time=-1657600374921818 bogo-microseconds)
                PalmFilterSample(major=24, minor=24, pressure=0.996094, edge=43.7273, tracking_id=0, point=PointF(188.909, 67.2727), time=-1657600374913818 bogo-microseconds)
                PalmFilterSample(major=24, minor=24, pressure=0.996094, edge=43.7273, tracking_id=0, point=PointF(188.909, 67.2727), time=-1657600374905818 bogo-microseconds)
                PalmFilterSample(major=24, minor=24, pressure=0.996094, edge=43.7273, tracking_id=0, point=PointF(188.909, 67.2727), time=-1657600374897818 bogo-microseconds)

              samples_seen() = 11
              tracking_id() = 0
              max_sample_count_ = 12
              resample_period_ = 0.008 s
              last_sample_ = PalmFilterSample(major=24, minor=24, pressure=0.996094, edge=43.7273, tracking_id=0, point=PointF(188.909, 67.2727), time=-1657600374895898 bogo-microseconds)
              unscaled_centroid_ = PointF(2078, 739.628)
              unscaled_centroid_sum_error_ = Vector2dF(3.05176e-05, -3.05176e-05)
            )

          previous_report_time_ = -1657600374895898 bogo-microseconds
          active_tracking_ids_ = {0, }
          tracking_ids_count_within_session_ = 1
          tracking_ids = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]
          palm_filter_dev_info_ = PalmFilterDeviceInfo(max_x=2559, max_y=1599, x_res=11, y_res=11, major_radius_res=1, minor_radius_res=1, minor_radius_supported=1)
        )

Bug: 198472780
Test: logcatcolor | grep -i palm
Merged-In: Idf2c887b0a04a15f5e398d82bff5a6ee8b03fea8
Change-Id: Idf2c887b0a04a15f5e398d82bff5a6ee8b03fea8
(cherry picked from commit 47494efddc42a233ef46fbe22f7d47c0c3ebe7d5)
diff --git a/services/inputflinger/UnwantedInteractionBlocker.cpp b/services/inputflinger/UnwantedInteractionBlocker.cpp
index 725f33f..a8e495f 100644
--- a/services/inputflinger/UnwantedInteractionBlocker.cpp
+++ b/services/inputflinger/UnwantedInteractionBlocker.cpp
@@ -29,6 +29,14 @@
 
 using android::base::StringPrintf;
 
+/**
+ * This type is declared here to ensure consistency between the instantiated type (used in the
+ * constructor via std::make_unique) and the cast-to type (used in PalmRejector::dump() with
+ * static_cast). Due to the lack of rtti support, dynamic_cast is not available, so this can't be
+ * checked at runtime to avoid undefined behaviour.
+ */
+using PalmFilterImplementation = ::ui::NeuralStylusPalmDetectionFilter;
+
 namespace android {
 
 // Category (=namespace) name for the input settings that are applied at boot time
@@ -81,21 +89,6 @@
     return MT_TOOL_FINGER;
 }
 
-static std::string dumpDeviceInfo(const AndroidPalmFilterDeviceInfo& info) {
-    std::string out;
-    out += StringPrintf("max_x = %.2f\n", info.max_x);
-    out += StringPrintf("max_y = %.2f\n", info.max_y);
-    out += StringPrintf("x_res = %.2f\n", info.x_res);
-    out += StringPrintf("y_res = %.2f\n", info.y_res);
-    out += StringPrintf("major_radius_res = %.2f\n", info.major_radius_res);
-    out += StringPrintf("minor_radius_res = %.2f\n", info.minor_radius_res);
-    out += StringPrintf("minor_radius_supported = %s\n",
-                        info.minor_radius_supported ? "true" : "false");
-    out += StringPrintf("touch_major_res = %" PRId32 "\n", info.touch_major_res);
-    out += StringPrintf("touch_minor_res = %" PRId32 "\n", info.touch_minor_res);
-    return out;
-}
-
 static int32_t getActionUpForPointerId(const NotifyMotionArgs& args, int32_t pointerId) {
     for (size_t i = 0; i < args.pointerCount; i++) {
         if (pointerId == args.pointerProperties[i].id) {
@@ -128,15 +121,6 @@
     return AMOTION_EVENT_ACTION_MOVE;
 }
 
-std::string toString(const ::ui::InProgressTouchEvdev& touch) {
-    return StringPrintf("x=%.1f, y=%.1f, tracking_id=%i, slot=%zu,"
-                        " pressure=%.1f, major=%i, minor=%i, "
-                        "tool_type=%i, altered=%s, was_touching=%s, touching=%s",
-                        touch.x, touch.y, touch.tracking_id, touch.slot, touch.pressure,
-                        touch.major, touch.minor, touch.tool_type, toString(touch.altered),
-                        toString(touch.was_touching), toString(touch.touching));
-}
-
 /**
  * Remove the data for the provided pointers from the args. The pointers are identified by their
  * pointerId, not by the index inside the array.
@@ -428,9 +412,10 @@
     dump += "UnwantedInteractionBlocker:\n";
     dump += "  mPreferStylusOverTouchBlocker:\n";
     dump += addLinePrefix(mPreferStylusOverTouchBlocker.dump(), "    ");
-    dump += StringPrintf("  mEnablePalmRejection: %s\n", toString(mEnablePalmRejection));
+    dump += StringPrintf("  mEnablePalmRejection: %s\n",
+                         std::to_string(mEnablePalmRejection).c_str());
     dump += StringPrintf("  isPalmRejectionEnabled (flag value): %s\n",
-                         toString(isPalmRejectionEnabled()));
+                         std::to_string(isPalmRejectionEnabled()).c_str());
     dump += mPalmRejectors.empty() ? "  mPalmRejectors: None\n" : "  mPalmRejectors:\n";
     for (const auto& [deviceId, palmRejector] : mPalmRejectors) {
         dump += StringPrintf("    deviceId = %" PRId32 ":\n", deviceId);
@@ -533,9 +518,8 @@
     }
     std::unique_ptr<::ui::NeuralStylusPalmDetectionFilterModel> model =
             std::make_unique<AndroidPalmRejectionModel>();
-    mPalmDetectionFilter =
-            std::make_unique<::ui::NeuralStylusPalmDetectionFilter>(mDeviceInfo, std::move(model),
-                                                                    mSharedPalmState.get());
+    mPalmDetectionFilter = std::make_unique<PalmFilterImplementation>(mDeviceInfo, std::move(model),
+                                                                      mSharedPalmState.get());
 }
 
 std::vector<::ui::InProgressTouchEvdev> getTouches(const NotifyMotionArgs& args,
@@ -656,12 +640,13 @@
         LOG_ALWAYS_FATAL_IF(checkArgs.action == ACTION_UNKNOWN, "%s", checkArgs.dump().c_str());
     }
 
-    if (mSuppressedPointerIds != oldSuppressedIds) {
-        if (argsWithoutUnwantedPointers.size() != 1 ||
-            argsWithoutUnwantedPointers[0].pointerCount != args.pointerCount) {
-            ALOGI("Palm detected, removing pointer ids %s from %s",
-                  dumpSet(mSuppressedPointerIds).c_str(), args.dump().c_str());
-        }
+    // Only log if new pointers are getting rejected. That means mSuppressedPointerIds is not a
+    // subset of oldSuppressedIds.
+    if (!std::includes(oldSuppressedIds.begin(), oldSuppressedIds.end(),
+                       mSuppressedPointerIds.begin(), mSuppressedPointerIds.end())) {
+        ALOGI("Palm detected, removing pointer ids %s after %" PRId64 "ms from %s",
+              dumpSet(mSuppressedPointerIds).c_str(), ns2ms(args.eventTime - args.downTime),
+              args.dump().c_str());
     }
 
     return argsWithoutUnwantedPointers;
@@ -674,11 +659,21 @@
 std::string PalmRejector::dump() const {
     std::string out;
     out += "mDeviceInfo:\n";
-    out += addLinePrefix(dumpDeviceInfo(mDeviceInfo), "  ");
+    std::stringstream deviceInfo;
+    deviceInfo << mDeviceInfo << ", touch_major_res=" << mDeviceInfo.touch_major_res
+               << ", touch_minor_res=" << mDeviceInfo.touch_minor_res << "\n";
+    out += addLinePrefix(deviceInfo.str(), "  ");
     out += "mSlotState:\n";
     out += addLinePrefix(mSlotState.dump(), "  ");
     out += "mSuppressedPointerIds: ";
     out += dumpSet(mSuppressedPointerIds) + "\n";
+    std::stringstream state;
+    state << *mSharedPalmState;
+    out += "mSharedPalmState: " + state.str() + "\n";
+    std::stringstream filter;
+    filter << static_cast<const PalmFilterImplementation&>(*mPalmDetectionFilter);
+    out += "mPalmDetectionFilter:\n";
+    out += addLinePrefix(filter.str(), "  ") + "\n";
     return out;
 }