1 /*
2  * Copyright (c) 2023 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 SWITCH_EVENT_INPUT_SUBSCRIBE_MANAGER_H
17 #define SWITCH_EVENT_INPUT_SUBSCRIBE_MANAGER_H
18 
19 #include <functional>
20 #include <map>
21 #include <memory>
22 
23 #include <singleton.h>
24 
25 #include "switch_event.h"
26 
27 namespace OHOS {
28 namespace MMI {
29 class SwitchEventInputSubscribeManager final {
30     DECLARE_SINGLETON(SwitchEventInputSubscribeManager);
31 
32 public:
33     class SubscribeSwitchEventInfo {
34     public:
35         SubscribeSwitchEventInfo(int32_t switchType,
36             std::function<void(std::shared_ptr<SwitchEvent>)> callback);
37         ~SubscribeSwitchEventInfo() = default;
38 
GetSwitchType()39         int32_t GetSwitchType() const
40         {
41             return switchType_;
42         }
43 
GetCallback()44         std::function<void(std::shared_ptr<SwitchEvent>)> GetCallback() const
45         {
46             return callback_;
47         }
48     private:
49         int32_t switchType_ { -1 };
50         std::function<void(std::shared_ptr<SwitchEvent>)> callback_ { nullptr };
51     };
52 
53 public:
54     DISALLOW_MOVE(SwitchEventInputSubscribeManager);
55 
56     int32_t SubscribeSwitchEvent(int32_t switchType, std::function<void(std::shared_ptr<SwitchEvent>)> callback);
57     int32_t UnsubscribeSwitchEvent(int32_t subscribeId);
58 
59     int32_t OnSubscribeSwitchEventCallback(std::shared_ptr<SwitchEvent> event, int32_t subscribeId);
60     void OnConnected();
61 
62 private:
63     std::map<int32_t, SubscribeSwitchEventInfo> subscribeInfos_;
64     static int32_t subscribeManagerId_;
65     std::mutex mtx_;
66 };
67 
68 #define SWITCH_EVENT_INPUT_SUBSCRIBE_MGR ::OHOS::Singleton<SwitchEventInputSubscribeManager>::GetInstance()
69 } // namespace MMI
70 } // namespace OHOS
71 #endif // SWITCH_EVENT_INPUT_SUBSCRIBE_MANAGER_H