1 /*
2  * Copyright (c) 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 #include "intention_manager.h"
17 
18 #include "display_manager.h"
19 
20 #include "devicestatus_define.h"
21 #include "drag_data.h"
22 
23 #undef LOG_TAG
24 #define LOG_TAG "IntentionManager"
25 
26 namespace OHOS {
27 namespace Msdp {
28 namespace DeviceStatus {
29 namespace {
30 constexpr int32_t INDEX_MAIN { 0 };
31 constexpr int32_t INDEX_FULL { 1 };
32 constexpr size_t POLICY_VEC_SIZE { 2 };
33 const std::string SCREEN_ROTATION { "1" };
34 } // namespace
35 
IntentionManager()36 IntentionManager::IntentionManager()
37 {
38     tunnel_ = std::make_shared<TunnelClient>();
39 }
40 
~IntentionManager()41 IntentionManager::~IntentionManager()
42 {
43     client_.reset();
44 }
45 
InitClient()46 void IntentionManager::InitClient()
47 {
48     CALL_DEBUG_ENTER;
49     {
50         std::lock_guard<std::mutex> guard(mutex_);
51         if (client_ != nullptr) {
52             return;
53         }
54         client_ = std::make_unique<SocketClient>(tunnel_);
55         InitMsgHandler();
56         client_->RegisterConnectedFunction([this] {
57             this->OnConnected();
58         });
59         client_->RegisterDisconnectedFunction([this] {
60             this->OnDisconnected();
61         });
62         client_->Start();
63     }
64     GetRotatePolicy(isScreenRotation_, foldRotatePolicys_);
65 }
66 
InitMsgHandler()67 void IntentionManager::InitMsgHandler()
68 {
69     CALL_DEBUG_ENTER;
70     std::map<MessageId, std::function<int32_t(const StreamClient&, NetPacket&)>> funs {
71 #ifdef OHOS_BUILD_ENABLE_COORDINATION
72         {MessageId::COORDINATION_ADD_LISTENER, [this](const StreamClient &client, NetPacket &pkt) {
73             return this->cooperate_.OnCoordinationListener(client, pkt);
74         }},
75         {MessageId::COORDINATION_MESSAGE, [this](const StreamClient &client, NetPacket &pkt) {
76             return this->cooperate_.OnCoordinationMessage(client, pkt);
77         }},
78         {MessageId::COORDINATION_GET_STATE, [this](const StreamClient &client, NetPacket &pkt) {
79             return this->cooperate_.OnCoordinationState(client, pkt);
80         }},
81         {MessageId::HOT_AREA_ADD_LISTENER, [this](const StreamClient &client, NetPacket &pkt) {
82             return this->cooperate_.OnHotAreaListener(client, pkt);
83         }},
84         {MessageId::MOUSE_LOCATION_ADD_LISTENER, [this](const StreamClient &client, NetPacket &pkt) {
85             return this->cooperate_.OnMouseLocationListener(client, pkt);
86         }},
87 #endif // OHOS_BUILD_ENABLE_COORDINATION
88 
89         {MessageId::DRAG_NOTIFY_RESULT, [this](const StreamClient &client, NetPacket &pkt) {
90             return this->drag_.OnNotifyResult(client, pkt);
91         }},
92         {MessageId::DRAG_STATE_LISTENER, [this](const StreamClient &client, NetPacket &pkt) {
93             return this->drag_.OnStateChangedMessage(client, pkt);
94         }},
95         {MessageId::DRAG_NOTIFY_HIDE_ICON, [this](const StreamClient &client, NetPacket &pkt) {
96             return this->drag_.OnNotifyHideIcon(client, pkt);
97         }},
98         {MessageId::DRAG_STYLE_LISTENER, [this](const StreamClient &client, NetPacket &pkt) {
99             return this->drag_.OnDragStyleChangedMessage(client, pkt);
100         }}
101     };
102     CHKPV(client_);
103     for (auto &[id, cb] : funs) {
104         if (!client_->RegisterEvent(id, cb)) {
105             FI_HILOGI("RegistER event handler msg:%{public}d already exists", id);
106         }
107     }
108 }
109 
SubscribeCallback(Type type,ActivityEvent event,ReportLatencyNs latency,sptr<IRemoteDevStaCallback> callback)110 int32_t IntentionManager::SubscribeCallback(Type type, ActivityEvent event, ReportLatencyNs latency,
111     sptr<IRemoteDevStaCallback> callback)
112 {
113     return stationary_.SubscribeCallback(*tunnel_, type, event, latency, callback);
114 }
115 
UnsubscribeCallback(Type type,ActivityEvent event,sptr<IRemoteDevStaCallback> callback)116 int32_t IntentionManager::UnsubscribeCallback(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback)
117 {
118     return stationary_.UnsubscribeCallback(*tunnel_, type, event, callback);
119 }
120 
GetDeviceStatusData(const Type type)121 Data IntentionManager::GetDeviceStatusData(const Type type)
122 {
123     return stationary_.GetDeviceStatusData(*tunnel_, type);
124 }
125 
RegisterCoordinationListener(std::shared_ptr<ICoordinationListener> listener,bool isCompatible)126 int32_t IntentionManager::RegisterCoordinationListener(
127     std::shared_ptr<ICoordinationListener> listener, bool isCompatible)
128 {
129     CALL_INFO_TRACE;
130 #ifdef OHOS_BUILD_ENABLE_COORDINATION
131     InitClient();
132     return cooperate_.RegisterListener(*tunnel_, listener, isCompatible);
133 #else
134     FI_HILOGW("Coordination does not support");
135     (void)(listener);
136     (void)(isCompatible);
137     return ERROR_UNSUPPORT;
138 #endif // OHOS_BUILD_ENABLE_COORDINATION
139 }
140 
UnregisterCoordinationListener(std::shared_ptr<ICoordinationListener> listener,bool isCompatible)141 int32_t IntentionManager::UnregisterCoordinationListener(
142     std::shared_ptr<ICoordinationListener> listener, bool isCompatible)
143 {
144     CALL_INFO_TRACE;
145 #ifdef OHOS_BUILD_ENABLE_COORDINATION
146     return cooperate_.UnregisterListener(*tunnel_, listener, isCompatible);
147 #else
148     FI_HILOGW("Coordination does not support");
149     (void)(listener);
150     (void)(isCompatible);
151     return ERROR_UNSUPPORT;
152 #endif // OHOS_BUILD_ENABLE_COORDINATION
153 }
154 
PrepareCoordination(CooperateMsgInfoCallback callback,bool isCompatible)155 int32_t IntentionManager::PrepareCoordination(CooperateMsgInfoCallback callback, bool isCompatible)
156 {
157     CALL_INFO_TRACE;
158 #ifdef OHOS_BUILD_ENABLE_COORDINATION
159     InitClient();
160     return cooperate_.Enable(*tunnel_, callback, isCompatible);
161 #else
162     FI_HILOGW("Coordination does not support");
163     (void)(callback);
164     (void)(isCompatible);
165     return ERROR_UNSUPPORT;
166 #endif // OHOS_BUILD_ENABLE_COORDINATION
167 }
168 
UnprepareCoordination(CooperateMsgInfoCallback callback,bool isCompatible)169 int32_t IntentionManager::UnprepareCoordination(CooperateMsgInfoCallback callback, bool isCompatible)
170 {
171     CALL_INFO_TRACE;
172 #ifdef OHOS_BUILD_ENABLE_COORDINATION
173     InitClient();
174     return cooperate_.Disable(*tunnel_, callback, isCompatible);
175 #else
176     FI_HILOGW("Coordination does not support");
177     (void)(callback);
178     (void)(isCompatible);
179     return ERROR_UNSUPPORT;
180 #endif // OHOS_BUILD_ENABLE_COORDINATION
181 }
182 
ActivateCoordination(const std::string & remoteNetworkId,int32_t startDeviceId,CooperateMsgInfoCallback callback,bool isCompatible)183 int32_t IntentionManager::ActivateCoordination(const std::string &remoteNetworkId, int32_t startDeviceId,
184     CooperateMsgInfoCallback callback, bool isCompatible)
185 {
186     CALL_INFO_TRACE;
187 #ifdef OHOS_BUILD_ENABLE_COORDINATION
188     InitClient();
189     return cooperate_.Start(*tunnel_, remoteNetworkId, startDeviceId, callback, isCompatible);
190 #else
191     FI_HILOGW("Coordination does not support");
192     (void)(remoteNetworkId);
193     (void)(startDeviceId);
194     (void)(callback);
195     (void)(isCompatible);
196     return ERROR_UNSUPPORT;
197 #endif // OHOS_BUILD_ENABLE_COORDINATION
198 }
199 
DeactivateCoordination(bool isUnchained,CooperateMsgInfoCallback callback,bool isCompatible)200 int32_t IntentionManager::DeactivateCoordination(bool isUnchained,
201     CooperateMsgInfoCallback callback, bool isCompatible)
202 {
203     CALL_INFO_TRACE;
204 #ifdef OHOS_BUILD_ENABLE_COORDINATION
205     InitClient();
206     return cooperate_.Stop(*tunnel_, isUnchained, callback, isCompatible);
207 #else
208     FI_HILOGW("Coordination does not support");
209     (void)(callback);
210     (void)(isCompatible);
211     return ERROR_UNSUPPORT;
212 #endif // OHOS_BUILD_ENABLE_COORDINATION
213 }
214 
GetCoordinationState(const std::string & networkId,std::function<void (bool)> callback,bool isCompatible)215 int32_t IntentionManager::GetCoordinationState(
216     const std::string &networkId, std::function<void(bool)> callback, bool isCompatible)
217 {
218     CALL_INFO_TRACE;
219 #ifdef OHOS_BUILD_ENABLE_COORDINATION
220     InitClient();
221     return cooperate_.GetCooperateState(*tunnel_, networkId, callback, isCompatible);
222 #else
223     (void)(networkId);
224     (void)(callback);
225     (void)(isCompatible);
226     FI_HILOGW("Coordination does not support");
227     return ERROR_UNSUPPORT;
228 #endif // OHOS_BUILD_ENABLE_COORDINATION
229 }
230 
GetCoordinationState(const std::string & udId,bool & state)231 int32_t IntentionManager::GetCoordinationState(const std::string &udId, bool &state)
232 {
233     CALL_INFO_TRACE;
234 #ifdef OHOS_BUILD_ENABLE_COORDINATION
235     InitClient();
236     return cooperate_.GetCooperateState(*tunnel_, udId, state);
237 #else
238     (void)(udId);
239     (void)(state);
240     FI_HILOGW("Coordination does not support");
241     return ERROR_UNSUPPORT;
242 #endif // OHOS_BUILD_ENABLE_COORDINATION
243 }
244 
RegisterEventListener(const std::string & networkId,std::shared_ptr<IEventListener> listener)245 int32_t IntentionManager::RegisterEventListener(const std::string &networkId, std::shared_ptr<IEventListener> listener)
246 {
247     CALL_INFO_TRACE;
248 #ifdef OHOS_BUILD_ENABLE_COORDINATION
249     InitClient();
250     return cooperate_.RegisterEventListener(*tunnel_, networkId, listener);
251 #else
252     (void)(networkId);
253     (void)(listener);
254     FI_HILOGW("Coordination does not support");
255     return ERROR_UNSUPPORT;
256 #endif // OHOS_BUILD_ENABLE_COORDINATION
257 }
258 
UnregisterEventListener(const std::string & networkId,std::shared_ptr<IEventListener> listener)259 int32_t IntentionManager::UnregisterEventListener(const std::string &networkId,
260     std::shared_ptr<IEventListener> listener)
261 {
262     CALL_INFO_TRACE;
263 #ifdef OHOS_BUILD_ENABLE_COORDINATION
264     InitClient();
265     return cooperate_.UnregisterEventListener(*tunnel_, networkId, listener);
266 #else
267     (void)(networkId);
268     (void)(listener);
269     FI_HILOGW("Coordination does not support");
270     return ERROR_UNSUPPORT;
271 #endif // OHOS_BUILD_ENABLE_COORDINATION
272 }
273 
UpdateDragStyle(DragCursorStyle style,int32_t eventId)274 int32_t IntentionManager::UpdateDragStyle(DragCursorStyle style, int32_t eventId)
275 {
276     CALL_DEBUG_ENTER;
277     return drag_.UpdateDragStyle(*tunnel_, style, eventId);
278 }
279 
StartDrag(const DragData & dragData,std::shared_ptr<IStartDragListener> listener)280 int32_t IntentionManager::StartDrag(const DragData &dragData, std::shared_ptr<IStartDragListener> listener)
281 {
282     CALL_DEBUG_ENTER;
283     InitClient();
284     return drag_.StartDrag(*tunnel_, dragData, listener);
285 }
286 
StopDrag(const DragDropResult & dropResult)287 int32_t IntentionManager::StopDrag(const DragDropResult &dropResult)
288 {
289     CALL_DEBUG_ENTER;
290     return drag_.StopDrag(*tunnel_, dropResult);
291 }
292 
GetDragTargetPid()293 int32_t IntentionManager::GetDragTargetPid()
294 {
295     CALL_DEBUG_ENTER;
296     return drag_.GetDragTargetPid(*tunnel_);
297 }
298 
GetUdKey(std::string & udKey)299 int32_t IntentionManager::GetUdKey(std::string &udKey)
300 {
301     CALL_DEBUG_ENTER;
302     return drag_.GetUdKey(*tunnel_, udKey);
303 }
304 
AddDraglistener(DragListenerPtr listener,bool isJsCaller)305 int32_t IntentionManager::AddDraglistener(DragListenerPtr listener, bool isJsCaller)
306 {
307     CALL_DEBUG_ENTER;
308     InitClient();
309     return drag_.AddDraglistener(*tunnel_, listener, isJsCaller);
310 }
311 
RemoveDraglistener(DragListenerPtr listener,bool isJsCaller)312 int32_t IntentionManager::RemoveDraglistener(DragListenerPtr listener, bool isJsCaller)
313 {
314     CALL_DEBUG_ENTER;
315     return drag_.RemoveDraglistener(*tunnel_, listener, isJsCaller);
316 }
317 
AddSubscriptListener(SubscriptListenerPtr listener)318 int32_t IntentionManager::AddSubscriptListener(SubscriptListenerPtr listener)
319 {
320     CALL_DEBUG_ENTER;
321     InitClient();
322     return drag_.AddSubscriptListener(*tunnel_, listener);
323 }
324 
RemoveSubscriptListener(SubscriptListenerPtr listener)325 int32_t IntentionManager::RemoveSubscriptListener(SubscriptListenerPtr listener)
326 {
327     CALL_DEBUG_ENTER;
328     return drag_.RemoveSubscriptListener(*tunnel_, listener);
329 }
330 
SetDragWindowVisible(bool visible,bool isForce)331 int32_t IntentionManager::SetDragWindowVisible(bool visible, bool isForce)
332 {
333     CALL_DEBUG_ENTER;
334     return drag_.SetDragWindowVisible(*tunnel_, visible, isForce);
335 }
336 
GetShadowOffset(ShadowOffset & shadowOffset)337 int32_t IntentionManager::GetShadowOffset(ShadowOffset &shadowOffset)
338 {
339     CALL_DEBUG_ENTER;
340     return drag_.GetShadowOffset(*tunnel_, shadowOffset);
341 }
342 
UpdateShadowPic(const ShadowInfo & shadowInfo)343 int32_t IntentionManager::UpdateShadowPic(const ShadowInfo &shadowInfo)
344 {
345     CALL_DEBUG_ENTER;
346     return drag_.UpdateShadowPic(*tunnel_, shadowInfo);
347 }
348 
GetDragData(DragData & dragData)349 int32_t IntentionManager::GetDragData(DragData &dragData)
350 {
351     CALL_DEBUG_ENTER;
352     return drag_.GetDragData(*tunnel_, dragData);
353 }
354 
GetDragState(DragState & dragState)355 int32_t IntentionManager::GetDragState(DragState &dragState)
356 {
357     CALL_DEBUG_ENTER;
358     return drag_.GetDragState(*tunnel_, dragState);
359 }
360 
GetDragAction(DragAction & dragAction)361 int32_t IntentionManager::GetDragAction(DragAction &dragAction)
362 {
363     CALL_DEBUG_ENTER;
364     return drag_.GetDragAction(*tunnel_, dragAction);
365 }
366 
GetExtraInfo(std::string & extraInfo)367 int32_t IntentionManager::GetExtraInfo(std::string &extraInfo)
368 {
369     CALL_DEBUG_ENTER;
370     return drag_.GetExtraInfo(*tunnel_, extraInfo);
371 }
372 
AddHotAreaListener(std::shared_ptr<IHotAreaListener> listener)373 int32_t IntentionManager::AddHotAreaListener(std::shared_ptr<IHotAreaListener> listener)
374 {
375     CALL_DEBUG_ENTER;
376 #ifdef OHOS_BUILD_ENABLE_COORDINATION
377     InitClient();
378     return cooperate_.AddHotAreaListener(*tunnel_, listener);
379 #else
380     FI_HILOGW("Coordination does not support");
381     (void)(listener);
382     return ERROR_UNSUPPORT;
383 #endif // OHOS_BUILD_ENABLE_COORDINATION
384 }
385 
RemoveHotAreaListener(std::shared_ptr<IHotAreaListener> listener)386 int32_t IntentionManager::RemoveHotAreaListener(std::shared_ptr<IHotAreaListener> listener)
387 {
388     CALL_DEBUG_ENTER;
389 #ifdef OHOS_BUILD_ENABLE_COORDINATION
390     return cooperate_.RemoveHotAreaListener(*tunnel_, listener);
391 #else
392     FI_HILOGW("Coordination does not support");
393     (void)(listener);
394     return ERROR_UNSUPPORT;
395 #endif // OHOS_BUILD_ENABLE_COORDINATION
396 }
397 
UpdatePreviewStyle(const PreviewStyle & previewStyle)398 int32_t IntentionManager::UpdatePreviewStyle(const PreviewStyle &previewStyle)
399 {
400     CALL_DEBUG_ENTER;
401     return drag_.UpdatePreviewStyle(*tunnel_, previewStyle);
402 }
403 
UpdatePreviewStyleWithAnimation(const PreviewStyle & previewStyle,const PreviewAnimation & animation)404 int32_t IntentionManager::UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle,
405     const PreviewAnimation &animation)
406 {
407     CALL_DEBUG_ENTER;
408     return drag_.UpdatePreviewStyleWithAnimation(*tunnel_, previewStyle, animation);
409 }
410 
RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction> & rsTransaction)411 int32_t IntentionManager::RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
412 {
413     CALL_DEBUG_ENTER;
414     if (isScreenRotation_) {
415         FI_HILOGW("Screen rotation, not need rotate drag window");
416         return RET_OK;
417     }
418     if (Rosen::DisplayManager::GetInstance().IsFoldable()) {
419         if ((foldRotatePolicys_.empty()) || (foldRotatePolicys_.size() < POLICY_VEC_SIZE)) {
420             FI_HILOGE("foldRotatePolicys_ is invalid");
421             return drag_.RotateDragWindowSync(*tunnel_, rsTransaction);
422         }
423         if (Rosen::DisplayManager::GetInstance().GetFoldDisplayMode() == Rosen::FoldDisplayMode::FULL) {
424             if (foldRotatePolicys_[INDEX_FULL] == SCREEN_ROTATION) {
425                 FI_HILOGD("Full display rotation, not need rotate drag window");
426                 return RET_OK;
427             }
428         } else if (Rosen::DisplayManager::GetInstance().GetFoldDisplayMode() == Rosen::FoldDisplayMode::MAIN) {
429             if (foldRotatePolicys_[INDEX_MAIN] == SCREEN_ROTATION) {
430                 FI_HILOGD("Main display rotation, not need rotate drag window");
431                 return RET_OK;
432             }
433         }
434     }
435     return drag_.RotateDragWindowSync(*tunnel_, rsTransaction);
436 }
437 
SetDragWindowScreenId(uint64_t displayId,uint64_t screenId)438 int32_t IntentionManager::SetDragWindowScreenId(uint64_t displayId, uint64_t screenId)
439 {
440     CALL_DEBUG_ENTER;
441     return drag_.SetDragWindowScreenId(*tunnel_, displayId, screenId);
442 }
443 
GetDragSummary(std::map<std::string,int64_t> & summarys,bool isJsCaller)444 int32_t IntentionManager::GetDragSummary(std::map<std::string, int64_t> &summarys, bool isJsCaller)
445 {
446     CALL_DEBUG_ENTER;
447     return drag_.GetDragSummary(*tunnel_, summarys, isJsCaller);
448 }
449 
EnterTextEditorArea(bool enable)450 int32_t IntentionManager::EnterTextEditorArea(bool enable)
451 {
452     CALL_DEBUG_ENTER;
453     return drag_.EnableUpperCenterMode(*tunnel_, enable);
454 }
455 
AddPrivilege()456 int32_t IntentionManager::AddPrivilege()
457 {
458     CALL_DEBUG_ENTER;
459     return drag_.AddPrivilege(*tunnel_);
460 }
461 
EraseMouseIcon()462 int32_t IntentionManager::EraseMouseIcon()
463 {
464     CALL_DEBUG_ENTER;
465     return drag_.EraseMouseIcon(*tunnel_);
466 }
467 
SetMouseDragMonitorState(bool state)468 int32_t IntentionManager::SetMouseDragMonitorState(bool state)
469 {
470     CALL_DEBUG_ENTER;
471     return drag_.SetMouseDragMonitorState(*tunnel_, state);
472 }
473 
OnConnected()474 void IntentionManager::OnConnected()
475 {
476     CALL_DEBUG_ENTER;
477     CHKPV(tunnel_);
478     drag_.OnConnected(*tunnel_);
479 }
480 
OnDisconnected()481 void IntentionManager::OnDisconnected()
482 {
483     CALL_DEBUG_ENTER;
484     CHKPV(tunnel_);
485     drag_.OnDisconnected(*tunnel_);
486 }
487 } // namespace DeviceStatus
488 } // namespace Msdp
489 } // namespace OHOS
490