• 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 "extra_data.h"
19 #include "hitrace_meter.h"
20 #include "input_manager.h"
21 #include "pixel_map.h"
22 #include "pointer_style.h"
23 
24 #include "devicestatus_define.h"
25 #include "drag_data.h"
26 #include "drag_data_manager.h"
27 #include "fi_log.h"
28 #include "proto.h"
29 
30 #include "udmf_client.h"
31 #include "unified_types.h"
32 
33 namespace OHOS {
34 namespace Msdp {
35 namespace DeviceStatus {
36 namespace {
37 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DragManager" };
38 constexpr int32_t TIMEOUT_MS { 2000 };
39 #ifdef OHOS_DRAG_ENABLE_INTERCEPTOR
40 constexpr int32_t DRAG_PRIORITY { 500 };
41 #endif // OHOS_DRAG_ENABLE_INTERCEPTOR
42 } // namespace
43 
Init(IContext * context)44 int32_t DragManager::Init(IContext* context)
45 {
46     CALL_INFO_TRACE;
47     CHKPR(context, RET_ERR);
48     context_ = context;
49     return RET_OK;
50 }
51 
OnSessionLost(SessionPtr session)52 void DragManager::OnSessionLost(SessionPtr session)
53 {
54     CALL_DEBUG_ENTER;
55     if (RemoveListener(session) != RET_OK) {
56         FI_HILOGE("Failed to clear client listener");
57     }
58 }
59 
AddListener(SessionPtr session)60 int32_t DragManager::AddListener(SessionPtr session)
61 {
62     CALL_DEBUG_ENTER;
63     CHKPR(session, RET_ERR);
64     auto info = std::make_shared<StateChangeNotify::MessageInfo>();
65     info->session = session;
66     info->msgId = MessageId::DRAG_STATE_LISTENER;
67     stateNotify_.AddNotifyMsg(info);
68     return RET_OK;
69 }
70 
RemoveListener(SessionPtr session)71 int32_t DragManager::RemoveListener(SessionPtr session)
72 {
73     CALL_DEBUG_ENTER;
74     CHKPR(session, RET_ERR);
75     auto info = std::make_shared<StateChangeNotify::MessageInfo>();
76     info->session = session;
77     stateNotify_.RemoveNotifyMsg(info);
78     return RET_OK;
79 }
80 
StartDrag(const DragData & dragData,SessionPtr sess)81 int32_t DragManager::StartDrag(const DragData &dragData, SessionPtr sess)
82 {
83     CALL_DEBUG_ENTER;
84     FI_HILOGD("PixelFormat:%{public}d, PixelAlphaType:%{public}d, PixelAllocatorType:%{public}d,"
85         " PixelWidth:%{public}d, PixelHeight:%{public}d, shadowX:%{public}d, shadowY:%{public}d,"
86         " sourceType:%{public}d, pointerId:%{public}d, displayId:%{public}d, displayX:%{public}d,"
87         " displayY:%{public}d, dragNum:%{public}d, hasCanceledAnimation:%{public}d",
88         static_cast<int32_t>(dragData.shadowInfo.pixelMap->GetPixelFormat()),
89         static_cast<int32_t>(dragData.shadowInfo.pixelMap->GetAlphaType()),
90         static_cast<int32_t>(dragData.shadowInfo.pixelMap->GetAllocatorType()),
91         dragData.shadowInfo.pixelMap->GetWidth(), dragData.shadowInfo.pixelMap->GetHeight(),
92         dragData.shadowInfo.x, dragData.shadowInfo.y, dragData.sourceType, dragData.pointerId,
93         dragData.displayId, dragData.displayX, dragData.displayY, dragData.dragNum, dragData.hasCanceledAnimation);
94     if (dragState_ == DragState::START) {
95         FI_HILOGE("Drag instance is running, can not start drag again");
96         return RET_ERR;
97     }
98     dragOutSession_ = sess;
99     if (InitDataManager(dragData) != RET_OK) {
100         FI_HILOGE("Init data manager failed");
101         return RET_ERR;
102     }
103     if (OnStartDrag() != RET_OK) {
104         FI_HILOGE("OnStartDrag failed");
105         return RET_ERR;
106     }
107     dragState_ = DragState::START;
108     stateNotify_.StateChangedNotify(DragState::START);
109     StateChangedNotify(DragState::START);
110     return RET_OK;
111 }
112 
StopDrag(DragResult result,bool hasCustomAnimation)113 int32_t DragManager::StopDrag(DragResult result, bool hasCustomAnimation)
114 {
115     CALL_DEBUG_ENTER;
116     if (dragState_ == DragState::STOP) {
117         FI_HILOGE("No drag instance running, can not stop drag");
118         return RET_ERR;
119     }
120     if ((result != DragResult::DRAG_EXCEPTION) && (context_ != nullptr) && (timerId_ >= 0)) {
121         context_->GetTimerManager().RemoveTimer(timerId_);
122         timerId_ = -1;
123     }
124     int32_t ret = RET_OK;
125     if (OnStopDrag(result, hasCustomAnimation) != RET_OK) {
126         FI_HILOGE("On stop drag failed");
127         ret = RET_ERR;
128     }
129     dragState_ = DragState::STOP;
130     stateNotify_.StateChangedNotify(DragState::STOP);
131     if (NotifyDragResult(result) != RET_OK) {
132         FI_HILOGE("Notify drag result failed");
133     }
134     DRAG_DATA_MGR.ResetDragData();
135     dragResult_ = static_cast<DragResult>(result);
136     StateChangedNotify(DragState::STOP);
137     return ret;
138 }
139 
GetDragTargetPid() const140 int32_t DragManager::GetDragTargetPid() const
141 {
142     return DRAG_DATA_MGR.GetTargetPid();
143 }
144 
GetUdKey(std::string & udKey) const145 int32_t DragManager::GetUdKey(std::string &udKey) const
146 {
147     CALL_DEBUG_ENTER;
148     DragData dragData = DRAG_DATA_MGR.GetDragData();
149     if (dragData.udKey.empty()) {
150         FI_HILOGE("Target udKey is empty");
151         return RET_ERR;
152     }
153     udKey = dragData.udKey;
154     return RET_OK;
155 }
156 
UpdateDragStyle(DragCursorStyle style,int32_t targetPid,int32_t targetTid)157 int32_t DragManager::UpdateDragStyle(DragCursorStyle style, int32_t targetPid, int32_t targetTid)
158 {
159     CALL_DEBUG_ENTER;
160     if (dragState_ != DragState::START) {
161         FI_HILOGE("No drag instance running, can not update drag style");
162         return RET_ERR;
163     }
164     if ((style < DragCursorStyle::DEFAULT) || (style > DragCursorStyle::MOVE)) {
165         FI_HILOGE("Invalid style:%{public}d", style);
166         return RET_ERR;
167     }
168     DRAG_DATA_MGR.SetDragStyle(style);
169     DRAG_DATA_MGR.SetTargetPid(targetPid);
170     DRAG_DATA_MGR.SetTargetTid(targetTid);
171     return dragDrawing_.UpdateDragStyle(style);
172 }
173 
UpdateShadowPic(const ShadowInfo & shadowInfo)174 int32_t DragManager::UpdateShadowPic(const ShadowInfo &shadowInfo)
175 {
176     CALL_DEBUG_ENTER;
177     if (dragState_ != DragState::START) {
178         FI_HILOGE("No drag instance running, can not update shadow picture");
179         return RET_ERR;
180     }
181     DRAG_DATA_MGR.SetShadowInfo(shadowInfo);
182     return dragDrawing_.UpdateShadowPic(shadowInfo);
183 }
184 
NotifyDragResult(DragResult result)185 int32_t DragManager::NotifyDragResult(DragResult result)
186 {
187     CALL_DEBUG_ENTER;
188     DragData dragData = DRAG_DATA_MGR.GetDragData();
189     int32_t targetPid = GetDragTargetPid();
190     NetPacket pkt(MessageId::DRAG_NOTIFY_RESULT);
191     if ((result < DragResult::DRAG_SUCCESS) || (result > DragResult::DRAG_EXCEPTION)) {
192         FI_HILOGE("Invalid result:%{public}d", static_cast<int32_t>(result));
193         return RET_ERR;
194     }
195     pkt << dragData.displayX << dragData.displayY << static_cast<int32_t>(result) << targetPid;
196     if (pkt.ChkRWError()) {
197         FI_HILOGE("Packet write data failed");
198         return RET_ERR;
199     }
200     CHKPR(dragOutSession_, RET_ERR);
201     if (!dragOutSession_->SendMsg(pkt)) {
202         FI_HILOGE("Send message failed");
203         return MSG_SEND_FAIL;
204     }
205     return RET_OK;
206 }
207 
DragCallback(std::shared_ptr<MMI::PointerEvent> pointerEvent)208 void DragManager::DragCallback(std::shared_ptr<MMI::PointerEvent> pointerEvent)
209 {
210     CALL_DEBUG_ENTER;
211     CHKPV(pointerEvent);
212     int32_t pointerAction = pointerEvent->GetPointerAction();
213     if (pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_MOVE) {
214         OnDragMove(pointerEvent);
215         return;
216     }
217     if (pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_UP) {
218         OnDragUp(pointerEvent);
219         return;
220     }
221     FI_HILOGD("Unknow action, sourceType:%{public}d, pointerId:%{public}d, pointerAction:%{public}d",
222         pointerEvent->GetSourceType(), pointerEvent->GetPointerId(), pointerAction);
223 }
224 
OnDragMove(std::shared_ptr<MMI::PointerEvent> pointerEvent)225 void DragManager::OnDragMove(std::shared_ptr<MMI::PointerEvent> pointerEvent)
226 {
227     CALL_DEBUG_ENTER;
228     CHKPV(pointerEvent);
229     MMI::PointerEvent::PointerItem pointerItem;
230     pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem);
231     FI_HILOGD("SourceType:%{public}d, pointerId:%{public}d, displayX:%{public}d, displayY:%{public}d",
232         pointerEvent->GetSourceType(), pointerEvent->GetPointerId(),
233         pointerItem.GetDisplayX(), pointerItem.GetDisplayY());
234     dragDrawing_.Draw(pointerEvent->GetTargetDisplayId(), pointerItem.GetDisplayX(), pointerItem.GetDisplayY());
235 }
236 
SendDragData(int32_t targetTid,const std::string & udKey)237 void DragManager::SendDragData(int32_t targetTid, const std::string &udKey)
238 {
239     CALL_DEBUG_ENTER;
240     UDMF::QueryOption option;
241     option.key = udKey;
242     UDMF::Privilege privilege;
243     privilege.tokenId = static_cast<uint32_t>(targetTid);
244     FI_HILOGD("AddPrivilege enter");
245     int32_t ret = UDMF::UdmfClient::GetInstance().AddPrivilege(option, privilege);
246     if (ret != RET_OK) {
247         FI_HILOGE("Failed to send pid to Udmf client");
248     }
249 }
250 
OnDragUp(std::shared_ptr<MMI::PointerEvent> pointerEvent)251 void DragManager::OnDragUp(std::shared_ptr<MMI::PointerEvent> pointerEvent)
252 {
253     CALL_DEBUG_ENTER;
254     CHKPV(pointerEvent);
255     FI_HILOGD("SourceType:%{public}d, pointerId:%{public}d",
256         pointerEvent->GetSourceType(), pointerEvent->GetPointerId());
257     DragData dragData = DRAG_DATA_MGR.GetDragData();
258     if (dragData.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
259         dragDrawing_.EraseMouseIcon();
260         MMI::InputManager::GetInstance()->SetPointerVisible(true);
261     }
262     int32_t targetTid = DRAG_DATA_MGR.GetTargetTid();
263     FI_HILOGD("Target window drag tid:%{public}d", targetTid);
264     SendDragData(targetTid, dragData.udKey);
265     CHKPV(context_);
266     int32_t repeatCount = 1;
267     timerId_ = context_->GetTimerManager().AddTimer(TIMEOUT_MS, repeatCount, [this]() {
268         this->StopDrag(DragResult::DRAG_EXCEPTION, false);
269     });
270     CHKPV(notifyPUllUpCallback_);
271     notifyPUllUpCallback_();
272 }
273 
274 #ifdef OHOS_DRAG_ENABLE_INTERCEPTOR
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const275 void DragManager::InterceptorConsumer::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
276 {
277     CALL_DEBUG_ENTER;
278 }
279 
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const280 void DragManager::InterceptorConsumer::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
281 {
282     CALL_DEBUG_ENTER;
283     CHKPV(pointerEvent);
284     CHKPV(callback_);
285     CHKPV(context_);
286     callback_(pointerEvent);
287     pointerEvent->AddFlag(MMI::InputEvent::EVENT_FLAG_NO_INTERCEPT);
288     auto fun = [] (std::shared_ptr<MMI::PointerEvent> pointerEvent) -> int32_t {
289         MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
290         if (pointerEvent->GetPointerAction() == MMI::PointerEvent::POINTER_ACTION_PULL_UP) {
291             MMI::InputManager::GetInstance()->AppendExtraData(DragManager::CreateExtraData(false));
292         }
293         return RET_OK;
294     };
295     int32_t ret = context_->GetDelegateTasks().PostAsyncTask(std::bind(fun, pointerEvent));
296     if (ret != RET_OK) {
297         FI_HILOGE("Post async task failed");
298     }
299 }
300 
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const301 void DragManager::InterceptorConsumer::OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const
302 {
303     CALL_DEBUG_ENTER;
304 }
305 #endif // OHOS_DRAG_ENABLE_INTERCEPTOR
306 
307 #ifdef OHOS_DRAG_ENABLE_MONITOR
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const308 void DragManager::MonitorConsumer::OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const
309 {
310     CALL_DEBUG_ENTER;
311 }
312 
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const313 void DragManager::MonitorConsumer::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
314 {
315     CALL_DEBUG_ENTER;
316     CHKPV(pointerEvent);
317     CHKPV(callback_);
318     callback_(pointerEvent);
319     if (pointerEvent->GetPointerAction() == MMI::PointerEvent::POINTER_ACTION_PULL_UP) {
320         MMI::InputManager::GetInstance()->AppendExtraData(DragManager::CreateExtraData(false));
321     }
322 }
323 
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const324 void DragManager::MonitorConsumer::OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const
325 {
326     CALL_DEBUG_ENTER;
327 }
328 #endif // OHOS_DRAG_ENABLE_MONITOR
329 
Dump(int32_t fd) const330 void DragManager::Dump(int32_t fd) const
331 {
332     CALL_DEBUG_ENTER;
333     DragCursorStyle style = DRAG_DATA_MGR.GetDragStyle();
334     int32_t targetTid = DRAG_DATA_MGR.GetTargetTid();
335     dprintf(fd, "Drag information:\n");
336 #ifdef OHOS_DRAG_ENABLE_INTERCEPTOR
337     dprintf(fd,
338             "dragState:%s | dragResult:%s | interceptorId:%d | dragTargetPid:%d | dragTargetTid:%d | "
339             "cursorStyle:%s | isWindowVisble:%s\n", GetDragState(dragState_).c_str(),
340             GetDragResult(dragResult_).c_str(), interceptorId_, GetDragTargetPid(), targetTid,
341             GetDragCursorStyle(style).c_str(), DRAG_DATA_MGR.GetDragWindowVisible() ? "true" : "false");
342 #endif // OHOS_DRAG_ENABLE_INTERCEPTOR
343 #ifdef OHOS_DRAG_ENABLE_MONITOR
344     dprintf(fd,
345             "dragState:%s | dragResult:%s | monitorId:%d | dragTargetPid:%d | dragTargetTid:%d | "
346             "cursorStyle:%s | isWindowVisble:%s\n", GetDragState(dragState_).c_str(),
347             GetDragResult(dragResult_).c_str(), monitorId_, GetDragTargetPid(), targetTid,
348             GetDragCursorStyle(style).c_str(), DRAG_DATA_MGR.GetDragWindowVisible() ? "true" : "false");
349 #endif // OHOS_DRAG_ENABLE_MONITOR
350     DragData dragData = DRAG_DATA_MGR.GetDragData();
351     std::string udKey;
352     if (RET_ERR == GetUdKey(udKey)) {
353         FI_HILOGE("Target udKey is empty");
354         udKey = "";
355     }
356     dprintf(fd, "dragData = {\n"
357             "\tshadowInfoX:%d\n\tshadowInfoY:%d\n\tudKey:%s\n\tsourceType:%d\n\tdragNum:%d\n\tpointerId:%d\n"
358             "\tdisplayX:%d\n\tdisplayY:%d\n""\tdisplayId:%d\n\thasCanceledAnimation:%s\n",
359             dragData.shadowInfo.x, dragData.shadowInfo.y, udKey.c_str(), dragData.sourceType, dragData.dragNum,
360             dragData.pointerId, dragData.displayX, dragData.displayY, dragData.displayId,
361             dragData.hasCanceledAnimation ? "true" : "false");
362     if (dragState_ != DragState::STOP) {
363         std::shared_ptr<Media::PixelMap> pixelMap = dragData.shadowInfo.pixelMap;
364         CHKPV(pixelMap);
365         dprintf(fd, "\tpixelMapWidth:%d\n\tpixelMapHeight:%d\n", pixelMap->GetWidth(), pixelMap->GetHeight());
366     }
367     dprintf(fd, "}\n");
368 }
369 
GetDragState(DragState value) const370 std::string DragManager::GetDragState(DragState value) const
371 {
372     std::string state;
373     switch (value) {
374         case DragState::START: {
375             state = "start";
376             break;
377         }
378         case DragState::STOP: {
379             state = "stop";
380             break;
381         }
382         case DragState::CANCEL: {
383             state = "cancel";
384             break;
385         }
386         case DragState::ERROR: {
387             state = "error";
388             break;
389         }
390         default: {
391             state = "unknown";
392             FI_HILOGW("Drag status unknown");
393             break;
394         }
395     }
396     return state;
397 }
398 
GetDragResult(DragResult value) const399 std::string DragManager::GetDragResult(DragResult value) const
400 {
401     std::string result;
402     switch (value) {
403         case DragResult::DRAG_SUCCESS: {
404             result = "success";
405             break;
406         }
407         case DragResult::DRAG_FAIL: {
408             result = "fail";
409             break;
410         }
411         case DragResult::DRAG_CANCEL: {
412             result = "cancel";
413             break;
414         }
415         case DragResult::DRAG_EXCEPTION: {
416             result = "abnormal";
417             break;
418         }
419         default: {
420             result = "unknown";
421             FI_HILOGW("Drag result unknown");
422             break;
423         }
424     }
425     return result;
426 }
427 
GetDragCursorStyle(DragCursorStyle value) const428 std::string DragManager::GetDragCursorStyle(DragCursorStyle value) const
429 {
430     std::string style;
431     switch (value) {
432         case DragCursorStyle::COPY: {
433             style = "copy";
434             break;
435         }
436         case DragCursorStyle::DEFAULT: {
437             style = "default";
438             break;
439         }
440         case DragCursorStyle::FORBIDDEN: {
441             style = "forbidden";
442             break;
443         }
444         case DragCursorStyle::MOVE: {
445             style = "move";
446             break;
447         }
448         default: {
449             style = "unknown";
450             FI_HILOGW("Drag cursor style unknown");
451             break;
452         }
453     }
454     return style;
455 }
456 
CreateExtraData(bool appended)457 MMI::ExtraData DragManager::CreateExtraData(bool appended)
458 {
459     CALL_DEBUG_ENTER;
460     DragData dragData = DRAG_DATA_MGR.GetDragData();
461     MMI::ExtraData extraData;
462     extraData.buffer = dragData.buffer;
463     extraData.sourceType = dragData.sourceType;
464     extraData.pointerId = dragData.pointerId;
465     extraData.appended = appended;
466     FI_HILOGD("sourceType:%{public}d, pointerId:%{public}d", extraData.sourceType, extraData.pointerId);
467     return extraData;
468 }
469 
InitDataManager(const DragData & dragData) const470 int32_t DragManager::InitDataManager(const DragData &dragData) const
471 {
472     CALL_DEBUG_ENTER;
473     MMI::PointerStyle pointerStyle;
474     if (MMI::InputManager::GetInstance()->GetPointerStyle(MMI::GLOBAL_WINDOW_ID, pointerStyle) != RET_OK) {
475         FI_HILOGE("Get pointer style failed");
476         return RET_ERR;
477     }
478     DRAG_DATA_MGR.Init(dragData, pointerStyle);
479     return RET_OK;
480 }
481 
AddDragEventHandler(int32_t sourceType)482 int32_t DragManager::AddDragEventHandler(int32_t sourceType)
483 {
484     CALL_DEBUG_ENTER;
485 #ifdef OHOS_DRAG_ENABLE_INTERCEPTOR
486     uint32_t deviceTags = 0;
487     if (sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
488         deviceTags = MMI::CapabilityToTags(MMI::INPUT_DEV_CAP_POINTER);
489     } else if (sourceType == MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
490         deviceTags = MMI::CapabilityToTags(MMI::INPUT_DEV_CAP_TOUCH) |
491             MMI::CapabilityToTags(MMI::INPUT_DEV_CAP_TABLET_TOOL);
492     } else {
493         FI_HILOGW("Drag is not supported for this device type:%{public}d", sourceType);
494         return RET_ERR;
495     }
496 #endif // OHOS_DRAG_ENABLE_INTERCEPTOR
497 #ifdef OHOS_DRAG_ENABLE_MONITOR
498     auto monitor = std::make_shared<MonitorConsumer>(std::bind(&DragManager::DragCallback, this,
499         std::placeholders::_1));
500     monitorId_ = MMI::InputManager::GetInstance()->AddMonitor(monitor);
501     if (monitorId_ <= 0) {
502         FI_HILOGE("Failed to add monitor, error code:%{public}d", monitorId_);
503         return RET_ERR;
504     }
505 #else
506     auto callback = std::bind(&DragManager::DragCallback, this, std::placeholders::_1);
507     auto interceptor = std::make_shared<InterceptorConsumer>(context_, callback);
508     interceptorId_ = MMI::InputManager::GetInstance()->AddInterceptor(interceptor, DRAG_PRIORITY, deviceTags);
509     if (interceptorId_ <= 0) {
510         FI_HILOGE("Failed to add interceptor, error code:%{public}d", interceptorId_);
511         return RET_ERR;
512     }
513 #endif // OHOS_DRAG_ENABLE_MONITOR
514     return RET_OK;
515 }
516 
OnStartDrag()517 int32_t DragManager::OnStartDrag()
518 {
519     auto extraData = CreateExtraData(true);
520     MMI::InputManager::GetInstance()->AppendExtraData(extraData);
521     DragData dragData = DRAG_DATA_MGR.GetDragData();
522     int32_t ret = dragDrawing_.Init(dragData);
523     if (ret == INIT_FAIL) {
524         FI_HILOGE("Init drag drawing failed");
525         dragDrawing_.DestroyDragWindow();
526         return RET_ERR;
527     }
528     if (ret == INIT_CANCEL) {
529         FI_HILOGE("Init drag drawing cancel, drag animation is running");
530         return RET_ERR;
531     }
532     dragDrawing_.Draw(dragData.displayId, dragData.displayX, dragData.displayY);
533     ret = AddDragEventHandler(dragData.sourceType);
534     if (ret != RET_OK) {
535 #ifdef OHOS_DRAG_ENABLE_MONITOR
536         FI_HILOGE("Failed to add drag event monitor");
537 #else
538         FI_HILOGE("Failed to add drag event interceptor");
539 #endif // OHOS_DRAG_ENABLE_MONITOR
540         dragDrawing_.DestroyDragWindow();
541         return RET_ERR;
542     }
543     if (dragData.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
544         MMI::InputManager::GetInstance()->SetPointerVisible(false);
545     }
546     return RET_OK;
547 }
548 
OnStopDrag(DragResult result,bool hasCustomAnimation)549 int32_t DragManager::OnStopDrag(DragResult result, bool hasCustomAnimation)
550 {
551     CALL_DEBUG_ENTER;
552 #ifdef OHOS_DRAG_ENABLE_MONITOR
553     if (monitorId_ <= 0) {
554         FI_HILOGE("Invalid monitor to be removed, monitorId_:%{public}d", monitorId_);
555         return RET_ERR;
556     }
557     MMI::InputManager::GetInstance()->RemoveMonitor(monitorId_);
558     monitorId_ = -1;
559 #else
560     if (interceptorId_ <= 0) {
561         FI_HILOGE("Invalid interceptorId_:%{public}d", interceptorId_);
562         return RET_ERR;
563     }
564     MMI::InputManager::GetInstance()->RemoveInterceptor(interceptorId_);
565     interceptorId_ = -1;
566 #endif // OHOS_DRAG_ENABLE_MONITOR
567     DragData dragData = DRAG_DATA_MGR.GetDragData();
568     if ((dragData.sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) && !DRAG_DATA_MGR.IsMotionDrag()) {
569         dragDrawing_.EraseMouseIcon();
570         MMI::InputManager::GetInstance()->SetPointerVisible(true);
571     }
572     return HandleDragResult(result, hasCustomAnimation);
573 }
574 
OnSetDragWindowVisible(bool visible)575 int32_t DragManager::OnSetDragWindowVisible(bool visible)
576 {
577     if (dragState_ == DragState::MOTION_DRAGGING) {
578         FI_HILOGW("Currently in motion dragging");
579         return RET_OK;
580     }
581     if (dragState_ == DragState::STOP) {
582         FI_HILOGW("No drag instance running, can not set drag window visible");
583         return RET_ERR;
584     }
585     DRAG_DATA_MGR.SetDragWindowVisible(visible);
586     dragDrawing_.UpdateDragWindowState(visible);
587     return RET_OK;
588 }
589 
OnGetShadowOffset(int32_t & offsetX,int32_t & offsetY,int32_t & width,int32_t & height)590 int32_t DragManager::OnGetShadowOffset(int32_t& offsetX, int32_t& offsetY, int32_t& width, int32_t& height)
591 {
592     return DRAG_DATA_MGR.GetShadowOffset(offsetX, offsetY, width, height);
593 }
594 
RegisterStateChange(std::function<void (DragState)> callback)595 void DragManager::RegisterStateChange(std::function<void(DragState)> callback)
596 {
597     CALL_DEBUG_ENTER;
598     CHKPV(callback);
599     stateChangedCallback_ = callback;
600 }
601 
RegisterNotifyPullUp(std::function<void (void)> callback)602 void DragManager::RegisterNotifyPullUp(std::function<void(void)> callback)
603 {
604     CALL_DEBUG_ENTER;
605     CHKPV(callback);
606     notifyPUllUpCallback_ = callback;
607 }
608 
StateChangedNotify(DragState state)609 void DragManager::StateChangedNotify(DragState state)
610 {
611     CALL_DEBUG_ENTER;
612     if ((stateChangedCallback_ != nullptr) && (!DRAG_DATA_MGR.IsMotionDrag())) {
613         stateChangedCallback_(state);
614     }
615 }
616 
GetExtraData(bool appended) const617 MMI::ExtraData DragManager::GetExtraData(bool appended) const
618 {
619     return CreateExtraData(appended);
620 }
621 
GetDragState() const622 DragState DragManager::GetDragState() const
623 {
624     return dragState_;
625 }
626 
SetDragState(DragState state)627 void DragManager::SetDragState(DragState state)
628 {
629     dragState_ = state;
630 }
631 
GetDragResult() const632 DragResult DragManager::GetDragResult() const
633 {
634     CALL_DEBUG_ENTER;
635     return dragResult_;
636 }
637 
HandleDragResult(DragResult result,bool hasCustomAnimation)638 int32_t DragManager::HandleDragResult(DragResult result, bool hasCustomAnimation)
639 {
640     CALL_DEBUG_ENTER;
641     switch (result) {
642         case DragResult::DRAG_SUCCESS: {
643             if (!hasCustomAnimation) {
644                 dragDrawing_.OnDragSuccess();
645             } else {
646                 dragDrawing_.DestroyDragWindow();
647                 dragDrawing_.UpdateDrawingState();
648             }
649             break;
650         }
651         case DragResult::DRAG_FAIL:
652         case DragResult::DRAG_CANCEL: {
653             if (!hasCustomAnimation) {
654                 dragDrawing_.OnDragFail();
655             } else {
656                 dragDrawing_.DestroyDragWindow();
657                 dragDrawing_.UpdateDrawingState();
658             }
659             break;
660         }
661         case DragResult::DRAG_EXCEPTION: {
662             dragDrawing_.DestroyDragWindow();
663             dragDrawing_.UpdateDrawingState();
664             break;
665         }
666         default: {
667             FI_HILOGW("Unsupported DragResult type, DragResult:%{public}d", result);
668             break;
669         }
670     }
671     return RET_OK;
672 }
673 } // namespace DeviceStatus
674 } // namespace Msdp
675 } // namespace OHOS
676