• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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     InteractionManager() = default;
48     virtual ~InteractionManager() = default;
49     static std::shared_ptr<InteractionManager> GetInstance();
50 
51 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
52     /**
53      * @brief Registers a listener for screen hopping events of the mouse pointer.
54      * @param listener Indicates the listener for screen hopping events of the mouse pointer.
55      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
56      * @since 9
57      */
58     int32_t RegisterCoordinationListener(std::shared_ptr<ICoordinationListener> listener,
59         bool isCompatible = false);
60 
61     /**
62      * @brief Unregisters a listener for screen hopping events of the mouse pointer.
63      * @param listener Indicates the listener for screen hopping events of the mouse pointer.
64      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
65      * @since 9
66      */
67     int32_t UnregisterCoordinationListener(std::shared_ptr<ICoordinationListener> listener,
68         bool isCompatible = false);
69 
70     /**
71      * @brief Prepares for screen hopping.
72      * @param callback Indicates the callback used to receive the result of enabling or disabling screen hopping.
73      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
74      * @since 9
75      */
76     int32_t PrepareCoordination(std::function<void(const std::string&, const CoordinationMsgInfo&)> callback,
77         bool isCompatible = false);
78 
79     /**
80      * @brief Cancels the preparation for screen hopping.
81      * @param callback Indicates the callback used to receive the result of enabling or disabling screen hopping.
82      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
83      * @since 9
84      */
85     int32_t UnprepareCoordination(std::function<void(const std::string&, const CoordinationMsgInfo&)> callback,
86         bool isCompatible = false);
87 
88     /**
89      * @brief Starts screen hopping for the mouse pointer.
90      * @param s remoteNetworkId Indicates the descriptor of the target input device (network ID) for screen hopping.
91      * @param startDeviceId Indicates the ID of the source input device (device ID handle) for screen hopping.
92      * @param callback Indicates the callback used to receive the result of starting screen hopping.
93      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
94      * @since 9
95      */
96     int32_t ActivateCoordination(const std::string &remoteNetworkId, int32_t startDeviceId,
97         std::function<void(const std::string&, const CoordinationMsgInfo&)> callback, bool isCompatible = false);
98 
99     /**
100      * @brief Starts screen hopping for the mouse pointer.
101      * @param remoteNetworkId Indicates the descriptor of the target input device (network ID) for screen hopping.
102      * @param startDeviceId Indicates the ID of the source input device (device ID handle) for screen hopping.
103      * @param callback Indicates the callback used to receive the result of starting screen hopping.
104      * @param cooperateOptions cooperation options for peer device.
105      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
106      * @since 20
107      */
108     int32_t ActivateCooperateWithOptions(const std::string &remoteNetworkId, int32_t startDeviceId, std::function
109         <void(const std::string&, const CoordinationMsgInfo&)> callback, const CooperateOptions &cooperateOptions);
110 
111     /**
112      * @brief Stops screen hopping for the mouse pointer.
113      * @param isUnchained Specifies Whether to disable the cross-device link.
114      * The value <b>true</b> means to disable the cross-device link, and <b>false</b> means the opposite.
115      * @param callback Indicates the callback used to receive the result of stopping screen hopping.
116      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
117      * @since 9
118      */
119     int32_t DeactivateCoordination(bool isUnchained,
120         std::function<void(const std::string&, const CoordinationMsgInfo&)> callback, bool isCompatible = false);
121 
122     /**
123      * @brief Obtains the screen hopping status of a mouse pointer.
124      * @param networkId Indicates the descriptor of the input device.
125      * @param callback Indicates the callback used to receive the screen hopping status.
126      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
127      * @since 9
128      */
129     int32_t GetCoordinationState(const std::string &networkId, std::function<void(bool)> callback,
130         bool isCompatible = false);
131 
132     /**
133      * @brief Obtains the screen hopping status of a mouse pointer.
134      * @param udId Indicates the descriptor of the input device.
135      * @param state Indicates the state of crossing switch.
136      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
137      * @since 12
138      */
139     int32_t GetCoordinationState(const std::string &udId, bool &state);
140 
141     /**
142      * @brief Registers a listener for mouse pointer position information on the specified device.
143      * @param networkId Indicates the descriptor of the input device.
144      * @param listener Indicates the listener for mouse pointer position information on the specified device.
145      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
146      * @since 9
147      */
148     int32_t RegisterEventListener(const std::string &networkId, std::shared_ptr<IEventListener> listener);
149 
150     /**
151      * @brief Unregisters a listener for mouse pointer position information on the specified device.
152      * @param networkId Indicates the descriptor of the input device.
153      * @param listener Indicates the listener mouse pointer position information on the specified device.
154      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
155      * @since 9
156      */
157     int32_t UnregisterEventListener(const std::string &networkId, std::shared_ptr<IEventListener> listener = nullptr);
158 
159     int32_t SetDamplingCoefficient(uint32_t direction, double coefficient);
160 
161     /**
162      * @brief Starts dragging.
163      * @param dragData Indicates additional data used for dragging.
164      * @param listener Indicates the listener used to notify dragging result etc.
165      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
166      * @since 10
167      */
168     int32_t StartDrag(const DragData &dragData, std::shared_ptr<IStartDragListener> listener);
169 #else
170     /**
171      * @brief Starts dragging.
172      * @param dragData Indicates additional data used for dragging.
173      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
174      * @since 12
175      */
176     int32_t StartDrag(const DragData &dragData);
177 #endif // OHOS_BUILD_ENABLE_ARKUI_X
178     /**
179      * @brief Stops dragging.
180      * @param result Indicates the dragging result. The value <b>0</b> means that the dragging operation is successful;
181      * <b>1</b> means that the dragging operation is failed; <b>2</b> means that the dragging operation is canceled.
182      * @param hasCustomAnimation Specifies whether a custom animation is played when the dragging is successful.
183      * The value <b>true</b> means that a custom animation is played,
184      * and <b>false</b> means that the default animation is played.
185      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
186      * @since 10
187      */
188     int32_t StopDrag(const DragDropResult &dropResult);
189 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
190     /**
191      * @brief Updates the mouse pointer style used for dragging.
192      * @param style Indicates the new mouse pointer style.
193      * @param eventId Indicates the descriptor of the event.
194      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
195      * @since 10
196      */
197     int32_t UpdateDragStyle(DragCursorStyle style, int32_t eventId = -1);
198 #else
199     /**
200      * @brief Updates the mouse pointer style used for dragging.
201      * @param style Indicates the new mouse pointer style.
202      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
203      * @since 10
204      */
205     int32_t UpdateDragStyle(DragCursorStyle style);
206 #endif // OHOS_BUILD_ENABLE_ARKUI_X
207     /**
208      * @brief Obtains the PID of the target window.
209      * @return Returns a value greater than or equal to 0 in normal cases; returns <b>-1</b> if the PID is invalid.
210      * @since 10
211      */
212     int32_t GetDragTargetPid();
213 
214     /**
215      * @brief Obtains the unified data key of the target window.
216      * @param UdKey Indicates the unified data key of the target window.
217      * @return Returns a value greater than or equal to 0 in normal cases; returns <b>-1</b> if the PID is invalid.
218      * @since 10
219      */
220     int32_t GetUdKey(std::string &udKey);
221 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
222     /**
223      * @brief Registers a listener for dragging status changes.
224      * @param listener Indicates the listener for dragging status changes.
225      * @param isJsCaller Indicates whether to add checking.
226      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
227      * @since 10
228      */
229     int32_t AddDraglistener(std::shared_ptr<IDragListener> listener, bool isJsCaller = false);
230 
231     /**
232      * @brief Unregisters a listener for dragging status changes.
233      * @param listener Indicates the listener for dragging status changes.
234      * If no value is passed, all listeners are canceled.
235      * @param isJsCaller Indicates whether to add checking.
236      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
237      * @since 10
238      */
239     int32_t RemoveDraglistener(std::shared_ptr<IDragListener> listener = nullptr, bool isJsCaller = false);
240 
241     /**
242      * @brief Register a listener for dragging corner style changes.
243      * @param listener Indicates the listener for dragging corner style changes.
244      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
245      * @since 10
246      */
247     int32_t AddSubscriptListener(std::shared_ptr<ISubscriptListener> listener);
248 
249     /**
250      * @brief Unregisters a listener for dragging corner style changes.
251      * @param listener Indicates the listener for dragging corner style changes.
252      * If no value is passed, all listeners are canceled.
253      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
254      * @since 10
255      */
256     int32_t RemoveSubscriptListener(std::shared_ptr<ISubscriptListener> listener);
257 #endif // OHOS_BUILD_ENABLE_ARKUI_X
258 
259     /**
260      * @brief Displays or hides the dragging window.
261      * @param visible Specifies whether to display the dragging window.
262      * The value <b>true</b> means to display the dragging window, and <b>false</b> means to hide the window.
263      * @param isForce Specifies Enforce the visibility of the drag window, which is applied to this drag.
264      * For example, if you set the drag window to Hidden and isForce to true during a drag, the setting does not
265      * take effect when the drag window is displayed and isForce is false, and the setting becomes invalid at the
266      * end of the current drag.
267      * @param rsTransaction Indicates utterances displays or hides the sync handle.
268      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
269      * @since 10
270      */
271     int32_t SetDragWindowVisible(bool visible, bool isForce = false,
272         const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr);
273 
274     /**
275      * @brief Obtains the position of the touch point or mouse pointer relative to
276      * the upper left corner of the shadow thumbnail.
277      * @param offsetX Indicates the x coordinate.
278      * @param offsetY Indicates the y coordinate.
279      * @param width Indicates the width of the shadow thumbnail.
280      * @param height Indicates the height of the shadow thumbnail.
281      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
282      * @since 10
283      */
284     int32_t GetShadowOffset(int32_t &offsetX, int32_t &offsetY, int32_t &width, int32_t &height);
285 
286     /**
287      * @brief Updates the shadow thumbnail information used for dragging.
288      * @param shadowInfo Indicates the new shadow thumbnail information.
289      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
290      * @since 10
291      */
292     int32_t UpdateShadowPic(const ShadowInfo &shadowInfo);
293 
294     /**
295      * @brief Obtains the dragging data.
296      * @param dragData Indicates the dragging data.
297      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
298      * @since 10
299      */
300     int32_t GetDragData(DragData &dragData);
301 
302     /**
303      * @brief Obtains the current droping type.
304      * @param dragAction dropping type while user pressed ctrl or not.
305      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
306      * @since 10
307      */
308     int32_t GetDragAction(DragAction &dragAction);
309 
310     /**
311      * @brief Obtains the 'extraInfo' field in the drag data.
312      * @param extraInfo Indicates the 'extraInfo' field in the drag data, mainly to save whether to allow drag across
313      * the device "drag_allow_distributed" field.
314      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
315      * @since 10
316      */
317     int32_t GetExtraInfo(std::string &extraInfo);
318 
319     /**
320      * @brief Obtains the dragging source information.
321      * @param dragBundleInfo Output parameter, mainly to save the dragging source information.
322      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
323      * @since 20
324      */
325     int32_t GetDragBundleInfo(DragBundleInfo &dragBundleInfo);
326 
327     /**
328      * @brief Enable the internal animation.
329      * @param animationInfo Indicates the internal drop animation's configuration.
330      * @return 0 - Success.
331      *         202 - Called by no-system application
332      *         401 - Parameter error.
333      *         801 - Capability not support
334      *         -1  - Other error.
335      * @since 20
336      */
337     int32_t EnableInternalDropAnimation(const std::string &animationInfo);
338 
339     /**
340      * @brief Determine whether the current dragging state is DragState::START.
341      * @return true - The current dragging state is DragState::START.
342      *         false - The current dragging state is not DragState::START.
343      * @since 20
344      */
345     bool IsDragStart();
346 
347 #ifndef  OHOS_BUILD_ENABLE_ARKUI_X
348     /**
349      * @brief Registers a listener for screen hot area of the mouse pointer.
350      * @param listener Indicates the listener for screen hot area of the mouse pointer.
351      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
352      * @since 11
353      */
354     int32_t AddHotAreaListener(std::shared_ptr<IHotAreaListener> listener);
355 #endif // OHOS_BUILD_ENABLE_ARKUI_X
356     /**
357      * @brief Obtains the dragging state.
358      * @param dragState Dragging state.
359      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
360      * @since 10
361      */
362     int32_t GetDragState(DragState &dragState);
363 #ifndef  OHOS_BUILD_ENABLE_ARKUI_X
364     /**
365      * @brief Unregisters a listener for screen hot area of the mouse pointer.
366      * @param listener Indicates the listener for screen hot area of the mouse pointer.
367      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
368      * @since 9
369      */
370     int32_t RemoveHotAreaListener(std::shared_ptr<IHotAreaListener> listener = nullptr);
371 #endif // OHOS_BUILD_ENABLE_ARKUI_X
372 
373     /**
374      * @brief Update preview style when dragging.
375      * @param previewStyle Indicates the preview style param for dragged item.
376      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
377      * @since 11
378      */
379     int32_t UpdatePreviewStyle(const PreviewStyle &previewStyle);
380 
381     /**
382      * @brief Update preview style with animation when dragging.
383      * @param previewStyle Indicates the preview style param for dragged item.
384      * @param animation Indicates the animation param for dragged item.
385      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
386      * @since 11
387      */
388     int32_t UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle, const PreviewAnimation &animation);
389 
390 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
391     /**
392      * @brief Rotate drag window sync.
393      * @param rsTransaction Indicates utterances rotate the sync handle.
394      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
395      * @since 12
396      */
397     int32_t RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr);
398 
399     /**
400      * @brief Obtains data summary of the drag object.
401      * @param summarys Indicates data summary of the drag object.
402      * @param isJsCaller Indicates whether to add checking.
403      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
404      * @since 11
405      */
406     int32_t GetDragSummary(std::map<std::string, int64_t> &summarys, bool isJsCaller = false);
407 
408     /**
409      * @brief Sets the master switch for enhancing the drag capability.
410      * @param enable Switch state.
411      * @param isJsCaller Indicates whether to add checking.
412      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
413      * @since 15
414      */
415     int32_t SetDragSwitchState(bool enable, bool isJsCaller = false);
416 
417     /**
418      * @brief Sets the app switch for enhancing the drag capability.
419      * @param enable Switch state.
420      * @param pkgName App package name.
421      * @param isJsCaller Indicates whether to add checking.
422      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
423      * @since 15
424      */
425     int32_t SetAppDragSwitchState(bool enable, const std::string &pkgName, bool isJsCaller = false);
426 #else
427     /**
428      * @brief Obtains data summary of the drag object.
429      * @param summarys Indicates data summary of the drag object.
430      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
431      * @since 12
432      */
433     int32_t GetDragSummary(std::map<std::string, int64_t> &summarys);
434 #endif // OHOS_BUILD_ENABLE_ARKUI_X
435 
436     /**
437      * @brief Specifies whether to implement 8dp movement in the text editor area.
438      * @param enable Indicates whether to enable 8dp movement.
439      * The value <b>true</b> means to enable 8dp movement, and the value <b>false</b> means the opposite.
440      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
441      * @since 11
442      */
443     int32_t EnterTextEditorArea(bool enable);
444 
445     /**
446      * @brief Obtains data summary info of the drag object.
447      * @param dragSummaryInfo Output parameter, indicates data summary info of the drag object.
448      * @return 0 - Success.
449      *         202 - Called by no-system application
450      *         401 - Parameter error.
451      *         801 - Capability not support
452      *         -1  - Other error.
453      * @since 20
454      */
455     int32_t GetDragSummaryInfo(DragSummaryInfo &dragSummaryInfo);
456 
457 #ifndef OHOS_BUILD_ENABLE_ARKUI_X
458     int32_t AddPrivilege();
459 
460     int32_t EraseMouseIcon();
461 
462     int32_t SetDragWindowScreenId(uint64_t displayId, uint64_t screenId);
463 
464     int32_t SetMouseDragMonitorState(bool state);
465 
466     /**
467      * @brief Set drag state.
468      * @param state drag state, if application can drag, set true, else set false.
469      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
470      * @since 15
471      */
472     int32_t SetDraggableState(bool state);
473 
474     /**
475      * @brief Get the app switch for enhancing the drag capability.
476      * @param state Switch state.
477      * @return Returns <b>0</b> if the operation is successful; returns other values if the operation fails.
478      * @since 15
479      */
480     int32_t GetAppDragSwitchState(bool &state);
481 
482     /**
483      * @brief Set drag state asynchronous.
484      * @param state drag state, if application can drag, set true, else set false.
485      * @param downTime input down time.
486      * @return
487      * @since 15
488      */
489     void SetDraggableStateAsync(bool state, int64_t downTime);
490 #else
491     /**
492      * @brief convert relative pointerEvent action to PULL_MOVE or PULL_UP.
493      * @param pointerEvent the normal input event need to deal with.
494      * @return Returns <b>0</b> if the operation is successful; returns a non-zero value otherwise.
495      * @since 12
496      */
497     int32_t UpdatePointerAction(std::shared_ptr<MMI::PointerEvent> pointerEvent);
498 
499     /**
500      * @brief set window.
501      * @param window drag drawing needs window.
502      * @return
503      * @since 12
504      */
505     void SetDragWindow(std::shared_ptr<OHOS::Rosen::Window> window);
506 
507     /**
508      * @brief set callback to destroy window.
509      * @param cb callback function.
510      * @return
511      * @since 12
512      */
513     void RegisterDragWindow(std::function<void()> cb);
514 
515     /**
516      * @brief set VSG file path.
517      * @param filePath save SVG file path.
518      * @return
519      * @since 12
520      */
521     void SetSVGFilePath(const std::string &filePath);
522 #endif // OHOS_BUILD_ENABLE_ARKUI_X
523 
524 private:
525     DISALLOW_COPY_AND_MOVE(InteractionManager);
526     static std::shared_ptr<InteractionManager> instance_;
527     static std::mutex mutex_;
528 };
529 } // namespace DeviceStatus
530 } // namespace Msdp
531 } // namespace OHOS
532 
533 #define INTERACTION_MGR OHOS::Msdp::DeviceStatus::InteractionManager::GetInstance()
534 
535 #endif // INTERACTION_MANAGER_H
536