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 "accessible_ability_client_interface.h" 23 #include "event_handler.h" 24 #include "event_runner.h" 25 #include "gesture_simulation.h" 26 #include "hilog_wrapper.h" 27 #include "pointer_event.h" 28 #include "singleton.h" 29 30 namespace OHOS { 31 namespace Accessibility { 32 const int64_t DOUBLE_TAP_MIN_TIME = 50; 33 34 struct SendEventArgs { 35 std::shared_ptr<MMI::PointerEvent> event_; 36 bool isLastEvent_; 37 }; 38 39 class TouchEventInjector; 40 class TouchInjectHandler : public AppExecFwk::EventHandler { 41 public: 42 TouchInjectHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, TouchEventInjector &server); 43 virtual ~TouchInjectHandler() = default; 44 /** 45 * @brief Process the event of install system bundles. 46 * @param event Indicates the event to be processed. 47 * @return 48 */ 49 virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 50 private: 51 TouchEventInjector &server_; 52 }; 53 54 class TouchEventInjector : public EventTransmission, public AppExecFwk::EventHandler { 55 public: 56 /** 57 * @brief A constructor used to create a TouchEventInjector instance. 58 * @param 59 * @return 60 */ 61 TouchEventInjector(); 62 63 /** 64 * @brief A destructor used to delete the TouchEventInjector instance. 65 * @param 66 * @return 67 */ ~TouchEventInjector()68 ~TouchEventInjector() {} 69 70 /** 71 * @brief Handle pointer events from previous event stream node. 72 * @param event the pointer event from Multimodal 73 * @return 74 */ 75 void OnPointerEvent(MMI::PointerEvent &event) override; 76 77 /** 78 * @brief Clear event state from specific input source. 79 * @param inputSource the input source 80 * @return 81 */ 82 void ClearEvents(uint32_t inputSource) override; 83 84 /** 85 * @brief Destroy event state. 86 * @param 87 * @return 88 */ 89 void DestroyEvents() override; 90 91 /** 92 * @brief Inject simulated gestures. 93 * @param gesturePath the vector of gesture path 94 * @param service the corresponding AccessiblityAbility 95 * @param sequence the sequence of gesture 96 * @return 97 */ 98 void InjectEvents(const std::vector<GesturePathDefine> &gesturePath, 99 const sptr<IAccessibleAbilityClient> &service, int sequence); 100 101 /** 102 * @brief Send pointer event to next stream node. 103 * @param event the touch event prepared to send 104 * @return 105 */ 106 void SendPointerEvent(MMI::PointerEvent &event); 107 108 /** 109 * @brief Parsing inject simulated gestures. 110 * @param 111 * @return 112 */ 113 void InjectEventsInner(); 114 115 /** 116 * @brief Get current gesture service. 117 * @param 118 * @return the corresponding AccessiblityAbility 119 */ GetCurrentGestureService()120 sptr<IAccessibleAbilityClient> GetCurrentGestureService() 121 { 122 return currentGestureService_; 123 } 124 125 /** 126 * @brief Get sequence of gesture. 127 * @param 128 * @return the sequence of gesture 129 */ GetSequence()130 int GetSequence() 131 { 132 return sequence_; 133 } 134 135 static constexpr uint32_t SEND_TOUCH_EVENT_MSG = 1; 136 static constexpr uint32_t INJECT_EVENT_MSG = 2; 137 138 private: 139 /** 140 * @brief Cancel the gesture. 141 * @param 142 * @return 143 */ 144 void CancelGesture(); 145 146 /** 147 * @brief Cancel the injected events. 148 * @param 149 * @return 150 */ 151 void CancelInjectedEvents(); 152 153 /** 154 * @brief Get taps events. 155 * @param 156 * @return 157 */ 158 void GetTapsEvents(int64_t startTime); 159 160 /** 161 * @brief Get move events. 162 * @param 163 * @return 164 */ 165 void GetMovesEvents(int64_t startTime); 166 167 /** 168 * @brief Get touchevents from gesturepath. 169 * @param 170 * @return 171 */ 172 void GetTouchEventsFromGesturePath(int64_t startTime); 173 174 /** 175 * @brief create touchevent. 176 * @param action the action of event 177 * @param point the endpoint of event 178 * @return the created touchevent 179 */ 180 std::shared_ptr<MMI::PointerEvent> obtainTouchEvent(int action, 181 MMI::PointerEvent::PointerItem point, int64_t actionTime); 182 183 /** 184 * @brief Get the number of milliseconds elapsed since the system was booted. 185 * @param 186 * @return the number of milliseconds elapsed since the system was booted 187 */ 188 int64_t getSystemTime(); 189 190 int sequence_ = -1; 191 bool isGestureUnderway_ = false; 192 bool isDestroyEvent_ = false; 193 std::vector<GesturePathDefine> gesturePath_; 194 sptr<IAccessibleAbilityClient> currentGestureService_ = nullptr; 195 std::shared_ptr<TouchInjectHandler> handler_ = nullptr; 196 std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr; 197 std::vector<std::shared_ptr<MMI::PointerEvent>> injectedEvents_; 198 }; 199 } // namespace Accessibility 200 } // namespace OHOS 201 #endif // ACCESSIBILITY_TOUCHEVENT_INJECTOR_H