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