• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this] {
83         CHKPR(this->context_, RET_ERR);
84         if (this->context_->GetDragManager().GetDragState() == DragState::START) {
85             DragDropResult dropResult { DragResult::DRAG_CANCEL, false, -1 };
86             this->context_->GetDragManager().StopDrag(dropResult);
87         }
88         return RET_OK;
89     });
90     if (ret != RET_OK) {
91         FI_HILOGE("Post async task failed");
92     }
93 }
94 
DragAbilityStatusChange(std::shared_ptr<EventHub> eventHub)95 DragAbilityStatusChange::DragAbilityStatusChange(std::shared_ptr<EventHub> eventHub)
96     : eventHub_(eventHub)
97 {}
98 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)99 void DragAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
100 {
101     FI_HILOGI("OnAddSystemAbility,systemAbilityId:%{public}d", systemAbilityId);
102     if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
103         FI_HILOGE("systemAbilityId is not COMMON_EVENT_SERVICE_ID");
104         return;
105     }
106     if (eventHub_ == nullptr) {
107         FI_HILOGE("OnAddSystemAbility eventHub_ is nullptr");
108         return;
109     }
110     EventHub::RegisterEvent(eventHub_);
111 }
112 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)113 void DragAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
114 {
115     FI_HILOGI("OnRemoveSystemAbility,systemAbilityId:%{public}d", systemAbilityId);
116     return;
117 }
118 } // namespace DeviceStatus
119 } // namespace Msdp
120 } // namespace OHOS