1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed On an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 
17 #ifndef ACCESSIBILITY_TOUCHEVENT_INJECTOR_H
18 #define ACCESSIBILITY_TOUCHEVENT_INJECTOR_H
19 
20 #include <time.h>
21 #include "accessibility_event_transmission.h"
22 #include "event_handler.h"
23 #include "event_runner.h"
24 #include "accessibility_gesture_inject_path.h"
25 #include "pointer_event.h"
26 #include "singleton.h"
27 
28 namespace OHOS {
29 namespace Accessibility {
30 const int64_t DOUBLE_TAP_MIN_TIME = 50000; // microsecond
31 
32 struct SendEventArgs {
33     std::shared_ptr<MMI::PointerEvent> event_;
34 };
35 
36 class TouchEventInjector;
37 class TouchInjectHandler : public AppExecFwk::EventHandler {
38 public:
39     TouchInjectHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, TouchEventInjector &server);
40     virtual ~TouchInjectHandler() = default;
41     /**
42      * @brief Process the event of install system bundles.
43      * @param event Indicates the event to be processed.
44      */
45     virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
46 private:
47     TouchEventInjector &server_;
48 };
49 
50 class TouchEventInjector : public EventTransmission, public AppExecFwk::EventHandler {
51 public:
52     static constexpr uint32_t SEND_TOUCH_EVENT_MSG = 1;
53 
54     /**
55      * @brief A constructor used to create a TouchEventInjector instance.
56      */
57     TouchEventInjector();
58 
59     /**
60      * @brief A destructor used to delete the TouchEventInjector instance.
61      */
~TouchEventInjector()62     ~TouchEventInjector() {}
63 
64     /**
65      * @brief Handle pointer events from previous event stream node.
66      * @param event the pointer event from Multimodal
67      * @return true: the event has been processed and does not need to be passed to the next node;
68      *         false: the event is not processed.
69      */
70     bool OnPointerEvent(MMI::PointerEvent &event) override;
71 
72     /**
73      * @brief Destroy event state.
74      */
75     void DestroyEvents() override;
76 
77     /**
78      * @brief Send pointer event to next stream node.
79      * @param event the touch event prepared to send
80      */
81     void SendPointerEvent(MMI::PointerEvent &event);
82 
83     /**
84      * @brief Inject simulated gestures.
85      * @param gesturePath the gesture path
86      */
87     void InjectEvents(const std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath);
88 
89 private:
90     /**
91      * @brief Cancel the gesture.
92      */
93     void CancelGesture();
94 
95     /**
96      * @brief Cancel the injected events.
97      */
98     void CancelInjectedEvents();
99 
100     /**
101      * @brief create touchevent.
102      * @param action the action of event
103      * @param point the endpoint of event
104      * @return the created touchevent
105      */
106     std::shared_ptr<MMI::PointerEvent> obtainTouchEvent(int32_t action,
107         MMI::PointerEvent::PointerItem point, int64_t actionTime);
108 
109     /**
110      * @brief Get the number of microseconds elapsed since the system was booted.
111      * @return the number of microseconds elapsed since the system was booted
112      */
113     int64_t GetSystemTime();
114 
115     /**
116      * @brief Parse taps events.
117      * @param startTime the start time of gesture injection
118      * @param gesturePath the gesture path
119      */
120     void ParseTapsEvents(int64_t startTime, const std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath);
121 
122     /**
123      * @brief Parse move events.
124      * @param startTime the start time of gesture injection
125      * @param gesturePath the gesture path
126      */
127     void ParseMovesEvents(int64_t startTime, const std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath);
128 
129     /**
130      * @brief Parse touchevents from gesturepath.
131      * @param startTime the start time of gesture injection
132      * @param gesturePath the gesture path
133      */
134     void ParseTouchEventsFromGesturePath(int64_t startTime,
135         const std::shared_ptr<AccessibilityGestureInjectPath>& gesturePath);
136 
137     bool isGestureUnderway_ = false;
138     bool isDestroyEvent_ = false;
139     std::shared_ptr<TouchInjectHandler> handler_ = nullptr;
140     std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr;
141     std::vector<std::shared_ptr<MMI::PointerEvent>> injectedEvents_;
142 };
143 } // namespace Accessibility
144 } // namespace OHOS
145 #endif // ACCESSIBILITY_TOUCHEVENT_INJECTOR_H