Skip to content

Commit 79c6309

Browse files
Vaibhav DevmurariAndroid (Google) Code Review
authored andcommitted
Merge "Pass displayId to interceptBeforeDispatching policy for key events" into main
2 parents 514de0f + 7a694ce commit 79c6309

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

services/inputflinger/dispatcher/InputDispatcher.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,14 +1896,14 @@ bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<con
18961896
entry->interceptKeyWakeupTime = 0;
18971897
}
18981898

1899+
const ui::LogicalDisplayId displayId = getTargetDisplayId(*entry);
18991900
// Give the policy a chance to intercept the key.
19001901
if (entry->interceptKeyResult == KeyEntry::InterceptKeyResult::UNKNOWN) {
19011902
if (entry->policyFlags & POLICY_FLAG_PASS_TO_USER) {
1902-
sp<IBinder> focusedWindowToken =
1903-
mFocusResolver.getFocusedWindowToken(getTargetDisplayId(*entry));
1903+
sp<IBinder> focusedWindowToken = mFocusResolver.getFocusedWindowToken(displayId);
19041904

1905-
auto command = [this, focusedWindowToken, entry]() REQUIRES(mLock) {
1906-
doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry);
1905+
auto command = [this, focusedWindowToken, displayId, entry]() REQUIRES(mLock) {
1906+
doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, displayId, *entry);
19071907
};
19081908
postCommandLocked(std::move(command));
19091909
return false; // wait for the command to run
@@ -1952,7 +1952,7 @@ bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr<con
19521952
InputTarget::Flags::FOREGROUND, getDownTime(*entry), inputTargets);
19531953

19541954
// Add monitor channels from event's or focused display.
1955-
addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry));
1955+
addGlobalMonitoringTargetsLocked(inputTargets, displayId);
19561956

19571957
if (mTracer) {
19581958
ensureEventTraced(*entry);
@@ -6632,8 +6632,10 @@ void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel,
66326632
}
66336633

66346634
void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken,
6635+
const ui::LogicalDisplayId displayId,
66356636
const KeyEntry& entry) {
6636-
const KeyEvent event = createKeyEvent(entry);
6637+
KeyEvent event = createKeyEvent(entry);
6638+
event.setDisplayId(displayId);
66376639
std::variant<nsecs_t, KeyEntry::InterceptKeyResult> interceptResult;
66386640
nsecs_t delay = 0;
66396641
{ // release lock

services/inputflinger/dispatcher/InputDispatcher.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,7 @@ class InputDispatcher : public android::InputDispatcherInterface {
898898
const std::shared_ptr<Connection>& connection, uint32_t seq,
899899
bool handled, nsecs_t consumeTime) REQUIRES(mLock);
900900
void doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken,
901+
const ui::LogicalDisplayId displayId,
901902
const KeyEntry& entry) REQUIRES(mLock);
902903
void onFocusChangedLocked(const FocusResolver::FocusChanges& changes,
903904
const std::unique_ptr<trace::EventTrackerInterface>& traceTracker,

services/inputflinger/tests/FakeInputDispatcherPolicy.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,14 @@ void FakeInputDispatcherPolicy::interceptMotionBeforeQueueing(ui::LogicalDisplay
411411
int32_t, nsecs_t, uint32_t&) {}
412412

413413
std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult>
414-
FakeInputDispatcherPolicy::interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&,
414+
FakeInputDispatcherPolicy::interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent& event,
415415
uint32_t) {
416-
if (std::holds_alternative<inputdispatcher::KeyEntry::InterceptKeyResult>(
417-
mInterceptKeyBeforeDispatchingResult)) {
416+
if (inputdispatcher::KeyEntry::InterceptKeyResult* result =
417+
std::get_if<inputdispatcher::KeyEntry::InterceptKeyResult>(
418+
&mInterceptKeyBeforeDispatchingResult)) {
419+
if (*result == inputdispatcher::KeyEntry::InterceptKeyResult::SKIP) {
420+
mKeysConsumedByPolicy.emplace(event);
421+
}
418422
return mInterceptKeyBeforeDispatchingResult;
419423
}
420424

@@ -430,6 +434,15 @@ FakeInputDispatcherPolicy::interceptKeyBeforeDispatching(const sp<IBinder>&, con
430434
return delay;
431435
}
432436

437+
void FakeInputDispatcherPolicy::assertKeyConsumedByPolicy(
438+
const ::testing::Matcher<KeyEvent>& matcher) {
439+
ASSERT_THAT(*mKeysConsumedByPolicy.popWithTimeout(100ms), matcher);
440+
}
441+
442+
void FakeInputDispatcherPolicy::assertNoKeysConsumedByPolicy() {
443+
ASSERT_TRUE(mKeysConsumedByPolicy.empty());
444+
}
445+
433446
std::optional<KeyEvent> FakeInputDispatcherPolicy::dispatchUnhandledKey(const sp<IBinder>&,
434447
const KeyEvent& event,
435448
uint32_t) {

services/inputflinger/tests/FakeInputDispatcherPolicy.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "InputDispatcherInterface.h"
2222
#include "NotifyArgs.h"
23+
#include "TestEventMatchers.h"
2324

2425
#include <condition_variable>
2526
#include <functional>
@@ -119,6 +120,8 @@ class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
119120
void setInterceptKeyBeforeDispatchingResult(
120121
std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult> result);
121122
void assertFocusedDisplayNotified(ui::LogicalDisplayId expectedDisplay);
123+
void assertKeyConsumedByPolicy(const ::testing::Matcher<KeyEvent>& matcher);
124+
void assertNoKeysConsumedByPolicy();
122125

123126
private:
124127
std::mutex mLock;
@@ -150,6 +153,7 @@ class FakeInputDispatcherPolicy : public InputDispatcherPolicyInterface {
150153

151154
std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult>
152155
mInterceptKeyBeforeDispatchingResult;
156+
BlockingQueue<KeyEvent> mKeysConsumedByPolicy;
153157

154158
BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;
155159

services/inputflinger/tests/InputDispatcher_test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5695,6 +5695,33 @@ TEST_F(InputDispatcherTest, InterceptKeyIfKeyUp) {
56955695
window->consumeKeyUp(ui::LogicalDisplayId::DEFAULT);
56965696
}
56975697

5698+
/**
5699+
* Keys are sent to policy with correct displayId
5700+
*/
5701+
TEST_F(InputDispatcherTest, InterceptKeyBeforeDispatchingPolicy_getsCorrectDisplayId) {
5702+
std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
5703+
sp<FakeWindowHandle> window =
5704+
sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
5705+
ui::LogicalDisplayId(2));
5706+
window->setFocusable(true);
5707+
5708+
mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
5709+
mDispatcher->setFocusedDisplay(ui::LogicalDisplayId(2));
5710+
setFocusedWindow(window);
5711+
5712+
window->consumeFocusEvent(true);
5713+
5714+
mFakePolicy->setInterceptKeyBeforeDispatchingResult(
5715+
inputdispatcher::KeyEntry::InterceptKeyResult::SKIP);
5716+
5717+
mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
5718+
.keyCode(AKEYCODE_A)
5719+
.displayId(ui::LogicalDisplayId::INVALID)
5720+
.build());
5721+
mFakePolicy->assertKeyConsumedByPolicy(
5722+
AllOf(WithKeyCode(AKEYCODE_A), WithDisplayId(ui::LogicalDisplayId(2))));
5723+
}
5724+
56985725
/**
56995726
* Two windows. First is a regular window. Second does not overlap with the first, and has
57005727
* WATCH_OUTSIDE_TOUCH.

0 commit comments

Comments
 (0)