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 #include "event_hub.h"
17
18 #include <map>
19
20 #include "want.h"
21
22 #include "drag_manager.h"
23 #include "fi_log.h"
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "EventHub" };
29 static std::map<std::string, EventId> g_actionMap = {
30 { EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON, EventId::EVENT_SCREEN_ON },
31 { EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF, EventId::EVENT_SCREEN_OFF },
32 { EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED, EventId::EVENT_SCREEN_LOCK },
33 { EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED, EventId::EVENT_SCREEN_UNLOCK },
34 { EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_LOW, EventId::EVENT_BATTERY_LOW },
35 { EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_OKAY, EventId::EVENT_BATTERY_OKAY },
36 };
37
GetEventHub(IContext * context)38 std::shared_ptr<EventHub> EventHub::GetEventHub(IContext* context)
39 {
40 CALL_DEBUG_ENTER;
41 auto skill = std::make_shared<EventFwk::MatchingSkills>();
42 for (auto &actionPair : g_actionMap) {
43 skill->AddEvent(actionPair.first);
44 }
45 auto info = std::make_shared<EventFwk::CommonEventSubscribeInfo>(*skill);
46 return std::make_shared<EventHub>(*info, context);
47 }
48
RegisterEvent(std::shared_ptr<EventHub> eventHub)49 void EventHub::RegisterEvent(std::shared_ptr<EventHub> eventHub)
50 {
51 CALL_DEBUG_ENTER;
52 bool result = EventFwk::CommonEventManager::SubscribeCommonEvent(eventHub);
53 if (!result) {
54 FI_HILOGE("Failed to subscribe common event");
55 }
56 }
57
UnRegisterEvent(std::shared_ptr<EventHub> eventHub)58 void EventHub::UnRegisterEvent(std::shared_ptr<EventHub> eventHub)
59 {
60 CALL_DEBUG_ENTER;
61 bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(eventHub);
62 if (!result) {
63 FI_HILOGE("Failed to unSubscribe common event");
64 }
65 }
66
OnReceiveEvent(const EventFwk::CommonEventData & event)67 void EventHub::OnReceiveEvent(const EventFwk::CommonEventData &event)
68 {
69 const auto want = event.GetWant();
70 const auto action = want.GetAction();
71 if (g_actionMap.find(action) == g_actionMap.end()) {
72 return;
73 }
74 EventId eventId = g_actionMap[action];
75 FI_HILOGD("Receive action:%{public}s, eventId:%{public}d", action.c_str(), static_cast<int32_t>(eventId));
76 if (eventId != EventId::EVENT_SCREEN_LOCK) {
77 return;
78 }
79 CHKPV(context_);
80 auto fun = [] (IContext* context) -> int32_t {
81 if (context->GetDragManager().GetDragState() == DragState::START) {
82 DragDropResult dropResult { DragResult::DRAG_CANCEL, false, -1 };
83 context->GetDragManager().StopDrag(dropResult);
84 }
85 return RET_OK;
86 };
87 int32_t ret = context_->GetDelegateTasks().PostAsyncTask(std::bind(fun, context_));
88 if (ret != RET_OK) {
89 FI_HILOGE("Post async task failed");
90 }
91 }
92
DragAbilityStatusChange(std::shared_ptr<EventHub> eventHub)93 DragAbilityStatusChange::DragAbilityStatusChange(std::shared_ptr<EventHub> eventHub)
94 : eventHub_(eventHub)
95 {}
96
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)97 void DragAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
98 {
99 FI_HILOGI("OnAddSystemAbility,systemAbilityId:%{public}d", systemAbilityId);
100 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
101 FI_HILOGE("systemAbilityId is not COMMON_EVENT_SERVICE_ID");
102 return;
103 }
104 if (eventHub_ == nullptr) {
105 FI_HILOGE("OnAddSystemAbility eventHub_ is nullptr");
106 return;
107 }
108 EventHub::RegisterEvent(eventHub_);
109 }
110
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)111 void DragAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
112 {
113 FI_HILOGI("OnRemoveSystemAbility,systemAbilityId:%{public}d", systemAbilityId);
114 return;
115 }
116 } // namespace DeviceStatus
117 } // namespace Msdp
118 } // namespace OHOS