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_KEYEVENT_FILTER_H 17 #define ACCESSIBILITY_KEYEVENT_FILTER_H 18 19 #include <map> 20 #include <vector> 21 #include <memory> 22 23 #include "accessibility_event_transmission.h" 24 #include "event_handler.h" 25 #include "ffrt.h" 26 27 namespace OHOS { 28 namespace Accessibility { 29 class AccessibleAbilityConnection; 30 class AccessibleAbilityManagerService; 31 class KeyEventFilterEventHandler; 32 33 class KeyEventFilter : public EventTransmission { 34 public: 35 struct ProcessingEvent { 36 std::shared_ptr<MMI::KeyEvent> event_; 37 uint32_t usedCount_; 38 uint32_t seqNum_; 39 }; 40 41 /** 42 * @brief A constructor used to create a KeyEventFilter instance. 43 */ 44 KeyEventFilter(); 45 46 /** 47 * @brief A destructor used to delete the KeyEventFilter instance. 48 */ 49 virtual ~KeyEventFilter(); 50 51 /** 52 * @brief Handle key events from previous event stream node. 53 * @param event the key event from Multimodal 54 * @return true: the event has been processed and does not need to be passed to the next node; 55 * false: the event is not processed. 56 */ 57 bool OnKeyEvent(MMI::KeyEvent &event) override; 58 59 /** 60 * @brief Send key event to next stream node. 61 * @param event the key event prepared to send 62 */ 63 void SendEventToNext(MMI::KeyEvent &event); 64 65 /** 66 * @brief Set AccessibleAbility keyevent result. 67 * @param connection the AccessibleAbility 68 * @param isHandled true if the AccessibleAbility can handle the event else false 69 * @param sequenceNum the sequence of keyevent 70 */ 71 void SetServiceOnKeyEventResult(AccessibleAbilityConnection &connection, bool isHandled, uint32_t sequenceNum); 72 73 /** 74 * @brief Clear AccessibleAbility keyevents. 75 * @param connection the AccessibleAbility 76 */ 77 void ClearServiceKeyEvents(AccessibleAbilityConnection &connection); 78 79 /** 80 * @brief Destroy the events. 81 */ 82 void DestroyEvents() override; 83 84 /** 85 * @brief Remove the processing event. 86 * @param event the event be removed 87 * @return true if remove successfully else false 88 */ 89 bool RemoveProcessingEvent(std::shared_ptr<ProcessingEvent> event); 90 91 private: 92 /** 93 * @brief Dispatch the keyevents. 94 * @param event the keyevent from Multimodal 95 */ 96 void DispatchKeyEvent(MMI::KeyEvent &event); 97 98 /** 99 * @brief Find processing event. 100 * @param connection the corresponding AccessibleAbility 101 * @param sequenceNum the sequence of event 102 * @return the processing event 103 */ 104 std::shared_ptr<ProcessingEvent> FindProcessingEvent(AccessibleAbilityConnection &connection, 105 uint32_t sequenceNum); 106 107 std::map<sptr<AccessibleAbilityConnection>, std::vector<std::shared_ptr<ProcessingEvent>>> eventMaps_; 108 std::shared_ptr<KeyEventFilterEventHandler> timeoutHandler_ = nullptr; 109 std::shared_ptr<AppExecFwk::EventRunner> runner_ = nullptr; 110 uint32_t sequenceNum_ = 0; 111 ffrt::mutex mutex_; 112 }; 113 114 class KeyEventFilterEventHandler : public AppExecFwk::EventHandler { 115 public: 116 /** 117 * @brief A constructor used to create a KeyEventFilterEventHandler instance. 118 */ 119 KeyEventFilterEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, KeyEventFilter &keyEventFilter); 120 virtual ~KeyEventFilterEventHandler() = default; 121 122 /** 123 * @brief Process the event of install system bundles. 124 * @param event Indicates the event to be processed. 125 */ 126 virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 127 128 private: 129 KeyEventFilter &keyEventFilter_; 130 }; 131 } // namespace Accessibility 132 } // namespace OHOS 133 #endif // ACCESSIBILITY_KEYEVENT_FILTER_H