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 #ifndef LOG_TAG
16 #define LOG_TAG "CallbackHandlerInner"
17 #endif
18 
19 #include "callback_handler.h"
20 #include "event_handler.h"
21 #include "event_runner.h"
22 #include "audio_service_log.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
26 using namespace std;
27 class CallbackHandlerInner : public CallbackHandler, public AppExecFwk::EventHandler {
28 public:
29     explicit CallbackHandlerInner(std::shared_ptr<IHandler> iHandler, const std::string &handlerName);
30     ~CallbackHandlerInner();
31 
32     void SendCallbackEvent(uint32_t eventCode, int64_t data) override;
33     void SendCallbackEvent(uint32_t eventCode, int64_t data, int64_t delayTime) override;
34 
35     void ReleaseEventRunner() override;
36 
37 protected:
38     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
39 
40 private:
41     std::weak_ptr<IHandler> iHandler_;
42 };
43 
GetInstance(std::shared_ptr<IHandler> iHandler,const std::string & handlerName)44 std::shared_ptr<CallbackHandler> CallbackHandler::GetInstance(std::shared_ptr<IHandler> iHandler,
45     const std::string &handlerName)
46 {
47     return std::make_shared<CallbackHandlerInner>(iHandler, handlerName);
48 }
49 
CallbackHandlerInner(std::shared_ptr<IHandler> iHandler,const std::string & handlerName)50 CallbackHandlerInner::CallbackHandlerInner(std::shared_ptr<IHandler> iHandler, const std::string &handlerName)
51     : AppExecFwk::EventHandler(AppExecFwk::EventRunner::Create(handlerName))
52 {
53     iHandler_ = iHandler;
54 }
55 
~CallbackHandlerInner()56 CallbackHandlerInner::~CallbackHandlerInner()
57 {
58     AUDIO_WARNING_LOG("Destructor callback handler inner");
59 }
60 
SendCallbackEvent(uint32_t eventCode,int64_t data)61 void CallbackHandlerInner::SendCallbackEvent(uint32_t eventCode, int64_t data)
62 {
63     SendEvent(AppExecFwk::InnerEvent::Get(eventCode, data));
64 }
65 
SendCallbackEvent(uint32_t eventCode,int64_t data,int64_t delayTime)66 void CallbackHandlerInner::SendCallbackEvent(uint32_t eventCode, int64_t data, int64_t delayTime)
67 {
68     SendEvent(AppExecFwk::InnerEvent::Get(eventCode, data), delayTime, Priority::LOW);
69 }
70 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)71 void CallbackHandlerInner::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
72 {
73     uint32_t eventCode = event->GetInnerEventId();
74     int64_t data = event->GetParam();
75     std::shared_ptr<IHandler> handler = iHandler_.lock();
76     if (handler == nullptr) {
77         AUDIO_ERR_LOG("iHandler is nullptr");
78         return;
79     }
80     handler->OnHandle(eventCode, data);
81 }
82 
ReleaseEventRunner()83 void CallbackHandlerInner::ReleaseEventRunner()
84 {
85     SetEventRunner(nullptr);
86 }
87 } // namespace AudioStandard
88 } // namespace OHOS
89