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