blob: c1064102e8ed3f6c6254bd6f18f19ef6a0a3752b [file] [log] [blame]
Yeabkal Wubshit6b662762023-05-22 23:07:31 -07001/*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <utils/Timers.h>
20
21namespace android {
22
23/**
24 * Controls a slop logic. Slop here refers to an approach to try and drop insignificant input
25 * events. This is helpful in cases where unintentional input events may cause unintended outcomes,
26 * like scrolling a screen or keeping the screen awake.
27 *
28 * Current slop logic:
29 * "If time since last event > Xns, then discard the next N values."
30 */
Yeabkal Wubshitf1ed7052023-06-16 10:01:53 -070031class SlopController final {
Yeabkal Wubshit6b662762023-05-22 23:07:31 -070032public:
33 SlopController(float slopThreshold, nsecs_t slopDurationNanos);
Yeabkal Wubshit6b662762023-05-22 23:07:31 -070034
35 /**
36 * Consumes an event with a given time and value for slop processing.
37 * Returns an amount <=value that should be consumed.
38 */
39 float consumeEvent(nsecs_t eventTime, float value);
40
41private:
Yeabkal Wubshitf1ed7052023-06-16 10:01:53 -070042 bool shouldResetSlopTracking(nsecs_t eventTimeNanos, float value) const;
Yeabkal Wubshit6b662762023-05-22 23:07:31 -070043
44 /** The amount of event values ignored after an inactivity of the slop duration. */
45 const float mSlopThreshold;
46 /** The duration of inactivity that resets slop controlling. */
47 const nsecs_t mSlopDurationNanos;
48
49 nsecs_t mLastEventTimeNanos = 0;
50 float mCumulativeValue = 0;
51 bool mHasSlopBeenMet = false;
52};
53
54} // namespace android