Prevent display brightness flicker on power button
ag/22712141 tried to fix an issue where using system keys would
not trigger user activity, allowing the device to go into sleep
mode even while interacting with things like volume controls. The
cause of this is that system shortcuts were never sent to keyguard,
which is responsible for manging the power state. Unfortunately,
the fix caused the side effect of considering the power button user
activity, which would brighten the display if in dim mode before
bringing the display to sleep. This was quite subtle as we only
learned of this problem recently.
This CL refines the logic introduced in ag/22712141, correcting the
specific issue while restoring the original user activity logic.
Bug: 312671872
Test: Flashed on device, tested on keyguard and several apps
Flag: EXEMPT bugfix
Change-Id: I57f98ec1b138d4e71cd8c21742eacd8a2813e9d3
diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp
index 9b97629..ffdb1c4 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.cpp
+++ b/services/inputflinger/dispatcher/InputDispatcher.cpp
@@ -1894,8 +1894,6 @@
doInterceptKeyBeforeDispatchingCommand(focusedWindowToken, *entry);
};
postCommandLocked(std::move(command));
- // Poke user activity for keys not passed to user
- pokeUserActivityLocked(*entry);
return false; // wait for the command to run
} else {
entry->interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE;
@@ -1912,8 +1910,12 @@
*dropReason == DropReason::POLICY ? InputEventInjectionResult::SUCCEEDED
: InputEventInjectionResult::FAILED);
mReporter->reportDroppedKey(entry->id);
- // Poke user activity for undispatched keys
- pokeUserActivityLocked(*entry);
+ // Poke user activity for consumed keys, as it may have not been reported due to
+ // the focused window requesting user activity to be disabled
+ if (*dropReason == DropReason::POLICY &&
+ mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER) {
+ pokeUserActivityLocked(*entry);
+ }
return true;
}
@@ -3313,22 +3315,16 @@
if (keyEntry.flags & AKEY_EVENT_FLAG_CANCELED) {
return;
}
- // If the key code is unknown, we don't consider it user activity
- if (keyEntry.keyCode == AKEYCODE_UNKNOWN) {
- return;
- }
// Don't inhibit events that were intercepted or are not passed to
// the apps, like system shortcuts
if (windowDisablingUserActivityInfo != nullptr &&
- keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP &&
- keyEntry.policyFlags & POLICY_FLAG_PASS_TO_USER) {
+ keyEntry.interceptKeyResult != KeyEntry::InterceptKeyResult::SKIP) {
if (DEBUG_DISPATCH_CYCLE) {
ALOGD("Not poking user activity: disabled by window '%s'.",
windowDisablingUserActivityInfo->name.c_str());
}
return;
}
-
break;
}
default: {
diff --git a/services/inputflinger/tests/FakeInputDispatcherPolicy.cpp b/services/inputflinger/tests/FakeInputDispatcherPolicy.cpp
index 530416c..e17ee3a 100644
--- a/services/inputflinger/tests/FakeInputDispatcherPolicy.cpp
+++ b/services/inputflinger/tests/FakeInputDispatcherPolicy.cpp
@@ -215,6 +215,10 @@
mStaleEventTimeout = timeout;
}
+void FakeInputDispatcherPolicy::setConsumeKeyBeforeDispatching(bool consumeKeyBeforeDispatching) {
+ mConsumeKeyBeforeDispatching = consumeKeyBeforeDispatching;
+}
+
void FakeInputDispatcherPolicy::assertUserActivityNotPoked() {
std::unique_lock lock(mLock);
base::ScopedLockAssertion assumeLocked(mLock);
@@ -401,6 +405,9 @@
nsecs_t FakeInputDispatcherPolicy::interceptKeyBeforeDispatching(const sp<IBinder>&,
const KeyEvent&, uint32_t) {
+ if (mConsumeKeyBeforeDispatching) {
+ return -1;
+ }
nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
// Clear intercept state so we could dispatch the event in next wake.
mInterceptKeyTimeout = 0ms;
diff --git a/services/inputflinger/tests/FakeInputDispatcherPolicy.h b/services/inputflinger/tests/FakeInputDispatcherPolicy.h
index 2c86146..62ff10f 100644
--- a/services/inputflinger/tests/FakeInputDispatcherPolicy.h
+++ b/services/inputflinger/tests/FakeInputDispatcherPolicy.h
@@ -115,6 +115,7 @@
void setUnhandledKeyHandler(std::function<std::optional<KeyEvent>(const KeyEvent&)> handler);
void assertUnhandledKeyReported(int32_t keycode);
void assertUnhandledKeyNotReported();
+ void setConsumeKeyBeforeDispatching(bool consumeKeyBeforeDispatching);
private:
std::mutex mLock;
@@ -144,6 +145,8 @@
std::chrono::nanoseconds mStaleEventTimeout = 1000ms;
+ bool mConsumeKeyBeforeDispatching = false;
+
BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions;
std::condition_variable mNotifyUnhandledKey;
diff --git a/services/inputflinger/tests/InputDispatcher_test.cpp b/services/inputflinger/tests/InputDispatcher_test.cpp
index 8de28c6..0ca84c2 100644
--- a/services/inputflinger/tests/InputDispatcher_test.cpp
+++ b/services/inputflinger/tests/InputDispatcher_test.cpp
@@ -610,28 +610,6 @@
return args;
}
-static NotifyKeyArgs generateSystemShortcutArgs(
- int32_t action, ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) {
- nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
- // Define a valid key event.
- NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID,
- AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_C, KEY_C,
- AMETA_META_ON, currentTime);
-
- return args;
-}
-
-static NotifyKeyArgs generateAssistantKeyArgs(
- int32_t action, ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID) {
- nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
- // Define a valid key event.
- NotifyKeyArgs args(InputEvent::nextId(), currentTime, /*readTime=*/0, DEVICE_ID,
- AINPUT_SOURCE_KEYBOARD, displayId, 0, action, /*flags=*/0, AKEYCODE_ASSIST,
- KEY_ASSISTANT, AMETA_NONE, currentTime);
-
- return args;
-}
-
[[nodiscard]] static NotifyMotionArgs generateMotionArgs(int32_t action, int32_t source,
ui::LogicalDisplayId displayId,
const std::vector<PointF>& points) {
@@ -6628,17 +6606,18 @@
window->consumeFocusEvent(true);
- mDispatcher->notifyKey(generateKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
+ mDispatcher->notifyKey(
+ KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
// Window should receive key down event.
window->consumeKeyDown(ui::LogicalDisplayId::DEFAULT);
- // Should have poked user activity
+ // Should have not poked user activity
mDispatcher->waitForIdle();
mFakePolicy->assertUserActivityNotPoked();
}
-TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveSystemShortcut) {
+TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceivePolicyConsumedKey) {
std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
sp<FakeWindowHandle> window =
sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
@@ -6650,41 +6629,20 @@
window->consumeFocusEvent(true);
+ mFakePolicy->setConsumeKeyBeforeDispatching(true);
+
mDispatcher->notifyKey(
- generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
+ KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
mDispatcher->waitForIdle();
- // System key is not passed down
+ // Key is not passed down
window->assertNoEvents();
// Should have poked user activity
mFakePolicy->assertUserActivityPoked();
}
-TEST_F(InputDispatcherTest, FocusedWindow_DoesNotReceiveAssistantKey) {
- std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
- sp<FakeWindowHandle> window =
- sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
- ui::LogicalDisplayId::DEFAULT);
-
- window->setFocusable(true);
- mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
- setFocusedWindow(window);
-
- window->consumeFocusEvent(true);
-
- mDispatcher->notifyKey(
- generateAssistantKeyArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
- mDispatcher->waitForIdle();
-
- // System key is not passed down
- window->assertNoEvents();
-
- // Should have poked user activity
- mFakePolicy->assertUserActivityPoked();
-}
-
-TEST_F(InputDispatcherTest, FocusedWindow_SystemKeyIgnoresDisableUserActivity) {
+TEST_F(InputDispatcherTest, FocusedWindow_PolicyConsumedKeyIgnoresDisableUserActivity) {
std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
sp<FakeWindowHandle> window =
sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
@@ -6697,8 +6655,10 @@
window->consumeFocusEvent(true);
+ mFakePolicy->setConsumeKeyBeforeDispatching(true);
+
mDispatcher->notifyKey(
- generateSystemShortcutArgs(AKEY_EVENT_ACTION_DOWN, ui::LogicalDisplayId::DEFAULT));
+ KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).keyCode(AKEYCODE_A).build());
mDispatcher->waitForIdle();
// System key is not passed down
@@ -6708,6 +6668,39 @@
mFakePolicy->assertUserActivityPoked();
}
+class DisableUserActivityInputDispatcherTest : public InputDispatcherTest,
+ public ::testing::WithParamInterface<bool> {};
+
+TEST_P(DisableUserActivityInputDispatcherTest, NotPassedToUserUserActivity) {
+ std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
+ sp<FakeWindowHandle> window =
+ sp<FakeWindowHandle>::make(application, mDispatcher, "Fake Window",
+ ui::LogicalDisplayId::DEFAULT);
+
+ window->setDisableUserActivity(GetParam());
+
+ window->setFocusable(true);
+ mDispatcher->onWindowInfosChanged({{*window->getInfo()}, {}, 0, 0});
+ setFocusedWindow(window);
+
+ window->consumeFocusEvent(true);
+
+ mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
+ .keyCode(AKEYCODE_A)
+ .policyFlags(0)
+ .build());
+ mDispatcher->waitForIdle();
+
+ // Key is not passed down
+ window->assertNoEvents();
+
+ // Should not have poked user activity
+ mFakePolicy->assertUserActivityNotPoked();
+}
+
+INSTANTIATE_TEST_CASE_P(DisableUserActivity, DisableUserActivityInputDispatcherTest,
+ ::testing::Bool());
+
TEST_F(InputDispatcherTest, InjectedTouchesPokeUserActivity) {
std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>();
sp<FakeWindowHandle> window =