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