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 "interaction_impl.h"
17 #include "drag_data.h"
18 #include "core/common/interaction/interaction_data.h"
19 #include "core/gestures/gesture_info.h"
20 
21 #include "interaction_manager.h"
22 #include "adapter/ohos/capability/interaction/start_drag_listener_impl.h"
23 #include "core/components_ng/manager/drag_drop/drag_drop_behavior_reporter/drag_drop_behavior_reporter.h"
24 
25 using namespace OHOS::Msdp::DeviceStatus;
26 
27 namespace OHOS::Ace {
28     Msdp::DeviceStatus::DragCursorStyle TranslateDragCursorStyle(OHOS::Ace::DragCursorStyleCore style);
29     Msdp::DeviceStatus::DragResult TranslateDragResult(DragRet dragResult);
30     Msdp::DeviceStatus::PreviewStyle TranslatePreviewStyle(const OHOS::Ace::PreviewStyle& previewStyle);
31     DragRet TranslateDragResult(Msdp::DeviceStatus::DragResult dragResult);
32     Msdp::DeviceStatus::DragBehavior TranslateDragBehavior(OHOS::Ace::DragBehavior dragBehavior);
33     OHOS::Ace::DragBehavior TranslateDragBehavior(Msdp::DeviceStatus::DragBehavior dragBehavior);
34 
GetInstance()35 InteractionInterface* InteractionInterface::GetInstance()
36 {
37     static InteractionImpl instance;
38     return &instance;
39 }
40 
UpdateShadowPic(const OHOS::Ace::ShadowInfoCore & shadowInfo)41 int32_t InteractionImpl::UpdateShadowPic(const OHOS::Ace::ShadowInfoCore& shadowInfo)
42 {
43     auto pixelMap = shadowInfo.pixelMap;
44     if (!pixelMap) {
45         Msdp::DeviceStatus::ShadowInfo msdpShadowInfo { nullptr, shadowInfo.x, shadowInfo.y };
46         return InteractionManager::GetInstance()->UpdateShadowPic(msdpShadowInfo);
47     }
48     Msdp::DeviceStatus::ShadowInfo msdpShadowInfo { shadowInfo.pixelMap->GetPixelMapSharedPtr(), shadowInfo.x,
49         shadowInfo.y };
50     return InteractionManager::GetInstance()->UpdateShadowPic(msdpShadowInfo);
51 }
52 
SetDragWindowVisible(bool visible)53 int32_t InteractionImpl::SetDragWindowVisible(bool visible)
54 {
55     return InteractionManager::GetInstance()->SetDragWindowVisible(visible);
56 }
57 
SetMouseDragMonitorState(bool state)58 int32_t InteractionImpl::SetMouseDragMonitorState(bool state)
59 {
60     return InteractionManager::GetInstance()->SetMouseDragMonitorState(state);
61 }
62 
StartDrag(const DragDataCore & dragData,std::function<void (const OHOS::Ace::DragNotifyMsg &)> callback)63 int32_t InteractionImpl::StartDrag(const DragDataCore& dragData,
64     std::function<void(const OHOS::Ace::DragNotifyMsg&)> callback)
65 {
66     std::function<void(const Msdp::DeviceStatus::DragNotifyMsg&)> callbackCore
67         = [=](const Msdp::DeviceStatus::DragNotifyMsg& dragNotifyMsg) {
68         OHOS::Ace::DragNotifyMsg msg { dragNotifyMsg.displayX, dragNotifyMsg.displayY, dragNotifyMsg.targetPid,
69             TranslateDragResult(dragNotifyMsg.result), TranslateDragBehavior(dragNotifyMsg.dragBehavior) };
70         if (callback) {
71             callback(msg);
72         }
73     };
74     Msdp::DeviceStatus::DragData msdpDragData { {}, dragData.buffer, dragData.udKey, dragData.extraInfo,
75     dragData.filterInfo, dragData.sourceType, dragData.dragNum, dragData.pointerId, dragData.displayX,
76     dragData.displayY, dragData.displayId, dragData.mainWindow, dragData.hasCanceledAnimation,
77     dragData.hasCoordinateCorrected, dragData.summarys };
78     for (auto& shadowInfo: dragData.shadowInfos) {
79         if (shadowInfo.pixelMap) {
80             msdpDragData.shadowInfos.push_back({ shadowInfo.pixelMap->GetPixelMapSharedPtr(),
81                 shadowInfo.x, shadowInfo.y });
82         } else {
83             msdpDragData.shadowInfos.push_back({ nullptr, shadowInfo.x, shadowInfo.y });
84         }
85     }
86     return InteractionManager::GetInstance()->StartDrag(msdpDragData,
87         std::make_shared<StartDragListenerImpl>(callbackCore));
88 }
89 
UpdateDragStyle(OHOS::Ace::DragCursorStyleCore style,const int32_t eventId)90 int32_t InteractionImpl::UpdateDragStyle(OHOS::Ace::DragCursorStyleCore style, const int32_t eventId)
91 {
92     return InteractionManager::GetInstance()->UpdateDragStyle(TranslateDragCursorStyle(style), eventId);
93 }
94 
UpdatePreviewStyle(const OHOS::Ace::PreviewStyle & previewStyle)95 int32_t InteractionImpl::UpdatePreviewStyle(const OHOS::Ace::PreviewStyle& previewStyle)
96 {
97     Msdp::DeviceStatus::PreviewStyle msdpPreviewStyle = TranslatePreviewStyle(previewStyle);
98     return InteractionManager::GetInstance()->UpdatePreviewStyle(msdpPreviewStyle);
99 }
100 
UpdatePreviewStyleWithAnimation(const OHOS::Ace::PreviewStyle & previewStyle,const OHOS::Ace::PreviewAnimation & animation)101 int32_t InteractionImpl::UpdatePreviewStyleWithAnimation(const OHOS::Ace::PreviewStyle& previewStyle,
102     const OHOS::Ace::PreviewAnimation& animation)
103 {
104     Msdp::DeviceStatus::PreviewStyle msdpPreviewStyle = TranslatePreviewStyle(previewStyle);
105     Msdp::DeviceStatus::PreviewAnimation msdpAnimation { animation.duration, animation.curveName, animation.curve };
106     return InteractionManager::GetInstance()->UpdatePreviewStyleWithAnimation(msdpPreviewStyle, msdpAnimation);
107 }
108 
StopDrag(DragDropRet result)109 int32_t InteractionImpl::StopDrag(DragDropRet result)
110 {
111     Msdp::DeviceStatus::DragDropResult dragDropResult { TranslateDragResult(result.result), result.hasCustomAnimation,
112     result.mainWindow, TranslateDragBehavior(result.dragBehavior) };
113     auto ret = InteractionManager::GetInstance()->StopDrag(dragDropResult);
114     NG::DragDropBehaviorReporter::GetInstance().UpdateDragStopResult(
115         ret ? NG::DragStopResult::DRAGFWK_STOP_FAIL : NG::DragStopResult::DRAG_SOTP_SUCCESS);
116     NG::DragDropBehaviorReporter::GetInstance().Submit(NG::DragReporterPharse::DRAG_STOP, -1);
117     return ret;
118 }
119 
GetUdKey(std::string & udKey)120 int32_t InteractionImpl::GetUdKey(std::string& udKey)
121 {
122     return InteractionManager::GetInstance()->GetUdKey(udKey);
123 }
124 
GetShadowOffset(ShadowOffsetData & shadowOffsetData)125 int32_t InteractionImpl::GetShadowOffset(ShadowOffsetData& shadowOffsetData)
126 {
127     return InteractionManager::GetInstance()->GetShadowOffset(
128         shadowOffsetData.offsetX, shadowOffsetData.offsetY, shadowOffsetData.width, shadowOffsetData.height);
129 }
130 
GetDragSummary(std::map<std::string,int64_t> & summary)131 int32_t InteractionImpl::GetDragSummary(std::map<std::string, int64_t>& summary)
132 {
133     return InteractionManager::GetInstance()->GetDragSummary(summary);
134 }
135 
GetDragExtraInfo(std::string & extraInfo)136 int32_t InteractionImpl::GetDragExtraInfo(std::string& extraInfo)
137 {
138     return InteractionManager::GetInstance()->GetExtraInfo(extraInfo);
139 }
140 
AddPrivilege()141 int32_t InteractionImpl::AddPrivilege()
142 {
143     return InteractionManager::GetInstance()->AddPrivilege();
144 }
145 
EnterTextEditorArea(bool enable)146 int32_t InteractionImpl::EnterTextEditorArea(bool enable)
147 {
148     return InteractionManager::GetInstance()->EnterTextEditorArea(enable);
149 }
150 
RegisterCoordinationListener(std::function<void ()> dragOutCallback)151 int32_t InteractionImpl::RegisterCoordinationListener(std::function<void()> dragOutCallback)
152 {
153     std::function<void(Msdp::CoordinationMessage)> callback
154         = [dragOutCallback](Msdp::CoordinationMessage dragNotifyMsg) {
155         if (dragOutCallback && dragNotifyMsg == Msdp::CoordinationMessage::COORDINATION_SUCCESS) {
156             dragOutCallback();
157         }
158     };
159     if (consumer_) {
160         UnRegisterCoordinationListener();
161     }
162     consumer_ = std::make_shared<CoordinationListenerImpl>(callback);
163     return InteractionManager::GetInstance()->RegisterCoordinationListener(consumer_);
164 }
165 
UnRegisterCoordinationListener()166 int32_t InteractionImpl::UnRegisterCoordinationListener()
167 {
168     CHECK_NULL_RETURN(consumer_, 0);
169     auto ret = InteractionManager::GetInstance()->UnregisterCoordinationListener(consumer_);
170     consumer_ = nullptr;
171     return ret;
172 }
173 
TranslateDragCursorStyle(OHOS::Ace::DragCursorStyleCore style)174 Msdp::DeviceStatus::DragCursorStyle TranslateDragCursorStyle(OHOS::Ace::DragCursorStyleCore style)
175 {
176     switch (style) {
177         case OHOS::Ace::DragCursorStyleCore::DEFAULT:
178             return Msdp::DeviceStatus::DragCursorStyle::DEFAULT;
179         case OHOS::Ace::DragCursorStyleCore::FORBIDDEN:
180             return Msdp::DeviceStatus::DragCursorStyle::FORBIDDEN;
181         case OHOS::Ace::DragCursorStyleCore::COPY:
182             return Msdp::DeviceStatus::DragCursorStyle::COPY;
183         case OHOS::Ace::DragCursorStyleCore::MOVE:
184             return Msdp::DeviceStatus::DragCursorStyle::MOVE;
185         default:
186             return Msdp::DeviceStatus::DragCursorStyle::DEFAULT;
187     }
188 }
189 
TranslateDragResult(DragRet dragResult)190 Msdp::DeviceStatus::DragResult TranslateDragResult(DragRet dragResult)
191 {
192     switch (dragResult) {
193         case DragRet::DRAG_SUCCESS:
194             return Msdp::DeviceStatus::DragResult::DRAG_SUCCESS;
195         case DragRet::DRAG_FAIL:
196             return Msdp::DeviceStatus::DragResult::DRAG_FAIL;
197         case DragRet::DRAG_CANCEL:
198             return Msdp::DeviceStatus::DragResult::DRAG_CANCEL;
199         default:
200             return Msdp::DeviceStatus::DragResult::DRAG_SUCCESS;
201     }
202 }
203 
TranslatePreviewStyle(const OHOS::Ace::PreviewStyle & previewStyle)204 Msdp::DeviceStatus::PreviewStyle TranslatePreviewStyle(const OHOS::Ace::PreviewStyle& previewStyle)
205 {
206     Msdp::DeviceStatus::PreviewStyle msdpPreviewStyle;
207     for (auto& previewType: previewStyle.types) {
208         switch (previewType) {
209             case OHOS::Ace::PreviewType::FOREGROUND_COLOR:
210                 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::FOREGROUND_COLOR);
211                 break;
212             case OHOS::Ace::PreviewType::OPACITY:
213                 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::OPACITY);
214                 break;
215             case OHOS::Ace::PreviewType::RADIUS:
216                 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::RADIUS);
217                 break;
218             case OHOS::Ace::PreviewType::SCALE:
219                 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::SCALE);
220                 break;
221             default:
222                 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::FOREGROUND_COLOR);
223                 break;
224         }
225     }
226     msdpPreviewStyle.foregroundColor = previewStyle.foregroundColor;
227     msdpPreviewStyle.opacity = previewStyle.opacity;
228     msdpPreviewStyle.radius = previewStyle.radius;
229     msdpPreviewStyle.scale = previewStyle.scale;
230     return msdpPreviewStyle;
231 }
232 
TranslateDragResult(Msdp::DeviceStatus::DragResult dragResult)233 DragRet TranslateDragResult(Msdp::DeviceStatus::DragResult dragResult)
234 {
235     switch (dragResult) {
236         case Msdp::DeviceStatus::DragResult::DRAG_SUCCESS:
237             return DragRet::DRAG_SUCCESS;
238         case Msdp::DeviceStatus::DragResult::DRAG_FAIL:
239             return DragRet::DRAG_FAIL;
240         case Msdp::DeviceStatus::DragResult::DRAG_CANCEL:
241             return DragRet::DRAG_CANCEL;
242         default:
243             return DragRet::DRAG_SUCCESS;
244     }
245 }
246 
GetDragState(DragState & dragState) const247 int32_t InteractionImpl::GetDragState(DragState& dragState) const
248 {
249     Msdp::DeviceStatus::DragState state;
250     int32_t ret = InteractionManager::GetInstance()->GetDragState(state);
251     switch (state) {
252         case Msdp::DeviceStatus::DragState::ERROR:
253             dragState = DragState::ERROR;
254             break;
255         case Msdp::DeviceStatus::DragState::START:
256             dragState = DragState::START;
257             break;
258         case Msdp::DeviceStatus::DragState::STOP:
259             dragState = DragState::STOP;
260             break;
261         case Msdp::DeviceStatus::DragState::CANCEL:
262             dragState = DragState::CANCEL;
263             break;
264         case Msdp::DeviceStatus::DragState::MOTION_DRAGGING:
265             dragState = DragState::MOTION_DRAGGING;
266             break;
267         default:
268             dragState = DragState::ERROR;
269             LOGW("unknow msdp drag state: %d", state);
270             break;
271     }
272     return ret;
273 }
274 
TranslateDragBehavior(OHOS::Ace::DragBehavior dragBehavior)275 Msdp::DeviceStatus::DragBehavior TranslateDragBehavior(OHOS::Ace::DragBehavior dragBehavior)
276 {
277     switch (dragBehavior) {
278         case OHOS::Ace::DragBehavior::COPY:
279             return Msdp::DeviceStatus::DragBehavior::COPY;
280         case OHOS::Ace::DragBehavior::MOVE:
281             return Msdp::DeviceStatus::DragBehavior::MOVE;
282         default:
283             return Msdp::DeviceStatus::DragBehavior::UNKNOWN;
284     }
285 }
TranslateDragBehavior(Msdp::DeviceStatus::DragBehavior dragBehavior)286 OHOS::Ace::DragBehavior TranslateDragBehavior(Msdp::DeviceStatus::DragBehavior dragBehavior)
287 {
288     switch (dragBehavior) {
289         case Msdp::DeviceStatus::DragBehavior::COPY:
290             return OHOS::Ace::DragBehavior::COPY;
291         case Msdp::DeviceStatus::DragBehavior::MOVE:
292             return OHOS::Ace::DragBehavior::MOVE;
293         default:
294             return OHOS::Ace::DragBehavior::UNKNOWN;
295     }
296 }
297 
298 } // namespace OHOS::Ace