• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "ans_notification.h"
17 #include "ans_const_define.h"
18 #include "ans_inner_errors.h"
19 #include "ans_log_wrapper.h"
20 #include "hitrace_meter.h"
21 #include "iservice_registry.h"
22 #include "reminder_request_alarm.h"
23 #include "reminder_request_calendar.h"
24 #include "reminder_request_timer.h"
25 #include "system_ability_definition.h"
26 
27 namespace OHOS {
28 namespace Notification {
AddNotificationSlot(const NotificationSlot & slot)29 ErrCode AnsNotification::AddNotificationSlot(const NotificationSlot &slot)
30 {
31     std::vector<NotificationSlot> slots;
32     slots.push_back(slot);
33     return AddNotificationSlots(slots);
34 }
35 
AddSlotByType(const NotificationConstant::SlotType & slotType)36 ErrCode AnsNotification::AddSlotByType(const NotificationConstant::SlotType &slotType)
37 {
38     if (!GetAnsManagerProxy()) {
39         ANS_LOGE("GetAnsManagerProxy fail.");
40         return ERR_ANS_SERVICE_NOT_CONNECTED;
41     }
42     return ansManagerProxy_->AddSlotByType(slotType);
43 }
44 
AddNotificationSlots(const std::vector<NotificationSlot> & slots)45 ErrCode AnsNotification::AddNotificationSlots(const std::vector<NotificationSlot> &slots)
46 {
47     if (slots.size() == 0) {
48         ANS_LOGE("Failed to add notification slots because input slots size is 0.");
49         return ERR_ANS_INVALID_PARAM;
50     }
51     if (!GetAnsManagerProxy()) {
52         ANS_LOGE("GetAnsManagerProxy fail.");
53         return ERR_ANS_SERVICE_NOT_CONNECTED;
54     }
55 
56     std::vector<sptr<NotificationSlot>> slotsSptr;
57     for (auto it = slots.begin(); it != slots.end(); ++it) {
58         sptr<NotificationSlot> slot = new (std::nothrow) NotificationSlot(*it);
59         if (slot == nullptr) {
60             ANS_LOGE("Failed to create NotificationSlot ptr.");
61             return ERR_ANS_NO_MEMORY;
62         }
63         slotsSptr.emplace_back(slot);
64     }
65 
66     return ansManagerProxy_->AddSlots(slotsSptr);
67 }
68 
RemoveNotificationSlot(const NotificationConstant::SlotType & slotType)69 ErrCode AnsNotification::RemoveNotificationSlot(const NotificationConstant::SlotType &slotType)
70 {
71     if (!GetAnsManagerProxy()) {
72         ANS_LOGE("GetAnsManagerProxy fail.");
73         return ERR_ANS_SERVICE_NOT_CONNECTED;
74     }
75     return ansManagerProxy_->RemoveSlotByType(slotType);
76 }
77 
RemoveAllSlots()78 ErrCode AnsNotification::RemoveAllSlots()
79 {
80     if (!GetAnsManagerProxy()) {
81         ANS_LOGE("GetAnsManagerProxy fail.");
82         return ERR_ANS_SERVICE_NOT_CONNECTED;
83     }
84     return ansManagerProxy_->RemoveAllSlots();
85 }
86 
GetNotificationSlot(const NotificationConstant::SlotType & slotType,sptr<NotificationSlot> & slot)87 ErrCode AnsNotification::GetNotificationSlot(
88     const NotificationConstant::SlotType &slotType, sptr<NotificationSlot> &slot)
89 {
90     if (!GetAnsManagerProxy()) {
91         ANS_LOGE("GetAnsManagerProxy fail.");
92         return ERR_ANS_SERVICE_NOT_CONNECTED;
93     }
94     return ansManagerProxy_->GetSlotByType(slotType, slot);
95 }
96 
GetNotificationSlots(std::vector<sptr<NotificationSlot>> & slots)97 ErrCode AnsNotification::GetNotificationSlots(std::vector<sptr<NotificationSlot>> &slots)
98 {
99     if (!GetAnsManagerProxy()) {
100         ANS_LOGE("GetAnsManagerProxy fail.");
101         return ERR_ANS_SERVICE_NOT_CONNECTED;
102     }
103     return ansManagerProxy_->GetSlots(slots);
104 }
105 
GetNotificationSlotNumAsBundle(const NotificationBundleOption & bundleOption,uint64_t & num)106 ErrCode AnsNotification::GetNotificationSlotNumAsBundle(const NotificationBundleOption &bundleOption, uint64_t &num)
107 {
108     if (bundleOption.GetBundleName().empty()) {
109         ANS_LOGE("Invalid bundle name.");
110         return ERR_ANS_INVALID_PARAM;
111     }
112 
113     if (!GetAnsManagerProxy()) {
114         ANS_LOGE("GetAnsManagerProxy fail.");
115         return ERR_ANS_SERVICE_NOT_CONNECTED;
116     }
117 
118     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
119     return ansManagerProxy_->GetSlotNumAsBundle(bo, num);
120 }
121 
PublishNotification(const NotificationRequest & request)122 ErrCode AnsNotification::PublishNotification(const NotificationRequest &request)
123 {
124     ANS_LOGI("enter");
125     return PublishNotification(std::string(), request);
126 }
127 
PublishNotification(const std::string & label,const NotificationRequest & request)128 ErrCode AnsNotification::PublishNotification(const std::string &label, const NotificationRequest &request)
129 {
130     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
131     ANS_LOGI("enter");
132 
133     if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
134         ANS_LOGE("Refuse to publish the notification without valid content");
135         return ERR_ANS_INVALID_PARAM;
136     }
137 
138     if (!CanPublishMediaContent(request)) {
139         ANS_LOGE("Refuse to publish the notification because the sequence numbers actions not match those assigned to "
140                  "added action buttons.");
141         return ERR_ANS_INVALID_PARAM;
142     }
143 
144     ErrCode checkErr = CheckImageSize(request);
145     if (checkErr != ERR_OK) {
146         ANS_LOGE("The size of one picture exceeds the limit");
147         return checkErr;
148     }
149 
150     if (!GetAnsManagerProxy()) {
151         ANS_LOGE("GetAnsManagerProxy fail.");
152         return ERR_ANS_SERVICE_NOT_CONNECTED;
153     }
154 
155     sptr<NotificationRequest> reqPtr = new (std::nothrow) NotificationRequest(request);
156     if (reqPtr == nullptr) {
157         ANS_LOGE("Failed to create NotificationRequest ptr");
158         return ERR_ANS_NO_MEMORY;
159     }
160     if (IsNonDistributedNotificationType(reqPtr->GetNotificationType())) {
161         reqPtr->SetDistributed(false);
162     }
163     return ansManagerProxy_->Publish(label, reqPtr);
164 }
165 
CancelNotification(int32_t notificationId)166 ErrCode AnsNotification::CancelNotification(int32_t notificationId)
167 {
168     return CancelNotification("", notificationId);
169 }
170 
CancelNotification(const std::string & label,int32_t notificationId)171 ErrCode AnsNotification::CancelNotification(const std::string &label, int32_t notificationId)
172 {
173     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
174     if (!GetAnsManagerProxy()) {
175         ANS_LOGE("GetAnsManagerProxy fail.");
176         return ERR_ANS_SERVICE_NOT_CONNECTED;
177     }
178     return ansManagerProxy_->Cancel(notificationId, label);
179 }
180 
CancelAllNotifications()181 ErrCode AnsNotification::CancelAllNotifications()
182 {
183     if (!GetAnsManagerProxy()) {
184         ANS_LOGE("GetAnsManagerProxy fail.");
185         return ERR_ANS_SERVICE_NOT_CONNECTED;
186     }
187     return ansManagerProxy_->CancelAll();
188 }
189 
CancelAsBundle(int32_t notificationId,const std::string & representativeBundle,int32_t userId)190 ErrCode AnsNotification::CancelAsBundle(
191     int32_t notificationId, const std::string &representativeBundle, int32_t userId)
192 {
193     if (!GetAnsManagerProxy()) {
194         ANS_LOGE("GetAnsManagerProxy fail.");
195         return ERR_ANS_SERVICE_NOT_CONNECTED;
196     }
197     return ansManagerProxy_->CancelAsBundle(notificationId, representativeBundle, userId);
198 }
199 
GetActiveNotificationNums(uint64_t & num)200 ErrCode AnsNotification::GetActiveNotificationNums(uint64_t &num)
201 {
202     if (!GetAnsManagerProxy()) {
203         ANS_LOGE("GetAnsManagerProxy fail.");
204         return ERR_ANS_SERVICE_NOT_CONNECTED;
205     }
206     return ansManagerProxy_->GetActiveNotificationNums(num);
207 }
208 
GetActiveNotifications(std::vector<sptr<NotificationRequest>> & request)209 ErrCode AnsNotification::GetActiveNotifications(std::vector<sptr<NotificationRequest>> &request)
210 {
211     if (!GetAnsManagerProxy()) {
212         ANS_LOGE("GetAnsManagerProxy fail.");
213         return ERR_ANS_SERVICE_NOT_CONNECTED;
214     }
215     return ansManagerProxy_->GetActiveNotifications(request);
216 }
217 
SetNotificationAgent(const std::string & agent)218 ErrCode AnsNotification::SetNotificationAgent(const std::string &agent)
219 {
220     if (!GetAnsManagerProxy()) {
221         ANS_LOGE("GetAnsManagerProxy fail.");
222         return ERR_ANS_SERVICE_NOT_CONNECTED;
223     }
224     return ansManagerProxy_->SetNotificationAgent(agent);
225 }
226 
GetNotificationAgent(std::string & agent)227 ErrCode AnsNotification::GetNotificationAgent(std::string &agent)
228 {
229     if (!GetAnsManagerProxy()) {
230         ANS_LOGE("GetAnsManagerProxy fail.");
231         return ERR_ANS_SERVICE_NOT_CONNECTED;
232     }
233     return ansManagerProxy_->GetNotificationAgent(agent);
234 }
235 
CanPublishNotificationAsBundle(const std::string & representativeBundle,bool & canPublish)236 ErrCode AnsNotification::CanPublishNotificationAsBundle(const std::string &representativeBundle, bool &canPublish)
237 {
238     if (representativeBundle.empty()) {
239         ANS_LOGW("Input representativeBundle is empty");
240         return ERR_ANS_INVALID_PARAM;
241     }
242 
243     if (!GetAnsManagerProxy()) {
244         ANS_LOGE("GetAnsManagerProxy fail.");
245         return ERR_ANS_SERVICE_NOT_CONNECTED;
246     }
247     return ansManagerProxy_->CanPublishAsBundle(representativeBundle, canPublish);
248 }
249 
PublishNotificationAsBundle(const std::string & representativeBundle,const NotificationRequest & request)250 ErrCode AnsNotification::PublishNotificationAsBundle(
251     const std::string &representativeBundle, const NotificationRequest &request)
252 {
253     if (representativeBundle.empty()) {
254         ANS_LOGE("Refuse to publish the notification whit invalid representativeBundle");
255         return ERR_ANS_INVALID_PARAM;
256     }
257 
258     if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
259         ANS_LOGE("Refuse to publish the notification without valid content");
260         return ERR_ANS_INVALID_PARAM;
261     }
262 
263     if (!CanPublishMediaContent(request)) {
264         ANS_LOGE("Refuse to publish the notification because the sequence numbers actions not match those assigned to "
265                  "added action buttons.");
266         return ERR_ANS_INVALID_PARAM;
267     }
268 
269     ErrCode checkErr = CheckImageSize(request);
270     if (checkErr != ERR_OK) {
271         ANS_LOGE("The size of one picture exceeds the limit");
272         return checkErr;
273     }
274 
275     if (!GetAnsManagerProxy()) {
276         ANS_LOGE("GetAnsManagerProxy fail.");
277         return ERR_ANS_SERVICE_NOT_CONNECTED;
278     }
279 
280     sptr<NotificationRequest> reqPtr = new (std::nothrow) NotificationRequest(request);
281     if (reqPtr == nullptr) {
282         ANS_LOGE("Failed to create NotificationRequest ptr");
283         return ERR_ANS_NO_MEMORY;
284     }
285     if (IsNonDistributedNotificationType(reqPtr->GetNotificationType())) {
286         reqPtr->SetDistributed(false);
287     }
288     return ansManagerProxy_->PublishAsBundle(reqPtr, representativeBundle);
289 }
290 
SetNotificationBadgeNum()291 ErrCode AnsNotification::SetNotificationBadgeNum()
292 {
293     if (!GetAnsManagerProxy()) {
294         ANS_LOGE("GetAnsManagerProxy fail.");
295         return ERR_ANS_SERVICE_NOT_CONNECTED;
296     }
297     int32_t num = -1;
298     return ansManagerProxy_->SetNotificationBadgeNum(num);
299 }
300 
SetNotificationBadgeNum(int32_t num)301 ErrCode AnsNotification::SetNotificationBadgeNum(int32_t num)
302 {
303     if (!GetAnsManagerProxy()) {
304         ANS_LOGE("GetAnsManagerProxy fail.");
305         return ERR_ANS_SERVICE_NOT_CONNECTED;
306     }
307     return ansManagerProxy_->SetNotificationBadgeNum(num);
308 }
309 
IsAllowedNotify(bool & allowed)310 ErrCode AnsNotification::IsAllowedNotify(bool &allowed)
311 {
312     if (!GetAnsManagerProxy()) {
313         ANS_LOGE("GetAnsManagerProxy fail.");
314         return ERR_ANS_SERVICE_NOT_CONNECTED;
315     }
316     return ansManagerProxy_->IsAllowedNotify(allowed);
317 }
318 
IsAllowedNotifySelf(bool & allowed)319 ErrCode AnsNotification::IsAllowedNotifySelf(bool &allowed)
320 {
321     ANS_LOGD("enter");
322     if (!GetAnsManagerProxy()) {
323         ANS_LOGE("GetAnsManagerProxy fail.");
324         return ERR_ANS_SERVICE_NOT_CONNECTED;
325     }
326     return ansManagerProxy_->IsAllowedNotifySelf(allowed);
327 }
328 
RequestEnableNotification(std::string & deviceId,sptr<IRemoteObject> & callerToken)329 ErrCode AnsNotification::RequestEnableNotification(std::string &deviceId, sptr<IRemoteObject> &callerToken)
330 {
331     ANS_LOGD("enter");
332     if (!GetAnsManagerProxy()) {
333         ANS_LOGE("GetAnsManagerProxy fail.");
334         return ERR_ANS_SERVICE_NOT_CONNECTED;
335     }
336     return ansManagerProxy_->RequestEnableNotification(deviceId, callerToken);
337 }
338 
HasNotificationPolicyAccessPermission(bool & hasPermission)339 ErrCode AnsNotification::HasNotificationPolicyAccessPermission(bool &hasPermission)
340 {
341     if (!GetAnsManagerProxy()) {
342         ANS_LOGE("GetAnsManagerProxy fail.");
343         return ERR_ANS_SERVICE_NOT_CONNECTED;
344     }
345     return ansManagerProxy_->HasNotificationPolicyAccessPermission(hasPermission);
346 }
347 
GetBundleImportance(NotificationSlot::NotificationLevel & importance)348 ErrCode AnsNotification::GetBundleImportance(NotificationSlot::NotificationLevel &importance)
349 {
350     if (!GetAnsManagerProxy()) {
351         ANS_LOGE("GetAnsManagerProxy fail.");
352         return ERR_ANS_SERVICE_NOT_CONNECTED;
353     }
354     int32_t importanceTemp;
355     ErrCode ret = ansManagerProxy_->GetBundleImportance(importanceTemp);
356     if ((NotificationSlot::LEVEL_NONE <= importanceTemp) && (importanceTemp <= NotificationSlot::LEVEL_HIGH)) {
357         importance = static_cast<NotificationSlot::NotificationLevel>(importanceTemp);
358     } else {
359         importance = NotificationSlot::LEVEL_UNDEFINED;
360     }
361     return ret;
362 }
363 
SubscribeNotification(const NotificationSubscriber & subscriber)364 ErrCode AnsNotification::SubscribeNotification(const NotificationSubscriber &subscriber)
365 {
366     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
367     if (!GetAnsManagerProxy()) {
368         ANS_LOGE("GetAnsManagerProxy fail.");
369         return ERR_ANS_SERVICE_NOT_CONNECTED;
370     }
371 
372     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
373     if (subscriberSptr == nullptr) {
374         ANS_LOGE("Failed to subscribe with SubscriberImpl null ptr.");
375         return ERR_ANS_INVALID_PARAM;
376     }
377     return ansManagerProxy_->Subscribe(subscriberSptr, nullptr);
378 }
379 
SubscribeNotification(const NotificationSubscriber & subscriber,const NotificationSubscribeInfo & subscribeInfo)380 ErrCode AnsNotification::SubscribeNotification(
381     const NotificationSubscriber &subscriber, const NotificationSubscribeInfo &subscribeInfo)
382 {
383     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
384     if (!GetAnsManagerProxy()) {
385         ANS_LOGE("GetAnsManagerProxy fail.");
386         return ERR_ANS_SERVICE_NOT_CONNECTED;
387     }
388 
389     sptr<NotificationSubscribeInfo> sptrInfo = new (std::nothrow) NotificationSubscribeInfo(subscribeInfo);
390     if (sptrInfo == nullptr) {
391         ANS_LOGE("Failed to create NotificationSubscribeInfo ptr.");
392         return ERR_ANS_NO_MEMORY;
393     }
394 
395     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
396     if (subscriberSptr == nullptr) {
397         ANS_LOGE("Failed to subscribe with SubscriberImpl null ptr.");
398         return ERR_ANS_INVALID_PARAM;
399     }
400     return ansManagerProxy_->Subscribe(subscriberSptr, sptrInfo);
401 }
402 
UnSubscribeNotification(NotificationSubscriber & subscriber)403 ErrCode AnsNotification::UnSubscribeNotification(NotificationSubscriber &subscriber)
404 {
405     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
406     if (!GetAnsManagerProxy()) {
407         ANS_LOGE("GetAnsManagerProxy fail.");
408         return ERR_ANS_SERVICE_NOT_CONNECTED;
409     }
410 
411     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
412     if (subscriberSptr == nullptr) {
413         ANS_LOGE("Failed to unsubscribe with SubscriberImpl null ptr.");
414         return ERR_ANS_INVALID_PARAM;
415     }
416     return ansManagerProxy_->Unsubscribe(subscriberSptr, nullptr);
417 }
418 
UnSubscribeNotification(NotificationSubscriber & subscriber,NotificationSubscribeInfo subscribeInfo)419 ErrCode AnsNotification::UnSubscribeNotification(
420     NotificationSubscriber &subscriber, NotificationSubscribeInfo subscribeInfo)
421 {
422     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
423     if (!GetAnsManagerProxy()) {
424         ANS_LOGE("GetAnsManagerProxy fail.");
425         return ERR_ANS_SERVICE_NOT_CONNECTED;
426     }
427 
428     sptr<NotificationSubscribeInfo> sptrInfo = new (std::nothrow) NotificationSubscribeInfo(subscribeInfo);
429     if (sptrInfo == nullptr) {
430         ANS_LOGE("Failed to create NotificationSubscribeInfo ptr.");
431         return ERR_ANS_NO_MEMORY;
432     }
433 
434     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
435     if (subscriberSptr == nullptr) {
436         ANS_LOGE("Failed to unsubscribe with SubscriberImpl null ptr.");
437         return ERR_ANS_INVALID_PARAM;
438     }
439     return ansManagerProxy_->Unsubscribe(subscriberSptr, sptrInfo);
440 }
441 
RemoveNotification(const std::string & key,int32_t removeReason)442 ErrCode AnsNotification::RemoveNotification(const std::string &key, int32_t removeReason)
443 {
444     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
445     if (key.empty()) {
446         ANS_LOGW("Input key is empty.");
447         return ERR_ANS_INVALID_PARAM;
448     }
449 
450     if (!GetAnsManagerProxy()) {
451         ANS_LOGE("GetAnsManagerProxy fail.");
452         return ERR_ANS_SERVICE_NOT_CONNECTED;
453     }
454     return ansManagerProxy_->Delete(key, removeReason);
455 }
456 
RemoveNotification(const NotificationBundleOption & bundleOption,const int32_t notificationId,const std::string & label,int32_t removeReason)457 ErrCode AnsNotification::RemoveNotification(const NotificationBundleOption &bundleOption,
458     const int32_t notificationId, const std::string &label, int32_t removeReason)
459 {
460     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
461     if (bundleOption.GetBundleName().empty()) {
462         ANS_LOGE("Invalid bundle name.");
463         return ERR_ANS_INVALID_PARAM;
464     }
465 
466     if (!GetAnsManagerProxy()) {
467         ANS_LOGE("GetAnsManagerProxy fail.");
468         return ERR_ANS_SERVICE_NOT_CONNECTED;
469     }
470 
471     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
472     return ansManagerProxy_->RemoveNotification(bo, notificationId, label, removeReason);
473 }
474 
RemoveAllNotifications(const NotificationBundleOption & bundleOption)475 ErrCode AnsNotification::RemoveAllNotifications(const NotificationBundleOption &bundleOption)
476 {
477     if (bundleOption.GetBundleName().empty()) {
478         ANS_LOGE("Invalid bundle name.");
479         return ERR_ANS_INVALID_PARAM;
480     }
481 
482     if (!GetAnsManagerProxy()) {
483         ANS_LOGE("GetAnsManagerProxy fail.");
484         return ERR_ANS_SERVICE_NOT_CONNECTED;
485     }
486 
487     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
488     return ansManagerProxy_->RemoveAllNotifications(bo);
489 }
490 
RemoveNotifications(const std::vector<std::string> hashcodes,int32_t removeReason)491 ErrCode AnsNotification::RemoveNotifications(const std::vector<std::string> hashcodes, int32_t removeReason)
492 {
493     if (hashcodes.empty()) {
494         ANS_LOGE("Hashcodes is empty");
495         return ERR_ANS_INVALID_PARAM;
496     }
497 
498     if (!GetAnsManagerProxy()) {
499         ANS_LOGE("GetAnsManagerProxy fail.");
500         return ERR_ANS_SERVICE_NOT_CONNECTED;
501     }
502 
503     return ansManagerProxy_->RemoveNotifications(hashcodes, removeReason);
504 }
505 
RemoveNotificationsByBundle(const NotificationBundleOption & bundleOption)506 ErrCode AnsNotification::RemoveNotificationsByBundle(const NotificationBundleOption &bundleOption)
507 {
508     if (bundleOption.GetBundleName().empty()) {
509         ANS_LOGE("Invalid bundle name.");
510         return ERR_ANS_INVALID_PARAM;
511     }
512 
513     if (!GetAnsManagerProxy()) {
514         ANS_LOGE("GetAnsManagerProxy fail.");
515         return ERR_ANS_SERVICE_NOT_CONNECTED;
516     }
517 
518     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
519     return ansManagerProxy_->DeleteByBundle(bo);
520 }
521 
RemoveNotifications()522 ErrCode AnsNotification::RemoveNotifications()
523 {
524     if (!GetAnsManagerProxy()) {
525         ANS_LOGE("GetAnsManagerProxy fail.");
526         return ERR_ANS_SERVICE_NOT_CONNECTED;
527     }
528     return ansManagerProxy_->DeleteAll();
529 }
530 
GetNotificationSlotsForBundle(const NotificationBundleOption & bundleOption,std::vector<sptr<NotificationSlot>> & slots)531 ErrCode AnsNotification::GetNotificationSlotsForBundle(
532     const NotificationBundleOption &bundleOption, std::vector<sptr<NotificationSlot>> &slots)
533 {
534     if (bundleOption.GetBundleName().empty()) {
535         ANS_LOGE("Input bundleName is empty.");
536         return ERR_ANS_INVALID_PARAM;
537     }
538 
539     if (!GetAnsManagerProxy()) {
540         ANS_LOGE("GetAnsManagerProxy fail.");
541         return ERR_ANS_SERVICE_NOT_CONNECTED;
542     }
543 
544     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
545     return ansManagerProxy_->GetSlotsByBundle(bo, slots);
546 }
547 
UpdateNotificationSlots(const NotificationBundleOption & bundleOption,const std::vector<sptr<NotificationSlot>> & slots)548 ErrCode AnsNotification::UpdateNotificationSlots(
549     const NotificationBundleOption &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
550 {
551     if (bundleOption.GetBundleName().empty()) {
552         ANS_LOGE("Invalid bundle name.");
553         return ERR_ANS_INVALID_PARAM;
554     }
555 
556     if (!GetAnsManagerProxy()) {
557         ANS_LOGE("GetAnsManagerProxy fail.");
558         return ERR_ANS_SERVICE_NOT_CONNECTED;
559     }
560 
561     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
562     return ansManagerProxy_->UpdateSlots(bo, slots);
563 }
564 
GetAllActiveNotifications(std::vector<sptr<Notification>> & notification)565 ErrCode AnsNotification::GetAllActiveNotifications(std::vector<sptr<Notification>> &notification)
566 {
567     if (!GetAnsManagerProxy()) {
568         ANS_LOGE("GetAnsManagerProxy fail.");
569         return ERR_ANS_SERVICE_NOT_CONNECTED;
570     }
571     return ansManagerProxy_->GetAllActiveNotifications(notification);
572 }
573 
GetAllActiveNotifications(const std::vector<std::string> key,std::vector<sptr<Notification>> & notification)574 ErrCode AnsNotification::GetAllActiveNotifications(
575     const std::vector<std::string> key, std::vector<sptr<Notification>> &notification)
576 {
577     if (!GetAnsManagerProxy()) {
578         ANS_LOGE("GetAnsManagerProxy fail.");
579         return ERR_ANS_SERVICE_NOT_CONNECTED;
580     }
581     return ansManagerProxy_->GetSpecialActiveNotifications(key, notification);
582 }
583 
IsAllowedNotify(const NotificationBundleOption & bundleOption,bool & allowed)584 ErrCode AnsNotification::IsAllowedNotify(const NotificationBundleOption &bundleOption, bool &allowed)
585 {
586     if (bundleOption.GetBundleName().empty()) {
587         ANS_LOGE("Input bundle is empty.");
588         return ERR_ANS_INVALID_PARAM;
589     }
590 
591     if (!GetAnsManagerProxy()) {
592         ANS_LOGE("GetAnsManagerProxy fail.");
593         return ERR_ANS_SERVICE_NOT_CONNECTED;
594     }
595 
596     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
597     return ansManagerProxy_->IsSpecialBundleAllowedNotify(bo, allowed);
598 }
599 
SetNotificationsEnabledForAllBundles(const std::string & deviceId,bool enabled)600 ErrCode AnsNotification::SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled)
601 {
602     if (!GetAnsManagerProxy()) {
603         ANS_LOGE("GetAnsManagerProxy fail.");
604         return ERR_ANS_SERVICE_NOT_CONNECTED;
605     }
606     return ansManagerProxy_->SetNotificationsEnabledForAllBundles(deviceId, enabled);
607 }
608 
SetNotificationsEnabledForDefaultBundle(const std::string & deviceId,bool enabled)609 ErrCode AnsNotification::SetNotificationsEnabledForDefaultBundle(const std::string &deviceId, bool enabled)
610 {
611     if (!GetAnsManagerProxy()) {
612         ANS_LOGE("GetAnsManagerProxy fail.");
613         return ERR_ANS_SERVICE_NOT_CONNECTED;
614     }
615     return ansManagerProxy_->SetNotificationsEnabledForBundle(deviceId, enabled);
616 }
617 
SetNotificationsEnabledForSpecifiedBundle(const NotificationBundleOption & bundleOption,const std::string & deviceId,bool enabled)618 ErrCode AnsNotification::SetNotificationsEnabledForSpecifiedBundle(
619     const NotificationBundleOption &bundleOption, const std::string &deviceId, bool enabled)
620 {
621     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
622     if (bundleOption.GetBundleName().empty()) {
623         ANS_LOGE("Invalid bundle name.");
624         return ERR_ANS_INVALID_PARAM;
625     }
626 
627     if (!GetAnsManagerProxy()) {
628         ANS_LOGE("GetAnsManagerProxy fail.");
629         return ERR_ANS_SERVICE_NOT_CONNECTED;
630     }
631 
632     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
633     return ansManagerProxy_->SetNotificationsEnabledForSpecialBundle(deviceId, bo, enabled);
634 }
635 
SetShowBadgeEnabledForBundle(const NotificationBundleOption & bundleOption,bool enabled)636 ErrCode AnsNotification::SetShowBadgeEnabledForBundle(const NotificationBundleOption &bundleOption, bool enabled)
637 {
638     if (bundleOption.GetBundleName().empty()) {
639         ANS_LOGE("Invalid bundle name.");
640         return ERR_ANS_INVALID_PARAM;
641     }
642 
643     if (!GetAnsManagerProxy()) {
644         ANS_LOGE("GetAnsManagerProxy fail.");
645         return ERR_ANS_SERVICE_NOT_CONNECTED;
646     }
647 
648     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
649     return ansManagerProxy_->SetShowBadgeEnabledForBundle(bo, enabled);
650 }
651 
GetShowBadgeEnabledForBundle(const NotificationBundleOption & bundleOption,bool & enabled)652 ErrCode AnsNotification::GetShowBadgeEnabledForBundle(const NotificationBundleOption &bundleOption, bool &enabled)
653 {
654     if (bundleOption.GetBundleName().empty()) {
655         ANS_LOGE("Invalid bundle name.");
656         return ERR_ANS_INVALID_PARAM;
657     }
658 
659     if (!GetAnsManagerProxy()) {
660         ANS_LOGE("GetAnsManagerProxy fail.");
661         return ERR_ANS_SERVICE_NOT_CONNECTED;
662     }
663 
664     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
665     return ansManagerProxy_->GetShowBadgeEnabledForBundle(bo, enabled);
666 }
667 
GetShowBadgeEnabled(bool & enabled)668 ErrCode AnsNotification::GetShowBadgeEnabled(bool &enabled)
669 {
670     if (!GetAnsManagerProxy()) {
671         ANS_LOGE("GetAnsManagerProxy fail.");
672         return ERR_ANS_SERVICE_NOT_CONNECTED;
673     }
674 
675     return ansManagerProxy_->GetShowBadgeEnabled(enabled);
676 }
677 
CancelGroup(const std::string & groupName)678 ErrCode AnsNotification::CancelGroup(const std::string &groupName)
679 {
680     if (groupName.empty()) {
681         ANS_LOGE("Invalid group name.");
682         return ERR_ANS_INVALID_PARAM;
683     }
684 
685     if (!GetAnsManagerProxy()) {
686         ANS_LOGE("GetAnsManagerProxy fail.");
687         return ERR_ANS_SERVICE_NOT_CONNECTED;
688     }
689 
690     return ansManagerProxy_->CancelGroup(groupName);
691 }
692 
RemoveGroupByBundle(const NotificationBundleOption & bundleOption,const std::string & groupName)693 ErrCode AnsNotification::RemoveGroupByBundle(
694     const NotificationBundleOption &bundleOption, const std::string &groupName)
695 {
696     if (bundleOption.GetBundleName().empty() || groupName.empty()) {
697         ANS_LOGE("Invalid parameter.");
698         return ERR_ANS_INVALID_PARAM;
699     }
700 
701     if (!GetAnsManagerProxy()) {
702         ANS_LOGE("GetAnsManagerProxy fail.");
703         return ERR_ANS_SERVICE_NOT_CONNECTED;
704     }
705 
706     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
707     return ansManagerProxy_->RemoveGroupByBundle(bo, groupName);
708 }
709 
SetDoNotDisturbDate(const NotificationDoNotDisturbDate & doNotDisturbDate)710 ErrCode AnsNotification::SetDoNotDisturbDate(const NotificationDoNotDisturbDate &doNotDisturbDate)
711 {
712     if (!GetAnsManagerProxy()) {
713         ANS_LOGE("GetAnsManagerProxy fail.");
714         return ERR_ANS_SERVICE_NOT_CONNECTED;
715     }
716 
717     auto dndDatePtr = new (std::nothrow) NotificationDoNotDisturbDate(doNotDisturbDate);
718     if (dndDatePtr == nullptr) {
719         ANS_LOGE("create DoNotDisturbDate failed.");
720         return ERR_ANS_NO_MEMORY;
721     }
722 
723     sptr<NotificationDoNotDisturbDate> dndDate(dndDatePtr);
724     return ansManagerProxy_->SetDoNotDisturbDate(dndDate);
725 }
726 
GetDoNotDisturbDate(NotificationDoNotDisturbDate & doNotDisturbDate)727 ErrCode AnsNotification::GetDoNotDisturbDate(NotificationDoNotDisturbDate &doNotDisturbDate)
728 {
729     if (!GetAnsManagerProxy()) {
730         ANS_LOGE("GetAnsManagerProxy fail.");
731         return ERR_ANS_SERVICE_NOT_CONNECTED;
732     }
733 
734     sptr<NotificationDoNotDisturbDate> dndDate = nullptr;
735     auto ret = ansManagerProxy_->GetDoNotDisturbDate(dndDate);
736     if (ret != ERR_OK) {
737         ANS_LOGE("Get DoNotDisturbDate failed.");
738         return ret;
739     }
740 
741     if (!dndDate) {
742         ANS_LOGE("Invalid DoNotDisturbDate.");
743         return ERR_ANS_NO_MEMORY;
744     }
745 
746     doNotDisturbDate = *dndDate;
747     return ret;
748 }
749 
DoesSupportDoNotDisturbMode(bool & doesSupport)750 ErrCode AnsNotification::DoesSupportDoNotDisturbMode(bool &doesSupport)
751 {
752     if (!GetAnsManagerProxy()) {
753         ANS_LOGE("GetAnsManagerProxy fail.");
754         return ERR_ANS_SERVICE_NOT_CONNECTED;
755     }
756 
757     return ansManagerProxy_->DoesSupportDoNotDisturbMode(doesSupport);
758 }
759 
PublishContinuousTaskNotification(const NotificationRequest & request)760 ErrCode AnsNotification::PublishContinuousTaskNotification(const NotificationRequest &request)
761 {
762     if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
763         ANS_LOGE("Refuse to publish the notification without valid content");
764         return ERR_ANS_INVALID_PARAM;
765     }
766 
767     if (!CanPublishMediaContent(request)) {
768         ANS_LOGE("Refuse to publish the notification because the sequence numbers actions not match those assigned to "
769                  "added action buttons.");
770         return ERR_ANS_INVALID_PARAM;
771     }
772 
773     ErrCode checkErr = CheckImageSize(request);
774     if (checkErr != ERR_OK) {
775         ANS_LOGE("The size of one picture exceeds the limit");
776         return checkErr;
777     }
778 
779     if (!GetAnsManagerProxy()) {
780         ANS_LOGE("GetAnsManagerProxy fail.");
781         return ERR_ANS_SERVICE_NOT_CONNECTED;
782     }
783 
784     auto pReq = new (std::nothrow) NotificationRequest(request);
785     if (pReq == nullptr) {
786         ANS_LOGE("Failed to create NotificationRequest ptr.");
787         return ERR_ANS_NO_MEMORY;
788     }
789 
790     sptr<NotificationRequest> sptrReq(pReq);
791     if (IsNonDistributedNotificationType(sptrReq->GetNotificationType())) {
792         sptrReq->SetDistributed(false);
793     }
794     return ansManagerProxy_->PublishContinuousTaskNotification(sptrReq);
795 }
796 
CancelContinuousTaskNotification(const std::string & label,int32_t notificationId)797 ErrCode AnsNotification::CancelContinuousTaskNotification(const std::string &label, int32_t notificationId)
798 {
799     if (!GetAnsManagerProxy()) {
800         ANS_LOGE("GetAnsManagerProxy fail.");
801         return ERR_ANS_SERVICE_NOT_CONNECTED;
802     }
803 
804     return ansManagerProxy_->CancelContinuousTaskNotification(label, notificationId);
805 }
806 
IsDistributedEnabled(bool & enabled)807 ErrCode AnsNotification::IsDistributedEnabled(bool &enabled)
808 {
809     if (!GetAnsManagerProxy()) {
810         ANS_LOGE("GetAnsManagerProxy fail.");
811         return ERR_ANS_SERVICE_NOT_CONNECTED;
812     }
813 
814     return ansManagerProxy_->IsDistributedEnabled(enabled);
815 }
816 
EnableDistributed(const bool enabled)817 ErrCode AnsNotification::EnableDistributed(const bool enabled)
818 {
819     if (!GetAnsManagerProxy()) {
820         ANS_LOGE("GetAnsManagerProxy fail.");
821         return ERR_ANS_SERVICE_NOT_CONNECTED;
822     }
823 
824     return ansManagerProxy_->EnableDistributed(enabled);
825 }
826 
EnableDistributedByBundle(const NotificationBundleOption & bundleOption,const bool enabled)827 ErrCode AnsNotification::EnableDistributedByBundle(const NotificationBundleOption &bundleOption, const bool enabled)
828 {
829     if (!GetAnsManagerProxy()) {
830         ANS_LOGE("GetAnsManagerProxy fail.");
831         return ERR_ANS_SERVICE_NOT_CONNECTED;
832     }
833 
834     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
835     return ansManagerProxy_->EnableDistributedByBundle(bo, enabled);
836 }
837 
EnableDistributedSelf(const bool enabled)838 ErrCode AnsNotification::EnableDistributedSelf(const bool enabled)
839 {
840     if (!GetAnsManagerProxy()) {
841         ANS_LOGE("GetAnsManagerProxy fail.");
842         return ERR_ANS_SERVICE_NOT_CONNECTED;
843     }
844 
845     return ansManagerProxy_->EnableDistributedSelf(enabled);
846 }
847 
IsDistributedEnableByBundle(const NotificationBundleOption & bundleOption,bool & enabled)848 ErrCode AnsNotification::IsDistributedEnableByBundle(const NotificationBundleOption &bundleOption, bool &enabled)
849 {
850     if (!GetAnsManagerProxy()) {
851         ANS_LOGE("GetAnsManagerProxy fail.");
852         return ERR_ANS_SERVICE_NOT_CONNECTED;
853     }
854 
855     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
856     return ansManagerProxy_->IsDistributedEnableByBundle(bo, enabled);
857 }
858 
GetDeviceRemindType(NotificationConstant::RemindType & remindType)859 ErrCode AnsNotification::GetDeviceRemindType(NotificationConstant::RemindType &remindType)
860 {
861     if (!GetAnsManagerProxy()) {
862         ANS_LOGE("GetAnsManagerProxy fail.");
863         return ERR_ANS_SERVICE_NOT_CONNECTED;
864     }
865 
866     return ansManagerProxy_->GetDeviceRemindType(remindType);
867 }
868 
ResetAnsManagerProxy()869 void AnsNotification::ResetAnsManagerProxy()
870 {
871     ANS_LOGI("enter");
872     std::lock_guard<std::mutex> lock(mutex_);
873     if ((ansManagerProxy_ != nullptr) && (ansManagerProxy_->AsObject() != nullptr)) {
874         ansManagerProxy_->AsObject()->RemoveDeathRecipient(recipient_);
875     }
876     ansManagerProxy_ = nullptr;
877 }
878 
PublishReminder(ReminderRequest & reminder)879 ErrCode AnsNotification::PublishReminder(ReminderRequest &reminder)
880 {
881     if (!GetAnsManagerProxy()) {
882         ANS_LOGE("GetAnsManagerProxy fail.");
883         return ERR_ANS_SERVICE_NOT_CONNECTED;
884     }
885     sptr<ReminderRequest> tarReminder = nullptr;
886     switch (reminder.GetReminderType()) {
887         case (ReminderRequest::ReminderType::TIMER): {
888             ANSR_LOGI("Publish timer");
889             ReminderRequestTimer &timer = (ReminderRequestTimer &)reminder;
890             tarReminder = new (std::nothrow) ReminderRequestTimer(timer);
891             break;
892         }
893         case (ReminderRequest::ReminderType::ALARM): {
894             ANSR_LOGI("Publish alarm");
895             ReminderRequestAlarm &alarm = (ReminderRequestAlarm &)reminder;
896             tarReminder = new (std::nothrow) ReminderRequestAlarm(alarm);
897             break;
898         }
899         case (ReminderRequest::ReminderType::CALENDAR): {
900             ANSR_LOGI("Publish calendar");
901             ReminderRequestCalendar &calendar = (ReminderRequestCalendar &)reminder;
902             tarReminder = new (std::nothrow) ReminderRequestCalendar(calendar);
903             break;
904         }
905         default: {
906             ANSR_LOGW("PublishReminder fail.");
907             return ERR_ANS_INVALID_PARAM;
908         }
909     }
910     ErrCode code = ansManagerProxy_->PublishReminder(tarReminder);
911     reminder.SetReminderId(tarReminder->GetReminderId());
912     return code;
913 }
914 
CancelReminder(const int32_t reminderId)915 ErrCode AnsNotification::CancelReminder(const int32_t reminderId)
916 {
917     if (!GetAnsManagerProxy()) {
918         ANS_LOGE("GetAnsManagerProxy fail.");
919         return ERR_ANS_SERVICE_NOT_CONNECTED;
920     }
921     return ansManagerProxy_->CancelReminder(reminderId);
922 }
923 
CancelAllReminders()924 ErrCode AnsNotification::CancelAllReminders()
925 {
926     if (!GetAnsManagerProxy()) {
927         ANS_LOGE("GetAnsManagerProxy fail.");
928         return ERR_ANS_SERVICE_NOT_CONNECTED;
929     }
930     return ansManagerProxy_->CancelAllReminders();
931 }
932 
GetValidReminders(std::vector<sptr<ReminderRequest>> & validReminders)933 ErrCode AnsNotification::GetValidReminders(std::vector<sptr<ReminderRequest>> &validReminders)
934 {
935     if (!GetAnsManagerProxy()) {
936         ANS_LOGE("GetAnsManagerProxy fail.");
937         return ERR_ANS_SERVICE_NOT_CONNECTED;
938     }
939     return ansManagerProxy_->GetValidReminders(validReminders);
940 }
941 
GetAnsManagerProxy()942 bool AnsNotification::GetAnsManagerProxy()
943 {
944     if (!ansManagerProxy_) {
945         std::lock_guard<std::mutex> lock(mutex_);
946         if (!ansManagerProxy_) {
947             sptr<ISystemAbilityManager> systemAbilityManager =
948                 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
949             if (!systemAbilityManager) {
950                 ANS_LOGE("Failed to get system ability mgr.");
951                 return false;
952             }
953 
954             sptr<IRemoteObject> remoteObject =
955                 systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
956             if (!remoteObject) {
957                 ANS_LOGE("Failed to get notification Manager.");
958                 return false;
959             }
960 
961             ansManagerProxy_ = iface_cast<AnsManagerInterface>(remoteObject);
962             if ((!ansManagerProxy_) || (!ansManagerProxy_->AsObject())) {
963                 ANS_LOGE("Failed to get notification Manager's proxy");
964                 return false;
965             }
966 
967             recipient_ = new (std::nothrow) AnsManagerDeathRecipient();
968             if (!recipient_) {
969                 ANS_LOGE("Failed to create death recipient");
970                 return false;
971             }
972             ansManagerProxy_->AsObject()->AddDeathRecipient(recipient_);
973         }
974     }
975 
976     return true;
977 }
978 
CanPublishMediaContent(const NotificationRequest & request) const979 bool AnsNotification::CanPublishMediaContent(const NotificationRequest &request) const
980 {
981     if (NotificationContent::Type::MEDIA != request.GetNotificationType()) {
982         return true;
983     }
984 
985     if (request.GetContent() == nullptr) {
986         ANS_LOGE("Failed to publish notification with null content.");
987         return false;
988     }
989 
990     auto media = std::static_pointer_cast<NotificationMediaContent>(request.GetContent()->GetNotificationContent());
991     if (media == nullptr) {
992         ANS_LOGE("Failed to get media content.");
993         return false;
994     }
995 
996     auto showActions = media->GetShownActions();
997     size_t size = request.GetActionButtons().size();
998     for (auto it = showActions.begin(); it != showActions.end(); ++it) {
999         if (*it > size) {
1000             ANS_LOGE("The sequence numbers actions is: %{public}d, the assigned to added action buttons size is: "
1001                      "%{public}zu.", *it, size);
1002             return false;
1003         }
1004     }
1005 
1006     return true;
1007 }
1008 
CheckImageOverSizeForPixelMap(const std::shared_ptr<Media::PixelMap> & pixelMap,uint32_t maxSize)1009 bool AnsNotification::CheckImageOverSizeForPixelMap(const std::shared_ptr<Media::PixelMap> &pixelMap, uint32_t maxSize)
1010 {
1011     if (!pixelMap) {
1012         return false;
1013     }
1014 
1015     uint32_t size = static_cast<uint32_t>(pixelMap->GetByteCount());
1016     if (size > maxSize) {
1017         return true;
1018     }
1019     return false;
1020 }
1021 
CheckImageSizeForContent(const NotificationRequest & request)1022 ErrCode AnsNotification::CheckImageSizeForContent(const NotificationRequest &request)
1023 {
1024     auto content = request.GetContent();
1025     if (!content) {
1026         ANS_LOGW("Invalid content in NotificationRequest");
1027         return ERR_OK;
1028     }
1029 
1030     auto basicContent = request.GetContent()->GetNotificationContent();
1031     if (!basicContent) {
1032         ANS_LOGW("Invalid content in NotificationRequest");
1033         return ERR_OK;
1034     }
1035 
1036     auto contentType = request.GetNotificationType();
1037     switch (contentType) {
1038         case NotificationContent::Type::CONVERSATION: {
1039             auto conversationalContent = std::static_pointer_cast<NotificationConversationalContent>(basicContent);
1040 
1041             auto picture = conversationalContent->GetMessageUser().GetPixelMap();
1042             if (CheckImageOverSizeForPixelMap(picture, MAX_ICON_SIZE)) {
1043                 ANS_LOGE("The size of picture in ConversationalContent's message user exceeds limit");
1044                 return ERR_ANS_ICON_OVER_SIZE;
1045             }
1046 
1047             auto messages = conversationalContent->GetAllConversationalMessages();
1048             for (auto &msg : messages) {
1049                 if (!msg) {
1050                     continue;
1051                 }
1052 
1053                 auto img = msg->GetSender().GetPixelMap();
1054                 if (CheckImageOverSizeForPixelMap(img, MAX_ICON_SIZE)) {
1055                     ANS_LOGE("The size of picture in ConversationalContent's message exceeds limit");
1056                     return ERR_ANS_ICON_OVER_SIZE;
1057                 }
1058             }
1059             break;
1060         }
1061         case NotificationContent::Type::PICTURE: {
1062             auto pictureContent = std::static_pointer_cast<NotificationPictureContent>(basicContent);
1063 
1064             auto bigPicture = pictureContent->GetBigPicture();
1065             if (CheckImageOverSizeForPixelMap(bigPicture, MAX_PICTURE_SIZE)) {
1066                 ANS_LOGE("The size of big picture in PictureContent exceeds limit");
1067                 return ERR_ANS_PICTURE_OVER_SIZE;
1068             }
1069             break;
1070         }
1071         default:
1072             break;
1073     }
1074 
1075     return ERR_OK;
1076 }
1077 
CheckImageSize(const NotificationRequest & request)1078 ErrCode AnsNotification::CheckImageSize(const NotificationRequest &request)
1079 {
1080     auto littleIcon = request.GetLittleIcon();
1081     if (CheckImageOverSizeForPixelMap(littleIcon, MAX_ICON_SIZE)) {
1082         ANS_LOGE("The size of little icon exceeds limit");
1083         return ERR_ANS_ICON_OVER_SIZE;
1084     }
1085 
1086     auto bigIcon = request.GetBigIcon();
1087     if (CheckImageOverSizeForPixelMap(bigIcon, MAX_ICON_SIZE)) {
1088         ANS_LOGE("The size of big icon exceeds limit");
1089         return ERR_ANS_ICON_OVER_SIZE;
1090     }
1091 
1092     ErrCode err = CheckImageSizeForContent(request);
1093     if (err != ERR_OK) {
1094         return err;
1095     }
1096 
1097     auto buttons = request.GetActionButtons();
1098     for (auto &btn : buttons) {
1099         if (!btn) {
1100             continue;
1101         }
1102         auto icon = btn->GetIcon();
1103         if (CheckImageOverSizeForPixelMap(icon, MAX_ICON_SIZE)) {
1104             ANS_LOGE("The size of icon in ActionButton exceeds limit");
1105             return ERR_ANS_ICON_OVER_SIZE;
1106         }
1107     }
1108 
1109     auto users = request.GetMessageUsers();
1110     for (auto &user : users) {
1111         if (!user) {
1112             continue;
1113         }
1114         auto icon = user->GetPixelMap();
1115         if (CheckImageOverSizeForPixelMap(icon, MAX_ICON_SIZE)) {
1116             ANS_LOGE("The size of picture in MessageUser exceeds limit");
1117             return ERR_ANS_ICON_OVER_SIZE;
1118         }
1119     }
1120 
1121     return ERR_OK;
1122 }
1123 
IsSupportTemplate(const std::string & templateName,bool & support)1124 ErrCode AnsNotification::IsSupportTemplate(const std::string &templateName, bool &support)
1125 {
1126     if (!GetAnsManagerProxy()) {
1127         ANS_LOGE("GetAnsManagerProxy fail.");
1128         return ERR_ANS_SERVICE_NOT_CONNECTED;
1129     }
1130 
1131     return ansManagerProxy_->IsSupportTemplate(templateName, support);
1132 }
1133 
IsNonDistributedNotificationType(const NotificationContent::Type & type)1134 bool AnsNotification::IsNonDistributedNotificationType(const NotificationContent::Type &type)
1135 {
1136     return ((type == NotificationContent::Type::CONVERSATION) || (type == NotificationContent::Type::PICTURE));
1137 }
1138 
IsAllowedNotify(const int32_t & userId,bool & allowed)1139 ErrCode AnsNotification::IsAllowedNotify(const int32_t &userId, bool &allowed)
1140 {
1141     if (userId <= SUBSCRIBE_USER_INIT) {
1142         ANS_LOGE("Input userId is invalid.");
1143         return ERR_ANS_INVALID_PARAM;
1144     }
1145 
1146     if (!GetAnsManagerProxy()) {
1147         ANS_LOGE("GetAnsManagerProxy fail.");
1148         return ERR_ANS_SERVICE_NOT_CONNECTED;
1149     }
1150 
1151     return ansManagerProxy_->IsSpecialUserAllowedNotify(userId, allowed);
1152 }
1153 
SetNotificationsEnabledForAllBundles(const int32_t & userId,bool enabled)1154 ErrCode AnsNotification::SetNotificationsEnabledForAllBundles(const int32_t &userId, bool enabled)
1155 {
1156     if (userId <= SUBSCRIBE_USER_INIT) {
1157         ANS_LOGE("Input userId is invalid.");
1158         return ERR_ANS_INVALID_PARAM;
1159     }
1160 
1161     if (!GetAnsManagerProxy()) {
1162         ANS_LOGE("GetAnsManagerProxy fail.");
1163         return ERR_ANS_SERVICE_NOT_CONNECTED;
1164     }
1165     return ansManagerProxy_->SetNotificationsEnabledByUser(userId, enabled);
1166 }
1167 
RemoveNotifications(const int32_t & userId)1168 ErrCode AnsNotification::RemoveNotifications(const int32_t &userId)
1169 {
1170     if (userId <= SUBSCRIBE_USER_INIT) {
1171         ANS_LOGE("Input userId is invalid.");
1172         return ERR_ANS_INVALID_PARAM;
1173     }
1174 
1175     if (!GetAnsManagerProxy()) {
1176         ANS_LOGE("GetAnsManagerProxy fail.");
1177         return ERR_ANS_SERVICE_NOT_CONNECTED;
1178     }
1179 
1180     return ansManagerProxy_->DeleteAllByUser(userId);
1181 }
1182 
SetDoNotDisturbDate(const int32_t & userId,const NotificationDoNotDisturbDate & doNotDisturbDate)1183 ErrCode AnsNotification::SetDoNotDisturbDate(const int32_t &userId,
1184     const NotificationDoNotDisturbDate &doNotDisturbDate)
1185 {
1186     if (userId <= SUBSCRIBE_USER_INIT) {
1187         ANS_LOGE("Input userId is invalid.");
1188         return ERR_ANS_INVALID_PARAM;
1189     }
1190 
1191     if (!GetAnsManagerProxy()) {
1192         ANS_LOGE("GetAnsManagerProxy fail.");
1193         return ERR_ANS_SERVICE_NOT_CONNECTED;
1194     }
1195 
1196     auto dndDatePtr = new (std::nothrow) NotificationDoNotDisturbDate(doNotDisturbDate);
1197     if (dndDatePtr == nullptr) {
1198         ANS_LOGE("create DoNotDisturbDate failed.");
1199         return ERR_ANS_NO_MEMORY;
1200     }
1201 
1202     sptr<NotificationDoNotDisturbDate> dndDate(dndDatePtr);
1203     return ansManagerProxy_->SetDoNotDisturbDate(dndDate);
1204 }
1205 
GetDoNotDisturbDate(const int32_t & userId,NotificationDoNotDisturbDate & doNotDisturbDate)1206 ErrCode AnsNotification::GetDoNotDisturbDate(const int32_t &userId, NotificationDoNotDisturbDate &doNotDisturbDate)
1207 {
1208     if (userId <= SUBSCRIBE_USER_INIT) {
1209         ANS_LOGE("Input userId is invalid.");
1210         return ERR_ANS_INVALID_PARAM;
1211     }
1212 
1213     if (!GetAnsManagerProxy()) {
1214         ANS_LOGE("GetAnsManagerProxy fail.");
1215         return ERR_ANS_SERVICE_NOT_CONNECTED;
1216     }
1217 
1218     sptr<NotificationDoNotDisturbDate> dndDate = nullptr;
1219     auto ret = ansManagerProxy_->GetDoNotDisturbDate(dndDate);
1220     if (ret != ERR_OK) {
1221         ANS_LOGE("Get DoNotDisturbDate failed.");
1222         return ret;
1223     }
1224 
1225     if (!dndDate) {
1226         ANS_LOGE("Invalid DoNotDisturbDate.");
1227         return ERR_ANS_NO_MEMORY;
1228     }
1229 
1230     doNotDisturbDate = *dndDate;
1231     return ret;
1232 }
1233 
SetEnabledForBundleSlot(const NotificationBundleOption & bundleOption,const NotificationConstant::SlotType & slotType,bool enabled)1234 ErrCode AnsNotification::SetEnabledForBundleSlot(
1235     const NotificationBundleOption &bundleOption, const NotificationConstant::SlotType &slotType, bool enabled)
1236 {
1237     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
1238     if (bundleOption.GetBundleName().empty()) {
1239         ANS_LOGE("Invalid bundle name.");
1240         return ERR_ANS_INVALID_PARAM;
1241     }
1242 
1243     if (!GetAnsManagerProxy()) {
1244         ANS_LOGE("SetEnabledForBundleSlot fail.");
1245         return ERR_ANS_SERVICE_NOT_CONNECTED;
1246     }
1247 
1248     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1249     return ansManagerProxy_->SetEnabledForBundleSlot(bo, slotType, enabled);
1250 }
1251 
GetEnabledForBundleSlot(const NotificationBundleOption & bundleOption,const NotificationConstant::SlotType & slotType,bool & enabled)1252 ErrCode AnsNotification::GetEnabledForBundleSlot(
1253     const NotificationBundleOption &bundleOption, const NotificationConstant::SlotType &slotType, bool &enabled)
1254 {
1255     if (bundleOption.GetBundleName().empty()) {
1256         ANS_LOGE("Invalid bundle name.");
1257         return ERR_ANS_INVALID_PARAM;
1258     }
1259 
1260     if (!GetAnsManagerProxy()) {
1261         ANS_LOGE("GetEnabledForBundleSlot fail.");
1262         return ERR_ANS_SERVICE_NOT_CONNECTED;
1263     }
1264 
1265     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1266     return ansManagerProxy_->GetEnabledForBundleSlot(bo, slotType, enabled);
1267 }
1268 
ShellDump(const std::string & cmd,const std::string & bundle,int32_t userId,std::vector<std::string> & dumpInfo)1269 ErrCode AnsNotification::ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId,
1270     std::vector<std::string> &dumpInfo)
1271 {
1272     if (!GetAnsManagerProxy()) {
1273         ANS_LOGE("GetAnsManagerProxy fail.");
1274         return ERR_ANS_SERVICE_NOT_CONNECTED;
1275     }
1276 
1277     return ansManagerProxy_->ShellDump(cmd, bundle, userId, dumpInfo);
1278 }
1279 
SetSyncNotificationEnabledWithoutApp(const int32_t userId,const bool enabled)1280 ErrCode AnsNotification::SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled)
1281 {
1282     if (userId <= SUBSCRIBE_USER_INIT) {
1283         ANS_LOGE("Input userId is invalid.");
1284         return ERR_ANS_INVALID_PARAM;
1285     }
1286 
1287     if (!GetAnsManagerProxy()) {
1288         ANS_LOGE("GetAnsManagerProxy fail.");
1289         return ERR_ANS_SERVICE_NOT_CONNECTED;
1290     }
1291 
1292     return ansManagerProxy_->SetSyncNotificationEnabledWithoutApp(userId, enabled);
1293 }
1294 
GetSyncNotificationEnabledWithoutApp(const int32_t userId,bool & enabled)1295 ErrCode AnsNotification::GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled)
1296 {
1297     if (userId <= SUBSCRIBE_USER_INIT) {
1298         ANS_LOGE("Input userId is invalid.");
1299         return ERR_ANS_INVALID_PARAM;
1300     }
1301 
1302     if (!GetAnsManagerProxy()) {
1303         ANS_LOGE("GetAnsManagerProxy fail.");
1304         return ERR_ANS_SERVICE_NOT_CONNECTED;
1305     }
1306 
1307     return ansManagerProxy_->GetSyncNotificationEnabledWithoutApp(userId, enabled);
1308 }
1309 
SetBadgeNumber(int32_t badgeNumber)1310 ErrCode AnsNotification::SetBadgeNumber(int32_t badgeNumber)
1311 {
1312     if (!GetAnsManagerProxy()) {
1313         ANS_LOGE("SetBadgeNumber fail.");
1314         return ERR_ANS_SERVICE_NOT_CONNECTED;
1315     }
1316 
1317     return ansManagerProxy_->SetBadgeNumber(badgeNumber);
1318 }
1319 
RegisterPushCallback(const sptr<IRemoteObject> & pushCallback)1320 ErrCode AnsNotification::RegisterPushCallback(const sptr<IRemoteObject>& pushCallback)
1321 {
1322     if (!GetAnsManagerProxy()) {
1323         ANS_LOGE("RegisterPushCallback fail.");
1324         return ERR_ANS_SERVICE_NOT_CONNECTED;
1325     }
1326 
1327     return ansManagerProxy_->RegisterPushCallback(pushCallback);
1328 }
1329 
UnregisterPushCallback()1330 ErrCode AnsNotification::UnregisterPushCallback()
1331 {
1332     if (!GetAnsManagerProxy()) {
1333         ANS_LOGE("UnregisterPushCallback fail.");
1334         return ERR_ANS_SERVICE_NOT_CONNECTED;
1335     }
1336 
1337     return ansManagerProxy_->UnregisterPushCallback();
1338 }
1339 }  // namespace Notification
1340 }  // namespace OHOS