Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4942d6ca authored by Jimmy's avatar Jimmy Committed by Jimmy Gong
Browse files

Refactor interceptKeyBeforeDispatching to use variant

Use <variant> as interceptKeyBeforeDispatching can have either
a KeyEntry::InterceptKeyResult or nsecs_t to indicate state or
amount of delay on a key interception attempt.

Bug: 384113980
Test: compile + local

Flag: EXEMPT refactor

Change-Id: I90a67a7ba2efc0ff7e03ddb0d11193902021f4be
parent 3c3d749c
Loading
Loading
Loading
Loading
+11 −7
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@
#include <ctime>
#include <queue>
#include <sstream>
#include <variant>

#include "../InputDeviceMetricsSource.h"

@@ -6608,24 +6609,27 @@ void InputDispatcher::updateLastAnrStateLocked(const std::string& windowLabel,
void InputDispatcher::doInterceptKeyBeforeDispatchingCommand(const sp<IBinder>& focusedWindowToken,
                                                             const KeyEntry& entry) {
    const KeyEvent event = createKeyEvent(entry);
    std::variant<nsecs_t, KeyEntry::InterceptKeyResult> interceptResult;
    nsecs_t delay = 0;
    { // release lock
        scoped_unlock unlock(mLock);
        android::base::Timer t;
        delay = mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags);
        interceptResult =
                mPolicy.interceptKeyBeforeDispatching(focusedWindowToken, event, entry.policyFlags);
        if (t.duration() > SLOW_INTERCEPTION_THRESHOLD) {
            ALOGW("Excessive delay in interceptKeyBeforeDispatching; took %s ms",
                  std::to_string(t.duration().count()).c_str());
        }
    } // acquire lock

    if (delay < 0) {
        entry.interceptKeyResult = KeyEntry::InterceptKeyResult::SKIP;
    } else if (delay == 0) {
        entry.interceptKeyResult = KeyEntry::InterceptKeyResult::CONTINUE;
    } else {
    if (std::holds_alternative<KeyEntry::InterceptKeyResult>(interceptResult)) {
        entry.interceptKeyResult = std::get<KeyEntry::InterceptKeyResult>(interceptResult);
        return;
    }

    if (std::holds_alternative<nsecs_t>(interceptResult)) {
        entry.interceptKeyResult = KeyEntry::InterceptKeyResult::TRY_AGAIN_LATER;
        entry.interceptKeyWakeupTime = now() + delay;
        entry.interceptKeyWakeupTime = now() + std::get<nsecs_t>(interceptResult);
    }
}

+5 −3
Original line number Diff line number Diff line
@@ -20,12 +20,14 @@

#include <android-base/properties.h>
#include <binder/IBinder.h>
#include <dispatcher/Entry.h>
#include <gui/InputApplication.h>
#include <gui/PidUid.h>
#include <input/Input.h>
#include <input/InputDevice.h>
#include <utils/RefBase.h>
#include <set>
#include <variant>

namespace android {

@@ -106,8 +108,8 @@ public:
                                               uint32_t& policyFlags) = 0;

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

    /* Allows the policy a chance to perform default processing for an unhandled key.
+11 −3
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#include "FakeInputDispatcherPolicy.h"

#include <variant>

#include <gtest/gtest.h>

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

nsecs_t FakeInputDispatcherPolicy::interceptKeyBeforeDispatching(const sp<IBinder>&,
                                                                 const KeyEvent&, uint32_t) {
std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult>
FakeInputDispatcherPolicy::interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&,
                                                         uint32_t) {
    if (mConsumeKeyBeforeDispatching) {
        return -1;
        return inputdispatcher::KeyEntry::InterceptKeyResult::SKIP;
    }

    nsecs_t delay = std::chrono::nanoseconds(mInterceptKeyTimeout).count();
    if (delay == 0) {
        return inputdispatcher::KeyEntry::InterceptKeyResult::CONTINUE;
    }

    // Clear intercept state so we could dispatch the event in next wake.
    mInterceptKeyTimeout = 0ms;
    return delay;
+4 −1
Original line number Diff line number Diff line
@@ -28,11 +28,13 @@
#include <optional>
#include <queue>
#include <string>
#include <variant>
#include <vector>

#include <android-base/logging.h>
#include <android-base/thread_annotations.h>
#include <binder/IBinder.h>
#include <dispatcher/Entry.h>
#include <gui/PidUid.h>
#include <gui/WindowInfo.h>
#include <input/BlockingQueue.h>
@@ -189,7 +191,8 @@ private:
    void interceptKeyBeforeQueueing(const KeyEvent& inputEvent, uint32_t&) override;
    void interceptMotionBeforeQueueing(ui::LogicalDisplayId, uint32_t, int32_t, nsecs_t,
                                       uint32_t&) override;
    nsecs_t interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override;
    std::variant<nsecs_t, inputdispatcher::KeyEntry::InterceptKeyResult>
    interceptKeyBeforeDispatching(const sp<IBinder>&, const KeyEvent&, uint32_t) override;
    std::optional<KeyEvent> dispatchUnhandledKey(const sp<IBinder>&, const KeyEvent& event,
                                                 uint32_t) override;
    void notifySwitch(nsecs_t when, uint32_t switchValues, uint32_t switchMask,