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_FOLDED { 0 };
31 constexpr int32_t INDEX_EXPAND { 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         {MessageId::ADD_SELECTED_PIXELMAP_RESULT, [this](const StreamClient &client, NetPacket &pkt) {
102             return this->drag_.OnAddSelectedPixelMapResult(client, pkt);
103         }}
104     };
105     CHKPV(client_);
106     for (auto &[id, cb] : funs) {
107         if (!client_->RegisterEvent(id, cb)) {
108             FI_HILOGI("RegistER event handler msg:%{public}d already exists", id);
109         }
110     }
111 }
112 
SubscribeCallback(Type type, ActivityEvent event, ReportLatencyNs latency, sptr<IRemoteDevStaCallback> callback)113 int32_t IntentionManager::SubscribeCallback(Type type, ActivityEvent event, ReportLatencyNs latency,
114     sptr<IRemoteDevStaCallback> callback)
115 {
116     return stationary_.SubscribeCallback(*tunnel_, type, event, latency, callback);
117 }
118 
UnsubscribeCallback(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback)119 int32_t IntentionManager::UnsubscribeCallback(Type type, ActivityEvent event, sptr<IRemoteDevStaCallback> callback)
120 {
121     return stationary_.UnsubscribeCallback(*tunnel_, type, event, callback);
122 }
123 
GetDeviceStatusData(const Type type)124 Data IntentionManager::GetDeviceStatusData(const Type type)
125 {
126     return stationary_.GetDeviceStatusData(*tunnel_, type);
127 }
128 
RegisterCoordinationListener( std::shared_ptr<ICoordinationListener> listener, bool isCompatible)129 int32_t IntentionManager::RegisterCoordinationListener(
130     std::shared_ptr<ICoordinationListener> listener, bool isCompatible)
131 {
132     CALL_INFO_TRACE;
133 #ifdef OHOS_BUILD_ENABLE_COORDINATION
134     InitClient();
135     return cooperate_.RegisterListener(*tunnel_, listener, isCompatible);
136 #else
137     FI_HILOGW("Coordination does not support");
138     (void)(listener);
139     (void)(isCompatible);
140     return ERROR_UNSUPPORT;
141 #endif // OHOS_BUILD_ENABLE_COORDINATION
142 }
143 
UnregisterCoordinationListener( std::shared_ptr<ICoordinationListener> listener, bool isCompatible)144 int32_t IntentionManager::UnregisterCoordinationListener(
145     std::shared_ptr<ICoordinationListener> listener, bool isCompatible)
146 {
147     CALL_INFO_TRACE;
148 #ifdef OHOS_BUILD_ENABLE_COORDINATION
149     return cooperate_.UnregisterListener(*tunnel_, listener, isCompatible);
150 #else
151     FI_HILOGW("Coordination does not support");
152     (void)(listener);
153     (void)(isCompatible);
154     return ERROR_UNSUPPORT;
155 #endif // OHOS_BUILD_ENABLE_COORDINATION
156 }
157 
PrepareCoordination(CooperateMsgInfoCallback callback, bool isCompatible)158 int32_t IntentionManager::PrepareCoordination(CooperateMsgInfoCallback callback, bool isCompatible)
159 {
160     CALL_INFO_TRACE;
161 #ifdef OHOS_BUILD_ENABLE_COORDINATION
162     InitClient();
163     return cooperate_.Enable(*tunnel_, callback, isCompatible);
164 #else
165     FI_HILOGW("Coordination does not support");
166     (void)(callback);
167     (void)(isCompatible);
168     return ERROR_UNSUPPORT;
169 #endif // OHOS_BUILD_ENABLE_COORDINATION
170 }
171 
UnprepareCoordination(CooperateMsgInfoCallback callback, bool isCompatible)172 int32_t IntentionManager::UnprepareCoordination(CooperateMsgInfoCallback callback, bool isCompatible)
173 {
174     CALL_INFO_TRACE;
175 #ifdef OHOS_BUILD_ENABLE_COORDINATION
176     InitClient();
177     return cooperate_.Disable(*tunnel_, callback, isCompatible);
178 #else
179     FI_HILOGW("Coordination does not support");
180     (void)(callback);
181     (void)(isCompatible);
182     return ERROR_UNSUPPORT;
183 #endif // OHOS_BUILD_ENABLE_COORDINATION
184 }
185 
ActivateCoordination(const std::string &remoteNetworkId, int32_t startDeviceId, CooperateMsgInfoCallback callback, bool isCompatible)186 int32_t IntentionManager::ActivateCoordination(const std::string &remoteNetworkId, int32_t startDeviceId,
187     CooperateMsgInfoCallback callback, bool isCompatible)
188 {
189     CALL_INFO_TRACE;
190 #ifdef OHOS_BUILD_ENABLE_COORDINATION
191     InitClient();
192     return cooperate_.Start(*tunnel_, remoteNetworkId, startDeviceId, callback, isCompatible);
193 #else
194     FI_HILOGW("Coordination does not support");
195     (void)(remoteNetworkId);
196     (void)(startDeviceId);
197     (void)(callback);
198     (void)(isCompatible);
199     return ERROR_UNSUPPORT;
200 #endif // OHOS_BUILD_ENABLE_COORDINATION
201 }
202 
DeactivateCoordination(bool isUnchained, CooperateMsgInfoCallback callback, bool isCompatible)203 int32_t IntentionManager::DeactivateCoordination(bool isUnchained,
204     CooperateMsgInfoCallback callback, bool isCompatible)
205 {
206     CALL_INFO_TRACE;
207 #ifdef OHOS_BUILD_ENABLE_COORDINATION
208     InitClient();
209     return cooperate_.Stop(*tunnel_, isUnchained, callback, isCompatible);
210 #else
211     FI_HILOGW("Coordination does not support");
212     (void)(callback);
213     (void)(isCompatible);
214     return ERROR_UNSUPPORT;
215 #endif // OHOS_BUILD_ENABLE_COORDINATION
216 }
217 
GetCoordinationState( const std::string &networkId, std::function<void(bool)> callback, bool isCompatible)218 int32_t IntentionManager::GetCoordinationState(
219     const std::string &networkId, std::function<void(bool)> callback, bool isCompatible)
220 {
221     CALL_INFO_TRACE;
222 #ifdef OHOS_BUILD_ENABLE_COORDINATION
223     InitClient();
224     return cooperate_.GetCooperateState(*tunnel_, networkId, callback, isCompatible);
225 #else
226     (void)(networkId);
227     (void)(callback);
228     (void)(isCompatible);
229     FI_HILOGW("Coordination does not support");
230     return ERROR_UNSUPPORT;
231 #endif // OHOS_BUILD_ENABLE_COORDINATION
232 }
233 
GetCoordinationState(const std::string &udId, bool &state)234 int32_t IntentionManager::GetCoordinationState(const std::string &udId, bool &state)
235 {
236     CALL_INFO_TRACE;
237 #ifdef OHOS_BUILD_ENABLE_COORDINATION
238     InitClient();
239     return cooperate_.GetCooperateState(*tunnel_, udId, state);
240 #else
241     (void)(udId);
242     (void)(state);
243     FI_HILOGW("Coordination does not support");
244     return ERROR_UNSUPPORT;
245 #endif // OHOS_BUILD_ENABLE_COORDINATION
246 }
247 
RegisterEventListener(const std::string &networkId, std::shared_ptr<IEventListener> listener)248 int32_t IntentionManager::RegisterEventListener(const std::string &networkId, std::shared_ptr<IEventListener> listener)
249 {
250     CALL_INFO_TRACE;
251 #ifdef OHOS_BUILD_ENABLE_COORDINATION
252     InitClient();
253     return cooperate_.RegisterEventListener(*tunnel_, networkId, listener);
254 #else
255     (void)(networkId);
256     (void)(listener);
257     FI_HILOGW("Coordination does not support");
258     return ERROR_UNSUPPORT;
259 #endif // OHOS_BUILD_ENABLE_COORDINATION
260 }
261 
UnregisterEventListener(const std::string &networkId, std::shared_ptr<IEventListener> listener)262 int32_t IntentionManager::UnregisterEventListener(const std::string &networkId,
263     std::shared_ptr<IEventListener> listener)
264 {
265     CALL_INFO_TRACE;
266 #ifdef OHOS_BUILD_ENABLE_COORDINATION
267     InitClient();
268     return cooperate_.UnregisterEventListener(*tunnel_, networkId, listener);
269 #else
270     (void)(networkId);
271     (void)(listener);
272     FI_HILOGW("Coordination does not support");
273     return ERROR_UNSUPPORT;
274 #endif // OHOS_BUILD_ENABLE_COORDINATION
275 }
276 
SetDamplingCoefficient(uint32_t direction, double coefficient)277 int32_t IntentionManager::SetDamplingCoefficient(uint32_t direction, double coefficient)
278 {
279     CALL_INFO_TRACE;
280 #ifdef OHOS_BUILD_ENABLE_COORDINATION
281     InitClient();
282     return cooperate_.SetDamplingCoefficient(*tunnel_, direction, coefficient);
283 #else
284     (void)(direction);
285     (void)(coefficient);
286     FI_HILOGW("Coordination does not support");
287     return ERROR_UNSUPPORT;
288 #endif // OHOS_BUILD_ENABLE_COORDINATION
289 }
290 
UpdateDragStyle(DragCursorStyle style, int32_t eventId)291 int32_t IntentionManager::UpdateDragStyle(DragCursorStyle style, int32_t eventId)
292 {
293     CALL_DEBUG_ENTER;
294     return drag_.UpdateDragStyle(*tunnel_, style, eventId);
295 }
296 
StartDrag(const DragData &dragData, std::shared_ptr<IStartDragListener> listener)297 int32_t IntentionManager::StartDrag(const DragData &dragData, std::shared_ptr<IStartDragListener> listener)
298 {
299     CALL_DEBUG_ENTER;
300     InitClient();
301     return drag_.StartDrag(*tunnel_, dragData, listener);
302 }
303 
StopDrag(const DragDropResult &dropResult)304 int32_t IntentionManager::StopDrag(const DragDropResult &dropResult)
305 {
306     CALL_DEBUG_ENTER;
307     return drag_.StopDrag(*tunnel_, dropResult);
308 }
309 
GetDragTargetPid()310 int32_t IntentionManager::GetDragTargetPid()
311 {
312     CALL_DEBUG_ENTER;
313     return drag_.GetDragTargetPid(*tunnel_);
314 }
315 
GetUdKey(std::string &udKey)316 int32_t IntentionManager::GetUdKey(std::string &udKey)
317 {
318     CALL_DEBUG_ENTER;
319     return drag_.GetUdKey(*tunnel_, udKey);
320 }
321 
AddDraglistener(DragListenerPtr listener, bool isJsCaller)322 int32_t IntentionManager::AddDraglistener(DragListenerPtr listener, bool isJsCaller)
323 {
324     CALL_DEBUG_ENTER;
325     InitClient();
326     return drag_.AddDraglistener(*tunnel_, listener, isJsCaller);
327 }
328 
RemoveDraglistener(DragListenerPtr listener, bool isJsCaller)329 int32_t IntentionManager::RemoveDraglistener(DragListenerPtr listener, bool isJsCaller)
330 {
331     CALL_DEBUG_ENTER;
332     return drag_.RemoveDraglistener(*tunnel_, listener, isJsCaller);
333 }
334 
AddSubscriptListener(SubscriptListenerPtr listener)335 int32_t IntentionManager::AddSubscriptListener(SubscriptListenerPtr listener)
336 {
337     CALL_DEBUG_ENTER;
338     InitClient();
339     return drag_.AddSubscriptListener(*tunnel_, listener);
340 }
341 
RemoveSubscriptListener(SubscriptListenerPtr listener)342 int32_t IntentionManager::RemoveSubscriptListener(SubscriptListenerPtr listener)
343 {
344     CALL_DEBUG_ENTER;
345     return drag_.RemoveSubscriptListener(*tunnel_, listener);
346 }
347 
SetDragWindowVisible(bool visible, bool isForce)348 int32_t IntentionManager::SetDragWindowVisible(bool visible, bool isForce)
349 {
350     CALL_DEBUG_ENTER;
351     return drag_.SetDragWindowVisible(*tunnel_, visible, isForce);
352 }
353 
GetShadowOffset(ShadowOffset &shadowOffset)354 int32_t IntentionManager::GetShadowOffset(ShadowOffset &shadowOffset)
355 {
356     CALL_DEBUG_ENTER;
357     return drag_.GetShadowOffset(*tunnel_, shadowOffset);
358 }
359 
UpdateShadowPic(const ShadowInfo &shadowInfo)360 int32_t IntentionManager::UpdateShadowPic(const ShadowInfo &shadowInfo)
361 {
362     CALL_DEBUG_ENTER;
363     return drag_.UpdateShadowPic(*tunnel_, shadowInfo);
364 }
365 
GetDragData(DragData &dragData)366 int32_t IntentionManager::GetDragData(DragData &dragData)
367 {
368     CALL_DEBUG_ENTER;
369     return drag_.GetDragData(*tunnel_, dragData);
370 }
371 
GetDragState(DragState &dragState)372 int32_t IntentionManager::GetDragState(DragState &dragState)
373 {
374     CALL_DEBUG_ENTER;
375     return drag_.GetDragState(*tunnel_, dragState);
376 }
377 
GetDragAction(DragAction &dragAction)378 int32_t IntentionManager::GetDragAction(DragAction &dragAction)
379 {
380     CALL_DEBUG_ENTER;
381     return drag_.GetDragAction(*tunnel_, dragAction);
382 }
383 
GetExtraInfo(std::string &extraInfo)384 int32_t IntentionManager::GetExtraInfo(std::string &extraInfo)
385 {
386     CALL_DEBUG_ENTER;
387     return drag_.GetExtraInfo(*tunnel_, extraInfo);
388 }
389 
AddHotAreaListener(std::shared_ptr<IHotAreaListener> listener)390 int32_t IntentionManager::AddHotAreaListener(std::shared_ptr<IHotAreaListener> listener)
391 {
392     CALL_DEBUG_ENTER;
393 #ifdef OHOS_BUILD_ENABLE_COORDINATION
394     InitClient();
395     return cooperate_.AddHotAreaListener(*tunnel_, listener);
396 #else
397     FI_HILOGW("Coordination does not support");
398     (void)(listener);
399     return ERROR_UNSUPPORT;
400 #endif // OHOS_BUILD_ENABLE_COORDINATION
401 }
402 
RemoveHotAreaListener(std::shared_ptr<IHotAreaListener> listener)403 int32_t IntentionManager::RemoveHotAreaListener(std::shared_ptr<IHotAreaListener> listener)
404 {
405     CALL_DEBUG_ENTER;
406 #ifdef OHOS_BUILD_ENABLE_COORDINATION
407     return cooperate_.RemoveHotAreaListener(*tunnel_, listener);
408 #else
409     FI_HILOGW("Coordination does not support");
410     (void)(listener);
411     return ERROR_UNSUPPORT;
412 #endif // OHOS_BUILD_ENABLE_COORDINATION
413 }
414 
UpdatePreviewStyle(const PreviewStyle &previewStyle)415 int32_t IntentionManager::UpdatePreviewStyle(const PreviewStyle &previewStyle)
416 {
417     CALL_DEBUG_ENTER;
418     return drag_.UpdatePreviewStyle(*tunnel_, previewStyle);
419 }
420 
UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle, const PreviewAnimation &animation)421 int32_t IntentionManager::UpdatePreviewStyleWithAnimation(const PreviewStyle &previewStyle,
422     const PreviewAnimation &animation)
423 {
424     CALL_DEBUG_ENTER;
425     return drag_.UpdatePreviewStyleWithAnimation(*tunnel_, previewStyle, animation);
426 }
427 
RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)428 int32_t IntentionManager::RotateDragWindowSync(const std::shared_ptr<Rosen::RSTransaction>& rsTransaction)
429 {
430     CALL_DEBUG_ENTER;
431     if (isScreenRotation_) {
432         FI_HILOGW("Screen rotation, not need rotate drag window");
433         return RET_OK;
434     }
435     if (Rosen::DisplayManager::GetInstance().IsFoldable()) {
436         if ((foldRotatePolicys_.empty()) || (foldRotatePolicys_.size() < POLICY_VEC_SIZE)) {
437             FI_HILOGE("foldRotatePolicys_ is invalid");
438             return drag_.RotateDragWindowSync(*tunnel_, rsTransaction);
439         }
440         Rosen::FoldStatus foldStatus = Rosen::DisplayManager::GetInstance().GetFoldStatus();
441         if (((foldStatus == Rosen::FoldStatus::EXPAND) && (foldRotatePolicys_[INDEX_EXPAND] == SCREEN_ROTATION)) ||
442             ((foldStatus == Rosen::FoldStatus::FOLDED) && (foldRotatePolicys_[INDEX_FOLDED] == SCREEN_ROTATION))) {
443             FI_HILOGD("Full display rotation, not need rotate drag window");
444             return RET_OK;
445         }
446     }
447     return drag_.RotateDragWindowSync(*tunnel_, rsTransaction);
448 }
449 
SetDragWindowScreenId(uint64_t displayId, uint64_t screenId)450 int32_t IntentionManager::SetDragWindowScreenId(uint64_t displayId, uint64_t screenId)
451 {
452     CALL_DEBUG_ENTER;
453     return drag_.SetDragWindowScreenId(*tunnel_, displayId, screenId);
454 }
455 
GetDragSummary(std::map<std::string, int64_t> &summarys, bool isJsCaller)456 int32_t IntentionManager::GetDragSummary(std::map<std::string, int64_t> &summarys, bool isJsCaller)
457 {
458     CALL_DEBUG_ENTER;
459     return drag_.GetDragSummary(*tunnel_, summarys, isJsCaller);
460 }
461 
EnterTextEditorArea(bool enable)462 int32_t IntentionManager::EnterTextEditorArea(bool enable)
463 {
464     CALL_DEBUG_ENTER;
465     return drag_.EnableUpperCenterMode(*tunnel_, enable);
466 }
467 
AddPrivilege()468 int32_t IntentionManager::AddPrivilege()
469 {
470     CALL_DEBUG_ENTER;
471     return drag_.AddPrivilege(*tunnel_);
472 }
473 
EraseMouseIcon()474 int32_t IntentionManager::EraseMouseIcon()
475 {
476     CALL_DEBUG_ENTER;
477     return drag_.EraseMouseIcon(*tunnel_);
478 }
479 
SetMouseDragMonitorState(bool state)480 int32_t IntentionManager::SetMouseDragMonitorState(bool state)
481 {
482     CALL_DEBUG_ENTER;
483     return drag_.SetMouseDragMonitorState(*tunnel_, state);
484 }
485 
AddSelectedPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap, std::function<void(bool)> callback)486 int32_t IntentionManager::AddSelectedPixelMap(std::shared_ptr<OHOS::Media::PixelMap> pixelMap,
487     std::function<void(bool)> callback)
488 {
489     CALL_DEBUG_ENTER;
490     return drag_.AddSelectedPixelMap(*tunnel_, pixelMap, callback);
491 }
492 
OnConnected()493 void IntentionManager::OnConnected()
494 {
495     CALL_DEBUG_ENTER;
496     CHKPV(tunnel_);
497     drag_.OnConnected(*tunnel_);
498 }
499 
OnDisconnected()500 void IntentionManager::OnDisconnected()
501 {
502     CALL_DEBUG_ENTER;
503     CHKPV(tunnel_);
504     drag_.OnDisconnected(*tunnel_);
505 }
506 } // namespace DeviceStatus
507 } // namespace Msdp
508 } // namespace OHOS
509