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/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 =