1 /*
2 * Copyright (c) 2021 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 "ans_subscriber_stub.h"
17
18 #include "ans_const_define.h"
19 #include "ans_inner_errors.h"
20 #include "ans_log_wrapper.h"
21 #include "message_option.h"
22 #include "message_parcel.h"
23 #include "parcel.h"
24
25 namespace OHOS {
26 namespace Notification {
AnsSubscriberStub()27 AnsSubscriberStub::AnsSubscriberStub()
28 {
29 interfaces_.emplace(ON_CONNECTED,
30 std::bind(&AnsSubscriberStub::HandleOnConnected, this, std::placeholders::_1, std::placeholders::_2));
31 interfaces_.emplace(ON_DISCONNECTED,
32 std::bind(&AnsSubscriberStub::HandleOnDisconnected, this, std::placeholders::_1, std::placeholders::_2));
33 interfaces_.emplace(ON_CONSUMED,
34 std::bind(&AnsSubscriberStub::HandleOnConsumed, this, std::placeholders::_1, std::placeholders::_2));
35 interfaces_.emplace(ON_CONSUMED_MAP,
36 std::bind(&AnsSubscriberStub::HandleOnConsumedMap, this, std::placeholders::_1, std::placeholders::_2));
37 interfaces_.emplace(ON_CANCELED_MAP,
38 std::bind(&AnsSubscriberStub::HandleOnCanceledMap, this, std::placeholders::_1, std::placeholders::_2));
39 interfaces_.emplace(
40 ON_UPDATED, std::bind(&AnsSubscriberStub::HandleOnUpdated, this, std::placeholders::_1, std::placeholders::_2));
41 interfaces_.emplace(ON_DND_DATE_CHANGED,
42 std::bind(
43 &AnsSubscriberStub::HandleOnDoNotDisturbDateChange, this, std::placeholders::_1, std::placeholders::_2));
44 interfaces_.emplace(ON_ENABLED_NOTIFICATION_CHANGED,
45 std::bind(&AnsSubscriberStub::HandleOnEnabledNotificationChanged, this, std::placeholders::_1,
46 std::placeholders::_2));
47 }
48
~AnsSubscriberStub()49 AnsSubscriberStub::~AnsSubscriberStub()
50 {}
51
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & flags)52 int32_t AnsSubscriberStub::OnRemoteRequest(
53 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &flags)
54 {
55 std::u16string descriptor = AnsSubscriberStub::GetDescriptor();
56 std::u16string remoteDescriptor = data.ReadInterfaceToken();
57 if (descriptor != remoteDescriptor) {
58 ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!");
59 return OBJECT_NULL;
60 }
61
62 auto it = interfaces_.find(code);
63 if (it == interfaces_.end()) {
64 ANS_LOGW("[OnRemoteRequest] fail: unknown code!");
65 return IRemoteStub<AnsSubscriberInterface>::OnRemoteRequest(code, data, reply, flags);
66 }
67
68 auto fun = it->second;
69 if (fun == nullptr) {
70 ANS_LOGW("[OnRemoteRequest] fail: not find function!");
71 return IRemoteStub<AnsSubscriberInterface>::OnRemoteRequest(code, data, reply, flags);
72 }
73
74 fun(data, reply);
75 return NO_ERROR;
76 }
77
HandleOnConnected(MessageParcel & data,MessageParcel & reply)78 ErrCode AnsSubscriberStub::HandleOnConnected(MessageParcel &data, MessageParcel &reply)
79 {
80 OnConnected();
81 return ERR_OK;
82 }
83
HandleOnDisconnected(MessageParcel & data,MessageParcel & reply)84 ErrCode AnsSubscriberStub::HandleOnDisconnected(MessageParcel &data, MessageParcel &reply)
85 {
86 OnDisconnected();
87 return ERR_OK;
88 }
89
HandleOnConsumed(MessageParcel & data,MessageParcel & reply)90 ErrCode AnsSubscriberStub::HandleOnConsumed(MessageParcel &data, MessageParcel &reply)
91 {
92 sptr<Notification> notification = data.ReadParcelable<Notification>();
93 if (!notification) {
94 ANS_LOGW("[HandleOnConsumed] fail: notification ReadParcelable failed");
95 return ERR_ANS_PARCELABLE_FAILED;
96 }
97
98 OnConsumed(notification);
99 return ERR_OK;
100 }
101
HandleOnConsumedMap(MessageParcel & data,MessageParcel & reply)102 ErrCode AnsSubscriberStub::HandleOnConsumedMap(MessageParcel &data, MessageParcel &reply)
103 {
104 sptr<Notification> notification = data.ReadParcelable<Notification>();
105 if (!notification) {
106 ANS_LOGW("[HandleOnConsumedMap] fail: notification ReadParcelable failed");
107 return ERR_ANS_PARCELABLE_FAILED;
108 }
109
110 bool existMap = false;
111 if (!data.ReadBool(existMap)) {
112 ANS_LOGW("[HandleOnConsumedMap] fail: read existMap failed");
113 return ERR_ANS_PARCELABLE_FAILED;
114 }
115
116 sptr<NotificationSortingMap> notificationMap = nullptr;
117 if (existMap) {
118 notificationMap = data.ReadParcelable<NotificationSortingMap>();
119 if (notificationMap == nullptr) {
120 ANS_LOGW("[HandleOnConsumedMap] fail: read NotificationSortingMap failed");
121 return ERR_ANS_PARCELABLE_FAILED;
122 }
123 }
124
125 OnConsumed(notification, notificationMap);
126 return ERR_OK;
127 }
128
HandleOnCanceledMap(MessageParcel & data,MessageParcel & reply)129 ErrCode AnsSubscriberStub::HandleOnCanceledMap(MessageParcel &data, MessageParcel &reply)
130 {
131 sptr<Notification> notification = data.ReadParcelable<Notification>();
132 if (!notification) {
133 ANS_LOGW("[HandleOnCanceledMap] fail: notification ReadParcelable failed");
134 return ERR_ANS_PARCELABLE_FAILED;
135 }
136
137 bool existMap = false;
138 if (!data.ReadBool(existMap)) {
139 ANS_LOGW("[HandleOnCanceledMap] fail: read existMap failed");
140 return ERR_ANS_PARCELABLE_FAILED;
141 }
142
143 sptr<NotificationSortingMap> notificationMap = nullptr;
144 if (existMap) {
145 notificationMap = data.ReadParcelable<NotificationSortingMap>();
146 if (notificationMap == nullptr) {
147 ANS_LOGW("[HandleOnCanceledMap] fail: read NotificationSortingMap failed");
148 return ERR_ANS_PARCELABLE_FAILED;
149 }
150 }
151
152 int32_t reason = 0;
153 if (!data.ReadInt32(reason)) {
154 ANS_LOGW("[HandleOnCanceledMap] fail: read reason failed");
155 return ERR_ANS_PARCELABLE_FAILED;
156 }
157
158 OnCanceled(notification, notificationMap, reason);
159 return ERR_OK;
160 }
161
HandleOnUpdated(MessageParcel & data,MessageParcel & reply)162 ErrCode AnsSubscriberStub::HandleOnUpdated(MessageParcel &data, MessageParcel &reply)
163 {
164 sptr<NotificationSortingMap> notificationMap = data.ReadParcelable<NotificationSortingMap>();
165 if (!notificationMap) {
166 ANS_LOGW("[HandleOnUpdated] fail: notificationMap ReadParcelable failed");
167 return ERR_ANS_PARCELABLE_FAILED;
168 }
169
170 OnUpdated(notificationMap);
171 return ERR_OK;
172 }
173
HandleOnDoNotDisturbDateChange(MessageParcel & data,MessageParcel & reply)174 ErrCode AnsSubscriberStub::HandleOnDoNotDisturbDateChange(MessageParcel &data, MessageParcel &reply)
175 {
176 sptr<NotificationDoNotDisturbDate> date = data.ReadParcelable<NotificationDoNotDisturbDate>();
177 if (!date) {
178 ANS_LOGW("[HandleOnDoNotDisturbDateChange] fail: date ReadParcelable failed");
179 return ERR_ANS_PARCELABLE_FAILED;
180 }
181 OnDoNotDisturbDateChange(date);
182 return ERR_OK;
183 }
184
HandleOnEnabledNotificationChanged(MessageParcel & data,MessageParcel & reply)185 ErrCode AnsSubscriberStub::HandleOnEnabledNotificationChanged(MessageParcel &data, MessageParcel &reply)
186 {
187 sptr<EnabledNotificationCallbackData> callbackData = data.ReadParcelable<EnabledNotificationCallbackData>();
188 if (!callbackData) {
189 ANS_LOGW("[HandleOnEnabledNotificationChanged] fail: callbackData ReadParcelable failed");
190 return ERR_ANS_PARCELABLE_FAILED;
191 }
192 OnEnabledNotificationChanged(callbackData);
193 return ERR_OK;
194 }
195
OnConnected()196 void AnsSubscriberStub::OnConnected()
197 {}
198
OnDisconnected()199 void AnsSubscriberStub::OnDisconnected()
200 {}
201
OnConsumed(const sptr<Notification> & notification)202 void AnsSubscriberStub::OnConsumed(const sptr<Notification> ¬ification)
203 {}
204
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)205 void AnsSubscriberStub::OnConsumed(
206 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap)
207 {}
208
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)209 void AnsSubscriberStub::OnCanceled(
210 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason)
211 {}
212
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)213 void AnsSubscriberStub::OnUpdated(const sptr<NotificationSortingMap> ¬ificationMap)
214 {}
215
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)216 void AnsSubscriberStub::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
217 {}
218
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)219 void AnsSubscriberStub::OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData)
220 {}
221 } // namespace Notification
222 } // namespace OHOS
223