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