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_COMMON_EVENT_REGISTRY_H
17 #define ACCESSIBILITY_COMMON_EVENT_REGISTRY_H
18 
19 #include <map>
20 #include <memory>
21 #include <functional>
22 #include <string>
23 #include "common_event_subscriber.h"
24 #include "event_handler.h"
25 #include "singleton.h"
26 #include "want.h"
27 
28 using EventHandle = std::function<void(const OHOS::EventFwk::CommonEventData &)>;
29 
30 namespace OHOS {
31 namespace Accessibility {
32 class AccessibilityCommonEvent {
33     DECLARE_SINGLETON(AccessibilityCommonEvent)
34 public:
35     void SubscriberEvent(const std::shared_ptr<AppExecFwk::EventHandler> &handler);
36     void UnSubscriberEvent();
37     void OnReceiveEvent(const EventFwk::CommonEventData &data);
38 
39 private:
40     class AccessibilityCommonEventSubscriber : public EventFwk::CommonEventSubscriber {
41     public:
AccessibilityCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscriberInfo,AccessibilityCommonEvent & registry)42         explicit AccessibilityCommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscriberInfo,
43             AccessibilityCommonEvent &registry)
44             : CommonEventSubscriber(subscriberInfo), registry_(registry) {}
45         ~AccessibilityCommonEventSubscriber() = default;
46 
OnReceiveEvent(const EventFwk::CommonEventData & data)47         void OnReceiveEvent(const EventFwk::CommonEventData &data) override
48         {
49             registry_.OnReceiveEvent(data);
50         }
51 
52     private:
53         AccessibilityCommonEvent &registry_;
54     };
55 
56     void HandleUserAdded(const EventFwk::CommonEventData &data) const;
57     void HandleUserRemoved(const EventFwk::CommonEventData &data) const;
58     void HandleUserSwitched(const EventFwk::CommonEventData &data) const;
59 
60     void HandlePackageRemoved(const EventFwk::CommonEventData &data) const;
61     void HandlePackageChanged(const EventFwk::CommonEventData &data) const;
62     void HandlePackageAdd(const EventFwk::CommonEventData &data) const;
63 
64     typedef void (AccessibilityCommonEvent::*HandleEventFunc)(const EventFwk::CommonEventData &) const;
65     std::map<std::string, EventHandle> eventHandles_;
66     std::map<std::string, HandleEventFunc> handleEventFunc_;
67     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ = nullptr;
68     std::shared_ptr<AccessibilityCommonEventSubscriber> subscriber_ = nullptr;
69 };
70 } // namespace Accessibility
71 } // namespace OHOS
72 #endif // ACCESSIBILITY_COMMON_EVENT_REGISTRY_H