• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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> &notification, const sptr<NotificationSortingMap> &notificationMap)
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>> &notifications,
137     const sptr<NotificationSortingMap> &notificationMap)
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> &notification, const sptr<NotificationSortingMap> &notificationMap, 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>> &notifications,
232     const sptr<NotificationSortingMap> &notificationMap, 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 (!data.SetMaxCapacity(NotificationConstant::NOTIFICATION_MAX_LIVE_VIEW_SIZE)) {
252         ANS_LOGE("[OnConsumedList] fail: set max capacity failed.");
253     }
254     if (!WriteParcelableVector(notifications, data)) {
255         ANS_LOGE("Write notifications failed");
256         return;
257     }
258 
259     if (!data.WriteBool(notificationMap != nullptr)) {
260         ANS_LOGE("Write existMap failed");
261         return;
262     }
263 
264     if (notificationMap != nullptr) {
265         if (!data.WriteParcelable(notificationMap)) {
266             ANS_LOGE("Write notificationMap failed");
267             return;
268         }
269     }
270 
271     if (!data.WriteInt32(deleteReason)) {
272         ANS_LOGE("Write deleteReason failed.");
273         return;
274     }
275 
276     MessageParcel reply;
277     MessageOption option = {MessageOption::TF_ASYNC};
278     ErrCode result = InnerTransact(NotificationInterfaceCode::ON_CANCELED_LIST_MAP, option, data, reply);
279     if (result != ERR_OK) {
280         ANS_LOGE("Transact ErrCode=ERR_ANS_TRANSACT_FAILED");
281         return;
282     }
283 }
284 
OnUpdated(const sptr<NotificationSortingMap> & notificationMap)285 void AnsSubscriberProxy::OnUpdated(const sptr<NotificationSortingMap> &notificationMap)
286 {
287     if (notificationMap == nullptr) {
288         ANS_LOGE("[OnUpdated] fail: notificationMap is empty.");
289         return;
290     }
291 
292     MessageParcel data;
293     if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
294         ANS_LOGE("[OnUpdated] fail: write interface token failed.");
295         return;
296     }
297 
298     if (!data.WriteParcelable(notificationMap)) {
299         ANS_LOGE("[OnUpdated] fail: write notificationMap failed.");
300         return;
301     }
302 
303     MessageParcel reply;
304     MessageOption option = {MessageOption::TF_ASYNC};
305     ErrCode result = InnerTransact(NotificationInterfaceCode::ON_UPDATED, option, data, reply);
306     if (result != ERR_OK) {
307         ANS_LOGE("[OnUpdated] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
308         return;
309     }
310 }
311 
OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> & date)312 void AnsSubscriberProxy::OnDoNotDisturbDateChange(const sptr<NotificationDoNotDisturbDate> &date)
313 {
314     MessageParcel data;
315     if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
316         ANS_LOGE("[OnDoNotDisturbDateChange] fail: write interface token failed.");
317         return;
318     }
319 
320     if (!data.WriteParcelable(date)) {
321         ANS_LOGE("[OnDoNotDisturbDateChange] fail: write date failed");
322         return;
323     }
324 
325     MessageParcel reply;
326     MessageOption option = {MessageOption::TF_ASYNC};
327     ErrCode result = InnerTransact(NotificationInterfaceCode::ON_DND_DATE_CHANGED, option, data, reply);
328     if (result != ERR_OK) {
329         ANS_LOGE("[OnDoNotDisturbDateChange] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
330         return;
331     }
332 }
333 
OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)334 void AnsSubscriberProxy::OnEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> &callbackData)
335 {
336     MessageParcel data;
337     if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
338         ANS_LOGE("[OnEnabledNotificationChanged] fail: write interface token failed.");
339         return;
340     }
341 
342     if (!data.WriteParcelable(callbackData)) {
343         ANS_LOGE("[OnEnabledNotificationChanged] fail: write callbackData failed");
344         return;
345     }
346 
347     MessageParcel reply;
348     MessageOption option = {MessageOption::TF_ASYNC};
349     ErrCode result = InnerTransact(NotificationInterfaceCode::ON_ENABLED_NOTIFICATION_CHANGED, option, data, reply);
350     if (result != ERR_OK) {
351         ANS_LOGE("[OnEnabledNotificationChanged] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
352         return;
353     }
354 }
355 
OnBadgeChanged(const sptr<BadgeNumberCallbackData> & badgeData)356 void AnsSubscriberProxy::OnBadgeChanged(const sptr<BadgeNumberCallbackData> &badgeData)
357 {
358     MessageParcel data;
359     if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
360         ANS_LOGE("[OnBadgeChanged] fail: write interface token failed.");
361         return;
362     }
363 
364     if (!data.WriteParcelable(badgeData)) {
365         ANS_LOGE("[OnBadgeChanged] fail: write badgeData failed");
366         return;
367     }
368 
369     MessageParcel reply;
370     MessageOption option = {MessageOption::TF_ASYNC};
371     ErrCode result = InnerTransact(NotificationInterfaceCode::ON_BADGE_CHANGED, option, data, reply);
372     if (result != ERR_OK) {
373         ANS_LOGE("[OnBadgeChanged] fail: transact ErrCode=ERR_ANS_TRANSACT_FAILED");
374         return;
375     }
376 }
377 
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)378 void AnsSubscriberProxy::OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData)
379 {
380     if (callbackData == nullptr) {
381         ANS_LOGE("Callback data is nullptr.");
382         return;
383     }
384     MessageParcel data;
385     if (!data.WriteInterfaceToken(AnsSubscriberProxy::GetDescriptor())) {
386         ANS_LOGE("Write interface token failed.");
387         return;
388     }
389 
390     if (!data.WriteParcelable(callbackData)) {
391         ANS_LOGE("Write callback data failed.");
392         return;
393     }
394 
395     MessageParcel reply;
396     MessageOption option(MessageOption::TF_ASYNC);
397     ErrCode result = InnerTransact(NotificationInterfaceCode::ON_BADGE_ENABLED_CHANGED, option, data, reply);
398     if (result != ERR_OK) {
399         ANS_LOGE("Transact error code is: %{public}d.", result);
400         return;
401     }
402 }
403 }  // namespace Notification
404 }  // namespace OHOS
405