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 "interaction_manager_impl.h"
17
18 #include "devicestatus_define.h"
19 #include "devicestatus_func_callback.h"
20 #include "drag_data.h"
21 #include "drag_manager_impl.h"
22
23 namespace OHOS {
24 namespace Msdp {
25 namespace DeviceStatus {
26 namespace {
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "InteractionManagerImpl" };
28 } // namespace
29
InteractionManagerImpl()30 InteractionManagerImpl::InteractionManagerImpl() {}
~InteractionManagerImpl()31 InteractionManagerImpl::~InteractionManagerImpl()
32 {
33 CALL_DEBUG_ENTER;
34 }
35
InitClient()36 bool InteractionManagerImpl::InitClient()
37 {
38 CALL_DEBUG_ENTER;
39 if (client_ != nullptr && client_->CheckValidFd()) {
40 return true;
41 }
42 client_ = std::make_shared<Client>();
43 InitMsgHandler();
44 if (!(client_->Start())) {
45 client_.reset();
46 client_ = nullptr;
47 FI_HILOGE("The client fails to start");
48 return false;
49 }
50 return true;
51 }
52
InitMsgHandler()53 void InteractionManagerImpl::InitMsgHandler()
54 {
55 CALL_DEBUG_ENTER;
56 Client::MsgCallback funs[] = {
57 #ifdef OHOS_BUILD_ENABLE_COORDINATION
58 {MessageId::COORDINATION_ADD_LISTENER,
59 MsgCallbackBind2(&CoordinationManagerImpl::OnCoordinationListener, &coordinationManagerImpl_)},
60 {MessageId::COORDINATION_MESSAGE,
61 MsgCallbackBind2(&CoordinationManagerImpl::OnCoordinationMessage, &coordinationManagerImpl_)},
62 {MessageId::COORDINATION_GET_STATE,
63 MsgCallbackBind2(&CoordinationManagerImpl::OnCoordinationState, &coordinationManagerImpl_)},
64 #endif // OHOS_BUILD_ENABLE_COORDINATION
65 {MessageId::DRAG_NOTIFY_RESULT,
66 MsgCallbackBind2(&DragManagerImpl::OnNotifyResult, &dragManagerImpl_)},
67 {MessageId::DRAG_STATE_LISTENER,
68 MsgCallbackBind2(&DragManagerImpl::OnStateChangedMessage, &dragManagerImpl_)}
69 };
70 CHKPV(client_);
71 for (auto &it : funs) {
72 if (!client_->RegisterEvent(it)) {
73 FI_HILOGI("RegistER event handler msg:%{publid}d already exists", it.id);
74 }
75 }
76 }
77
RegisterCoordinationListener(std::shared_ptr<ICoordinationListener> listener)78 int32_t InteractionManagerImpl::RegisterCoordinationListener(std::shared_ptr<ICoordinationListener> listener)
79 {
80 CALL_DEBUG_ENTER;
81 #ifdef OHOS_BUILD_ENABLE_COORDINATION
82 std::lock_guard<std::mutex> guard(mutex_);
83 if (!InitClient()) {
84 FI_HILOGE("Get client is nullptr");
85 return RET_ERR;
86 }
87 return coordinationManagerImpl_.RegisterCoordinationListener(listener);
88 #else
89 FI_HILOGW("Coordination does not support");
90 (void)(listener);
91 return ERROR_UNSUPPORT;
92 #endif // OHOS_BUILD_ENABLE_COORDINATION
93 }
94
UnregisterCoordinationListener(std::shared_ptr<ICoordinationListener> listener)95 int32_t InteractionManagerImpl::UnregisterCoordinationListener(std::shared_ptr<ICoordinationListener> listener)
96 {
97 CALL_DEBUG_ENTER;
98 #ifdef OHOS_BUILD_ENABLE_COORDINATION
99 std::lock_guard<std::mutex> guard(mutex_);
100 return coordinationManagerImpl_.UnregisterCoordinationListener(listener);
101 #else
102 FI_HILOGW("Coordination does not support");
103 (void)(listener);
104 return ERROR_UNSUPPORT;
105 #endif // OHOS_BUILD_ENABLE_COORDINATION
106 }
107
PrepareCoordination(std::function<void (std::string,CoordinationMessage)> callback)108 int32_t InteractionManagerImpl::PrepareCoordination(std::function<void(std::string, CoordinationMessage)> callback)
109 {
110 CALL_DEBUG_ENTER;
111 #ifdef OHOS_BUILD_ENABLE_COORDINATION
112 std::lock_guard<std::mutex> guard(mutex_);
113 if (!InitClient()) {
114 FI_HILOGE("Get client is nullptr");
115 return RET_ERR;
116 }
117 return coordinationManagerImpl_.PrepareCoordination(callback);
118 #else
119 FI_HILOGW("Coordination does not support");
120 (void)(callback);
121 return ERROR_UNSUPPORT;
122 #endif // OHOS_BUILD_ENABLE_COORDINATION
123 }
124
UnprepareCoordination(std::function<void (std::string,CoordinationMessage)> callback)125 int32_t InteractionManagerImpl::UnprepareCoordination(std::function<void(std::string, CoordinationMessage)> callback)
126 {
127 CALL_DEBUG_ENTER;
128 #ifdef OHOS_BUILD_ENABLE_COORDINATION
129 std::lock_guard<std::mutex> guard(mutex_);
130 if (!InitClient()) {
131 FI_HILOGE("Get client is nullptr");
132 return RET_ERR;
133 }
134 return coordinationManagerImpl_.UnprepareCoordination(callback);
135 #else
136 FI_HILOGW("Coordination does not support");
137 (void)(callback);
138 return ERROR_UNSUPPORT;
139 #endif // OHOS_BUILD_ENABLE_COORDINATION
140 }
141
ActivateCoordination(const std::string & remoteNetworkId,int32_t startDeviceId,std::function<void (std::string,CoordinationMessage)> callback)142 int32_t InteractionManagerImpl::ActivateCoordination(const std::string &remoteNetworkId, int32_t startDeviceId,
143 std::function<void(std::string, CoordinationMessage)> callback)
144 {
145 CALL_DEBUG_ENTER;
146 #ifdef OHOS_BUILD_ENABLE_COORDINATION
147 std::lock_guard<std::mutex> guard(mutex_);
148 if (!InitClient()) {
149 FI_HILOGE("Get client is nullptr");
150 return RET_ERR;
151 }
152 return coordinationManagerImpl_.ActivateCoordination(remoteNetworkId, startDeviceId, callback);
153 #else
154 FI_HILOGW("Coordination does not support");
155 (void)(remoteNetworkId);
156 (void)(startDeviceId);
157 (void)(callback);
158 return ERROR_UNSUPPORT;
159 #endif // OHOS_BUILD_ENABLE_COORDINATION
160 }
161
DeactivateCoordination(bool isUnchained,std::function<void (std::string,CoordinationMessage)> callback)162 int32_t InteractionManagerImpl::DeactivateCoordination(bool isUnchained,
163 std::function<void(std::string, CoordinationMessage)> callback)
164 {
165 CALL_DEBUG_ENTER;
166 #ifdef OHOS_BUILD_ENABLE_COORDINATION
167 std::lock_guard<std::mutex> guard(mutex_);
168 if (!InitClient()) {
169 FI_HILOGE("Get client is nullptr");
170 return RET_ERR;
171 }
172 return coordinationManagerImpl_.DeactivateCoordination(isUnchained, callback);
173 #else
174 FI_HILOGW("Coordination does not support");
175 (void)(callback);
176 return ERROR_UNSUPPORT;
177 #endif // OHOS_BUILD_ENABLE_COORDINATION
178 }
179
GetCoordinationState(const std::string & deviceId,std::function<void (bool)> callback)180 int32_t InteractionManagerImpl::GetCoordinationState(
181 const std::string &deviceId, std::function<void(bool)> callback)
182 {
183 CALL_DEBUG_ENTER;
184 #ifdef OHOS_BUILD_ENABLE_COORDINATION
185 std::lock_guard<std::mutex> guard(mutex_);
186 if (!InitClient()) {
187 FI_HILOGE("Get client is nullptr");
188 return RET_ERR;
189 }
190 return coordinationManagerImpl_.GetCoordinationState(deviceId, callback);
191 #else
192 (void)(deviceId);
193 (void)(callback);
194 FI_HILOGW("Coordination does not support");
195 return ERROR_UNSUPPORT;
196 #endif // OHOS_BUILD_ENABLE_COORDINATION
197 }
198
UpdateDragStyle(DragCursorStyle style)199 int32_t InteractionManagerImpl::UpdateDragStyle(DragCursorStyle style)
200 {
201 CALL_DEBUG_ENTER;
202 return dragManagerImpl_.UpdateDragStyle(style);
203 }
204
StartDrag(const DragData & dragData,std::function<void (const DragNotifyMsg &)> callback)205 int32_t InteractionManagerImpl::StartDrag(const DragData &dragData, std::function<void(const DragNotifyMsg&)> callback)
206 {
207 CALL_DEBUG_ENTER;
208 std::lock_guard<std::mutex> guard(mutex_);
209 if (!InitClient()) {
210 FI_HILOGE("Get client is nullptr");
211 return RET_ERR;
212 }
213 return dragManagerImpl_.StartDrag(dragData, callback);
214 }
215
StopDrag(DragResult result,bool hasCustomAnimation)216 int32_t InteractionManagerImpl::StopDrag(DragResult result, bool hasCustomAnimation)
217 {
218 CALL_DEBUG_ENTER;
219 return dragManagerImpl_.StopDrag(result, hasCustomAnimation);
220 }
221
GetDragTargetPid()222 int32_t InteractionManagerImpl::GetDragTargetPid()
223 {
224 CALL_DEBUG_ENTER;
225 return dragManagerImpl_.GetDragTargetPid();
226 }
227
GetUdKey(std::string & udKey)228 int32_t InteractionManagerImpl::GetUdKey(std::string &udKey)
229 {
230 CALL_DEBUG_ENTER;
231 return dragManagerImpl_.GetUdKey(udKey);
232 }
233
AddDraglistener(DragListenerPtr listener)234 int32_t InteractionManagerImpl::AddDraglistener(DragListenerPtr listener)
235 {
236 CALL_DEBUG_ENTER;
237 std::lock_guard<std::mutex> guard(mutex_);
238 if (!InitClient()) {
239 FI_HILOGE("Get client is nullptr");
240 return RET_ERR;
241 }
242 return dragManagerImpl_.AddDraglistener(listener);
243 }
244
RemoveDraglistener(DragListenerPtr listener)245 int32_t InteractionManagerImpl::RemoveDraglistener(DragListenerPtr listener)
246 {
247 CALL_DEBUG_ENTER;
248 return dragManagerImpl_.RemoveDraglistener(listener);
249 }
250
SetDragWindowVisible(bool visible)251 int32_t InteractionManagerImpl::SetDragWindowVisible(bool visible)
252 {
253 CALL_DEBUG_ENTER;
254 return dragManagerImpl_.SetDragWindowVisible(visible);
255 }
256
GetShadowOffset(int32_t & offsetX,int32_t & offsetY,int32_t & width,int32_t & height)257 int32_t InteractionManagerImpl::GetShadowOffset(int32_t& offsetX, int32_t& offsetY, int32_t& width, int32_t& height)
258 {
259 CALL_DEBUG_ENTER;
260 return dragManagerImpl_.GetShadowOffset(offsetX, offsetY, width, height);
261 }
262
UpdateShadowPic(const ShadowInfo & shadowInfo)263 int32_t InteractionManagerImpl::UpdateShadowPic(const ShadowInfo &shadowInfo)
264 {
265 CALL_DEBUG_ENTER;
266 return dragManagerImpl_.UpdateShadowPic(shadowInfo);
267 }
268 } // namespace DeviceStatus
269 } // namespace Msdp
270 } // namespace OHOS
271