1 /*
2  * Copyright (c) 2022 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 #include "input_manager.h"
17 
18 #include <event_runner.h>
19 
20 #include "transient_task_log.h"
21 
22 namespace OHOS {
23 namespace BackgroundTaskMgr {
24 
InputManager(const std::shared_ptr<AppExecFwk::EventRunner> & runner)25 InputManager::InputManager(const std::shared_ptr<AppExecFwk::EventRunner>& runner)
26 {
27     if (runner != nullptr) {
28         SetEventRunner(runner);
29     }
30 }
31 
~InputManager()32 InputManager::~InputManager() {}
33 
RegisterEventHub()34 void InputManager::RegisterEventHub()
35 {
36     eventHub_ = EventHub::RegisterEvent(*this);
37     if (eventHub_ == nullptr) {
38         BGTASK_LOGE("Failed to register event");
39     }
40 }
41 
RegisterEventListener(const std::shared_ptr<IEventListener> & listener)42 void InputManager::RegisterEventListener(const std::shared_ptr<IEventListener>& listener)
43 {
44     if (listener == nullptr) {
45         return;
46     }
47 
48     std::lock_guard<std::mutex> lock(listenerLock_);
49     listenerList_.push_back(listener);
50 }
51 
SendEventInfo(const std::shared_ptr<EventInfo> & eventInfo)52 void InputManager::SendEventInfo(const std::shared_ptr<EventInfo>& eventInfo)
53 {
54     if (eventInfo == nullptr) {
55         return;
56     }
57 
58     BGTASK_LOGD("Send event info: %{public}s", eventInfo->ToString().c_str());
59     SendImmediateEvent(eventInfo->GetEventId(), eventInfo);
60 }
61 
UnRegisterEventListener(const std::shared_ptr<IEventListener> & listener)62 void InputManager::UnRegisterEventListener(const std::shared_ptr<IEventListener>& listener)
63 {
64     if (listener == nullptr) {
65         return;
66     }
67 
68     std::lock_guard<std::mutex> lock(listenerLock_);
69     auto findIt = find(listenerList_.begin(), listenerList_.end(), listener);
70     if (findIt == listenerList_.end()) {
71         BGTASK_LOGE("Listener not found");
72         return;
73     }
74     listenerList_.erase(findIt);
75 }
76 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)77 void InputManager::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
78 {
79     if (event == nullptr) {
80         return;
81     }
82     int32_t eventId = event->GetInnerEventId();
83     BGTASK_LOGD("Process event, eventId: %{public}d", eventId);
84     const std::shared_ptr<EventInfo>& eventInfo = event->GetSharedObject<EventInfo>();
85     if (eventInfo == nullptr) {
86         BGTASK_LOGE("Invailed event info, eventId: %{public}d", eventId);
87         return;
88     }
89     std::lock_guard<std::mutex> lock(listenerLock_);
90     for (const auto& listener : listenerList_) {
91         listener->OnInputEvent(*eventInfo);
92     }
93 }
94 }  // namespace BackgroundTaskMgr
95 }  // namespace OHOS