• 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     { EventFwk::CommonEventSupport::COMMON_EVENT_LOCALE_CHANGED, EventId::EVENT_LOCALE_CHANGED },
39 };
40 
GetEventHub(IContext * context)41 std::shared_ptr<EventHub> EventHub::GetEventHub(IContext* context)
42 {
43     CALL_DEBUG_ENTER;
44     auto skill = std::make_shared<EventFwk::MatchingSkills>();
45     for (auto &actionPair : g_actionMap) {
46         skill->AddEvent(actionPair.first);
47     }
48     auto info = std::make_shared<EventFwk::CommonEventSubscribeInfo>(*skill);
49     return std::make_shared<EventHub>(*info, context);
50 }
51 
RegisterEvent(std::shared_ptr<EventHub> eventHub)52 void EventHub::RegisterEvent(std::shared_ptr<EventHub> eventHub)
53 {
54     CALL_DEBUG_ENTER;
55     bool result = EventFwk::CommonEventManager::SubscribeCommonEvent(eventHub);
56     if (!result) {
57         FI_HILOGE("Failed to subscribe common event");
58     }
59 }
60 
UnRegisterEvent(std::shared_ptr<EventHub> eventHub)61 void EventHub::UnRegisterEvent(std::shared_ptr<EventHub> eventHub)
62 {
63     CALL_DEBUG_ENTER;
64     bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(eventHub);
65     if (!result) {
66         FI_HILOGE("Failed to unSubscribe common event");
67     }
68 }
69 
OnReceiveEvent(const EventFwk::CommonEventData & event)70 void EventHub::OnReceiveEvent(const EventFwk::CommonEventData &event)
71 {
72     const auto want = event.GetWant();
73     const auto action = want.GetAction();
74     if (g_actionMap.find(action) == g_actionMap.end()) {
75         return;
76     }
77     EventId eventId = g_actionMap[action];
78     FI_HILOGD("Receive action:%{public}s, eventId:%{public}d", action.c_str(), static_cast<int32_t>(eventId));
79 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
80     if (eventId == EventId::EVENT_LOCALE_CHANGED) {
81         CHKPV(context_);
82         int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this] {
83             CHKPR(this->context_, RET_ERR);
84             if (auto dragState = this->context_->GetDragManager().GetDragState();
85                 dragState == DragState::START || dragState == DragState::MOTION_DRAGGING) {
86                 this->context_->GetDragManager().UpdateDragStylePositon();
87             }
88             return RET_OK;
89         });
90         if (ret != RET_OK) {
91             FI_HILOGE("Post async task failed");
92         }
93     }
94 #endif // OHOS_BUILD_ENABLE_ARKUI_X
95     if (eventId != EventId::EVENT_SCREEN_LOCK) {
96         return;
97     }
98     CHKPV(context_);
99     int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this] {
100         CHKPR(this->context_, RET_ERR);
101         if (this->context_->GetDragManager().GetDragState() == DragState::START) {
102 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
103             this->context_->GetDragManager().SimulatePullCancelEvent();
104 #else
105             DragDropResult dropResult { DragResult::DRAG_CANCEL, false, -1 };
106             this->context_->GetDragManager().StopDrag(dropResult);
107 #endif // OHOS_BUILD_ENABLE_ARKUI_X
108         }
109         return RET_OK;
110     });
111     if (ret != RET_OK) {
112         FI_HILOGE("Post async task failed");
113     }
114 }
115 
DragAbilityStatusChange(std::shared_ptr<EventHub> eventHub)116 DragAbilityStatusChange::DragAbilityStatusChange(std::shared_ptr<EventHub> eventHub)
117     : eventHub_(eventHub)
118 {}
119 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)120 void DragAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
121 {
122     FI_HILOGI("OnAddSystemAbility,systemAbilityId:%{public}d", systemAbilityId);
123     if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
124         FI_HILOGE("systemAbilityId is not COMMON_EVENT_SERVICE_ID");
125         return;
126     }
127     if (eventHub_ == nullptr) {
128         FI_HILOGE("OnAddSystemAbility eventHub_ is nullptr");
129         return;
130     }
131     EventHub::RegisterEvent(eventHub_);
132 }
133 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)134 void DragAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
135 {
136     FI_HILOGI("OnRemoveSystemAbility,systemAbilityId:%{public}d", systemAbilityId);
137     return;
138 }
139 } // namespace DeviceStatus
140 } // namespace Msdp
141 } // namespace OHOS