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