1/*
2 * Copyright (c) 2023-2024 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 DRAG_MANAGER_H
17#define DRAG_MANAGER_H
18
19#include <atomic>
20#include <string>
21
22#ifndef OHOS_BUILD_ENABLE_ARKUI_X
23#include "extra_data.h"
24#include "i_input_event_consumer.h"
25#include "input_manager.h"
26#endif // OHOS_BUILD_ENABLE_ARKUI_X
27#include "pixel_map.h"
28
29#ifndef OHOS_BUILD_ENABLE_ARKUI_X
30#include "collaboration_service_status_change.h"
31#include "display_change_event_listener.h"
32#endif // OHOS_BUILD_ENABLE_ARKUI_X
33#include "devicestatus_define.h"
34#include "drag_data.h"
35#include "drag_drawing.h"
36#include "id_factory.h"
37#ifndef OHOS_BUILD_ENABLE_ARKUI_X
38#include "event_hub.h"
39#include "i_context.h"
40#include "state_change_notify.h"
41#else
42#include "i_drag_manager.h"
43#endif // OHOS_BUILD_ENABLE_ARKUI_X
44
45namespace OHOS {
46namespace Msdp {
47namespace DeviceStatus {
48class DragManager : public IDragManager,
49                    public IdFactory<int32_t> {
50public:
51#ifdef OHOS_BUILD_ENABLE_ARKUI_X
52    static DragManager &GetInstance();
53#endif // OHOS_BUILD_ENABLE_ARKUI_X
54    DragManager() = default;
55    DISALLOW_COPY_AND_MOVE(DragManager);
56    ~DragManager();
57
58#ifndef OHOS_BUILD_ENABLE_ARKUI_X
59    int32_t Init(IContext* context);
60    void OnSessionLost(SocketSessionPtr session);
61    int32_t AddListener(int32_t pid) override;
62    int32_t RemoveListener(int32_t pid) override;
63    int32_t AddSubscriptListener(int32_t pid) override;
64    int32_t RemoveSubscriptListener(int32_t pid) override;
65    int32_t StartDrag(const DragData &dragData, int32_t pid, const std::string &peerNetId = "") override;
66#else
67    int32_t StartDrag(const DragData &dragData) override;
68    int32_t UpdatePointerAction(std::shared_ptr<MMI::PointerEvent> pointerEvent);
69#endif // OHOS_BUILD_ENABLE_ARKUI_X
70    int32_t StopDrag(const DragDropResult &dropResult, const std::string &packageName = "", int32_t pid = -1) override;
71    int32_t GetDragTargetPid() const override;
72    int32_t GetUdKey(std::string &udKey) const override;
73    void SendDragData(int32_t targetTid, const std::string &udKey);
74#ifndef OHOS_BUILD_ENABLE_ARKUI_X
75    int32_t UpdateDragStyle(
76        DragCursorStyle style, int32_t targetPid, int32_t targetTid, int32_t eventId = -1) override;
77#else
78    int32_t UpdateDragStyle(DragCursorStyle style) override;
79#endif // OHOS_BUILD_ENABLE_ARKUI_X
80    int32_t UpdateShadowPic(const ShadowInfo &shadowInfo) override;
81    int32_t GetDragData(DragData &dragData) override;
82    int32_t GetDragState(DragState &dragState) override;
83    DragCursorStyle GetDragStyle() const override;
84    void GetAllowDragState(bool &isAllowDrag) override;
85    void DragCallback(std::shared_ptr<MMI::PointerEvent> pointerEvent);
86    int32_t OnDragUp(std::shared_ptr<MMI::PointerEvent> pointerEvent);
87#ifndef OHOS_BUILD_ENABLE_ARKUI_X
88    void OnDragMove(std::shared_ptr<MMI::PointerEvent> pointerEvent);
89#else
90    int32_t OnDragMove(std::shared_ptr<MMI::PointerEvent> pointerEvent);
91#endif // OHOS_BUILD_ENABLE_ARKUI_X
92    int32_t OnSetDragWindowVisible(bool visible, bool isForce = false) override;
93    MMI::ExtraData GetExtraData(bool appended) const override;
94    int32_t OnGetShadowOffset(ShadowOffset &shadowOffset) override;
95#ifndef OHOS_BUILD_ENABLE_ARKUI_X
96    void Dump(int32_t fd) const override;
97    void RegisterStateChange(std::function<void(DragState)> callback) override;
98    void UnregisterStateChange() override;
99    void RegisterNotifyPullUp(std::function<void(bool)> callback) override;
100    void UnregisterNotifyPullUp() override;
101    void RegisterCrossDrag(std::function<void(bool)> callback) override;
102    void UnregisterCrossDrag() override;
103    void NotifyCrossDrag(bool isButtonDown) override;
104#endif // OHOS_BUILD_ENABLE_ARKUI_X
105    void SetPointerEventFilterTime(int64_t filterTime) override;
106    void MoveTo(int32_t x, int32_t y, bool isMultiSelectedAnimation = true) override;
107    DragResult GetDragResult() const override;
108    DragState GetDragState() const override;
109    void SetDragState(DragState state) override;
110    void SetDragOriginDpi(float dragOriginDpi) override;
111    int32_t UpdatePreviewStyle(const PreviewStyle &previewStyle) override;
112    int32_t UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle,
113        const PreviewAnimation &animation) override;
114    int32_t RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction>& rsTransaction = nullptr) override;
115    int32_t GetDragSummary(std::map<std::string, int64_t> &summarys) override;
116    void DragKeyEventCallback(std::shared_ptr<MMI::KeyEvent> keyEvent);
117    int32_t EnterTextEditorArea(bool enable) override;
118    int32_t GetDragAction(DragAction &dragAction) const override;
119    int32_t GetExtraInfo(std::string &extraInfo) const override;
120    int32_t AddPrivilege(int32_t tokenId) override;
121    int32_t EraseMouseIcon() override;
122#ifndef OHOS_BUILD_ENABLE_ARKUI_X
123    int32_t AddSelectedPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap) override;
124#endif // OHOS_BUILD_ENABLE_ARKUI_X
125    int32_t RotateDragWindow(Rosen::Rotation rotation) override;
126    int32_t ScreenRotate(Rosen::Rotation rotation, Rosen::Rotation lastRotation) override;
127    void SetDragWindowScreenId(uint64_t displayId, uint64_t screenId) override;
128#ifndef OHOS_BUILD_ENABLE_ARKUI_X
129    int32_t SetMouseDragMonitorState(bool state) override;
130#ifdef OHOS_DRAG_ENABLE_INTERCEPTOR
131    class InterceptorConsumer : public MMI::IInputEventConsumer {
132    public:
133        InterceptorConsumer(std::function<void (std::shared_ptr<MMI::PointerEvent>)> cb)
134            : pointerEventCallback_(cb) {}
135        void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override;
136        void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override;
137        void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override;
138    private:
139        std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointerEventCallback_ { nullptr };
140    };
141#endif // OHOS_DRAG_ENABLE_INTERCEPTOR
142
143#ifdef OHOS_DRAG_ENABLE_MONITOR
144    class MonitorConsumer : public MMI::IInputEventConsumer {
145    public:
146        explicit MonitorConsumer(
147            std::function<void (std::shared_ptr<MMI::PointerEvent>)> cb) : pointerEventCallback_(cb) {}
148        void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override;
149        void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override;
150        void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override;
151    private:
152        std::function<void (std::shared_ptr<MMI::PointerEvent>)> pointerEventCallback_;
153    };
154#endif //OHOS_DRAG_ENABLE_MONITOR
155#else
156    void SetDragWindow(std::shared_ptr<OHOS::Rosen::Window> window) override;
157    void AddDragDestroy(std::function<void()> cb) override;
158    void SetSVGFilePath(const std::string &filePath) override;
159#endif // OHOS_BUILD_ENABLE_ARKUI_X
160private:
161    void PrintDragData(const DragData &dragData, const std::string &packageName = "");
162#ifndef OHOS_BUILD_ENABLE_ARKUI_X
163    int32_t AddDragEventHandler(int32_t sourceType);
164    int32_t AddPointerEventHandler(uint32_t deviceTags);
165    int32_t AddKeyEventMonitor();
166    int32_t RemoveDragEventHandler();
167    int32_t RemoveKeyEventMonitor();
168    int32_t RemovePointerEventHandler();
169    int32_t NotifyDragResult(DragResult result, DragBehavior dragBehavior);
170    int32_t NotifyHideIcon();
171#endif // OHOS_BUILD_ENABLE_ARKUI_X
172    int32_t InitDataManager(const DragData &dragData) const;
173    int32_t OnStartDrag(const std::string &packageName = "", int32_t pid = -1);
174    int32_t OnStopDrag(DragResult result, bool hasCustomAnimation, const std::string &packageName = "",
175        int32_t pid = -1);
176    std::string GetDragState(DragState value) const;
177    std::string GetDragResult(DragResult value) const;
178    std::string GetDragCursorStyle(DragCursorStyle value) const;
179    static MMI::ExtraData CreateExtraData(bool appended);
180#ifndef OHOS_BUILD_ENABLE_ARKUI_X
181    void StateChangedNotify(DragState state);
182    int32_t AddDragEvent(const DragData &dragData, const std::string &packageName);
183#endif // OHOS_BUILD_ENABLE_ARKUI_X
184    void CtrlKeyStyleChangedNotify(DragCursorStyle style, DragAction action);
185    int32_t HandleDragResult(DragResult result, bool hasCustomAnimation);
186    void HandleCtrlKeyEvent(DragCursorStyle style, DragAction action);
187    int32_t OnUpdateDragStyle(DragCursorStyle style);
188    void UpdateDragStyleCross();
189    inline std::string GetDragStyleName(DragCursorStyle style);
190    DragCursorStyle GetRealDragStyle(DragCursorStyle style);
191    void GetDragBehavior(const DragDropResult &dropResult, DragBehavior &dragBehavior);
192#ifndef OHOS_BUILD_ENABLE_ARKUI_X
193    int32_t NotifyAddSelectedPixelMapResult(bool result);
194    void ResetMouseDragMonitorInfo();
195    void ResetMouseDragMonitorTimerId(const DragData &dragData);
196#endif // OHOS_BUILD_ENABLE_ARKUI_X
197#ifndef OHOS_BUILD_ENABLE_ARKUI_X
198    void ReportDragWindowVisibleRadarInfo(StageRes stageRes, DragRadarErrCode errCode, const std::string &funcName);
199    void ReportDragRadarInfo(struct DragRadarInfo &dragRadarInfo);
200    void ReportStartDragRadarInfo(BizState bizState, StageRes stageRes, DragRadarErrCode errCode,
201        const std::string &packageName, const std::string &peerNetId);
202    void ReportStopDragRadarInfo(BizState bizState, StageRes stageRes, DragRadarErrCode errCode, int32_t pid,
203        const std::string &packageName);
204    void ReportStartDragFailedRadarInfo(StageRes stageRes, DragRadarErrCode errCode, const std::string &funcName,
205        const std::string &packageName);
206#endif // OHOS_BUILD_ENABLE_ARKUI_X
207private:
208    int32_t timerId_ { -1 };
209    int32_t mouseDragMonitorTimerId_ { -1 };
210    DragState dragState_ { DragState::STOP };
211    DragResult dragResult_ { DragResult::DRAG_FAIL };
212    std::atomic<DragAction> dragAction_ { DragAction::MOVE };
213    DragDrawing dragDrawing_;
214    bool isControlMultiScreenVisible_ = false;
215    inline static std::atomic<int32_t> pullId_ { -1 };
216#ifndef OHOS_BUILD_ENABLE_ARKUI_X
217    StateChangeNotify stateNotify_;
218    int32_t keyEventMonitorId_ { -1 };
219    IContext* context_ { nullptr };
220#ifdef OHOS_DRAG_ENABLE_INTERCEPTOR
221    int32_t pointerEventInterceptorId_ { -1 };
222#endif // OHOS_DRAG_ENABLE_INTERCEPTOR
223#ifdef OHOS_DRAG_ENABLE_MONITOR
224    int32_t pointerEventMonitorId_ { -1 };
225#endif //OHOS_DRAG_ENABLE_MONITOR
226    SocketSessionPtr dragOutSession_ { nullptr };
227    std::function<void(DragState)> stateChangedCallback_ { nullptr };
228    std::function<void(bool)> notifyPUllUpCallback_ { nullptr };
229    std::function<void(bool)> crossDragCallback_ { nullptr };
230    std::shared_ptr<EventHub> eventHub_ { nullptr };
231    sptr<ISystemAbilityStatusChange> statusListener_ { nullptr };
232    sptr<ISystemAbilityStatusChange> displayAbilityStatusChange_ { nullptr };
233    sptr<ISystemAbilityStatusChange> appStateObserverStatusChange_ { nullptr };
234    sptr<ISystemAbilityStatusChange> CollaborationServiceStatusChange_ { nullptr };
235#endif // OHOS_BUILD_ENABLE_ARKUI_X
236    uint64_t displayId_ { 0 };
237    uint64_t screenId_ { 0 };
238    int32_t lastEventId_ { -1 };
239    int64_t mouseDragMonitorDisplayX_ { -1 };
240    int64_t mouseDragMonitorDisplayY_ { -1 };
241    bool mouseDragMonitorState_ { false };
242    bool existMouseMoveDragCallback_ { false };
243    int32_t lastDisplayId_ { -1 };
244};
245} // namespace DeviceStatus
246} // namespace Msdp
247} // namespace OHOS
248
249#ifdef OHOS_BUILD_ENABLE_ARKUI_X
250#define DRAG_MANAGER  OHOS::Msdp::DeviceStatus::DragManager::GetInstance()
251#endif // OHOS_BUILD_ENABLE_ARKUI_X
252#endif // DRAG_MANAGER_H
253