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_notification.h"
17 #include "ans_const_define.h"
18 #include "ans_inner_errors.h"
19 #include "ans_log_wrapper.h"
20 #include "ans_manager_death_recipient.h"
21 #include "ans_manager_proxy.h"
22 #include "hitrace_meter_adapter.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "notification_button_option.h"
26 #include "notification_local_live_view_subscriber.h"
27 #include "reminder_request_alarm.h"
28 #include "reminder_request_calendar.h"
29 #include "reminder_request_timer.h"
30 #include "system_ability_definition.h"
31 #include "unique_fd.h"
32 
33 #include <memory>
34 #include <thread>
35 
36 namespace OHOS {
37 namespace Notification {
38 namespace {
39 const int32_t MAX_RETRY_TIME = 30;
40 const int32_t SLEEP_TIME = 1000;
41 const uint32_t MAX_PUBLISH_DELAY_TIME = 5;
42 const int32_t DEFAULT_INSTANCE_KEY = -1;
43 const std::string DOWNLOAD_TITLE = "title";
44 const std::string DOWNLOAD_FILENAME = "fileName";
45 }
AddNotificationSlot(const NotificationSlot & slot)46 ErrCode AnsNotification::AddNotificationSlot(const NotificationSlot &slot)
47 {
48     std::vector<NotificationSlot> slots;
49     slots.push_back(slot);
50     return AddNotificationSlots(slots);
51 }
52 
AddSlotByType(const NotificationConstant::SlotType & slotType)53 ErrCode AnsNotification::AddSlotByType(const NotificationConstant::SlotType &slotType)
54 {
55     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
56     if (!proxy) {
57         ANS_LOGE("GetAnsManagerProxy fail.");
58         return ERR_ANS_SERVICE_NOT_CONNECTED;
59     }
60     return proxy->AddSlotByType(slotType);
61 }
62 
AddNotificationSlots(const std::vector<NotificationSlot> & slots)63 ErrCode AnsNotification::AddNotificationSlots(const std::vector<NotificationSlot> &slots)
64 {
65     if (slots.size() == 0) {
66         ANS_LOGE("Failed to add notification slots because input slots size is 0.");
67         return ERR_ANS_INVALID_PARAM;
68     }
69 
70     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
71     if (!proxy) {
72         ANS_LOGE("GetAnsManagerProxy fail.");
73         return ERR_ANS_SERVICE_NOT_CONNECTED;
74     }
75 
76     std::vector<sptr<NotificationSlot>> slotsSptr;
77     for (auto it = slots.begin(); it != slots.end(); ++it) {
78         sptr<NotificationSlot> slot = new (std::nothrow) NotificationSlot(*it);
79         if (slot == nullptr) {
80             ANS_LOGE("Failed to create NotificationSlot ptr.");
81             return ERR_ANS_NO_MEMORY;
82         }
83         slotsSptr.emplace_back(slot);
84     }
85 
86     return proxy->AddSlots(slotsSptr);
87 }
88 
RemoveNotificationSlot(const NotificationConstant::SlotType & slotType)89 ErrCode AnsNotification::RemoveNotificationSlot(const NotificationConstant::SlotType &slotType)
90 {
91     ANS_LOGI("enter RemoveNotificationSlot,slotType:%{public}d", slotType);
92     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
93     if (!proxy) {
94         ANS_LOGE("GetAnsManagerProxy fail.");
95         return ERR_ANS_SERVICE_NOT_CONNECTED;
96     }
97     return proxy->RemoveSlotByType(slotType);
98 }
99 
RemoveAllSlots()100 ErrCode AnsNotification::RemoveAllSlots()
101 {
102     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
103     if (!proxy) {
104         ANS_LOGE("GetAnsManagerProxy fail.");
105         return ERR_ANS_SERVICE_NOT_CONNECTED;
106     }
107     return proxy->RemoveAllSlots();
108 }
109 
GetNotificationSlot(const NotificationConstant::SlotType & slotType,sptr<NotificationSlot> & slot)110 ErrCode AnsNotification::GetNotificationSlot(
111     const NotificationConstant::SlotType &slotType, sptr<NotificationSlot> &slot)
112 {
113     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
114     if (!proxy) {
115         ANS_LOGE("GetAnsManagerProxy fail.");
116         return ERR_ANS_SERVICE_NOT_CONNECTED;
117     }
118     return proxy->GetSlotByType(slotType, slot);
119 }
120 
GetNotificationSlots(std::vector<sptr<NotificationSlot>> & slots)121 ErrCode AnsNotification::GetNotificationSlots(std::vector<sptr<NotificationSlot>> &slots)
122 {
123     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
124     if (!proxy) {
125         ANS_LOGE("GetAnsManagerProxy fail.");
126         return ERR_ANS_SERVICE_NOT_CONNECTED;
127     }
128     return proxy->GetSlots(slots);
129 }
130 
GetNotificationSlotNumAsBundle(const NotificationBundleOption & bundleOption,uint64_t & num)131 ErrCode AnsNotification::GetNotificationSlotNumAsBundle(const NotificationBundleOption &bundleOption, uint64_t &num)
132 {
133     if (bundleOption.GetBundleName().empty()) {
134         ANS_LOGE("Invalid bundle name.");
135         return ERR_ANS_INVALID_PARAM;
136     }
137 
138     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
139     if (!proxy) {
140         ANS_LOGE("Fail to GetAnsManagerProxy.");
141         return ERR_ANS_SERVICE_NOT_CONNECTED;
142     }
143 
144     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
145     return proxy->GetSlotNumAsBundle(bo, num);
146 }
147 
GetNotificationSlotFlagsAsBundle(const NotificationBundleOption & bundleOption,uint32_t & slotFlags)148 ErrCode AnsNotification::GetNotificationSlotFlagsAsBundle(const NotificationBundleOption &bundleOption,
149     uint32_t &slotFlags)
150 {
151     if (bundleOption.GetBundleName().empty()) {
152         ANS_LOGE("Invalid bundle name.");
153         return ERR_ANS_INVALID_PARAM;
154     }
155 
156     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
157     if (!proxy) {
158         ANS_LOGE("Fail to GetAnsManagerProxy.");
159         return ERR_ANS_SERVICE_NOT_CONNECTED;
160     }
161 
162     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
163     return proxy->GetSlotFlagsAsBundle(bo, slotFlags);
164 }
165 
SetNotificationSlotFlagsAsBundle(const NotificationBundleOption & bundleOption,uint32_t slotFlags)166 ErrCode AnsNotification::SetNotificationSlotFlagsAsBundle(const NotificationBundleOption &bundleOption,
167     uint32_t slotFlags)
168 {
169     if (bundleOption.GetBundleName().empty()) {
170         ANS_LOGE("Invalid bundle name.");
171         return ERR_ANS_INVALID_PARAM;
172     }
173 
174     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
175     if (!proxy) {
176         ANS_LOGE("Fail to GetAnsManagerProxy.");
177         return ERR_ANS_SERVICE_NOT_CONNECTED;
178     }
179 
180     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
181     return proxy->SetSlotFlagsAsBundle(bo, slotFlags);
182 }
183 
PublishNotification(const NotificationRequest & request)184 ErrCode AnsNotification::PublishNotification(const NotificationRequest &request)
185 {
186     ANS_LOGD("enter");
187     return PublishNotification(std::string(), request);
188 }
189 
PublishNotification(const std::string & label,const NotificationRequest & request)190 ErrCode AnsNotification::PublishNotification(const std::string &label, const NotificationRequest &request)
191 {
192     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
193     ANS_LOGD("enter");
194 
195     if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
196         ANS_LOGE("Refuse to publish the notification without valid content");
197         return ERR_ANS_INVALID_PARAM;
198     }
199 
200     if (!IsValidTemplate(request) || !IsValidDelayTime(request)) {
201         return ERR_ANS_INVALID_PARAM;
202     }
203 
204     if (!CanPublishMediaContent(request)) {
205         ANS_LOGE("Refuse to publish the notification because the series numbers actions not match those assigned to "
206                  "added action buttons.");
207         return ERR_ANS_INVALID_PARAM;
208     }
209 
210     if (!CanPublishLiveViewContent(request)) {
211         ANS_LOGE("Refuse to publish the notification without valid live view content.");
212         return ERR_ANS_INVALID_PARAM;
213     }
214 
215     ErrCode checkErr = CheckImageSize(request);
216     if (checkErr != ERR_OK) {
217         ANS_LOGE("The size of one picture exceeds the limit");
218         return checkErr;
219     }
220 
221     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
222     if (!proxy) {
223         ANS_LOGE("Failed to GetAnsManagerProxy.");
224         return ERR_ANS_SERVICE_NOT_CONNECTED;
225     }
226 
227     sptr<NotificationRequest> reqPtr = new (std::nothrow) NotificationRequest(request);
228     if (reqPtr == nullptr) {
229         ANS_LOGE("Create notificationRequest ptr fail.");
230         return ERR_ANS_NO_MEMORY;
231     }
232 
233     if (IsNonDistributedNotificationType(reqPtr->GetNotificationType())) {
234         reqPtr->SetDistributed(false);
235     }
236     int32_t instanceKey = DEFAULT_INSTANCE_KEY;
237     reqPtr->SetCreatorInstanceKey(instanceKey);
238 
239     return proxy->Publish(label, reqPtr);
240 }
241 
PublishNotificationForIndirectProxy(const NotificationRequest & request)242 ErrCode AnsNotification::PublishNotificationForIndirectProxy(const NotificationRequest &request)
243 {
244     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
245     ANS_LOGD("enter");
246 
247     if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
248         ANS_LOGE("Refuse to publish the notification without valid content");
249         return ERR_ANS_INVALID_PARAM;
250     }
251 
252     if (!IsValidTemplate(request) || !IsValidDelayTime(request)) {
253         return ERR_ANS_INVALID_PARAM;
254     }
255 
256     if (!CanPublishMediaContent(request)) {
257         ANS_LOGE("Refuse to publish the notification because the series numbers actions not match those assigned to "
258                  "added action buttons.");
259         return ERR_ANS_INVALID_PARAM;
260     }
261 
262     if (!CanPublishLiveViewContent(request)) {
263         ANS_LOGE("Refuse to publish the notification without valid live view content.");
264         return ERR_ANS_INVALID_PARAM;
265     }
266 
267     ErrCode checkErr = CheckImageSize(request);
268     if (checkErr != ERR_OK) {
269         ANS_LOGE("The size of one picture exceeds the limit");
270         return checkErr;
271     }
272 
273     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
274     if (!proxy) {
275         ANS_LOGE("Failed to GetAnsManagerProxy.");
276         return ERR_ANS_SERVICE_NOT_CONNECTED;
277     }
278 
279     sptr<NotificationRequest> reqPtr = new (std::nothrow) NotificationRequest(request);
280     if (reqPtr == nullptr) {
281         ANS_LOGE("Create notificationRequest ptr fail.");
282         return ERR_ANS_NO_MEMORY;
283     }
284 
285     if (IsNonDistributedNotificationType(reqPtr->GetNotificationType())) {
286         reqPtr->SetDistributed(false);
287     }
288     int32_t instanceKey = DEFAULT_INSTANCE_KEY;
289     reqPtr->SetCreatorInstanceKey(instanceKey);
290 
291     return proxy->PublishNotificationForIndirectProxy(reqPtr);
292 }
293 
CancelNotification(int32_t notificationId)294 ErrCode AnsNotification::CancelNotification(int32_t notificationId)
295 {
296     return CancelNotification("", notificationId);
297 }
298 
CancelNotification(const std::string & label,int32_t notificationId)299 ErrCode AnsNotification::CancelNotification(const std::string &label, int32_t notificationId)
300 {
301     ANS_LOGI("enter CancelNotification,notificationId:%{public}d", notificationId);
302     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
303     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
304     if (!proxy) {
305         ANS_LOGE("GetAnsManagerProxy fail.");
306         return ERR_ANS_SERVICE_NOT_CONNECTED;
307     }
308     int32_t instanceKey = DEFAULT_INSTANCE_KEY;
309     return proxy->Cancel(notificationId, label, instanceKey);
310 }
311 
CancelAllNotifications()312 ErrCode AnsNotification::CancelAllNotifications()
313 {
314     ANS_LOGI("CancelAllNotifications called.");
315 
316     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
317     if (!proxy) {
318         ANS_LOGE("GetAnsManagerProxy fail.");
319         return ERR_ANS_SERVICE_NOT_CONNECTED;
320     }
321     int32_t instanceKey = DEFAULT_INSTANCE_KEY;
322     return proxy->CancelAll(instanceKey);
323 }
324 
CancelAsBundle(int32_t notificationId,const std::string & representativeBundle,int32_t userId)325 ErrCode AnsNotification::CancelAsBundle(
326     int32_t notificationId, const std::string &representativeBundle, int32_t userId)
327 {
328     ANS_LOGI("enter CancelAsBundle,notificationId:%{public}d", notificationId);
329     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
330     if (!proxy) {
331         ANS_LOGE("GetAnsManagerProxy fail.");
332         return ERR_ANS_SERVICE_NOT_CONNECTED;
333     }
334     return proxy->CancelAsBundle(notificationId, representativeBundle, userId);
335 }
336 
CancelAsBundle(const NotificationBundleOption & bundleOption,int32_t notificationId)337 ErrCode AnsNotification::CancelAsBundle(
338     const NotificationBundleOption &bundleOption, int32_t notificationId)
339 {
340     ANS_LOGI("enter CancelAsBundle,notificationId:%{public}d", notificationId);
341     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
342     if (!proxy) {
343         ANS_LOGE("GetAnsManagerProxy fail.");
344         return ERR_ANS_SERVICE_NOT_CONNECTED;
345     }
346     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
347     return proxy->CancelAsBundle(bo, notificationId);
348 }
349 
GetActiveNotificationNums(uint64_t & num)350 ErrCode AnsNotification::GetActiveNotificationNums(uint64_t &num)
351 {
352     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
353     if (!proxy) {
354         ANS_LOGE("GetAnsManagerProxy fail.");
355         return ERR_ANS_SERVICE_NOT_CONNECTED;
356     }
357     return proxy->GetActiveNotificationNums(num);
358 }
359 
GetActiveNotifications(std::vector<sptr<NotificationRequest>> & request)360 ErrCode AnsNotification::GetActiveNotifications(std::vector<sptr<NotificationRequest>> &request)
361 {
362     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
363     if (!proxy) {
364         ANS_LOGE("GetAnsManagerProxy fail.");
365         return ERR_ANS_SERVICE_NOT_CONNECTED;
366     }
367     int32_t instanceKey = DEFAULT_INSTANCE_KEY;
368     return proxy->GetActiveNotifications(request, instanceKey);
369 }
370 
SetNotificationAgent(const std::string & agent)371 ErrCode AnsNotification::SetNotificationAgent(const std::string &agent)
372 {
373     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
374     if (!proxy) {
375         ANS_LOGE("GetAnsManagerProxy fail.");
376         return ERR_ANS_SERVICE_NOT_CONNECTED;
377     }
378     return proxy->SetNotificationAgent(agent);
379 }
380 
GetNotificationAgent(std::string & agent)381 ErrCode AnsNotification::GetNotificationAgent(std::string &agent)
382 {
383     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
384     if (!proxy) {
385         ANS_LOGE("GetAnsManagerProxy fail.");
386         return ERR_ANS_SERVICE_NOT_CONNECTED;
387     }
388     return proxy->GetNotificationAgent(agent);
389 }
390 
CanPublishNotificationAsBundle(const std::string & representativeBundle,bool & canPublish)391 ErrCode AnsNotification::CanPublishNotificationAsBundle(const std::string &representativeBundle, bool &canPublish)
392 {
393     if (representativeBundle.empty()) {
394         ANS_LOGW("Input representativeBundle is empty");
395         return ERR_ANS_INVALID_PARAM;
396     }
397 
398     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
399     if (!proxy) {
400         ANS_LOGE("GetAnsManagerProxy fail.");
401         return ERR_ANS_SERVICE_NOT_CONNECTED;
402     }
403     return proxy->CanPublishAsBundle(representativeBundle, canPublish);
404 }
405 
PublishNotificationAsBundle(const std::string & representativeBundle,const NotificationRequest & request)406 ErrCode AnsNotification::PublishNotificationAsBundle(
407     const std::string &representativeBundle, const NotificationRequest &request)
408 {
409     if (representativeBundle.empty()) {
410         ANS_LOGE("Refuse to publish the notification whit invalid representativeBundle");
411         return ERR_ANS_INVALID_PARAM;
412     }
413 
414     if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
415         ANS_LOGE("Refuse to publish the notification without effective content");
416         return ERR_ANS_INVALID_PARAM;
417     }
418 
419     if (!CanPublishMediaContent(request)) {
420         ANS_LOGE("Refuse to publish the notification because the sequence numbers actions not match those assigned to "
421                  "added action buttons.");
422         return ERR_ANS_INVALID_PARAM;
423     }
424 
425     if (!CanPublishLiveViewContent(request)) {
426         ANS_LOGE("Refuse to publish the notification without valid live view content.");
427         return ERR_ANS_INVALID_PARAM;
428     }
429 
430     ErrCode checkErr = CheckImageSize(request);
431     if (checkErr != ERR_OK) {
432         ANS_LOGE("The size of one picture overtake the limit");
433         return checkErr;
434     }
435 
436     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
437     if (!proxy) {
438         ANS_LOGE("GetAnsManagerProxy fail.");
439         return ERR_ANS_SERVICE_NOT_CONNECTED;
440     }
441 
442     sptr<NotificationRequest> reqPtr = new (std::nothrow) NotificationRequest(request);
443     if (reqPtr == nullptr) {
444         ANS_LOGE("Failed to create NotificationRequest ptr");
445         return ERR_ANS_NO_MEMORY;
446     }
447     if (IsNonDistributedNotificationType(reqPtr->GetNotificationType())) {
448         reqPtr->SetDistributed(false);
449     }
450     return proxy->PublishAsBundle(reqPtr, representativeBundle);
451 }
452 
SetNotificationBadgeNum()453 ErrCode AnsNotification::SetNotificationBadgeNum()
454 {
455     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
456     if (!proxy) {
457         ANS_LOGE("GetAnsManagerProxy fail.");
458         return ERR_ANS_SERVICE_NOT_CONNECTED;
459     }
460     int32_t num = -1;
461     return proxy->SetNotificationBadgeNum(num);
462 }
463 
SetNotificationBadgeNum(int32_t num)464 ErrCode AnsNotification::SetNotificationBadgeNum(int32_t num)
465 {
466     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
467     if (!proxy) {
468         ANS_LOGE("GetAnsManagerProxy fail.");
469         return ERR_ANS_SERVICE_NOT_CONNECTED;
470     }
471     return proxy->SetNotificationBadgeNum(num);
472 }
473 
IsAllowedNotify(bool & allowed)474 ErrCode AnsNotification::IsAllowedNotify(bool &allowed)
475 {
476     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
477     if (!proxy) {
478         ANS_LOGE("GetAnsManagerProxy fail.");
479         return ERR_ANS_SERVICE_NOT_CONNECTED;
480     }
481     return proxy->IsAllowedNotify(allowed);
482 }
483 
IsAllowedNotifySelf(bool & allowed)484 ErrCode AnsNotification::IsAllowedNotifySelf(bool &allowed)
485 {
486     ANS_LOGD("enter");
487     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
488     if (!proxy) {
489         ANS_LOGE("GetAnsManagerProxy fail.");
490         return ERR_ANS_SERVICE_NOT_CONNECTED;
491     }
492     return proxy->IsAllowedNotifySelf(allowed);
493 }
494 
CanPopEnableNotificationDialog(sptr<AnsDialogHostClient> & hostClient,bool & canPop,std::string & bundleName)495 ErrCode AnsNotification::CanPopEnableNotificationDialog(sptr<AnsDialogHostClient> &hostClient,
496     bool &canPop, std::string &bundleName)
497 {
498     ANS_LOGD("enter");
499     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
500     if (!proxy) {
501         ANS_LOGE("GetAnsManagerProxy fail.");
502         return ERR_ANS_SERVICE_NOT_CONNECTED;
503     }
504     return proxy->CanPopEnableNotificationDialog(hostClient, canPop, bundleName);
505 }
506 
RemoveEnableNotificationDialog()507 ErrCode AnsNotification::RemoveEnableNotificationDialog()
508 {
509     ANS_LOGD("enter");
510     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
511     if (!proxy) {
512         ANS_LOGE("GetAnsManagerProxy fail.");
513         return ERR_ANS_SERVICE_NOT_CONNECTED;
514     }
515     return proxy->RemoveEnableNotificationDialog();
516 }
517 
RequestEnableNotification(std::string & deviceId,sptr<AnsDialogHostClient> & hostClient,sptr<IRemoteObject> & callerToken)518 ErrCode AnsNotification::RequestEnableNotification(std::string &deviceId,
519     sptr<AnsDialogHostClient> &hostClient,
520     sptr<IRemoteObject> &callerToken)
521 {
522     ANS_LOGD("enter");
523     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
524     if (!proxy) {
525         ANS_LOGE("GetAnsManagerProxy fail.");
526         return ERR_ANS_SERVICE_NOT_CONNECTED;
527     }
528     return proxy->RequestEnableNotification(deviceId, hostClient, callerToken);
529 }
530 
HasNotificationPolicyAccessPermission(bool & hasPermission)531 ErrCode AnsNotification::HasNotificationPolicyAccessPermission(bool &hasPermission)
532 {
533     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
534     if (!proxy) {
535         ANS_LOGE("GetAnsManagerProxy fail.");
536         return ERR_ANS_SERVICE_NOT_CONNECTED;
537     }
538     return proxy->HasNotificationPolicyAccessPermission(hasPermission);
539 }
540 
GetBundleImportance(NotificationSlot::NotificationLevel & importance)541 ErrCode AnsNotification::GetBundleImportance(NotificationSlot::NotificationLevel &importance)
542 {
543     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
544     if (!proxy) {
545         ANS_LOGE("GetAnsManagerProxy fail.");
546         return ERR_ANS_SERVICE_NOT_CONNECTED;
547     }
548     int32_t importanceTemp;
549     ErrCode ret = proxy->GetBundleImportance(importanceTemp);
550     if ((NotificationSlot::LEVEL_NONE <= importanceTemp) && (importanceTemp <= NotificationSlot::LEVEL_HIGH)) {
551         importance = static_cast<NotificationSlot::NotificationLevel>(importanceTemp);
552     } else {
553         importance = NotificationSlot::LEVEL_UNDEFINED;
554     }
555     return ret;
556 }
557 
SubscribeNotification(const NotificationSubscriber & subscriber)558 ErrCode AnsNotification::SubscribeNotification(const NotificationSubscriber &subscriber)
559 {
560     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
561     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
562     if (!proxy) {
563         ANS_LOGE("GetAnsManagerProxy fail.");
564         return ERR_ANS_SERVICE_NOT_CONNECTED;
565     }
566 
567     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
568     if (subscriberSptr == nullptr) {
569         ANS_LOGE("Failed to subscribe with SubscriberImpl null ptr.");
570         return ERR_ANS_INVALID_PARAM;
571     }
572     return proxy->Subscribe(subscriberSptr, nullptr);
573 }
574 
SubscribeNotificationSelf(const NotificationSubscriber & subscriber)575 ErrCode AnsNotification::SubscribeNotificationSelf(const NotificationSubscriber &subscriber)
576 {
577     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
578     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
579     if (!proxy) {
580         ANS_LOGE("GetAnsManagerProxy fail.");
581         return ERR_ANS_SERVICE_NOT_CONNECTED;
582     }
583 
584     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
585     if (subscriberSptr == nullptr) {
586         ANS_LOGE("Failed to subscribeSelf with SubscriberImpl null ptr.");
587         return ERR_ANS_INVALID_PARAM;
588     }
589     return proxy->SubscribeSelf(subscriberSptr);
590 }
591 
SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber & subscriber,const bool isNative)592 ErrCode AnsNotification::SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber &subscriber,
593     const bool isNative)
594 {
595     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
596     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
597     if (!proxy) {
598         ANS_LOGE("GetAnsManagerProxy fail.");
599         return ERR_ANS_SERVICE_NOT_CONNECTED;
600     }
601 
602     sptr<NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl> subscriberSptr = subscriber.GetImpl();
603     if (subscriberSptr == nullptr) {
604         ANS_LOGE("Failed to subscribe with SubscriberImpl null ptr.");
605         return ERR_ANS_INVALID_PARAM;
606     }
607     return proxy->SubscribeLocalLiveView(subscriberSptr, nullptr, isNative);
608 }
609 
SubscribeNotification(const NotificationSubscriber & subscriber,const NotificationSubscribeInfo & subscribeInfo)610 ErrCode AnsNotification::SubscribeNotification(
611     const NotificationSubscriber &subscriber, const NotificationSubscribeInfo &subscribeInfo)
612 {
613     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
614     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
615     if (!proxy) {
616         ANS_LOGE("Failed to GetAnsManagerProxy.");
617         return ERR_ANS_SERVICE_NOT_CONNECTED;
618     }
619 
620     sptr<NotificationSubscribeInfo> sptrInfo = new (std::nothrow) NotificationSubscribeInfo(subscribeInfo);
621     if (sptrInfo == nullptr) {
622         ANS_LOGE("Failed to create NotificationSubscribeInfo ptr.");
623         return ERR_ANS_NO_MEMORY;
624     }
625 
626     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
627     if (subscriberSptr == nullptr) {
628         ANS_LOGE("Failed to subscribe with SubscriberImpl null ptr.");
629         return ERR_ANS_INVALID_PARAM;
630     }
631     subscriberSptr->subscriber_.SetDeviceType(subscribeInfo.GetDeviceType());
632     return proxy->Subscribe(subscriberSptr, sptrInfo);
633 }
634 
UnSubscribeNotification(NotificationSubscriber & subscriber)635 ErrCode AnsNotification::UnSubscribeNotification(NotificationSubscriber &subscriber)
636 {
637     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
638     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
639     if (!proxy) {
640         ANS_LOGE("GetAnsManagerProxy fail.");
641         return ERR_ANS_SERVICE_NOT_CONNECTED;
642     }
643 
644     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
645     if (subscriberSptr == nullptr) {
646         ANS_LOGE("Failed to unsubscribe with SubscriberImpl null ptr.");
647         return ERR_ANS_INVALID_PARAM;
648     }
649     return proxy->Unsubscribe(subscriberSptr, nullptr);
650 }
651 
UnSubscribeNotification(NotificationSubscriber & subscriber,NotificationSubscribeInfo subscribeInfo)652 ErrCode AnsNotification::UnSubscribeNotification(
653     NotificationSubscriber &subscriber, NotificationSubscribeInfo subscribeInfo)
654 {
655     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
656     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
657     if (!proxy) {
658         ANS_LOGE("GetAnsManagerProxy fail.");
659         return ERR_ANS_SERVICE_NOT_CONNECTED;
660     }
661 
662     sptr<NotificationSubscribeInfo> sptrInfo = new (std::nothrow) NotificationSubscribeInfo(subscribeInfo);
663     if (sptrInfo == nullptr) {
664         ANS_LOGE("Failed to create NotificationSubscribeInfo ptr.");
665         return ERR_ANS_NO_MEMORY;
666     }
667 
668     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
669     if (subscriberSptr == nullptr) {
670         ANS_LOGE("Failed to unsubscribe with SubscriberImpl null ptr.");
671         return ERR_ANS_INVALID_PARAM;
672     }
673     return proxy->Unsubscribe(subscriberSptr, sptrInfo);
674 }
675 
SubscribeNotification(const std::shared_ptr<NotificationSubscriber> & subscriber)676 ErrCode AnsNotification::SubscribeNotification(const std::shared_ptr<NotificationSubscriber> &subscriber)
677 {
678     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
679     return SubscribeNotification(subscriber, nullptr);
680 }
681 
SubscribeNotificationSelf(const std::shared_ptr<NotificationSubscriber> & subscriber)682 ErrCode AnsNotification::SubscribeNotificationSelf(const std::shared_ptr<NotificationSubscriber> &subscriber)
683 {
684     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
685     if (subscriber == nullptr) {
686         ANS_LOGE("Subscriber is nullptr.");
687         return ERR_ANS_INVALID_PARAM;
688     }
689 
690     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
691     if (!proxy) {
692         ANS_LOGE("GetAnsManagerProxy fail.");
693         return ERR_ANS_SERVICE_NOT_CONNECTED;
694     }
695 
696     sptr<SubscriberListener> listener = nullptr;
697     CreateSubscribeListener(subscriber, listener);
698     if (listener == nullptr) {
699         ANS_LOGE("Failed to subscribe due to create subscriber listener failed.");
700         return ERR_ANS_NO_MEMORY;
701     }
702     DelayedSingleton<AnsManagerDeathRecipient>::GetInstance()->SubscribeSAManager();
703     return proxy->SubscribeSelf(listener);
704 }
705 
SubscribeNotification(const std::shared_ptr<NotificationSubscriber> & subscriber,const sptr<NotificationSubscribeInfo> & subscribeInfo)706 ErrCode AnsNotification::SubscribeNotification(const std::shared_ptr<NotificationSubscriber> &subscriber,
707     const sptr<NotificationSubscribeInfo> &subscribeInfo)
708 {
709     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
710     if (subscriber == nullptr) {
711         ANS_LOGE("Subscriber is nullptr.");
712         return ERR_ANS_INVALID_PARAM;
713     }
714 
715     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
716     if (!proxy) {
717         ANS_LOGE("Failed to GetAnsManagerProxy.");
718         return ERR_ANS_SERVICE_NOT_CONNECTED;
719     }
720 
721     sptr<SubscriberListener> listener = nullptr;
722     CreateSubscribeListener(subscriber, listener);
723     if (listener == nullptr) {
724         ANS_LOGE("Failed to subscribe due to create subscriber listener failed.");
725         return ERR_ANS_NO_MEMORY;
726     }
727     if (subscribeInfo != nullptr) {
728         subscriber->SetDeviceType(subscribeInfo->GetDeviceType());
729     }
730     DelayedSingleton<AnsManagerDeathRecipient>::GetInstance()->SubscribeSAManager();
731     return proxy->Subscribe(listener, subscribeInfo);
732 }
733 
UnSubscribeNotification(const std::shared_ptr<NotificationSubscriber> & subscriber)734 ErrCode AnsNotification::UnSubscribeNotification(const std::shared_ptr<NotificationSubscriber> &subscriber)
735 {
736     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
737     return UnSubscribeNotification(subscriber, nullptr);
738 }
739 
UnSubscribeNotification(const std::shared_ptr<NotificationSubscriber> & subscriber,const sptr<NotificationSubscribeInfo> & subscribeInfo)740 ErrCode AnsNotification::UnSubscribeNotification(const std::shared_ptr<NotificationSubscriber> &subscriber,
741     const sptr<NotificationSubscribeInfo> &subscribeInfo)
742 {
743     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
744     if (subscriber == nullptr) {
745         ANS_LOGE("Subscriber is nullptr.");
746         return ERR_ANS_INVALID_PARAM;
747     }
748 
749     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
750     if (!proxy) {
751         ANS_LOGE("GetAnsManagerProxy fail.");
752         return ERR_ANS_SERVICE_NOT_CONNECTED;
753     }
754     std::lock_guard<std::mutex> lock(subscriberMutex_);
755     auto item = subscribers_.find(subscriber);
756     if (item != subscribers_.end()) {
757         sptr<SubscriberListener> listener = item->second;
758         int32_t ret = proxy->Unsubscribe(listener, subscribeInfo);
759         if (ret == ERR_OK) {
760             subscribers_.erase(item);
761         }
762         return ret;
763     }
764     ANS_LOGE("Failed to unsubscribe due to subscriber not found.");
765     return ERR_ANS_INVALID_PARAM;
766 }
767 
TriggerLocalLiveView(const NotificationBundleOption & bundleOption,const int32_t notificationId,const NotificationButtonOption & buttonOption)768 ErrCode AnsNotification::TriggerLocalLiveView(const NotificationBundleOption &bundleOption,
769     const int32_t notificationId, const NotificationButtonOption &buttonOption)
770 {
771     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
772 
773     if (buttonOption.GetButtonName().empty()) {
774         ANS_LOGE("Invalid button name.");
775         return ERR_ANS_INVALID_PARAM;
776     }
777 
778     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
779     if (!proxy) {
780         ANS_LOGE("Fail to GetAnsManagerProxy.");
781         return ERR_ANS_SERVICE_NOT_CONNECTED;
782     }
783 
784     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
785     sptr<NotificationButtonOption> button(new (std::nothrow) NotificationButtonOption(buttonOption));
786     return proxy->TriggerLocalLiveView(bo, notificationId, button);
787 }
788 
RemoveNotification(const std::string & key,int32_t removeReason)789 ErrCode AnsNotification::RemoveNotification(const std::string &key, int32_t removeReason)
790 {
791     ANS_LOGI("enter RemoveNotification,key:%{public}s,removeReason:%{public}d",
792         key.c_str(), removeReason);
793     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
794     if (key.empty()) {
795         ANS_LOGW("Input key is empty.");
796         return ERR_ANS_INVALID_PARAM;
797     }
798 
799     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
800     if (!proxy) {
801         ANS_LOGE("GetAnsManagerProxy fail.");
802         return ERR_ANS_SERVICE_NOT_CONNECTED;
803     }
804     return proxy->Delete(key, removeReason);
805 }
806 
RemoveNotification(const NotificationBundleOption & bundleOption,const int32_t notificationId,const std::string & label,int32_t removeReason)807 ErrCode AnsNotification::RemoveNotification(const NotificationBundleOption &bundleOption,
808     const int32_t notificationId, const std::string &label, int32_t removeReason)
809 {
810     ANS_LOGI("enter RemoveNotification,bundle:%{public}s,Id:%{public}d,reason:%{public}d",
811         bundleOption.GetBundleName().c_str(), notificationId, removeReason);
812     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
813     if (bundleOption.GetBundleName().empty()) {
814         ANS_LOGE("Invalid bundle name.");
815         return ERR_ANS_INVALID_PARAM;
816     }
817 
818     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
819     if (!proxy) {
820         ANS_LOGE("Fail to GetAnsManagerProxy.");
821         return ERR_ANS_SERVICE_NOT_CONNECTED;
822     }
823 
824     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
825     return proxy->RemoveNotification(bo, notificationId, label, removeReason);
826 }
827 
RemoveAllNotifications(const NotificationBundleOption & bundleOption)828 ErrCode AnsNotification::RemoveAllNotifications(const NotificationBundleOption &bundleOption)
829 {
830     ANS_LOGI("enter RemoveAllNotifications,bundleName:%{public}s", bundleOption.GetBundleName().c_str());
831     if (bundleOption.GetBundleName().empty()) {
832         ANS_LOGE("Invalid bundle name.");
833         return ERR_ANS_INVALID_PARAM;
834     }
835 
836     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
837     if (!proxy) {
838         ANS_LOGE("GetAnsManagerProxy defeat.");
839         return ERR_ANS_SERVICE_NOT_CONNECTED;
840     }
841 
842     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
843     return proxy->RemoveAllNotifications(bo);
844 }
845 
RemoveNotifications(const std::vector<std::string> hashcodes,int32_t removeReason)846 ErrCode AnsNotification::RemoveNotifications(const std::vector<std::string> hashcodes, int32_t removeReason)
847 {
848     ANS_LOGI("enter RemoveNotifications,removeReason:%{public}d", removeReason);
849     if (hashcodes.empty()) {
850         ANS_LOGE("Hashcodes is empty");
851         return ERR_ANS_INVALID_PARAM;
852     }
853 
854     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
855     if (!proxy) {
856         ANS_LOGE("GetAnsManagerProxy fail.");
857         return ERR_ANS_SERVICE_NOT_CONNECTED;
858     }
859 
860     return proxy->RemoveNotifications(hashcodes, removeReason);
861 }
862 
RemoveNotificationsByBundle(const NotificationBundleOption & bundleOption)863 ErrCode AnsNotification::RemoveNotificationsByBundle(const NotificationBundleOption &bundleOption)
864 {
865     ANS_LOGI("enter RemoveNotificationsByBundle,bundleName:%{public}s", bundleOption.GetBundleName().c_str());
866     if (bundleOption.GetBundleName().empty()) {
867         ANS_LOGE("Invalid bundle name.");
868         return ERR_ANS_INVALID_PARAM;
869     }
870 
871     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
872     if (!proxy) {
873         ANS_LOGE("Defeated to GetAnsManagerProxy.");
874         return ERR_ANS_SERVICE_NOT_CONNECTED;
875     }
876 
877     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
878     return proxy->DeleteByBundle(bo);
879 }
880 
RemoveNotifications()881 ErrCode AnsNotification::RemoveNotifications()
882 {
883     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
884     if (!proxy) {
885         ANS_LOGE("GetAnsManagerProxy fail.");
886         return ERR_ANS_SERVICE_NOT_CONNECTED;
887     }
888     return proxy->DeleteAll();
889 }
890 
GetNotificationSlotsForBundle(const NotificationBundleOption & bundleOption,std::vector<sptr<NotificationSlot>> & slots)891 ErrCode AnsNotification::GetNotificationSlotsForBundle(
892     const NotificationBundleOption &bundleOption, std::vector<sptr<NotificationSlot>> &slots)
893 {
894     if (bundleOption.GetBundleName().empty()) {
895         ANS_LOGE("Input bundleName is empty.");
896         return ERR_ANS_INVALID_PARAM;
897     }
898 
899     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
900     if (!proxy) {
901         ANS_LOGE("GetAnsManagerProxy fail.");
902         return ERR_ANS_SERVICE_NOT_CONNECTED;
903     }
904 
905     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
906     return proxy->GetSlotsByBundle(bo, slots);
907 }
908 
GetNotificationSlotForBundle(const NotificationBundleOption & bundleOption,const NotificationConstant::SlotType & slotType,sptr<NotificationSlot> & slot)909 ErrCode AnsNotification::GetNotificationSlotForBundle(
910     const NotificationBundleOption &bundleOption, const NotificationConstant::SlotType &slotType,
911     sptr<NotificationSlot> &slot)
912 {
913     if (bundleOption.GetBundleName().empty()) {
914         ANS_LOGE("Input bundleName is empty.");
915         return ERR_ANS_INVALID_PARAM;
916     }
917 
918     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
919     if (!proxy) {
920         ANS_LOGE("GetAnsManagerProxy fail.");
921         return ERR_ANS_SERVICE_NOT_CONNECTED;
922     }
923 
924     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
925     return proxy->GetSlotByBundle(bo, slotType, slot);
926 }
927 
UpdateNotificationSlots(const NotificationBundleOption & bundleOption,const std::vector<sptr<NotificationSlot>> & slots)928 ErrCode AnsNotification::UpdateNotificationSlots(
929     const NotificationBundleOption &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
930 {
931     if (bundleOption.GetBundleName().empty()) {
932         ANS_LOGE("Invalid bundle name.");
933         return ERR_ANS_INVALID_PARAM;
934     }
935 
936     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
937     if (!proxy) {
938         ANS_LOGE("GetAnsManagerProxy flop.");
939         return ERR_ANS_SERVICE_NOT_CONNECTED;
940     }
941 
942     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
943     return proxy->UpdateSlots(bo, slots);
944 }
945 
GetAllActiveNotifications(std::vector<sptr<Notification>> & notification)946 ErrCode AnsNotification::GetAllActiveNotifications(std::vector<sptr<Notification>> &notification)
947 {
948     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
949     if (!proxy) {
950         ANS_LOGE("GetAnsManagerProxy fail.");
951         return ERR_ANS_SERVICE_NOT_CONNECTED;
952     }
953     return proxy->GetAllActiveNotifications(notification);
954 }
955 
GetAllActiveNotifications(const std::vector<std::string> key,std::vector<sptr<Notification>> & notification)956 ErrCode AnsNotification::GetAllActiveNotifications(
957     const std::vector<std::string> key, std::vector<sptr<Notification>> &notification)
958 {
959     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
960     if (!proxy) {
961         ANS_LOGE("GetAnsManagerProxy fail.");
962         return ERR_ANS_SERVICE_NOT_CONNECTED;
963     }
964     return proxy->GetSpecialActiveNotifications(key, notification);
965 }
966 
GetActiveNotificationByFilter(const LiveViewFilter & filter,sptr<NotificationRequest> & request)967 ErrCode AnsNotification::GetActiveNotificationByFilter(const LiveViewFilter &filter,
968     sptr<NotificationRequest> &request)
969 {
970     if (filter.bundle.GetBundleName().empty()) {
971         ANS_LOGE("Invalid bundle name.");
972         return ERR_ANS_INVALID_PARAM;
973     }
974 
975     ANS_LOGD("Bundle name %{public}s, uid %{public}d, notification id %{public}d, label %{public}s.",
976         filter.bundle.GetBundleName().c_str(), filter.bundle.GetUid(), filter.notificationKey.id,
977         filter.notificationKey.label.c_str());
978 
979     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
980     if (!proxy) {
981         ANS_LOGE("GetAnsManagerProxy fail.");
982         return ERR_ANS_SERVICE_NOT_CONNECTED;
983     }
984 
985     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(filter.bundle));
986     return proxy->GetActiveNotificationByFilter(bo, filter.notificationKey.id, filter.notificationKey.label,
987         filter.extraInfoKeys, request);
988 }
989 
IsAllowedNotify(const NotificationBundleOption & bundleOption,bool & allowed)990 ErrCode AnsNotification::IsAllowedNotify(const NotificationBundleOption &bundleOption, bool &allowed)
991 {
992     if (bundleOption.GetBundleName().empty()) {
993         ANS_LOGE("Input bundle is empty.");
994         return ERR_ANS_INVALID_PARAM;
995     }
996 
997     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
998     if (!proxy) {
999         ANS_LOGE("GetAnsManagerProxy fail.");
1000         return ERR_ANS_SERVICE_NOT_CONNECTED;
1001     }
1002 
1003     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1004     return proxy->IsSpecialBundleAllowedNotify(bo, allowed);
1005 }
1006 
SetNotificationsEnabledForAllBundles(const std::string & deviceId,bool enabled)1007 ErrCode AnsNotification::SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled)
1008 {
1009     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1010     if (!proxy) {
1011         ANS_LOGE("GetAnsManagerProxy fail.");
1012         return ERR_ANS_SERVICE_NOT_CONNECTED;
1013     }
1014     return proxy->SetNotificationsEnabledForAllBundles(deviceId, enabled);
1015 }
1016 
SetNotificationsEnabledForDefaultBundle(const std::string & deviceId,bool enabled)1017 ErrCode AnsNotification::SetNotificationsEnabledForDefaultBundle(const std::string &deviceId, bool enabled)
1018 {
1019     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1020     if (!proxy) {
1021         ANS_LOGE("GetAnsManagerProxy fail.");
1022         return ERR_ANS_SERVICE_NOT_CONNECTED;
1023     }
1024     return proxy->SetNotificationsEnabledForBundle(deviceId, enabled);
1025 }
1026 
SetNotificationsEnabledForSpecifiedBundle(const NotificationBundleOption & bundleOption,const std::string & deviceId,bool enabled)1027 ErrCode AnsNotification::SetNotificationsEnabledForSpecifiedBundle(
1028     const NotificationBundleOption &bundleOption, const std::string &deviceId, bool enabled)
1029 {
1030     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
1031     if (bundleOption.GetBundleName().empty()) {
1032         ANS_LOGE("Invalid bundle name.");
1033         return ERR_ANS_INVALID_PARAM;
1034     }
1035 
1036     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1037     if (!proxy) {
1038         ANS_LOGE("GetAnsManagerProxy fail.");
1039         return ERR_ANS_SERVICE_NOT_CONNECTED;
1040     }
1041 
1042     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1043     return proxy->SetNotificationsEnabledForSpecialBundle(deviceId, bo, enabled);
1044 }
1045 
SetShowBadgeEnabledForBundle(const NotificationBundleOption & bundleOption,bool enabled)1046 ErrCode AnsNotification::SetShowBadgeEnabledForBundle(const NotificationBundleOption &bundleOption, bool enabled)
1047 {
1048     if (bundleOption.GetBundleName().empty()) {
1049         ANS_LOGE("Invalidated bundle name.");
1050         return ERR_ANS_INVALID_PARAM;
1051     }
1052 
1053     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1054     if (!proxy) {
1055         ANS_LOGE("GetAnsManagerProxy fail.");
1056         return ERR_ANS_SERVICE_NOT_CONNECTED;
1057     }
1058 
1059     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1060     return proxy->SetShowBadgeEnabledForBundle(bo, enabled);
1061 }
1062 
GetShowBadgeEnabledForBundle(const NotificationBundleOption & bundleOption,bool & enabled)1063 ErrCode AnsNotification::GetShowBadgeEnabledForBundle(const NotificationBundleOption &bundleOption, bool &enabled)
1064 {
1065     if (bundleOption.GetBundleName().empty()) {
1066         ANS_LOGE("Invalid bundle name.");
1067         return ERR_ANS_INVALID_PARAM;
1068     }
1069 
1070     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1071     if (!proxy) {
1072         ANS_LOGE("GetAnsManagerProxy fail.");
1073         return ERR_ANS_SERVICE_NOT_CONNECTED;
1074     }
1075 
1076     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1077     return proxy->GetShowBadgeEnabledForBundle(bo, enabled);
1078 }
1079 
GetShowBadgeEnabled(bool & enabled)1080 ErrCode AnsNotification::GetShowBadgeEnabled(bool &enabled)
1081 {
1082     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1083     if (!proxy) {
1084         ANS_LOGE("GetAnsManagerProxy fail.");
1085         return ERR_ANS_SERVICE_NOT_CONNECTED;
1086     }
1087 
1088     return proxy->GetShowBadgeEnabled(enabled);
1089 }
1090 
CancelGroup(const std::string & groupName)1091 ErrCode AnsNotification::CancelGroup(const std::string &groupName)
1092 {
1093     ANS_LOGI("enter CancelGroup,groupName:%{public}s", groupName.c_str());
1094     if (groupName.empty()) {
1095         ANS_LOGE("Invalid group name.");
1096         return ERR_ANS_INVALID_PARAM;
1097     }
1098 
1099     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1100     if (!proxy) {
1101         ANS_LOGE("GetAnsManagerProxy fail.");
1102         return ERR_ANS_SERVICE_NOT_CONNECTED;
1103     }
1104     int32_t instanceKey = DEFAULT_INSTANCE_KEY;
1105     return proxy->CancelGroup(groupName, instanceKey);
1106 }
1107 
RemoveGroupByBundle(const NotificationBundleOption & bundleOption,const std::string & groupName)1108 ErrCode AnsNotification::RemoveGroupByBundle(
1109     const NotificationBundleOption &bundleOption, const std::string &groupName)
1110 {
1111     ANS_LOGI("enter RemoveGroupByBundle,bundleName:%{public}s", bundleOption.GetBundleName().c_str());
1112     if (bundleOption.GetBundleName().empty() || groupName.empty()) {
1113         ANS_LOGE("Invalid parameter.");
1114         return ERR_ANS_INVALID_PARAM;
1115     }
1116 
1117     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1118     if (!proxy) {
1119         ANS_LOGE("GetAnsManagerProxy fail.");
1120         return ERR_ANS_SERVICE_NOT_CONNECTED;
1121     }
1122 
1123     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1124     return proxy->RemoveGroupByBundle(bo, groupName);
1125 }
1126 
SetDoNotDisturbDate(const NotificationDoNotDisturbDate & doNotDisturbDate)1127 ErrCode AnsNotification::SetDoNotDisturbDate(const NotificationDoNotDisturbDate &doNotDisturbDate)
1128 {
1129     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1130     if (!proxy) {
1131         ANS_LOGE("GetAnsManagerProxy fail.");
1132         return ERR_ANS_SERVICE_NOT_CONNECTED;
1133     }
1134 
1135     auto dndDatePtr = new (std::nothrow) NotificationDoNotDisturbDate(doNotDisturbDate);
1136     if (dndDatePtr == nullptr) {
1137         ANS_LOGE("Create notificationDoNotDisturbDate failed.");
1138         return ERR_ANS_NO_MEMORY;
1139     }
1140 
1141     sptr<NotificationDoNotDisturbDate> dndDate(dndDatePtr);
1142     return proxy->SetDoNotDisturbDate(dndDate);
1143 }
1144 
GetDoNotDisturbDate(NotificationDoNotDisturbDate & doNotDisturbDate)1145 ErrCode AnsNotification::GetDoNotDisturbDate(NotificationDoNotDisturbDate &doNotDisturbDate)
1146 {
1147     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1148     if (!proxy) {
1149         ANS_LOGE("GetAnsManagerProxy fail.");
1150         return ERR_ANS_SERVICE_NOT_CONNECTED;
1151     }
1152 
1153     sptr<NotificationDoNotDisturbDate> dndDate = nullptr;
1154     auto ret = proxy->GetDoNotDisturbDate(dndDate);
1155     if (ret != ERR_OK) {
1156         ANS_LOGE("GetDoNotDisturbDate failed.");
1157         return ret;
1158     }
1159 
1160     if (!dndDate) {
1161         ANS_LOGE("Invalid DoNotDisturbDate.");
1162         return ERR_ANS_NO_MEMORY;
1163     }
1164 
1165     doNotDisturbDate = *dndDate;
1166     return ret;
1167 }
1168 
AddDoNotDisturbProfiles(const std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)1169 ErrCode AnsNotification::AddDoNotDisturbProfiles(const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
1170 {
1171     if (profiles.empty()) {
1172         ANS_LOGW("The profiles is empty.");
1173         return ERR_ANS_INVALID_PARAM;
1174     }
1175     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1176     if (!proxy) {
1177         ANS_LOGW("Get ans manager proxy fail.");
1178         return ERR_ANS_SERVICE_NOT_CONNECTED;
1179     }
1180     return proxy->AddDoNotDisturbProfiles(profiles);
1181 }
1182 
RemoveDoNotDisturbProfiles(const std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)1183 ErrCode AnsNotification::RemoveDoNotDisturbProfiles(const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
1184 {
1185     if (profiles.empty()) {
1186         ANS_LOGW("The profiles is empty.");
1187         return ERR_ANS_INVALID_PARAM;
1188     }
1189     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1190     if (!proxy) {
1191         ANS_LOGW("Get ans manager proxy fail.");
1192         return ERR_ANS_SERVICE_NOT_CONNECTED;
1193     }
1194     return proxy->RemoveDoNotDisturbProfiles(profiles);
1195 }
1196 
DoesSupportDoNotDisturbMode(bool & doesSupport)1197 ErrCode AnsNotification::DoesSupportDoNotDisturbMode(bool &doesSupport)
1198 {
1199     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1200     if (!proxy) {
1201         ANS_LOGE("GetAnsManagerProxy fail.");
1202         return ERR_ANS_SERVICE_NOT_CONNECTED;
1203     }
1204 
1205     return proxy->DoesSupportDoNotDisturbMode(doesSupport);
1206 }
1207 
IsNeedSilentInDoNotDisturbMode(const std::string & phoneNumber,int32_t callerType)1208 ErrCode AnsNotification::IsNeedSilentInDoNotDisturbMode(const std::string &phoneNumber, int32_t callerType)
1209 {
1210     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1211     if (!proxy) {
1212         ANS_LOGE("GetAnsManagerProxy fail.");
1213         return ERR_ANS_SERVICE_NOT_CONNECTED;
1214     }
1215 
1216     return proxy->IsNeedSilentInDoNotDisturbMode(phoneNumber, callerType);
1217 }
1218 
PublishContinuousTaskNotification(const NotificationRequest & request)1219 ErrCode AnsNotification::PublishContinuousTaskNotification(const NotificationRequest &request)
1220 {
1221     if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
1222         ANS_LOGE("Refuse to publish the notification without valid content");
1223         return ERR_ANS_INVALID_PARAM;
1224     }
1225 
1226     if (!CanPublishMediaContent(request)) {
1227         ANS_LOGE("Refuse to publish the notification because the sequence numbers actions not match those assigned to "
1228                  "added action buttons.");
1229         return ERR_ANS_INVALID_PARAM;
1230     }
1231 
1232     ErrCode checkErr = CheckImageSize(request);
1233     if (checkErr != ERR_OK) {
1234         ANS_LOGE("The size of one picture exceeds the limit");
1235         return checkErr;
1236     }
1237 
1238     if (!CanPublishLiveViewContent(request)) {
1239         ANS_LOGE("Refuse to publish the notification without valid live view content.");
1240         return ERR_ANS_INVALID_PARAM;
1241     }
1242 
1243     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1244     if (!proxy) {
1245         ANS_LOGE("GetAnsManagerProxy fail.");
1246         return ERR_ANS_SERVICE_NOT_CONNECTED;
1247     }
1248 
1249     auto pReq = new (std::nothrow) NotificationRequest(request);
1250     if (pReq == nullptr) {
1251         ANS_LOGE("Failed to create NotificationRequest ptr.");
1252         return ERR_ANS_NO_MEMORY;
1253     }
1254 
1255     sptr<NotificationRequest> sptrReq(pReq);
1256     if (IsNonDistributedNotificationType(sptrReq->GetNotificationType())) {
1257         sptrReq->SetDistributed(false);
1258     }
1259     return proxy->PublishContinuousTaskNotification(sptrReq);
1260 }
1261 
CancelContinuousTaskNotification(const std::string & label,int32_t notificationId)1262 ErrCode AnsNotification::CancelContinuousTaskNotification(const std::string &label, int32_t notificationId)
1263 {
1264     ANS_LOGI("enter CancelContinuousTaskNotification,notificationId:%{public}d", notificationId);
1265     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1266     if (!proxy) {
1267         ANS_LOGE("GetAnsManagerProxy fail.");
1268         return ERR_ANS_SERVICE_NOT_CONNECTED;
1269     }
1270 
1271     return proxy->CancelContinuousTaskNotification(label, notificationId);
1272 }
1273 
IsDistributedEnabled(bool & enabled)1274 ErrCode AnsNotification::IsDistributedEnabled(bool &enabled)
1275 {
1276     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1277     if (!proxy) {
1278         ANS_LOGE("GetAnsManagerProxy fail.");
1279         return ERR_ANS_SERVICE_NOT_CONNECTED;
1280     }
1281 
1282     return proxy->IsDistributedEnabled(enabled);
1283 }
1284 
EnableDistributed(const bool enabled)1285 ErrCode AnsNotification::EnableDistributed(const bool enabled)
1286 {
1287     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1288     if (!proxy) {
1289         ANS_LOGE("GetAnsManagerProxy fail.");
1290         return ERR_ANS_SERVICE_NOT_CONNECTED;
1291     }
1292 
1293     return proxy->EnableDistributed(enabled);
1294 }
1295 
EnableDistributedByBundle(const NotificationBundleOption & bundleOption,const bool enabled)1296 ErrCode AnsNotification::EnableDistributedByBundle(const NotificationBundleOption &bundleOption, const bool enabled)
1297 {
1298     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1299     if (!proxy) {
1300         ANS_LOGE("GetAnsManagerProxy fail.");
1301         return ERR_ANS_SERVICE_NOT_CONNECTED;
1302     }
1303 
1304     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1305     return proxy->EnableDistributedByBundle(bo, enabled);
1306 }
1307 
EnableDistributedSelf(const bool enabled)1308 ErrCode AnsNotification::EnableDistributedSelf(const bool enabled)
1309 {
1310     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1311     if (!proxy) {
1312         ANS_LOGE("GetAnsManagerProxy fail.");
1313         return ERR_ANS_SERVICE_NOT_CONNECTED;
1314     }
1315 
1316     return proxy->EnableDistributedSelf(enabled);
1317 }
1318 
IsDistributedEnableByBundle(const NotificationBundleOption & bundleOption,bool & enabled)1319 ErrCode AnsNotification::IsDistributedEnableByBundle(const NotificationBundleOption &bundleOption, bool &enabled)
1320 {
1321     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1322     if (!proxy) {
1323         ANS_LOGE("GetAnsManagerProxy fail.");
1324         return ERR_ANS_SERVICE_NOT_CONNECTED;
1325     }
1326 
1327     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1328     return proxy->IsDistributedEnableByBundle(bo, enabled);
1329 }
1330 
GetDeviceRemindType(NotificationConstant::RemindType & remindType)1331 ErrCode AnsNotification::GetDeviceRemindType(NotificationConstant::RemindType &remindType)
1332 {
1333     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1334     if (!proxy) {
1335         ANS_LOGE("GetAnsManagerProxy fail.");
1336         return ERR_ANS_SERVICE_NOT_CONNECTED;
1337     }
1338 
1339     return proxy->GetDeviceRemindType(remindType);
1340 }
1341 
ResetAnsManagerProxy()1342 void AnsNotification::ResetAnsManagerProxy()
1343 {}
1344 
Reconnect()1345 void AnsNotification::Reconnect()
1346 {
1347     ANS_LOGD("enter");
1348     for (int32_t i = 0; i < MAX_RETRY_TIME; i++) {
1349         // try to connect ans
1350         sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1351         if (!proxy) {
1352             // Sleep 1000 milliseconds before reconnect.
1353             std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
1354             ANS_LOGE("get ans proxy fail, try again.");
1355             continue;
1356         }
1357         ANS_LOGD("get ans proxy success.");
1358         return;
1359     }
1360 }
1361 
PublishReminder(ReminderRequest & reminder)1362 ErrCode AnsNotification::PublishReminder(ReminderRequest &reminder)
1363 {
1364     sptr<ReminderRequest> tarReminder = nullptr;
1365     switch (reminder.GetReminderType()) {
1366         case (ReminderRequest::ReminderType::TIMER): {
1367             ANSR_LOGI("Publish timer");
1368             ReminderRequestTimer &timer = (ReminderRequestTimer &)reminder;
1369             tarReminder = new (std::nothrow) ReminderRequestTimer(timer);
1370             break;
1371         }
1372         case (ReminderRequest::ReminderType::ALARM): {
1373             ANSR_LOGI("Publish alarm");
1374             ReminderRequestAlarm &alarm = (ReminderRequestAlarm &)reminder;
1375             tarReminder = new (std::nothrow) ReminderRequestAlarm(alarm);
1376             break;
1377         }
1378         case (ReminderRequest::ReminderType::CALENDAR): {
1379             ANSR_LOGI("Publish calendar");
1380             ReminderRequestCalendar &calendar = (ReminderRequestCalendar &)reminder;
1381             tarReminder = new (std::nothrow) ReminderRequestCalendar(calendar);
1382             break;
1383         }
1384         default: {
1385             ANSR_LOGW("PublishReminder fail.");
1386             return ERR_ANS_INVALID_PARAM;
1387         }
1388     }
1389     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1390     if (!proxy) {
1391         ANS_LOGE("GetAnsManagerProxy fail.");
1392         return ERR_ANS_SERVICE_NOT_CONNECTED;
1393     }
1394     ErrCode code = proxy->PublishReminder(tarReminder);
1395     reminder.SetReminderId(tarReminder->GetReminderId());
1396     return code;
1397 }
1398 
CancelReminder(const int32_t reminderId)1399 ErrCode AnsNotification::CancelReminder(const int32_t reminderId)
1400 {
1401     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1402     if (!proxy) {
1403         ANS_LOGE("GetAnsManagerProxy fail.");
1404         return ERR_ANS_SERVICE_NOT_CONNECTED;
1405     }
1406     return proxy->CancelReminder(reminderId);
1407 }
1408 
CancelAllReminders()1409 ErrCode AnsNotification::CancelAllReminders()
1410 {
1411     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1412     if (!proxy) {
1413         ANS_LOGE("GetAnsManagerProxy fail.");
1414         return ERR_ANS_SERVICE_NOT_CONNECTED;
1415     }
1416     return proxy->CancelAllReminders();
1417 }
1418 
GetValidReminders(std::vector<sptr<ReminderRequest>> & validReminders)1419 ErrCode AnsNotification::GetValidReminders(std::vector<sptr<ReminderRequest>> &validReminders)
1420 {
1421     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1422     if (!proxy) {
1423         ANS_LOGE("GetAnsManagerProxy fail.");
1424         return ERR_ANS_SERVICE_NOT_CONNECTED;
1425     }
1426     return proxy->GetValidReminders(validReminders);
1427 }
1428 
AddExcludeDate(const int32_t reminderId,const uint64_t date)1429 ErrCode AnsNotification::AddExcludeDate(const int32_t reminderId, const uint64_t date)
1430 {
1431     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1432     if (!proxy) {
1433         ANS_LOGE("GetAnsManagerProxy fail.");
1434         return ERR_ANS_SERVICE_NOT_CONNECTED;
1435     }
1436     return proxy->AddExcludeDate(reminderId, date);
1437 }
1438 
DelExcludeDates(const int32_t reminderId)1439 ErrCode AnsNotification::DelExcludeDates(const int32_t reminderId)
1440 {
1441     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1442     if (!proxy) {
1443         ANS_LOGE("GetAnsManagerProxy fail.");
1444         return ERR_ANS_SERVICE_NOT_CONNECTED;
1445     }
1446     return proxy->DelExcludeDates(reminderId);
1447 }
1448 
GetExcludeDates(const int32_t reminderId,std::vector<uint64_t> & dates)1449 ErrCode AnsNotification::GetExcludeDates(const int32_t reminderId, std::vector<uint64_t>& dates)
1450 {
1451     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1452     if (!proxy) {
1453         ANS_LOGE("GetAnsManagerProxy fail.");
1454         return ERR_ANS_SERVICE_NOT_CONNECTED;
1455     }
1456     return proxy->GetExcludeDates(reminderId, dates);
1457 }
1458 
GetAnsManagerProxy()1459 sptr<AnsManagerInterface> AnsNotification::GetAnsManagerProxy()
1460 {
1461     sptr<ISystemAbilityManager> systemAbilityManager =
1462         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1463     if (!systemAbilityManager) {
1464         ANS_LOGE("Failed to get system ability mgr.");
1465         return nullptr;
1466     }
1467 
1468     sptr<IRemoteObject> remoteObject =
1469         systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
1470     if (!remoteObject) {
1471         ANS_LOGE("Failed to get notification Manager.");
1472         return nullptr;
1473     }
1474 
1475     sptr<AnsManagerInterface> proxy = iface_cast<AnsManagerInterface>(remoteObject);
1476     if ((!proxy) || (!proxy->AsObject())) {
1477         ANS_LOGE("Failed to get notification Manager's proxy");
1478         return nullptr;
1479     }
1480     return proxy;
1481 }
1482 
CanPublishMediaContent(const NotificationRequest & request) const1483 bool AnsNotification::CanPublishMediaContent(const NotificationRequest &request) const
1484 {
1485     if (NotificationContent::Type::MEDIA != request.GetNotificationType()) {
1486         return true;
1487     }
1488 
1489     if (request.GetContent() == nullptr) {
1490         ANS_LOGE("Failed to publish notification with null content.");
1491         return false;
1492     }
1493 
1494     auto media = std::static_pointer_cast<NotificationMediaContent>(request.GetContent()->GetNotificationContent());
1495     if (media == nullptr) {
1496         ANS_LOGE("Failed to get media content.");
1497         return false;
1498     }
1499 
1500     auto showActions = media->GetShownActions();
1501     size_t size = request.GetActionButtons().size();
1502     for (auto it = showActions.begin(); it != showActions.end(); ++it) {
1503         if (*it > size) {
1504             ANS_LOGE("The sequence numbers actions is: %{public}d, the assigned to added action buttons size is: "
1505                      "%{public}zu.", *it, size);
1506             return false;
1507         }
1508     }
1509 
1510     return true;
1511 }
1512 
CanPublishLiveViewContent(const NotificationRequest & request) const1513 bool AnsNotification::CanPublishLiveViewContent(const NotificationRequest &request) const
1514 {
1515     if (!request.IsCommonLiveView()) {
1516         return true;
1517     }
1518 
1519     if (request.GetContent() == nullptr) {
1520         ANS_LOGE("Failed to publish notification with null content.");
1521         return false;
1522     }
1523 
1524     auto content = request.GetContent()->GetNotificationContent();
1525     auto liveView = std::static_pointer_cast<NotificationLiveViewContent>(content);
1526     if (liveView == nullptr) {
1527         ANS_LOGE("Failed to get live view content.");
1528         return false;
1529     }
1530 
1531     auto status = liveView->GetLiveViewStatus();
1532     if (status >= NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT) {
1533         ANS_LOGE("Invalid status %{public}u.", status);
1534         return false;
1535     }
1536 
1537     return true;
1538 }
1539 
CheckImageSize(const NotificationRequest & request)1540 ErrCode AnsNotification::CheckImageSize(const NotificationRequest &request)
1541 {
1542     auto littleIcon = request.GetLittleIcon();
1543     if (NotificationRequest::CheckImageOverSizeForPixelMap(littleIcon, MAX_ICON_SIZE)) {
1544         ANS_LOGE("The size of little icon exceeds limit");
1545         return ERR_ANS_ICON_OVER_SIZE;
1546     }
1547 
1548     auto overlayIcon = request.GetOverlayIcon();
1549     if (overlayIcon && NotificationRequest::CheckImageOverSizeForPixelMap(overlayIcon, MAX_ICON_SIZE)) {
1550         ANS_LOGE("The size of overlay icon exceeds limit");
1551         return ERR_ANS_ICON_OVER_SIZE;
1552     }
1553 
1554     ErrCode err = request.CheckImageSizeForContent();
1555     if (err != ERR_OK) {
1556         return err;
1557     }
1558 
1559     auto buttons = request.GetActionButtons();
1560     for (auto &btn : buttons) {
1561         if (!btn) {
1562             continue;
1563         }
1564         auto icon = btn->GetIcon();
1565         if (NotificationRequest::CheckImageOverSizeForPixelMap(icon, MAX_ICON_SIZE)) {
1566             ANS_LOGE("The size of icon in ActionButton exceeds limit");
1567             return ERR_ANS_ICON_OVER_SIZE;
1568         }
1569     }
1570 
1571     auto users = request.GetMessageUsers();
1572     for (auto &user : users) {
1573         if (!user) {
1574             continue;
1575         }
1576         auto icon = user->GetPixelMap();
1577         if (NotificationRequest::CheckImageOverSizeForPixelMap(icon, MAX_ICON_SIZE)) {
1578             ANS_LOGE("The size of picture in MessageUser exceeds limit");
1579             return ERR_ANS_ICON_OVER_SIZE;
1580         }
1581     }
1582 
1583     auto bigIcon = request.GetBigIcon();
1584     if (NotificationRequest::CheckImageOverSizeForPixelMap(bigIcon, MAX_ICON_SIZE)) {
1585         request.ResetBigIcon();
1586         ANS_LOGI("The size of big icon exceeds limit");
1587     }
1588 
1589     return ERR_OK;
1590 }
1591 
IsSupportTemplate(const std::string & templateName,bool & support)1592 ErrCode AnsNotification::IsSupportTemplate(const std::string &templateName, bool &support)
1593 {
1594     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1595     if (!proxy) {
1596         ANS_LOGE("GetAnsManagerProxy fail.");
1597         return ERR_ANS_SERVICE_NOT_CONNECTED;
1598     }
1599 
1600     return proxy->IsSupportTemplate(templateName, support);
1601 }
1602 
IsNonDistributedNotificationType(const NotificationContent::Type & type)1603 bool AnsNotification::IsNonDistributedNotificationType(const NotificationContent::Type &type)
1604 {
1605     return ((type == NotificationContent::Type::CONVERSATION) ||
1606         (type == NotificationContent::Type::PICTURE) ||
1607         (type == NotificationContent::Type::LIVE_VIEW));
1608 }
1609 
IsAllowedNotify(const int32_t & userId,bool & allowed)1610 ErrCode AnsNotification::IsAllowedNotify(const int32_t &userId, bool &allowed)
1611 {
1612     if (userId <= SUBSCRIBE_USER_INIT) {
1613         ANS_LOGE("Input userId is invalid.");
1614         return ERR_ANS_INVALID_PARAM;
1615     }
1616 
1617     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1618     if (!proxy) {
1619         ANS_LOGE("GetAnsManagerProxy fail.");
1620         return ERR_ANS_SERVICE_NOT_CONNECTED;
1621     }
1622 
1623     return proxy->IsSpecialUserAllowedNotify(userId, allowed);
1624 }
1625 
SetNotificationsEnabledForAllBundles(const int32_t & userId,bool enabled)1626 ErrCode AnsNotification::SetNotificationsEnabledForAllBundles(const int32_t &userId, bool enabled)
1627 {
1628     if (userId <= SUBSCRIBE_USER_INIT) {
1629         ANS_LOGE("Input userId is invalid.");
1630         return ERR_ANS_INVALID_PARAM;
1631     }
1632 
1633     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1634     if (!proxy) {
1635         ANS_LOGE("GetAnsManagerProxy fail.");
1636         return ERR_ANS_SERVICE_NOT_CONNECTED;
1637     }
1638     return proxy->SetNotificationsEnabledByUser(userId, enabled);
1639 }
1640 
RemoveNotifications(const int32_t & userId)1641 ErrCode AnsNotification::RemoveNotifications(const int32_t &userId)
1642 {
1643     if (userId <= SUBSCRIBE_USER_INIT) {
1644         ANS_LOGE("Input userId is invalid.");
1645         return ERR_ANS_INVALID_PARAM;
1646     }
1647 
1648     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1649     if (!proxy) {
1650         ANS_LOGE("GetAnsManagerProxy fail.");
1651         return ERR_ANS_SERVICE_NOT_CONNECTED;
1652     }
1653 
1654     return proxy->DeleteAllByUser(userId);
1655 }
1656 
SetDoNotDisturbDate(const int32_t & userId,const NotificationDoNotDisturbDate & doNotDisturbDate)1657 ErrCode AnsNotification::SetDoNotDisturbDate(const int32_t &userId,
1658     const NotificationDoNotDisturbDate &doNotDisturbDate)
1659 {
1660     if (userId <= SUBSCRIBE_USER_INIT) {
1661         ANS_LOGE("Input userId is invalid.");
1662         return ERR_ANS_INVALID_PARAM;
1663     }
1664 
1665     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1666     if (!proxy) {
1667         ANS_LOGE("GetAnsManagerProxy fail.");
1668         return ERR_ANS_SERVICE_NOT_CONNECTED;
1669     }
1670 
1671     auto dndDatePtr = new (std::nothrow) NotificationDoNotDisturbDate(doNotDisturbDate);
1672     if (dndDatePtr == nullptr) {
1673         ANS_LOGE("create DoNotDisturbDate failed.");
1674         return ERR_ANS_NO_MEMORY;
1675     }
1676 
1677     sptr<NotificationDoNotDisturbDate> dndDate(dndDatePtr);
1678     return proxy->SetDoNotDisturbDate(dndDate);
1679 }
1680 
GetDoNotDisturbDate(const int32_t & userId,NotificationDoNotDisturbDate & doNotDisturbDate)1681 ErrCode AnsNotification::GetDoNotDisturbDate(const int32_t &userId, NotificationDoNotDisturbDate &doNotDisturbDate)
1682 {
1683     if (userId <= SUBSCRIBE_USER_INIT) {
1684         ANS_LOGE("Input userId is invalid.");
1685         return ERR_ANS_INVALID_PARAM;
1686     }
1687 
1688     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1689     if (!proxy) {
1690         ANS_LOGE("GetAnsManagerProxy fail.");
1691         return ERR_ANS_SERVICE_NOT_CONNECTED;
1692     }
1693 
1694     sptr<NotificationDoNotDisturbDate> dndDate = nullptr;
1695     auto ret = proxy->GetDoNotDisturbDate(dndDate);
1696     if (ret != ERR_OK) {
1697         ANS_LOGE("Get DoNotDisturbDate failed.");
1698         return ret;
1699     }
1700 
1701     if (!dndDate) {
1702         ANS_LOGE("Invalid DoNotDisturbDate.");
1703         return ERR_ANS_NO_MEMORY;
1704     }
1705 
1706     doNotDisturbDate = *dndDate;
1707     return ret;
1708 }
1709 
SetEnabledForBundleSlot(const NotificationBundleOption & bundleOption,const NotificationConstant::SlotType & slotType,bool enabled,bool isForceControl)1710 ErrCode AnsNotification::SetEnabledForBundleSlot(const NotificationBundleOption &bundleOption,
1711     const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl)
1712 {
1713     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
1714     if (bundleOption.GetBundleName().empty()) {
1715         ANS_LOGE("Invalid bundle name.");
1716         return ERR_ANS_INVALID_PARAM;
1717     }
1718 
1719     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1720     if (!proxy) {
1721         ANS_LOGE("SetEnabledForBundleSlot fail.");
1722         return ERR_ANS_SERVICE_NOT_CONNECTED;
1723     }
1724 
1725     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1726     return proxy->SetEnabledForBundleSlot(bo, slotType, enabled, isForceControl);
1727 }
1728 
GetEnabledForBundleSlot(const NotificationBundleOption & bundleOption,const NotificationConstant::SlotType & slotType,bool & enabled)1729 ErrCode AnsNotification::GetEnabledForBundleSlot(
1730     const NotificationBundleOption &bundleOption, const NotificationConstant::SlotType &slotType, bool &enabled)
1731 {
1732     if (bundleOption.GetBundleName().empty()) {
1733         ANS_LOGE("Invalid bundle name.");
1734         return ERR_ANS_INVALID_PARAM;
1735     }
1736 
1737     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1738     if (!proxy) {
1739         ANS_LOGE("GetEnabledForBundleSlot fail.");
1740         return ERR_ANS_SERVICE_NOT_CONNECTED;
1741     }
1742 
1743     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1744     return proxy->GetEnabledForBundleSlot(bo, slotType, enabled);
1745 }
1746 
GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType & slotType,bool & enabled)1747 ErrCode AnsNotification::GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType &slotType, bool &enabled)
1748 {
1749     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1750     if (!proxy) {
1751         ANS_LOGE("GetEnabledForBundleSlotSelf fail.");
1752         return ERR_ANS_SERVICE_NOT_CONNECTED;
1753     }
1754 
1755     return proxy->GetEnabledForBundleSlotSelf(slotType, enabled);
1756 }
1757 
ShellDump(const std::string & cmd,const std::string & bundle,int32_t userId,int32_t recvUserId,std::vector<std::string> & dumpInfo)1758 ErrCode AnsNotification::ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId,
1759     int32_t recvUserId, std::vector<std::string> &dumpInfo)
1760 {
1761     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1762     if (!proxy) {
1763         ANS_LOGE("GetAnsManagerProxy fail.");
1764         return ERR_ANS_SERVICE_NOT_CONNECTED;
1765     }
1766 
1767     return proxy->ShellDump(cmd, bundle, userId, recvUserId, dumpInfo);
1768 }
1769 
SetSyncNotificationEnabledWithoutApp(const int32_t userId,const bool enabled)1770 ErrCode AnsNotification::SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled)
1771 {
1772     if (userId <= SUBSCRIBE_USER_INIT) {
1773         ANS_LOGE("Input userId is invalid.");
1774         return ERR_ANS_INVALID_PARAM;
1775     }
1776 
1777     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1778     if (!proxy) {
1779         ANS_LOGE("GetAnsManagerProxy fail.");
1780         return ERR_ANS_SERVICE_NOT_CONNECTED;
1781     }
1782 
1783     return proxy->SetSyncNotificationEnabledWithoutApp(userId, enabled);
1784 }
1785 
GetSyncNotificationEnabledWithoutApp(const int32_t userId,bool & enabled)1786 ErrCode AnsNotification::GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled)
1787 {
1788     if (userId <= SUBSCRIBE_USER_INIT) {
1789         ANS_LOGE("Input userId is invalid.");
1790         return ERR_ANS_INVALID_PARAM;
1791     }
1792 
1793     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1794     if (!proxy) {
1795         ANS_LOGE("GetAnsManagerProxy fail.");
1796         return ERR_ANS_SERVICE_NOT_CONNECTED;
1797     }
1798 
1799     return proxy->GetSyncNotificationEnabledWithoutApp(userId, enabled);
1800 }
1801 
SetBadgeNumber(int32_t badgeNumber)1802 ErrCode AnsNotification::SetBadgeNumber(int32_t badgeNumber)
1803 {
1804     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1805     if (!proxy) {
1806         ANS_LOGE("SetBadgeNumber fail.");
1807         return ERR_ANS_SERVICE_NOT_CONNECTED;
1808     }
1809     int32_t instanceKey = DEFAULT_INSTANCE_KEY;
1810     return proxy->SetBadgeNumber(badgeNumber, instanceKey);
1811 }
1812 
SetBadgeNumberByBundle(const NotificationBundleOption & bundleOption,int32_t badgeNumber)1813 ErrCode AnsNotification::SetBadgeNumberByBundle(const NotificationBundleOption &bundleOption, int32_t badgeNumber)
1814 {
1815     if (bundleOption.GetBundleName().empty()) {
1816         ANS_LOGE("Invalid bundle name.");
1817         return ERR_ANS_INVALID_PARAM;
1818     }
1819 
1820     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1821     if (!proxy) {
1822         ANS_LOGE("Unable to connect to ANS service.");
1823         return ERR_ANS_SERVICE_NOT_CONNECTED;
1824     }
1825 
1826     sptr<NotificationBundleOption> bundleInfo(new (std::nothrow) NotificationBundleOption(bundleOption));
1827     if (bundleInfo == nullptr) {
1828         ANS_LOGE("Unable to create new bundle info.");
1829         return ERR_ANS_NO_MEMORY;
1830     }
1831     return proxy->SetBadgeNumberByBundle(bundleInfo, badgeNumber);
1832 }
1833 
GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> & bundleOption)1834 ErrCode AnsNotification::GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> &bundleOption)
1835 {
1836     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1837     if (!proxy) {
1838         ANS_LOGE("Fail to GetAnsManagerProxy.");
1839         return ERR_ANS_SERVICE_NOT_CONNECTED;
1840     }
1841     return proxy->GetAllNotificationEnabledBundles(bundleOption);
1842 }
1843 
RegisterPushCallback(const sptr<IRemoteObject> & pushCallback,const sptr<NotificationCheckRequest> & notificationCheckRequest)1844 ErrCode AnsNotification::RegisterPushCallback(
1845     const sptr<IRemoteObject>& pushCallback, const sptr<NotificationCheckRequest> &notificationCheckRequest)
1846 {
1847     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1848     if (!proxy) {
1849         ANS_LOGE("RegisterPushCallback fail.");
1850         return ERR_ANS_SERVICE_NOT_CONNECTED;
1851     }
1852 
1853     return proxy->RegisterPushCallback(pushCallback, notificationCheckRequest);
1854 }
1855 
UnregisterPushCallback()1856 ErrCode AnsNotification::UnregisterPushCallback()
1857 {
1858     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1859     if (!proxy) {
1860         ANS_LOGE("UnregisterPushCallback fail.");
1861         return ERR_ANS_SERVICE_NOT_CONNECTED;
1862     }
1863 
1864     return proxy->UnregisterPushCallback();
1865 }
1866 
SetAdditionConfig(const std::string & key,const std::string & value)1867 ErrCode AnsNotification::SetAdditionConfig(const std::string &key, const std::string &value)
1868 {
1869     if (key.empty()) {
1870         ANS_LOGE("Set package config fail: key is empty.");
1871         return ERR_ANS_INVALID_PARAM;
1872     }
1873     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1874     if (!proxy) {
1875         ANS_LOGE("Get ans manager proxy fail.");
1876         return ERR_ANS_SERVICE_NOT_CONNECTED;
1877     }
1878 
1879     return proxy->SetAdditionConfig(key, value);
1880 }
1881 
SetDistributedEnabledByBundle(const NotificationBundleOption & bundleOption,const std::string & deviceType,const bool enabled)1882 ErrCode AnsNotification::SetDistributedEnabledByBundle(const NotificationBundleOption &bundleOption,
1883     const std::string &deviceType, const bool enabled)
1884 {
1885     ANS_LOGD("enter");
1886     if (bundleOption.GetBundleName().empty() || deviceType.empty()) {
1887         ANS_LOGE("Invalid bundle name.");
1888         return ERR_ANS_INVALID_PARAM;
1889     }
1890 
1891     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1892     if (!proxy) {
1893         ANS_LOGE("SetDistributedEnabledByBundleCallback fail.");
1894         return ERR_ANS_SERVICE_NOT_CONNECTED;
1895     }
1896 
1897     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1898     return proxy->SetDistributedEnabledByBundle(bo, deviceType, enabled);
1899 }
1900 
IsDistributedEnabledByBundle(const NotificationBundleOption & bundleOption,const std::string & deviceType,bool & enabled)1901 ErrCode AnsNotification::IsDistributedEnabledByBundle(const NotificationBundleOption &bundleOption,
1902     const std::string &deviceType, bool &enabled)
1903 {
1904     ANS_LOGD("enter");
1905     if (bundleOption.GetBundleName().empty() || deviceType.empty()) {
1906         ANS_LOGE("Invalid bundle name.");
1907         return ERR_ANS_INVALID_PARAM;
1908     }
1909 
1910     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1911     if (!proxy) {
1912         ANS_LOGE("IsDistributedEnabledByBundleCallback fail.");
1913         return ERR_ANS_SERVICE_NOT_CONNECTED;
1914     }
1915 
1916     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1917     return proxy->IsDistributedEnabledByBundle(bo, deviceType, enabled);
1918 }
1919 
SetSmartReminderEnabled(const std::string & deviceType,const bool enabled)1920 ErrCode AnsNotification::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled)
1921 {
1922     ANS_LOGD("enter");
1923     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1924     if (!proxy) {
1925         ANS_LOGE("UnregisterPushCallback fail.");
1926         return ERR_ANS_SERVICE_NOT_CONNECTED;
1927     }
1928 
1929     return proxy->SetSmartReminderEnabled(deviceType, enabled);
1930 }
1931 
CancelAsBundleWithAgent(const NotificationBundleOption & bundleOption,const int32_t id)1932 ErrCode AnsNotification::CancelAsBundleWithAgent(const NotificationBundleOption &bundleOption, const int32_t id)
1933 {
1934     ANS_LOGI("enter CancelAsBundleWithAgent,bundleName:%{public}s,id:%{public}d",
1935         bundleOption.GetBundleName().c_str(), id);
1936     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1937     if (!proxy) {
1938         ANS_LOGE("GetAnsManagerProxy fail.");
1939         return ERR_ANS_SERVICE_NOT_CONNECTED;
1940     }
1941 
1942     sptr<NotificationBundleOption> bundle(new (std::nothrow) NotificationBundleOption(bundleOption));
1943     return proxy->CancelAsBundleWithAgent(bundle, id);
1944 }
1945 
IsSmartReminderEnabled(const std::string & deviceType,bool & enabled)1946 ErrCode AnsNotification::IsSmartReminderEnabled(const std::string &deviceType, bool &enabled)
1947 {
1948     ANS_LOGD("enter");
1949     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1950     if (!proxy) {
1951         ANS_LOGE("UnregisterPushCallback fail.");
1952         return ERR_ANS_SERVICE_NOT_CONNECTED;
1953     }
1954 
1955     return proxy->IsSmartReminderEnabled(deviceType, enabled);
1956 }
1957 
SetTargetDeviceStatus(const std::string & deviceType,const uint32_t status)1958 ErrCode AnsNotification::SetTargetDeviceStatus(const std::string &deviceType, const uint32_t status)
1959 {
1960     ANS_LOGD("enter");
1961     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1962     if (!proxy) {
1963         ANS_LOGE("UnregisterPushCallback fail.");
1964         return ERR_ANS_SERVICE_NOT_CONNECTED;
1965     }
1966 
1967     return proxy->SetTargetDeviceStatus(deviceType, status);
1968 }
1969 
1970 
IsValidTemplate(const NotificationRequest & request) const1971 bool AnsNotification::IsValidTemplate(const NotificationRequest &request) const
1972 {
1973     if (request.GetTemplate() == nullptr) {
1974         return true;
1975     }
1976 
1977     std::string name = request.GetTemplate()->GetTemplateName();
1978     if (strcmp(name.c_str(), DOWNLOAD_TEMPLATE_NAME.c_str()) == 0) {
1979         std::shared_ptr<AAFwk::WantParams> data = request.GetTemplate()->GetTemplateData();
1980         if (data ==nullptr || !data->HasParam(DOWNLOAD_FILENAME) || !data->HasParam(DOWNLOAD_TITLE)) {
1981             ANS_LOGE("No required parameters.");
1982             return false;
1983         }
1984     }
1985 
1986     return true;
1987 }
1988 
IsValidDelayTime(const NotificationRequest & request) const1989 bool AnsNotification::IsValidDelayTime(const NotificationRequest &request)  const
1990 {
1991     return request.GetPublishDelayTime() <= MAX_PUBLISH_DELAY_TIME;
1992 }
1993 
GetDoNotDisturbProfile(int32_t id,sptr<NotificationDoNotDisturbProfile> & profile)1994 ErrCode AnsNotification::GetDoNotDisturbProfile(int32_t id, sptr<NotificationDoNotDisturbProfile> &profile)
1995 {
1996     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
1997     if (!proxy) {
1998         ANS_LOGE("Fail to GetAnsManagerProxy.");
1999         return ERR_ANS_SERVICE_NOT_CONNECTED;
2000     }
2001     return proxy->GetDoNotDisturbProfile(id, profile);
2002 }
2003 
CreateSubscribeListener(const std::shared_ptr<NotificationSubscriber> & subscriber,sptr<SubscriberListener> & listener)2004 void AnsNotification::CreateSubscribeListener(const std::shared_ptr<NotificationSubscriber> &subscriber,
2005     sptr<SubscriberListener> &listener)
2006 {
2007     std::lock_guard<std::mutex> lock(subscriberMutex_);
2008     auto item = subscribers_.find(subscriber);
2009     if (item != subscribers_.end()) {
2010         listener = item->second;
2011         ANS_LOGD("subscriber has listener");
2012         return;
2013     }
2014     listener = new (std::nothrow) SubscriberListener(subscriber);
2015     if (listener != nullptr) {
2016         subscribers_[subscriber] = listener;
2017         ANS_LOGD("CreateSubscribeListener success");
2018     }
2019 }
2020 
OnServiceDied()2021 void AnsNotification::OnServiceDied()
2022 {
2023     std::lock_guard<std::mutex> lock(subscriberMutex_);
2024     for (auto item : subscribers_) {
2025         item.first->OnDied();
2026     }
2027 }
2028 
2029 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
RegisterSwingCallback(const std::function<void (bool,int)> swingCbFunc)2030 ErrCode AnsNotification::RegisterSwingCallback(const std::function<void(bool, int)> swingCbFunc)
2031 {
2032     ANS_LOGD("enter");
2033     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
2034     if (!proxy) {
2035         ANS_LOGE("RegisterSwingCallback fail.");
2036         return ERR_ANS_SERVICE_NOT_CONNECTED;
2037     }
2038     swingCallBackStub_ = new(std::nothrow) SwingCallBackStub(swingCbFunc);
2039     if (swingCallBackStub_ == nullptr) {
2040         ANS_LOGE("RegisterSwingCallback swingCallBackStub_ == null");
2041         return ERR_ANS_INVALID_PARAM;
2042     }
2043     return proxy->RegisterSwingCallback(swingCallBackStub_->AsObject());
2044 }
2045 #endif
2046 
UpdateNotificationTimerByUid(const int32_t uid,const bool isPaused)2047 ErrCode AnsNotification::UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused)
2048 {
2049     sptr<AnsManagerInterface> proxy = GetAnsManagerProxy();
2050     if (!proxy) {
2051         ANS_LOGE("UpdateNotificationTimerByUid fail.");
2052         return ERR_ANS_SERVICE_NOT_CONNECTED;
2053     }
2054     return proxy->UpdateNotificationTimerByUid(uid, isPaused);
2055 }
2056 }  // namespace Notification
2057 }  // namespace OHOS
2058