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