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 TELEPHONY_EVENT_HANDLER_H 17 #define TELEPHONY_EVENT_HANDLER_H 18 19 #include "event_handler.h" 20 #include "telephony_errors.h" 21 #include "telephony_log_wrapper.h" 22 23 namespace OHOS { 24 namespace Telephony { 25 class TelEventQueue; 26 using TelTask = std::function<void()>; 27 class TelFFRTUtils final { 28 public: 29 /** 30 * Submit an FFRT asynchronous task. 31 * 32 * @param task TelTask. 33 */ 34 static void Submit(const TelTask &task); 35 /** 36 * Submit an FFRT asynchronous task. 37 * 38 * @param task TelTask. 39 */ 40 static void Submit(TelTask &&task); 41 42 /** 43 * Submit an FFRT synchronization task. 44 * 45 * @param task TelTask. 46 */ 47 static void SubmitSync(const TelTask &task); 48 49 /** 50 * Submit an FFRT synchronization task. 51 * 52 * @param task TelTask. 53 */ 54 static void SubmitSync(TelTask &&task); 55 56 /** 57 * FFRT task wait for some time. 58 * 59 * @param task TelTask. 60 */ 61 static void SleepFor(uint32_t timeoutMs); 62 }; 63 64 class TelEventHandler : public AppExecFwk::EventHandler { 65 public: 66 explicit TelEventHandler(const std::string &name); 67 68 /** 69 * Send an event. 70 * 71 * @param event Event which should be handled. 72 * @param delayTime Process the event after 'delayTime' milliseconds. 73 * @param priority Priority of the event queue for this event. 74 * @return Returns true if event has been sent successfully. If returns false, event should be released manually. 75 */ 76 bool SendEvent(AppExecFwk::InnerEvent::Pointer &event, int64_t delayTime = 0, Priority priority = Priority::LOW); 77 78 /** 79 * Process the event. Developers should override this method. 80 * 81 * @param event The event should be processed. 82 */ ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)83 virtual void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) {} 84 85 /** 86 * Send an event. 87 * 88 * @param event Event which should be handled. 89 * @param priority Priority of the event queue for this event. 90 * @return Returns true if event has been sent successfully. If returns false, event should be released manually. 91 */ SendEvent(AppExecFwk::InnerEvent::Pointer & event,Priority priority)92 inline bool SendEvent(AppExecFwk::InnerEvent::Pointer &event, Priority priority) 93 { 94 return SendEvent(event, 0, priority); 95 } 96 97 /** 98 * Send an event. 99 * 100 * @param event Event which should be handled. 101 * @param delayTime Process the event after 'delayTime' milliseconds. 102 * @param priority Priority of the event queue for this event. 103 * @return Returns true if event has been sent successfully. 104 */ 105 inline bool SendEvent(AppExecFwk::InnerEvent::Pointer &&event, int64_t delayTime = 0, 106 AppExecFwk::EventQueue::Priority priority = Priority::LOW) 107 { 108 return SendEvent(event, delayTime, priority); 109 } 110 111 /** 112 * Send an event. 113 * 114 * @param innerEventId The id of the event. 115 * @param param Basic parameter of the event, default is 0. 116 * @param delayTime Process the event after 'delayTime' milliseconds. 117 * @return Returns true if event has been sent successfully. 118 */ SendEvent(uint32_t innerEventId,int64_t param,int64_t delayTime)119 inline bool SendEvent(uint32_t innerEventId, int64_t param, int64_t delayTime) 120 { 121 return SendEvent(AppExecFwk::InnerEvent::Get(innerEventId, param), delayTime); 122 } 123 124 /** 125 * Send an event. 126 * 127 * @param innerEventId The id of the event. 128 * @param delayTime Process the event after 'delayTime' milliseconds. 129 * @param priority Priority of the event queue for this event. 130 * @return Returns true if event has been sent successfully. 131 */ 132 inline bool SendEvent( 133 uint32_t innerEventId, int64_t delayTime = 0, AppExecFwk::EventQueue::Priority priority = Priority::LOW) 134 { 135 return SendEvent(AppExecFwk::InnerEvent::Get(innerEventId, 0), delayTime, priority); 136 } 137 138 /** 139 * Send an event. 140 * 141 * @param innerEventId The id of the event. 142 * @param priority Priority of the event queue for this event. 143 * @return Returns true if event has been sent successfully. 144 */ SendEvent(uint32_t innerEventId,Priority priority)145 inline bool SendEvent(uint32_t innerEventId, Priority priority) 146 { 147 return SendEvent(AppExecFwk::InnerEvent::Get(innerEventId, 0), 0, priority); 148 } 149 150 /** 151 * Send an event. 152 * 153 * @param innerEventId The id of the event. 154 * @param object Shared pointer of object. 155 * @param delayTime Process the event after 'delayTime' milliseconds. 156 * @return Returns true if event has been sent successfully. 157 */ 158 template<typename T> 159 inline bool SendEvent(uint32_t innerEventId, const std::shared_ptr<T> &object, int64_t delayTime = 0) 160 { 161 return SendEvent(AppExecFwk::InnerEvent::Get(innerEventId, object), delayTime); 162 } 163 164 /** 165 * Send an event. 166 * 167 * @param innerEventId The id of the event. 168 * @param object Weak pointer of object. 169 * @param delayTime Process the event after 'delayTime' milliseconds. 170 * @return Returns true if event has been sent successfully. 171 */ 172 template<typename T> 173 inline bool SendEvent(uint32_t innerEventId, const std::weak_ptr<T> &object, int64_t delayTime = 0) 174 { 175 return SendEvent(AppExecFwk::InnerEvent::Get(innerEventId, object), delayTime); 176 } 177 178 /** 179 * Send an event. 180 * 181 * @param innerEventId The id of the event. 182 * @param object Unique pointer of object. 183 * @param delayTime Process the event after 'delayTime' milliseconds. 184 * @return Returns true if event has been sent successfully. 185 */ 186 template<typename T, typename D> 187 inline bool SendEvent(uint32_t innerEventId, std::unique_ptr<T, D> &object, int64_t delayTime = 0) 188 { 189 return SendEvent(AppExecFwk::InnerEvent::Get(innerEventId, object), delayTime); 190 } 191 192 /** 193 * Send an event. 194 * 195 * @param innerEventId The id of the event. 196 * @param object Unique pointer of object. 197 * @param delayTime Process the event after 'delayTime' milliseconds. 198 * @return Returns true if event has been sent successfully. 199 */ 200 template<typename T, typename D> 201 inline bool SendEvent(uint32_t innerEventId, std::unique_ptr<T, D> &&object, int64_t delayTime = 0) 202 { 203 return SendEvent(AppExecFwk::InnerEvent::Get(innerEventId, object), delayTime); 204 } 205 206 /** 207 * Send an immediate event. 208 * 209 * @param event Event which should be handled. 210 * @return Returns true if event has been sent successfully. 211 */ SendImmediateEvent(AppExecFwk::InnerEvent::Pointer & event)212 inline bool SendImmediateEvent(AppExecFwk::InnerEvent::Pointer &event) 213 { 214 return SendEvent(event, 0, Priority::IMMEDIATE); 215 } 216 217 /** 218 * Remove sent events. 219 * 220 * @param innerEventId The id of the event. 221 */ 222 void RemoveEvent(uint32_t innerEventId); 223 224 /** 225 * Check whether an event with the given ID can be found among the events that have been sent but not processed. 226 * 227 * @param innerEventId The id of the event. 228 */ 229 bool HasInnerEvent(uint32_t innerEventId); 230 231 /** 232 * Remove all sent events. 233 */ 234 void RemoveAllEvents(); 235 236 /** 237 * Send an event ,the event should execute now. 238 * 239 * @param event Event which should be handled. 240 * @return Returns true if event has been sent successfully. 241 */ 242 bool SendExecuteNowEvent(AppExecFwk::InnerEvent::Pointer &event); 243 244 /** 245 * Send an event. 246 * 247 * @param handler The instance of the EventHandler. 248 * @param ParamTypes params for send event. 249 * @return Returns true if event has been sent successfully. 250 */ 251 template<typename... ParamTypes> SendTelEvent(std::shared_ptr<AppExecFwk::EventHandler> handler,ParamTypes &&..._args)252 static bool SendTelEvent(std::shared_ptr<AppExecFwk::EventHandler> handler, ParamTypes &&... _args) 253 { 254 if (handler == nullptr) { 255 TELEPHONY_LOGE("handler is nullptr"); 256 return false; 257 } 258 if (handler->GetEventRunner() == nullptr && handler.get() != nullptr) { 259 return static_cast<TelEventHandler *>(handler.get())->SendEvent(std::forward<ParamTypes>(_args)...); 260 } 261 return handler->SendEvent(std::forward<ParamTypes>(_args)...); 262 } 263 264 private: 265 void ClearFfrt(bool isNeedEnd); 266 std::shared_ptr<TelEventQueue> queue_ = nullptr; 267 }; 268 } // namespace Telephony 269 } // namespace OHOS 270 #endif // TELEPHONY_EVENT_HANDLER_H