Input: Add isStylusToolType() utility function

Using this utility function will ensure we don't forget to check for
both TOOL_TYPE_STYLUS and TOOL_TYPE_ERASER when we are looking to
identify a stylus.

This will cause a behavior change in UnwantedInteractionBlocker, where
TOOL_TYPE_ERASER was not previously considered a stylus tool.

Bug: None
Test: atest inputflinger_tests
Change-Id: I3dae9b5037d7ac08a5672c6e4d6e3b62ee2bd352
diff --git a/include/input/Input.h b/include/input/Input.h
index dd74a51..d298d81 100644
--- a/include/input/Input.h
+++ b/include/input/Input.h
@@ -209,6 +209,8 @@
 
 bool isFromSource(uint32_t source, uint32_t test);
 
+bool isStylusToolType(uint32_t toolType);
+
 /*
  * Flags that flow alongside events in the input dispatch system to help with certain
  * policy decisions such as waking from device sleep.
diff --git a/libs/input/Input.cpp b/libs/input/Input.cpp
index cf5a7e7..3685f54 100644
--- a/libs/input/Input.cpp
+++ b/libs/input/Input.cpp
@@ -238,6 +238,10 @@
     return (source & test) == test;
 }
 
+bool isStylusToolType(uint32_t toolType) {
+    return toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS || toolType == AMOTION_EVENT_TOOL_TYPE_ERASER;
+}
+
 VerifiedKeyEvent verifiedKeyEventFromKeyEvent(const KeyEvent& event) {
     return {{VerifiedInputEvent::Type::KEY, event.getDeviceId(), event.getEventTime(),
              event.getSource(), event.getDisplayId()},
diff --git a/services/inputflinger/PreferStylusOverTouchBlocker.cpp b/services/inputflinger/PreferStylusOverTouchBlocker.cpp
index beec2e1..ddd5146 100644
--- a/services/inputflinger/PreferStylusOverTouchBlocker.cpp
+++ b/services/inputflinger/PreferStylusOverTouchBlocker.cpp
@@ -25,8 +25,7 @@
     for (size_t i = 0; i < args.pointerCount; i++) {
         // Make sure we are canceling stylus pointers
         const int32_t toolType = args.pointerProperties[i].toolType;
-        if (toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
-            toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
+        if (isStylusToolType(toolType)) {
             hasStylus = true;
         }
         if (toolType == AMOTION_EVENT_TOOL_TYPE_FINGER) {
diff --git a/services/inputflinger/UnwantedInteractionBlocker.cpp b/services/inputflinger/UnwantedInteractionBlocker.cpp
index ec41025..c170b81 100644
--- a/services/inputflinger/UnwantedInteractionBlocker.cpp
+++ b/services/inputflinger/UnwantedInteractionBlocker.cpp
@@ -99,14 +99,17 @@
 }
 
 static int getLinuxToolCode(int toolType) {
-    if (toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS) {
-        return BTN_TOOL_PEN;
+    switch (toolType) {
+        case AMOTION_EVENT_TOOL_TYPE_STYLUS:
+            return BTN_TOOL_PEN;
+        case AMOTION_EVENT_TOOL_TYPE_ERASER:
+            return BTN_TOOL_RUBBER;
+        case AMOTION_EVENT_TOOL_TYPE_FINGER:
+            return BTN_TOOL_FINGER;
+        default:
+            ALOGW("Got tool type %" PRId32 ", converting to BTN_TOOL_FINGER", toolType);
+            return BTN_TOOL_FINGER;
     }
-    if (toolType == AMOTION_EVENT_TOOL_TYPE_FINGER) {
-        return BTN_TOOL_FINGER;
-    }
-    ALOGW("Got tool type %" PRId32 ", converting to BTN_TOOL_FINGER", toolType);
-    return BTN_TOOL_FINGER;
 }
 
 static int32_t getActionUpForPointerId(const NotifyMotionArgs& args, int32_t pointerId) {
@@ -195,7 +198,7 @@
 static std::optional<NotifyMotionArgs> removeStylusPointerIds(const NotifyMotionArgs& args) {
     std::set<int32_t> stylusPointerIds;
     for (uint32_t i = 0; i < args.pointerCount; i++) {
-        if (args.pointerProperties[i].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS) {
+        if (isStylusToolType(args.pointerProperties[i].toolType)) {
             stylusPointerIds.insert(args.pointerProperties[i].id);
         }
     }
diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp
index f698217..4091310 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.cpp
+++ b/services/inputflinger/dispatcher/InputDispatcher.cpp
@@ -479,8 +479,7 @@
 
 bool isPointerFromStylus(const MotionEntry& entry, int32_t pointerIndex) {
     return isFromSource(entry.source, AINPUT_SOURCE_STYLUS) &&
-            (entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
-             entry.pointerProperties[pointerIndex].toolType == AMOTION_EVENT_TOOL_TYPE_ERASER);
+            isStylusToolType(entry.pointerProperties[pointerIndex].toolType);
 }
 
 // Determines if the given window can be targeted as InputTarget::FLAG_FOREGROUND.
diff --git a/services/inputflinger/reader/InputReader.cpp b/services/inputflinger/reader/InputReader.cpp
index f8b1b3f..f04a646 100644
--- a/services/inputflinger/reader/InputReader.cpp
+++ b/services/inputflinger/reader/InputReader.cpp
@@ -66,8 +66,7 @@
         return false;
     }
     const auto actionIndex = MotionEvent::getActionIndex(motionArgs.action);
-    return motionArgs.pointerProperties[actionIndex].toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
-            motionArgs.pointerProperties[actionIndex].toolType == AMOTION_EVENT_TOOL_TYPE_ERASER;
+    return isStylusToolType(motionArgs.pointerProperties[actionIndex].toolType);
 }
 
 // --- InputReader ---
diff --git a/services/inputflinger/reader/mapper/TouchInputMapper.cpp b/services/inputflinger/reader/mapper/TouchInputMapper.cpp
index f8d6cf9..7db73db 100644
--- a/services/inputflinger/reader/mapper/TouchInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/TouchInputMapper.cpp
@@ -1595,8 +1595,7 @@
             uint32_t id = idBits.clearFirstMarkedBit();
             const RawPointerData::Pointer& pointer =
                     mCurrentRawState.rawPointerData.pointerForId(id);
-            if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
-                pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
+            if (isStylusToolType(pointer.toolType)) {
                 mCurrentCookedState.stylusIdBits.markBit(id);
             } else if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_FINGER ||
                        pointer.toolType == AMOTION_EVENT_TOOL_TYPE_UNKNOWN) {
@@ -1609,8 +1608,7 @@
             uint32_t id = idBits.clearFirstMarkedBit();
             const RawPointerData::Pointer& pointer =
                     mCurrentRawState.rawPointerData.pointerForId(id);
-            if (pointer.toolType == AMOTION_EVENT_TOOL_TYPE_STYLUS ||
-                pointer.toolType == AMOTION_EVENT_TOOL_TYPE_ERASER) {
+            if (isStylusToolType(pointer.toolType)) {
                 mCurrentCookedState.stylusIdBits.markBit(id);
             }
         }