1 /*
2 * Copyright (c) 2021-2024 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
~AnsSubscriberStub()29 AnsSubscriberStub::~AnsSubscriberStub() {}
30
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & flags)31 int32_t AnsSubscriberStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
32 MessageOption &flags)
33 {
34 std::u16string descriptor = AnsSubscriberStub::GetDescriptor();
35 std::u16string remoteDescriptor = data.ReadInterfaceToken();
36 if (descriptor != remoteDescriptor) {
37 ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!");
38 return OBJECT_NULL;
39 }
40 ErrCode result = NO_ERROR;
41 switch (code) {
42 case static_cast<uint32_t>(NotificationInterfaceCode::ON_CONNECTED): {
43 result = HandleOnConnected(data, reply);
44 break;
45 }
46 case static_cast<uint32_t>(NotificationInterfaceCode::ON_DISCONNECTED): {
47 result = HandleOnDisconnected(data, reply);
48 break;
49 }
50 case static_cast<uint32_t>(NotificationInterfaceCode::ON_CONSUMED_MAP): {
51 result = HandleOnConsumedMap(data, reply);
52 break;
53 }
54 case static_cast<uint32_t>(NotificationInterfaceCode::ON_CONSUMED_LIST_MAP): {
55 result = HandleOnConsumedListMap(data, reply);
56 break;
57 }
58 case static_cast<uint32_t>(NotificationInterfaceCode::ON_CANCELED_MAP): {
59 result = HandleOnCanceledMap(data, reply);
60 break;
61 }
62 case static_cast<uint32_t>(NotificationInterfaceCode::ON_CANCELED_LIST_MAP): {
63 result = HandleOnCanceledListMap(data, reply);
64 break;
65 }
66 case static_cast<uint32_t>(NotificationInterfaceCode::ON_UPDATED): {
67 result = HandleOnUpdated(data, reply);
68 break;
69 }
70 case static_cast<uint32_t>(NotificationInterfaceCode::ON_DND_DATE_CHANGED): {
71 result = HandleOnDoNotDisturbDateChange(data, reply);
72 break;
73 }
74 case static_cast<uint32_t>(NotificationInterfaceCode::ON_ENABLED_NOTIFICATION_CHANGED): {
75 result = HandleOnEnabledNotificationChanged(data, reply);
76 break;
77 }
78 case static_cast<uint32_t>(NotificationInterfaceCode::ON_BADGE_CHANGED): {
79 result = HandleOnBadgeChanged(data, reply);
80 break;
81 }
82 case static_cast<uint32_t>(NotificationInterfaceCode::ON_BADGE_ENABLED_CHANGED): {
83 result = HandleOnBadgeEnabledChanged(data, reply);
84 break;
85 }
86 case static_cast<uint32_t>(NotificationInterfaceCode::ON_APPLICATION_INFO_NEED_CHANGED): {
87 result = HandleOnApplicationInfoNeedChanged(data, reply);
88 break;
89 }
90 case static_cast<uint32_t>(NotificationInterfaceCode::ON_RESPONSE_LISTENER): {
91 result = HandleOnResponse(data, reply);
92 break;
93 }
94 default: {
95 ANS_LOGE("[OnRemoteRequest] fail: unknown code!");
96 return IPCObjectStub::OnRemoteRequest(code, data, reply, flags);
97 }
98 }
99 return result;
100 }
101
HandleOnConnected(MessageParcel & data,MessageParcel & reply)102 ErrCode AnsSubscriberStub::HandleOnConnected(MessageParcel &data, MessageParcel &reply)
103 {
104 OnConnected();
105 return ERR_OK;
106 }
107
HandleOnDisconnected(MessageParcel & data,MessageParcel & reply)108 ErrCode AnsSubscriberStub::HandleOnDisconnected(MessageParcel &data, MessageParcel &reply)
109 {
110 OnDisconnected();
111 return ERR_OK;
112 }
113
HandleOnConsumedMap(MessageParcel & data,MessageParcel & reply)114 ErrCode AnsSubscriberStub::HandleOnConsumedMap(MessageParcel &data, MessageParcel &reply)
115 {
116 sptr<Notification> notification = data.ReadParcelable<Notification>();
117 if (!notification) {
118 ANS_LOGW("[HandleOnConsumedMap] fail: notification ReadParcelable failed");
119 return ERR_ANS_PARCELABLE_FAILED;
120 }
121
122 bool existMap = false;
123 if (!data.ReadBool(existMap)) {
124 ANS_LOGW("[HandleOnConsumedMap] fail: read existMap failed");
125 return ERR_ANS_PARCELABLE_FAILED;
126 }
127
128 sptr<NotificationSortingMap> notificationMap = nullptr;
129 if (existMap) {
130 notificationMap = data.ReadParcelable<NotificationSortingMap>();
131 if (notificationMap == nullptr) {
132 ANS_LOGW("[HandleOnConsumedMap] fail: read NotificationSortingMap failed");
133 return ERR_ANS_PARCELABLE_FAILED;
134 }
135 }
136
137 OnConsumed(notification, notificationMap);
138 return ERR_OK;
139 }
140
HandleOnConsumedListMap(MessageParcel & data,MessageParcel & reply)141 ErrCode AnsSubscriberStub::HandleOnConsumedListMap(MessageParcel &data, MessageParcel &reply)
142 {
143 ANS_LOGD("Start handle notifications in consumed list.");
144
145 std::vector<sptr<Notification>> notifications;
146 if (!ReadParcelableVector(notifications, data)) {
147 ANS_LOGE("read notifications failed");
148 return ERR_ANS_PARCELABLE_FAILED;
149 }
150
151 bool existMap = false;
152 if (!data.ReadBool(existMap)) {
153 ANS_LOGE("read existMap failed");
154 return ERR_ANS_PARCELABLE_FAILED;
155 }
156
157 sptr<NotificationSortingMap> notificationMap = nullptr;
158 if (existMap) {
159 notificationMap = data.ReadParcelable<NotificationSortingMap>();
160 if (notificationMap == nullptr) {
161 ANS_LOGE("read NotificationSortingMap failed");
162 return ERR_ANS_PARCELABLE_FAILED;
163 }
164 }
165
166 OnConsumedList(notifications, notificationMap);
167 return ERR_OK;
168 }
169
HandleOnCanceledMap(MessageParcel & data,MessageParcel & reply)170 ErrCode AnsSubscriberStub::HandleOnCanceledMap(MessageParcel &data, MessageParcel &reply)
171 {
172 sptr<Notification> notification = data.ReadParcelable<Notification>();
173 if (!notification) {
174 ANS_LOGW("[HandleOnCanceledMap] fail: notification ReadParcelable failed");
175 return ERR_ANS_PARCELABLE_FAILED;
176 }
177
178 bool existMap = false;
179 if (!data.ReadBool(existMap)) {
180 ANS_LOGW("[HandleOnCanceledMap] fail: read existMap failed");
181 return ERR_ANS_PARCELABLE_FAILED;
182 }
183
184 sptr<NotificationSortingMap> notificationMap = nullptr;
185 if (existMap) {
186 notificationMap = data.ReadParcelable<NotificationSortingMap>();
187 if (notificationMap == nullptr) {
188 ANS_LOGW("[HandleOnCanceledMap] fail: read NotificationSortingMap failed");
189 return ERR_ANS_PARCELABLE_FAILED;
190 }
191 }
192
193 int32_t reason = 0;
194 if (!data.ReadInt32(reason)) {
195 ANS_LOGW("[HandleOnCanceledMap] fail: read reason failed");
196 return ERR_ANS_PARCELABLE_FAILED;
197 }
198
199 OnCanceled(notification, notificationMap, reason);
200 return ERR_OK;
201 }
202
203
HandleOnCanceledListMap(MessageParcel & data,MessageParcel & reply)204 ErrCode AnsSubscriberStub::HandleOnCanceledListMap(MessageParcel &data, MessageParcel &reply)
205 {
206 std::vector<sptr<Notification>> notifications;
207 if (!ReadParcelableVector(notifications, data)) {
208 ANS_LOGE("read notifications failed");
209 return ERR_ANS_PARCELABLE_FAILED;
210 }
211
212 bool existMap = false;
213 if (!data.ReadBool(existMap)) {
214 ANS_LOGE("read existMap failed");
215 return ERR_ANS_PARCELABLE_FAILED;
216 }
217
218 sptr<NotificationSortingMap> notificationMap = nullptr;
219 if (existMap) {
220 notificationMap = data.ReadParcelable<NotificationSortingMap>();
221 if (notificationMap == nullptr) {
222 ANS_LOGE("read NotificationSortingMap failed");
223 return ERR_ANS_PARCELABLE_FAILED;
224 }
225 }
226
227 int32_t reason = 0;
228 if (!data.ReadInt32(reason)) {
229 ANS_LOGE("read reason failed");
230 return ERR_ANS_PARCELABLE_FAILED;
231 }
232
233 OnCanceledList(notifications, notificationMap, reason);
234 return ERR_OK;
235 }
236
237
238 template<typename T>
ReadParcelableVector(std::vector<sptr<T>> & parcelableInfos,MessageParcel & data)239 bool AnsSubscriberStub::ReadParcelableVector(std::vector<sptr<T>> &parcelableInfos, MessageParcel &data)
240 {
241 int32_t infoSize = 0;
242 if (!data.ReadInt32(infoSize)) {
243 ANS_LOGE("read Parcelable size failed.");
244 return false;
245 }
246
247 parcelableInfos.clear();
248 infoSize = (infoSize < MAX_PARCELABLE_VECTOR_NUM) ? infoSize : MAX_PARCELABLE_VECTOR_NUM;
249 for (int32_t index = 0; index < infoSize; index++) {
250 sptr<T> info = data.ReadStrongParcelable<T>();
251 if (info == nullptr) {
252 ANS_LOGE("read Parcelable infos failed.");
253 return false;
254 }
255 parcelableInfos.emplace_back(info);
256 }
257
258 return true;
259 }
260
HandleOnUpdated(MessageParcel & data,MessageParcel & reply)261 ErrCode AnsSubscriberStub::HandleOnUpdated(MessageParcel &data, MessageParcel &reply)
262 {
263 sptr<NotificationSortingMap> notificationMap = data.ReadParcelable<NotificationSortingMap>();
264 if (!notificationMap) {
265 ANS_LOGW("[HandleOnUpdated] fail: notificationMap ReadParcelable failed");
266 return ERR_ANS_PARCELABLE_FAILED;
267 }
268
269 OnUpdated(notificationMap);
270 return ERR_OK;
271 }
272
HandleOnDoNotDisturbDateChange(MessageParcel & data,MessageParcel & reply)273 ErrCode AnsSubscriberStub::HandleOnDoNotDisturbDateChange(MessageParcel &data, MessageParcel &reply)
274 {
275 sptr<NotificationDoNotDisturbDate> date = data.ReadParcelable<NotificationDoNotDisturbDate>();
276 if (!date) {
277 ANS_LOGW("[HandleOnDoNotDisturbDateChange] fail: date ReadParcelable failed");
278 return ERR_ANS_PARCELABLE_FAILED;
279 }
280 OnDoNotDisturbDateChange(date);
281 return ERR_OK;
282 }
283
HandleOnEnabledNotificationChanged(MessageParcel & data,MessageParcel & reply)284 ErrCode AnsSubscriberStub::HandleOnEnabledNotificationChanged(MessageParcel &data, MessageParcel &reply)
285 {
286 sptr<EnabledNotificationCallbackData> callbackData = data.ReadParcelable<EnabledNotificationCallbackData>();
287 if (!callbackData) {
288 ANS_LOGW("[HandleOnEnabledNotificationChanged] fail: callbackData ReadParcelable failed");
289 return ERR_ANS_PARCELABLE_FAILED;
290 }
291 OnEnabledNotificationChanged(callbackData);
292 return ERR_OK;
293 }
294
HandleOnBadgeChanged(MessageParcel & data,MessageParcel & reply)295 ErrCode AnsSubscriberStub::HandleOnBadgeChanged(MessageParcel &data, MessageParcel &reply)
296 {
297 sptr<BadgeNumberCallbackData> callbackData = data.ReadParcelable<BadgeNumberCallbackData>();
298 if (!callbackData) {
299 ANS_LOGW("[HandleOnBadgeChanged] fail: callbackData ReadParcelable failed");
300 return ERR_ANS_PARCELABLE_FAILED;
301 }
302 OnBadgeChanged(callbackData);
303 return ERR_OK;
304 }
305
HandleOnBadgeEnabledChanged(MessageParcel & data,MessageParcel & reply)306 ErrCode AnsSubscriberStub::HandleOnBadgeEnabledChanged(MessageParcel &data, MessageParcel &reply)
307 {
308 sptr<EnabledNotificationCallbackData> callbackData = data.ReadParcelable<EnabledNotificationCallbackData>();
309 if (callbackData == nullptr) {
310 ANS_LOGE("Read callback data failed.");
311 return ERR_ANS_PARCELABLE_FAILED;
312 }
313 OnBadgeEnabledChanged(callbackData);
314 return ERR_OK;
315 }
316
HandleOnApplicationInfoNeedChanged(MessageParcel & data,MessageParcel & reply)317 ErrCode AnsSubscriberStub::HandleOnApplicationInfoNeedChanged(MessageParcel &data, MessageParcel &reply)
318 {
319 std::string bundleName;
320 if (!data.ReadString(bundleName)) {
321 ANS_LOGE("[HandleGetAllDistribuedEnabledBundles] fail: read deviceType failed.");
322 return ERR_ANS_PARCELABLE_FAILED;
323 }
324 OnApplicationInfoNeedChanged(bundleName);
325 return ERR_OK;
326 }
327
HandleOnResponse(MessageParcel & data,MessageParcel & reply)328 ErrCode AnsSubscriberStub::HandleOnResponse(MessageParcel &data, MessageParcel &reply)
329 {
330 sptr<NotificationOperationInfo> operationInfo = data.ReadParcelable<NotificationOperationInfo>();
331 if (!operationInfo) {
332 ANS_LOGW("notification ReadParcelable failed");
333 return ERR_ANS_PARCELABLE_FAILED;
334 }
335 ErrCode result = OnOperationResponse(operationInfo);
336 if (!reply.WriteInt32(result)) {
337 ANS_LOGE("write result failed, ErrCode=%{public}d", result);
338 return ERR_ANS_PARCELABLE_FAILED;
339 }
340 return ERR_OK;
341 }
342
OnConnected()343 void AnsSubscriberStub::OnConnected() {}
344
OnDisconnected()345 void AnsSubscriberStub::OnDisconnected() {}
346
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)347 void AnsSubscriberStub::OnConsumed(
348 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap)
349 {}
350
OnConsumedList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap)351 void AnsSubscriberStub::OnConsumedList(const std::vector<sptr<Notification>> ¬ifications,
352 const sptr<NotificationSortingMap> ¬ificationMap)
353 {}
354
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)355 void AnsSubscriberStub::OnCanceled(
356 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason)
357 {}
358
OnCanceledList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)359 void AnsSubscriberStub::OnCanceledList(const std::vector<sptr<Notification>> ¬ifications,
360 const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason)
361 {}
362
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)363 void AnsSubscriberStub::OnUpdated(const sptr<NotificationSortingMap> ¬ificationMap) {}
364
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)365 void AnsSubscriberStub::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date) {}
366
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)367 void AnsSubscriberStub::OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData) {}
368
OnBadgeChanged(const sptr<BadgeNumberCallbackData> & badgeData)369 void AnsSubscriberStub::OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData) {}
370
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)371 void AnsSubscriberStub::OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) {}
372
OnApplicationInfoNeedChanged(const std::string & bundleName)373 void AnsSubscriberStub::OnApplicationInfoNeedChanged(const std::string& bundleName) {}
374
OnOperationResponse(const sptr<NotificationOperationInfo> & operationInfo)375 ErrCode AnsSubscriberStub::OnOperationResponse(const sptr<NotificationOperationInfo>& operationInfo) { return 0; }
376 } // namespace Notification
377 } // namespace OHOS
378