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