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