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)50 int32_t InteractionImpl::SetDragWindowVisible(bool visible)
51 {
52 return InteractionManager::GetInstance()->SetDragWindowVisible(visible);
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.toolType,
73 dragData.displayX, dragData.displayY, dragData.displayId, dragData.mainWindow,
74 dragData.hasCanceledAnimation, dragData.hasCoordinateCorrected, dragData.summarys };
75 for (auto& shadowInfo: dragData.shadowInfos) {
76 if (shadowInfo.pixelMap) {
77 msdpDragData.shadowInfos.push_back({ shadowInfo.pixelMap->GetPixelMapSharedPtr(),
78 shadowInfo.x, shadowInfo.y });
79 } else {
80 msdpDragData.shadowInfos.push_back({ nullptr, shadowInfo.x, shadowInfo.y });
81 }
82 }
83 return InteractionManager::GetInstance()->StartDrag(msdpDragData,
84 std::make_shared<StartDragListenerImpl>(callbackCore));
85 }
86
UpdateDragStyle(OHOS::Ace::DragCursorStyleCore style,const int32_t eventId)87 int32_t InteractionImpl::UpdateDragStyle(OHOS::Ace::DragCursorStyleCore style, const int32_t eventId)
88 {
89 return InteractionManager::GetInstance()->UpdateDragStyle(TranslateDragCursorStyle(style), eventId);
90 }
91
UpdatePreviewStyle(const OHOS::Ace::PreviewStyle & previewStyle)92 int32_t InteractionImpl::UpdatePreviewStyle(const OHOS::Ace::PreviewStyle& previewStyle)
93 {
94 Msdp::DeviceStatus::PreviewStyle msdpPreviewStyle = TranslatePreviewStyle(previewStyle);
95 return InteractionManager::GetInstance()->UpdatePreviewStyle(msdpPreviewStyle);
96 }
97
UpdatePreviewStyleWithAnimation(const OHOS::Ace::PreviewStyle & previewStyle,const OHOS::Ace::PreviewAnimation & animation)98 int32_t InteractionImpl::UpdatePreviewStyleWithAnimation(const OHOS::Ace::PreviewStyle& previewStyle,
99 const OHOS::Ace::PreviewAnimation& animation)
100 {
101 Msdp::DeviceStatus::PreviewStyle msdpPreviewStyle = TranslatePreviewStyle(previewStyle);
102 Msdp::DeviceStatus::PreviewAnimation msdpAnimation { animation.duration, animation.curveName, animation.curve };
103 return InteractionManager::GetInstance()->UpdatePreviewStyleWithAnimation(msdpPreviewStyle, msdpAnimation);
104 }
105
StopDrag(DragDropRet result)106 int32_t InteractionImpl::StopDrag(DragDropRet result)
107 {
108 Msdp::DeviceStatus::DragDropResult dragDropResult { TranslateDragResult(result.result), result.hasCustomAnimation,
109 result.mainWindow, TranslateDragBehavior(result.dragBehavior) };
110 auto ret = InteractionManager::GetInstance()->StopDrag(dragDropResult);
111 NG::DragDropBehaviorReporter::GetInstance().UpdateDragStopResult(
112 ret ? NG::DragStopResult::DRAGFWK_STOP_FAIL : NG::DragStopResult::DRAG_SOTP_SUCCESS);
113 NG::DragDropBehaviorReporter::GetInstance().Submit(NG::DragReporterPharse::DRAG_STOP, -1);
114 return ret;
115 }
116
GetUdKey(std::string & udKey)117 int32_t InteractionImpl::GetUdKey(std::string& udKey)
118 {
119 return InteractionManager::GetInstance()->GetUdKey(udKey);
120 }
121
GetShadowOffset(ShadowOffsetData & shadowOffsetData)122 int32_t InteractionImpl::GetShadowOffset(ShadowOffsetData& shadowOffsetData)
123 {
124 return InteractionManager::GetInstance()->GetShadowOffset(
125 shadowOffsetData.offsetX, shadowOffsetData.offsetY, shadowOffsetData.width, shadowOffsetData.height);
126 }
127
GetDragSummary(std::map<std::string,int64_t> & summary)128 int32_t InteractionImpl::GetDragSummary(std::map<std::string, int64_t>& summary)
129 {
130 return InteractionManager::GetInstance()->GetDragSummary(summary);
131 }
132
GetDragExtraInfo(std::string & extraInfo)133 int32_t InteractionImpl::GetDragExtraInfo(std::string& extraInfo)
134 {
135 return InteractionManager::GetInstance()->GetExtraInfo(extraInfo);
136 }
137
EnterTextEditorArea(bool enable)138 int32_t InteractionImpl::EnterTextEditorArea(bool enable)
139 {
140 return InteractionManager::GetInstance()->EnterTextEditorArea(enable);
141 }
142
AddPrivilege()143 int32_t InteractionImpl::AddPrivilege()
144 {
145 return InteractionManager::GetInstance()->AddPrivilege();
146 }
147
RegisterCoordinationListener(std::function<void ()> dragOutCallback)148 int32_t InteractionImpl::RegisterCoordinationListener(std::function<void()> dragOutCallback)
149 {
150 std::function<void(Msdp::CoordinationMessage)> callback
151 = [dragOutCallback](Msdp::CoordinationMessage dragNotifyMsg) {
152 if (dragOutCallback && dragNotifyMsg == Msdp::CoordinationMessage::COORDINATION_SUCCESS) {
153 dragOutCallback();
154 }
155 };
156 if (consumer_) {
157 UnRegisterCoordinationListener();
158 }
159 consumer_ = std::make_shared<CoordinationListenerImpl>(callback);
160 return InteractionManager::GetInstance()->RegisterCoordinationListener(consumer_);
161 }
162
UnRegisterCoordinationListener()163 int32_t InteractionImpl::UnRegisterCoordinationListener()
164 {
165 CHECK_NULL_RETURN(consumer_, 0);
166 auto ret = InteractionManager::GetInstance()->UnregisterCoordinationListener(consumer_);
167 consumer_ = nullptr;
168 return ret;
169 }
170
SetDraggableState(bool state)171 int32_t InteractionImpl::SetDraggableState(bool state)
172 {
173 return InteractionManager::GetInstance()->SetDraggableState(state);
174 }
175
GetAppDragSwitchState(bool & state)176 int32_t InteractionImpl::GetAppDragSwitchState(bool& state)
177 {
178 return InteractionManager::GetInstance()->GetAppDragSwitchState(state);
179 }
180
SetDraggableStateAsync(bool state,int64_t downTime)181 void InteractionImpl::SetDraggableStateAsync(bool state, int64_t downTime)
182 {
183 InteractionManager::GetInstance()->SetDraggableStateAsync(state, downTime);
184 }
185
TranslateDragCursorStyle(OHOS::Ace::DragCursorStyleCore style)186 Msdp::DeviceStatus::DragCursorStyle TranslateDragCursorStyle(OHOS::Ace::DragCursorStyleCore style)
187 {
188 switch (style) {
189 case OHOS::Ace::DragCursorStyleCore::DEFAULT:
190 return Msdp::DeviceStatus::DragCursorStyle::DEFAULT;
191 case OHOS::Ace::DragCursorStyleCore::FORBIDDEN:
192 return Msdp::DeviceStatus::DragCursorStyle::FORBIDDEN;
193 case OHOS::Ace::DragCursorStyleCore::COPY:
194 return Msdp::DeviceStatus::DragCursorStyle::COPY;
195 case OHOS::Ace::DragCursorStyleCore::MOVE:
196 return Msdp::DeviceStatus::DragCursorStyle::MOVE;
197 default:
198 return Msdp::DeviceStatus::DragCursorStyle::DEFAULT;
199 }
200 }
201
TranslateDragResult(DragRet dragResult)202 Msdp::DeviceStatus::DragResult TranslateDragResult(DragRet dragResult)
203 {
204 switch (dragResult) {
205 case DragRet::DRAG_SUCCESS:
206 return Msdp::DeviceStatus::DragResult::DRAG_SUCCESS;
207 case DragRet::DRAG_FAIL:
208 return Msdp::DeviceStatus::DragResult::DRAG_FAIL;
209 case DragRet::DRAG_CANCEL:
210 return Msdp::DeviceStatus::DragResult::DRAG_CANCEL;
211 default:
212 return Msdp::DeviceStatus::DragResult::DRAG_SUCCESS;
213 }
214 }
215
TranslatePreviewStyle(const OHOS::Ace::PreviewStyle & previewStyle)216 Msdp::DeviceStatus::PreviewStyle TranslatePreviewStyle(const OHOS::Ace::PreviewStyle& previewStyle)
217 {
218 Msdp::DeviceStatus::PreviewStyle msdpPreviewStyle;
219 for (auto& previewType: previewStyle.types) {
220 switch (previewType) {
221 case OHOS::Ace::PreviewType::FOREGROUND_COLOR:
222 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::FOREGROUND_COLOR);
223 break;
224 case OHOS::Ace::PreviewType::OPACITY:
225 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::OPACITY);
226 break;
227 case OHOS::Ace::PreviewType::RADIUS:
228 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::RADIUS);
229 break;
230 case OHOS::Ace::PreviewType::SCALE:
231 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::SCALE);
232 break;
233 default:
234 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::FOREGROUND_COLOR);
235 break;
236 }
237 }
238 msdpPreviewStyle.foregroundColor = previewStyle.foregroundColor;
239 msdpPreviewStyle.opacity = previewStyle.opacity;
240 msdpPreviewStyle.radius = previewStyle.radius;
241 msdpPreviewStyle.scale = previewStyle.scale;
242 return msdpPreviewStyle;
243 }
244
TranslateDragResult(Msdp::DeviceStatus::DragResult dragResult)245 DragRet TranslateDragResult(Msdp::DeviceStatus::DragResult dragResult)
246 {
247 switch (dragResult) {
248 case Msdp::DeviceStatus::DragResult::DRAG_SUCCESS:
249 return DragRet::DRAG_SUCCESS;
250 case Msdp::DeviceStatus::DragResult::DRAG_FAIL:
251 return DragRet::DRAG_FAIL;
252 case Msdp::DeviceStatus::DragResult::DRAG_CANCEL:
253 return DragRet::DRAG_CANCEL;
254 default:
255 return DragRet::DRAG_SUCCESS;
256 }
257 }
258
GetDragState(DragState & dragState) const259 int32_t InteractionImpl::GetDragState(DragState& dragState) const
260 {
261 Msdp::DeviceStatus::DragState state;
262 int32_t ret = InteractionManager::GetInstance()->GetDragState(state);
263 switch (state) {
264 case Msdp::DeviceStatus::DragState::ERROR:
265 dragState = DragState::ERROR;
266 break;
267 case Msdp::DeviceStatus::DragState::START:
268 dragState = DragState::START;
269 break;
270 case Msdp::DeviceStatus::DragState::STOP:
271 dragState = DragState::STOP;
272 break;
273 case Msdp::DeviceStatus::DragState::CANCEL:
274 dragState = DragState::CANCEL;
275 break;
276 case Msdp::DeviceStatus::DragState::MOTION_DRAGGING:
277 dragState = DragState::MOTION_DRAGGING;
278 break;
279 default:
280 dragState = DragState::ERROR;
281 LOGW("unknow msdp drag state: %d", state);
282 break;
283 }
284 return ret;
285 }
286
TranslateDragBehavior(OHOS::Ace::DragBehavior dragBehavior)287 Msdp::DeviceStatus::DragBehavior TranslateDragBehavior(OHOS::Ace::DragBehavior dragBehavior)
288 {
289 switch (dragBehavior) {
290 case OHOS::Ace::DragBehavior::COPY:
291 return Msdp::DeviceStatus::DragBehavior::COPY;
292 case OHOS::Ace::DragBehavior::MOVE:
293 return Msdp::DeviceStatus::DragBehavior::MOVE;
294 default:
295 return Msdp::DeviceStatus::DragBehavior::UNKNOWN;
296 }
297 }
TranslateDragBehavior(Msdp::DeviceStatus::DragBehavior dragBehavior)298 OHOS::Ace::DragBehavior TranslateDragBehavior(Msdp::DeviceStatus::DragBehavior dragBehavior)
299 {
300 switch (dragBehavior) {
301 case Msdp::DeviceStatus::DragBehavior::COPY:
302 return OHOS::Ace::DragBehavior::COPY;
303 case Msdp::DeviceStatus::DragBehavior::MOVE:
304 return OHOS::Ace::DragBehavior::MOVE;
305 default:
306 return OHOS::Ace::DragBehavior::UNKNOWN;
307 }
308 }
309
310 } // namespace OHOS::Ace