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