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

Commit b4e50943 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Metrics Collector: Prevent queuing interactions from ignored devices"...

Merge "Metrics Collector: Prevent queuing interactions from ignored devices" into udc-qpr-dev am: dff3962c am: acebd1e8

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/native/+/23866546



Change-Id: I9d8ae2f97304b87ebe4467fc46e40ba98cc3fade
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents ae0a492d acebd1e8
Loading
Loading
Loading
Loading
+9 −1
Original line number Original line Diff line number Diff line
@@ -39,6 +39,8 @@ constexpr nanoseconds DEFAULT_USAGE_SESSION_TIMEOUT = std::chrono::minutes(2);
 */
 */
const bool DEBUG = __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO);
const bool DEBUG = __android_log_is_loggable(ANDROID_LOG_DEBUG, LOG_TAG, ANDROID_LOG_INFO);


constexpr size_t INTERACTIONS_QUEUE_CAPACITY = 500;

int32_t linuxBusToInputDeviceBusEnum(int32_t linuxBus) {
int32_t linuxBusToInputDeviceBusEnum(int32_t linuxBus) {
    // When adding cases to this switch, also add them to the copy of this method in
    // When adding cases to this switch, also add them to the copy of this method in
    // TouchpadInputMapper.cpp.
    // TouchpadInputMapper.cpp.
@@ -201,7 +203,10 @@ InputDeviceMetricsCollector::InputDeviceMetricsCollector(InputListenerInterface&
InputDeviceMetricsCollector::InputDeviceMetricsCollector(InputListenerInterface& listener,
InputDeviceMetricsCollector::InputDeviceMetricsCollector(InputListenerInterface& listener,
                                                         InputDeviceMetricsLogger& logger,
                                                         InputDeviceMetricsLogger& logger,
                                                         nanoseconds usageSessionTimeout)
                                                         nanoseconds usageSessionTimeout)
      : mNextListener(listener), mLogger(logger), mUsageSessionTimeout(usageSessionTimeout) {}
      : mNextListener(listener),
        mLogger(logger),
        mUsageSessionTimeout(usageSessionTimeout),
        mInteractionsQueue(INTERACTIONS_QUEUE_CAPACITY) {}


void InputDeviceMetricsCollector::notifyInputDevicesChanged(
void InputDeviceMetricsCollector::notifyInputDevicesChanged(
        const NotifyInputDevicesChangedArgs& args) {
        const NotifyInputDevicesChangedArgs& args) {
@@ -262,6 +267,9 @@ void InputDeviceMetricsCollector::notifyPointerCaptureChanged(


void InputDeviceMetricsCollector::notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
void InputDeviceMetricsCollector::notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp,
                                                          const std::set<Uid>& uids) {
                                                          const std::set<Uid>& uids) {
    if (isIgnoredInputDeviceId(deviceId)) {
        return;
    }
    mInteractionsQueue.push(DeviceId{deviceId}, timestamp, uids);
    mInteractionsQueue.push(DeviceId{deviceId}, timestamp, uids);
}
}


+15 −2
Original line number Original line Diff line number Diff line
@@ -27,6 +27,10 @@ namespace android {
template <class T>
template <class T>
class SyncQueue {
class SyncQueue {
public:
public:
    SyncQueue() = default;

    SyncQueue(size_t capacity) : mCapacity(capacity) {}

    /** Retrieve and remove the oldest object. Returns std::nullopt if the queue is empty. */
    /** Retrieve and remove the oldest object. Returns std::nullopt if the queue is empty. */
    std::optional<T> pop() {
    std::optional<T> pop() {
        std::scoped_lock lock(mLock);
        std::scoped_lock lock(mLock);
@@ -38,14 +42,23 @@ public:
        return t;
        return t;
    };
    };


    /** Add a new object to the queue. */
    /**
     * Add a new object to the queue.
     * Return true if an element was successfully added.
     * Return false if the queue is full.
     */
    template <class... Args>
    template <class... Args>
    void push(Args&&... args) {
    bool push(Args&&... args) {
        std::scoped_lock lock(mLock);
        std::scoped_lock lock(mLock);
        if (mCapacity && mQueue.size() == mCapacity) {
            return false;
        }
        mQueue.emplace_back(args...);
        mQueue.emplace_back(args...);
        return true;
    };
    };


private:
private:
    const std::optional<size_t> mCapacity;
    std::mutex mLock;
    std::mutex mLock;
    std::list<T> mQueue GUARDED_BY(mLock);
    std::list<T> mQueue GUARDED_BY(mLock);
};
};
+1 −0
Original line number Original line Diff line number Diff line
@@ -421,6 +421,7 @@ TEST_F(InputDeviceMetricsCollectorTest, DontLogUsageForIgnoredDevices) {
        // Device was used.
        // Device was used.
        mMetricsCollector.notifyMotion(generateMotionArgs(ignoredDeviceId));
        mMetricsCollector.notifyMotion(generateMotionArgs(ignoredDeviceId));
        mTestListener.assertNotifyMotionWasCalled();
        mTestListener.assertNotifyMotionWasCalled();
        mMetricsCollector.notifyDeviceInteraction(ignoredDeviceId, TIME.count(), uids({0, 1, 2}));
        ASSERT_NO_FATAL_FAILURE(assertUsageNotLogged());
        ASSERT_NO_FATAL_FAILURE(assertUsageNotLogged());


        // Device was used again after the usage timeout expired, but we still don't log usage.
        // Device was used again after the usage timeout expired, but we still don't log usage.
+12 −0
Original line number Original line Diff line number Diff line
@@ -50,6 +50,18 @@ TEST(SyncQueueTest, isFIFO) {
    }
    }
}
}


// Make sure the queue has strict capacity limits.
TEST(SyncQueueTest, QueueReachesCapacity) {
    constexpr size_t capacity = 3;
    SyncQueue<int> queue(capacity);

    // First 3 elements should be added successfully
    ASSERT_TRUE(queue.push(1));
    ASSERT_TRUE(queue.push(2));
    ASSERT_TRUE(queue.push(3));
    ASSERT_FALSE(queue.push(4)) << "Queue should reach capacity at size " << capacity;
}

TEST(SyncQueueTest, AllowsMultipleThreads) {
TEST(SyncQueueTest, AllowsMultipleThreads) {
    SyncQueue<int> queue;
    SyncQueue<int> queue;