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 "event_hub.h"
17
18 #include <map>
19
20 #include <common_event_subscribe_info.h>
21 #include <common_event_manager.h>
22 #include <common_event_support.h>
23
24 #include "transient_task_log.h"
25 #include "input_manager.h"
26 #include "want.h"
27
28 namespace OHOS {
29 namespace BackgroundTaskMgr {
30 static std::map<std::string, EventId> g_actionMap = {
31 { EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON, EVENT_SCREEN_ON },
32 { EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF, EVENT_SCREEN_OFF },
33 { EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED, EVENT_SCREEN_UNLOCK },
34 { EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_LOW, EVENT_BATTERY_LOW },
35 { EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_OKAY, EVENT_BATTERY_OKAY },
36 };
37
RegisterEvent(InputManager & inputManager)38 std::shared_ptr<EventHub> EventHub::RegisterEvent(InputManager& inputManager)
39 {
40 auto skill = std::make_shared<EventFwk::MatchingSkills>();
41 for (auto &actionPair : g_actionMap) {
42 skill->AddEvent(actionPair.first);
43 }
44 auto info = std::make_shared<EventFwk::CommonEventSubscribeInfo>(*skill);
45 auto hub = std::make_shared<EventHub>(*info, inputManager);
46 bool result = EventFwk::CommonEventManager::SubscribeCommonEvent(hub);
47 if (!result) {
48 BGTASK_LOGW("Failed to subscribe common event");
49 }
50 return hub;
51 }
52
OnReceiveEvent(const EventFwk::CommonEventData & event)53 void EventHub::OnReceiveEvent(const EventFwk::CommonEventData &event)
54 {
55 std::vector<int32_t> intArgs;
56 std::vector<std::string> stringArgs;
57 intArgs.push_back(event.GetCode());
58 stringArgs.push_back(event.GetData());
59
60 const auto want = event.GetWant();
61 const auto action = want.GetAction();
62 BGTASK_LOGD("Receive action: %{public}s, eventId: %{public}d", action.c_str(), g_actionMap[action]);
63 auto eventInfoEx = std::make_shared<EventInfo>();
64 eventInfoEx->SetIntArgs(intArgs);
65 eventInfoEx->SetStringArgs(stringArgs);
66 eventInfoEx->SetEventId(g_actionMap[action]);
67 inputManager_.SendEventInfo(eventInfoEx);
68 }
69 } // namespace BackgroundTaskMgr
70 } // namespace OHOS