Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "UnwantedInteractionBlocker" |
| 18 | #include "UnwantedInteractionBlocker.h" |
| 19 | |
| 20 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame^] | 21 | #include <ftl/enum.h> |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 22 | #include <input/PrintTools.h> |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 23 | #include <inttypes.h> |
| 24 | #include <linux/input-event-codes.h> |
| 25 | #include <linux/input.h> |
| 26 | #include <server_configurable_flags/get_flags.h> |
| 27 | |
| 28 | #include "ui/events/ozone/evdev/touch_filter/neural_stylus_palm_detection_filter.h" |
| 29 | #include "ui/events/ozone/evdev/touch_filter/palm_model/onedevice_train_palm_detection_filter_model.h" |
| 30 | |
| 31 | using android::base::StringPrintf; |
| 32 | |
Siarhei Vishniakou | 15a5c5a | 2022-07-06 15:42:57 -0700 | [diff] [blame] | 33 | /** |
| 34 | * This type is declared here to ensure consistency between the instantiated type (used in the |
| 35 | * constructor via std::make_unique) and the cast-to type (used in PalmRejector::dump() with |
| 36 | * static_cast). Due to the lack of rtti support, dynamic_cast is not available, so this can't be |
| 37 | * checked at runtime to avoid undefined behaviour. |
| 38 | */ |
| 39 | using PalmFilterImplementation = ::ui::NeuralStylusPalmDetectionFilter; |
| 40 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | |
Siarhei Vishniakou | d5fe518 | 2022-07-20 23:28:40 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Log detailed debug messages about each inbound motion event notification to the blocker. |
| 45 | * Enable this via "adb shell setprop log.tag.UnwantedInteractionBlockerInboundMotion DEBUG" |
| 46 | * (requires restart) |
| 47 | */ |
| 48 | const bool DEBUG_INBOUND_MOTION = |
| 49 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "InboundMotion", ANDROID_LOG_INFO); |
| 50 | |
| 51 | /** |
| 52 | * Log detailed debug messages about each outbound motion event processed by the blocker. |
| 53 | * Enable this via "adb shell setprop log.tag.UnwantedInteractionBlockerOutboundMotion DEBUG" |
| 54 | * (requires restart) |
| 55 | */ |
| 56 | const bool DEBUG_OUTBOUND_MOTION = |
| 57 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "OutboundMotion", ANDROID_LOG_INFO); |
| 58 | |
| 59 | /** |
| 60 | * Log the data sent to the model and received back from the model. |
| 61 | * Enable this via "adb shell setprop log.tag.UnwantedInteractionBlockerModel DEBUG" |
| 62 | * (requires restart) |
| 63 | */ |
| 64 | const bool DEBUG_MODEL = |
| 65 | __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG "Model", ANDROID_LOG_INFO); |
| 66 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 67 | // Category (=namespace) name for the input settings that are applied at boot time |
| 68 | static const char* INPUT_NATIVE_BOOT = "input_native_boot"; |
| 69 | /** |
| 70 | * Feature flag name. This flag determines whether palm rejection is enabled. To enable, specify |
| 71 | * 'true' (not case sensitive) or '1'. To disable, specify any other value. |
| 72 | */ |
| 73 | static const char* PALM_REJECTION_ENABLED = "palm_rejection_enabled"; |
| 74 | |
| 75 | static std::string toLower(std::string s) { |
| 76 | std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); }); |
| 77 | return s; |
| 78 | } |
| 79 | |
| 80 | static bool isFromTouchscreen(int32_t source) { |
Siarhei Vishniakou | 6573583 | 2022-08-09 19:18:37 +0000 | [diff] [blame] | 81 | return isFromSource(source, AINPUT_SOURCE_TOUCHSCREEN); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static ::base::TimeTicks toChromeTimestamp(nsecs_t eventTime) { |
Siarhei Vishniakou | 229a880 | 2022-07-28 21:58:56 +0000 | [diff] [blame] | 85 | return ::base::TimeTicks::UnixEpoch() + ::base::TimeDelta::FromNanosecondsD(eventTime); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Return true if palm rejection is enabled via the server configurable flags. Return false |
| 90 | * otherwise. |
| 91 | */ |
| 92 | static bool isPalmRejectionEnabled() { |
| 93 | std::string value = toLower( |
| 94 | server_configurable_flags::GetServerConfigurableFlag(INPUT_NATIVE_BOOT, |
Josep del Rio | 4b20779 | 2022-07-29 16:39:03 +0000 | [diff] [blame] | 95 | PALM_REJECTION_ENABLED, "0")); |
| 96 | if (value == "1") { |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 97 | return true; |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame^] | 102 | static int getLinuxToolCode(ToolType toolType) { |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 103 | switch (toolType) { |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame^] | 104 | case ToolType::STYLUS: |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 105 | return BTN_TOOL_PEN; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame^] | 106 | case ToolType::ERASER: |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 107 | return BTN_TOOL_RUBBER; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame^] | 108 | case ToolType::FINGER: |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 109 | return BTN_TOOL_FINGER; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame^] | 110 | case ToolType::UNKNOWN: |
| 111 | case ToolType::MOUSE: |
| 112 | case ToolType::PALM: |
| 113 | break; |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 114 | } |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame^] | 115 | ALOGW("Got tool type %s, converting to BTN_TOOL_FINGER", ftl::enum_string(toolType).c_str()); |
| 116 | return BTN_TOOL_FINGER; |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 119 | static int32_t getActionUpForPointerId(const NotifyMotionArgs& args, int32_t pointerId) { |
| 120 | for (size_t i = 0; i < args.pointerCount; i++) { |
| 121 | if (pointerId == args.pointerProperties[i].id) { |
| 122 | return AMOTION_EVENT_ACTION_POINTER_UP | |
| 123 | (i << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 124 | } |
| 125 | } |
| 126 | LOG_ALWAYS_FATAL("Can't find pointerId %" PRId32 " in %s", pointerId, args.dump().c_str()); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Find the action for individual pointer at the given pointer index. |
| 131 | * This is always equal to MotionEvent::getActionMasked, except for |
| 132 | * POINTER_UP or POINTER_DOWN events. For example, in a POINTER_UP event, the action for |
| 133 | * the active pointer is ACTION_POINTER_UP, while the action for the other pointers is ACTION_MOVE. |
| 134 | */ |
| 135 | static int32_t resolveActionForPointer(uint8_t pointerIndex, int32_t action) { |
| 136 | const int32_t actionMasked = MotionEvent::getActionMasked(action); |
| 137 | if (actionMasked != AMOTION_EVENT_ACTION_POINTER_DOWN && |
| 138 | actionMasked != AMOTION_EVENT_ACTION_POINTER_UP) { |
| 139 | return actionMasked; |
| 140 | } |
| 141 | // This is a POINTER_DOWN or POINTER_UP event |
| 142 | const uint8_t actionIndex = MotionEvent::getActionIndex(action); |
| 143 | if (pointerIndex == actionIndex) { |
| 144 | return actionMasked; |
| 145 | } |
| 146 | // When POINTER_DOWN or POINTER_UP happens, it's actually a MOVE for all of the other |
| 147 | // pointers |
| 148 | return AMOTION_EVENT_ACTION_MOVE; |
| 149 | } |
| 150 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 151 | NotifyMotionArgs removePointerIds(const NotifyMotionArgs& args, |
| 152 | const std::set<int32_t>& pointerIds) { |
| 153 | const uint8_t actionIndex = MotionEvent::getActionIndex(args.action); |
| 154 | const int32_t actionMasked = MotionEvent::getActionMasked(args.action); |
| 155 | const bool isPointerUpOrDownAction = actionMasked == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 156 | actionMasked == AMOTION_EVENT_ACTION_POINTER_UP; |
| 157 | |
| 158 | NotifyMotionArgs newArgs{args}; |
| 159 | newArgs.pointerCount = 0; |
| 160 | int32_t newActionIndex = 0; |
| 161 | for (uint32_t i = 0; i < args.pointerCount; i++) { |
| 162 | const int32_t pointerId = args.pointerProperties[i].id; |
| 163 | if (pointerIds.find(pointerId) != pointerIds.end()) { |
| 164 | // skip this pointer |
| 165 | if (isPointerUpOrDownAction && i == actionIndex) { |
| 166 | // The active pointer is being removed, so the action is no longer valid. |
| 167 | // Set the action to 'UNKNOWN' here. The caller is responsible for updating this |
| 168 | // action later to a proper value. |
| 169 | newArgs.action = ACTION_UNKNOWN; |
| 170 | } |
| 171 | continue; |
| 172 | } |
| 173 | newArgs.pointerProperties[newArgs.pointerCount].copyFrom(args.pointerProperties[i]); |
| 174 | newArgs.pointerCoords[newArgs.pointerCount].copyFrom(args.pointerCoords[i]); |
| 175 | if (i == actionIndex) { |
| 176 | newActionIndex = newArgs.pointerCount; |
| 177 | } |
| 178 | newArgs.pointerCount++; |
| 179 | } |
| 180 | // Update POINTER_DOWN or POINTER_UP actions |
| 181 | if (isPointerUpOrDownAction && newArgs.action != ACTION_UNKNOWN) { |
| 182 | newArgs.action = |
| 183 | actionMasked | (newActionIndex << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT); |
| 184 | // Convert POINTER_DOWN and POINTER_UP to DOWN and UP if there's only 1 pointer remaining |
| 185 | if (newArgs.pointerCount == 1) { |
| 186 | if (actionMasked == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 187 | newArgs.action = AMOTION_EVENT_ACTION_DOWN; |
| 188 | } else if (actionMasked == AMOTION_EVENT_ACTION_POINTER_UP) { |
| 189 | newArgs.action = AMOTION_EVENT_ACTION_UP; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | return newArgs; |
| 194 | } |
| 195 | |
Siarhei Vishniakou | 6573583 | 2022-08-09 19:18:37 +0000 | [diff] [blame] | 196 | /** |
| 197 | * Remove stylus pointers from the provided NotifyMotionArgs. |
| 198 | * |
| 199 | * Return NotifyMotionArgs where the stylus pointers have been removed. |
| 200 | * If this results in removal of the active pointer, then return nullopt. |
| 201 | */ |
| 202 | static std::optional<NotifyMotionArgs> removeStylusPointerIds(const NotifyMotionArgs& args) { |
| 203 | std::set<int32_t> stylusPointerIds; |
| 204 | for (uint32_t i = 0; i < args.pointerCount; i++) { |
Prabir Pradhan | e562696 | 2022-10-27 20:30:53 +0000 | [diff] [blame] | 205 | if (isStylusToolType(args.pointerProperties[i].toolType)) { |
Siarhei Vishniakou | 6573583 | 2022-08-09 19:18:37 +0000 | [diff] [blame] | 206 | stylusPointerIds.insert(args.pointerProperties[i].id); |
| 207 | } |
| 208 | } |
| 209 | NotifyMotionArgs withoutStylusPointers = removePointerIds(args, stylusPointerIds); |
| 210 | if (withoutStylusPointers.pointerCount == 0 || withoutStylusPointers.action == ACTION_UNKNOWN) { |
| 211 | return std::nullopt; |
| 212 | } |
| 213 | return withoutStylusPointers; |
| 214 | } |
| 215 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 216 | std::optional<AndroidPalmFilterDeviceInfo> createPalmFilterDeviceInfo( |
| 217 | const InputDeviceInfo& deviceInfo) { |
| 218 | if (!isFromTouchscreen(deviceInfo.getSources())) { |
| 219 | return std::nullopt; |
| 220 | } |
| 221 | AndroidPalmFilterDeviceInfo out; |
| 222 | const InputDeviceInfo::MotionRange* axisX = |
| 223 | deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN); |
| 224 | if (axisX != nullptr) { |
| 225 | out.max_x = axisX->max; |
| 226 | out.x_res = axisX->resolution; |
| 227 | } else { |
| 228 | ALOGW("Palm rejection is disabled for %s because AXIS_X is not supported", |
| 229 | deviceInfo.getDisplayName().c_str()); |
| 230 | return std::nullopt; |
| 231 | } |
| 232 | const InputDeviceInfo::MotionRange* axisY = |
| 233 | deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN); |
| 234 | if (axisY != nullptr) { |
| 235 | out.max_y = axisY->max; |
| 236 | out.y_res = axisY->resolution; |
| 237 | } else { |
| 238 | ALOGW("Palm rejection is disabled for %s because AXIS_Y is not supported", |
| 239 | deviceInfo.getDisplayName().c_str()); |
| 240 | return std::nullopt; |
| 241 | } |
| 242 | const InputDeviceInfo::MotionRange* axisMajor = |
| 243 | deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_TOUCH_MAJOR, AINPUT_SOURCE_TOUCHSCREEN); |
| 244 | if (axisMajor != nullptr) { |
| 245 | out.major_radius_res = axisMajor->resolution; |
| 246 | out.touch_major_res = axisMajor->resolution; |
| 247 | } else { |
| 248 | return std::nullopt; |
| 249 | } |
| 250 | const InputDeviceInfo::MotionRange* axisMinor = |
| 251 | deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_TOUCH_MINOR, AINPUT_SOURCE_TOUCHSCREEN); |
| 252 | if (axisMinor != nullptr) { |
| 253 | out.minor_radius_res = axisMinor->resolution; |
| 254 | out.touch_minor_res = axisMinor->resolution; |
| 255 | out.minor_radius_supported = true; |
| 256 | } else { |
| 257 | out.minor_radius_supported = false; |
| 258 | } |
| 259 | |
| 260 | return out; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Synthesize CANCEL events for any new pointers that should be canceled, while removing pointers |
| 265 | * that have already been canceled. |
| 266 | * The flow of the function is as follows: |
| 267 | * 1. Remove all already canceled pointers |
| 268 | * 2. Cancel all newly suppressed pointers |
| 269 | * 3. Decide what to do with the current event : keep it, or drop it |
| 270 | * The pointers can never be "unsuppressed": once a pointer is canceled, it will never become valid. |
| 271 | */ |
| 272 | std::vector<NotifyMotionArgs> cancelSuppressedPointers( |
| 273 | const NotifyMotionArgs& args, const std::set<int32_t>& oldSuppressedPointerIds, |
| 274 | const std::set<int32_t>& newSuppressedPointerIds) { |
| 275 | LOG_ALWAYS_FATAL_IF(args.pointerCount == 0, "0 pointers in %s", args.dump().c_str()); |
| 276 | |
| 277 | // First, let's remove the old suppressed pointers. They've already been canceled previously. |
| 278 | NotifyMotionArgs oldArgs = removePointerIds(args, oldSuppressedPointerIds); |
| 279 | |
| 280 | // Cancel any newly suppressed pointers. |
| 281 | std::vector<NotifyMotionArgs> out; |
| 282 | const int32_t activePointerId = |
| 283 | args.pointerProperties[MotionEvent::getActionIndex(args.action)].id; |
| 284 | const int32_t actionMasked = MotionEvent::getActionMasked(args.action); |
| 285 | // We will iteratively remove pointers from 'removedArgs'. |
| 286 | NotifyMotionArgs removedArgs{oldArgs}; |
| 287 | for (uint32_t i = 0; i < oldArgs.pointerCount; i++) { |
| 288 | const int32_t pointerId = oldArgs.pointerProperties[i].id; |
| 289 | if (newSuppressedPointerIds.find(pointerId) == newSuppressedPointerIds.end()) { |
| 290 | // This is a pointer that should not be canceled. Move on. |
| 291 | continue; |
| 292 | } |
| 293 | if (pointerId == activePointerId && actionMasked == AMOTION_EVENT_ACTION_POINTER_DOWN) { |
| 294 | // Remove this pointer, but don't cancel it. We'll just not send the POINTER_DOWN event |
| 295 | removedArgs = removePointerIds(removedArgs, {pointerId}); |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | if (removedArgs.pointerCount == 1) { |
| 300 | // We are about to remove the last pointer, which means there will be no more gesture |
| 301 | // remaining. This is identical to canceling all pointers, so just send a single CANCEL |
| 302 | // event, without any of the preceding POINTER_UP with FLAG_CANCELED events. |
| 303 | oldArgs.flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 304 | oldArgs.action = AMOTION_EVENT_ACTION_CANCEL; |
| 305 | return {oldArgs}; |
| 306 | } |
| 307 | // Cancel the current pointer |
| 308 | out.push_back(removedArgs); |
| 309 | out.back().flags |= AMOTION_EVENT_FLAG_CANCELED; |
| 310 | out.back().action = getActionUpForPointerId(out.back(), pointerId); |
| 311 | |
| 312 | // Remove the newly canceled pointer from the args |
| 313 | removedArgs = removePointerIds(removedArgs, {pointerId}); |
| 314 | } |
| 315 | |
| 316 | // Now 'removedArgs' contains only pointers that are valid. |
| 317 | if (removedArgs.pointerCount <= 0 || removedArgs.action == ACTION_UNKNOWN) { |
| 318 | return out; |
| 319 | } |
| 320 | out.push_back(removedArgs); |
| 321 | return out; |
| 322 | } |
| 323 | |
| 324 | UnwantedInteractionBlocker::UnwantedInteractionBlocker(InputListenerInterface& listener) |
| 325 | : UnwantedInteractionBlocker(listener, isPalmRejectionEnabled()){}; |
| 326 | |
| 327 | UnwantedInteractionBlocker::UnwantedInteractionBlocker(InputListenerInterface& listener, |
| 328 | bool enablePalmRejection) |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 329 | : mQueuedListener(listener), mEnablePalmRejection(enablePalmRejection) {} |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 330 | |
| 331 | void UnwantedInteractionBlocker::notifyConfigurationChanged( |
| 332 | const NotifyConfigurationChangedArgs* args) { |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 333 | mQueuedListener.notifyConfigurationChanged(args); |
| 334 | mQueuedListener.flush(); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | void UnwantedInteractionBlocker::notifyKey(const NotifyKeyArgs* args) { |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 338 | mQueuedListener.notifyKey(args); |
| 339 | mQueuedListener.flush(); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void UnwantedInteractionBlocker::notifyMotion(const NotifyMotionArgs* args) { |
Siarhei Vishniakou | d5fe518 | 2022-07-20 23:28:40 +0000 | [diff] [blame] | 343 | ALOGD_IF(DEBUG_INBOUND_MOTION, "%s: %s", __func__, args->dump().c_str()); |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 344 | { // acquire lock |
| 345 | std::scoped_lock lock(mLock); |
| 346 | const std::vector<NotifyMotionArgs> processedArgs = |
| 347 | mPreferStylusOverTouchBlocker.processMotion(*args); |
| 348 | for (const NotifyMotionArgs& loopArgs : processedArgs) { |
| 349 | notifyMotionLocked(&loopArgs); |
| 350 | } |
| 351 | } // release lock |
| 352 | |
| 353 | // Call out to the next stage without holding the lock |
| 354 | mQueuedListener.flush(); |
Siarhei Vishniakou | 814ace3 | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Siarhei Vishniakou | d5fe518 | 2022-07-20 23:28:40 +0000 | [diff] [blame] | 357 | void UnwantedInteractionBlocker::enqueueOutboundMotionLocked(const NotifyMotionArgs& args) { |
| 358 | ALOGD_IF(DEBUG_OUTBOUND_MOTION, "%s: %s", __func__, args.dump().c_str()); |
| 359 | mQueuedListener.notifyMotion(&args); |
| 360 | } |
| 361 | |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 362 | void UnwantedInteractionBlocker::notifyMotionLocked(const NotifyMotionArgs* args) { |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 363 | auto it = mPalmRejectors.find(args->deviceId); |
| 364 | const bool sendToPalmRejector = it != mPalmRejectors.end() && isFromTouchscreen(args->source); |
| 365 | if (!sendToPalmRejector) { |
Siarhei Vishniakou | d5fe518 | 2022-07-20 23:28:40 +0000 | [diff] [blame] | 366 | enqueueOutboundMotionLocked(*args); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 367 | return; |
| 368 | } |
| 369 | |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 370 | std::vector<NotifyMotionArgs> processedArgs = it->second.processMotion(*args); |
| 371 | for (const NotifyMotionArgs& loopArgs : processedArgs) { |
Siarhei Vishniakou | d5fe518 | 2022-07-20 23:28:40 +0000 | [diff] [blame] | 372 | enqueueOutboundMotionLocked(loopArgs); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | void UnwantedInteractionBlocker::notifySwitch(const NotifySwitchArgs* args) { |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 377 | mQueuedListener.notifySwitch(args); |
| 378 | mQueuedListener.flush(); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | void UnwantedInteractionBlocker::notifySensor(const NotifySensorArgs* args) { |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 382 | mQueuedListener.notifySensor(args); |
| 383 | mQueuedListener.flush(); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void UnwantedInteractionBlocker::notifyVibratorState(const NotifyVibratorStateArgs* args) { |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 387 | mQueuedListener.notifyVibratorState(args); |
| 388 | mQueuedListener.flush(); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 389 | } |
| 390 | void UnwantedInteractionBlocker::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 391 | { // acquire lock |
| 392 | std::scoped_lock lock(mLock); |
| 393 | auto it = mPalmRejectors.find(args->deviceId); |
| 394 | if (it != mPalmRejectors.end()) { |
| 395 | AndroidPalmFilterDeviceInfo info = it->second.getPalmFilterDeviceInfo(); |
| 396 | // Re-create the object instead of resetting it |
| 397 | mPalmRejectors.erase(it); |
| 398 | mPalmRejectors.emplace(args->deviceId, info); |
| 399 | } |
| 400 | mQueuedListener.notifyDeviceReset(args); |
| 401 | mPreferStylusOverTouchBlocker.notifyDeviceReset(*args); |
| 402 | } // release lock |
| 403 | // Send events to the next stage without holding the lock |
| 404 | mQueuedListener.flush(); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | void UnwantedInteractionBlocker::notifyPointerCaptureChanged( |
| 408 | const NotifyPointerCaptureChangedArgs* args) { |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 409 | mQueuedListener.notifyPointerCaptureChanged(args); |
| 410 | mQueuedListener.flush(); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | void UnwantedInteractionBlocker::notifyInputDevicesChanged( |
| 414 | const std::vector<InputDeviceInfo>& inputDevices) { |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 415 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 416 | if (!mEnablePalmRejection) { |
| 417 | // Palm rejection is disabled. Don't create any palm rejector objects. |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | // Let's see which of the existing devices didn't change, so that we can keep them |
| 422 | // and prevent event stream disruption |
| 423 | std::set<int32_t /*deviceId*/> devicesToKeep; |
| 424 | for (const InputDeviceInfo& device : inputDevices) { |
| 425 | std::optional<AndroidPalmFilterDeviceInfo> info = createPalmFilterDeviceInfo(device); |
| 426 | if (!info) { |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | auto [it, emplaced] = mPalmRejectors.try_emplace(device.getId(), *info); |
| 431 | if (!emplaced && *info != it->second.getPalmFilterDeviceInfo()) { |
| 432 | // Re-create the PalmRejector because the device info has changed. |
| 433 | mPalmRejectors.erase(it); |
| 434 | mPalmRejectors.emplace(device.getId(), *info); |
| 435 | } |
| 436 | devicesToKeep.insert(device.getId()); |
| 437 | } |
| 438 | // Delete all devices that we don't need to keep |
| 439 | std::erase_if(mPalmRejectors, [&devicesToKeep](const auto& item) { |
| 440 | auto const& [deviceId, _] = item; |
| 441 | return devicesToKeep.find(deviceId) == devicesToKeep.end(); |
| 442 | }); |
Siarhei Vishniakou | 814ace3 | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 443 | mPreferStylusOverTouchBlocker.notifyInputDevicesChanged(inputDevices); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void UnwantedInteractionBlocker::dump(std::string& dump) { |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 447 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 448 | dump += "UnwantedInteractionBlocker:\n"; |
Siarhei Vishniakou | 814ace3 | 2022-03-04 15:12:16 -0800 | [diff] [blame] | 449 | dump += " mPreferStylusOverTouchBlocker:\n"; |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 450 | dump += addLinePrefix(mPreferStylusOverTouchBlocker.dump(), " "); |
Siarhei Vishniakou | 15a5c5a | 2022-07-06 15:42:57 -0700 | [diff] [blame] | 451 | dump += StringPrintf(" mEnablePalmRejection: %s\n", |
| 452 | std::to_string(mEnablePalmRejection).c_str()); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 453 | dump += StringPrintf(" isPalmRejectionEnabled (flag value): %s\n", |
Siarhei Vishniakou | 15a5c5a | 2022-07-06 15:42:57 -0700 | [diff] [blame] | 454 | std::to_string(isPalmRejectionEnabled()).c_str()); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 455 | dump += mPalmRejectors.empty() ? " mPalmRejectors: None\n" : " mPalmRejectors:\n"; |
| 456 | for (const auto& [deviceId, palmRejector] : mPalmRejectors) { |
| 457 | dump += StringPrintf(" deviceId = %" PRId32 ":\n", deviceId); |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 458 | dump += addLinePrefix(palmRejector.dump(), " "); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 462 | void UnwantedInteractionBlocker::monitor() { |
| 463 | std::scoped_lock lock(mLock); |
| 464 | } |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 465 | |
| 466 | UnwantedInteractionBlocker::~UnwantedInteractionBlocker() {} |
| 467 | |
| 468 | void SlotState::update(const NotifyMotionArgs& args) { |
| 469 | for (size_t i = 0; i < args.pointerCount; i++) { |
| 470 | const int32_t pointerId = args.pointerProperties[i].id; |
| 471 | const int32_t resolvedAction = resolveActionForPointer(i, args.action); |
| 472 | processPointerId(pointerId, resolvedAction); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | size_t SlotState::findUnusedSlot() const { |
| 477 | size_t unusedSlot = 0; |
| 478 | // Since the collection is ordered, we can rely on the in-order traversal |
| 479 | for (const auto& [slot, trackingId] : mPointerIdsBySlot) { |
| 480 | if (unusedSlot != slot) { |
| 481 | break; |
| 482 | } |
| 483 | unusedSlot++; |
| 484 | } |
| 485 | return unusedSlot; |
| 486 | } |
| 487 | |
| 488 | void SlotState::processPointerId(int pointerId, int32_t actionMasked) { |
| 489 | switch (MotionEvent::getActionMasked(actionMasked)) { |
| 490 | case AMOTION_EVENT_ACTION_DOWN: |
| 491 | case AMOTION_EVENT_ACTION_POINTER_DOWN: |
| 492 | case AMOTION_EVENT_ACTION_HOVER_ENTER: { |
| 493 | // New pointer going down |
| 494 | size_t newSlot = findUnusedSlot(); |
| 495 | mPointerIdsBySlot[newSlot] = pointerId; |
| 496 | mSlotsByPointerId[pointerId] = newSlot; |
| 497 | return; |
| 498 | } |
| 499 | case AMOTION_EVENT_ACTION_MOVE: |
| 500 | case AMOTION_EVENT_ACTION_HOVER_MOVE: { |
| 501 | return; |
| 502 | } |
| 503 | case AMOTION_EVENT_ACTION_CANCEL: |
| 504 | case AMOTION_EVENT_ACTION_POINTER_UP: |
| 505 | case AMOTION_EVENT_ACTION_UP: |
| 506 | case AMOTION_EVENT_ACTION_HOVER_EXIT: { |
| 507 | auto it = mSlotsByPointerId.find(pointerId); |
| 508 | LOG_ALWAYS_FATAL_IF(it == mSlotsByPointerId.end()); |
| 509 | size_t slot = it->second; |
| 510 | // Erase this pointer from both collections |
| 511 | mPointerIdsBySlot.erase(slot); |
| 512 | mSlotsByPointerId.erase(pointerId); |
| 513 | return; |
| 514 | } |
| 515 | } |
| 516 | LOG_ALWAYS_FATAL("Unhandled action : %s", MotionEvent::actionToString(actionMasked).c_str()); |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | std::optional<size_t> SlotState::getSlotForPointerId(int32_t pointerId) const { |
| 521 | auto it = mSlotsByPointerId.find(pointerId); |
| 522 | if (it == mSlotsByPointerId.end()) { |
| 523 | return std::nullopt; |
| 524 | } |
| 525 | return it->second; |
| 526 | } |
| 527 | |
| 528 | std::string SlotState::dump() const { |
| 529 | std::string out = "mSlotsByPointerId:\n"; |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 530 | out += addLinePrefix(dumpMap(mSlotsByPointerId), " ") + "\n"; |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 531 | out += "mPointerIdsBySlot:\n"; |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 532 | out += addLinePrefix(dumpMap(mPointerIdsBySlot), " ") + "\n"; |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 533 | return out; |
| 534 | } |
| 535 | |
Siarhei Vishniakou | 4aeef8c | 2022-06-13 11:19:34 -0700 | [diff] [blame] | 536 | class AndroidPalmRejectionModel : public ::ui::OneDeviceTrainNeuralStylusPalmDetectionFilterModel { |
| 537 | public: |
| 538 | AndroidPalmRejectionModel() |
| 539 | : ::ui::OneDeviceTrainNeuralStylusPalmDetectionFilterModel(/*default version*/ "", |
Siarhei Vishniakou | 127b45d | 2022-06-13 13:56:56 -0700 | [diff] [blame] | 540 | std::vector<float>()) { |
Siarhei Vishniakou | 5d67346 | 2022-07-08 10:47:57 -0700 | [diff] [blame] | 541 | config_.resample_period = ::ui::kResamplePeriod; |
Siarhei Vishniakou | 127b45d | 2022-06-13 13:56:56 -0700 | [diff] [blame] | 542 | } |
Siarhei Vishniakou | 4aeef8c | 2022-06-13 11:19:34 -0700 | [diff] [blame] | 543 | }; |
| 544 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 545 | PalmRejector::PalmRejector(const AndroidPalmFilterDeviceInfo& info, |
| 546 | std::unique_ptr<::ui::PalmDetectionFilter> filter) |
| 547 | : mSharedPalmState(std::make_unique<::ui::SharedPalmDetectionFilterState>()), |
| 548 | mDeviceInfo(info), |
| 549 | mPalmDetectionFilter(std::move(filter)) { |
| 550 | if (mPalmDetectionFilter != nullptr) { |
| 551 | // This path is used for testing. Non-testing invocations should let this constructor |
| 552 | // create a real PalmDetectionFilter |
| 553 | return; |
| 554 | } |
| 555 | std::unique_ptr<::ui::NeuralStylusPalmDetectionFilterModel> model = |
Siarhei Vishniakou | 4aeef8c | 2022-06-13 11:19:34 -0700 | [diff] [blame] | 556 | std::make_unique<AndroidPalmRejectionModel>(); |
Siarhei Vishniakou | 15a5c5a | 2022-07-06 15:42:57 -0700 | [diff] [blame] | 557 | mPalmDetectionFilter = std::make_unique<PalmFilterImplementation>(mDeviceInfo, std::move(model), |
| 558 | mSharedPalmState.get()); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | std::vector<::ui::InProgressTouchEvdev> getTouches(const NotifyMotionArgs& args, |
| 562 | const AndroidPalmFilterDeviceInfo& deviceInfo, |
| 563 | const SlotState& oldSlotState, |
| 564 | const SlotState& newSlotState) { |
| 565 | std::vector<::ui::InProgressTouchEvdev> touches; |
| 566 | |
| 567 | for (size_t i = 0; i < args.pointerCount; i++) { |
| 568 | const int32_t pointerId = args.pointerProperties[i].id; |
| 569 | touches.emplace_back(::ui::InProgressTouchEvdev()); |
| 570 | touches.back().major = args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR); |
| 571 | touches.back().minor = args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR); |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 572 | // The field 'tool_type' is not used for palm rejection |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 573 | |
| 574 | // Whether there is new information for the touch. |
| 575 | touches.back().altered = true; |
| 576 | |
| 577 | // Whether the touch was cancelled. Touch events should be ignored till a |
| 578 | // new touch is initiated. |
| 579 | touches.back().was_cancelled = false; |
| 580 | |
| 581 | // Whether the touch is going to be canceled. |
| 582 | touches.back().cancelled = false; |
| 583 | |
| 584 | // Whether the touch is delayed at first appearance. Will not be reported yet. |
| 585 | touches.back().delayed = false; |
| 586 | |
| 587 | // Whether the touch was delayed before. |
| 588 | touches.back().was_delayed = false; |
| 589 | |
| 590 | // Whether the touch is held until end or no longer held. |
| 591 | touches.back().held = false; |
| 592 | |
| 593 | // Whether this touch was held before being sent. |
| 594 | touches.back().was_held = false; |
| 595 | |
| 596 | const int32_t resolvedAction = resolveActionForPointer(i, args.action); |
| 597 | const bool isDown = resolvedAction == AMOTION_EVENT_ACTION_POINTER_DOWN || |
| 598 | resolvedAction == AMOTION_EVENT_ACTION_DOWN; |
| 599 | touches.back().was_touching = !isDown; |
| 600 | |
| 601 | const bool isUpOrCancel = resolvedAction == AMOTION_EVENT_ACTION_CANCEL || |
| 602 | resolvedAction == AMOTION_EVENT_ACTION_UP || |
| 603 | resolvedAction == AMOTION_EVENT_ACTION_POINTER_UP; |
| 604 | |
| 605 | touches.back().x = args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_X); |
| 606 | touches.back().y = args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_Y); |
| 607 | |
| 608 | std::optional<size_t> slot = newSlotState.getSlotForPointerId(pointerId); |
| 609 | if (!slot) { |
| 610 | slot = oldSlotState.getSlotForPointerId(pointerId); |
| 611 | } |
| 612 | LOG_ALWAYS_FATAL_IF(!slot, "Could not find slot for pointer %d", pointerId); |
| 613 | touches.back().slot = *slot; |
| 614 | touches.back().tracking_id = (!isUpOrCancel) ? pointerId : -1; |
| 615 | touches.back().touching = !isUpOrCancel; |
| 616 | |
| 617 | // The fields 'radius_x' and 'radius_x' are not used for palm rejection |
| 618 | touches.back().pressure = args.pointerCoords[i].getAxisValue(AMOTION_EVENT_AXIS_PRESSURE); |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 619 | touches.back().tool_code = getLinuxToolCode(args.pointerProperties[i].toolType); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 620 | // The field 'orientation' is not used for palm rejection |
| 621 | // The fields 'tilt_x' and 'tilt_y' are not used for palm rejection |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 622 | // The field 'reported_tool_type' is not used for palm rejection |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 623 | touches.back().stylus_button = false; |
| 624 | } |
| 625 | return touches; |
| 626 | } |
| 627 | |
Siarhei Vishniakou | e491fb5 | 2022-08-11 01:51:24 +0000 | [diff] [blame] | 628 | std::set<int32_t> PalmRejector::detectPalmPointers(const NotifyMotionArgs& args) { |
| 629 | std::bitset<::ui::kNumTouchEvdevSlots> slotsToHold; |
| 630 | std::bitset<::ui::kNumTouchEvdevSlots> slotsToSuppress; |
| 631 | |
| 632 | // Store the slot state before we call getTouches and update it. This way, we can find |
| 633 | // the slots that have been removed due to the incoming event. |
| 634 | SlotState oldSlotState = mSlotState; |
| 635 | mSlotState.update(args); |
| 636 | |
| 637 | std::vector<::ui::InProgressTouchEvdev> touches = |
| 638 | getTouches(args, mDeviceInfo, oldSlotState, mSlotState); |
| 639 | ::base::TimeTicks chromeTimestamp = toChromeTimestamp(args.eventTime); |
| 640 | |
| 641 | if (DEBUG_MODEL) { |
| 642 | std::stringstream touchesStream; |
| 643 | for (const ::ui::InProgressTouchEvdev& touch : touches) { |
| 644 | touchesStream << touch.tracking_id << " : " << touch << "\n"; |
| 645 | } |
| 646 | ALOGD("Filter: touches = %s", touchesStream.str().c_str()); |
| 647 | } |
| 648 | |
| 649 | mPalmDetectionFilter->Filter(touches, chromeTimestamp, &slotsToHold, &slotsToSuppress); |
| 650 | |
| 651 | ALOGD_IF(DEBUG_MODEL, "Response: slotsToHold = %s, slotsToSuppress = %s", |
| 652 | slotsToHold.to_string().c_str(), slotsToSuppress.to_string().c_str()); |
| 653 | |
| 654 | // Now that we know which slots should be suppressed, let's convert those to pointer id's. |
| 655 | std::set<int32_t> newSuppressedIds; |
| 656 | for (size_t i = 0; i < args.pointerCount; i++) { |
| 657 | const int32_t pointerId = args.pointerProperties[i].id; |
| 658 | std::optional<size_t> slot = oldSlotState.getSlotForPointerId(pointerId); |
| 659 | if (!slot) { |
| 660 | slot = mSlotState.getSlotForPointerId(pointerId); |
| 661 | LOG_ALWAYS_FATAL_IF(!slot, "Could not find slot for pointer id %" PRId32, pointerId); |
| 662 | } |
| 663 | if (slotsToSuppress.test(*slot)) { |
| 664 | newSuppressedIds.insert(pointerId); |
| 665 | } |
| 666 | } |
| 667 | return newSuppressedIds; |
| 668 | } |
| 669 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 670 | std::vector<NotifyMotionArgs> PalmRejector::processMotion(const NotifyMotionArgs& args) { |
| 671 | if (mPalmDetectionFilter == nullptr) { |
| 672 | return {args}; |
| 673 | } |
| 674 | const bool skipThisEvent = args.action == AMOTION_EVENT_ACTION_HOVER_ENTER || |
| 675 | args.action == AMOTION_EVENT_ACTION_HOVER_MOVE || |
| 676 | args.action == AMOTION_EVENT_ACTION_HOVER_EXIT || |
| 677 | args.action == AMOTION_EVENT_ACTION_BUTTON_PRESS || |
| 678 | args.action == AMOTION_EVENT_ACTION_BUTTON_RELEASE || |
| 679 | args.action == AMOTION_EVENT_ACTION_SCROLL; |
| 680 | if (skipThisEvent) { |
| 681 | // Lets not process hover events, button events, or scroll for now. |
| 682 | return {args}; |
| 683 | } |
| 684 | if (args.action == AMOTION_EVENT_ACTION_DOWN) { |
| 685 | mSuppressedPointerIds.clear(); |
| 686 | } |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 687 | |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 688 | std::set<int32_t> oldSuppressedIds; |
| 689 | std::swap(oldSuppressedIds, mSuppressedPointerIds); |
Siarhei Vishniakou | 6573583 | 2022-08-09 19:18:37 +0000 | [diff] [blame] | 690 | |
| 691 | std::optional<NotifyMotionArgs> touchOnlyArgs = removeStylusPointerIds(args); |
| 692 | if (touchOnlyArgs) { |
| 693 | mSuppressedPointerIds = detectPalmPointers(*touchOnlyArgs); |
| 694 | } else { |
| 695 | // This is a stylus-only event. |
| 696 | // We can skip this event and just keep the suppressed pointer ids the same as before. |
| 697 | mSuppressedPointerIds = oldSuppressedIds; |
| 698 | } |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 699 | |
| 700 | std::vector<NotifyMotionArgs> argsWithoutUnwantedPointers = |
| 701 | cancelSuppressedPointers(args, oldSuppressedIds, mSuppressedPointerIds); |
| 702 | for (const NotifyMotionArgs& checkArgs : argsWithoutUnwantedPointers) { |
| 703 | LOG_ALWAYS_FATAL_IF(checkArgs.action == ACTION_UNKNOWN, "%s", checkArgs.dump().c_str()); |
| 704 | } |
| 705 | |
Siarhei Vishniakou | 15a5c5a | 2022-07-06 15:42:57 -0700 | [diff] [blame] | 706 | // Only log if new pointers are getting rejected. That means mSuppressedPointerIds is not a |
| 707 | // subset of oldSuppressedIds. |
| 708 | if (!std::includes(oldSuppressedIds.begin(), oldSuppressedIds.end(), |
| 709 | mSuppressedPointerIds.begin(), mSuppressedPointerIds.end())) { |
| 710 | ALOGI("Palm detected, removing pointer ids %s after %" PRId64 "ms from %s", |
| 711 | dumpSet(mSuppressedPointerIds).c_str(), ns2ms(args.eventTime - args.downTime), |
| 712 | args.dump().c_str()); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | return argsWithoutUnwantedPointers; |
| 716 | } |
| 717 | |
Siarhei Vishniakou | 88151b8 | 2022-08-11 00:53:38 +0000 | [diff] [blame] | 718 | const AndroidPalmFilterDeviceInfo& PalmRejector::getPalmFilterDeviceInfo() const { |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 719 | return mDeviceInfo; |
| 720 | } |
| 721 | |
| 722 | std::string PalmRejector::dump() const { |
| 723 | std::string out; |
| 724 | out += "mDeviceInfo:\n"; |
Siarhei Vishniakou | 15a5c5a | 2022-07-06 15:42:57 -0700 | [diff] [blame] | 725 | std::stringstream deviceInfo; |
| 726 | deviceInfo << mDeviceInfo << ", touch_major_res=" << mDeviceInfo.touch_major_res |
| 727 | << ", touch_minor_res=" << mDeviceInfo.touch_minor_res << "\n"; |
| 728 | out += addLinePrefix(deviceInfo.str(), " "); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 729 | out += "mSlotState:\n"; |
Siarhei Vishniakou | a91d857 | 2022-05-17 05:03:42 -0700 | [diff] [blame] | 730 | out += addLinePrefix(mSlotState.dump(), " "); |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 731 | out += "mSuppressedPointerIds: "; |
| 732 | out += dumpSet(mSuppressedPointerIds) + "\n"; |
Siarhei Vishniakou | 15a5c5a | 2022-07-06 15:42:57 -0700 | [diff] [blame] | 733 | std::stringstream state; |
| 734 | state << *mSharedPalmState; |
| 735 | out += "mSharedPalmState: " + state.str() + "\n"; |
| 736 | std::stringstream filter; |
| 737 | filter << static_cast<const PalmFilterImplementation&>(*mPalmDetectionFilter); |
| 738 | out += "mPalmDetectionFilter:\n"; |
| 739 | out += addLinePrefix(filter.str(), " ") + "\n"; |
Siarhei Vishniakou | ba0a875 | 2021-09-14 14:43:25 -0700 | [diff] [blame] | 740 | return out; |
| 741 | } |
| 742 | |
| 743 | } // namespace android |