blob: 3f5616190b4d8843dc0413fdbcd5c95d67b1bf5e [file] [log] [blame]
Siarhei Vishniakoua3c8e512022-02-10 19:46:34 -08001/*
2 * Copyright (C) 2022 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 <ftl/static_vector.h>
20#include <optional>
21#include "InputListener.h"
22
23namespace android {
24
25/**
26 * When stylus is down, we ignore all touch.
27 * TODO(b/210159205): delete this when simultaneous stylus and touch is supported
28 */
29class PreferStylusOverTouchBlocker {
30public:
31 /**
32 * Process the provided event and emit up to 2 events in response.
33 * In the majority of cases, the returned result will just be the provided args (array with
34 * only 1 element), unmodified.
35 *
36 * If the gesture should be blocked, the returned result may be:
37 *
38 * a) An empty array, if the current event should just be ignored completely
39 * b) An array of 2 elements, containing an event with ACTION_CANCEL and the current event.
40 *
41 * bool is set to 'true'.
42 * NotifyMotionArgs potentially contains an event that should be used to cancel the existing
43 * gesture.
44 *
45 * If the event should not be blocked, bool contains 'false'.
46 */
47 ftl::StaticVector<NotifyMotionArgs, 2> processMotion(const NotifyMotionArgs& args);
48 std::string dump();
49
50private:
51 bool mIsTouchDown = false;
52 bool mIsStylusDown = false;
53 // Provide some default values for the stored MotionEvent to allow printint the event before
54 // any real event is received.
55 NotifyMotionArgs mLastTouchEvent{0 /*id*/,
56 0 /*eventTime*/,
57 0 /*readTime*/,
58 0 /*deviceId*/,
59 AINPUT_SOURCE_TOUCHSCREEN,
60 0 /*displayId*/,
61 0 /*policyFlags*/,
62 0 /*action*/,
63 0 /*actionButton*/,
64 0 /*flags*/,
65 0 /*metaState*/,
66 0 /*buttonState*/,
67 MotionClassification::NONE,
68 AMOTION_EVENT_EDGE_FLAG_NONE,
69 0 /*pointerCount*/,
70 nullptr /*properties*/,
71 nullptr /*coords*/,
72 0. /*xPrecision*/,
73 0. /*yPrecision*/,
74 AMOTION_EVENT_INVALID_CURSOR_POSITION,
75 AMOTION_EVENT_INVALID_CURSOR_POSITION,
76 0 /*downTime*/,
77 {}};
78 bool mCurrentTouchIsCanceled = false;
79};
80
81} // namespace android