Skip to content

Commit 3ef7a98

Browse files
Treehugger RobotAndroid (Google) Code Review
authored andcommitted
Merge "Refactor interceptKeyBeforeDispatching to use variant" into main
2 parents edc7146 + 4942d6c commit 3ef7a98

4 files changed

Lines changed: 31 additions & 14 deletions

File tree

services/inputflinger/dispatcher/InputDispatcher.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <ctime>
4747
#include <queue>
4848
#include <sstream>
49+
#include <variant>
4950

5051
#include "../InputDeviceMetricsSource.h"
5152

@@ -6651,24 +6652,27 @@ void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel,
66516652
void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken,
66526653
const KeyEntry& entry) {
66536654
const KeyEvent event = createKeyEvent(entry);
6655+
std::variant<nsecs_t, KeyEntry::InterceptKeyResult> interceptResult;
66546656
nsecs_t delay = 0;
66556657
{ // release lock
66566658
scoped_unlock unlock(mLock);
66576659
android::base::Timer t;
6658-
delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags);
6660+
interceptResult =
6661+
mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags);
66596662
if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
66606663
ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms",
66616664
std::to_string(t.duration().count()).c_str());
66626665
}
66636666
} // acquire lock
66646667

6665-
if (delay < 0) {
6666-
entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP;
6667-
} else if (delay == 0) {
6668-
entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE;
6669-
} else {
6668+
if (std::holds_alternative<KeyEntry::InterceptKeyResult>(interceptResult)) {
6669+
entry.interceptKeyResult = std::get<KeyEntry::InterceptKeyResult>(interceptResult);
6670+
return;
6671+
}
6672+
6673+
if (std::holds_alternative<nsecs_t>(interceptResult)) {
66706674
entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER;
6671-
entry.interceptKeyWakeupTime = now() + delay;
6675+
entry.interceptKeyWakeupTime = now() + std::get<nsecs_t>(interceptResult);
66726676
}
66736677
}
66746678

services/inputflinger/dispatcher/include/InputDispatcherPolicyInterface.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020

2121
#include <android-base/properties.h>
2222
#include <binder/IBinder.h>
23+
#include <dispatcher/Entry.h>
2324
#include <gui/InputApplication.h>
2425
#include <gui/PidUid.h>
2526
#include <input/Input.h>
2627
#include <input/InputDevice.h>
2728
#include <utils/RefBase.h>
2829
#include <set>
30+
#include <variant>
2931

3032
namespace android {
3133

@@ -106,9 +108,9 @@ class InputDispatcherPolicyInterface {
106108
uint32_t& policyFlags) = 0;
107109

108110
/* Allows the policy a chance to intercept a key before dispatching. */
109-
virtual nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>& token,
110-
const KeyEvent& keyEvent,
111-
uint32_t policyFlags) = 0;
111+
virtual std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult>
112+
interceptKeyBeforeDispatching(const sp<IBinder>& token, const KeyEvent& keyEvent,
113+
uint32_t policyFlags) = 0;
112114

113115
/* Allows the policy a chance to perform default processing for an unhandled key.
114116
* Returns an alternate key event to redispatch as a fallback, if needed. */

services/inputflinger/tests/FakeInputDispatcherPolicy.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "FakeInputDispatcherPolicy.h"
1818

19+
#include <variant>
20+
1921
#include <gtest/gtest.h>
2022

2123
namespace android {
@@ -409,12 +411,18 @@ void FakeInputDispatcherPolicy::interceptKeyBeforeQueueing(const KeyEvent& input
409411
void FakeInputDispatcherPolicy::interceptMotionBeforeQueueing(ui::LogicalDisplayId, uint32_t,
410412
int32_t, nsecs_t, uint32_t&) {}
411413

412-
nsecs_t FakeInputDispatcherPolicy::interceptKeyBeforeDispatching(const sp<IBinder>&,
413-
const KeyEvent&, uint32_t) {
414+
std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult>
415+
FakeInputDispatcherPolicy::interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&,
416+
uint32_t) {
414417
if (mConsumeKeyBeforeDispatching) {
415-
return -1;
418+
return inputdispatcher::KeyEntry::InterceptKeyResult::SKIP;
416419
}
420+
417421
nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
422+
if (delay == 0) {
423+
return inputdispatcher::KeyEntry::InterceptKeyResult::CONTINUE;
424+
}
425+
418426
// Clear intercept state so we could dispatch the event in next wake.
419427
mInterceptKeyTimeout = 0ms;
420428
return delay;

services/inputflinger/tests/FakeInputDispatcherPolicy.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
#include <optional>
2929
#include <queue>
3030
#include <string>
31+
#include <variant>
3132
#include <vector>
3233

3334
#include <android-base/logging.h>
3435
#include <android-base/thread_annotations.h>
3536
#include <binder/IBinder.h>
37+
#include <dispatcher/Entry.h>
3638
#include <gui/PidUid.h>
3739
#include <gui/WindowInfo.h>
3840
#include <input/BlockingQueue.h>
@@ -189,7 +191,8 @@ class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
189191
void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override;
190192
void interceptMotionBeforeQueueing(ui::LogicalDisplayId, uint32_t, int32_t, nsecs_t,
191193
uint32_t&) override;
192-
nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override;
194+
std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult>
195+
interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override;
193196
std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event,
194197
uint32_t) override;
195198
void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,

0 commit comments

Comments
 (0)