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_manager.h"
17
18 #include <atomic>
19
20 #include "display_manager.h"
21 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
22 #include "extra_data.h"
23 #ifdef MSDP_HIVIEWDFX_HITRACE_ENABLE
24 #include "hitrace_meter.h"
25 #endif // MSDP_HIVIEWDFX_HITRACE_ENABLE
26 #endif // OHOS_BUILD_ENABLE_ARKUI_X
27 #include "pixel_map.h"
28 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
29 #ifdef MSDP_FRAMEWORK_UDMF_ENABLED
30 #include "udmf_client.h"
31 #endif // MSDP_FRAMEWORK_UDMF_ENABLED
32 #include "unified_types.h"
33 #include "window_manager_lite.h"
34 #endif // OHOS_BUILD_ENABLE_ARKUI_X
35
36 #include "devicestatus_define.h"
37 #include "drag_data.h"
38 #include "drag_data_manager.h"
39 #include "drag_hisysevent.h"
40 #include "fi_log.h"
41 #include "devicestatus_proto.h"
42 #include "utility.h"
43
44 #undef LOG_TAG
45 #define LOG_TAG "DragManager"
46 namespace OHOS {
47 namespace Msdp {
48 namespace DeviceStatus {
49 namespace {
50 constexpr int32_t TIMEOUT_MS { 3000 };
51 constexpr int32_t INTERVAL_MS { 500 };
52 constexpr int32_t POWER_SQUARED { 2 };
53 constexpr int32_t TEN_POWER { 10 * 10 };
54 std::atomic<int64_t> g_startFilterTime { -1 };
55 const std::string DRAG_STYLE_DEFAULT {"DEFAULT"};
56 const std::string DRAG_STYLE_FORBIDDEN {"FORBIDDEN"};
57 const std::string DRAG_STYLE_COPY {"COPY"};
58 const std::string DRAG_STYLE_MOVE {"MOVE"};
59 const std::string DRAG_STYLE_UNKNOW {"UNKNOW"};
60 const std::string DRAG_BEHAVIOR {"DRAG_BEHAVIOR"};
61 const std::string ORG_PKG_NAME {"device_status"};
62 const std::string APP_VERSION_ID {"1.0.0"};
63 const std::string DRAG_FRAMEWORK {"DRAG_FRAMEWORK"};
64 const std::string START_CROSSING_DRAG {"START_CROSSING_DRAG"};
65 const std::string END_CROSSING_DRAG {"END_CROSSING_DRAG"};
66 #ifdef OHOS_DRAG_ENABLE_INTERCEPTOR
67 constexpr int32_t DRAG_PRIORITY { 500 };
68 #endif // OHOS_DRAG_ENABLE_INTERCEPTOR
69 } // namespace
70
71 #ifdef OHOS_BUILD_ENABLE_ARKUI_X
GetInstance()72 DragManager &DragManager::GetInstance()
73 {
74 static DragManager instance;
75 return instance;
76 }
77 #endif // OHOS_BUILD_ENABLE_ARKUI_X
78
~DragManager()79 DragManager::~DragManager()
80 {
81 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
82 EventHub::UnRegisterEvent(eventHub_);
83 #endif // OHOS_BUILD_ENABLE_ARKUI_X
84 }
85
86 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
Init(IContext * context)87 int32_t DragManager::Init(IContext* context)
88 {
89 FI_HILOGI("enter");
90 CHKPR(context, RET_ERR);
91 context_ = context;
92 int32_t repeatCount = 1;
93 context_->GetTimerManager().AddTimer(INTERVAL_MS, repeatCount, [this]() {
94 if (eventHub_ == nullptr) {
95 eventHub_ = EventHub::GetEventHub(context_);
96 }
97 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
98 if (samgrProxy == nullptr) {
99 FI_HILOGE("samgrProxy is nullptr");
100 return;
101 }
102 statusListener_ = new (std::nothrow) DragAbilityStatusChange(eventHub_);
103 if (statusListener_ == nullptr) {
104 FI_HILOGE("statusListener_ is nullptr");
105 return;
106 }
107 int32_t ret = samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, statusListener_);
108 FI_HILOGI("SubscribeSystemAbility COMMON_EVENT_SERVICE_ID result:%{public}d", ret);
109 displayAbilityStatusChange_ = new (std::nothrow) DisplayAbilityStatusChange(context_);
110 if (displayAbilityStatusChange_ == nullptr) {
111 FI_HILOGE("displayAbilityStatusChange is nullptr");
112 return;
113 }
114 ret = samgrProxy->SubscribeSystemAbility(DISPLAY_MANAGER_SERVICE_SA_ID, displayAbilityStatusChange_);
115 FI_HILOGI("SubscribeSystemAbility DISPLAY_MANAGER_SERVICE_SA_ID result:%{public}d", ret);
116 appStateObserverStatusChange_ = new (std::nothrow) AppStateObserverStatusChange(context_);
117 if (appStateObserverStatusChange_ == nullptr) {
118 FI_HILOGE("appStateObserverStatusChange_ is nullptr");
119 return;
120 }
121 ret = samgrProxy->SubscribeSystemAbility(APP_MGR_SERVICE_ID, appStateObserverStatusChange_);
122 FI_HILOGI("SubscribeSystemAbility APP_MGR_SERVICE_ID result:%{public}d", ret);
123 CollaborationServiceStatusChange_ = new (std::nothrow) CollaborationServiceStatusChange(context_);
124 if (CollaborationServiceStatusChange_ == nullptr) {
125 FI_HILOGE("CollaborationServiceStatusChange_ is nullptr");
126 return;
127 }
128 ret = samgrProxy->SubscribeSystemAbility(DEVICE_COLLABORATION_SA_ID, CollaborationServiceStatusChange_);
129 FI_HILOGI("SubscribeSystemAbility DEVICE_COLLABORATION_SA_ID result:%{public}d", ret);
130 });
131 FI_HILOGI("leave");
132 return RET_OK;
133 }
134
OnSessionLost(SocketSessionPtr session)135 void DragManager::OnSessionLost(SocketSessionPtr session)
136 {
137 CHKPV(session);
138 RemoveListener(session->GetPid());
139 }
140
AddListener(int32_t pid)141 int32_t DragManager::AddListener(int32_t pid)
142 {
143 FI_HILOGI("enter");
144 CHKPR(context_, RET_ERR);
145 auto session = context_->GetSocketSessionManager().FindSessionByPid(pid);
146 CHKPR(session, RET_ERR);
147 auto info = std::make_shared<StateChangeNotify::MessageInfo>();
148 info->session = session;
149 info->msgId = MessageId::DRAG_STATE_LISTENER;
150 info->msgType = MessageType::NOTIFY_STATE;
151 stateNotify_.AddNotifyMsg(info);
152 context_->GetSocketSessionManager().AddSessionDeletedCallback(pid,
153 [this](SocketSessionPtr session) { this->OnSessionLost(session); });
154 FI_HILOGI("leave");
155 return RET_OK;
156 }
157
RemoveListener(int32_t pid)158 int32_t DragManager::RemoveListener(int32_t pid)
159 {
160 FI_HILOGI("Remove listener, pid:%{public}d", pid);
161 CHKPR(context_, RET_ERR);
162 auto session = context_->GetSocketSessionManager().FindSessionByPid(pid);
163 CHKPR(session, RET_ERR);
164 auto info = std::make_shared<StateChangeNotify::MessageInfo>();
165 info->session = session;
166 info->msgType = MessageType::NOTIFY_STATE;
167 stateNotify_.RemoveNotifyMsg(info);
168 FI_HILOGI("leave");
169 return RET_OK;
170 }
171
AddSubscriptListener(int32_t pid)172 int32_t DragManager::AddSubscriptListener(int32_t pid)
173 {
174 FI_HILOGI("enter");
175 CHKPR(context_, RET_ERR);
176 auto session = context_->GetSocketSessionManager().FindSessionByPid(pid);
177 CHKPR(session, RET_ERR);
178 auto info = std::make_shared<StateChangeNotify::MessageInfo>();
179 info->session = session;
180 info->msgId = MessageId::DRAG_STYLE_LISTENER;
181 info->msgType = MessageType::NOTIFY_STYLE;
182 stateNotify_.AddNotifyMsg(info);
183 FI_HILOGI("leave");
184 return RET_OK;
185 }
186
RemoveSubscriptListener(int32_t pid)187 int32_t DragManager::RemoveSubscriptListener(int32_t pid)
188 {
189 FI_HILOGI("enter");
190 CHKPR(context_, RET_ERR);
191 auto session = context_->GetSocketSessionManager().FindSessionByPid(pid);
192 CHKPR(session, RET_ERR);
193 auto info = std::make_shared<StateChangeNotify::MessageInfo>();
194 info->msgType = MessageType::NOTIFY_STYLE;
195 info->session = session;
196 stateNotify_.RemoveNotifyMsg(info);
197 FI_HILOGI("leave");
198 return RET_OK;
199 }
200 #endif // OHOS_BUILD_ENABLE_ARKUI_X
201
PrintDragData(const DragData & dragData,const std::string & packageName)202 void DragManager::PrintDragData(const DragData &dragData, const std::string &packageName)
203 {
204 FI_HILOGI("enter");
205 for (const auto& shadowInfo : dragData.shadowInfos) {
206 CHKPV(shadowInfo.pixelMap);
207 FI_HILOGI("PixelFormat:%{public}d, PixelAlphaType:%{public}d, PixelAllocatorType:%{public}d,"
208 " PixelWidth:%{public}d, PixelHeight:%{public}d, shadowX:%{public}d, shadowY:%{public}d",
209 static_cast<int32_t>(shadowInfo.pixelMap->GetPixelFormat()),
210 static_cast<int32_t>(shadowInfo.pixelMap->GetAlphaType()),
211 static_cast<int32_t>(shadowInfo.pixelMap->GetAllocatorType()),
212 shadowInfo.pixelMap->GetWidth(), shadowInfo.pixelMap->GetHeight(), shadowInfo.x, shadowInfo.y);
213 }
214 std::string summarys;
215 for (const auto &[udKey, recordSize] : dragData.summarys) {
216 std::string str = udKey + "-" + std::to_string(recordSize) + ";";
217 summarys += str;
218 }
219 FI_HILOGI("SourceType:%{public}d, pointerId:%{public}d, displayId:%{public}d,"
220 " displayX:%{private}d, displayY:%{private}d, dragNum:%{public}d,"
221 " hasCanceledAnimation:%{public}d, udKey:%{public}s, hasCoordinateCorrected:%{public}d, summarys:%{public}s,"
222 " packageName:%{public}s", dragData.sourceType, dragData.pointerId, dragData.displayId, dragData.displayX,
223 dragData.displayY, dragData.dragNum, dragData.hasCanceledAnimation, GetAnonyString(dragData.udKey).c_str(),
224 dragData.hasCoordinateCorrected, summarys.c_str(), packageName.c_str());
225 }
226
227 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
ResetMouseDragMonitorTimerId(const DragData & dragData)228 void DragManager::ResetMouseDragMonitorTimerId(const DragData &dragData)
229 {
230 if ((context_ != nullptr) && (mouseDragMonitorTimerId_ >= 0) &&
231 (dragData.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE)) {
232 context_->GetTimerManager().RemoveTimer(mouseDragMonitorTimerId_);
233 mouseDragMonitorTimerId_ = -1;
234 }
235 }
236
GetPackageName(int32_t pid)237 std::string DragManager::GetPackageName(int32_t pid)
238 {
239 CHKPS(context_);
240 std::string packageName = std::string();
241 if (pid == -1) {
242 packageName = "Cross-device drag";
243 } else {
244 context_->GetSocketSessionManager().AddSessionDeletedCallback(pid,
245 [this](SocketSessionPtr session) { this->OnSessionLost(session); });
246 dragOutSession_ = context_->GetSocketSessionManager().FindSessionByPid(pid);
247 if (dragOutSession_ != nullptr) {
248 packageName = dragOutSession_->GetProgramName();
249 }
250 }
251 return packageName;
252 }
253
IsCrossDragging()254 bool DragManager::IsCrossDragging()
255 {
256 return isCrossDragging_;
257 }
258
GetDragRadarPackageName(int32_t pid,const std::string & packageName,const std::string & appCaller)259 DragRadarPackageName DragManager::GetDragRadarPackageName(int32_t pid, const std::string &packageName,
260 const std::string &appCaller)
261 {
262 FI_HILOGI("enter");
263 DragRadarPackageName dragRadarPackageName;
264 dragRadarPackageName.packageName = packageName;
265 if (pid == -1) {
266 dragRadarPackageName.appCaller = appCaller;
267 } else {
268 dragRadarPackageName.appCaller = packageName;
269 }
270 FI_HILOGI("leave");
271 return dragRadarPackageName;
272 }
273
StartDrag(const DragData & dragData,int32_t pid,const std::string & peerNetId,bool isLongPressDrag,const std::string & appCaller)274 int32_t DragManager::StartDrag(
275 const DragData &dragData, int32_t pid, const std::string &peerNetId, bool isLongPressDrag,
276 const std::string &appCaller)
277 {
278 FI_HILOGI("enter");
279 ResetMouseDragMonitorTimerId(dragData);
280 if (pid == -1) {
281 isCrossDragging_ = true;
282 } else {
283 isCrossDragging_ = false;
284 }
285 if (dragState_ == DragState::START || dragState_ == DragState::MOTION_DRAGGING) {
286 FI_HILOGE("Drag instance already exists, no need to start drag again");
287 return RET_ERR;
288 }
289 peerNetId_ = peerNetId;
290 std::string packageName = GetPackageName(pid);
291 DragRadarPackageName dragRadarPackageName = GetDragRadarPackageName(pid, packageName, appCaller);
292 ReportStartDragRadarInfo(BizState::STATE_BEGIN, StageRes::RES_IDLE, DragRadarErrCode::DRAG_SUCCESS, peerNetId,
293 dragRadarPackageName);
294 PrintDragData(dragData, packageName);
295 if (InitDataManager(dragData, dragRadarPackageName.appCaller) != RET_OK) {
296 FI_HILOGE("Failed to init data manager");
297 ResetMouseDragMonitorInfo();
298 ReportStartDragFailedRadarInfo(StageRes::RES_FAIL, DragRadarErrCode::INVALID_DRAG_DATA, __func__,
299 dragRadarPackageName);
300 return RET_ERR;
301 }
302 isLongPressDrag_ = isLongPressDrag;
303 if (OnStartDrag(dragRadarPackageName, pid) != RET_OK) {
304 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
305 DragDFX::WriteStartDrag(dragState_, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
306 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
307 FI_HILOGE("Failed to execute OnStartDrag");
308 ResetMouseDragMonitorInfo();
309 return RET_ERR;
310 }
311 if (notifyPUllUpCallback_ != nullptr) {
312 notifyPUllUpCallback_(false);
313 }
314 SetDragState(DragState::START);
315 dragDrawing_.OnStartDragExt();
316 stateNotify_.StateChangedNotify(DragState::START);
317 StateChangedNotify(DragState::START);
318 ReportStartDragRadarInfo(BizState::STATE_IDLE, StageRes::RES_SUCCESS, DragRadarErrCode::DRAG_SUCCESS, peerNetId,
319 dragRadarPackageName);
320 if (pid == -1) {
321 ReportStartDragUEInfo(packageName);
322 }
323 FI_HILOGI("leave");
324 return RET_OK;
325 }
326 #else
StartDrag(const DragData & dragData)327 int32_t DragManager::StartDrag(const DragData &dragData)
328 {
329 CALL_INFO_TRACE;
330 if (dragState_ == DragState::START) {
331 FI_HILOGE("Drag instance already exists, no need to start drag again");
332 return RET_ERR;
333 }
334 std::string packageName;
335 PrintDragData(dragData, packageName);
336
337 if (InitDataManager(dragData) != RET_OK) {
338 FI_HILOGE("Failed to init data manager");
339 return RET_ERR;
340 }
341 if (OnStartDrag() != RET_OK) {
342 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
343 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
344 DragDFX::WriteStartDrag(dragState_, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
345 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
346 #endif // OHOS_BUILD_ENABLE_ARKUI_X
347 FI_HILOGE("Failed to execute OnStartDrag");
348 return RET_ERR;
349 }
350 SetDragState(DragState::START);
351 return RET_OK;
352 }
353
UpdatePointerAction(std::shared_ptr<MMI::PointerEvent> pointerEvent)354 int32_t DragManager::UpdatePointerAction(std::shared_ptr<MMI::PointerEvent> pointerEvent)
355 {
356 CALL_INFO_TRACE;
357 CHKPR(pointerEvent, RET_ERR);
358 if (dragState_ != DragState::START || dragState_ == DragState::MOTION_DRAGGING) {
359 FI_HILOGE("ARKUI_X DragState not started");
360 return RET_ERR;
361 }
362
363 int32_t action = pointerEvent->GetPointerAction();
364 switch (action) {
365 case MMI::PointerEvent::POINTER_ACTION_MOVE: {
366 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_MOVE);
367 FI_HILOGD("ARKUI_X UpdatePointAction to POINTER_ACTION_PULL_MOVE");
368 return OnDragMove(pointerEvent);
369 }
370 case MMI::PointerEvent::POINTER_ACTION_BUTTON_UP:
371 case MMI::PointerEvent::POINTER_ACTION_UP: {
372 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_UP);
373 FI_HILOGD("ARKUI_X UpdatePointAction to POINTER_ACTION_PULL_UP");
374 return OnDragUp(pointerEvent);
375 }
376 default: {
377 FI_HILOGD("ARKUI_X unknown action:%{public}d", action);
378 return RET_ERR;
379 }
380 }
381 return RET_OK;
382 }
383
OnDragMove(std::shared_ptr<MMI::PointerEvent> pointerEvent)384 int32_t DragManager::OnDragMove(std::shared_ptr<MMI::PointerEvent> pointerEvent)
385 {
386 CALL_DEBUG_ENTER;
387 CHKPR(pointerEvent, RET_ERR);
388 MMI::PointerEvent::PointerItem pointerItem;
389 int32_t pointerId = pointerEvent->GetPointerId();
390 if (!pointerEvent->GetPointerItem(pointerId, pointerItem)) {
391 FI_HILOGE("ARKUI_X GetPointerItem fail");
392 return RET_ERR;
393 }
394
395 int32_t displayX = pointerItem.GetDisplayX();
396 int32_t displayY = pointerItem.GetDisplayY();
397 int32_t sourceType = pointerEvent->GetSourceType();
398 dragDrawing_.OnDragMove(pointerEvent->GetTargetDisplayId(), displayX,
399 displayY, pointerEvent->GetActionTime());
400 FI_HILOGD("ARKUI_X SourceType:%{public}d, pointerId:%{public}d, displayX:%{private}d, displayY:%{private}d",
401 sourceType, pointerId, displayX, displayY);
402
403 return RET_OK;
404 }
405 #endif // OHOS_BUILD_ENABLE_ARKUI_X
406
StopDrag(const DragDropResult & dropResult,const std::string & packageName,int32_t pid,bool isStopCooperate,const std::string & appCallee)407 int32_t DragManager::StopDrag(const DragDropResult &dropResult, const std::string &packageName, int32_t pid,
408 bool isStopCooperate, const std::string &appCallee)
409 {
410 FI_HILOGI("enter");
411 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
412 DragRadarPackageName dragRadarPackageName;
413 dragRadarPackageName.packageName = packageName;
414 if (pid == -1) {
415 dragRadarPackageName.appCallee = appCallee;
416 } else {
417 dragRadarPackageName.appCallee = packageName;
418 }
419 ReportStopDragRadarInfo(BizState::STATE_IDLE, StageRes::RES_IDLE, DragRadarErrCode::DRAG_SUCCESS, pid,
420 dragRadarPackageName);
421 std::string dragOutPkgName =
422 (dragOutSession_ == nullptr) ? "Cross-device drag" : dragOutSession_->GetProgramName();
423 FI_HILOGI("mainWindow:%{public}d, dragResult:%{public}d, drop packageName:%{public}s,"
424 "drag out packageName:%{public}s", dropResult.mainWindow, dropResult.result, packageName.c_str(),
425 dragOutPkgName.c_str());
426 if (dragState_ == DragState::STOP) {
427 FI_HILOGE("No drag instance running, can not stop drag");
428 ReportStopDragRadarInfo(BizState::STATE_END, StageRes::RES_FAIL, DragRadarErrCode::REPEATE_STOP_DRAG_EXCEPTION,
429 pid, dragRadarPackageName);
430 return RET_ERR;
431 }
432 #ifdef OHOS_DRAG_ENABLE_ANIMATION
433 dragDrawing_.NotifyDragInfo(dragOutPkgName, packageName);
434 #endif // OHOS_DRAG_ENABLE_ANIMATION
435 if ((dropResult.result != DragResult::DRAG_EXCEPTION) && (context_ != nullptr) && (timerId_ >= 0)) {
436 context_->GetTimerManager().RemoveTimer(timerId_);
437 timerId_ = -1;
438 }
439 #endif // OHOS_BUILD_ENABLE_ARKUI_X
440 int32_t ret = RET_OK;
441 if (OnStopDrag(dropResult.result, dropResult.hasCustomAnimation, packageName, pid, isStopCooperate) != RET_OK) {
442 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
443 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
444 DragDFX::WriteStopDrag(dragState_, dropResult, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
445 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
446 #endif // OHOS_BUILD_ENABLE_ARKUI_X
447 FI_HILOGE("On stop drag failed");
448 ret = RET_ERR;
449 }
450 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
451 if (dropResult.result == DragResult::DRAG_SUCCESS && dropResult.mainWindow > 0) {
452 Rosen::WMError result = Rosen::WindowManagerLite::GetInstance().RaiseWindowToTop(dropResult.mainWindow);
453 if (result != Rosen::WMError::WM_OK) {
454 FI_HILOGE("Raise window to top failed, mainWindow:%{public}d", dropResult.mainWindow);
455 }
456 }
457 stateNotify_.StateChangedNotify(DragState::STOP);
458 DragBehavior dragBehavior = dropResult.dragBehavior;
459 GetDragBehavior(dropResult, dragBehavior);
460 if (NotifyDragResult(dropResult.result, dragBehavior) != RET_OK) {
461 FI_HILOGE("Notify drag result failed");
462 ReportStopDragRadarInfo(BizState::STATE_IDLE, StageRes::RES_FAIL, DragRadarErrCode::FAILED_NOTIFY_DRAG_RESULT,
463 pid, dragRadarPackageName);
464 }
465 lastDisplayId_ = -1;
466 lastEventId_ = -1;
467 mouseDragMonitorDisplayX_ = -1;
468 mouseDragMonitorDisplayY_ = -1;
469 mouseDragMonitorState_ = false;
470 existMouseMoveDragCallback_ = false;
471 needLongPressDragAnimation_ = true;
472 isLongPressDrag_ = false;
473 currentPointerEvent_ = nullptr;
474 DRAG_DATA_MGR.ResetDragData();
475 dragResult_ = static_cast<DragResult>(dropResult.result);
476 appCallee_ = dragRadarPackageName.appCallee;
477 StateChangedNotify(DragState::STOP);
478 #else
479 DragBehavior dragBehavior = dropResult.dragBehavior;
480 GetDragBehavior(dropResult, dragBehavior);
481 DRAG_DATA_MGR.ResetDragData();
482 dragResult_ = static_cast<DragResult>(dropResult.result);
483 #endif // OHOS_BUILD_ENABLE_ARKUI_X
484 SetDragState(DragState::STOP);
485 if (GetControlCollaborationVisible()) {
486 SetControlCollaborationVisible(false);
487 }
488 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
489 ReportStopDragRadarInfo(BizState::STATE_END, StageRes::RES_SUCCESS, DragRadarErrCode::DRAG_SUCCESS, pid,
490 dragRadarPackageName);
491 if (dragOutSession_ == nullptr) {
492 ReportStopDragUEInfo(packageName);
493 }
494 dragOutSession_ = nullptr;
495 #endif // OHOS_BUILD_ENABLE_ARKUI_X
496 peerNetId_ = "";
497 FI_HILOGI("leave");
498 return ret;
499 }
500
GetDragTargetPid() const501 int32_t DragManager::GetDragTargetPid() const
502 {
503 FI_HILOGI("enter");
504 return DRAG_DATA_MGR.GetTargetPid();
505 }
506
GetUdKey(std::string & udKey) const507 int32_t DragManager::GetUdKey(std::string &udKey) const
508 {
509 FI_HILOGI("enter");
510 DragData dragData = DRAG_DATA_MGR.GetDragData();
511 if (dragData.udKey.empty()) {
512 FI_HILOGE("Target udKey is empty");
513 return RET_ERR;
514 }
515 udKey = dragData.udKey;
516 FI_HILOGI("leave");
517 return RET_OK;
518 }
519
520 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
UpdateDragStyle(DragCursorStyle style,int32_t targetPid,int32_t targetTid,int32_t eventId)521 int32_t DragManager::UpdateDragStyle(DragCursorStyle style, int32_t targetPid, int32_t targetTid, int32_t eventId)
522 #else
523 int32_t DragManager::UpdateDragStyle(DragCursorStyle style)
524 #endif // OHOS_BUILD_ENABLE_ARKUI_X
525 {
526 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
527 FI_HILOGD("DragStyle from ark is dragStyle:%{public}s, event:%{public}d",
528 GetDragStyleName(style).c_str(), eventId);
529 if ((eventId != -1) && (eventId < lastEventId_)) {
530 FI_HILOGE("Invalid eventId:%{public}d, lastEvent:%{public}d", eventId, lastEventId_);
531 return RET_ERR;
532 }
533 lastEventId_ = eventId;
534 auto lastTargetPid = DRAG_DATA_MGR.GetTargetPid();
535 DRAG_DATA_MGR.SetTargetPid(targetPid);
536 DRAG_DATA_MGR.SetTargetTid(targetTid);
537 #endif // OHOS_BUILD_ENABLE_ARKUI_X
538 if (style == DRAG_DATA_MGR.GetDragStyle()) {
539 FI_HILOGD("Not need update drag style");
540 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
541 if (targetPid != lastTargetPid) {
542 stateNotify_.StyleChangedNotify(GetRealDragStyle(style));
543 }
544 #endif // OHOS_BUILD_ENABLE_ARKUI_X
545 return RET_OK;
546 }
547 DRAG_DATA_MGR.SetDragStyle(style);
548 if (dragState_ != DragState::START) {
549 FI_HILOGE("No drag instance running, can not update drag style");
550 return RET_ERR;
551 }
552 if ((style < DragCursorStyle::DEFAULT) || (style > DragCursorStyle::MOVE)) {
553 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
554 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
555 DragDFX::WriteUpdateDragStyle(style, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
556 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
557 #endif // OHOS_BUILD_ENABLE_ARKUI_X
558 FI_HILOGE("Invalid style:%{public}d", style);
559 return RET_ERR;
560 }
561 if (OnUpdateDragStyle(style) != RET_OK) {
562 FI_HILOGE("OnUpdateDragStyle dragStyle:%{public}s failed", GetDragStyleName(style).c_str());
563 return RET_ERR;
564 }
565 return RET_OK;
566 }
567
UpdateShadowPic(const ShadowInfo & shadowInfo)568 int32_t DragManager::UpdateShadowPic(const ShadowInfo &shadowInfo)
569 {
570 FI_HILOGI("enter");
571 if (dragState_ != DragState::START) {
572 FI_HILOGE("No drag instance running, can not update shadow picture");
573 return RET_ERR;
574 }
575 DRAG_DATA_MGR.SetShadowInfos({ shadowInfo });
576 FI_HILOGI("leave");
577 return dragDrawing_.UpdateShadowPic(shadowInfo);
578 }
579
GetDragData(DragData & dragData)580 int32_t DragManager::GetDragData(DragData &dragData)
581 {
582 FI_HILOGI("enter");
583 if ((dragState_ != DragState::START) && (dragState_ != DragState::MOTION_DRAGGING)) {
584 FI_HILOGE("No drag instance running, can not get dragData");
585 return RET_ERR;
586 }
587 dragData = DRAG_DATA_MGR.GetDragData();
588 FI_HILOGI("leave");
589 return RET_OK;
590 }
591
GetDragState(DragState & dragState)592 int32_t DragManager::GetDragState(DragState &dragState)
593 {
594 FI_HILOGD("enter");
595 dragState = GetDragState();
596 if (dragState == DragState::ERROR) {
597 FI_HILOGE("dragState_ is error");
598 return RET_ERR;
599 }
600 FI_HILOGD("leave");
601 return RET_OK;
602 }
603
GetDragStyle() const604 DragCursorStyle DragManager::GetDragStyle() const
605 {
606 return DRAG_DATA_MGR.GetDragStyle();
607 }
608
609 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
NotifyDragResult(DragResult result,DragBehavior dragBehavior)610 int32_t DragManager::NotifyDragResult(DragResult result, DragBehavior dragBehavior)
611 {
612 FI_HILOGI("enter");
613 DragData dragData = DRAG_DATA_MGR.GetDragData();
614 int32_t targetPid = GetDragTargetPid();
615 NetPacket pkt(MessageId::DRAG_NOTIFY_RESULT);
616 if ((result < DragResult::DRAG_SUCCESS) || (result > DragResult::DRAG_EXCEPTION)) {
617 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
618 DragDFX::WriteNotifyDragResult(result, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
619 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
620 FI_HILOGE("The invalid result:%{public}d", static_cast<int32_t>(result));
621 return RET_ERR;
622 }
623 pkt << dragData.displayX << dragData.displayY << static_cast<int32_t>(result) << targetPid <<
624 static_cast<int32_t>(dragBehavior);
625 if (pkt.ChkRWError()) {
626 FI_HILOGE("Failed to packet write data");
627 return RET_ERR;
628 }
629 CHKPR(dragOutSession_, RET_ERR);
630 if (!dragOutSession_->SendMsg(pkt)) {
631 FI_HILOGE("Failed to send message");
632 return MSG_SEND_FAIL;
633 }
634 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
635 DragDFX::WriteNotifyDragResult(result, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR);
636 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
637 FI_HILOGI("leave");
638 return RET_OK;
639 }
640
NotifyHideIcon()641 int32_t DragManager::NotifyHideIcon()
642 {
643 FI_HILOGD("enter");
644 NetPacket pkt(MessageId::DRAG_NOTIFY_HIDE_ICON);
645 if (pkt.ChkRWError()) {
646 FI_HILOGE("Packet write data failed");
647 return RET_ERR;
648 }
649 CHKPR(dragOutSession_, RET_ERR);
650 if (!dragOutSession_->SendMsg(pkt)) {
651 FI_HILOGE("Send message failed");
652 return MSG_SEND_FAIL;
653 }
654 FI_HILOGD("leave");
655 return RET_OK;
656 }
657
DealPullInWindowEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent,int32_t targetDisplayId)658 int32_t DragManager::DealPullInWindowEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent, int32_t targetDisplayId)
659 {
660 CHKPR(pointerEvent, RET_ERR);
661 MMI::PointerEvent::PointerItem pointerItem;
662 pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem);
663 int32_t displayX = -1;
664 int32_t displayY = -1;
665 if (MMI::PointerEvent::FixedMode::ONE_HAND == pointerEvent->GetFixedMode()) {
666 displayX = pointerItem.GetFixedDisplayX();
667 displayY = pointerItem.GetFixedDisplayY();
668 } else {
669 displayX = pointerItem.GetDisplayX();
670 displayY = pointerItem.GetDisplayY();
671 }
672 dragDrawing_.DetachToDisplay(targetDisplayId);
673 bool isNeedAdjustDisplayXY = true;
674 bool isMultiSelectedAnimation = false;
675 dragDrawing_.Draw(targetDisplayId, displayX, displayY, isNeedAdjustDisplayXY, isMultiSelectedAnimation);
676 dragDrawing_.UpdateDragWindowDisplay(targetDisplayId);
677 dragDrawing_.OnDragMove(targetDisplayId, displayX, displayY, pointerEvent->GetActionTime());
678 lastDisplayId_ = targetDisplayId;
679 return RET_OK;
680 }
681
DragCallback(std::shared_ptr<MMI::PointerEvent> pointerEvent)682 void DragManager::DragCallback(std::shared_ptr<MMI::PointerEvent> pointerEvent)
683 {
684 CHKPV(pointerEvent);
685 currentPointerEvent_ = pointerEvent;
686 int32_t pointerAction = pointerEvent->GetPointerAction();
687 if ((pointerEvent->GetSourceType() == MMI::PointerEvent::SOURCE_TYPE_MOUSE) &&
688 (pointerAction == MMI::PointerEvent::POINTER_ACTION_MOVE) && mouseDragMonitorState_) {
689 MMI::PointerEvent::PointerItem pointerItem;
690 pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem);
691 mouseDragMonitorDisplayX_ = pointerItem.GetDisplayX();
692 mouseDragMonitorDisplayY_ = pointerItem.GetDisplayY();
693 existMouseMoveDragCallback_ = true;
694 }
695 if (pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_MOVE) {
696 mouseDragMonitorDisplayX_ = -1;
697 mouseDragMonitorDisplayY_ = -1;
698 OnDragMove(pointerEvent);
699 return;
700 }
701 FI_HILOGD("DragCallback, pointerAction:%{public}d", pointerAction);
702 if ((pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_UP) ||
703 (pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_CANCEL)) {
704 dragDrawing_.StopVSyncStation();
705 mouseDragMonitorDisplayX_ = -1;
706 mouseDragMonitorDisplayY_ = -1;
707 CHKPV(context_);
708 int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this, pointerEvent] {
709 return this->OnDragUp(pointerEvent);
710 });
711 if (ret != RET_OK) {
712 FI_HILOGE("Post async task failed");
713 }
714 return;
715 }
716 int32_t targetDisplayId = pointerEvent->GetTargetDisplayId();
717 if ((pointerEvent->GetSourceType() == MMI::PointerEvent::SOURCE_TYPE_MOUSE) &&
718 (pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_IN_WINDOW) && (lastDisplayId_ != targetDisplayId)) {
719 FI_HILOGI("Interface for processing extended screen access");
720 CHKPV(context_);
721 int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this, pointerEvent, targetDisplayId] {
722 return this->DealPullInWindowEvent(pointerEvent, targetDisplayId);
723 });
724 if (ret != RET_OK) {
725 FI_HILOGE("Post async task failed");
726 }
727 }
728 FI_HILOGD("Unknown action, sourceType:%{public}d, pointerId:%{public}d, pointerAction:%{public}d",
729 pointerEvent->GetSourceType(), pointerEvent->GetPointerId(), pointerAction);
730 }
731
DoLongPressDragZoomOutAnimation(int32_t displayX,int32_t displayY)732 void DragManager::DoLongPressDragZoomOutAnimation(int32_t displayX, int32_t displayY)
733 {
734 auto LongPressDragZoomOutAnimation = [displayX, displayY, this]() {
735 if (needLongPressDragAnimation_) {
736 DragData dragData = DRAG_DATA_MGR.GetDragData();
737 int32_t deltaX = abs(displayX - dragData.displayX);
738 int32_t deltaY = abs(displayY - dragData.displayY);
739 if ((pow(deltaX, POWER_SQUARED) + pow(deltaY, POWER_SQUARED)) > TEN_POWER) {
740 dragDrawing_.LongPressDragZoomOutAnimation();
741 needLongPressDragAnimation_ = false;
742 }
743 }
744 return RET_OK;
745 };
746 if (isLongPressDrag_) {
747 CHKPV(context_);
748 int32_t ret = context_->GetDelegateTasks().PostAsyncTask(LongPressDragZoomOutAnimation);
749 if (ret != RET_OK) {
750 FI_HILOGE("Post async task failed, ret:%{public}d", ret);
751 }
752 }
753 }
754
OnDragMove(std::shared_ptr<MMI::PointerEvent> pointerEvent)755 void DragManager::OnDragMove(std::shared_ptr<MMI::PointerEvent> pointerEvent)
756 {
757 CHKPV(pointerEvent);
758 MMI::PointerEvent::PointerItem pointerItem;
759 pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem);
760 int32_t pointerId = pointerEvent->GetPointerId();
761 int32_t displayX = -1;
762 int32_t displayY = -1;
763 if (MMI::PointerEvent::FixedMode::ONE_HAND == pointerEvent->GetFixedMode()) {
764 displayX = pointerItem.GetFixedDisplayX();
765 displayY = pointerItem.GetFixedDisplayY();
766 } else {
767 displayX = pointerItem.GetDisplayX();
768 displayY = pointerItem.GetDisplayY();
769 }
770 DoLongPressDragZoomOutAnimation(displayX, displayY);
771 int32_t targetDisplayId = pointerEvent->GetTargetDisplayId();
772 FI_HILOGD("SourceType:%{public}d, pointerId:%{public}d, displayX:%{private}d, displayY:%{private}d, "
773 "targetDisplayId:%{public}d, pullId:%{public}d", pointerEvent->GetSourceType(), pointerId, displayX, displayY,
774 targetDisplayId, pointerEvent->GetPullId());
775 if (lastDisplayId_ == -1) {
776 lastDisplayId_ = targetDisplayId;
777 dragDrawing_.OnDragMove(targetDisplayId, displayX, displayY, pointerEvent->GetActionTime());
778 } else if (lastDisplayId_ != targetDisplayId) {
779 dragDrawing_.DetachToDisplay(targetDisplayId);
780 bool isNeedAdjustDisplayXY = true;
781 bool isMultiSelectedAnimation = false;
782 dragDrawing_.Draw(targetDisplayId, displayX, displayY, isNeedAdjustDisplayXY, isMultiSelectedAnimation);
783 dragDrawing_.UpdateDragWindowDisplay(targetDisplayId);
784 dragDrawing_.OnDragMove(targetDisplayId, displayX, displayY, pointerEvent->GetActionTime());
785 lastDisplayId_ = targetDisplayId;
786 } else {
787 dragDrawing_.OnDragMove(targetDisplayId, displayX, displayY, pointerEvent->GetActionTime());
788 }
789 }
790
OnDragCancel(std::shared_ptr<MMI::PointerEvent> pointerEvent)791 void DragManager::OnDragCancel(std::shared_ptr<MMI::PointerEvent> pointerEvent)
792 {
793 FI_HILOGI("enter");
794 CHKPV(pointerEvent);
795 if (dragState_ != DragState::START) {
796 FI_HILOGW("No drag instance running");
797 return;
798 }
799 DragData dragData = DRAG_DATA_MGR.GetDragData();
800 if (dragData.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
801 dragDrawing_.EraseMouseIcon();
802 FI_HILOGI("Set the pointer cursor visible");
803 MMI::InputManager::GetInstance()->SetPointerVisible(true);
804 }
805 DragDropResult dropResult { DragResult::DRAG_CANCEL, false, -1 };
806 StopDrag(dropResult);
807 DragRadarInfo dragRadarInfo;
808 dragRadarInfo.funcName = __func__;
809 dragRadarInfo.bizState = static_cast<int32_t>(BizState::STATE_END);
810 dragRadarInfo.bizStage = static_cast<int32_t>(BizStage::STAGE_STOP_DRAG);
811 dragRadarInfo.stageRes = static_cast<int32_t>(StageRes::RES_FAIL);
812 dragRadarInfo.errCode = static_cast<int32_t>(DragRadarErrCode::DRAG_STOP_CANCEL);
813 dragRadarInfo.hostName = "";
814 dragRadarInfo.callingPid = "";
815 ReportDragRadarInfo(dragRadarInfo);
816 }
817 #endif // OHOS_BUILD_ENABLE_ARKUI_X
818
SendDragData(int32_t targetTid,const std::string & udKey)819 void DragManager::SendDragData(int32_t targetTid, const std::string &udKey)
820 {
821 FI_HILOGI("enter");
822 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
823 #ifdef MSDP_FRAMEWORK_UDMF_ENABLED
824 UDMF::QueryOption option;
825 option.key = udKey;
826 UDMF::Privilege privilege;
827 privilege.tokenId = static_cast<uint32_t>(targetTid);
828 int32_t ret = UDMF::UdmfClient::GetInstance().AddPrivilege(option, privilege);
829 if (ret != RET_OK) {
830 FI_HILOGE("Failed to send pid to Udmf client");
831 }
832 #endif // MSDP_FRAMEWORK_UDMF_ENABLED
833 #endif // OHOS_BUILD_ENABLE_ARKUI_X
834 FI_HILOGI("leave");
835 }
836
OnDragUp(std::shared_ptr<MMI::PointerEvent> pointerEvent)837 int32_t DragManager::OnDragUp(std::shared_ptr<MMI::PointerEvent> pointerEvent)
838 {
839 FI_HILOGI("enter");
840 CHKPR(pointerEvent, RET_ERR);
841 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
842 if (notifyPUllUpCallback_ != nullptr) {
843 notifyPUllUpCallback_(true);
844 }
845 #endif // OHOS_BUILD_ENABLE_ARKUI_X
846 if (dragState_ != DragState::START) {
847 FI_HILOGW("No drag instance running");
848 return RET_ERR;
849 }
850 DragData dragData = DRAG_DATA_MGR.GetDragData();
851 #ifndef OHOS_BUILD_PC_PRODUCT
852 if (dragData.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
853 dragDrawing_.EraseMouseIcon();
854 FI_HILOGI("Set the pointer cursor visible");
855 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
856 MMI::InputManager::GetInstance()->SetPointerVisible(true);
857 #endif // OHOS_BUILD_ENABLE_ARKUI_X
858 }
859 #endif // OHOS_BUILD_PC_PRODUCT
860
861 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
862 CHKPR(context_, RET_ERR);
863 int32_t repeatCount = 1;
864 timerId_ = context_->GetTimerManager().AddTimer(TIMEOUT_MS, repeatCount, [this, dragData]() {
865 DragDropResult dropResult { DragResult::DRAG_EXCEPTION, false, -1 };
866 FI_HILOGW("Timeout, automatically stop dragging");
867 this->StopDrag(dropResult);
868 DragRadarInfo dragRadarInfo;
869 dragRadarInfo.funcName = __func__;
870 dragRadarInfo.bizState = static_cast<int32_t>(BizState::STATE_END);
871 dragRadarInfo.bizStage = static_cast<int32_t>(BizStage::STAGE_STOP_DRAG);
872 dragRadarInfo.stageRes = static_cast<int32_t>(StageRes::RES_FAIL);
873 dragRadarInfo.errCode = static_cast<int32_t>(DragRadarErrCode::DRAG_STOP_EXCEPTION);
874 dragRadarInfo.hostName = "";
875 dragRadarInfo.callingPid = "";
876 ReportDragRadarInfo(dragRadarInfo);
877 });
878 #endif // OHOS_BUILD_ENABLE_ARKUI_X
879 FI_HILOGI("leave");
880 return RET_OK;
881 }
882
883 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
884 #ifdef OHOS_DRAG_ENABLE_INTERCEPTOR
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const885 void DragManager::InterceptorConsumer::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
886 {
887 }
888
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const889 void DragManager::InterceptorConsumer::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
890 {
891 CHKPV(pointerEvent);
892 if (g_startFilterTime > 0) {
893 auto actionTime = pointerEvent->GetActionTime();
894 if (g_startFilterTime >= actionTime
895 && pointerEvent->GetPointerAction() == MMI::PointerEvent::POINTER_ACTION_PULL_MOVE) {
896 FI_HILOGW("Invalid event");
897 return;
898 }
899 g_startFilterTime = -1;
900 }
901 CHKPV(pointerEventCallback_);
902 pointerEventCallback_(pointerEvent);
903 pointerEvent->AddFlag(MMI::InputEvent::EVENT_FLAG_NO_INTERCEPT);
904 MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
905 if (pointerEvent->GetPointerAction() == MMI::PointerEvent::POINTER_ACTION_PULL_UP) {
906 FI_HILOGI("Pointer button is released, appened extra data");
907 MMI::InputManager::GetInstance()->AppendExtraData(DragManager::CreateExtraData(false));
908 }
909 }
910
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const911 void DragManager::InterceptorConsumer::OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const
912 {
913 }
914 #endif // OHOS_DRAG_ENABLE_INTERCEPTOR
915
916 #ifdef OHOS_DRAG_ENABLE_MONITOR
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const917 void DragManager::MonitorConsumer::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
918 {
919 FI_HILOGD("enter");
920 }
921
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const922 void DragManager::MonitorConsumer::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
923 {
924 FI_HILOGD("enter");
925 CHKPV(pointerEvent);
926 CHKPV(pointerEventCallback_);
927 pointerEventCallback_(pointerEvent);
928 if (pointerEvent->GetPointerAction() == MMI::PointerEvent::POINTER_ACTION_PULL_UP) {
929 FI_HILOGI("Pointer button is released, appened extra data");
930 int32_t ret = context_->GetDelegateTasks().PostAsyncTask([] {
931 return MMI::InputManager::GetInstance()->AppendExtraData(DragManager::CreateExtraData(false));
932 });
933 if (ret != RET_OK) {
934 FI_HILOGE("Post async task failed");
935 }
936 }
937 FI_HILOGD("leave");
938 }
939
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const940 void DragManager::MonitorConsumer::OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const
941 {
942 FI_HILOGD("enter");
943 }
944 #endif // OHOS_DRAG_ENABLE_MONITOR
945
Dump(int32_t fd) const946 void DragManager::Dump(int32_t fd) const
947 {
948 DragCursorStyle style = DRAG_DATA_MGR.GetDragStyle();
949 int32_t targetTid = DRAG_DATA_MGR.GetTargetTid();
950 dprintf(fd, "Drag information:\n");
951 #ifdef OHOS_DRAG_ENABLE_INTERCEPTOR
952 dprintf(fd,
953 "dragState:%s | dragResult:%s | interceptorId:%d | dragTargetPid:%d | dragTargetTid:%d | "
954 "cursorStyle:%s | isWindowVisble:%s\n", GetDragState(dragState_).c_str(),
955 GetDragResult(dragResult_).c_str(), pointerEventInterceptorId_, GetDragTargetPid(), targetTid,
956 GetDragCursorStyle(style).c_str(), DRAG_DATA_MGR.GetDragWindowVisible() ? "true" : "false");
957 #endif // OHOS_DRAG_ENABLE_INTERCEPTOR
958 #ifdef OHOS_DRAG_ENABLE_MONITOR
959 dprintf(fd,
960 "dragState:%s | dragResult:%s | monitorId:%d | dragTargetPid:%d | dragTargetTid:%d | "
961 "cursorStyle:%s | isWindowVisble:%s\n", GetDragState(dragState_).c_str(),
962 GetDragResult(dragResult_).c_str(), pointerEventMonitorId_, GetDragTargetPid(), targetTid,
963 GetDragCursorStyle(style).c_str(), DRAG_DATA_MGR.GetDragWindowVisible() ? "true" : "false");
964 #endif // OHOS_DRAG_ENABLE_MONITOR
965 DragData dragData = DRAG_DATA_MGR.GetDragData();
966 std::string udKey;
967 if (RET_ERR == GetUdKey(udKey)) {
968 FI_HILOGE("Target udKey is empty");
969 udKey = "";
970 }
971 for (const auto& shadowInfo : dragData.shadowInfos) {
972 dprintf(fd, "dragData = {\n""\tshadowInfoX:%d\n\tshadowInfoY\n", shadowInfo.x, shadowInfo.y);
973 }
974 dprintf(fd, "dragData = {\n"
975 "\tudKey:%s\n\tfilterInfo:%s\n\textraInfo:%s\n\tsourceType:%d"
976 "\tdragNum:%d\n\tpointerId:%d\n\tdisplayX:%d\n\tdisplayY:%d\n""\tdisplayId:%d\n\thasCanceledAnimation:%s\n",
977 GetAnonyString(dragData.udKey).c_str(), dragData.filterInfo.c_str(), dragData.extraInfo.c_str(),
978 dragData.sourceType, dragData.dragNum, dragData.pointerId, dragData.displayX, dragData.displayY,
979 dragData.displayId, dragData.hasCanceledAnimation ? "true" : "false");
980 if (dragState_ != DragState::STOP) {
981 for (const auto& shadowInfo : dragData.shadowInfos) {
982 CHKPV(shadowInfo.pixelMap);
983 dprintf(fd, "\tpixelMapWidth:%d\n\tpixelMapHeight:%d\n", shadowInfo.pixelMap->GetWidth(),
984 shadowInfo.pixelMap->GetHeight());
985 }
986 }
987 dprintf(fd, "}\n");
988 }
989 #endif // OHOS_BUILD_ENABLE_ARKUI_X
990
GetDragState(DragState value) const991 std::string DragManager::GetDragState(DragState value) const
992 {
993 std::string state = "unknown";
994 const std::map<DragState, std::string> dragStates = {
995 { DragState::START, "start" },
996 { DragState::STOP, "stop" },
997 { DragState::CANCEL, "cancel" },
998 { DragState::ERROR, "error" }
999 };
1000 auto iter = dragStates.find(value);
1001 if (iter != dragStates.end()) {
1002 state = iter->second;
1003 }
1004 return state;
1005 }
1006
GetDragResult(DragResult value) const1007 std::string DragManager::GetDragResult(DragResult value) const
1008 {
1009 std::string result = "unknown";
1010 const std::map<DragResult, std::string> dragResults = {
1011 { DragResult::DRAG_SUCCESS, "success" },
1012 { DragResult::DRAG_FAIL, "fail" },
1013 { DragResult::DRAG_CANCEL, "cancel" },
1014 { DragResult::DRAG_EXCEPTION, "abnormal" }
1015 };
1016 auto iter = dragResults.find(value);
1017 if (iter != dragResults.end()) {
1018 result = iter->second;
1019 }
1020 return result;
1021 }
1022
GetDragCursorStyle(DragCursorStyle value) const1023 std::string DragManager::GetDragCursorStyle(DragCursorStyle value) const
1024 {
1025 std::string style = "unknown";
1026 const std::map<DragCursorStyle, std::string> cursorStyles = {
1027 { DragCursorStyle::COPY, "copy" },
1028 { DragCursorStyle::DEFAULT, "default" },
1029 { DragCursorStyle::FORBIDDEN, "forbidden" },
1030 { DragCursorStyle::MOVE, "move" }
1031 };
1032 auto iter = cursorStyles.find(value);
1033 if (iter != cursorStyles.end()) {
1034 style = iter->second;
1035 }
1036 return style;
1037 }
1038
CreateExtraData(bool appended,bool drawCursor)1039 MMI::ExtraData DragManager::CreateExtraData(bool appended, bool drawCursor)
1040 {
1041 DragData dragData = DRAG_DATA_MGR.GetDragData();
1042 MMI::ExtraData extraData;
1043 extraData.buffer = dragData.buffer;
1044 extraData.toolType = dragData.toolType;
1045 extraData.sourceType = dragData.sourceType;
1046 extraData.pointerId = dragData.pointerId;
1047 extraData.appended = appended;
1048 extraData.pullId = pullId_;
1049 extraData.drawCursor = drawCursor;
1050 extraData.eventId = DRAG_DATA_MGR.GetEventId();
1051 FI_HILOGD("sourceType:%{public}d, pointerId:%{public}d, eventId:%{public}d",
1052 extraData.sourceType, extraData.pointerId, extraData.eventId);
1053 return extraData;
1054 }
1055
InitDataManager(const DragData & dragData,const std::string & appCaller)1056 int32_t DragManager::InitDataManager(const DragData &dragData, const std::string &appCaller)
1057 {
1058 FI_HILOGI("enter");
1059 DRAG_DATA_MGR.Init(dragData, appCaller);
1060 FI_HILOGI("leave");
1061 return RET_OK;
1062 }
1063
1064 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
AddDragEventHandler(int32_t sourceType)1065 int32_t DragManager::AddDragEventHandler(int32_t sourceType)
1066 {
1067 FI_HILOGI("enter");
1068 uint32_t deviceTags = 0;
1069 #ifdef OHOS_DRAG_ENABLE_INTERCEPTOR
1070 if (sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
1071 deviceTags = MMI::CapabilityToTags(MMI::INPUT_DEV_CAP_POINTER);
1072 } else if (sourceType == MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
1073 deviceTags = MMI::CapabilityToTags(MMI::INPUT_DEV_CAP_TOUCH) |
1074 MMI::CapabilityToTags(MMI::INPUT_DEV_CAP_TABLET_TOOL);
1075 } else {
1076 FI_HILOGW("Drag is not supported for this device type:%{public}d", sourceType);
1077 return RET_ERR;
1078 }
1079 #endif // OHOS_DRAG_ENABLE_INTERCEPTOR
1080 if (AddKeyEventMonitor() != RET_OK) {
1081 FI_HILOGE("Failed to add key event handler");
1082 return RET_ERR;
1083 }
1084 if (AddPointerEventHandler(deviceTags) != RET_OK) {
1085 FI_HILOGE("Failed to add pointer event handler");
1086 return RET_ERR;
1087 }
1088 FI_HILOGI("leave");
1089 return RET_OK;
1090 }
1091
AddPointerEventHandler(uint32_t deviceTags)1092 int32_t DragManager::AddPointerEventHandler(uint32_t deviceTags)
1093 {
1094 FI_HILOGI("enter");
1095 if (pointerEventMonitorId_ <= 0) {
1096 #ifdef OHOS_DRAG_ENABLE_MONITOR
1097 auto monitor = std::make_shared<MonitorConsumer>([this](std::shared_ptr<MMI::PointerEvent> pointerEvent) {
1098 return this->DragCallback(pointerEvent);
1099 }, context_);
1100 pointerEventMonitorId_ = MMI::InputManager::GetInstance()->AddMonitor(monitor);
1101 if (pointerEventMonitorId_ <= 0) {
1102 FI_HILOGE("Failed to add pointer event monitor");
1103 return RET_ERR;
1104 }
1105 #else
1106 auto callback = [this](std::shared_ptr<MMI::PointerEvent> pointerEvent) {
1107 return this->DragCallback(pointerEvent);
1108 };
1109 auto interceptor = std::make_shared<InterceptorConsumer>(callback);
1110 pointerEventInterceptorId_ = MMI::InputManager::GetInstance()->AddInterceptor(
1111 interceptor, DRAG_PRIORITY, deviceTags);
1112 if (pointerEventInterceptorId_ <= 0) {
1113 FI_HILOGE("Failed to add pointer event interceptor");
1114 return RET_ERR;
1115 }
1116 #endif // OHOS_DRAG_ENABLE_MONITOR
1117 FI_HILOGI("Add drag poniter event handle successfully");
1118 FI_HILOGI("leave");
1119 return RET_OK;
1120 } else {
1121 FI_HILOGI("leave");
1122 return RET_ERR;
1123 }
1124 }
1125
AddKeyEventMonitor()1126 int32_t DragManager::AddKeyEventMonitor()
1127 {
1128 FI_HILOGI("enter");
1129 if (keyEventMonitorId_ <= 0) {
1130 keyEventMonitorId_ = MMI::InputManager::GetInstance()->AddMonitor(
1131 [this](std::shared_ptr<MMI::KeyEvent> keyEvent) {
1132 return this->DragKeyEventCallback(keyEvent);
1133 });
1134 if (keyEventMonitorId_ <= 0) {
1135 FI_HILOGE("Failed to add key event monitor");
1136 return RET_ERR;
1137 }
1138 FI_HILOGI("Add drag key event monitor successfully");
1139 FI_HILOGI("leave");
1140 return RET_OK;
1141 } else {
1142 FI_HILOGI("leave");
1143 return RET_ERR;
1144 }
1145 }
1146
RemovePointerEventHandler()1147 int32_t DragManager::RemovePointerEventHandler()
1148 {
1149 FI_HILOGI("enter");
1150 #ifdef OHOS_DRAG_ENABLE_MONITOR
1151 if (pointerEventMonitorId_ <= 0) {
1152 FI_HILOGE("Invalid pointer event monitor id:%{public}d", pointerEventMonitorId_);
1153 return RET_ERR;
1154 }
1155 MMI::InputManager::GetInstance()->RemoveMonitor(pointerEventMonitorId_);
1156 pointerEventMonitorId_ = -1;
1157 #else
1158 if (pointerEventInterceptorId_ <= 0) {
1159 FI_HILOGE("Invalid pointer event interceptor id:%{public}d", pointerEventInterceptorId_);
1160 }
1161 MMI::InputManager::GetInstance()->RemoveInterceptor(pointerEventInterceptorId_);
1162 pointerEventInterceptorId_ = -1;
1163 #endif // OHOS_DRAG_ENABLE_MONITOR
1164 FI_HILOGI("Remove drag pointer event handler successfully");
1165 return RET_OK;
1166 }
1167
RemoveDragEventHandler()1168 int32_t DragManager::RemoveDragEventHandler()
1169 {
1170 FI_HILOGI("enter");
1171 if (RemoveKeyEventMonitor() != RET_OK) {
1172 FI_HILOGE("Failed to remove key event handler");
1173 return RET_ERR;
1174 }
1175 if (RemovePointerEventHandler() != RET_OK) {
1176 FI_HILOGE("Failed to remove pointer event handler");
1177 return RET_ERR;
1178 }
1179 FI_HILOGI("leave");
1180 return RET_OK;
1181 }
1182
RemoveKeyEventMonitor()1183 int32_t DragManager::RemoveKeyEventMonitor()
1184 {
1185 FI_HILOGI("enter");
1186 if (keyEventMonitorId_ <= 0) {
1187 FI_HILOGE("Invalid key event monitor id:%{public}d", keyEventMonitorId_);
1188 return RET_ERR;
1189 }
1190 MMI::InputManager::GetInstance()->RemoveMonitor(keyEventMonitorId_);
1191 keyEventMonitorId_ = -1;
1192 FI_HILOGI("Remove drag key event handle successfully");
1193 return RET_OK;
1194 }
1195 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1196
1197 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
OnStartDrag(const struct DragRadarPackageName & dragRadarPackageName,int32_t pid)1198 int32_t DragManager::OnStartDrag(const struct DragRadarPackageName &dragRadarPackageName, int32_t pid)
1199 #else
1200 int32_t DragManager::OnStartDrag()
1201 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1202 {
1203 FI_HILOGI("enter");
1204 pullId_ = GenerateId();
1205 FI_HILOGI("Current pullId:%{public}d", pullId_.load());
1206 if (GetControlCollaborationVisible()) {
1207 SetControlCollaborationVisible(false);
1208 }
1209 DragData dragData = DRAG_DATA_MGR.GetDragData();
1210 bool drawCursor = false;
1211 #ifdef OHOS_BUILD_PC_PRODUCT
1212 if (dragData.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
1213 drawCursor = true;
1214 }
1215 #endif // OHOS_BUILD_PC_PRODUCT
1216 auto extraData = CreateExtraData(true, drawCursor);
1217 bool isHicarOrSuperLauncher = false;
1218 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1219 sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDisplayById(dragData.displayId);
1220 if (display != nullptr) {
1221 std::string displayName = display->GetName();
1222 isHicarOrSuperLauncher = ((displayName == "HiCar") || (displayName == "SuperLauncher"));
1223 }
1224 dragDrawing_.SetScreenId(dragData.displayId);
1225 if (Rosen::DisplayManager::GetInstance().IsFoldable() && !isHicarOrSuperLauncher) {
1226 if (static_cast<uint64_t>(dragData.displayId) == displayId_) {
1227 dragDrawing_.SetScreenId(screenId_);
1228 }
1229 }
1230 int32_t ret = dragDrawing_.Init(dragData, context_, isLongPressDrag_);
1231 #else
1232 int32_t ret = dragDrawing_.Init(dragData);
1233 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1234 if (ret == INIT_FAIL) {
1235 FI_HILOGE("Init drag drawing failed");
1236 dragDrawing_.DestroyDragWindow();
1237 dragDrawing_.UpdateDrawingState();
1238 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1239 ReportStartDragFailedRadarInfo(StageRes::RES_FAIL, DragRadarErrCode::FAILED_INIT_DRAWING, __func__,
1240 dragRadarPackageName);
1241 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1242 return RET_ERR;
1243 }
1244 if (ret == INIT_CANCEL) {
1245 FI_HILOGE("Init drag drawing cancel, drag animation is running");
1246 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1247 ReportStartDragFailedRadarInfo(StageRes::RES_CANCEL, DragRadarErrCode::REPEATE_START_DRAG_EXCEPTION, __func__,
1248 dragRadarPackageName);
1249 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1250 return RET_ERR;
1251 }
1252 bool isNeedAdjustDisplayXY = true;
1253 bool isMultiSelectedAnimation = false;
1254 if (!mouseDragMonitorState_ || !existMouseMoveDragCallback_) {
1255 dragDrawing_.Draw(dragData.displayId, dragData.displayX, dragData.displayY, isNeedAdjustDisplayXY,
1256 isMultiSelectedAnimation);
1257 } else if (mouseDragMonitorState_ && existMouseMoveDragCallback_ && (mouseDragMonitorDisplayX_ != -1)
1258 && (mouseDragMonitorDisplayY_ != -1)) {
1259 dragDrawing_.Draw(dragData.displayId, mouseDragMonitorDisplayX_, mouseDragMonitorDisplayY_,
1260 isNeedAdjustDisplayXY, isMultiSelectedAnimation);
1261 }
1262 FI_HILOGI("Start drag, appened extra data");
1263 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1264 ret = AddDragEvent(dragData, dragRadarPackageName);
1265 if (ret != RET_OK) {
1266 FI_HILOGE("Failed to add drag event handler");
1267 return RET_ERR;
1268 }
1269 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1270 dragAction_.store(DragAction::MOVE);
1271 FI_HILOGI("leave");
1272 return RET_OK;
1273 }
1274
OnStopDrag(DragResult result,bool hasCustomAnimation,const std::string & packageName,int32_t pid,bool isStopCooperate)1275 int32_t DragManager::OnStopDrag(DragResult result, bool hasCustomAnimation, const std::string &packageName, int32_t pid,
1276 bool isStopCooperate)
1277 {
1278 FI_HILOGI("Add custom animation:%{public}s", hasCustomAnimation ? "true" : "false");
1279 DragData dragData = DRAG_DATA_MGR.GetDragData();
1280 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1281 if ((RemovePointerEventHandler()!= RET_OK) || (RemoveKeyEventMonitor() != RET_OK)) {
1282 DragRadarInfo dragRadarInfo;
1283 dragRadarInfo.funcName = __func__;
1284 dragRadarInfo.bizState = static_cast<int32_t>(BizState::STATE_END);
1285 dragRadarInfo.bizStage = static_cast<int32_t>(BizStage::STAGE_STOP_DRAG);
1286 dragRadarInfo.stageRes = static_cast<int32_t>(StageRes::RES_FAIL);
1287 dragRadarInfo.errCode = static_cast<int32_t>(DragRadarErrCode::FAILED_REMOVE_INPUT_MONITOR);
1288 dragRadarInfo.hostName = packageName;
1289 dragRadarInfo.callingPid = pid;
1290 ReportDragRadarInfo(dragRadarInfo);
1291 }
1292 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1293 dragAction_.store(DragAction::MOVE);
1294 FI_HILOGI("Stop drag, appened extra data");
1295 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1296 MMI::InputManager::GetInstance()->AppendExtraData(DragManager::CreateExtraData(false));
1297 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1298
1299 #ifndef OHOS_BUILD_PC_PRODUCT
1300 if (dragData.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
1301 dragDrawing_.EraseMouseIcon();
1302 if (dragState_ != DragState::MOTION_DRAGGING) {
1303 FI_HILOGI("Set the pointer cursor visible");
1304 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1305 if (isStopCooperate) {
1306 CHKPR(context_, RET_ERR);
1307 bool hasLocalPointerDevice = context_->GetDeviceManager().HasLocalPointerDevice() ||
1308 context_->GetInput().HasLocalPointerDevice();
1309 MMI::InputManager::GetInstance()->SetPointerVisible(hasLocalPointerDevice);
1310 } else {
1311 MMI::InputManager::GetInstance()->SetPointerVisible(true);
1312 }
1313 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1314 }
1315 }
1316 #endif // OHOS_BUILD_PC_PRODUCT
1317
1318 pullId_ = -1;
1319 return HandleDragResult(result, hasCustomAnimation);
1320 }
1321
OnSetDragWindowVisible(bool visible,bool isForce,bool isZoomInAndAlphaChanged)1322 int32_t DragManager::OnSetDragWindowVisible(bool visible, bool isForce, bool isZoomInAndAlphaChanged)
1323 {
1324 FI_HILOGI("Set drag window visibleion:%{public}s", visible ? "true" : "false");
1325 if (dragState_ == DragState::MOTION_DRAGGING) {
1326 FI_HILOGW("Currently in motion dragging");
1327 return RET_OK;
1328 }
1329 if (dragState_ == DragState::STOP) {
1330 FI_HILOGW("No drag instance running, can not set drag window visible");
1331 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1332 ReportDragWindowVisibleRadarInfo(StageRes::RES_FAIL, DragRadarErrCode::FAILED_SET_DRAG_VISIBLE, __func__);
1333 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1334 return RET_ERR;
1335 }
1336 if (!isForce && GetControlCollaborationVisible()) {
1337 FI_HILOGW("The drag-and-drop window is controlled by multi-screen coordination,"
1338 "can not set drag window visible:%{public}d", visible);
1339 return RET_OK;
1340 }
1341 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1342 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
1343 DragDFX::WriteDragWindowVisible(dragState_, visible, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR);
1344 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
1345 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1346 DRAG_DATA_MGR.SetDragWindowVisible(visible);
1347 dragDrawing_.UpdateDragWindowState(visible, isZoomInAndAlphaChanged);
1348 DragData dragData = DRAG_DATA_MGR.GetDragData();
1349 #ifndef OHOS_BUILD_PC_PRODUCT
1350 if (dragData.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE && visible) {
1351 FI_HILOGI("Set the pointer cursor invisible");
1352 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1353 MMI::InputManager::GetInstance()->SetPointerVisible(false);
1354 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1355 }
1356 #endif // OHOS_BUILD_PC_PRODUCT
1357
1358 if (isForce) {
1359 SetControlCollaborationVisible(isForce);
1360 FI_HILOGW("The drag-and-drop window is controlled by multi-screen coordination");
1361 }
1362 return RET_OK;
1363 }
1364
OnGetShadowOffset(ShadowOffset & shadowOffset)1365 int32_t DragManager::OnGetShadowOffset(ShadowOffset &shadowOffset)
1366 {
1367 FI_HILOGI("enter");
1368 return DRAG_DATA_MGR.GetShadowOffset(shadowOffset);
1369 }
1370
1371 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
RegisterStateChange(std::function<void (DragState)> callback)1372 void DragManager::RegisterStateChange(std::function<void(DragState)> callback)
1373 {
1374 FI_HILOGI("enter");
1375 CHKPV(callback);
1376 stateChangedCallback_ = callback;
1377 FI_HILOGI("leave");
1378 }
1379
UnregisterStateChange()1380 void DragManager::UnregisterStateChange()
1381 {
1382 FI_HILOGI("Unregister state-change callback");
1383 stateChangedCallback_ = nullptr;
1384 }
1385
RegisterNotifyPullUp(std::function<void (bool)> callback)1386 void DragManager::RegisterNotifyPullUp(std::function<void(bool)> callback)
1387 {
1388 FI_HILOGI("enter");
1389 CHKPV(callback);
1390 notifyPUllUpCallback_ = callback;
1391 FI_HILOGI("leave");
1392 }
1393
UnregisterNotifyPullUp()1394 void DragManager::UnregisterNotifyPullUp()
1395 {
1396 FI_HILOGI("Unregister notify-pullup callback");
1397 notifyPUllUpCallback_ = nullptr;
1398 }
1399
RegisterCrossDrag(std::function<void (bool)> callback)1400 void DragManager::RegisterCrossDrag(std::function<void(bool)> callback)
1401 {
1402 FI_HILOGD("Register cross_drag callback");
1403 crossDragCallback_ = callback;
1404 }
1405
UnregisterCrossDrag()1406 void DragManager::UnregisterCrossDrag()
1407 {
1408 FI_HILOGD("Unregister cross_drag callback");
1409 crossDragCallback_ = nullptr;
1410 }
1411
NotifyCrossDrag(bool isButtonDown)1412 void DragManager::NotifyCrossDrag(bool isButtonDown)
1413 {
1414 CHKPV(crossDragCallback_);
1415 crossDragCallback_(isButtonDown);
1416 }
1417
StateChangedNotify(DragState state)1418 void DragManager::StateChangedNotify(DragState state)
1419 {
1420 FI_HILOGD("enter");
1421 CHKPV(stateChangedCallback_);
1422 if (state == DragState::STOP) {
1423 stateChangedCallback_(state);
1424 } else if (dragState_ != DragState::MOTION_DRAGGING) {
1425 stateChangedCallback_(state);
1426 }
1427 FI_HILOGD("leave");
1428 }
1429 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1430
GetExtraData(bool appended) const1431 MMI::ExtraData DragManager::GetExtraData(bool appended) const
1432 {
1433 return CreateExtraData(appended);
1434 }
1435
GetDragState() const1436 DragState DragManager::GetDragState() const
1437 {
1438 return dragState_;
1439 }
1440
GetAllowDragState(bool & isAllowDrag)1441 void DragManager::GetAllowDragState(bool &isAllowDrag)
1442 {
1443 FI_HILOGD("enter");
1444 if (dragState_ != DragState::START) {
1445 FI_HILOGW("Currently state is \'%{public}d\', allow cooperate", static_cast<int32_t>(dragState_));
1446 return;
1447 }
1448 isAllowDrag = dragDrawing_.GetAllowDragState();
1449 FI_HILOGD("leave");
1450 }
1451
SetDragState(DragState state)1452 void DragManager::SetDragState(DragState state)
1453 {
1454 FI_HILOGI("SetDragState:%{public}d to %{public}d", static_cast<int32_t>(dragState_), static_cast<int32_t>(state));
1455 dragState_ = state;
1456 dragDrawing_.UpdateDragState(state);
1457 if (state == DragState::START) {
1458 UpdateDragStyleCross();
1459 }
1460 }
1461
SetDragOriginDpi(float dragOriginDpi)1462 void DragManager::SetDragOriginDpi(float dragOriginDpi)
1463 {
1464 DRAG_DATA_MGR.SetDragOriginDpi(dragOriginDpi);
1465 }
1466
GetDragResult() const1467 DragResult DragManager::GetDragResult() const
1468 {
1469 return dragResult_;
1470 }
1471
GetAppCallee() const1472 std::string DragManager::GetAppCallee() const
1473 {
1474 return appCallee_;
1475 }
1476
SetControlCollaborationVisible(bool visible)1477 void DragManager::SetControlCollaborationVisible(bool visible)
1478 {
1479 isControlCollaborationVisible_ = visible;
1480 }
GetControlCollaborationVisible() const1481 bool DragManager::GetControlCollaborationVisible() const
1482 {
1483 return isControlCollaborationVisible_;
1484 }
1485
GetDragSummary(std::map<std::string,int64_t> & summarys)1486 int32_t DragManager::GetDragSummary(std::map<std::string, int64_t> &summarys)
1487 {
1488 FI_HILOGI("enter");
1489 DragData dragData = DRAG_DATA_MGR.GetDragData();
1490 summarys = dragData.summarys;
1491 if (summarys.empty()) {
1492 FI_HILOGD("Summarys is empty");
1493 }
1494 FI_HILOGI("leave");
1495 return RET_OK;
1496 }
1497
HandleDragResult(DragResult result,bool hasCustomAnimation)1498 int32_t DragManager::HandleDragResult(DragResult result, bool hasCustomAnimation)
1499 {
1500 FI_HILOGI("enter");
1501 switch (result) {
1502 case DragResult::DRAG_SUCCESS: {
1503 if (!hasCustomAnimation) {
1504 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1505 dragDrawing_.OnDragSuccess(context_);
1506 #else
1507 dragDrawing_.OnDragSuccess();
1508 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1509 } else {
1510 dragDrawing_.DestroyDragWindow();
1511 dragDrawing_.UpdateDrawingState();
1512 }
1513 break;
1514 }
1515 case DragResult::DRAG_FAIL:
1516 case DragResult::DRAG_CANCEL: {
1517 if (!hasCustomAnimation) {
1518 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1519 dragDrawing_.OnDragFail(context_, isLongPressDrag_);
1520 #else
1521 dragDrawing_.OnDragFail();
1522 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1523 } else {
1524 dragDrawing_.DestroyDragWindow();
1525 dragDrawing_.UpdateDrawingState();
1526 }
1527 break;
1528 }
1529 case DragResult::DRAG_EXCEPTION: {
1530 dragDrawing_.DestroyDragWindow();
1531 dragDrawing_.UpdateDrawingState();
1532 break;
1533 }
1534 default: {
1535 FI_HILOGW("Unsupported DragResult type, DragResult:%{public}d", result);
1536 break;
1537 }
1538 }
1539 FI_HILOGI("leave");
1540 return RET_OK;
1541 }
1542
SetPointerEventFilterTime(int64_t filterTime)1543 void DragManager::SetPointerEventFilterTime(int64_t filterTime)
1544 {
1545 FI_HILOGD("enter");
1546 g_startFilterTime = filterTime;
1547 FI_HILOGD("leave");
1548 }
1549
MoveTo(int32_t x,int32_t y,bool isMultiSelectedAnimation)1550 void DragManager::MoveTo(int32_t x, int32_t y, bool isMultiSelectedAnimation)
1551 {
1552 if (dragState_ != DragState::START && dragState_ != DragState::MOTION_DRAGGING) {
1553 FI_HILOGE("Drag instance not running");
1554 return;
1555 }
1556 DragData dragData = DRAG_DATA_MGR.GetDragData();
1557 FI_HILOGI("displayId:%{public}d, x:%{private}d, y:%{private}d", dragData.displayId, x, y);
1558 dragDrawing_.Draw(dragData.displayId, x, y, true, isMultiSelectedAnimation);
1559 }
1560
SetMultiSelectedAnimationFlag(bool needMultiSelectedAnimation)1561 void DragManager::SetMultiSelectedAnimationFlag(bool needMultiSelectedAnimation)
1562 {
1563 FI_HILOGI("needMultiSelectedAnimation:%{public}d", needMultiSelectedAnimation);
1564 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1565 CHKPV(context_);
1566 int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this, needMultiSelectedAnimation] {
1567 this->dragDrawing_.SetMultiSelectedAnimationFlag(needMultiSelectedAnimation);
1568 return RET_OK;
1569 });
1570 if (ret != RET_OK) {
1571 FI_HILOGE("Post async task failed, ret:%{public}d", ret);
1572 }
1573 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1574 }
1575
UpdatePreviewStyle(const PreviewStyle & previewStyle)1576 int32_t DragManager::UpdatePreviewStyle(const PreviewStyle &previewStyle)
1577 {
1578 if (dragState_ != DragState::START && dragState_ != DragState::MOTION_DRAGGING) {
1579 FI_HILOGE("Drag instance not running");
1580 return RET_ERR;
1581 }
1582 if (previewStyle == DRAG_DATA_MGR.GetPreviewStyle()) {
1583 FI_HILOGD("Not need to update previewStyle");
1584 return RET_OK;
1585 }
1586 DRAG_DATA_MGR.SetPreviewStyle(previewStyle);
1587 FI_HILOGI("Update previewStyle successfully");
1588 return dragDrawing_.UpdatePreviewStyle(previewStyle);
1589 }
1590
UpdatePreviewStyleWithAnimation(const PreviewStyle & previewStyle,const PreviewAnimation & animation)1591 int32_t DragManager::UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle,
1592 const PreviewAnimation &animation)
1593 {
1594 if (dragState_ != DragState::START && dragState_ != DragState::MOTION_DRAGGING) {
1595 FI_HILOGE("Drag instance not running");
1596 return RET_ERR;
1597 }
1598 if (previewStyle == DRAG_DATA_MGR.GetPreviewStyle()) {
1599 FI_HILOGD("Not need to update previewStyle");
1600 return RET_OK;
1601 }
1602 DRAG_DATA_MGR.SetPreviewStyle(previewStyle);
1603 FI_HILOGI("Update previewStyle successfully");
1604 return dragDrawing_.UpdatePreviewStyleWithAnimation(previewStyle, animation);
1605 }
1606
RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction> & rsTransaction)1607 int32_t DragManager::RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
1608 {
1609 return dragDrawing_.RotateDragWindowSync(rsTransaction);
1610 }
1611
SetDragWindowScreenId(uint64_t displayId,uint64_t screenId)1612 void DragManager::SetDragWindowScreenId(uint64_t displayId, uint64_t screenId)
1613 {
1614 FI_HILOGI("displayId:%{public}" PRId64 ", screenId:%{public}" PRId64 "", displayId, screenId);
1615 displayId_ = displayId;
1616 screenId_ = screenId;
1617 }
1618
1619 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
DragKeyEventCallback(std::shared_ptr<MMI::KeyEvent> keyEvent)1620 void DragManager::DragKeyEventCallback(std::shared_ptr<MMI::KeyEvent> keyEvent)
1621 {
1622 CHKPV(keyEvent);
1623 if (keyEvent->GetKeyCode() == MMI::KeyEvent::KEYCODE_ESCAPE) {
1624 CHKPV(currentPointerEvent_);
1625 currentPointerEvent_->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_CANCEL);
1626 MMI::InputManager::GetInstance()->SimulateInputEvent(currentPointerEvent_);
1627 return;
1628 }
1629 auto keyItems = keyEvent->GetKeyItems();
1630 auto iter = std::find_if(keyItems.begin(), keyItems.end(),
1631 [] (std::optional<MMI::KeyEvent::KeyItem> keyItem) {
1632 return ((keyItem->GetKeyCode() == MMI::KeyEvent::KEYCODE_CTRL_LEFT) ||
1633 (keyItem->GetKeyCode() == MMI::KeyEvent::KEYCODE_CTRL_RIGHT));
1634 });
1635 if (iter == keyItems.end()) {
1636 dragAction_.store(DragAction::MOVE);
1637 return;
1638 }
1639 if ((DRAG_DATA_MGR.GetDragStyle() == DragCursorStyle::DEFAULT) ||
1640 (DRAG_DATA_MGR.GetDragStyle() == DragCursorStyle::FORBIDDEN)) {
1641 dragAction_.store(DragAction::MOVE);
1642 return;
1643 }
1644 if (!iter->IsPressed()) {
1645 CtrlKeyStyleChangedNotify(DRAG_DATA_MGR.GetDragStyle(), DragAction::MOVE);
1646 HandleCtrlKeyEvent(DRAG_DATA_MGR.GetDragStyle(), DragAction::MOVE);
1647 dragAction_.store(DragAction::MOVE);
1648 return;
1649 }
1650 if (DRAG_DATA_MGR.GetDragStyle() == DragCursorStyle::COPY) {
1651 FI_HILOGD("Not need update drag style");
1652 return;
1653 }
1654 CtrlKeyStyleChangedNotify(DragCursorStyle::COPY, DragAction::COPY);
1655 HandleCtrlKeyEvent(DragCursorStyle::COPY, DragAction::COPY);
1656 dragAction_.store(DragAction::COPY);
1657 }
1658
HandleCtrlKeyEvent(DragCursorStyle style,DragAction action)1659 void DragManager::HandleCtrlKeyEvent(DragCursorStyle style, DragAction action)
1660 {
1661 FI_HILOGD("enter");
1662 if (action == dragAction_.load()) {
1663 FI_HILOGD("Not need update drag style");
1664 return;
1665 }
1666 CHKPV(context_);
1667 int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this, style] {
1668 return this->dragDrawing_.UpdateDragStyle(style);
1669 });
1670 if (ret != RET_OK) {
1671 FI_HILOGE("Post async task failed");
1672 }
1673 FI_HILOGD("leave");
1674 }
1675 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1676
OnUpdateDragStyle(DragCursorStyle style)1677 int32_t DragManager::OnUpdateDragStyle(DragCursorStyle style)
1678 {
1679 FI_HILOGD("enter");
1680 DragCursorStyle updateStyle = GetRealDragStyle(style);
1681 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1682 stateNotify_.StyleChangedNotify(updateStyle);
1683 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1684 if (dragDrawing_.UpdateDragStyle(updateStyle) != RET_OK) {
1685 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1686 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
1687 DragDFX::WriteUpdateDragStyle(updateStyle, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT);
1688 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
1689 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1690 return RET_ERR;
1691 }
1692 FI_HILOGD("Update dragStyle:%{public}s successfully", GetDragStyleName(updateStyle).c_str());
1693 return RET_OK;
1694 }
1695
UpdateDragStyleCross()1696 void DragManager::UpdateDragStyleCross()
1697 {
1698 FI_HILOGD("enter");
1699 auto dragStyle = DRAG_DATA_MGR.GetDragStyle();
1700 FI_HILOGI("OnUpdateDragStyle dragStyle:%{public}s", GetDragStyleName(dragStyle).c_str());
1701 if (OnUpdateDragStyle(DRAG_DATA_MGR.GetDragStyle()) != RET_OK) {
1702 FI_HILOGE("OnUpdateDragStyle failed");
1703 }
1704 FI_HILOGD("leave");
1705 }
1706
GetDragStyleName(DragCursorStyle style)1707 std::string DragManager::GetDragStyleName(DragCursorStyle style)
1708 {
1709 switch (style) {
1710 case DragCursorStyle::DEFAULT : {
1711 return DRAG_STYLE_DEFAULT;
1712 }
1713 case DragCursorStyle::FORBIDDEN : {
1714 return DRAG_STYLE_FORBIDDEN;
1715 }
1716 case DragCursorStyle::COPY : {
1717 return DRAG_STYLE_COPY;
1718 }
1719 case DragCursorStyle::MOVE : {
1720 return DRAG_STYLE_MOVE;
1721 }
1722 default:
1723 break;
1724 }
1725 return DRAG_STYLE_UNKNOW;
1726 }
1727
GetRealDragStyle(DragCursorStyle style)1728 DragCursorStyle DragManager::GetRealDragStyle(DragCursorStyle style)
1729 {
1730 if ((dragAction_ == DragAction::COPY) && (style == DragCursorStyle::MOVE)) {
1731 return DragCursorStyle::COPY;
1732 }
1733 return style;
1734 }
1735
GetDragBehavior(const DragDropResult & dropResult,DragBehavior & dragBehavior)1736 void DragManager::GetDragBehavior(const DragDropResult &dropResult, DragBehavior &dragBehavior)
1737 {
1738 FI_HILOGD("enter");
1739 if (dropResult.result != DragResult::DRAG_SUCCESS) {
1740 dragBehavior = DragBehavior::UNKNOWN;
1741 return;
1742 }
1743 if (dragBehavior == DragBehavior::UNKNOWN) {
1744 if (DRAG_DATA_MGR.GetDragStyle() == DragCursorStyle::COPY) {
1745 dragBehavior = DragBehavior::COPY;
1746 return;
1747 }
1748 if (dragAction_.load()== DragAction::COPY) {
1749 dragBehavior = DragBehavior::COPY;
1750 return;
1751 }
1752 DragData dragData = DRAG_DATA_MGR.GetDragData();
1753 if (dropResult.mainWindow == dragData.mainWindow) {
1754 dragBehavior = DragBehavior::MOVE;
1755 } else {
1756 dragBehavior = DragBehavior::COPY;
1757 }
1758 }
1759 FI_HILOGD("leave");
1760 }
1761
1762 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
CtrlKeyStyleChangedNotify(DragCursorStyle style,DragAction action)1763 void DragManager::CtrlKeyStyleChangedNotify(DragCursorStyle style, DragAction action)
1764 {
1765 FI_HILOGD("enter");
1766 if (action == dragAction_.load()) {
1767 FI_HILOGD("Has notified");
1768 return;
1769 }
1770 CHKPV(context_);
1771 int32_t ret = context_->GetDelegateTasks().PostAsyncTask([this, style] {
1772 return this->stateNotify_.StyleChangedNotify(style);
1773 });
1774 if (ret != RET_OK) {
1775 FI_HILOGE("Post async task failed");
1776 }
1777 FI_HILOGD("leave");
1778 }
1779 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1780
GetDragAction(DragAction & dragAction) const1781 int32_t DragManager::GetDragAction(DragAction &dragAction) const
1782 {
1783 FI_HILOGD("enter");
1784 if (dragState_ != DragState::START) {
1785 FI_HILOGE("No drag instance running, can not get drag action");
1786 return RET_ERR;
1787 }
1788 dragAction = dragAction_.load();
1789 FI_HILOGD("leave");
1790 return RET_OK;
1791 }
1792
EnterTextEditorArea(bool enable)1793 int32_t DragManager::EnterTextEditorArea(bool enable)
1794 {
1795 FI_HILOGD("enter");
1796 if (dragState_ != DragState::START) {
1797 FI_HILOGE("No drag instance running");
1798 return RET_ERR;
1799 }
1800 if (DRAG_DATA_MGR.GetTextEditorAreaFlag() == enable) {
1801 FI_HILOGE("Set textEditorArea:%{public}s already", (enable ? "true" : "false"));
1802 return RET_ERR;
1803 }
1804 if (DRAG_DATA_MGR.GetCoordinateCorrected()) {
1805 FI_HILOGE("GetCoordinateCorrected failed");
1806 return RET_ERR;
1807 }
1808 FI_HILOGD("leave");
1809 return dragDrawing_.EnterTextEditorArea(enable);
1810 }
1811
GetExtraInfo(std::string & extraInfo) const1812 int32_t DragManager::GetExtraInfo(std::string &extraInfo) const
1813 {
1814 FI_HILOGD("enter");
1815 DragData dragData = DRAG_DATA_MGR.GetDragData();
1816 if (dragData.extraInfo.empty()) {
1817 FI_HILOGE("The extraInfo is empty");
1818 return RET_ERR;
1819 }
1820 extraInfo = dragData.extraInfo;
1821 FI_HILOGD("leave");
1822 return RET_OK;
1823 }
1824
AddPrivilege(int32_t tokenId)1825 int32_t DragManager::AddPrivilege(int32_t tokenId)
1826 {
1827 FI_HILOGD("enter");
1828 if (dragState_ != DragState::START && dragState_ != DragState::MOTION_DRAGGING) {
1829 FI_HILOGE("Drag instance not running");
1830 return RET_ERR;
1831 }
1832 DragData dragData = DRAG_DATA_MGR.GetDragData();
1833 FI_HILOGD("Target window drag tid:%{public}d", tokenId);
1834 SendDragData(tokenId, dragData.udKey);
1835 FI_HILOGD("leave");
1836 return RET_OK;
1837 }
1838
EraseMouseIcon()1839 int32_t DragManager::EraseMouseIcon()
1840 {
1841 FI_HILOGD("enter");
1842 if (dragState_ != DragState::START && dragState_ != DragState::MOTION_DRAGGING) {
1843 FI_HILOGE("Drag instance not running");
1844 return RET_ERR;
1845 }
1846 dragDrawing_.EraseMouseIcon();
1847 FI_HILOGD("leave");
1848 return RET_OK;
1849 }
1850
RotateDragWindow(Rosen::Rotation rotation)1851 int32_t DragManager::RotateDragWindow(Rosen::Rotation rotation)
1852 {
1853 FI_HILOGI("Rotation:%{public}d", static_cast<int32_t>(rotation));
1854 auto SetDragWindowRotate = [rotation, this]() {
1855 dragDrawing_.SetRotation(rotation);
1856 if ((dragState_ == DragState::START) || (dragState_ == DragState::MOTION_DRAGGING)) {
1857 dragDrawing_.RotateDragWindowAsync(rotation);
1858 }
1859 return RET_OK;
1860 };
1861 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1862 CHKPR(context_, RET_ERR);
1863 int32_t ret = context_->GetDelegateTasks().PostAsyncTask(SetDragWindowRotate);
1864 if (ret != RET_OK) {
1865 FI_HILOGE("Post async task failed, ret:%{public}d", ret);
1866 return ret;
1867 }
1868 #else
1869 SetDragWindowRotate();
1870 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1871 FI_HILOGD("leave");
1872 return RET_OK;
1873 }
1874
ScreenRotate(Rosen::Rotation rotation,Rosen::Rotation lastRotation)1875 int32_t DragManager::ScreenRotate(Rosen::Rotation rotation, Rosen::Rotation lastRotation)
1876 {
1877 FI_HILOGI("Rotation:%{public}d, lastRotation:%{public}d",
1878 static_cast<int32_t>(rotation), static_cast<int32_t>(lastRotation));
1879 DragData dragData = DRAG_DATA_MGR.GetDragData();
1880 if (dragData.sourceType != MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
1881 FI_HILOGD("Not need screen rotate");
1882 return RET_OK;
1883 }
1884 auto SetDragWindowRotate = [rotation, lastRotation, this]() {
1885 if ((dragState_ == DragState::START) || (dragState_ == DragState::MOTION_DRAGGING)) {
1886 dragDrawing_.ScreenRotate(rotation, lastRotation);
1887 }
1888 return RET_OK;
1889 };
1890 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
1891 CHKPR(context_, RET_ERR);
1892 int32_t ret = context_->GetDelegateTasks().PostAsyncTask(SetDragWindowRotate);
1893 if (ret != RET_OK) {
1894 FI_HILOGE("Post async task failed, ret:%{public}d", ret);
1895 return ret;
1896 }
1897 #else
1898 SetDragWindowRotate();
1899 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1900 FI_HILOGD("leave");
1901 return RET_OK;
1902 }
1903
1904 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
NotifyAddSelectedPixelMapResult(bool result)1905 int32_t DragManager::NotifyAddSelectedPixelMapResult(bool result)
1906 {
1907 FI_HILOGD("enter");
1908 NetPacket pkt(MessageId::ADD_SELECTED_PIXELMAP_RESULT);
1909 pkt << result;
1910 if (pkt.ChkRWError()) {
1911 FI_HILOGE("Failed to packet write data");
1912 return RET_ERR;
1913 }
1914 CHKPR(dragOutSession_, RET_ERR);
1915 if (!dragOutSession_->SendMsg(pkt)) {
1916 FI_HILOGE("Failed to send message");
1917 return MSG_SEND_FAIL;
1918 }
1919 FI_HILOGD("leave");
1920 return RET_OK;
1921 }
1922
AddSelectedPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap)1923 int32_t DragManager::AddSelectedPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap)
1924 {
1925 FI_HILOGD("enter");
1926 if (dragState_ != DragState::START) {
1927 FI_HILOGE("Drag not running");
1928 if (NotifyAddSelectedPixelMapResult(false) != RET_OK) {
1929 FI_HILOGE("Notify addSelectedPixelMap result failed");
1930 }
1931 return RET_ERR;
1932 }
1933 if (dragDrawing_.AddSelectedPixelMap(pixelMap) != RET_OK) {
1934 FI_HILOGE("Add select pixelmap fail");
1935 if (NotifyAddSelectedPixelMapResult(false) != RET_OK) {
1936 FI_HILOGE("Notify addSelectedPixelMap result failed");
1937 }
1938 return RET_ERR;
1939 }
1940 DRAG_DATA_MGR.UpdateShadowInfos(pixelMap);
1941 if (NotifyAddSelectedPixelMapResult(true) != RET_OK) {
1942 FI_HILOGW("Notify addSelectedPixelMap result failed");
1943 }
1944 FI_HILOGD("leave");
1945 return RET_OK;
1946 }
1947
1948 #else
SetDragWindow(std::shared_ptr<OHOS::Rosen::Window> window)1949 void DragManager::SetDragWindow(std::shared_ptr<OHOS::Rosen::Window> window)
1950 {
1951 dragDrawing_.SetDragWindow(window);
1952 }
1953
AddDragDestroy(std::function<void ()> cb)1954 void DragManager::AddDragDestroy(std::function<void()> cb)
1955 {
1956 dragDrawing_.AddDragDestroy(cb);
1957 }
1958
SetSVGFilePath(const std::string & filePath)1959 void DragManager::SetSVGFilePath(const std::string &filePath)
1960 {
1961 dragDrawing_.SetSVGFilePath(filePath);
1962 }
1963 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1964
1965 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
AddDragEvent(const DragData & dragData,const struct DragRadarPackageName & dragRadarPackageName)1966 int32_t DragManager::AddDragEvent(const DragData &dragData, const struct DragRadarPackageName &dragRadarPackageName)
1967 {
1968 bool drawCursor = false;
1969 #ifdef OHOS_BUILD_PC_PRODUCT
1970 if (dragData.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
1971 drawCursor = true;
1972 }
1973 #endif // OHOS_BUILD_PC_PRODUCT
1974 auto extraData = CreateExtraData(true, drawCursor);
1975 if (MMI::InputManager::GetInstance()->AppendExtraData(extraData) != RET_OK) {
1976 FI_HILOGE("Failed to append extra data to MMI");
1977 dragDrawing_.DestroyDragWindow();
1978 dragDrawing_.UpdateDrawingState();
1979 ReportStartDragFailedRadarInfo(StageRes::RES_FAIL, DragRadarErrCode::FAILED_APPEND_EXTRA_DATA, __func__,
1980 dragRadarPackageName);
1981 return RET_ERR;
1982 }
1983 if (pointerEventMonitorId_ <= 0) {
1984 if (AddDragEventHandler(dragData.sourceType) != RET_OK) {
1985 FI_HILOGE("Failed to add drag event handler");
1986 dragDrawing_.DestroyDragWindow();
1987 dragDrawing_.UpdateDrawingState();
1988 ReportStartDragFailedRadarInfo(StageRes::RES_FAIL, DragRadarErrCode::FAILED_ADD_INPUT_MONITOR, __func__,
1989 dragRadarPackageName);
1990 return RET_ERR;
1991 }
1992 }
1993 return RET_OK;
1994 }
1995 #endif // OHOS_BUILD_ENABLE_ARKUI_X
1996
1997 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
ResetMouseDragMonitorInfo()1998 void DragManager::ResetMouseDragMonitorInfo()
1999 {
2000 FI_HILOGI("enter");
2001 RemoveDragEventHandler();
2002 mouseDragMonitorDisplayX_ = -1;
2003 mouseDragMonitorDisplayY_ = -1;
2004 existMouseMoveDragCallback_ = false;
2005 mouseDragMonitorState_ = false;
2006 DRAG_DATA_MGR.SetEventId(-1);
2007 if ((context_ != nullptr) && (mouseDragMonitorTimerId_ >= 0)) {
2008 context_->GetTimerManager().RemoveTimer(mouseDragMonitorTimerId_);
2009 mouseDragMonitorTimerId_ = -1;
2010 }
2011 FI_HILOGI("leave");
2012 }
2013
SetMouseDragMonitorState(bool state)2014 int32_t DragManager::SetMouseDragMonitorState(bool state)
2015 {
2016 if (state) {
2017 if (AddDragEventHandler(MMI::PointerEvent::SOURCE_TYPE_MOUSE) != RET_OK) {
2018 FI_HILOGE("Failed to add drag event handler");
2019 return RET_ERR;
2020 }
2021 if (context_ != nullptr) {
2022 int32_t repeatCount = 1;
2023 mouseDragMonitorTimerId_ = context_->GetTimerManager().AddTimer(TIMEOUT_MS,
2024 repeatCount, [this]() {
2025 FI_HILOGW("Timeout, automatically remove monitor");
2026 this->ResetMouseDragMonitorInfo();
2027 });
2028 }
2029 } else {
2030 ResetMouseDragMonitorInfo();
2031 }
2032 mouseDragMonitorState_ = state;
2033 return RET_OK;
2034 }
2035
ReportDragWindowVisibleRadarInfo(StageRes stageRes,DragRadarErrCode errCode,const std::string & funcName)2036 void DragManager::ReportDragWindowVisibleRadarInfo(StageRes stageRes, DragRadarErrCode errCode,
2037 const std::string &funcName)
2038 {
2039 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
2040 HiSysEventWrite(
2041 OHOS::HiviewDFX::HiSysEvent::Domain::MSDP,
2042 DRAG_BEHAVIOR,
2043 HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
2044 "ORG_PKG", ORG_PKG_NAME,
2045 "FUNC", funcName,
2046 "BIZ_SCENE", 1,
2047 "BIZ_STATE", static_cast<int32_t>(BizState::STATE_IDLE),
2048 "BIZ_STAGE", static_cast<int32_t>(BizStage::STAGE_DRAGGING),
2049 "STAGE_RES", static_cast<int32_t>(stageRes),
2050 "ERROR_CODE", static_cast<int32_t>(errCode),
2051 "HOST_PKG", "",
2052 "LOCAL_NET_ID", "",
2053 "PEER_NET_ID", "",
2054 "DRAG_SUMMARY", "");
2055 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
2056 }
2057
ReportStopDragRadarInfo(BizState bizState,StageRes stageRes,DragRadarErrCode errCode,int32_t pid,struct DragRadarPackageName & dragRadarPackageName)2058 void DragManager::ReportStopDragRadarInfo(BizState bizState, StageRes stageRes, DragRadarErrCode errCode, int32_t pid,
2059 struct DragRadarPackageName &dragRadarPackageName)
2060 {
2061 DragRadarInfo dragRadarInfo;
2062 dragRadarInfo.funcName = "StopDrag";
2063 dragRadarInfo.bizState = static_cast<int32_t>(bizState);
2064 dragRadarInfo.bizStage = static_cast<int32_t>(BizStage::STAGE_STOP_DRAG);
2065 dragRadarInfo.stageRes = static_cast<int32_t>(stageRes);
2066 dragRadarInfo.errCode = static_cast<int32_t>(errCode);
2067 dragRadarInfo.hostName = dragRadarPackageName.packageName;
2068 dragRadarInfo.callingPid = std::to_string(pid);
2069 dragRadarInfo.appCallee = dragRadarPackageName.appCallee;
2070 ReportDragRadarInfo(dragRadarInfo);
2071 }
2072
ReportStartDragRadarInfo(BizState bizState,StageRes stageRes,DragRadarErrCode errCode,const std::string & peerNetId,struct DragRadarPackageName & dragRadarPackageName)2073 void DragManager::ReportStartDragRadarInfo(BizState bizState, StageRes stageRes, DragRadarErrCode errCode,
2074 const std::string &peerNetId, struct DragRadarPackageName &dragRadarPackageName)
2075 {
2076 DragRadarInfo dragRadarInfo;
2077 dragRadarInfo.funcName = "StartDrag";
2078 dragRadarInfo.bizState = static_cast<int32_t>(bizState);
2079 dragRadarInfo.bizStage = static_cast<int32_t>(BizStage::STAGE_START_DRAG);
2080 dragRadarInfo.stageRes = static_cast<int32_t>(stageRes);
2081 dragRadarInfo.errCode = static_cast<int32_t>(errCode);
2082 dragRadarInfo.hostName = dragRadarPackageName.packageName;
2083 dragRadarInfo.peerNetId = peerNetId;
2084 dragRadarInfo.appCaller = dragRadarPackageName.appCaller;
2085 ReportDragRadarInfo(dragRadarInfo);
2086 }
2087
ReportStartDragFailedRadarInfo(StageRes stageRes,DragRadarErrCode errCode,const std::string & funcName,const struct DragRadarPackageName & dragRadarPackageName)2088 void DragManager::ReportStartDragFailedRadarInfo(StageRes stageRes, DragRadarErrCode errCode,
2089 const std::string &funcName, const struct DragRadarPackageName &dragRadarPackageName)
2090 {
2091 DragRadarInfo dragRadarInfo;
2092 dragRadarInfo.funcName = funcName;
2093 dragRadarInfo.bizState = static_cast<int32_t>(BizState::STATE_END);
2094 dragRadarInfo.bizStage = static_cast<int32_t>(BizStage::STAGE_START_DRAG);
2095 dragRadarInfo.stageRes = static_cast<int32_t>(stageRes);
2096 dragRadarInfo.errCode = static_cast<int32_t>(errCode);
2097 dragRadarInfo.hostName = dragRadarPackageName.packageName;
2098 dragRadarInfo.appCaller = dragRadarPackageName.appCaller;
2099 ReportDragRadarInfo(dragRadarInfo);
2100 }
2101
ReportDragRadarInfo(struct DragRadarInfo & dragRadarInfo)2102 void DragManager::ReportDragRadarInfo(struct DragRadarInfo &dragRadarInfo)
2103 {
2104 #ifdef MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
2105 DragData dragData = DRAG_DATA_MGR.GetDragData();
2106 std::string summary;
2107 for (const auto &[udKey, recordSize] : dragData.summarys) {
2108 std::string str = udKey + "-" + std::to_string(recordSize) + ";";
2109 summary += str;
2110 }
2111 HiSysEventWrite(
2112 OHOS::HiviewDFX::HiSysEvent::Domain::MSDP,
2113 DRAG_BEHAVIOR,
2114 HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
2115 "ORG_PKG", ORG_PKG_NAME,
2116 "FUNC", dragRadarInfo.funcName,
2117 "BIZ_SCENE", 1,
2118 "BIZ_STATE", dragRadarInfo.bizState,
2119 "BIZ_STAGE", dragRadarInfo.bizStage,
2120 "STAGE_RES", dragRadarInfo.stageRes,
2121 "ERROR_CODE", dragRadarInfo.errCode,
2122 "HOST_PKG", dragRadarInfo.hostName,
2123 "LOCAL_NET_ID", dragRadarInfo.localNetId,
2124 "PEER_NET_ID", dragRadarInfo.peerNetId,
2125 "DRAG_SUMMARY", summary,
2126 "PACKAGE_NAME", dragRadarInfo.callingPid,
2127 "APP_CALLEE", dragRadarInfo.appCallee,
2128 "APP_CALLER", dragRadarInfo.appCaller);
2129 #endif // MSDP_HIVIEWDFX_HISYSEVENT_ENABLE
2130 }
2131
ReportStartDragUEInfo(const std::string & packageName)2132 void DragManager::ReportStartDragUEInfo(const std::string &packageName)
2133 {
2134 DragRadarInfo dragRadarInfo;
2135 dragRadarInfo.packageName = DRAG_FRAMEWORK;
2136 dragRadarInfo.appVersionId = APP_VERSION_ID;
2137 dragRadarInfo.hostName = packageName;
2138 dragRadarInfo.localNetId = Utility::DFXRadarAnonymize(IDSoftbusAdapter::GetLocalNetworkId().c_str());
2139 dragRadarInfo.peerNetId = peerNetId_;
2140 ReportDragUEInfo(dragRadarInfo, START_CROSSING_DRAG);
2141 }
2142
ReportStopDragUEInfo(const std::string & packageName)2143 void DragManager::ReportStopDragUEInfo(const std::string &packageName)
2144 {
2145 DragRadarInfo dragRadarInfo;
2146 dragRadarInfo.packageName = DRAG_FRAMEWORK;
2147 dragRadarInfo.appVersionId = APP_VERSION_ID;
2148 dragRadarInfo.hostName = packageName;
2149 dragRadarInfo.localNetId = Utility::DFXRadarAnonymize(IDSoftbusAdapter::GetLocalNetworkId().c_str());
2150 dragRadarInfo.peerNetId = peerNetId_;
2151 ReportDragUEInfo(dragRadarInfo, END_CROSSING_DRAG);
2152 }
2153
ReportDragUEInfo(struct DragRadarInfo & dragRadarInfo,const std::string & eventDescription)2154 void DragManager::ReportDragUEInfo(struct DragRadarInfo &dragRadarInfo, const std::string &eventDescription)
2155 {
2156 HiSysEventWrite(
2157 OHOS::HiviewDFX::HiSysEvent::Domain::DRAG_UE,
2158 eventDescription,
2159 HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
2160 "PNAMEID", dragRadarInfo.packageName,
2161 "PVERSIONID", dragRadarInfo.appVersionId,
2162 "HOSTNAME", dragRadarInfo.hostName,
2163 "LOCAL_NET_ID", dragRadarInfo.localNetId,
2164 "PEER_NET_ID", dragRadarInfo.peerNetId);
2165 }
2166 #endif // OHOS_BUILD_ENABLE_ARKUI_X
2167 } // namespace DeviceStatus
2168 } // namespace Msdp
2169 } // namespace OHOS
2170