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