Yeabkal Wubshit | 6b66276 | 2023-05-22 23:07:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 21 | namespace 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 Wubshit | f1ed705 | 2023-06-16 10:01:53 -0700 | [diff] [blame] | 31 | class SlopController final { |
Yeabkal Wubshit | 6b66276 | 2023-05-22 23:07:31 -0700 | [diff] [blame] | 32 | public: |
| 33 | SlopController(float slopThreshold, nsecs_t slopDurationNanos); |
Yeabkal Wubshit | 6b66276 | 2023-05-22 23:07:31 -0700 | [diff] [blame] | 34 | |
| 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 | |
| 41 | private: |
Yeabkal Wubshit | f1ed705 | 2023-06-16 10:01:53 -0700 | [diff] [blame] | 42 | bool shouldResetSlopTracking(nsecs_t eventTimeNanos, float value) const; |
Yeabkal Wubshit | 6b66276 | 2023-05-22 23:07:31 -0700 | [diff] [blame] | 43 | |
| 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 |