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 "state_change_notify.h"
17
18 #include "devicestatus_define.h"
19
20 namespace OHOS {
21 namespace Msdp {
22 namespace DeviceStatus {
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "StateChangeNotify" };
25 } // namespace
26 template void StateChangeNotify::OnDragInfoNotify<DragState>(SessionPtr session, MessageId msgId, DragState state);
27 template void StateChangeNotify::OnDragInfoNotify<DragCursorStyle>(SessionPtr session, MessageId msgId,
28 DragCursorStyle style);
29
AddNotifyMsg(std::shared_ptr<MessageInfo> info)30 void StateChangeNotify::AddNotifyMsg(std::shared_ptr<MessageInfo> info)
31 {
32 CHKPV(info);
33 auto it = std::find_if(msgInfos_[info->msgType].begin(), msgInfos_[info->msgType].end(),
34 [info] (auto msgInfo) {
35 return *msgInfo == info;
36 });
37 if (it != msgInfos_[info->msgType].end()) {
38 *it = info;
39 return;
40 }
41 msgInfos_[info->msgType].emplace_back(info);
42 }
43
RemoveNotifyMsg(std::shared_ptr<MessageInfo> info)44 void StateChangeNotify::RemoveNotifyMsg(std::shared_ptr<MessageInfo> info)
45 {
46 if (info == nullptr || msgInfos_.empty() || msgInfos_[info->msgType].empty()) {
47 FI_HILOGE("Remove listener failed");
48 return;
49 }
50 auto it = std::find_if(msgInfos_[info->msgType].begin(), msgInfos_[info->msgType].end(),
51 [info] (auto msgInfo) {
52 return *msgInfo == info;
53 });
54 if (it != msgInfos_[info->msgType].end()) {
55 msgInfos_[info->msgType].erase(it);
56 }
57 }
58
StyleChangedNotify(DragCursorStyle style)59 int32_t StateChangeNotify::StyleChangedNotify(DragCursorStyle style)
60 {
61 if (msgInfos_[MessageType::NOTIFY_STYLE].empty()) {
62 FI_HILOGW("No listener, send message failed");
63 return RET_ERR;
64 }
65 for (auto it = msgInfos_[MessageType::NOTIFY_STYLE].begin();
66 it != msgInfos_[MessageType::NOTIFY_STYLE].end(); ++it) {
67 auto info = *it;
68 CHKPC(info);
69 OnDragInfoNotify(info->session, info->msgId, style);
70 }
71 return RET_OK;
72 }
73
StateChangedNotify(DragState state)74 int32_t StateChangeNotify::StateChangedNotify(DragState state)
75 {
76 CALL_DEBUG_ENTER;
77 if (msgInfos_[MessageType::NOTIFY_STATE].empty()) {
78 FI_HILOGW("No listener, send message failed");
79 return RET_ERR;
80 }
81 for (auto it = msgInfos_[MessageType::NOTIFY_STATE].begin();
82 it != msgInfos_[MessageType::NOTIFY_STATE].end(); ++it) {
83 auto info = *it;
84 CHKPC(info);
85 OnDragInfoNotify(info->session, info->msgId, state);
86 }
87 return RET_OK;
88 }
89
90 template <typename T>
OnDragInfoNotify(SessionPtr session,MessageId msgId,T t)91 void StateChangeNotify::OnDragInfoNotify(SessionPtr session, MessageId msgId, T t)
92 {
93 CALL_DEBUG_ENTER;
94 CHKPV(session);
95 NetPacket pkt(msgId);
96 pkt << static_cast<int32_t>(t);
97 if (pkt.ChkRWError()) {
98 FI_HILOGE("Packet write data failed");
99 return;
100 }
101 if (!session->SendMsg(pkt)) {
102 FI_HILOGE("Sending failed");
103 return;
104 }
105 }
106 } // namespace DeviceStatus
107 } // namespace Msdp
108 } // namespace OHOS
109