1 /*
2  * Copyright (c) 2022-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 #ifndef INTERACTION_MANAGER_H
17 #define INTERACTION_MANAGER_H
18 
19 #include <functional>
20 #include <memory>
21 
22 #include "nocopyable.h"
23 
24 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
25 #include "coordination_message.h"
26 #endif // OHOS_BUILD_ENABLE_ARKUI_X
27 #include "drag_data.h"
28 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
29 #include "i_coordination_listener.h"
30 #include "i_drag_listener.h"
31 #include "i_event_listener.h"
32 #include "i_hotarea_listener.h"
33 #include "i_start_drag_listener.h"
34 #include "i_subscript_listener.h"
35 #include "transaction/rs_transaction.h"
36 #else
37 #include "pointer_event.h"
38 #include "virtual_rs_window.h"
39 #endif // OHOS_BUILD_ENABLE_ARKUI_X
40 
41 namespace OHOS {
42 namespace Msdp {
43 namespace DeviceStatus {
44 class InteractionManager {
45 public:
46 
47     static InteractionManager *GetInstance();
48     virtual ~InteractionManager() = default;
49 
50 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
51     /**
52      * @brief Registers a listener for screen hopping events of the mouse pointer.
53      * @param listener Indicates the listener for screen hopping events of the mouse pointer.
54      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
55      * @since 9
56      */
57     int32_t RegisterCoordinationListener(std::shared_ptr<ICoordinationListener> listener,
58         bool isCompatible = false);
59 
60     /**
61      * @brief Unregisters a listener for screen hopping events of the mouse pointer.
62      * @param listener Indicates the listener for screen hopping events of the mouse pointer.
63      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
64      * @since 9
65      */
66     int32_t UnregisterCoordinationListener(std::shared_ptr<ICoordinationListener> listener,
67         bool isCompatible = false);
68 
69     /**
70      * @brief Prepares for screen hopping.
71      * @param callback Indicates the callback used to receive the result of enabling or disabling screen hopping.
72      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
73      * @since 9
74      */
75     int32_t PrepareCoordination(std::function<void(const std::string&, const CoordinationMsgInfo&)> callback,
76         bool isCompatible = false);
77 
78     /**
79      * @brief Cancels the preparation for screen hopping.
80      * @param callback Indicates the callback used to receive the result of enabling or disabling screen hopping.
81      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
82      * @since 9
83      */
84     int32_t UnprepareCoordination(std::function<void(const std::string&, const CoordinationMsgInfo&)> callback,
85         bool isCompatible = false);
86 
87     /**
88      * @brief Starts screen hopping for the mouse pointer.
89      * @param s remoteNetworkId Indicates the descriptor of the target input device (network ID) for screen hopping.
90      * @param startDeviceId Indicates the ID of the source input device (device ID handle) for screen hopping.
91      * @param callback Indicates the callback used to receive the result of starting screen hopping.
92      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
93      * @since 9
94      */
95     int32_t ActivateCoordination(const std::string &remoteNetworkId, int32_t startDeviceId,
96         std::function<void(const std::string&, const CoordinationMsgInfo&)> callback, bool isCompatible = false);
97 
98     /**
99      * @brief Stops screen hopping for the mouse pointer.
100      * @param isUnchained Specifies Whether to disable the cross-device link.
101      * The value <b>true</b> means to disable the cross-device link, and <b>false</b> means the opposite.
102      * @param callback Indicates the callback used to receive the result of stopping screen hopping.
103      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
104      * @since 9
105      */
106     int32_t DeactivateCoordination(bool isUnchained,
107         std::function<void(const std::string&, const CoordinationMsgInfo&)> callback, bool isCompatible = false);
108 
109     /**
110      * @brief Obtains the screen hopping status of a mouse pointer.
111      * @param networkId Indicates the descriptor of the input device.
112      * @param callback Indicates the callback used to receive the screen hopping status.
113      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
114      * @since 9
115      */
116     int32_t GetCoordinationState(const std::string &networkId, std::function<void(bool)> callback,
117         bool isCompatible = false);
118 
119     /**
120      * @brief Obtains the screen hopping status of a mouse pointer.
121      * @param udId Indicates the descriptor of the input device.
122      * @param state Indicates the state of crossing switch.
123      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
124      * @since 12
125      */
126     int32_t GetCoordinationState(const std::string &udId, bool &state);
127 
128     /**
129      * @brief Registers a listener for mouse pointer position information on the specified device.
130      * @param networkId Indicates the descriptor of the input device.
131      * @param listener Indicates the listener for mouse pointer position information on the specified device.
132      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
133      * @since 9
134      */
135     int32_t RegisterEventListener(const std::string &networkId, std::shared_ptr<IEventListener> listener);
136 
137     /**
138      * @brief Unregisters a listener for mouse pointer position information on the specified device.
139      * @param networkId Indicates the descriptor of the input device.
140      * @param listener Indicates the listener mouse pointer position information on the specified device.
141      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
142      * @since 9
143      */
144     int32_t UnregisterEventListener(const std::string &networkId, std::shared_ptr<IEventListener> listener = nullptr);
145 
146     int32_t SetDamplingCoefficient(uint32_t direction, double coefficient);
147 
148     /**
149      * @brief Starts dragging.
150      * @param dragData Indicates additional data used for dragging.
151      * @param listener Indicates the listener used to notify dragging result etc.
152      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
153      * @since 10
154      */
155     int32_t StartDrag(const DragData &dragData, std::shared_ptr<IStartDragListener> listener);
156 #else
157     /**
158      * @brief Starts dragging.
159      * @param dragData Indicates additional data used for dragging.
160      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
161      * @since 12
162      */
163     int32_t StartDrag(const DragData &dragData);
164 #endif // OHOS_BUILD_ENABLE_ARKUI_X
165     /**
166      * @brief Stops dragging.
167      * @param result Indicates the dragging result. The value <b>0</b> means that the dragging operation is successful;
168      * <b>1</b> means that the dragging operation is failed; <b>2</b> means that the dragging operation is canceled.
169      * @param hasCustomAnimation Specifies whether a custom animation is played when the dragging is successful.
170      * The value <b>true</b> means that a custom animation is played,
171      * and <b>false</b> means that the default animation is played.
172      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
173      * @since 10
174      */
175     int32_t StopDrag(const DragDropResult &dropResult);
176 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
177     /**
178      * @brief Updates the mouse pointer style used for dragging.
179      * @param style Indicates the new mouse pointer style.
180      * @param eventId Indicates the descriptor of the event.
181      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
182      * @since 10
183      */
184     int32_t UpdateDragStyle(DragCursorStyle style, int32_t eventId = -1);
185 #else
186     /**
187      * @brief Updates the mouse pointer style used for dragging.
188      * @param style Indicates the new mouse pointer style.
189      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
190      * @since 10
191      */
192     int32_t UpdateDragStyle(DragCursorStyle style);
193 #endif // OHOS_BUILD_ENABLE_ARKUI_X
194     /**
195      * @brief Obtains the PID of the target window.
196      * @return Returns a value greater than or equal to 0 in normal cases; returns <b>-1</b> if the PID is invalid.
197      * @since 10
198      */
199     int32_t GetDragTargetPid();
200 
201     /**
202      * @brief Obtains the unified data key of the target window.
203      * @param UdKey Indicates the unified data key of the target window.
204      * @return Returns a value greater than or equal to 0 in normal cases; returns <b>-1</b> if the PID is invalid.
205      * @since 10
206      */
207     int32_t GetUdKey(std::string &udKey);
208 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
209     /**
210      * @brief Registers a listener for dragging status changes.
211      * @param listener Indicates the listener for dragging status changes.
212      * @param isJsCaller Indicates whether to add checking.
213      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
214      * @since 10
215      */
216     int32_t AddDraglistener(std::shared_ptr<IDragListener> listener, bool isJsCaller = false);
217 
218     /**
219      * @brief Unregisters a listener for dragging status changes.
220      * @param listener Indicates the listener for dragging status changes.
221      * If no value is passed, all listeners are canceled.
222      * @param isJsCaller Indicates whether to add checking.
223      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
224      * @since 10
225      */
226     int32_t RemoveDraglistener(std::shared_ptr<IDragListener> listener = nullptr, bool isJsCaller = false);
227 
228     /**
229      * @brief Register a listener for dragging corner style changes.
230      * @param listener Indicates the listener for dragging corner style changes.
231      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
232      * @since 10
233      */
234     int32_t AddSubscriptListener(std::shared_ptr<ISubscriptListener> listener);
235 
236     /**
237      * @brief Unregisters a listener for dragging corner style changes.
238      * @param listener Indicates the listener for dragging corner style changes.
239      * If no value is passed, all listeners are canceled.
240      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
241      * @since 10
242      */
243     int32_t RemoveSubscriptListener(std::shared_ptr<ISubscriptListener> listener);
244 #endif // OHOS_BUILD_ENABLE_ARKUI_X
245 
246     /**
247      * @brief Displays or hides the dragging window.
248      * @param visible Specifies whether to display the dragging window.
249      * The value <b>true</b> means to display the dragging window, and <b>false</b> means to hide the window.
250      * @param isForce Specifies Enforce the visibility of the drag window, which is applied to this drag.
251      * For example, if you set the drag window to Hidden and isForce to true during a drag, the setting does not
252      * take effect when the drag window is displayed and isForce is false, and the setting becomes invalid at the
253      * end of the current drag.
254      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
255      * @since 10
256      */
257     int32_t SetDragWindowVisible(bool visible, bool isForce = false);
258 
259     /**
260      * @brief Obtains the position of the touch point or mouse pointer relative to
261      * the upper left corner of the shadow thumbnail.
262      * @param offsetX Indicates the x coordinate.
263      * @param offsetY Indicates the y coordinate.
264      * @param width Indicates the width of the shadow thumbnail.
265      * @param height Indicates the height of the shadow thumbnail.
266      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
267      * @since 10
268      */
269     int32_t GetShadowOffset(int32_t &offsetX, int32_t &offsetY, int32_t &width, int32_t &height);
270 
271     /**
272      * @brief Updates the shadow thumbnail information used for dragging.
273      * @param shadowInfo Indicates the new shadow thumbnail information.
274      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
275      * @since 10
276      */
277     int32_t UpdateShadowPic(const ShadowInfo &shadowInfo);
278 
279     /**
280      * @brief Obtains the dragging data.
281      * @param dragData Indicates the dragging data.
282      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
283      * @since 10
284      */
285     int32_t GetDragData(DragData &dragData);
286 
287     /**
288      * @brief Obtains the current droping type.
289      * @param dragAction dropping type while user pressed ctrl or not.
290      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
291      * @since 10
292      */
293     int32_t GetDragAction(DragAction &dragAction);
294 
295     /**
296      * @brief Obtains the 'extraInfo' field in the drag data.
297      * @param extraInfo Indicates the 'extraInfo' field in the drag data, mainly to save whether to allow drag across
298      * the device "drag_allow_distributed" field.
299      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
300      * @since 10
301      */
302     int32_t GetExtraInfo(std::string &extraInfo);
303 #ifndef  OHOS_BUILD_ENABLE_ARKUI_X
304     /**
305      * @brief Registers a listener for screen hot area of the mouse pointer.
306      * @param listener Indicates the listener for screen hot area of the mouse pointer.
307      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
308      * @since 11
309      */
310     int32_t AddHotAreaListener(std::shared_ptr<IHotAreaListener> listener);
311 #endif // OHOS_BUILD_ENABLE_ARKUI_X
312     /**
313      * @brief Obtains the dragging state.
314      * @param dragState Dragging state.
315      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
316      * @since 10
317      */
318     int32_t GetDragState(DragState &dragState);
319 #ifndef  OHOS_BUILD_ENABLE_ARKUI_X
320     /**
321      * @brief Unregisters a listener for screen hot area of the mouse pointer.
322      * @param listener Indicates the listener for screen hot area of the mouse pointer.
323      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
324      * @since 9
325      */
326     int32_t RemoveHotAreaListener(std::shared_ptr<IHotAreaListener> listener = nullptr);
327 #endif // OHOS_BUILD_ENABLE_ARKUI_X
328 
329     /**
330      * @brief Update preview style when dragging.
331      * @param previewStyle Indicates the preview style param for dragged item.
332      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
333      * @since 11
334      */
335     int32_t UpdatePreviewStyle(const PreviewStyle &previewStyle);
336 
337     /**
338      * @brief Update preview style with animation when dragging.
339      * @param previewStyle Indicates the preview style param for dragged item.
340      * @param animation Indicates the animation param for dragged item.
341      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
342      * @since 11
343      */
344     int32_t UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle, const PreviewAnimation &animation);
345 
346 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
347     /**
348      * @brief Rotate drag window sync.
349      * @param rsTransaction Indicates utterances rotate the sync handle.
350      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
351      * @since 12
352      */
353     int32_t RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr);
354 
355     /**
356      * @brief Obtains data summary of the drag object.
357      * @param summarys Indicates data summary of the drag object.
358      * @param isJsCaller Indicates whether to add checking.
359      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
360      * @since 11
361      */
362     int32_t GetDragSummary(std::map<std::string, int64_t> &summarys, bool isJsCaller = false);
363 #else
364     /**
365      * @brief Obtains data summary of the drag object.
366      * @param summarys Indicates data summary of the drag object.
367      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
368      * @since 12
369      */
370     int32_t GetDragSummary(std::map<std::string, int64_t> &summarys);
371 #endif // OHOS_BUILD_ENABLE_ARKUI_X
372 
373     /**
374      * @brief Specifies whether to implement 8dp movement in the text editor area.
375      * @param enable Indicates whether to enable 8dp movement.
376      * The value <b>true</b> means to enable 8dp movement, and the value <b>false</b> means the opposite.
377      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
378      * @since 11
379      */
380     int32_t EnterTextEditorArea(bool enable);
381 
382 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
383     int32_t AddPrivilege();
384 
385     int32_t EraseMouseIcon();
386 
387     int32_t SetDragWindowScreenId(uint64_t displayId, uint64_t screenId);
388 
389     int32_t SetMouseDragMonitorState(bool state);
390 
391     /**
392      * @brief Add an image to the drag list.
393      * @param pixelMap Add Selected image information.
394      * @param callback Indicates the callback used to notify addSelectedPixelMap result.
395      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
396      * @since 12
397      */
398     int32_t AddSelectedPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, std::function<void(bool)> callback);
399 #else
400     /**
401      * @brief convert relative pointerEvent action to PULL_MOVE or PULL_UP.
402      * @param pointerEvent the normal input event need to deal with.
403      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
404      * @since 12
405      */
406     int32_t UpdatePointerAction(std::shared_ptr<MMI::PointerEvent> pointerEvent);
407 
408     /**
409      * @brief set window.
410      * @param window drag drawing needs window.
411      * @return
412      * @since 12
413      */
414     void SetDragWindow(std::shared_ptr<OHOS::Rosen::Window> window);
415 
416     /**
417      * @brief set callback to destroy window.
418      * @param cb callback function.
419      * @return
420      * @since 12
421      */
422     void RegisterDragWindow(std::function<void()> cb);
423 
424     /**
425      * @brief set VSG file path.
426      * @param filePath save SVG file path.
427      * @return
428      * @since 12
429      */
430     void SetSVGFilePath(const std::string &filePath);
431 #endif // OHOS_BUILD_ENABLE_ARKUI_X
432 
433 private:
434     InteractionManager() = default;
435     DISALLOW_COPY_AND_MOVE(InteractionManager);
436     static InteractionManager *instance_;
437 };
438 } // namespace DeviceStatus
439 } // namespace Msdp
440 } // namespace OHOS
441 
442 #define INTERACTION_MGR OHOS::Msdp::DeviceStatus::InteractionManager::GetInstance()
443 
444 #endif // INTERACTION_MANAGER_H
445