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