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