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