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_proxy.h"
17
18 #include "ans_inner_errors.h"
19 #include "ans_log_wrapper.h"
20 #include "message_option.h"
21 #include "message_parcel.h"
22
23 namespace OHOS {
24 namespace Notification {
AnsSubscriberProxy(const sptr<IRemoteObject> & impl)25 AnsSubscriberProxy::AnsSubscriberProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<AnsSubscriberInterface>(impl)
26 {}
27
~AnsSubscriberProxy()28 AnsSubscriberProxy::~AnsSubscriberProxy()
29 {}
30
InnerTransact(NotificationInterfaceCode code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)31 ErrCode AnsSubscriberProxy::InnerTransact(
32 NotificationInterfaceCode code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
33 {
34 auto remote = Remote();
35 if (remote == nullptr) {
36 ANS_LOGE("[InnerTransact] fail: get Remote fail code %{public}u", code);
37 return ERR_DEAD_OBJECT;
38 }
39
40 int32_t err = remote->SendRequest(static_cast<uint32_t>(code), data, reply, flags);
41 switch (err) {
42 case NO_ERROR: {
43 return ERR_OK;
44 }
45 case DEAD_OBJECT: {
46 ANS_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
47 return ERR_DEAD_OBJECT;
48 }
49 default: {
50 ANS_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
51 return ERR_ANS_TRANSACT_FAILED;
52 }
53 }
54 }
55
OnConnected()56 void AnsSubscriberProxy::OnConnected()
57 {
58 MessageParcel data;
59 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
60 ANS_LOGE("[OnConnected] fail: write interface token failed.");
61 return;
62 }
63
64 MessageParcel reply;
65 MessageOption option = {MessageOption::TF_ASYNC};
66 ErrCode result = InnerTransact(NotificationInterfaceCode::ON_CONNECTED, option, data, reply);
67 if (result != ERR_OK) {
68 ANS_LOGE("[OnConnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
69 return;
70 }
71 }
72
OnDisconnected()73 void AnsSubscriberProxy::OnDisconnected()
74 {
75 MessageParcel data;
76 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
77 ANS_LOGE("[OnDisconnected] fail: write interface token failed.");
78 return;
79 }
80
81 MessageParcel reply;
82 MessageOption option = {MessageOption::TF_ASYNC};
83 ErrCode result = InnerTransact(NotificationInterfaceCode::ON_DISCONNECTED, option, data, reply);
84 if (result != ERR_OK) {
85 ANS_LOGE("[OnDisconnected] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
86 return;
87 }
88 }
89
OnConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)90 void AnsSubscriberProxy::OnConsumed(
91 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap)
92 {
93 if (notification == nullptr) {
94 ANS_LOGE("[OnConsumed] fail: notification is nullptr.");
95 return;
96 }
97
98 MessageParcel data;
99 if (notification->GetNotificationRequest().IsCommonLiveView()) {
100 if (!data.SetMaxCapacity(NotificationConstant::NOTIFICATION_MAX_LIVE_VIEW_SIZE)) {
101 ANS_LOGE("[OnConsumed] fail: set max capacity failed.");
102 return;
103 }
104 }
105 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
106 ANS_LOGE("[OnConsumed] fail: write interface token failed.");
107 return;
108 }
109
110 if (!data.WriteParcelable(notification)) {
111 ANS_LOGE("[OnConsumed] fail: write notification failed.");
112 return;
113 }
114
115 if (!data.WriteBool(notificationMap != nullptr)) {
116 ANS_LOGE("[OnConsumed] fail: write existMap failed");
117 return;
118 }
119
120 if (notificationMap != nullptr) {
121 if (!data.WriteParcelable(notificationMap)) {
122 ANS_LOGE("[OnConsumed] fail: write notificationMap failed");
123 return;
124 }
125 }
126
127 MessageParcel reply;
128 MessageOption option = {MessageOption::TF_ASYNC};
129 ErrCode result = InnerTransact(NotificationInterfaceCode::ON_CONSUMED_MAP, option, data, reply);
130 if (result != ERR_OK) {
131 ANS_LOGE("[OnConsumed] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
132 return;
133 }
134 }
135
OnConsumedList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap)136 void AnsSubscriberProxy::OnConsumedList(const std::vector<sptr<Notification>> ¬ifications,
137 const sptr<NotificationSortingMap> ¬ificationMap)
138 {
139 ANS_LOGD("Start consumed list in proxy.");
140 if (notifications.empty() || notificationMap == nullptr) {
141 ANS_LOGE("Invalid notification to consumed.");
142 return;
143 }
144
145 MessageParcel data;
146 if (!data.SetMaxCapacity(NotificationConstant::NOTIFICATION_MAX_LIVE_VIEW_SIZE)) {
147 ANS_LOGE("[OnConsumedList] fail: set max capacity failed.");
148 }
149 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
150 ANS_LOGE("Write interface token failed.");
151 return;
152 }
153
154 if (!WriteParcelableVector(notifications, data)) {
155 ANS_LOGE("Write notifications failed");
156 return;
157 }
158
159 if (!data.WriteBool(notificationMap != nullptr)) {
160 ANS_LOGE("Write existMap failed");
161 return;
162 }
163
164 if (notificationMap != nullptr) {
165 if (!data.WriteParcelable(notificationMap)) {
166 ANS_LOGE("Write notificationMap failed");
167 return;
168 }
169 }
170
171 MessageParcel reply;
172 MessageOption option = {MessageOption::TF_ASYNC};
173 ErrCode result = InnerTransact(NotificationInterfaceCode::ON_CONSUMED_LIST_MAP, option, data, reply);
174 if (result != ERR_OK) {
175 ANS_LOGE("Transact ErrCode=ERR_ANS_TRANSACT_FAILED");
176 return;
177 }
178 }
179
OnCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)180 void AnsSubscriberProxy::OnCanceled(
181 const sptr<Notification> ¬ification, const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason)
182 {
183 if (notification == nullptr) {
184 ANS_LOGE("[OnCanceled] fail: notification is nullptr.");
185 return;
186 }
187
188 MessageParcel data;
189 if (notification->GetNotificationRequest().IsCommonLiveView()) {
190 if (!data.SetMaxCapacity(NotificationConstant::NOTIFICATION_MAX_LIVE_VIEW_SIZE)) {
191 ANS_LOGE("[OnCanceled] fail: set max capacity failed.");
192 return;
193 }
194 }
195 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
196 ANS_LOGE("[OnCanceled] fail: write interface token failed.");
197 return;
198 }
199
200 if (!data.WriteParcelable(notification)) {
201 ANS_LOGE("[OnCanceled] fail: write notification failed.");
202 return;
203 }
204
205 if (!data.WriteBool(notificationMap != nullptr)) {
206 ANS_LOGE("[OnCanceled] fail: write existMap failed");
207 return;
208 }
209
210 if (notificationMap != nullptr) {
211 if (!data.WriteParcelable(notificationMap)) {
212 ANS_LOGE("[OnCanceled] fail: write notificationMap failed");
213 return;
214 }
215 }
216
217 if (!data.WriteInt32(deleteReason)) {
218 ANS_LOGE("[OnCanceled] fail: write deleteReason failed.");
219 return;
220 }
221
222 MessageParcel reply;
223 MessageOption option = {MessageOption::TF_ASYNC};
224 ErrCode result = InnerTransact(NotificationInterfaceCode::ON_CANCELED_MAP, option, data, reply);
225 if (result != ERR_OK) {
226 ANS_LOGE("[OnCanceled] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
227 return;
228 }
229 }
230
OnCanceledList(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)231 void AnsSubscriberProxy::OnCanceledList(const std::vector<sptr<Notification>> ¬ifications,
232 const sptr<NotificationSortingMap> ¬ificationMap, int32_t deleteReason)
233 {
234 if (notifications.empty()) {
235 ANS_LOGE("Notifications is empty.");
236 return;
237 }
238
239 MessageParcel data;
240 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
241 ANS_LOGE("Write interface token failed.");
242 return;
243 }
244
245 for (size_t i = 0; i < notifications.size(); i ++) {
246 sptr<Notification> notification = notifications[i];
247 notification->GetNotificationRequestPoint()->SetBigIcon(nullptr);
248 notification->GetNotificationRequestPoint()->SetLittleIcon(nullptr);
249 notification->GetNotificationRequestPoint()->SetOverlayIcon(nullptr);
250 }
251 if (!WriteParcelableVector(notifications, data)) {
252 ANS_LOGE("Write notifications failed");
253 return;
254 }
255
256 if (!data.WriteBool(notificationMap != nullptr)) {
257 ANS_LOGE("Write existMap failed");
258 return;
259 }
260
261 if (notificationMap != nullptr) {
262 if (!data.WriteParcelable(notificationMap)) {
263 ANS_LOGE("Write notificationMap failed");
264 return;
265 }
266 }
267
268 if (!data.WriteInt32(deleteReason)) {
269 ANS_LOGE("Write deleteReason failed.");
270 return;
271 }
272
273 MessageParcel reply;
274 MessageOption option = {MessageOption::TF_ASYNC};
275 ErrCode result = InnerTransact(NotificationInterfaceCode::ON_CANCELED_LIST_MAP, option, data, reply);
276 if (result != ERR_OK) {
277 ANS_LOGE("Transact ErrCode=ERR_ANS_TRANSACT_FAILED");
278 return;
279 }
280 }
281
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)282 void AnsSubscriberProxy::OnUpdated(const sptr<NotificationSortingMap> ¬ificationMap)
283 {
284 if (notificationMap == nullptr) {
285 ANS_LOGE("[OnUpdated] fail: notificationMap is empty.");
286 return;
287 }
288
289 MessageParcel data;
290 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
291 ANS_LOGE("[OnUpdated] fail: write interface token failed.");
292 return;
293 }
294
295 if (!data.WriteParcelable(notificationMap)) {
296 ANS_LOGE("[OnUpdated] fail: write notificationMap failed.");
297 return;
298 }
299
300 MessageParcel reply;
301 MessageOption option = {MessageOption::TF_ASYNC};
302 ErrCode result = InnerTransact(NotificationInterfaceCode::ON_UPDATED, option, data, reply);
303 if (result != ERR_OK) {
304 ANS_LOGE("[OnUpdated] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
305 return;
306 }
307 }
308
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)309 void AnsSubscriberProxy::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
310 {
311 MessageParcel data;
312 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
313 ANS_LOGE("[OnDoNotDisturbDateChange] fail: write interface token failed.");
314 return;
315 }
316
317 if (!data.WriteParcelable(date)) {
318 ANS_LOGE("[OnDoNotDisturbDateChange] fail: write date failed");
319 return;
320 }
321
322 MessageParcel reply;
323 MessageOption option = {MessageOption::TF_ASYNC};
324 ErrCode result = InnerTransact(NotificationInterfaceCode::ON_DND_DATE_CHANGED, option, data, reply);
325 if (result != ERR_OK) {
326 ANS_LOGE("[OnDoNotDisturbDateChange] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
327 return;
328 }
329 }
330
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)331 void AnsSubscriberProxy::OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData)
332 {
333 MessageParcel data;
334 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
335 ANS_LOGE("[OnEnabledNotificationChanged] fail: write interface token failed.");
336 return;
337 }
338
339 if (!data.WriteParcelable(callbackData)) {
340 ANS_LOGE("[OnEnabledNotificationChanged] fail: write callbackData failed");
341 return;
342 }
343
344 MessageParcel reply;
345 MessageOption option = {MessageOption::TF_ASYNC};
346 ErrCode result = InnerTransact(NotificationInterfaceCode::ON_ENABLED_NOTIFICATION_CHANGED, option, data, reply);
347 if (result != ERR_OK) {
348 ANS_LOGE("[OnEnabledNotificationChanged] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
349 return;
350 }
351 }
352
OnBadgeChanged(const sptr<BadgeNumberCallbackData> & badgeData)353 void AnsSubscriberProxy::OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData)
354 {
355 MessageParcel data;
356 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
357 ANS_LOGE("[OnBadgeChanged] fail: write interface token failed.");
358 return;
359 }
360
361 if (!data.WriteParcelable(badgeData)) {
362 ANS_LOGE("[OnBadgeChanged] fail: write badgeData failed");
363 return;
364 }
365
366 MessageParcel reply;
367 MessageOption option = {MessageOption::TF_ASYNC};
368 ErrCode result = InnerTransact(NotificationInterfaceCode::ON_BADGE_CHANGED, option, data, reply);
369 if (result != ERR_OK) {
370 ANS_LOGE("[OnBadgeChanged] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
371 return;
372 }
373 }
374
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)375 void AnsSubscriberProxy::OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData)
376 {
377 if (callbackData == nullptr) {
378 ANS_LOGE("Callback data is nullptr.");
379 return;
380 }
381 MessageParcel data;
382 if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
383 ANS_LOGE("Write interface token failed.");
384 return;
385 }
386
387 if (!data.WriteParcelable(callbackData)) {
388 ANS_LOGE("Write callback data failed.");
389 return;
390 }
391
392 MessageParcel reply;
393 MessageOption option(MessageOption::TF_ASYNC);
394 ErrCode result = InnerTransact(NotificationInterfaceCode::ON_BADGE_ENABLED_CHANGED, option, data, reply);
395 if (result != ERR_OK) {
396 ANS_LOGE("Transact error code is: %{public}d.", result);
397 return;
398 }
399 }
400 } // namespace Notification
401 } // namespace OHOS
402