1 /*
2  * Copyright (C) 2021 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 EVENT_LISTENER_MANAGER_H
17 #define EVENT_LISTENER_MANAGER_H
18 
19 #include <cstdint>
20 #include <memory>
21 
22 #include "event_listener.h"
23 #include "event_listener_handler.h"
24 #include "telephony_log_wrapper.h"
25 #include "telephony_update_event_type.h"
26 
27 namespace OHOS {
28 namespace Telephony {
29 class EventListenerManager {
30 public:
31     template<typename T, typename D>
32     inline static bool SendEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object, int64_t delayTime = 0)
33     {
34         auto handler = DelayedSingleton<EventListenerHandler>::GetInstance();
35         if (handler == nullptr) {
36             TELEPHONY_LOGE("Get handler failed");
37             return false;
38         }
39         return handler->SendEvent(innerEventId, object, delayTime);
40     }
41 
SendEvent(uint32_t innerEventId)42     inline static bool SendEvent(uint32_t innerEventId)
43     {
44         auto handler = DelayedSingleton<EventListenerHandler>::GetInstance();
45         if (handler == nullptr) {
46             TELEPHONY_LOGE("Get handler failed");
47             return false;
48         }
49         return handler->SendEvent(innerEventId);
50     }
51 
52     static int32_t RegisterEventListener(EventListener &eventListener);
53     static int32_t UnregisterEventListener(napi_env env, const TelephonyUpdateEventType eventType, napi_ref ref,
54         std::list<EventListener> &removeListenerList);
55     static int32_t UnregisterEventListener(
56         napi_env env, TelephonyUpdateEventType eventType, std::list<EventListener> &removeListenerList);
57     static void UnRegisterAllListener(napi_env env);
58 };
59 } // namespace Telephony
60 } // namespace OHOS
61 #endif // EVENT_LISTENER_MANAGER_H
62