blob: 21695c354d0f6b471d74ae8a17f2133dec7d3341 [file] [log] [blame]
Siarhei Vishniakou473174e2017-12-27 16:44:42 -08001/*
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 Vishniakou34d6fef2022-02-01 19:03:45 -080020#include "InputCommonConverter.h"
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080021
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080022#include <android-base/stringprintf.h>
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -080023#include <android/binder_manager.h>
24#include <android/binder_process.h>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080025#include <inttypes.h>
26#include <log/log.h>
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -080027#include <algorithm>
28#include <cmath>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080029#if defined(__linux__)
30 #include <pthread.h>
31#endif
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080032#include <unordered_set>
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080033
Siarhei Vishniakoua028c442019-02-04 14:33:23 -080034#define INDENT1 " "
35#define INDENT2 " "
36#define INDENT3 " "
37#define INDENT4 " "
38#define INDENT5 " "
39
40using android::base::StringPrintf;
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -080041using namespace std::chrono_literals;
42using namespace ::aidl::android::hardware::input;
43using aidl::android::hardware::input::processor::IInputProcessor;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080044
45namespace android {
46
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080047//Max number of elements to store in mEvents.
48static constexpr size_t MAX_EVENTS = 5;
49
50template<class K, class V>
51static V getValueForKey(const std::unordered_map<K, V>& map, K key, V defaultValue) {
52 auto it = map.find(key);
53 if (it == map.end()) {
54 return defaultValue;
55 }
56 return it->second;
57}
58
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -080059static MotionClassification getMotionClassification(common::Classification classification) {
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080060 static_assert(MotionClassification::NONE ==
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -080061 static_cast<MotionClassification>(common::Classification::NONE));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080062 static_assert(MotionClassification::AMBIGUOUS_GESTURE ==
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -080063 static_cast<MotionClassification>(common::Classification::AMBIGUOUS_GESTURE));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080064 static_assert(MotionClassification::DEEP_PRESS ==
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -080065 static_cast<MotionClassification>(common::Classification::DEEP_PRESS));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080066 return static_cast<MotionClassification>(classification);
67}
68
69static bool isTouchEvent(const NotifyMotionArgs& args) {
Siarhei Vishniakoud9489572021-11-12 20:08:38 -080070 return isFromSource(args.source, AINPUT_SOURCE_TOUCHPAD) ||
71 isFromSource(args.source, AINPUT_SOURCE_TOUCHSCREEN);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -080072}
73
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -080074static void setCurrentThreadName(const char* name) {
75#if defined(__linux__)
76 // Set the thread name for debugging
77 pthread_setname_np(pthread_self(), name);
78#else
79 (void*)(name); // prevent unused variable warning
80#endif
81}
82
83static std::shared_ptr<IInputProcessor> getService() {
84 const std::string aidl_instance_name = std::string(IInputProcessor::descriptor) + "/default";
85
86 if (!AServiceManager_isDeclared(aidl_instance_name.c_str())) {
87 ALOGI("HAL %s is not declared", aidl_instance_name.c_str());
88 return nullptr;
89 }
90
91 ndk::SpAIBinder binder(AServiceManager_waitForService(aidl_instance_name.c_str()));
92 return IInputProcessor::fromBinder(binder);
93}
94
95// Temporarily releases a held mutex for the lifetime of the instance.
96// Named to match std::scoped_lock
97class scoped_unlock {
98public:
99 explicit scoped_unlock(std::mutex& mutex) : mMutex(mutex) { mMutex.unlock(); }
100 ~scoped_unlock() { mMutex.lock(); }
101
102private:
103 std::mutex& mMutex;
104};
105
106// --- ScopedDeathRecipient ---
107ScopedDeathRecipient::ScopedDeathRecipient(AIBinder_DeathRecipient_onBinderDied onBinderDied,
108 void* cookie)
109 : mCookie(cookie) {
110 mRecipient = AIBinder_DeathRecipient_new(onBinderDied);
111}
112
113void ScopedDeathRecipient::linkToDeath(AIBinder* binder) {
114 binder_status_t linked = AIBinder_linkToDeath(binder, mRecipient, mCookie);
115 if (linked != STATUS_OK) {
116 ALOGE("Could not link death recipient to the HAL death");
117 }
118}
119
120ScopedDeathRecipient::~ScopedDeathRecipient() {
121 AIBinder_DeathRecipient_delete(mRecipient);
122}
123
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800124// --- ClassifierEvent ---
125
126ClassifierEvent::ClassifierEvent(std::unique_ptr<NotifyMotionArgs> args) :
127 type(ClassifierEventType::MOTION), args(std::move(args)) { };
128ClassifierEvent::ClassifierEvent(std::unique_ptr<NotifyDeviceResetArgs> args) :
129 type(ClassifierEventType::DEVICE_RESET), args(std::move(args)) { };
130ClassifierEvent::ClassifierEvent(ClassifierEventType type, std::unique_ptr<NotifyArgs> args) :
131 type(type), args(std::move(args)) { };
132
133ClassifierEvent::ClassifierEvent(ClassifierEvent&& other) :
134 type(other.type), args(std::move(other.args)) { };
135
136ClassifierEvent& ClassifierEvent::operator=(ClassifierEvent&& other) {
137 type = other.type;
138 args = std::move(other.args);
139 return *this;
140}
141
142ClassifierEvent ClassifierEvent::createHalResetEvent() {
143 return ClassifierEvent(ClassifierEventType::HAL_RESET, nullptr);
144}
145
146ClassifierEvent ClassifierEvent::createExitEvent() {
147 return ClassifierEvent(ClassifierEventType::EXIT, nullptr);
148}
149
150std::optional<int32_t> ClassifierEvent::getDeviceId() const {
151 switch (type) {
152 case ClassifierEventType::MOTION: {
153 NotifyMotionArgs* motionArgs = static_cast<NotifyMotionArgs*>(args.get());
154 return motionArgs->deviceId;
155 }
156 case ClassifierEventType::DEVICE_RESET: {
157 NotifyDeviceResetArgs* deviceResetArgs =
158 static_cast<NotifyDeviceResetArgs*>(args.get());
159 return deviceResetArgs->deviceId;
160 }
161 case ClassifierEventType::HAL_RESET: {
162 return std::nullopt;
163 }
164 case ClassifierEventType::EXIT: {
165 return std::nullopt;
166 }
167 }
168}
169
170// --- MotionClassifier ---
171
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800172MotionClassifier::MotionClassifier(std::shared_ptr<IInputProcessor> service)
173 : mEvents(MAX_EVENTS), mService(std::move(service)) {
Siarhei Vishniakou6dbc3f62019-02-04 14:30:11 -0800174 // Under normal operation, we do not need to reset the HAL here. But in the case where system
175 // crashed, but HAL didn't, we may be connecting to an existing HAL process that might already
176 // have received events in the past. That means, that HAL could be in an inconsistent state
177 // once it receives events from the newly created MotionClassifier.
178 mEvents.push(ClassifierEvent::createHalResetEvent());
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700179
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800180 mHalThread = std::thread(&MotionClassifier::processEvents, this);
181#if defined(__linux__)
182 // Set the thread name for debugging
183 pthread_setname_np(mHalThread.native_handle(), "InputClassifier");
184#endif
185}
186
187std::unique_ptr<MotionClassifierInterface> MotionClassifier::create(
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800188 std::shared_ptr<IInputProcessor> service) {
189 LOG_ALWAYS_FATAL_IF(service == nullptr);
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800190 // Using 'new' to access a non-public constructor
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800191 return std::unique_ptr<MotionClassifier>(new MotionClassifier(std::move(service)));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800192}
193
194MotionClassifier::~MotionClassifier() {
195 requestExit();
196 mHalThread.join();
197}
198
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800199/**
200 * Obtain the classification from the HAL for a given MotionEvent.
201 * Should only be called from the InputClassifier thread (mHalThread).
202 * Should not be called from the thread that notifyMotion runs on.
203 *
204 * There is no way to provide a timeout for a HAL call. So if the HAL takes too long
205 * to return a classification, this would directly impact the touch latency.
206 * To remove any possibility of negatively affecting the touch latency, the HAL
207 * is called from a dedicated thread.
208 */
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800209void MotionClassifier::processEvents() {
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800210 while (true) {
211 ClassifierEvent event = mEvents.pop();
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800212 bool halResponseOk = true;
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800213 switch (event.type) {
214 case ClassifierEventType::MOTION: {
215 NotifyMotionArgs* motionArgs = static_cast<NotifyMotionArgs*>(event.args.get());
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800216 common::MotionEvent motionEvent = notifyMotionArgsToHalMotionEvent(*motionArgs);
217 common::Classification classification;
218 ndk::ScopedAStatus response = mService->classify(motionEvent, &classification);
219 if (response.isOk()) {
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800220 updateClassification(motionArgs->deviceId, motionArgs->eventTime,
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800221 getMotionClassification(classification));
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800222 }
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800223 break;
224 }
225 case ClassifierEventType::DEVICE_RESET: {
226 const int32_t deviceId = *(event.getDeviceId());
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800227 halResponseOk = mService->resetDevice(deviceId).isOk();
Siarhei Vishniakoue3021d72020-02-28 15:25:41 -0800228 clearDeviceState(deviceId);
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800229 break;
230 }
231 case ClassifierEventType::HAL_RESET: {
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800232 halResponseOk = mService->reset().isOk();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800233 clearClassifications();
234 break;
235 }
236 case ClassifierEventType::EXIT: {
237 clearClassifications();
238 return;
239 }
240 }
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800241 if (!halResponseOk) {
242 ALOGE("Error communicating with InputClassifier HAL. "
243 "Exiting MotionClassifier HAL thread");
244 clearClassifications();
245 return;
246 }
247 }
248}
249
250void MotionClassifier::enqueueEvent(ClassifierEvent&& event) {
251 bool eventAdded = mEvents.push(std::move(event));
252 if (!eventAdded) {
253 // If the queue is full, suspect the HAL is slow in processing the events.
Siarhei Vishniakou9b5a8212019-07-01 14:25:40 -0700254 ALOGE("Could not add the event to the queue. Resetting");
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800255 reset();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800256 }
257}
258
259void MotionClassifier::requestExit() {
260 reset();
261 mEvents.push(ClassifierEvent::createExitEvent());
262}
263
264void MotionClassifier::updateClassification(int32_t deviceId, nsecs_t eventTime,
265 MotionClassification classification) {
266 std::scoped_lock lock(mLock);
267 const nsecs_t lastDownTime = getValueForKey(mLastDownTimes, deviceId, static_cast<nsecs_t>(0));
268 if (eventTime < lastDownTime) {
269 // HAL just finished processing an event that belonged to an earlier gesture,
270 // but new gesture is already in progress. Drop this classification.
271 ALOGW("Received late classification. Late by at least %" PRId64 " ms.",
272 nanoseconds_to_milliseconds(lastDownTime - eventTime));
273 return;
274 }
275 mClassifications[deviceId] = classification;
276}
277
278void MotionClassifier::setClassification(int32_t deviceId, MotionClassification classification) {
279 std::scoped_lock lock(mLock);
280 mClassifications[deviceId] = classification;
281}
282
283void MotionClassifier::clearClassifications() {
284 std::scoped_lock lock(mLock);
285 mClassifications.clear();
286}
287
288MotionClassification MotionClassifier::getClassification(int32_t deviceId) {
289 std::scoped_lock lock(mLock);
290 return getValueForKey(mClassifications, deviceId, MotionClassification::NONE);
291}
292
293void MotionClassifier::updateLastDownTime(int32_t deviceId, nsecs_t downTime) {
294 std::scoped_lock lock(mLock);
295 mLastDownTimes[deviceId] = downTime;
296 mClassifications[deviceId] = MotionClassification::NONE;
297}
298
Siarhei Vishniakoue3021d72020-02-28 15:25:41 -0800299void MotionClassifier::clearDeviceState(int32_t deviceId) {
300 std::scoped_lock lock(mLock);
301 mClassifications.erase(deviceId);
302 mLastDownTimes.erase(deviceId);
303}
304
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800305MotionClassification MotionClassifier::classify(const NotifyMotionArgs& args) {
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800306 if ((args.action & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_DOWN) {
307 updateLastDownTime(args.deviceId, args.downTime);
308 }
309
310 ClassifierEvent event(std::make_unique<NotifyMotionArgs>(args));
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800311 enqueueEvent(std::move(event));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800312 return getClassification(args.deviceId);
313}
314
315void MotionClassifier::reset() {
316 mEvents.clear();
317 mEvents.push(ClassifierEvent::createHalResetEvent());
318}
319
320/**
321 * Per-device reset. Clear the outstanding events that are going to be sent to HAL.
322 * Request InputClassifier thread to call resetDevice for this particular device.
323 */
324void MotionClassifier::reset(const NotifyDeviceResetArgs& args) {
325 int32_t deviceId = args.deviceId;
326 // Clear the pending events right away, to avoid unnecessary work done by the HAL.
327 mEvents.erase([deviceId](const ClassifierEvent& event) {
328 std::optional<int32_t> eventDeviceId = event.getDeviceId();
329 return eventDeviceId && (*eventDeviceId == deviceId);
330 });
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800331 enqueueEvent(std::make_unique<NotifyDeviceResetArgs>(args));
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800332}
333
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800334void MotionClassifier::dump(std::string& dump) {
335 std::scoped_lock lock(mLock);
Prabir Pradhand7740d62022-07-01 17:54:18 +0000336 dump += StringPrintf(INDENT2 "mService connected: %s\n", mService ? "true" : "false");
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800337 dump += StringPrintf(INDENT2 "mEvents: %zu element(s) (max=%zu)\n",
338 mEvents.size(), MAX_EVENTS);
339 dump += INDENT2 "mClassifications, mLastDownTimes:\n";
340 dump += INDENT3 "Device Id\tClassification\tLast down time";
341 // Combine mClassifications and mLastDownTimes into a single table.
342 // Create a superset of device ids.
343 std::unordered_set<int32_t> deviceIds;
344 std::for_each(mClassifications.begin(), mClassifications.end(),
345 [&deviceIds](auto pair){ deviceIds.insert(pair.first); });
346 std::for_each(mLastDownTimes.begin(), mLastDownTimes.end(),
347 [&deviceIds](auto pair){ deviceIds.insert(pair.first); });
348 for(int32_t deviceId : deviceIds) {
349 const MotionClassification classification =
350 getValueForKey(mClassifications, deviceId, MotionClassification::NONE);
351 const nsecs_t downTime = getValueForKey(mLastDownTimes, deviceId, static_cast<nsecs_t>(0));
352 dump += StringPrintf("\n" INDENT4 "%" PRId32 "\t%s\t%" PRId64,
353 deviceId, motionClassificationToString(classification), downTime);
354 }
355}
356
Prabir Pradhand7740d62022-07-01 17:54:18 +0000357void MotionClassifier::monitor() {
358 std::scoped_lock lock(mLock);
359 if (mService) {
360 // Ping the HAL service to ensure it is alive and not blocked.
361 const binder_status_t status = AIBinder_ping(mService->asBinder().get());
362 if (status != STATUS_OK) {
363 ALOGW("IInputProcessor HAL is not responding; binder ping result: %s",
364 AStatus_getDescription(AStatus_fromStatus(status)));
365 }
366 }
367}
368
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800369// --- InputClassifier ---
370
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700371InputClassifier::InputClassifier(InputListenerInterface& listener) : mQueuedListener(listener) {}
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800372
373void InputClassifier::onBinderDied(void* cookie) {
374 InputClassifier* classifier = static_cast<InputClassifier*>(cookie);
375 if (classifier == nullptr) {
376 LOG_ALWAYS_FATAL("Cookie is not valid");
377 return;
378 }
379 classifier->setMotionClassifierEnabled(false);
380}
Siarhei Vishniakouc9ac19e2020-03-19 11:55:01 -0700381
382void InputClassifier::setMotionClassifierEnabled(bool enabled) {
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800383 std::scoped_lock lock(mLock);
Siarhei Vishniakouc9ac19e2020-03-19 11:55:01 -0700384 if (enabled) {
385 ALOGI("Enabling motion classifier");
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800386 if (mInitializeMotionClassifier.valid()) {
387 scoped_unlock unlock(mLock);
388 std::future_status status = mInitializeMotionClassifier.wait_for(5s);
389 if (status != std::future_status::ready) {
390 /**
391 * We don't have a better option here than to crash. We can't stop the thread,
392 * and we can't continue because 'mInitializeMotionClassifier' will block in its
393 * destructor.
394 */
395 LOG_ALWAYS_FATAL("The thread to load IInputClassifier is stuck!");
396 }
Siarhei Vishniakouc9ac19e2020-03-19 11:55:01 -0700397 }
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800398 mInitializeMotionClassifier = std::async(std::launch::async, [this] {
399 setCurrentThreadName("Create MotionClassifier");
400 std::shared_ptr<IInputProcessor> service = getService();
401 if (service == nullptr) {
402 // Keep the MotionClassifier null, no service was found
403 return;
404 }
405 { // acquire lock
406 std::scoped_lock threadLock(mLock);
407 mHalDeathRecipient =
408 std::make_unique<ScopedDeathRecipient>(onBinderDied, this /*cookie*/);
409 mHalDeathRecipient->linkToDeath(service->asBinder().get());
410 setMotionClassifierLocked(MotionClassifier::create(std::move(service)));
411 } // release lock
412 });
Siarhei Vishniakouc9ac19e2020-03-19 11:55:01 -0700413 } else {
414 ALOGI("Disabling motion classifier");
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800415 setMotionClassifierLocked(nullptr);
Siarhei Vishniakouc9ac19e2020-03-19 11:55:01 -0700416 }
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800417}
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800418
419void InputClassifier::notifyConfigurationChanged(const NotifyConfigurationChangedArgs* args) {
420 // pass through
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700421 mQueuedListener.notifyConfigurationChanged(args);
422 mQueuedListener.flush();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800423}
424
425void InputClassifier::notifyKey(const NotifyKeyArgs* args) {
426 // pass through
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700427 mQueuedListener.notifyKey(args);
428 mQueuedListener.flush();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800429}
430
431void InputClassifier::notifyMotion(const NotifyMotionArgs* args) {
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700432 { // acquire lock
433 std::scoped_lock lock(mLock);
434 // MotionClassifier is only used for touch events, for now
435 const bool sendToMotionClassifier = mMotionClassifier && isTouchEvent(*args);
436 if (!sendToMotionClassifier) {
437 mQueuedListener.notifyMotion(args);
438 } else {
439 NotifyMotionArgs newArgs(*args);
440 newArgs.classification = mMotionClassifier->classify(newArgs);
441 mQueuedListener.notifyMotion(&newArgs);
442 }
443 } // release lock
444 mQueuedListener.flush();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800445}
446
Chris Yef59a2f42020-10-16 12:55:26 -0700447void InputClassifier::notifySensor(const NotifySensorArgs* args) {
448 // pass through
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700449 mQueuedListener.notifySensor(args);
450 mQueuedListener.flush();
Chris Yef59a2f42020-10-16 12:55:26 -0700451}
452
Chris Yefb552902021-02-03 17:18:37 -0800453void InputClassifier::notifyVibratorState(const NotifyVibratorStateArgs* args) {
454 // pass through
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700455 mQueuedListener.notifyVibratorState(args);
456 mQueuedListener.flush();
Chris Yefb552902021-02-03 17:18:37 -0800457}
458
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800459void InputClassifier::notifySwitch(const NotifySwitchArgs* args) {
460 // pass through
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700461 mQueuedListener.notifySwitch(args);
462 mQueuedListener.flush();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800463}
464
465void InputClassifier::notifyDeviceReset(const NotifyDeviceResetArgs* args) {
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700466 { // acquire lock
467 std::scoped_lock lock(mLock);
468 if (mMotionClassifier) {
469 mMotionClassifier->reset(*args);
470 }
471 } // release lock
472
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800473 // continue to next stage
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700474 mQueuedListener.notifyDeviceReset(args);
475 mQueuedListener.flush();
Siarhei Vishniakou473174e2017-12-27 16:44:42 -0800476}
477
Prabir Pradhan7e186182020-11-10 13:56:45 -0800478void InputClassifier::notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs* args) {
479 // pass through
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700480 mQueuedListener.notifyPointerCaptureChanged(args);
481 mQueuedListener.flush();
Prabir Pradhan7e186182020-11-10 13:56:45 -0800482}
483
Siarhei Vishniakou34d6fef2022-02-01 19:03:45 -0800484void InputClassifier::setMotionClassifierLocked(
485 std::unique_ptr<MotionClassifierInterface> motionClassifier) REQUIRES(mLock) {
486 if (motionClassifier == nullptr) {
487 // Destroy the ScopedDeathRecipient object, which will cause it to unlinkToDeath.
488 // We can't call 'unlink' here because we don't have the binder handle.
489 mHalDeathRecipient = nullptr;
490 }
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800491 mMotionClassifier = std::move(motionClassifier);
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800492}
493
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800494void InputClassifier::dump(std::string& dump) {
Siarhei Vishniakou15b66d12019-02-04 14:27:29 -0800495 std::scoped_lock lock(mLock);
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800496 dump += "Input Classifier State:\n";
Siarhei Vishniakoua028c442019-02-04 14:33:23 -0800497 dump += INDENT1 "Motion Classifier:\n";
498 if (mMotionClassifier) {
499 mMotionClassifier->dump(dump);
500 } else {
501 dump += INDENT2 "<nullptr>";
502 }
503 dump += "\n";
504}
505
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700506void InputClassifier::monitor() {
507 std::scoped_lock lock(mLock);
Prabir Pradhand7740d62022-07-01 17:54:18 +0000508 if (mMotionClassifier) mMotionClassifier->monitor();
Siarhei Vishniakou9f330c52022-05-17 05:03:42 -0700509}
510
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800511InputClassifier::~InputClassifier() {
Siarhei Vishniakou16523972020-03-04 17:48:39 -0800512}
513
Siarhei Vishniakou4bdbb6a2019-04-11 09:42:09 -0700514} // namespace android