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