1 /*
2 * Copyright (c) 2023-2025 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 "drag_client.h"
17
18 #include "devicestatus_define.h"
19 #include "devicestatus_proto.h"
20 #include "intention_client.h"
21
22 #undef LOG_TAG
23 #define LOG_TAG "DragClient"
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28
StartDrag(const DragData & dragData,std::shared_ptr<IStartDragListener> listener)29 int32_t DragClient::StartDrag(const DragData &dragData, std::shared_ptr<IStartDragListener> listener)
30 {
31 CALL_DEBUG_ENTER;
32 CHKPR(listener, RET_ERR);
33 if (dragData.shadowInfos.empty()) {
34 FI_HILOGE("shadowInfos is empty");
35 return ERR_INVALID_VALUE;
36 }
37 for (const auto& shadowInfo : dragData.shadowInfos) {
38 CHKPR(shadowInfo.pixelMap, RET_ERR);
39 if ((shadowInfo.x > 0) || (shadowInfo.y > 0) ||
40 (shadowInfo.x < -shadowInfo.pixelMap->GetWidth()) ||
41 (shadowInfo.y < -shadowInfo.pixelMap->GetHeight())) {
42 FI_HILOGE("Invalid parameter, shadowInfox:%{private}d, shadowInfoy:%{private}d",
43 shadowInfo.x, shadowInfo.y);
44 return RET_ERR;
45 }
46 }
47 if ((dragData.dragNum <= 0) || (dragData.buffer.size() > MAX_BUFFER_SIZE) ||
48 (dragData.displayX < 0) || (dragData.displayY < 0)) {
49 FI_HILOGE("Start drag, invalid argument, dragNum:%{public}d, bufferSize:%{public}zu, "
50 "displayX:%{private}d, displayY:%{private}d",
51 dragData.dragNum, dragData.buffer.size(), dragData.displayX, dragData.displayY);
52 return RET_ERR;
53 }
54 {
55 std::lock_guard<std::mutex> guard(mtx_);
56 startDragListener_ = listener;
57 }
58 int32_t ret = INTENTION_CLIENT->StartDrag(dragData);
59 if (ret != RET_OK) {
60 FI_HILOGE("StartDrag fail");
61 }
62 return ret;
63 }
64
GetDragState(DragState & dragState)65 int32_t DragClient::GetDragState(DragState &dragState)
66 {
67 CALL_DEBUG_ENTER;
68 int32_t ret = INTENTION_CLIENT->GetDragState(dragState);
69 if (ret != RET_OK) {
70 FI_HILOGE("GetDragState fail");
71 }
72 return ret;
73 }
74
StopDrag(const DragDropResult & dropResult)75 int32_t DragClient::StopDrag(const DragDropResult &dropResult)
76 {
77 CALL_DEBUG_ENTER;
78 int32_t ret = INTENTION_CLIENT->StopDrag(dropResult);
79 if (ret != RET_OK) {
80 FI_HILOGE("StopDrag fail");
81 }
82 return ret;
83 }
84
EnableInternalDropAnimation(const std::string & animationInfo)85 int32_t DragClient::EnableInternalDropAnimation(const std::string &animationInfo)
86 {
87 CALL_DEBUG_ENTER;
88 int32_t ret = INTENTION_CLIENT->EnableInternalDropAnimation(animationInfo);
89 if (ret != RET_OK) {
90 FI_HILOGE("EnableInternalDropAnimation fail, ret:%{public}d", ret);
91 }
92 return ret;
93 }
94
UpdateDragStyle(DragCursorStyle style,int32_t eventId)95 int32_t DragClient::UpdateDragStyle(DragCursorStyle style, int32_t eventId)
96 {
97 CALL_DEBUG_ENTER;
98 if ((style < DragCursorStyle::DEFAULT) || (style > DragCursorStyle::MOVE)) {
99 FI_HILOGE("Invalid style:%{public}d", static_cast<int32_t>(style));
100 return RET_ERR;
101 }
102 int32_t ret = INTENTION_CLIENT->UpdateDragStyle(style, eventId);
103 if (ret != RET_OK) {
104 FI_HILOGE("UpdateDragStyle fail");
105 }
106 return ret;
107 }
108
GetDragTargetPid()109 int32_t DragClient::GetDragTargetPid()
110 {
111 CALL_DEBUG_ENTER;
112 int32_t targetPid = -1;
113 int32_t ret = INTENTION_CLIENT->GetDragTargetPid(targetPid);
114 if (ret != RET_OK) {
115 FI_HILOGE("GetDragTargetPid fail");
116 }
117 return targetPid;
118 }
119
GetUdKey(std::string & udKey)120 int32_t DragClient::GetUdKey(std::string &udKey)
121 {
122 CALL_DEBUG_ENTER;
123 int32_t ret = INTENTION_CLIENT->GetUdKey(udKey);
124 if (ret != RET_OK) {
125 FI_HILOGE("GetUdKey fail");
126 }
127 return ret;
128 }
129
AddDraglistener(DragListenerPtr listener,bool isJsCaller)130 int32_t DragClient::AddDraglistener(DragListenerPtr listener, bool isJsCaller)
131 {
132 CALL_DEBUG_ENTER;
133 CHKPR(listener, RET_ERR);
134 std::lock_guard<std::mutex> guard(mtx_);
135 if (dragListeners_.find(listener) != dragListeners_.end()) {
136 return RET_OK;
137 }
138 if (!hasRegistered_) {
139 FI_HILOGI("Start drag listening");
140 int32_t ret = INTENTION_CLIENT->AddDraglistener(isJsCaller);
141 if (ret != RET_OK) {
142 FI_HILOGE("AddDraglistener fail");
143 return ret;
144 }
145 hasRegistered_ = true;
146 }
147 dragListeners_.insert(listener);
148 return RET_OK;
149 }
150
RemoveDraglistener(DragListenerPtr listener,bool isJsCaller)151 int32_t DragClient::RemoveDraglistener(DragListenerPtr listener, bool isJsCaller)
152 {
153 CALL_DEBUG_ENTER;
154 std::lock_guard<std::mutex> guard(mtx_);
155 if (listener == nullptr) {
156 dragListeners_.clear();
157 } else {
158 dragListeners_.erase(listener);
159 }
160 if (hasRegistered_ && dragListeners_.empty()) {
161 hasRegistered_ = false;
162 FI_HILOGI("Stop drag listening");
163 int32_t ret = INTENTION_CLIENT->RemoveDraglistener(isJsCaller);
164 if (ret != RET_OK) {
165 FI_HILOGE("RemoveDraglistener fail");
166 return ret;
167 }
168 }
169 return RET_OK;
170 }
AddSubscriptListener(SubscriptListenerPtr listener)171 int32_t DragClient::AddSubscriptListener(SubscriptListenerPtr listener)
172 {
173 CALL_DEBUG_ENTER;
174 CHKPR(listener, RET_ERR);
175 std::lock_guard<std::mutex> guard(mtx_);
176 if (subscriptListeners_.find(listener) != subscriptListeners_.end()) {
177 return RET_OK;
178 }
179 if (!hasSubscriptRegistered_) {
180 FI_HILOGI("Start subscript listening");
181 int32_t ret = INTENTION_CLIENT->AddSubscriptListener();
182 if (ret != RET_OK) {
183 FI_HILOGE("AddSubscriptListener fail");
184 return ret;
185 }
186 hasSubscriptRegistered_ = true;
187 }
188 subscriptListeners_.insert(listener);
189 return RET_OK;
190 }
191
RemoveSubscriptListener(SubscriptListenerPtr listener)192 int32_t DragClient::RemoveSubscriptListener(SubscriptListenerPtr listener)
193 {
194 CALL_DEBUG_ENTER;
195 std::lock_guard<std::mutex> guard(mtx_);
196 if (listener == nullptr) {
197 subscriptListeners_.clear();
198 } else {
199 subscriptListeners_.erase(listener);
200 }
201 if (hasSubscriptRegistered_ && subscriptListeners_.empty()) {
202 hasSubscriptRegistered_ = false;
203 FI_HILOGI("Stop subscript listening");
204 int32_t ret = INTENTION_CLIENT->RemoveSubscriptListener();
205 if (ret != RET_OK) {
206 FI_HILOGE("RemoveSubscriptListener fail");
207 return ret;
208 }
209 }
210 return RET_OK;
211 }
212
SetDragWindowVisible(bool visible,bool isForce,const std::shared_ptr<Rosen::RSTransaction> & rsTransaction)213 int32_t DragClient::SetDragWindowVisible(bool visible, bool isForce,
214 const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
215 {
216 CALL_DEBUG_ENTER;
217 int32_t ret = INTENTION_CLIENT->SetDragWindowVisible(visible, isForce, rsTransaction);
218 if (ret != RET_OK) {
219 FI_HILOGE("SetDragWindowVisible fail");
220 }
221 return ret;
222 }
223
GetShadowOffset(ShadowOffset & shadowOffset)224 int32_t DragClient::GetShadowOffset(ShadowOffset &shadowOffset)
225 {
226 CALL_DEBUG_ENTER;
227 int32_t ret = INTENTION_CLIENT->GetShadowOffset(shadowOffset);
228 if (ret != RET_OK) {
229 FI_HILOGE("GetShadowOffset fail");
230 }
231 return ret;
232 }
233
UpdateShadowPic(const ShadowInfo & shadowInfo)234 int32_t DragClient::UpdateShadowPic(const ShadowInfo &shadowInfo)
235 {
236 CALL_DEBUG_ENTER;
237 CHKPR(shadowInfo.pixelMap, RET_ERR);
238 if ((shadowInfo.x > 0) || (shadowInfo.y > 0) ||
239 (shadowInfo.x < -shadowInfo.pixelMap->GetWidth()) ||
240 (shadowInfo.y < -shadowInfo.pixelMap->GetHeight())) {
241 FI_HILOGE("Invalid parameter, shadowInfox:%{private}d, shadowInfoy:%{private}d",
242 shadowInfo.x, shadowInfo.y);
243 return RET_ERR;
244 }
245 int32_t ret = INTENTION_CLIENT->UpdateShadowPic(shadowInfo);
246 if (ret != RET_OK) {
247 FI_HILOGE("UpdateShadowPic fail");
248 }
249 return ret;
250 }
251
GetDragData(DragData & dragData)252 int32_t DragClient::GetDragData(DragData &dragData)
253 {
254 CALL_DEBUG_ENTER;
255 int32_t ret = INTENTION_CLIENT->GetDragData(dragData);
256 if (ret != RET_OK) {
257 FI_HILOGE("GetDragData fail");
258 }
259 return ret;
260 }
261
UpdatePreviewStyle(const PreviewStyle & previewStyle)262 int32_t DragClient::UpdatePreviewStyle(const PreviewStyle &previewStyle)
263 {
264 CALL_DEBUG_ENTER;
265 int32_t ret = INTENTION_CLIENT->UpdatePreviewStyle(previewStyle);
266 if (ret != RET_OK) {
267 FI_HILOGE("UpdatePreviewStyle fail");
268 }
269 return ret;
270 }
271
UpdatePreviewStyleWithAnimation(const PreviewStyle & previewStyle,const PreviewAnimation & animation)272 int32_t DragClient::UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle, const PreviewAnimation &animation)
273 {
274 CALL_DEBUG_ENTER;
275 int32_t ret = INTENTION_CLIENT->UpdatePreviewStyleWithAnimation(previewStyle, animation);
276 if (ret != RET_OK) {
277 FI_HILOGE("UpdatePreviewStyleWithAnimation fail");
278 }
279 return ret;
280 }
281
RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction> & rsTransaction)282 int32_t DragClient::RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
283 {
284 CALL_DEBUG_ENTER;
285 int32_t ret = INTENTION_CLIENT->RotateDragWindowSync(rsTransaction);
286 if (ret != RET_OK) {
287 FI_HILOGE("RotateDragWindowSync fail");
288 }
289 return ret;
290 }
291
GetDragSummary(std::map<std::string,int64_t> & summarys,bool isJsCaller)292 int32_t DragClient::GetDragSummary(std::map<std::string, int64_t> &summarys, bool isJsCaller)
293 {
294 CALL_DEBUG_ENTER;
295 int32_t ret = INTENTION_CLIENT->GetDragSummary(summarys, isJsCaller);
296 if (ret != RET_OK) {
297 FI_HILOGE("GetDragSummary fail");
298 }
299 return ret;
300 }
301
SetDragSwitchState(bool enable,bool isJsCaller)302 int32_t DragClient::SetDragSwitchState(bool enable, bool isJsCaller)
303 {
304 CALL_DEBUG_ENTER;
305 int32_t ret = INTENTION_CLIENT->SetDragSwitchState(enable, isJsCaller);
306 if (ret != RET_OK) {
307 FI_HILOGE("SetDragSwitchState fail");
308 }
309 return ret;
310 }
311
SetAppDragSwitchState(bool enable,const std::string & pkgName,bool isJsCaller)312 int32_t DragClient::SetAppDragSwitchState(bool enable, const std::string &pkgName, bool isJsCaller)
313 {
314 CALL_DEBUG_ENTER;
315 int32_t ret = INTENTION_CLIENT->SetAppDragSwitchState(enable, pkgName, isJsCaller);
316 if (ret != RET_OK) {
317 FI_HILOGE("SetAppDragSwitchState fail");
318 }
319 return ret;
320 }
321
GetDragAction(DragAction & dragAction)322 int32_t DragClient::GetDragAction(DragAction &dragAction)
323 {
324 CALL_DEBUG_ENTER;
325 int32_t ret = INTENTION_CLIENT->GetDragAction(dragAction);
326 if (ret != RET_OK) {
327 FI_HILOGE("GetDragAction fail");
328 }
329 return ret;
330 }
331
GetExtraInfo(std::string & extraInfo)332 int32_t DragClient::GetExtraInfo(std::string &extraInfo)
333 {
334 CALL_DEBUG_ENTER;
335 int32_t ret = INTENTION_CLIENT->GetExtraInfo(extraInfo);
336 if (ret != RET_OK) {
337 FI_HILOGE("GetExtraInfo fail");
338 }
339 return ret;
340 }
341
AddPrivilege()342 int32_t DragClient::AddPrivilege()
343 {
344 CALL_DEBUG_ENTER;
345 int32_t ret = INTENTION_CLIENT->AddPrivilege();
346 if (ret != RET_OK) {
347 FI_HILOGE("AddPrivilege fail");
348 }
349 return ret;
350 }
351
EraseMouseIcon()352 int32_t DragClient::EraseMouseIcon()
353 {
354 CALL_DEBUG_ENTER;
355 int32_t ret = INTENTION_CLIENT->EraseMouseIcon();
356 if (ret != RET_OK) {
357 FI_HILOGE("EraseMouseIcon fail");
358 }
359 return ret;
360 }
361
SetDragWindowScreenId(uint64_t displayId,uint64_t screenId)362 int32_t DragClient::SetDragWindowScreenId(uint64_t displayId, uint64_t screenId)
363 {
364 CALL_DEBUG_ENTER;
365 int32_t ret = INTENTION_CLIENT->SetDragWindowScreenId(displayId, screenId);
366 if (ret != RET_OK) {
367 FI_HILOGE("SetDragWindowScreenId fail");
368 }
369 return ret;
370 }
371
SetMouseDragMonitorState(bool state)372 int32_t DragClient::SetMouseDragMonitorState(bool state)
373 {
374 CALL_DEBUG_ENTER;
375 int32_t ret = INTENTION_CLIENT->SetMouseDragMonitorState(state);
376 if (ret != RET_OK) {
377 FI_HILOGE("SetMouseDragMonitorState fail");
378 }
379 return ret;
380 }
381
SetDraggableState(bool state)382 int32_t DragClient::SetDraggableState(bool state)
383 {
384 CALL_DEBUG_ENTER;
385 int32_t ret = INTENTION_CLIENT->SetDraggableState(state);
386 if (ret != RET_OK) {
387 FI_HILOGE("SetDraggableState fail");
388 }
389 return ret;
390 }
391
GetAppDragSwitchState(bool & state)392 int32_t DragClient::GetAppDragSwitchState(bool &state)
393 {
394 CALL_DEBUG_ENTER;
395 int32_t ret = INTENTION_CLIENT->GetAppDragSwitchState(state);
396 if (ret != RET_OK) {
397 FI_HILOGE("GetAppDragSwitchState fail");
398 }
399 return ret;
400 }
401
EnableUpperCenterMode(bool enable)402 int32_t DragClient::EnableUpperCenterMode(bool enable)
403 {
404 CALL_DEBUG_ENTER;
405 int32_t ret = INTENTION_CLIENT->EnableUpperCenterMode(enable);
406 if (ret != RET_OK) {
407 FI_HILOGE("EnableUpperCenterMode fail");
408 }
409 return ret;
410 }
411
SetDraggableStateAsync(bool state,int64_t downTime)412 void DragClient::SetDraggableStateAsync(bool state, int64_t downTime)
413 {
414 CALL_DEBUG_ENTER;
415 INTENTION_CLIENT->SetDraggableStateAsync(state, downTime);
416 }
417
GetDragBundleInfo(DragBundleInfo & dragBundleInfo)418 int32_t DragClient::GetDragBundleInfo(DragBundleInfo &dragBundleInfo)
419 {
420 CALL_DEBUG_ENTER;
421 int32_t ret = INTENTION_CLIENT->GetDragBundleInfo(dragBundleInfo);
422 if (ret != RET_OK) {
423 FI_HILOGE("GetDragBundleInfo fail");
424 }
425 return ret;
426 }
427
OnNotifyResult(const StreamClient & client,NetPacket & pkt)428 int32_t DragClient::OnNotifyResult(const StreamClient &client, NetPacket &pkt)
429 {
430 CALL_DEBUG_ENTER;
431 DragNotifyMsg notifyMsg;
432 int32_t result = 0;
433 int32_t dragBehavior = -1;
434 pkt >> notifyMsg.displayX >> notifyMsg.displayY >> result >> notifyMsg.targetPid >> dragBehavior;
435 if (pkt.ChkRWError()) {
436 FI_HILOGE("Packet read drag msg failed");
437 return RET_ERR;
438 }
439 if ((result < static_cast<int32_t>(DragResult::DRAG_SUCCESS)) ||
440 (result > static_cast<int32_t>(DragResult::DRAG_EXCEPTION))) {
441 FI_HILOGE("Invalid result:%{public}d", result);
442 return RET_ERR;
443 }
444 notifyMsg.result = static_cast<DragResult>(result);
445 if ((dragBehavior < static_cast<int32_t>(DragBehavior::UNKNOWN)) ||
446 (dragBehavior > static_cast<int32_t>(DragBehavior::MOVE))) {
447 FI_HILOGE("Invalid dragBehavior:%{public}d", dragBehavior);
448 return RET_ERR;
449 }
450 notifyMsg.dragBehavior = static_cast<DragBehavior>(dragBehavior);
451 std::lock_guard<std::mutex> guard(mtx_);
452 CHKPR(startDragListener_, RET_ERR);
453 startDragListener_->OnDragEndMessage(notifyMsg);
454 return RET_OK;
455 }
456
OnStateChangedMessage(const StreamClient & client,NetPacket & pkt)457 int32_t DragClient::OnStateChangedMessage(const StreamClient &client, NetPacket &pkt)
458 {
459 CALL_DEBUG_ENTER;
460 int32_t state = 0;
461 pkt >> state;
462 if (pkt.ChkRWError()) {
463 FI_HILOGE("Packet read drag msg failed");
464 return RET_ERR;
465 }
466 std::lock_guard<std::mutex> guard(mtx_);
467 for (const auto &listener : dragListeners_) {
468 listener->OnDragMessage(static_cast<DragState>(state));
469 }
470 return RET_OK;
471 }
472
OnNotifyHideIcon(const StreamClient & client,NetPacket & pkt)473 int32_t DragClient::OnNotifyHideIcon(const StreamClient &client, NetPacket &pkt)
474 {
475 CALL_DEBUG_ENTER;
476 std::lock_guard<std::mutex> guard(mtx_);
477 CHKPR(startDragListener_, RET_ERR);
478 startDragListener_->OnHideIconMessage();
479 return RET_OK;
480 }
481
OnDragStyleChangedMessage(const StreamClient & client,NetPacket & pkt)482 int32_t DragClient::OnDragStyleChangedMessage(const StreamClient &client, NetPacket &pkt)
483 {
484 CALL_DEBUG_ENTER;
485 int32_t style = 0;
486 pkt >> style;
487 if (pkt.ChkRWError()) {
488 FI_HILOGE("Packet read drag msg failed");
489 return RET_ERR;
490 }
491 std::lock_guard<std::mutex> guard(mtx_);
492 for (const auto &listener : subscriptListeners_) {
493 listener->OnMessage(static_cast<DragCursorStyle>(style));
494 }
495 return RET_OK;
496 }
497
OnConnected()498 void DragClient::OnConnected()
499 {
500 CALL_INFO_TRACE;
501 if (connectedDragListeners_.empty()) {
502 FI_HILOGE("The connect drag listener set is empty");
503 return;
504 }
505 for (const auto &listener : connectedDragListeners_) {
506 if (AddDraglistener(listener) != RET_OK) {
507 FI_HILOGW("AddDraglistener failed");
508 }
509 }
510 connectedDragListeners_.clear();
511 }
512
OnDisconnected()513 void DragClient::OnDisconnected()
514 {
515 CALL_INFO_TRACE;
516 std::lock_guard<std::mutex> guard(mtx_);
517 if (dragListeners_.empty()) {
518 FI_HILOGE("The drag listener set is empty");
519 return;
520 }
521 connectedDragListeners_ = dragListeners_;
522 dragListeners_.clear();
523 hasRegistered_ = false;
524 }
525
IsDragStart()526 bool DragClient::IsDragStart()
527 {
528 CALL_DEBUG_ENTER;
529 bool isStart = false;
530 int32_t ret = INTENTION_CLIENT->IsDragStart(isStart);
531 if (ret != RET_OK) {
532 FI_HILOGE("IsDragStart fail, ret = %{public}d", ret);
533 isStart = false;
534 }
535 return isStart;
536 }
537
GetDragSummaryInfo(DragSummaryInfo & dragSummaryInfo)538 int32_t DragClient::GetDragSummaryInfo(DragSummaryInfo &dragSummaryInfo)
539 {
540 CALL_DEBUG_ENTER;
541 int32_t ret = INTENTION_CLIENT->GetDragSummaryInfo(dragSummaryInfo);
542 if (ret != RET_OK) {
543 FI_HILOGE("GetDragSummaryInfo fail, ret = %{public}d", ret);
544 }
545 return ret;
546 }
547 } // namespace DeviceStatus
548 } // namespace Msdp
549 } // namespace OHOS
550