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 #ifndef ACCESSIBILITY_TOUCH_GUIDER_H 17 #define ACCESSIBILITY_TOUCH_GUIDER_H 18 19 #include <string> 20 #include "accessibility_element_info.h" 21 #include "accessibility_element_operator_callback_stub.h" 22 #include "accessibility_event_transmission.h" 23 #include "accessibility_gesture_recognizer.h" 24 #include "accessible_ability_manager_service.h" 25 26 namespace OHOS { 27 namespace Accessibility { 28 class TouchGuider; 29 30 const int32_t MAX_POINTER_COUNT = 32; 31 const int64_t EXIT_GESTURE_REC_TIMEOUT = 2000; // millisecond 32 const double MAX_DRAG_GESTURE_COSINE = 0.525321989; 33 const int32_t MINI_POINTER_DISTANCE_DIP = 200; 34 const int32_t INDEX_0 = 0; 35 const int32_t INDEX_1 = 1; 36 const int32_t INIT_POINT_ID = -1; 37 const float INIT_MMIPOINT = 0.0f; 38 #define DIVIDE_2(num) ((num) / 2) 39 #define EPSINON 0.01 40 41 /** 42 * @brief touch Guider state define 43 */ 44 enum class TouchGuideState : int32_t { 45 TOUCH_GUIDING, 46 DRAGGING, 47 TRANSMITTING, 48 GESTURE_RECOGNIZING 49 }; 50 51 /** 52 * @brief Click location define 53 */ 54 enum ClickLocation : int32_t { 55 CLICK_NONE, 56 CLICK_ACCESSIBILITY_FOCUS, 57 CLICK_LAST_TOUCH_GUIDE 58 }; 59 60 /** 61 * @brief struct to record injected pointers. 62 */ 63 struct InjectedEventRecorder { 64 int32_t downPointers; 65 int32_t downPointerNum; 66 int64_t lastDownTime; 67 std::shared_ptr<MMI::PointerEvent> lastHoverEvent; 68 }; 69 70 /** 71 * @brief struct to record received pointers. 72 */ 73 struct ReceivedEventRecorder { 74 int32_t pointerDownX[MAX_POINTER_COUNT]; 75 int32_t pointerDownY[MAX_POINTER_COUNT]; 76 std::shared_ptr<MMI::PointerEvent> lastEvent; 77 }; 78 79 enum ChangeAction : int32_t { 80 NO_CHANGE, 81 HOVER_MOVE, 82 POINTER_DOWN, 83 POINTER_UP, 84 POINTER_MOVE, 85 }; 86 87 class TGEventHandler : public AppExecFwk::EventHandler { 88 public: 89 TGEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, 90 TouchGuider &tgServer); 91 virtual ~TGEventHandler() = default; 92 /** 93 * @brief Process the event of install system bundles. 94 * @param event Indicates the event to be processed. 95 */ 96 virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 97 98 private: 99 /** 100 * @brief Send HoverEnter and HoverMove to Multimodal. 101 */ 102 void HoverEnterAndMoveRunner(); 103 104 /** 105 * @brief Send HoverExit to Multimodal. 106 */ 107 void HoverExitRunner(); 108 TouchGuider &tgServer_; 109 }; 110 111 class TouchGuider : public EventTransmission { 112 public: 113 static constexpr uint32_t EXIT_GESTURE_REC_MSG = 0; 114 static constexpr uint32_t SEND_HOVER_ENTER_MOVE_MSG = 1; 115 static constexpr uint32_t SEND_HOVER_EXIT_MSG = 2; 116 static constexpr uint32_t SEND_TOUCH_INTERACTION_END_MSG = 3; 117 static constexpr uint32_t SEND_TOUCH_GUIDE_END_MSG = 4; 118 119 /** 120 * @brief A constructor used to create a touchGuide instance. 121 */ 122 TouchGuider(); 123 124 /** 125 * @brief A destructor used to delete the touchGuide instance. 126 */ ~TouchGuider()127 ~TouchGuider() {} 128 129 /** 130 * @brief TouchGuide start up. 131 */ 132 void StartUp(); 133 134 /** 135 * @brief Handle pointer events from previous event stream node. 136 * 137 * @param event the pointer event to be handled. 138 * @return true: the event has been processed and does not need to be passed to the next node; 139 * false: the event is not processed. 140 */ 141 bool OnPointerEvent(MMI::PointerEvent &event) override; 142 143 /** 144 * @brief Destroy event state. 145 */ 146 void DestroyEvents() override; 147 148 /** 149 * @brief Send event to multimodal input. 150 * @param event the event prepared to send to Multimodal 151 * @param action the action of the event 152 */ 153 void SendEventToMultimodal(MMI::PointerEvent &event, int32_t action); 154 155 /** 156 * @brief Send accessibility event to specific AccessibleAbility. 157 * @param eventType the type of the event 158 */ 159 void SendAccessibilityEventToAA(EventType eventType); 160 161 /** 162 * @brief Send gesture event to specific AccessibleAbility. 163 * @param gestureId the gesture id of the event 164 */ 165 void SendGestureEventToAA(GestureType gestureId); 166 167 /** 168 * @brief Get hover enter and move event. 169 * @return Returns pointerEvents_ list. 170 */ 171 std::list<MMI::PointerEvent> getHoverEnterAndMoveEvent(); 172 173 /** 174 * @brief Clear hover enter and move event. 175 */ 176 void ClearHoverEnterAndMoveEvent(); 177 178 /** 179 * @brief Get last received event. 180 * @return Returns last event ptr. 181 */ 182 std::shared_ptr<MMI::PointerEvent> getLastReceivedEvent(); 183 184 /* For TouchGuide */ OnTouchInteractionStart()185 inline void OnTouchInteractionStart() 186 { 187 isTouchStart_ = true; 188 } 189 OnTouchInteractionEnd()190 inline void OnTouchInteractionEnd() 191 { 192 isTouchStart_ = false; 193 } 194 195 /** 196 * @brief Perform action on Accessibility Focus. 197 * @param action the action of Accessibility node. 198 * @return Returns true if the action perform successfully; returns false code otherwise. 199 */ 200 bool ExecuteActionOnAccessibilityFocused(const ActionType &action); 201 202 private: 203 class TouchGuideListener : public AccessibilityGestureRecognizeListener { 204 public: 205 /** 206 * @brief A constructor used to create a TouchGuideListener instance. 207 */ TouchGuideListener(TouchGuider & server)208 explicit TouchGuideListener(TouchGuider &server) : server_(server) {}; 209 210 /** 211 * @brief Prepare to send the event corresponding to the single tap to the Multimodal. 212 * @param event the touch event from Multimodal 213 */ 214 bool OnDoubleTap(MMI::PointerEvent &event) override; 215 216 /** 217 * @brief Send GESTURE_BEGIN to AccessibleAbility. 218 */ 219 bool OnStarted() override; 220 221 /** 222 * @brief Send GESTURE_END and TOUCH_END to AccessibleAbility. 223 * @param gestureId the id of gesture 224 */ 225 bool OnCompleted(GestureType gestureId) override; 226 227 /** 228 * @brief The gesture has been cancelled. 229 * @param event the touch event from Multimodal 230 */ 231 bool OnCancelled(MMI::PointerEvent &event) override; 232 233 private: 234 TouchGuider &server_; 235 }; 236 237 class ElementOperatorCallbackImpl : public AccessibilityElementOperatorCallbackStub { 238 public: 239 ElementOperatorCallbackImpl() = default; 240 ~ElementOperatorCallbackImpl() = default; 241 242 virtual void SetSearchElementInfoByAccessibilityIdResult(const std::vector<AccessibilityElementInfo> &infos, 243 const int32_t requestId) override; 244 virtual void SetSearchElementInfoByTextResult(const std::vector<AccessibilityElementInfo> &infos, 245 const int32_t requestId) override; 246 virtual void SetFindFocusedElementInfoResult(const AccessibilityElementInfo &info, 247 const int32_t requestId) override; 248 virtual void SetFocusMoveSearchResult(const AccessibilityElementInfo &info, const int32_t requestId) override; 249 virtual void SetExecuteActionResult(const bool succeeded, const int32_t requestId) override; 250 251 private: 252 std::promise<void> promise_; 253 bool executeActionResult_ = false; 254 AccessibilityElementInfo accessibilityInfoResult_ = {}; 255 std::vector<AccessibilityElementInfo> elementInfosResult_; 256 257 friend class TouchGuider; 258 }; 259 260 /** 261 * @brief Determine whether to clear the touchguide. 262 */ 263 void Clear(); 264 265 /** 266 * @brief clear the touchguide. 267 * @param event the last event from Multimodal 268 */ 269 void Clear(MMI::PointerEvent &event); 270 271 /** 272 * @brief Handle touch events on touchExploring state. 273 * @param event the touch event from Multimodal 274 */ 275 void HandleTouchGuidingState(MMI::PointerEvent &event); 276 277 /** 278 * @brief Handle touch events on dragging state. 279 * @param event the touch event from Multimodal 280 */ 281 void HandleDraggingState(MMI::PointerEvent &event); 282 283 /** 284 * @brief Handle touch events on transmitting state. 285 * @param event the touch event from Multimodal 286 */ 287 void HandleTransmitingState(MMI::PointerEvent &event); 288 289 /** 290 * @brief Determine whether it is a drag gesture. 291 * @param event the touch event from Multimodal 292 * @return whether the dragGesture is accepted. 293 */ 294 bool IsDragGestureAccept(MMI::PointerEvent &event); 295 296 /** 297 * @brief Get Angle Cos value. 298 * @param offsetX the X value 299 * @param offsetY the Y value 300 * @param isGetX whether is the Angle corresponding to the X axis 301 * @return Angle Cos value. 302 */ 303 float GetAngleCos(float offsetX, float offsetY, bool isGetX); 304 305 /** 306 * @brief Get the info of injected event. 307 * @param event the event prepared to send to Multimodal 308 */ 309 void RecordInjectedEvent(MMI::PointerEvent &event); 310 311 /** 312 * @brief Get the info of Received event. 313 * @param event event the touch event from Multimodal 314 */ 315 void RecordReceivedEvent(MMI::PointerEvent &event); 316 317 /** 318 * @brief Clear received recorder info. 319 */ 320 void ClearReceivedEventRecorder(); 321 322 /** 323 * @brief Clear Injected recorder info. 324 */ 325 void ClearInjectedEventRecorder(); 326 327 /** 328 * @brief Send exit event to multimodal. 329 */ 330 void SendExitEvents(); 331 332 /** 333 * @brief Send all down events to multimodal. 334 * @param event the event prepared to send to Multimodal 335 */ 336 void SendAllDownEvents(MMI::PointerEvent &event); 337 338 /** 339 * @brief Send all up events to multimodal. 340 * @param event the event prepared to send to Multimodal 341 */ 342 void SendUpForAllInjectedEvent(MMI::PointerEvent &event); 343 344 /** 345 * @brief Send exit message. 346 */ 347 void PostGestureRecognizeExit(); 348 349 /** 350 * @brief Send enter and move message. 351 * @param event event the touch event from Multimodal 352 */ 353 void PostHoverEnterAndMove(MMI::PointerEvent &event); 354 355 /** 356 * @brief Send exit message. 357 */ 358 void PostHoverExit(); 359 360 /** 361 * @brief Send accessibility event message. 362 * @param innerEventID the id of inner event 363 */ 364 void PostAccessibilityEvent(uint32_t innerEventID); 365 366 /** 367 * @brief Cancel message. 368 * @param innerEventID the id of inner event 369 */ 370 void CancelPostEvent(uint32_t innerEventID); 371 372 /** 373 * @brief Cancel message if it has been sent. 374 * @param innerEventID the id of inner event 375 */ 376 void CancelPostEventIfNeed(uint32_t innerEventID); 377 378 /** 379 * @brief Check whether it has been sending. 380 * @param innerEventID the id of inner event 381 */ 382 bool HasEventPending(uint32_t innerEventID); 383 384 /** 385 * @brief Force send and remove event. 386 * @param innerEventID the id of inner event 387 * @param event event the touch event from Multimodal 388 */ 389 void ForceSendAndRemoveEvent(uint32_t innerEventID, MMI::PointerEvent &event); 390 391 /** 392 * @brief Handle down events on touchExploring state. 393 * @param event event the touch event from Multimodal 394 */ 395 void HandleTouchGuidingStateInnerDown(MMI::PointerEvent &event); 396 397 /** 398 * @brief Handle move events on touchExploring state. 399 * @param event event the touch event from Multimodal 400 */ 401 void HandleTouchGuidingStateInnerMove(MMI::PointerEvent &event); 402 403 /** 404 * @brief Handle move events on dragging state. 405 * @param event event the touch event from Multimodal 406 */ 407 void HandleDraggingStateInnerMove(MMI::PointerEvent &event); 408 409 int32_t currentState_ = -1; 410 int32_t longPressPointId_ = INIT_POINT_ID; 411 float longPressOffsetX_ = INIT_MMIPOINT; 412 float longPressOffsetY_ = INIT_MMIPOINT; 413 bool isTouchStart_ = false; 414 bool isTouchGuiding_ = false; 415 ReceivedEventRecorder receivedRecorder_ = {}; 416 InjectedEventRecorder injectedRecorder_ = {}; 417 std::list<MMI::PointerEvent> pointerEvents_ {}; 418 AccessibilityGestureRecognizer gestureRecognizer_; 419 std::unique_ptr<TouchGuideListener> touchGuideListener_ = nullptr; 420 std::shared_ptr<TGEventHandler> handler_ = nullptr; 421 std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr; 422 }; 423 } // namespace Accessibility 424 } // namespace OHOS 425 #endif // ACCESSIBILITY_TOUCH_GUIDER_H