Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 "InputClassifier" |
| 18 | |
| 19 | #include "InputClassifier.h" |
Siarhei Vishniakou | a47a4d4 | 2019-05-06 17:14:11 -0700 | [diff] [blame] | 20 | #include "InputClassifierConverter.h" |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 21 | |
| 22 | #include <algorithm> |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 23 | #include <android-base/stringprintf.h> |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 24 | #include <cmath> |
| 25 | #include <inttypes.h> |
| 26 | #include <log/log.h> |
| 27 | #if defined(__linux__) |
| 28 | #include <pthread.h> |
| 29 | #endif |
| 30 | #include <server_configurable_flags/get_flags.h> |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 31 | #include <unordered_set> |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 32 | |
| 33 | #include <android/hardware/input/classifier/1.0/IInputClassifier.h> |
| 34 | |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 35 | #define INDENT1 " " |
| 36 | #define INDENT2 " " |
| 37 | #define INDENT3 " " |
| 38 | #define INDENT4 " " |
| 39 | #define INDENT5 " " |
| 40 | |
| 41 | using android::base::StringPrintf; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 42 | using android::hardware::hidl_bitfield; |
| 43 | using android::hardware::hidl_vec; |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 44 | using android::hardware::Return; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 45 | using namespace android::hardware::input; |
| 46 | |
| 47 | namespace android { |
| 48 | |
| 49 | static constexpr bool DEBUG = false; |
| 50 | |
| 51 | // Category (=namespace) name for the input settings that are applied at boot time |
| 52 | static const char* INPUT_NATIVE_BOOT = "input_native_boot"; |
| 53 | // Feature flag name for the deep press feature |
| 54 | static const char* DEEP_PRESS_ENABLED = "deep_press_enabled"; |
| 55 | |
| 56 | //Max number of elements to store in mEvents. |
| 57 | static constexpr size_t MAX_EVENTS = 5; |
| 58 | |
| 59 | template<class K, class V> |
| 60 | static V getValueForKey(const std::unordered_map<K, V>& map, K key, V defaultValue) { |
| 61 | auto it = map.find(key); |
| 62 | if (it == map.end()) { |
| 63 | return defaultValue; |
| 64 | } |
| 65 | return it->second; |
| 66 | } |
| 67 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 68 | static MotionClassification getMotionClassification(common::V1_0::Classification classification) { |
| 69 | static_assert(MotionClassification::NONE == |
| 70 | static_cast<MotionClassification>(common::V1_0::Classification::NONE)); |
| 71 | static_assert(MotionClassification::AMBIGUOUS_GESTURE == |
| 72 | static_cast<MotionClassification>(common::V1_0::Classification::AMBIGUOUS_GESTURE)); |
| 73 | static_assert(MotionClassification::DEEP_PRESS == |
| 74 | static_cast<MotionClassification>(common::V1_0::Classification::DEEP_PRESS)); |
| 75 | return static_cast<MotionClassification>(classification); |
| 76 | } |
| 77 | |
| 78 | static bool isTouchEvent(const NotifyMotionArgs& args) { |
| 79 | return args.source == AINPUT_SOURCE_TOUCHPAD || args.source == AINPUT_SOURCE_TOUCHSCREEN; |
| 80 | } |
| 81 | |
| 82 | // Check if the "deep touch" feature is on. |
| 83 | static bool deepPressEnabled() { |
| 84 | std::string flag_value = server_configurable_flags::GetServerConfigurableFlag( |
Philip Quinn | 64f1c16 | 2019-12-09 12:08:44 -0800 | [diff] [blame] | 85 | INPUT_NATIVE_BOOT, DEEP_PRESS_ENABLED, "true"); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 86 | std::transform(flag_value.begin(), flag_value.end(), flag_value.begin(), ::tolower); |
| 87 | if (flag_value == "1" || flag_value == "true") { |
| 88 | ALOGI("Deep press feature enabled."); |
| 89 | return true; |
| 90 | } |
| 91 | ALOGI("Deep press feature is not enabled."); |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | // --- ClassifierEvent --- |
| 97 | |
| 98 | ClassifierEvent::ClassifierEvent(std::unique_ptr<NotifyMotionArgs> args) : |
| 99 | type(ClassifierEventType::MOTION), args(std::move(args)) { }; |
| 100 | ClassifierEvent::ClassifierEvent(std::unique_ptr<NotifyDeviceResetArgs> args) : |
| 101 | type(ClassifierEventType::DEVICE_RESET), args(std::move(args)) { }; |
| 102 | ClassifierEvent::ClassifierEvent(ClassifierEventType type, std::unique_ptr<NotifyArgs> args) : |
| 103 | type(type), args(std::move(args)) { }; |
| 104 | |
| 105 | ClassifierEvent::ClassifierEvent(ClassifierEvent&& other) : |
| 106 | type(other.type), args(std::move(other.args)) { }; |
| 107 | |
| 108 | ClassifierEvent& ClassifierEvent::operator=(ClassifierEvent&& other) { |
| 109 | type = other.type; |
| 110 | args = std::move(other.args); |
| 111 | return *this; |
| 112 | } |
| 113 | |
| 114 | ClassifierEvent ClassifierEvent::createHalResetEvent() { |
| 115 | return ClassifierEvent(ClassifierEventType::HAL_RESET, nullptr); |
| 116 | } |
| 117 | |
| 118 | ClassifierEvent ClassifierEvent::createExitEvent() { |
| 119 | return ClassifierEvent(ClassifierEventType::EXIT, nullptr); |
| 120 | } |
| 121 | |
| 122 | std::optional<int32_t> ClassifierEvent::getDeviceId() const { |
| 123 | switch (type) { |
| 124 | case ClassifierEventType::MOTION: { |
| 125 | NotifyMotionArgs* motionArgs = static_cast<NotifyMotionArgs*>(args.get()); |
| 126 | return motionArgs->deviceId; |
| 127 | } |
| 128 | case ClassifierEventType::DEVICE_RESET: { |
| 129 | NotifyDeviceResetArgs* deviceResetArgs = |
| 130 | static_cast<NotifyDeviceResetArgs*>(args.get()); |
| 131 | return deviceResetArgs->deviceId; |
| 132 | } |
| 133 | case ClassifierEventType::HAL_RESET: { |
| 134 | return std::nullopt; |
| 135 | } |
| 136 | case ClassifierEventType::EXIT: { |
| 137 | return std::nullopt; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // --- MotionClassifier --- |
| 143 | |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 144 | MotionClassifier::MotionClassifier(sp<android::hardware::hidl_death_recipient> deathRecipient) : |
| 145 | mDeathRecipient(deathRecipient), mEvents(MAX_EVENTS) { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 146 | mHalThread = std::thread(&MotionClassifier::callInputClassifierHal, this); |
| 147 | #if defined(__linux__) |
| 148 | // Set the thread name for debugging |
| 149 | pthread_setname_np(mHalThread.native_handle(), "InputClassifier"); |
| 150 | #endif |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /** |
| 154 | * This function may block for some time to initialize the HAL, so it should only be called |
| 155 | * from the "InputClassifier HAL" thread. |
| 156 | */ |
| 157 | bool MotionClassifier::init() { |
| 158 | ensureHalThread(__func__); |
| 159 | sp<android::hardware::input::classifier::V1_0::IInputClassifier> service = |
| 160 | classifier::V1_0::IInputClassifier::getService(); |
| 161 | if (!service) { |
| 162 | // Not really an error, maybe the device does not have this HAL, |
| 163 | // but somehow the feature flag is flipped |
| 164 | ALOGI("Could not obtain InputClassifier HAL"); |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | sp<android::hardware::hidl_death_recipient> recipient = mDeathRecipient.promote(); |
| 169 | if (recipient != nullptr) { |
| 170 | const bool linked = service->linkToDeath(recipient, 0 /* cookie */).withDefault(false); |
| 171 | if (!linked) { |
| 172 | ALOGE("Could not link MotionClassifier to the HAL death"); |
| 173 | return false; |
| 174 | } |
| 175 | } |
| 176 | |
Siarhei Vishniakou | 6dbc3f6 | 2019-02-04 14:30:11 -0800 | [diff] [blame] | 177 | // Under normal operation, we do not need to reset the HAL here. But in the case where system |
| 178 | // crashed, but HAL didn't, we may be connecting to an existing HAL process that might already |
| 179 | // have received events in the past. That means, that HAL could be in an inconsistent state |
| 180 | // once it receives events from the newly created MotionClassifier. |
| 181 | mEvents.push(ClassifierEvent::createHalResetEvent()); |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 182 | |
| 183 | { |
| 184 | std::scoped_lock lock(mLock); |
| 185 | if (mService) { |
| 186 | ALOGE("MotionClassifier::%s should only be called once", __func__); |
| 187 | } |
| 188 | mService = service; |
| 189 | } |
| 190 | return true; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | MotionClassifier::~MotionClassifier() { |
| 194 | requestExit(); |
| 195 | mHalThread.join(); |
| 196 | } |
| 197 | |
| 198 | void MotionClassifier::ensureHalThread(const char* function) { |
| 199 | if (DEBUG) { |
| 200 | if (std::this_thread::get_id() != mHalThread.get_id()) { |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 201 | LOG_FATAL("Function %s should only be called from InputClassifier thread", function); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Obtain the classification from the HAL for a given MotionEvent. |
| 208 | * Should only be called from the InputClassifier thread (mHalThread). |
| 209 | * Should not be called from the thread that notifyMotion runs on. |
| 210 | * |
| 211 | * There is no way to provide a timeout for a HAL call. So if the HAL takes too long |
| 212 | * to return a classification, this would directly impact the touch latency. |
| 213 | * To remove any possibility of negatively affecting the touch latency, the HAL |
| 214 | * is called from a dedicated thread. |
| 215 | */ |
| 216 | void MotionClassifier::callInputClassifierHal() { |
| 217 | ensureHalThread(__func__); |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 218 | const bool initialized = init(); |
| 219 | if (!initialized) { |
| 220 | // MotionClassifier no longer useful. |
| 221 | // Deliver death notification from a separate thread |
| 222 | // because ~MotionClassifier may be invoked, which calls mHalThread.join() |
| 223 | std::thread([deathRecipient = mDeathRecipient](){ |
| 224 | sp<android::hardware::hidl_death_recipient> recipient = deathRecipient.promote(); |
| 225 | if (recipient != nullptr) { |
| 226 | recipient->serviceDied(0 /*cookie*/, nullptr); |
| 227 | } |
| 228 | }).detach(); |
| 229 | return; |
| 230 | } |
| 231 | // From this point on, mService is guaranteed to be non-null. |
| 232 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 233 | while (true) { |
| 234 | ClassifierEvent event = mEvents.pop(); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 235 | bool halResponseOk = true; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 236 | switch (event.type) { |
| 237 | case ClassifierEventType::MOTION: { |
| 238 | NotifyMotionArgs* motionArgs = static_cast<NotifyMotionArgs*>(event.args.get()); |
Siarhei Vishniakou | a47a4d4 | 2019-05-06 17:14:11 -0700 | [diff] [blame] | 239 | common::V1_0::MotionEvent motionEvent = |
| 240 | notifyMotionArgsToHalMotionEvent(*motionArgs); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 241 | Return<common::V1_0::Classification> response = mService->classify(motionEvent); |
| 242 | halResponseOk = response.isOk(); |
| 243 | if (halResponseOk) { |
| 244 | common::V1_0::Classification halClassification = response; |
| 245 | updateClassification(motionArgs->deviceId, motionArgs->eventTime, |
| 246 | getMotionClassification(halClassification)); |
| 247 | } |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 248 | break; |
| 249 | } |
| 250 | case ClassifierEventType::DEVICE_RESET: { |
| 251 | const int32_t deviceId = *(event.getDeviceId()); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 252 | halResponseOk = mService->resetDevice(deviceId).isOk(); |
Siarhei Vishniakou | e3021d7 | 2020-02-28 15:25:41 -0800 | [diff] [blame^] | 253 | clearDeviceState(deviceId); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 254 | break; |
| 255 | } |
| 256 | case ClassifierEventType::HAL_RESET: { |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 257 | halResponseOk = mService->reset().isOk(); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 258 | clearClassifications(); |
| 259 | break; |
| 260 | } |
| 261 | case ClassifierEventType::EXIT: { |
| 262 | clearClassifications(); |
| 263 | return; |
| 264 | } |
| 265 | } |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 266 | if (!halResponseOk) { |
| 267 | ALOGE("Error communicating with InputClassifier HAL. " |
| 268 | "Exiting MotionClassifier HAL thread"); |
| 269 | clearClassifications(); |
| 270 | return; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void MotionClassifier::enqueueEvent(ClassifierEvent&& event) { |
| 276 | bool eventAdded = mEvents.push(std::move(event)); |
| 277 | if (!eventAdded) { |
| 278 | // If the queue is full, suspect the HAL is slow in processing the events. |
Siarhei Vishniakou | 9b5a821 | 2019-07-01 14:25:40 -0700 | [diff] [blame] | 279 | ALOGE("Could not add the event to the queue. Resetting"); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 280 | reset(); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | void MotionClassifier::requestExit() { |
| 285 | reset(); |
| 286 | mEvents.push(ClassifierEvent::createExitEvent()); |
| 287 | } |
| 288 | |
| 289 | void MotionClassifier::updateClassification(int32_t deviceId, nsecs_t eventTime, |
| 290 | MotionClassification classification) { |
| 291 | std::scoped_lock lock(mLock); |
| 292 | const nsecs_t lastDownTime = getValueForKey(mLastDownTimes, deviceId, static_cast<nsecs_t>(0)); |
| 293 | if (eventTime < lastDownTime) { |
| 294 | // HAL just finished processing an event that belonged to an earlier gesture, |
| 295 | // but new gesture is already in progress. Drop this classification. |
| 296 | ALOGW("Received late classification. Late by at least %" PRId64 " ms.", |
| 297 | nanoseconds_to_milliseconds(lastDownTime - eventTime)); |
| 298 | return; |
| 299 | } |
| 300 | mClassifications[deviceId] = classification; |
| 301 | } |
| 302 | |
| 303 | void MotionClassifier::setClassification(int32_t deviceId, MotionClassification classification) { |
| 304 | std::scoped_lock lock(mLock); |
| 305 | mClassifications[deviceId] = classification; |
| 306 | } |
| 307 | |
| 308 | void MotionClassifier::clearClassifications() { |
| 309 | std::scoped_lock lock(mLock); |
| 310 | mClassifications.clear(); |
| 311 | } |
| 312 | |
| 313 | MotionClassification MotionClassifier::getClassification(int32_t deviceId) { |
| 314 | std::scoped_lock lock(mLock); |
| 315 | return getValueForKey(mClassifications, deviceId, MotionClassification::NONE); |
| 316 | } |
| 317 | |
| 318 | void MotionClassifier::updateLastDownTime(int32_t deviceId, nsecs_t downTime) { |
| 319 | std::scoped_lock lock(mLock); |
| 320 | mLastDownTimes[deviceId] = downTime; |
| 321 | mClassifications[deviceId] = MotionClassification::NONE; |
| 322 | } |
| 323 | |
Siarhei Vishniakou | e3021d7 | 2020-02-28 15:25:41 -0800 | [diff] [blame^] | 324 | void MotionClassifier::clearDeviceState(int32_t deviceId) { |
| 325 | std::scoped_lock lock(mLock); |
| 326 | mClassifications.erase(deviceId); |
| 327 | mLastDownTimes.erase(deviceId); |
| 328 | } |
| 329 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 330 | MotionClassification MotionClassifier::classify(const NotifyMotionArgs& args) { |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 331 | if ((args.action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_DOWN) { |
| 332 | updateLastDownTime(args.deviceId, args.downTime); |
| 333 | } |
| 334 | |
| 335 | ClassifierEvent event(std::make_unique<NotifyMotionArgs>(args)); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 336 | enqueueEvent(std::move(event)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 337 | return getClassification(args.deviceId); |
| 338 | } |
| 339 | |
| 340 | void MotionClassifier::reset() { |
| 341 | mEvents.clear(); |
| 342 | mEvents.push(ClassifierEvent::createHalResetEvent()); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Per-device reset. Clear the outstanding events that are going to be sent to HAL. |
| 347 | * Request InputClassifier thread to call resetDevice for this particular device. |
| 348 | */ |
| 349 | void MotionClassifier::reset(const NotifyDeviceResetArgs& args) { |
| 350 | int32_t deviceId = args.deviceId; |
| 351 | // Clear the pending events right away, to avoid unnecessary work done by the HAL. |
| 352 | mEvents.erase([deviceId](const ClassifierEvent& event) { |
| 353 | std::optional<int32_t> eventDeviceId = event.getDeviceId(); |
| 354 | return eventDeviceId && (*eventDeviceId == deviceId); |
| 355 | }); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 356 | enqueueEvent(std::make_unique<NotifyDeviceResetArgs>(args)); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 359 | const char* MotionClassifier::getServiceStatus() REQUIRES(mLock) { |
| 360 | if (!mService) { |
| 361 | return "null"; |
| 362 | } |
| 363 | if (mService->ping().isOk()) { |
| 364 | return "running"; |
| 365 | } |
| 366 | return "not responding"; |
| 367 | } |
| 368 | |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 369 | void MotionClassifier::dump(std::string& dump) { |
| 370 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 371 | dump += StringPrintf(INDENT2 "mService status: %s\n", getServiceStatus()); |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 372 | dump += StringPrintf(INDENT2 "mEvents: %zu element(s) (max=%zu)\n", |
| 373 | mEvents.size(), MAX_EVENTS); |
| 374 | dump += INDENT2 "mClassifications, mLastDownTimes:\n"; |
| 375 | dump += INDENT3 "Device Id\tClassification\tLast down time"; |
| 376 | // Combine mClassifications and mLastDownTimes into a single table. |
| 377 | // Create a superset of device ids. |
| 378 | std::unordered_set<int32_t> deviceIds; |
| 379 | std::for_each(mClassifications.begin(), mClassifications.end(), |
| 380 | [&deviceIds](auto pair){ deviceIds.insert(pair.first); }); |
| 381 | std::for_each(mLastDownTimes.begin(), mLastDownTimes.end(), |
| 382 | [&deviceIds](auto pair){ deviceIds.insert(pair.first); }); |
| 383 | for(int32_t deviceId : deviceIds) { |
| 384 | const MotionClassification classification = |
| 385 | getValueForKey(mClassifications, deviceId, MotionClassification::NONE); |
| 386 | const nsecs_t downTime = getValueForKey(mLastDownTimes, deviceId, static_cast<nsecs_t>(0)); |
| 387 | dump += StringPrintf("\n" INDENT4 "%" PRId32 "\t%s\t%" PRId64, |
| 388 | deviceId, motionClassificationToString(classification), downTime); |
| 389 | } |
| 390 | } |
| 391 | |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 392 | |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 393 | // --- InputClassifier --- |
| 394 | |
| 395 | InputClassifier::InputClassifier(const sp<InputListenerInterface>& listener) : |
| 396 | mListener(listener) { |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 397 | // The rest of the initialization is done in onFirstRef, because we need to obtain |
| 398 | // an sp to 'this' in order to register for HAL death notifications |
| 399 | } |
| 400 | |
| 401 | void InputClassifier::onFirstRef() { |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 402 | if (!deepPressEnabled()) { |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 403 | // If feature is not enabled, MotionClassifier should stay null to avoid unnecessary work. |
| 404 | // When MotionClassifier is null, InputClassifier will forward all events |
| 405 | // to the next InputListener, unmodified. |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 406 | return; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 407 | } |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 408 | std::scoped_lock lock(mLock); |
| 409 | mMotionClassifier = std::make_unique<MotionClassifier>(this); |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 410 | } |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 411 | |
| 412 | void InputClassifier::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) { |
| 413 | // pass through |
| 414 | mListener->notifyConfigurationChanged(args); |
| 415 | } |
| 416 | |
| 417 | void InputClassifier::notifyKey(const NotifyKeyArgs* args) { |
| 418 | // pass through |
| 419 | mListener->notifyKey(args); |
| 420 | } |
| 421 | |
| 422 | void InputClassifier::notifyMotion(const NotifyMotionArgs* args) { |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 423 | std::scoped_lock lock(mLock); |
| 424 | // MotionClassifier is only used for touch events, for now |
| 425 | const bool sendToMotionClassifier = mMotionClassifier && isTouchEvent(*args); |
| 426 | if (!sendToMotionClassifier) { |
| 427 | mListener->notifyMotion(args); |
| 428 | return; |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 429 | } |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 430 | |
| 431 | NotifyMotionArgs newArgs(*args); |
| 432 | newArgs.classification = mMotionClassifier->classify(newArgs); |
| 433 | mListener->notifyMotion(&newArgs); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | void InputClassifier::notifySwitch(const NotifySwitchArgs* args) { |
| 437 | // pass through |
| 438 | mListener->notifySwitch(args); |
| 439 | } |
| 440 | |
| 441 | void InputClassifier::notifyDeviceReset(const NotifyDeviceResetArgs* args) { |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 442 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | 473174e | 2017-12-27 16:44:42 -0800 | [diff] [blame] | 443 | if (mMotionClassifier) { |
| 444 | mMotionClassifier->reset(*args); |
| 445 | } |
| 446 | // continue to next stage |
| 447 | mListener->notifyDeviceReset(args); |
| 448 | } |
| 449 | |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 450 | void InputClassifier::serviceDied(uint64_t /*cookie*/, |
| 451 | const wp<android::hidl::base::V1_0::IBase>& who) { |
| 452 | std::scoped_lock lock(mLock); |
| 453 | ALOGE("InputClassifier HAL has died. Setting mMotionClassifier to null"); |
| 454 | mMotionClassifier = nullptr; |
| 455 | sp<android::hidl::base::V1_0::IBase> service = who.promote(); |
| 456 | if (service) { |
| 457 | service->unlinkToDeath(this); |
| 458 | } |
| 459 | } |
| 460 | |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 461 | void InputClassifier::dump(std::string& dump) { |
Siarhei Vishniakou | 15b66d1 | 2019-02-04 14:27:29 -0800 | [diff] [blame] | 462 | std::scoped_lock lock(mLock); |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 463 | dump += "Input Classifier State:\n"; |
Siarhei Vishniakou | e3021d7 | 2020-02-28 15:25:41 -0800 | [diff] [blame^] | 464 | dump += StringPrintf(INDENT1 "Deep press: %s\n", deepPressEnabled() ? "enabled" : "disabled"); |
Siarhei Vishniakou | a028c44 | 2019-02-04 14:33:23 -0800 | [diff] [blame] | 465 | |
| 466 | dump += INDENT1 "Motion Classifier:\n"; |
| 467 | if (mMotionClassifier) { |
| 468 | mMotionClassifier->dump(dump); |
| 469 | } else { |
| 470 | dump += INDENT2 "<nullptr>"; |
| 471 | } |
| 472 | dump += "\n"; |
| 473 | } |
| 474 | |
Siarhei Vishniakou | 4bdbb6a | 2019-04-11 09:42:09 -0700 | [diff] [blame] | 475 | } // namespace android |