• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 
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 
StartDrag(const DragDataCore & dragData,std::function<void (const OHOS::Ace::DragNotifyMsg &)> callback)55 int32_t InteractionImpl::StartDrag(const DragDataCore& dragData,
56     std::function<void(const OHOS::Ace::DragNotifyMsg&)> callback)
57 {
58     std::function<void(const Msdp::DeviceStatus::DragNotifyMsg&)> callbackCore
59         = [=](const Msdp::DeviceStatus::DragNotifyMsg& dragNotifyMsg) {
60         OHOS::Ace::DragNotifyMsg msg { dragNotifyMsg.displayX,
61             dragNotifyMsg.displayY, dragNotifyMsg.targetPid, TranslateDragResult(dragNotifyMsg.result) };
62         if (callback) {
63             callback(msg);
64         }
65     };
66     Msdp::DeviceStatus::DragData msdpDragData { {},
67         dragData.buffer, dragData.udKey, dragData.extraInfo, dragData.filterInfo,
68         dragData.sourceType, dragData.dragNum, dragData.pointerId, dragData.displayX, dragData.displayY,
69         dragData.displayId, dragData.hasCanceledAnimation, dragData.hasCoordinateCorrected, dragData.summarys };
70     for (auto& shadowInfo: dragData.shadowInfos) {
71         if (shadowInfo.pixelMap) {
72             msdpDragData.shadowInfos.push_back({ shadowInfo.pixelMap->GetPixelMapSharedPtr(),
73                 shadowInfo.x, shadowInfo.y });
74         } else {
75             msdpDragData.shadowInfos.push_back({ nullptr, shadowInfo.x, shadowInfo.y });
76         }
77     }
78     return InteractionManager::GetInstance()->StartDrag(msdpDragData,
79         std::make_shared<StartDragListenerImpl>(callbackCore));
80 }
81 
UpdateDragStyle(OHOS::Ace::DragCursorStyleCore style)82 int32_t InteractionImpl::UpdateDragStyle(OHOS::Ace::DragCursorStyleCore style)
83 {
84     return InteractionManager::GetInstance()->UpdateDragStyle(TranslateDragCursorStyle(style));
85 }
86 
UpdatePreviewStyle(const OHOS::Ace::PreviewStyle & previewStyle)87 int32_t InteractionImpl::UpdatePreviewStyle(const OHOS::Ace::PreviewStyle& previewStyle)
88 {
89     Msdp::DeviceStatus::PreviewStyle msdpPreviewStyle = TranslatePreviewStyle(previewStyle);
90     return InteractionManager::GetInstance()->UpdatePreviewStyle(msdpPreviewStyle);
91 }
92 
UpdatePreviewStyleWithAnimation(const OHOS::Ace::PreviewStyle & previewStyle,const OHOS::Ace::PreviewAnimation & animation)93 int32_t InteractionImpl::UpdatePreviewStyleWithAnimation(const OHOS::Ace::PreviewStyle& previewStyle,
94     const OHOS::Ace::PreviewAnimation& animation)
95 {
96     Msdp::DeviceStatus::PreviewStyle msdpPreviewStyle = TranslatePreviewStyle(previewStyle);
97     Msdp::DeviceStatus::PreviewAnimation msdpAnimation { animation.duration, animation.curveName, animation.curve };
98     return InteractionManager::GetInstance()->UpdatePreviewStyleWithAnimation(msdpPreviewStyle, msdpAnimation);
99 }
100 
StopDrag(DragDropRet result)101 int32_t InteractionImpl::StopDrag(DragDropRet result)
102 {
103     if (SystemProperties::GetDebugEnabled()) {
104         LOGI("InteractionImpl StopDrag, DragResult is %{public}d.", result.result);
105     }
106     Msdp::DeviceStatus::DragDropResult dragDropResult {
107         TranslateDragResult(result.result), result.hasCustomAnimation, result.windowId };
108     return InteractionManager::GetInstance()->StopDrag(dragDropResult);
109 }
110 
GetUdKey(std::string & udKey)111 int32_t InteractionImpl::GetUdKey(std::string& udKey)
112 {
113     return InteractionManager::GetInstance()->GetUdKey(udKey);
114 }
115 
GetShadowOffset(ShadowOffsetData & shadowOffsetData)116 int32_t InteractionImpl::GetShadowOffset(ShadowOffsetData& shadowOffsetData)
117 {
118     return InteractionManager::GetInstance()->GetShadowOffset(
119         shadowOffsetData.offsetX, shadowOffsetData.offsetY, shadowOffsetData.width, shadowOffsetData.height);
120 }
121 
GetDragSummary(std::map<std::string,int64_t> & summary)122 int32_t InteractionImpl::GetDragSummary(std::map<std::string, int64_t>& summary)
123 {
124     return InteractionManager::GetInstance()->GetDragSummary(summary);
125 }
126 
GetDragExtraInfo(std::string & extraInfo)127 int32_t InteractionImpl::GetDragExtraInfo(std::string& extraInfo)
128 {
129     return InteractionManager::GetInstance()->GetExtraInfo(extraInfo);
130 }
131 
EnterTextEditorArea(bool enable)132 int32_t InteractionImpl::EnterTextEditorArea(bool enable)
133 {
134     return InteractionManager::GetInstance()->EnterTextEditorArea(enable);
135 }
136 
AddPrivilege()137 int32_t InteractionImpl::AddPrivilege()
138 {
139     return InteractionManager::GetInstance()->AddPrivilege();
140 }
141 
RegisterCoordinationListener(std::function<void ()> dragOutCallback)142 int32_t InteractionImpl::RegisterCoordinationListener(std::function<void()> dragOutCallback)
143 {
144     std::function<void(Msdp::CoordinationMessage)> callback
145         = [dragOutCallback](Msdp::CoordinationMessage dragNotifyMsg) {
146         if (dragOutCallback && dragNotifyMsg == Msdp::CoordinationMessage::COORDINATION_SUCCESS) {
147             dragOutCallback();
148         }
149     };
150     if (consumer_) {
151         UnRegisterCoordinationListener();
152     }
153     consumer_ = std::make_shared<CoordinationListenerImpl>(callback);
154     return InteractionManager::GetInstance()->RegisterCoordinationListener(consumer_);
155 }
156 
UnRegisterCoordinationListener()157 int32_t InteractionImpl::UnRegisterCoordinationListener()
158 {
159     CHECK_NULL_RETURN(consumer_, 0);
160     auto ret = InteractionManager::GetInstance()->UnregisterCoordinationListener(consumer_);
161     consumer_ = nullptr;
162     return ret;
163 }
164 
TranslateDragCursorStyle(OHOS::Ace::DragCursorStyleCore style)165 Msdp::DeviceStatus::DragCursorStyle TranslateDragCursorStyle(OHOS::Ace::DragCursorStyleCore style)
166 {
167     switch (style) {
168         case OHOS::Ace::DragCursorStyleCore::DEFAULT:
169             return Msdp::DeviceStatus::DragCursorStyle::DEFAULT;
170         case OHOS::Ace::DragCursorStyleCore::FORBIDDEN:
171             return Msdp::DeviceStatus::DragCursorStyle::FORBIDDEN;
172         case OHOS::Ace::DragCursorStyleCore::COPY:
173             return Msdp::DeviceStatus::DragCursorStyle::COPY;
174         case OHOS::Ace::DragCursorStyleCore::MOVE:
175             return Msdp::DeviceStatus::DragCursorStyle::MOVE;
176         default:
177             return Msdp::DeviceStatus::DragCursorStyle::DEFAULT;
178     }
179 }
180 
TranslateDragResult(DragRet dragResult)181 Msdp::DeviceStatus::DragResult TranslateDragResult(DragRet dragResult)
182 {
183     switch (dragResult) {
184         case DragRet::DRAG_SUCCESS:
185             return Msdp::DeviceStatus::DragResult::DRAG_SUCCESS;
186         case DragRet::DRAG_FAIL:
187             return Msdp::DeviceStatus::DragResult::DRAG_FAIL;
188         case DragRet::DRAG_CANCEL:
189             return Msdp::DeviceStatus::DragResult::DRAG_CANCEL;
190         default:
191             return Msdp::DeviceStatus::DragResult::DRAG_SUCCESS;
192     }
193 }
194 
TranslatePreviewStyle(const OHOS::Ace::PreviewStyle & previewStyle)195 Msdp::DeviceStatus::PreviewStyle TranslatePreviewStyle(const OHOS::Ace::PreviewStyle& previewStyle)
196 {
197     Msdp::DeviceStatus::PreviewStyle msdpPreviewStyle;
198     for (auto& previewType: previewStyle.types) {
199         switch (previewType) {
200             case OHOS::Ace::PreviewType::FOREGROUND_COLOR:
201                 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::FOREGROUND_COLOR);
202                 break;
203             case OHOS::Ace::PreviewType::OPACITY:
204                 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::OPACITY);
205                 break;
206             case OHOS::Ace::PreviewType::RADIUS:
207                 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::RADIUS);
208                 break;
209             case OHOS::Ace::PreviewType::SCALE:
210                 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::SCALE);
211                 break;
212             default:
213                 msdpPreviewStyle.types.push_back(Msdp::DeviceStatus::PreviewType::FOREGROUND_COLOR);
214                 break;
215         }
216     }
217     msdpPreviewStyle.foregroundColor = previewStyle.foregroundColor;
218     msdpPreviewStyle.opacity = previewStyle.opacity;
219     msdpPreviewStyle.radius = previewStyle.radius;
220     msdpPreviewStyle.scale = previewStyle.scale;
221     return msdpPreviewStyle;
222 }
223 
TranslateDragResult(Msdp::DeviceStatus::DragResult dragResult)224 DragRet TranslateDragResult(Msdp::DeviceStatus::DragResult dragResult)
225 {
226     switch (dragResult) {
227         case Msdp::DeviceStatus::DragResult::DRAG_SUCCESS:
228             return DragRet::DRAG_SUCCESS;
229         case Msdp::DeviceStatus::DragResult::DRAG_FAIL:
230             return DragRet::DRAG_FAIL;
231         case Msdp::DeviceStatus::DragResult::DRAG_CANCEL:
232             return DragRet::DRAG_CANCEL;
233         default:
234             return DragRet::DRAG_SUCCESS;
235     }
236 }
237 
GetDragState(DragState & dragState) const238 int32_t InteractionImpl::GetDragState(DragState& dragState) const
239 {
240     Msdp::DeviceStatus::DragState state;
241     int32_t ret = InteractionManager::GetInstance()->GetDragState(state);
242     switch (state) {
243         case Msdp::DeviceStatus::DragState::ERROR:
244             dragState = DragState::ERROR;
245             break;
246         case Msdp::DeviceStatus::DragState::START:
247             dragState = DragState::START;
248             break;
249         case Msdp::DeviceStatus::DragState::STOP:
250             dragState = DragState::STOP;
251             break;
252         case Msdp::DeviceStatus::DragState::CANCEL:
253             dragState = DragState::CANCEL;
254             break;
255         case Msdp::DeviceStatus::DragState::MOTION_DRAGGING:
256             dragState = DragState::MOTION_DRAGGING;
257             break;
258         default:
259             dragState = DragState::ERROR;
260             LOGW("unknow msdp drag state: %d", state);
261             break;
262     }
263     return ret;
264 }
265 
266 } // namespace OHOS::Ace