• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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_trace_wrapper.h"
21 #include "ans_manager_death_recipient.h"
22 #include "ans_manager_proxy.h"
23 #include "hitrace_meter_adapter.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "notification_button_option.h"
27 #include "notification_local_live_view_subscriber.h"
28 #include "system_ability_definition.h"
29 #include "unique_fd.h"
30 #include "hitrace_util.h"
31 
32 #include <memory>
33 #include <thread>
34 
35 namespace OHOS {
36 namespace Notification {
37 namespace {
38 const int32_t MAX_RETRY_TIME = 30;
39 const int32_t SLEEP_TIME = 1000;
40 const uint32_t MAX_PUBLISH_DELAY_TIME = 5;
41 const std::string DOWNLOAD_TITLE = "title";
42 const std::string DOWNLOAD_FILENAME = "fileName";
43 const static int MAX_SLOT_FLAGS = 0b111111;
44 }
AddNotificationSlot(const NotificationSlot & slot)45 ErrCode AnsNotification::AddNotificationSlot(const NotificationSlot &slot)
46 {
47     std::vector<NotificationSlot> slots;
48     slots.push_back(slot);
49     return AddNotificationSlots(slots);
50 }
51 
AddSlotByType(const NotificationConstant::SlotType & slotType)52 ErrCode AnsNotification::AddSlotByType(const NotificationConstant::SlotType &slotType)
53 {
54     sptr<IAnsManager> proxy = GetAnsManagerProxy();
55     if (!proxy) {
56         ANS_LOGE("GetAnsManagerProxy fail.");
57         return ERR_ANS_SERVICE_NOT_CONNECTED;
58     }
59     return proxy->AddSlotByType(slotType);
60 }
61 
AddNotificationSlots(const std::vector<NotificationSlot> & slots)62 ErrCode AnsNotification::AddNotificationSlots(const std::vector<NotificationSlot> &slots)
63 {
64     if (slots.size() == 0) {
65         ANS_LOGE("Failed to add notification slots because input slots size is 0.");
66         return ERR_ANS_INVALID_PARAM;
67     }
68 
69     sptr<IAnsManager> proxy = GetAnsManagerProxy();
70     if (!proxy) {
71         ANS_LOGE("GetAnsManagerProxy fail.");
72         return ERR_ANS_SERVICE_NOT_CONNECTED;
73     }
74 
75     std::vector<sptr<NotificationSlot>> slotsSptr;
76     for (auto it = slots.begin(); it != slots.end(); ++it) {
77         sptr<NotificationSlot> slot = new (std::nothrow) NotificationSlot(*it);
78         if (slot == nullptr) {
79             ANS_LOGE("null slot");
80             return ERR_ANS_NO_MEMORY;
81         }
82         slotsSptr.emplace_back(slot);
83     }
84 
85     size_t slotsSize = slotsSptr.size();
86     if (slotsSize > MAX_SLOT_NUM) {
87         ANS_LOGE("slotsSize over max size");
88         return ERR_ANS_INVALID_PARAM;
89     }
90 
91     return proxy->AddSlots(slotsSptr);
92 }
93 
RemoveNotificationSlot(const NotificationConstant::SlotType & slotType)94 ErrCode AnsNotification::RemoveNotificationSlot(const NotificationConstant::SlotType &slotType)
95 {
96     ANS_LOGI("slotType:%{public}d", slotType);
97     sptr<IAnsManager> proxy = GetAnsManagerProxy();
98     if (!proxy) {
99         ANS_LOGE("GetAnsManagerProxy fail.");
100         return ERR_ANS_SERVICE_NOT_CONNECTED;
101     }
102     return proxy->RemoveSlotByType(slotType);
103 }
104 
RemoveAllSlots()105 ErrCode AnsNotification::RemoveAllSlots()
106 {
107     sptr<IAnsManager> proxy = GetAnsManagerProxy();
108     if (!proxy) {
109         ANS_LOGE("GetAnsManagerProxy fail.");
110         return ERR_ANS_SERVICE_NOT_CONNECTED;
111     }
112     return proxy->RemoveAllSlots();
113 }
114 
GetNotificationSlot(const NotificationConstant::SlotType & slotType,sptr<NotificationSlot> & slot)115 ErrCode AnsNotification::GetNotificationSlot(
116     const NotificationConstant::SlotType &slotType, sptr<NotificationSlot> &slot)
117 {
118     sptr<IAnsManager> proxy = GetAnsManagerProxy();
119     if (!proxy) {
120         ANS_LOGE("GetAnsManagerProxy fail.");
121         return ERR_ANS_SERVICE_NOT_CONNECTED;
122     }
123     return proxy->GetSlotByType(slotType, slot);
124 }
125 
GetNotificationSlots(std::vector<sptr<NotificationSlot>> & slots)126 ErrCode AnsNotification::GetNotificationSlots(std::vector<sptr<NotificationSlot>> &slots)
127 {
128     sptr<IAnsManager> proxy = GetAnsManagerProxy();
129     if (!proxy) {
130         ANS_LOGE("GetAnsManagerProxy fail.");
131         return ERR_ANS_SERVICE_NOT_CONNECTED;
132     }
133     return proxy->GetSlots(slots);
134 }
135 
GetNotificationSlotNumAsBundle(const NotificationBundleOption & bundleOption,uint64_t & num)136 ErrCode AnsNotification::GetNotificationSlotNumAsBundle(const NotificationBundleOption &bundleOption, uint64_t &num)
137 {
138     if (bundleOption.GetBundleName().empty()) {
139         ANS_LOGE("Invalid bundle name.");
140         return ERR_ANS_INVALID_PARAM;
141     }
142 
143     sptr<IAnsManager> proxy = GetAnsManagerProxy();
144     if (!proxy) {
145         ANS_LOGE("Fail to GetAnsManagerProxy.");
146         return ERR_ANS_SERVICE_NOT_CONNECTED;
147     }
148 
149     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
150     if (bo == nullptr) {
151         ANS_LOGE("null bundleOption");
152         return ERR_ANS_INVALID_PARAM;
153     }
154     return proxy->GetSlotNumAsBundle(bo, num);
155 }
156 
GetNotificationSlotFlagsAsBundle(const NotificationBundleOption & bundleOption,uint32_t & slotFlags)157 ErrCode AnsNotification::GetNotificationSlotFlagsAsBundle(const NotificationBundleOption &bundleOption,
158     uint32_t &slotFlags)
159 {
160     if (bundleOption.GetBundleName().empty()) {
161         ANS_LOGE("Invalid bundle name.");
162         return ERR_ANS_INVALID_PARAM;
163     }
164 
165     sptr<IAnsManager> proxy = GetAnsManagerProxy();
166     if (!proxy) {
167         ANS_LOGE("Fail to GetAnsManagerProxy.");
168         return ERR_ANS_SERVICE_NOT_CONNECTED;
169     }
170 
171     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
172     if (bo == nullptr) {
173         ANS_LOGE("null bundleOption");
174         return ERR_ANS_INVALID_PARAM;
175     }
176     return proxy->GetSlotFlagsAsBundle(bo, slotFlags);
177 }
178 
GetNotificationSettings(uint32_t & slotFlags)179 ErrCode AnsNotification::GetNotificationSettings(uint32_t &slotFlags)
180 {
181     sptr<IAnsManager> proxy = GetAnsManagerProxy();
182     if (!proxy) {
183         ANS_LOGE("Fail to GetAnsManagerProxy.");
184         return ERR_ANS_SERVICE_NOT_CONNECTED;
185     }
186 
187     return proxy->GetNotificationSettings(slotFlags);
188 }
189 
SetNotificationSlotFlagsAsBundle(const NotificationBundleOption & bundleOption,uint32_t slotFlags)190 ErrCode AnsNotification::SetNotificationSlotFlagsAsBundle(const NotificationBundleOption &bundleOption,
191     uint32_t slotFlags)
192 {
193     if (bundleOption.GetBundleName().empty()) {
194         ANS_LOGE("Invalid bundle name.");
195         return ERR_ANS_INVALID_PARAM;
196     }
197     ANS_LOGI("bundleName:%{public}s, %{public}d", bundleOption.GetBundleName().c_str(), (int)slotFlags);
198 
199     sptr<IAnsManager> proxy = GetAnsManagerProxy();
200     if (!proxy) {
201         ANS_LOGE("Fail to GetAnsManagerProxy.");
202         return ERR_ANS_SERVICE_NOT_CONNECTED;
203     }
204 
205     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
206 
207     if (bo == nullptr) {
208         ANS_LOGE("null bundleOption");
209         return ERR_ANS_INVALID_PARAM;
210     }
211 
212     if (slotFlags > MAX_SLOT_FLAGS) {
213         ANS_LOGE("Invalid slotFlags");
214         return ERR_ANS_INVALID_PARAM;
215     }
216     // got the LSB 6 bits as slotflags;
217     uint32_t validSlotFlag = MAX_SLOT_FLAGS & slotFlags;
218 
219     return proxy->SetSlotFlagsAsBundle(bo, validSlotFlag);
220 }
221 
PublishNotification(const NotificationRequest & request,const std::string & instanceKey)222 ErrCode AnsNotification::PublishNotification(const NotificationRequest &request, const std::string &instanceKey)
223 {
224     ANS_LOGD("called");
225     return PublishNotification(std::string(), request, instanceKey);
226 }
227 
PublishNotification(const std::string & label,const NotificationRequest & request,const std::string & instanceKey)228 ErrCode AnsNotification::PublishNotification(const std::string &label, const NotificationRequest &request,
229     const std::string &instanceKey)
230 {
231     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
232     ANS_LOGI("notificationId:%{public}u", request.GetNotificationId());
233 
234     if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
235         ANS_LOGE("Refuse to publish the notification without valid content");
236         return ERR_ANS_INVALID_PARAM;
237     }
238 
239     if (!IsValidTemplate(request) || !IsValidDelayTime(request)) {
240         return ERR_ANS_INVALID_PARAM;
241     }
242 
243     if (!CanPublishMediaContent(request)) {
244         ANS_LOGE("Refuse to publish the notification because the series numbers actions not match those assigned to "
245                  "added action buttons.");
246         return ERR_ANS_INVALID_PARAM;
247     }
248 
249     if (!CanPublishLiveViewContent(request)) {
250         ANS_LOGE("Refuse to publish the notification without valid live view content.");
251         return ERR_ANS_INVALID_PARAM;
252     }
253 
254     ErrCode checkErr = CheckImageSize(request);
255     if (checkErr != ERR_OK) {
256         ANS_LOGE("The size of one picture exceeds the limit");
257         return checkErr;
258     }
259 
260     sptr<IAnsManager> proxy = GetAnsManagerProxy();
261     if (!proxy) {
262         ANS_LOGE("Failed to GetAnsManagerProxy.");
263         return ERR_ANS_SERVICE_NOT_CONNECTED;
264     }
265 
266     sptr<NotificationRequest> reqPtr = new (std::nothrow) NotificationRequest(request);
267     if (reqPtr == nullptr) {
268         ANS_LOGE("null reqPtr");
269         return ERR_ANS_NO_MEMORY;
270     }
271     if (IsNonDistributedNotificationType(reqPtr->GetNotificationType())) {
272         reqPtr->SetDistributed(false);
273     }
274     reqPtr->SetAppInstanceKey(instanceKey);
275     if (reqPtr->IsCommonLiveView()) {
276         return proxy->PublishWithMaxCapacity(label, reqPtr);
277     }
278     return proxy->Publish(label, reqPtr);
279 }
280 
PublishNotificationForIndirectProxy(const NotificationRequest & request)281 ErrCode AnsNotification::PublishNotificationForIndirectProxy(const NotificationRequest &request)
282 {
283     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
284     TraceChainUtil traceChain = TraceChainUtil();
285     ANS_LOGI("notificationId:%{public}u", request.GetNotificationId());
286 
287     if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
288         ANS_LOGE("Refuse to publish the notification without valid content");
289         return ERR_ANS_INVALID_PARAM;
290     }
291 
292     if (!IsValidTemplate(request) || !IsValidDelayTime(request)) {
293         return ERR_ANS_INVALID_PARAM;
294     }
295 
296     if (!CanPublishMediaContent(request)) {
297         ANS_LOGE("Refuse to publish the notification because the series numbers actions not match those assigned to "
298                  "added action buttons.");
299         return ERR_ANS_INVALID_PARAM;
300     }
301 
302     if (!CanPublishLiveViewContent(request)) {
303         ANS_LOGE("Refuse to publish the notification without valid live view content.");
304         return ERR_ANS_INVALID_PARAM;
305     }
306 
307     ErrCode checkErr = CheckImageSize(request);
308     if (checkErr != ERR_OK) {
309         ANS_LOGE("The size of one picture exceeds the limit");
310         return checkErr;
311     }
312 
313     sptr<IAnsManager> proxy = GetAnsManagerProxy();
314     if (!proxy) {
315         ANS_LOGE("Failed to GetAnsManagerProxy.");
316         return ERR_ANS_SERVICE_NOT_CONNECTED;
317     }
318 
319     sptr<NotificationRequest> reqPtr = new (std::nothrow) NotificationRequest(request);
320     if (reqPtr == nullptr) {
321         ANS_LOGE("null reqPtr");
322         return ERR_ANS_NO_MEMORY;
323     }
324     if (IsNonDistributedNotificationType(reqPtr->GetNotificationType())) {
325         reqPtr->SetDistributed(false);
326     }
327     if (reqPtr->IsCommonLiveView()) {
328         return proxy->PublishNotificationForIndirectProxyWithMaxCapacity(reqPtr);
329     }
330     return proxy->PublishNotificationForIndirectProxy(reqPtr);
331 }
332 
CancelNotification(int32_t notificationId,const std::string & instanceKey)333 ErrCode AnsNotification::CancelNotification(int32_t notificationId, const std::string &instanceKey)
334 {
335     return CancelNotification("", notificationId, instanceKey);
336 }
337 
CancelNotification(const std::string & label,int32_t notificationId,const std::string & instanceKey)338 ErrCode AnsNotification::CancelNotification(const std::string &label, int32_t notificationId,
339     const std::string &instanceKey)
340 {
341     ANS_LOGI("notificationId:%{public}d", notificationId);
342     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
343     sptr<IAnsManager> proxy = GetAnsManagerProxy();
344     if (!proxy) {
345         ANS_LOGE("GetAnsManagerProxy fail.");
346         return ERR_ANS_SERVICE_NOT_CONNECTED;
347     }
348     return proxy->Cancel(notificationId, label, instanceKey);
349 }
350 
CancelAllNotifications(const std::string & instanceKey)351 ErrCode AnsNotification::CancelAllNotifications(const std::string &instanceKey)
352 {
353     ANS_LOGI("called");
354 
355     sptr<IAnsManager> proxy = GetAnsManagerProxy();
356     if (!proxy) {
357         ANS_LOGE("GetAnsManagerProxy fail.");
358         return ERR_ANS_SERVICE_NOT_CONNECTED;
359     }
360     return proxy->CancelAll(instanceKey);
361 }
362 
CancelAsBundle(int32_t notificationId,const std::string & representativeBundle,int32_t userId)363 ErrCode AnsNotification::CancelAsBundle(
364     int32_t notificationId, const std::string &representativeBundle, int32_t userId)
365 {
366     ANS_LOGI("notificationId:%{public}d", notificationId);
367     sptr<IAnsManager> proxy = GetAnsManagerProxy();
368     if (!proxy) {
369         ANS_LOGE("GetAnsManagerProxy fail.");
370         return ERR_ANS_SERVICE_NOT_CONNECTED;
371     }
372     return proxy->CancelAsBundle(notificationId, representativeBundle, userId);
373 }
374 
CancelAsBundle(const NotificationBundleOption & bundleOption,int32_t notificationId)375 ErrCode AnsNotification::CancelAsBundle(
376     const NotificationBundleOption &bundleOption, int32_t notificationId)
377 {
378     ANS_LOGI("notificationId:%{public}d", notificationId);
379     sptr<IAnsManager> proxy = GetAnsManagerProxy();
380     if (!proxy) {
381         ANS_LOGE("GetAnsManagerProxy fail.");
382         return ERR_ANS_SERVICE_NOT_CONNECTED;
383     }
384     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
385     return proxy->CancelAsBundle(bo, notificationId);
386 }
387 
GetActiveNotificationNums(uint64_t & num)388 ErrCode AnsNotification::GetActiveNotificationNums(uint64_t &num)
389 {
390     sptr<IAnsManager> proxy = GetAnsManagerProxy();
391     if (!proxy) {
392         ANS_LOGE("GetAnsManagerProxy fail.");
393         return ERR_ANS_SERVICE_NOT_CONNECTED;
394     }
395     return proxy->GetActiveNotificationNums(num);
396 }
397 
GetActiveNotifications(std::vector<sptr<NotificationRequest>> & request,const std::string & instanceKey)398 ErrCode AnsNotification::GetActiveNotifications(std::vector<sptr<NotificationRequest>> &request,
399     const std::string &instanceKey)
400 {
401     sptr<IAnsManager> proxy = GetAnsManagerProxy();
402     if (!proxy) {
403         ANS_LOGE("GetAnsManagerProxy fail.");
404         return ERR_ANS_SERVICE_NOT_CONNECTED;
405     }
406     return proxy->GetActiveNotifications(request, instanceKey);
407 }
408 
CanPublishNotificationAsBundle(const std::string & representativeBundle,bool & canPublish)409 ErrCode AnsNotification::CanPublishNotificationAsBundle(const std::string &representativeBundle, bool &canPublish)
410 {
411     if (representativeBundle.empty()) {
412         ANS_LOGW("Input representativeBundle is empty");
413         return ERR_ANS_INVALID_PARAM;
414     }
415 
416     sptr<IAnsManager> proxy = GetAnsManagerProxy();
417     if (!proxy) {
418         ANS_LOGE("GetAnsManagerProxy fail.");
419         return ERR_ANS_SERVICE_NOT_CONNECTED;
420     }
421     return proxy->CanPublishAsBundle(representativeBundle, canPublish);
422 }
423 
PublishNotificationAsBundle(const std::string & representativeBundle,const NotificationRequest & request)424 ErrCode AnsNotification::PublishNotificationAsBundle(
425     const std::string &representativeBundle, const NotificationRequest &request)
426 {
427     ANS_LOGI("Bundle:%{public}s, notificationId:%{public}u",
428         representativeBundle.c_str(), request.GetNotificationId());
429     if (representativeBundle.empty()) {
430         ANS_LOGE("Refuse to publish the notification whit invalid representativeBundle");
431         return ERR_ANS_INVALID_PARAM;
432     }
433 
434     if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
435         ANS_LOGE("Refuse to publish the notification without effective content");
436         return ERR_ANS_INVALID_PARAM;
437     }
438 
439     if (!CanPublishMediaContent(request)) {
440         ANS_LOGE("Refuse to publish the notification because the sequence numbers actions not match those assigned to "
441                  "added action buttons.");
442         return ERR_ANS_INVALID_PARAM;
443     }
444 
445     if (!CanPublishLiveViewContent(request)) {
446         ANS_LOGE("Refuse to publish the notification without valid live view content.");
447         return ERR_ANS_INVALID_PARAM;
448     }
449 
450     ErrCode checkErr = CheckImageSize(request);
451     if (checkErr != ERR_OK) {
452         ANS_LOGE("The size of one picture overtake the limit");
453         return checkErr;
454     }
455 
456     sptr<IAnsManager> proxy = GetAnsManagerProxy();
457     if (!proxy) {
458         ANS_LOGE("GetAnsManagerProxy fail.");
459         return ERR_ANS_SERVICE_NOT_CONNECTED;
460     }
461 
462     sptr<NotificationRequest> reqPtr = new (std::nothrow) NotificationRequest(request);
463     if (reqPtr == nullptr) {
464         ANS_LOGE("null reqPtr");
465         return ERR_ANS_NO_MEMORY;
466     }
467     if (IsNonDistributedNotificationType(reqPtr->GetNotificationType())) {
468         reqPtr->SetDistributed(false);
469     }
470     if (reqPtr->IsCommonLiveView()) {
471         return proxy->PublishAsBundleWithMaxCapacity(reqPtr, representativeBundle);
472     }
473     return proxy->PublishAsBundle(reqPtr, representativeBundle);
474 }
475 
SetNotificationBadgeNum()476 ErrCode AnsNotification::SetNotificationBadgeNum()
477 {
478     sptr<IAnsManager> proxy = GetAnsManagerProxy();
479     if (!proxy) {
480         ANS_LOGE("GetAnsManagerProxy fail.");
481         return ERR_ANS_SERVICE_NOT_CONNECTED;
482     }
483     int32_t num = -1;
484     return proxy->SetNotificationBadgeNum(num);
485 }
486 
SetNotificationBadgeNum(int32_t num)487 ErrCode AnsNotification::SetNotificationBadgeNum(int32_t num)
488 {
489     sptr<IAnsManager> proxy = GetAnsManagerProxy();
490     if (!proxy) {
491         ANS_LOGE("GetAnsManagerProxy fail.");
492         return ERR_ANS_SERVICE_NOT_CONNECTED;
493     }
494     return proxy->SetNotificationBadgeNum(num);
495 }
496 
IsAllowedNotify(bool & allowed)497 ErrCode AnsNotification::IsAllowedNotify(bool &allowed)
498 {
499     sptr<IAnsManager> proxy = GetAnsManagerProxy();
500     if (!proxy) {
501         ANS_LOGE("GetAnsManagerProxy fail.");
502         return ERR_ANS_SERVICE_NOT_CONNECTED;
503     }
504     return proxy->IsAllowedNotify(allowed);
505 }
506 
IsAllowedNotifySelf(bool & allowed)507 ErrCode AnsNotification::IsAllowedNotifySelf(bool &allowed)
508 {
509     ANS_LOGD("called");
510     sptr<IAnsManager> proxy = GetAnsManagerProxy();
511     if (!proxy) {
512         ANS_LOGE("GetAnsManagerProxy fail.");
513         return ERR_ANS_SERVICE_NOT_CONNECTED;
514     }
515     return proxy->IsAllowedNotifySelf(allowed);
516 }
517 
CanPopEnableNotificationDialog(sptr<AnsDialogHostClient> & hostClient,bool & canPop,std::string & bundleName)518 ErrCode AnsNotification::CanPopEnableNotificationDialog(sptr<AnsDialogHostClient> &hostClient,
519     bool &canPop, std::string &bundleName)
520 {
521     ANS_LOGD("called");
522     sptr<IAnsManager> proxy = GetAnsManagerProxy();
523     if (!proxy) {
524         ANS_LOGE("GetAnsManagerProxy fail.");
525         return ERR_ANS_SERVICE_NOT_CONNECTED;
526     }
527     return proxy->CanPopEnableNotificationDialog(hostClient, canPop, bundleName);
528 }
529 
RemoveEnableNotificationDialog()530 ErrCode AnsNotification::RemoveEnableNotificationDialog()
531 {
532     ANS_LOGD("called");
533     sptr<IAnsManager> proxy = GetAnsManagerProxy();
534     if (!proxy) {
535         ANS_LOGE("GetAnsManagerProxy fail.");
536         return ERR_ANS_SERVICE_NOT_CONNECTED;
537     }
538     return proxy->RemoveEnableNotificationDialog();
539 }
540 
RequestEnableNotification(std::string & deviceId,sptr<AnsDialogHostClient> & hostClient,sptr<IRemoteObject> & callerToken)541 ErrCode AnsNotification::RequestEnableNotification(std::string &deviceId,
542     sptr<AnsDialogHostClient> &hostClient,
543     sptr<IRemoteObject> &callerToken)
544 {
545     ANS_LOGD("called");
546     sptr<IAnsManager> proxy = GetAnsManagerProxy();
547     if (!proxy) {
548         ANS_LOGE("GetAnsManagerProxy fail.");
549         return ERR_ANS_SERVICE_NOT_CONNECTED;
550     }
551     if (hostClient == nullptr) {
552         ANS_LOGE("null hostClient");
553         return ERR_ANS_INVALID_PARAM;
554     }
555     if (callerToken == nullptr) {
556         return proxy->RequestEnableNotification(deviceId, hostClient);
557     }
558     return proxy->RequestEnableNotification(deviceId, hostClient, callerToken);
559 }
560 
RequestEnableNotification(const std::string bundleName,const int32_t uid)561 ErrCode AnsNotification::RequestEnableNotification(const std::string bundleName, const int32_t uid)
562 {
563     ANS_LOGD("called");
564     sptr<IAnsManager> proxy = GetAnsManagerProxy();
565     if (!proxy) {
566         ANS_LOGE("GetAnsManagerProxy fail.");
567         return ERR_ANS_SERVICE_NOT_CONNECTED;
568     }
569     return proxy->RequestEnableNotification(bundleName, uid);
570 }
571 
HasNotificationPolicyAccessPermission(bool & hasPermission)572 ErrCode AnsNotification::HasNotificationPolicyAccessPermission(bool &hasPermission)
573 {
574     sptr<IAnsManager> proxy = GetAnsManagerProxy();
575     if (!proxy) {
576         ANS_LOGE("GetAnsManagerProxy fail.");
577         return ERR_ANS_SERVICE_NOT_CONNECTED;
578     }
579     return proxy->HasNotificationPolicyAccessPermission(hasPermission);
580 }
581 
GetBundleImportance(NotificationSlot::NotificationLevel & importance)582 ErrCode AnsNotification::GetBundleImportance(NotificationSlot::NotificationLevel &importance)
583 {
584     sptr<IAnsManager> proxy = GetAnsManagerProxy();
585     if (!proxy) {
586         ANS_LOGE("GetAnsManagerProxy fail.");
587         return ERR_ANS_SERVICE_NOT_CONNECTED;
588     }
589     int32_t importanceTemp;
590     ErrCode ret = proxy->GetBundleImportance(importanceTemp);
591     if ((NotificationSlot::LEVEL_NONE <= importanceTemp) && (importanceTemp <= NotificationSlot::LEVEL_HIGH)) {
592         importance = static_cast<NotificationSlot::NotificationLevel>(importanceTemp);
593     } else {
594         importance = NotificationSlot::LEVEL_UNDEFINED;
595     }
596     return ret;
597 }
598 
SubscribeNotification(const NotificationSubscriber & subscriber)599 ErrCode AnsNotification::SubscribeNotification(const NotificationSubscriber &subscriber)
600 {
601     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
602     sptr<IAnsManager> proxy = GetAnsManagerProxy();
603     if (!proxy) {
604         ANS_LOGE("GetAnsManagerProxy fail.");
605         return ERR_ANS_SERVICE_NOT_CONNECTED;
606     }
607 
608     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
609     if (subscriberSptr == nullptr) {
610         ANS_LOGE("null subscriberSptr");
611         return ERR_ANS_INVALID_PARAM;
612     }
613     return proxy->Subscribe(subscriberSptr);
614 }
615 
SubscribeNotificationSelf(const NotificationSubscriber & subscriber)616 ErrCode AnsNotification::SubscribeNotificationSelf(const NotificationSubscriber &subscriber)
617 {
618     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
619     sptr<IAnsManager> proxy = GetAnsManagerProxy();
620     if (!proxy) {
621         ANS_LOGE("GetAnsManagerProxy fail.");
622         return ERR_ANS_SERVICE_NOT_CONNECTED;
623     }
624 
625     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
626     if (subscriberSptr == nullptr) {
627         ANS_LOGE("null subscriberSptr");
628         return ERR_ANS_INVALID_PARAM;
629     }
630     return proxy->SubscribeSelf(subscriberSptr);
631 }
632 
SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber & subscriber,const bool isNative)633 ErrCode AnsNotification::SubscribeLocalLiveViewNotification(const NotificationLocalLiveViewSubscriber &subscriber,
634     const bool isNative)
635 {
636     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
637     sptr<IAnsManager> proxy = GetAnsManagerProxy();
638     if (!proxy) {
639         ANS_LOGE("GetAnsManagerProxy fail.");
640         return ERR_ANS_SERVICE_NOT_CONNECTED;
641     }
642 
643     sptr<NotificationLocalLiveViewSubscriber::SubscriberLocalLiveViewImpl> subscriberSptr = subscriber.GetImpl();
644     if (subscriberSptr == nullptr) {
645         ANS_LOGE("null subscriberSptr");
646         return ERR_ANS_INVALID_PARAM;
647     }
648     return proxy->SubscribeLocalLiveView(subscriberSptr, isNative);
649 }
650 
SubscribeNotification(const NotificationSubscriber & subscriber,const NotificationSubscribeInfo & subscribeInfo)651 ErrCode AnsNotification::SubscribeNotification(
652     const NotificationSubscriber &subscriber, const NotificationSubscribeInfo &subscribeInfo)
653 {
654     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
655     sptr<IAnsManager> proxy = GetAnsManagerProxy();
656     if (!proxy) {
657         ANS_LOGE("Failed to GetAnsManagerProxy.");
658         return ERR_ANS_SERVICE_NOT_CONNECTED;
659     }
660 
661     sptr<NotificationSubscribeInfo> sptrInfo = new (std::nothrow) NotificationSubscribeInfo(subscribeInfo);
662     if (sptrInfo == nullptr) {
663         ANS_LOGE("null sptrInfo");
664         return ERR_ANS_NO_MEMORY;
665     }
666 
667     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
668     if (subscriberSptr == nullptr) {
669         ANS_LOGE("null subscriberSptr");
670         return ERR_ANS_INVALID_PARAM;
671     }
672     if (!subscribeInfo.GetDeviceType().empty()) {
673         subscriberSptr->subscriber_.SetDeviceType(subscribeInfo.GetDeviceType());
674     }
675     return proxy->Subscribe(subscriberSptr, sptrInfo);
676 }
677 
UnSubscribeNotification(NotificationSubscriber & subscriber)678 ErrCode AnsNotification::UnSubscribeNotification(NotificationSubscriber &subscriber)
679 {
680     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
681     sptr<IAnsManager> proxy = GetAnsManagerProxy();
682     if (!proxy) {
683         ANS_LOGE("GetAnsManagerProxy fail.");
684         return ERR_ANS_SERVICE_NOT_CONNECTED;
685     }
686 
687     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
688     if (subscriberSptr == nullptr) {
689         ANS_LOGE("null subscriberSptr");
690         return ERR_ANS_INVALID_PARAM;
691     }
692     return proxy->Unsubscribe(subscriberSptr);
693 }
694 
UnSubscribeNotification(NotificationSubscriber & subscriber,NotificationSubscribeInfo subscribeInfo)695 ErrCode AnsNotification::UnSubscribeNotification(
696     NotificationSubscriber &subscriber, NotificationSubscribeInfo subscribeInfo)
697 {
698     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
699     sptr<IAnsManager> proxy = GetAnsManagerProxy();
700     if (!proxy) {
701         ANS_LOGE("GetAnsManagerProxy fail.");
702         return ERR_ANS_SERVICE_NOT_CONNECTED;
703     }
704 
705     sptr<NotificationSubscribeInfo> sptrInfo = new (std::nothrow) NotificationSubscribeInfo(subscribeInfo);
706     if (sptrInfo == nullptr) {
707         ANS_LOGE("null sptrInfo");
708         return ERR_ANS_NO_MEMORY;
709     }
710 
711     sptr<NotificationSubscriber::SubscriberImpl> subscriberSptr = subscriber.GetImpl();
712     if (subscriberSptr == nullptr) {
713         ANS_LOGE("null subscriberSptr");
714         return ERR_ANS_INVALID_PARAM;
715     }
716     return proxy->Unsubscribe(subscriberSptr, sptrInfo);
717 }
718 
SubscribeNotification(const std::shared_ptr<NotificationSubscriber> & subscriber)719 ErrCode AnsNotification::SubscribeNotification(const std::shared_ptr<NotificationSubscriber> &subscriber)
720 {
721     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
722     return SubscribeNotification(subscriber, nullptr);
723 }
724 
SubscribeNotificationSelf(const std::shared_ptr<NotificationSubscriber> & subscriber)725 ErrCode AnsNotification::SubscribeNotificationSelf(const std::shared_ptr<NotificationSubscriber> &subscriber)
726 {
727     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
728     if (subscriber == nullptr) {
729         ANS_LOGE("null subscriber");
730         return ERR_ANS_INVALID_PARAM;
731     }
732 
733     sptr<IAnsManager> proxy = GetAnsManagerProxy();
734     if (!proxy) {
735         ANS_LOGE("GetAnsManagerProxy fail.");
736         return ERR_ANS_SERVICE_NOT_CONNECTED;
737     }
738 
739     sptr<SubscriberListener> listener = nullptr;
740     CreateSubscribeListener(subscriber, listener);
741     if (listener == nullptr) {
742         ANS_LOGE("null listener");
743         return ERR_ANS_NO_MEMORY;
744     }
745     DelayedSingleton<AnsManagerDeathRecipient>::GetInstance()->SubscribeSAManager();
746     return proxy->SubscribeSelf(listener);
747 }
748 
SubscribeNotification(const std::shared_ptr<NotificationSubscriber> & subscriber,const sptr<NotificationSubscribeInfo> & subscribeInfo)749 ErrCode AnsNotification::SubscribeNotification(const std::shared_ptr<NotificationSubscriber> &subscriber,
750     const sptr<NotificationSubscribeInfo> &subscribeInfo)
751 {
752     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
753     if (subscriber == nullptr) {
754         ANS_LOGE("null subscriber");
755         return ERR_ANS_INVALID_PARAM;
756     }
757 
758     sptr<IAnsManager> proxy = GetAnsManagerProxy();
759     if (!proxy) {
760         ANS_LOGE("Failed to GetAnsManagerProxy.");
761         return ERR_ANS_SERVICE_NOT_CONNECTED;
762     }
763 
764     sptr<SubscriberListener> listener = nullptr;
765     CreateSubscribeListener(subscriber, listener);
766     if (listener == nullptr) {
767         ANS_LOGE("null listener");
768         return ERR_ANS_NO_MEMORY;
769     }
770     if (subscribeInfo != nullptr && !subscribeInfo->GetDeviceType().empty()) {
771         subscriber->SetDeviceType(subscribeInfo->GetDeviceType());
772     }
773     DelayedSingleton<AnsManagerDeathRecipient>::GetInstance()->SubscribeSAManager();
774 
775     if (subscribeInfo == nullptr) {
776         return proxy->Subscribe(listener);
777     }
778     return proxy->Subscribe(listener, subscribeInfo);
779 }
780 
UnSubscribeNotification(const std::shared_ptr<NotificationSubscriber> & subscriber)781 ErrCode AnsNotification::UnSubscribeNotification(const std::shared_ptr<NotificationSubscriber> &subscriber)
782 {
783     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
784     return UnSubscribeNotification(subscriber, nullptr);
785 }
786 
UnSubscribeNotification(const std::shared_ptr<NotificationSubscriber> & subscriber,const sptr<NotificationSubscribeInfo> & subscribeInfo)787 ErrCode AnsNotification::UnSubscribeNotification(const std::shared_ptr<NotificationSubscriber> &subscriber,
788     const sptr<NotificationSubscribeInfo> &subscribeInfo)
789 {
790     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
791     if (subscriber == nullptr) {
792         ANS_LOGE("null subscriber");
793         return ERR_ANS_INVALID_PARAM;
794     }
795 
796     sptr<IAnsManager> proxy = GetAnsManagerProxy();
797     if (!proxy) {
798         ANS_LOGE("GetAnsManagerProxy fail.");
799         return ERR_ANS_SERVICE_NOT_CONNECTED;
800     }
801     std::lock_guard<std::mutex> lock(subscriberMutex_);
802     auto item = subscribers_.find(subscriber);
803     if (item != subscribers_.end()) {
804         sptr<SubscriberListener> listener = item->second;
805         int32_t ret = -1;
806         if (subscribeInfo == nullptr) {
807             ret = proxy->Unsubscribe(listener);
808         } else {
809             ret = proxy->Unsubscribe(listener, subscribeInfo);
810         }
811         if (ret == ERR_OK) {
812             subscribers_.erase(item);
813         }
814         return ret;
815     }
816     ANS_LOGE("Failed to unsubscribe due to subscriber not found.");
817     return ERR_ANS_INVALID_PARAM;
818 }
819 
TriggerLocalLiveView(const NotificationBundleOption & bundleOption,const int32_t notificationId,const NotificationButtonOption & buttonOption)820 ErrCode AnsNotification::TriggerLocalLiveView(const NotificationBundleOption &bundleOption,
821     const int32_t notificationId, const NotificationButtonOption &buttonOption)
822 {
823     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
824 
825     if (buttonOption.GetButtonName().empty()) {
826         ANS_LOGE("Invalid button name.");
827         return ERR_ANS_INVALID_PARAM;
828     }
829     ANS_LOGI("notificationId:%{public}u,bundleName:%{public}s,button:%{public}s",
830         notificationId, bundleOption.GetBundleName().c_str(), buttonOption.GetButtonName().c_str());
831 
832     sptr<IAnsManager> proxy = GetAnsManagerProxy();
833     if (!proxy) {
834         ANS_LOGE("Fail to GetAnsManagerProxy.");
835         return ERR_ANS_SERVICE_NOT_CONNECTED;
836     }
837 
838     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
839     sptr<NotificationButtonOption> button(new (std::nothrow) NotificationButtonOption(buttonOption));
840     if (bo == nullptr) {
841         ANS_LOGE("null bundleOption");
842         return ERR_ANS_INVALID_PARAM;
843     }
844     return proxy->TriggerLocalLiveView(bo, notificationId, button);
845 }
846 
RemoveNotification(const std::string & key,int32_t removeReason)847 ErrCode AnsNotification::RemoveNotification(const std::string &key, int32_t removeReason)
848 {
849     ANS_LOGI("key:%{public}s,removeReason:%{public}d", key.c_str(), removeReason);
850     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
851     if (key.empty()) {
852         ANS_LOGW("Input key is empty.");
853         return ERR_ANS_INVALID_PARAM;
854     }
855 
856     sptr<IAnsManager> proxy = GetAnsManagerProxy();
857     if (!proxy) {
858         ANS_LOGE("GetAnsManagerProxy fail.");
859         return ERR_ANS_SERVICE_NOT_CONNECTED;
860     }
861     return proxy->Delete(key, removeReason);
862 }
863 
RemoveNotification(const NotificationBundleOption & bundleOption,const int32_t notificationId,const std::string & label,int32_t removeReason)864 ErrCode AnsNotification::RemoveNotification(const NotificationBundleOption &bundleOption,
865     const int32_t notificationId, const std::string &label, int32_t removeReason)
866 {
867     ANS_LOGI("notificationId:%{public}d,bundle:%{public}s,reason:%{public}d label:%{public}s",
868         notificationId, bundleOption.GetBundleName().c_str(), removeReason, label.c_str());
869     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
870     if (bundleOption.GetBundleName().empty()) {
871         ANS_LOGE("Invalid bundle name.");
872         return ERR_ANS_INVALID_PARAM;
873     }
874 
875     sptr<IAnsManager> proxy = GetAnsManagerProxy();
876     if (!proxy) {
877         ANS_LOGE("Fail to GetAnsManagerProxy.");
878         return ERR_ANS_SERVICE_NOT_CONNECTED;
879     }
880 
881     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
882     if (bo == nullptr) {
883         ANS_LOGE("null bundleOption");
884         return ERR_ANS_INVALID_PARAM;
885     }
886     return proxy->RemoveNotification(bo, notificationId, label, removeReason);
887 }
888 
RemoveAllNotifications(const NotificationBundleOption & bundleOption)889 ErrCode AnsNotification::RemoveAllNotifications(const NotificationBundleOption &bundleOption)
890 {
891     ANS_LOGI("bundleName:%{public}s", bundleOption.GetBundleName().c_str());
892     if (bundleOption.GetBundleName().empty()) {
893         ANS_LOGE("Invalid bundle name.");
894         return ERR_ANS_INVALID_PARAM;
895     }
896 
897     sptr<IAnsManager> proxy = GetAnsManagerProxy();
898     if (!proxy) {
899         ANS_LOGE("GetAnsManagerProxy defeat.");
900         return ERR_ANS_SERVICE_NOT_CONNECTED;
901     }
902 
903     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
904     if (bo == nullptr) {
905         ANS_LOGE("null bundleOption");
906         return ERR_ANS_INVALID_PARAM;
907     }
908     return proxy->RemoveAllNotifications(bo);
909 }
910 
RemoveNotifications(const std::vector<std::string> hashcodes,int32_t removeReason)911 ErrCode AnsNotification::RemoveNotifications(const std::vector<std::string> hashcodes, int32_t removeReason)
912 {
913     ANS_LOGI("removeReason:%{public}d", removeReason);
914     if (hashcodes.empty()) {
915         ANS_LOGE("Hashcodes is empty");
916         return ERR_ANS_INVALID_PARAM;
917     }
918 
919     sptr<IAnsManager> proxy = GetAnsManagerProxy();
920     if (!proxy) {
921         ANS_LOGE("GetAnsManagerProxy fail.");
922         return ERR_ANS_SERVICE_NOT_CONNECTED;
923     }
924 
925     return proxy->RemoveNotifications(hashcodes, removeReason);
926 }
927 
RemoveDistributedNotifications(const std::vector<std::string> & hashcodes,const NotificationConstant::SlotType & slotType,const NotificationConstant::DistributedDeleteType & deleteType,const int32_t removeReason,const std::string & deviceId)928 ErrCode AnsNotification::RemoveDistributedNotifications(const std::vector<std::string>& hashcodes,
929     const NotificationConstant::SlotType& slotType,
930     const NotificationConstant::DistributedDeleteType& deleteType,
931     const int32_t removeReason, const std::string& deviceId)
932 {
933     sptr<IAnsManager> proxy = GetAnsManagerProxy();
934     if (!proxy) {
935         ANS_LOGE("GetAnsManagerProxy fail.");
936         return ERR_ANS_SERVICE_NOT_CONNECTED;
937     }
938 
939     return proxy->RemoveDistributedNotifications(hashcodes, slotType, deleteType,
940         removeReason, deviceId);
941 }
942 
RemoveNotificationsByBundle(const NotificationBundleOption & bundleOption)943 ErrCode AnsNotification::RemoveNotificationsByBundle(const NotificationBundleOption &bundleOption)
944 {
945     ANS_LOGI("bundleName:%{public}s", bundleOption.GetBundleName().c_str());
946     if (bundleOption.GetBundleName().empty()) {
947         ANS_LOGE("Invalid bundle name.");
948         return ERR_ANS_INVALID_PARAM;
949     }
950 
951     sptr<IAnsManager> proxy = GetAnsManagerProxy();
952     if (!proxy) {
953         ANS_LOGE("Defeated to GetAnsManagerProxy.");
954         return ERR_ANS_SERVICE_NOT_CONNECTED;
955     }
956 
957     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
958     if (bo == nullptr) {
959         ANS_LOGE("null bundleOption");
960         return ERR_ANS_INVALID_PARAM;
961     }
962     return proxy->DeleteByBundle(bo);
963 }
964 
RemoveNotifications()965 ErrCode AnsNotification::RemoveNotifications()
966 {
967     sptr<IAnsManager> proxy = GetAnsManagerProxy();
968     if (!proxy) {
969         ANS_LOGE("GetAnsManagerProxy fail.");
970         return ERR_ANS_SERVICE_NOT_CONNECTED;
971     }
972     return proxy->DeleteAll();
973 }
974 
GetNotificationSlotsForBundle(const NotificationBundleOption & bundleOption,std::vector<sptr<NotificationSlot>> & slots)975 ErrCode AnsNotification::GetNotificationSlotsForBundle(
976     const NotificationBundleOption &bundleOption, std::vector<sptr<NotificationSlot>> &slots)
977 {
978     if (bundleOption.GetBundleName().empty()) {
979         ANS_LOGE("Input bundleName is empty.");
980         return ERR_ANS_INVALID_PARAM;
981     }
982 
983     sptr<IAnsManager> proxy = GetAnsManagerProxy();
984     if (!proxy) {
985         ANS_LOGE("GetAnsManagerProxy fail.");
986         return ERR_ANS_SERVICE_NOT_CONNECTED;
987     }
988 
989     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
990     if (bo == nullptr) {
991         ANS_LOGE("null bundleOption");
992         return ERR_ANS_INVALID_PARAM;
993     }
994     return proxy->GetSlotsByBundle(bo, slots);
995 }
996 
GetNotificationSlotForBundle(const NotificationBundleOption & bundleOption,const NotificationConstant::SlotType & slotType,sptr<NotificationSlot> & slot)997 ErrCode AnsNotification::GetNotificationSlotForBundle(
998     const NotificationBundleOption &bundleOption, const NotificationConstant::SlotType &slotType,
999     sptr<NotificationSlot> &slot)
1000 {
1001     if (bundleOption.GetBundleName().empty()) {
1002         ANS_LOGE("Input bundleName is empty.");
1003         return ERR_ANS_INVALID_PARAM;
1004     }
1005 
1006     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1007     if (!proxy) {
1008         ANS_LOGE("GetAnsManagerProxy fail.");
1009         return ERR_ANS_SERVICE_NOT_CONNECTED;
1010     }
1011 
1012     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1013     if (bo == nullptr) {
1014         ANS_LOGE("null bundleOption");
1015         return ERR_ANS_INVALID_PARAM;
1016     }
1017     return proxy->GetSlotByBundle(bo, slotType, slot);
1018 }
1019 
UpdateNotificationSlots(const NotificationBundleOption & bundleOption,const std::vector<sptr<NotificationSlot>> & slots)1020 ErrCode AnsNotification::UpdateNotificationSlots(
1021     const NotificationBundleOption &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
1022 {
1023     if (bundleOption.GetBundleName().empty()) {
1024         ANS_LOGE("Invalid bundle name.");
1025         return ERR_ANS_INVALID_PARAM;
1026     }
1027 
1028     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1029     if (!proxy) {
1030         ANS_LOGE("GetAnsManagerProxy flop.");
1031         return ERR_ANS_SERVICE_NOT_CONNECTED;
1032     }
1033 
1034     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1035 
1036     if (bo == nullptr) {
1037         ANS_LOGE("null bundleOption");
1038         return ERR_ANS_INVALID_PARAM;
1039     }
1040 
1041     if (slots.empty()) {
1042         ANS_LOGE("empty slots");
1043         return ERR_ANS_INVALID_PARAM;
1044     }
1045 
1046     size_t slotSize = slots.size();
1047     if (slotSize > MAX_SLOT_NUM) {
1048         ANS_LOGE("slotSize over max size");
1049         return ERR_ANS_INVALID_PARAM;
1050     }
1051 
1052     return proxy->UpdateSlots(bo, slots);
1053 }
1054 
GetAllActiveNotifications(std::vector<sptr<Notification>> & notification)1055 ErrCode AnsNotification::GetAllActiveNotifications(std::vector<sptr<Notification>> &notification)
1056 {
1057     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1058     if (!proxy) {
1059         ANS_LOGE("GetAnsManagerProxy fail.");
1060         return ERR_ANS_SERVICE_NOT_CONNECTED;
1061     }
1062     return proxy->GetAllActiveNotifications(notification);
1063 }
1064 
GetAllActiveNotifications(const std::vector<std::string> key,std::vector<sptr<Notification>> & notification)1065 ErrCode AnsNotification::GetAllActiveNotifications(
1066     const std::vector<std::string> key, std::vector<sptr<Notification>> &notification)
1067 {
1068     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1069     if (!proxy) {
1070         ANS_LOGE("GetAnsManagerProxy fail.");
1071         return ERR_ANS_SERVICE_NOT_CONNECTED;
1072     }
1073     if (key.empty()) {
1074         ANS_LOGE("empty key");
1075         return ERR_ANS_INVALID_PARAM;
1076     }
1077     return proxy->GetSpecialActiveNotifications(key, notification);
1078 }
1079 
GetActiveNotificationByFilter(const LiveViewFilter & filter,sptr<NotificationRequest> & request)1080 ErrCode AnsNotification::GetActiveNotificationByFilter(const LiveViewFilter &filter,
1081     sptr<NotificationRequest> &request)
1082 {
1083     if (filter.bundle.GetBundleName().empty()) {
1084         ANS_LOGE("Invalid bundle name.");
1085         return ERR_ANS_INVALID_PARAM;
1086     }
1087 
1088     ANS_LOGD("Bundle name %{public}s, uid %{public}d, notification id %{public}d, label %{public}s.",
1089         filter.bundle.GetBundleName().c_str(), filter.bundle.GetUid(), filter.notificationKey.id,
1090         filter.notificationKey.label.c_str());
1091 
1092     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1093     if (!proxy) {
1094         ANS_LOGE("GetAnsManagerProxy fail.");
1095         return ERR_ANS_SERVICE_NOT_CONNECTED;
1096     }
1097 
1098     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(filter.bundle));
1099     if (bo == nullptr) {
1100         ANS_LOGE("null bundleOption");
1101         return ERR_ANS_INVALID_PARAM;
1102     }
1103     return proxy->GetActiveNotificationByFilter(bo, filter.notificationKey.id,
1104         filter.notificationKey.label, filter.userId, filter.extraInfoKeys, request);
1105 }
1106 
IsAllowedNotify(const NotificationBundleOption & bundleOption,bool & allowed)1107 ErrCode AnsNotification::IsAllowedNotify(const NotificationBundleOption &bundleOption, bool &allowed)
1108 {
1109     if (bundleOption.GetBundleName().empty()) {
1110         ANS_LOGE("Input bundle is empty.");
1111         return ERR_ANS_INVALID_PARAM;
1112     }
1113 
1114     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1115     if (!proxy) {
1116         ANS_LOGE("GetAnsManagerProxy fail.");
1117         return ERR_ANS_SERVICE_NOT_CONNECTED;
1118     }
1119 
1120     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1121     if (bo == nullptr) {
1122         ANS_LOGE("null bundleOption");
1123         return ERR_ANS_INVALID_PARAM;
1124     }
1125     return proxy->IsSpecialBundleAllowedNotify(bo, allowed);
1126 }
1127 
SetNotificationsEnabledForAllBundles(const std::string & deviceId,bool enabled)1128 ErrCode AnsNotification::SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled)
1129 {
1130     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1131     if (!proxy) {
1132         ANS_LOGE("GetAnsManagerProxy fail.");
1133         return ERR_ANS_SERVICE_NOT_CONNECTED;
1134     }
1135     return proxy->SetNotificationsEnabledForAllBundles(deviceId, enabled);
1136 }
1137 
SetNotificationsEnabledForDefaultBundle(const std::string & deviceId,bool enabled)1138 ErrCode AnsNotification::SetNotificationsEnabledForDefaultBundle(const std::string &deviceId, bool enabled)
1139 {
1140     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1141     if (!proxy) {
1142         ANS_LOGE("GetAnsManagerProxy fail.");
1143         return ERR_ANS_SERVICE_NOT_CONNECTED;
1144     }
1145     return proxy->SetNotificationsEnabledForBundle(deviceId, enabled);
1146 }
1147 
SetNotificationsEnabledForSpecifiedBundle(const NotificationBundleOption & bundleOption,const std::string & deviceId,bool enabled)1148 ErrCode AnsNotification::SetNotificationsEnabledForSpecifiedBundle(
1149     const NotificationBundleOption &bundleOption, const std::string &deviceId, bool enabled)
1150 {
1151     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
1152     if (bundleOption.GetBundleName().empty()) {
1153         ANS_LOGE("Invalid bundle name.");
1154         return ERR_ANS_INVALID_PARAM;
1155     }
1156 
1157     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1158     if (!proxy) {
1159         ANS_LOGE("GetAnsManagerProxy fail.");
1160         return ERR_ANS_SERVICE_NOT_CONNECTED;
1161     }
1162 
1163     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1164     if (bo == nullptr) {
1165         ANS_LOGE("null bundleOption");
1166         return ERR_ANS_INVALID_PARAM;
1167     }
1168     return proxy->SetNotificationsEnabledForSpecialBundle(deviceId, bo, enabled, true);
1169 }
1170 
SetShowBadgeEnabledForBundle(const NotificationBundleOption & bundleOption,bool enabled)1171 ErrCode AnsNotification::SetShowBadgeEnabledForBundle(const NotificationBundleOption &bundleOption, bool enabled)
1172 {
1173     if (bundleOption.GetBundleName().empty()) {
1174         ANS_LOGE("Invalidated bundle name.");
1175         return ERR_ANS_INVALID_PARAM;
1176     }
1177 
1178     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1179     if (!proxy) {
1180         ANS_LOGE("GetAnsManagerProxy fail.");
1181         return ERR_ANS_SERVICE_NOT_CONNECTED;
1182     }
1183 
1184     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1185     if (bo == nullptr) {
1186         ANS_LOGE("null bundleOption");
1187         return ERR_ANS_INVALID_PARAM;
1188     }
1189     return proxy->SetShowBadgeEnabledForBundle(bo, enabled);
1190 }
1191 
GetShowBadgeEnabledForBundle(const NotificationBundleOption & bundleOption,bool & enabled)1192 ErrCode AnsNotification::GetShowBadgeEnabledForBundle(const NotificationBundleOption &bundleOption, bool &enabled)
1193 {
1194     if (bundleOption.GetBundleName().empty()) {
1195         ANS_LOGE("Invalid bundle name.");
1196         return ERR_ANS_INVALID_PARAM;
1197     }
1198 
1199     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1200     if (!proxy) {
1201         ANS_LOGE("GetAnsManagerProxy fail.");
1202         return ERR_ANS_SERVICE_NOT_CONNECTED;
1203     }
1204 
1205     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1206     if (bo == nullptr) {
1207         ANS_LOGE("null bundleOption");
1208         return ERR_ANS_INVALID_PARAM;
1209     }
1210     return proxy->GetShowBadgeEnabledForBundle(bo, enabled);
1211 }
1212 
GetShowBadgeEnabled(bool & enabled)1213 ErrCode AnsNotification::GetShowBadgeEnabled(bool &enabled)
1214 {
1215     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1216     if (!proxy) {
1217         ANS_LOGE("GetAnsManagerProxy fail.");
1218         return ERR_ANS_SERVICE_NOT_CONNECTED;
1219     }
1220 
1221     return proxy->GetShowBadgeEnabled(enabled);
1222 }
1223 
CancelGroup(const std::string & groupName,const std::string & instanceKey)1224 ErrCode AnsNotification::CancelGroup(const std::string &groupName, const std::string &instanceKey)
1225 {
1226     ANS_LOGI("groupName:%{public}s", groupName.c_str());
1227     if (groupName.empty()) {
1228         ANS_LOGE("Invalid group name.");
1229         return ERR_ANS_INVALID_PARAM;
1230     }
1231 
1232     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1233     if (!proxy) {
1234         ANS_LOGE("GetAnsManagerProxy fail.");
1235         return ERR_ANS_SERVICE_NOT_CONNECTED;
1236     }
1237     return proxy->CancelGroup(groupName, instanceKey);
1238 }
1239 
RemoveGroupByBundle(const NotificationBundleOption & bundleOption,const std::string & groupName)1240 ErrCode AnsNotification::RemoveGroupByBundle(
1241     const NotificationBundleOption &bundleOption, const std::string &groupName)
1242 {
1243     ANS_LOGI("bundleName:%{public}s", bundleOption.GetBundleName().c_str());
1244     if (bundleOption.GetBundleName().empty() || groupName.empty()) {
1245         ANS_LOGE("Invalid parameter.");
1246         return ERR_ANS_INVALID_PARAM;
1247     }
1248 
1249     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1250     if (!proxy) {
1251         ANS_LOGE("GetAnsManagerProxy fail.");
1252         return ERR_ANS_SERVICE_NOT_CONNECTED;
1253     }
1254 
1255     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1256     return proxy->RemoveGroupByBundle(bo, groupName);
1257 }
1258 
SetDoNotDisturbDate(const NotificationDoNotDisturbDate & doNotDisturbDate)1259 ErrCode AnsNotification::SetDoNotDisturbDate(const NotificationDoNotDisturbDate &doNotDisturbDate)
1260 {
1261     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1262     if (!proxy) {
1263         ANS_LOGE("GetAnsManagerProxy fail.");
1264         return ERR_ANS_SERVICE_NOT_CONNECTED;
1265     }
1266 
1267     auto dndDatePtr = new (std::nothrow) NotificationDoNotDisturbDate(doNotDisturbDate);
1268     if (dndDatePtr == nullptr) {
1269         ANS_LOGE("null dndDatePtr");
1270         return ERR_ANS_NO_MEMORY;
1271     }
1272 
1273     sptr<NotificationDoNotDisturbDate> dndDate(dndDatePtr);
1274     if (dndDate == nullptr) {
1275         ANS_LOGE("null dndDate");
1276         return ERR_ANS_INVALID_PARAM;
1277     }
1278     return proxy->SetDoNotDisturbDate(dndDate);
1279 }
1280 
GetDoNotDisturbDate(NotificationDoNotDisturbDate & doNotDisturbDate)1281 ErrCode AnsNotification::GetDoNotDisturbDate(NotificationDoNotDisturbDate &doNotDisturbDate)
1282 {
1283     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1284     if (!proxy) {
1285         ANS_LOGE("GetAnsManagerProxy fail.");
1286         return ERR_ANS_SERVICE_NOT_CONNECTED;
1287     }
1288 
1289     sptr<NotificationDoNotDisturbDate> dndDate = nullptr;
1290     auto ret = proxy->GetDoNotDisturbDate(dndDate);
1291     if (ret != ERR_OK) {
1292         ANS_LOGE("GetDoNotDisturbDate failed.");
1293         return ret;
1294     }
1295 
1296     if (!dndDate) {
1297         ANS_LOGE("Invalid DoNotDisturbDate.");
1298         return ERR_ANS_NO_MEMORY;
1299     }
1300 
1301     doNotDisturbDate = *dndDate;
1302     return ret;
1303 }
1304 
AddDoNotDisturbProfiles(const std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)1305 ErrCode AnsNotification::AddDoNotDisturbProfiles(const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
1306 {
1307     if (profiles.empty()) {
1308         ANS_LOGW("The profiles is empty.");
1309         return ERR_ANS_INVALID_PARAM;
1310     }
1311     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1312     if (!proxy) {
1313         ANS_LOGW("Get ans manager proxy fail.");
1314         return ERR_ANS_SERVICE_NOT_CONNECTED;
1315     }
1316     if (profiles.empty()) {
1317         ANS_LOGW("The profiles is empty.");
1318         return ERR_ANS_INVALID_PARAM;
1319     }
1320     if (profiles.size() > MAX_STATUS_VECTOR_NUM) {
1321         ANS_LOGE("The profiles is exceeds limit.");
1322         return ERR_ANS_INVALID_PARAM;
1323     }
1324     return proxy->AddDoNotDisturbProfiles(profiles);
1325 }
1326 
RemoveDoNotDisturbProfiles(const std::vector<sptr<NotificationDoNotDisturbProfile>> & profiles)1327 ErrCode AnsNotification::RemoveDoNotDisturbProfiles(const std::vector<sptr<NotificationDoNotDisturbProfile>> &profiles)
1328 {
1329     if (profiles.empty()) {
1330         ANS_LOGW("The profiles is empty.");
1331         return ERR_ANS_INVALID_PARAM;
1332     }
1333     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1334     if (!proxy) {
1335         ANS_LOGW("Get ans manager proxy fail.");
1336         return ERR_ANS_SERVICE_NOT_CONNECTED;
1337     }
1338     if (profiles.size() > MAX_STATUS_VECTOR_NUM) {
1339         ANS_LOGE("The profiles is exceeds limit.");
1340         return ERR_ANS_INVALID_PARAM;
1341     }
1342     return proxy->RemoveDoNotDisturbProfiles(profiles);
1343 }
1344 
DoesSupportDoNotDisturbMode(bool & doesSupport)1345 ErrCode AnsNotification::DoesSupportDoNotDisturbMode(bool &doesSupport)
1346 {
1347     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1348     if (!proxy) {
1349         ANS_LOGE("GetAnsManagerProxy fail.");
1350         return ERR_ANS_SERVICE_NOT_CONNECTED;
1351     }
1352 
1353     return proxy->DoesSupportDoNotDisturbMode(doesSupport);
1354 }
1355 
IsNeedSilentInDoNotDisturbMode(const std::string & phoneNumber,int32_t callerType)1356 ErrCode AnsNotification::IsNeedSilentInDoNotDisturbMode(const std::string &phoneNumber, int32_t callerType)
1357 {
1358     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1359     if (!proxy) {
1360         ANS_LOGE("GetAnsManagerProxy fail.");
1361         return ERR_ANS_SERVICE_NOT_CONNECTED;
1362     }
1363 
1364     return proxy->IsNeedSilentInDoNotDisturbMode(phoneNumber, callerType);
1365 }
1366 
PublishContinuousTaskNotification(const NotificationRequest & request)1367 ErrCode AnsNotification::PublishContinuousTaskNotification(const NotificationRequest &request)
1368 {
1369     if (request.GetContent() == nullptr || request.GetNotificationType() == NotificationContent::Type::NONE) {
1370         ANS_LOGE("Refuse to publish the notification without valid content");
1371         return ERR_ANS_INVALID_PARAM;
1372     }
1373 
1374     if (!CanPublishMediaContent(request)) {
1375         ANS_LOGE("Refuse to publish the notification because the sequence numbers actions not match those assigned to "
1376                  "added action buttons.");
1377         return ERR_ANS_INVALID_PARAM;
1378     }
1379 
1380     ErrCode checkErr = CheckImageSize(request);
1381     if (checkErr != ERR_OK) {
1382         ANS_LOGE("The size of one picture exceeds the limit");
1383         return checkErr;
1384     }
1385 
1386     if (!CanPublishLiveViewContent(request)) {
1387         ANS_LOGE("Refuse to publish the notification without valid live view content.");
1388         return ERR_ANS_INVALID_PARAM;
1389     }
1390 
1391     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1392     if (!proxy) {
1393         ANS_LOGE("GetAnsManagerProxy fail.");
1394         return ERR_ANS_SERVICE_NOT_CONNECTED;
1395     }
1396 
1397     auto pReq = new (std::nothrow) NotificationRequest(request);
1398     if (pReq == nullptr) {
1399         ANS_LOGE("null pReq");
1400         return ERR_ANS_NO_MEMORY;
1401     }
1402 
1403     sptr<NotificationRequest> sptrReq(pReq);
1404     if (IsNonDistributedNotificationType(sptrReq->GetNotificationType())) {
1405         sptrReq->SetDistributed(false);
1406     }
1407     if (sptrReq == nullptr) {
1408         ANS_LOGE("null sptrReq");
1409         return ERR_ANS_INVALID_PARAM;
1410     }
1411     return proxy->PublishContinuousTaskNotification(sptrReq);
1412 }
1413 
CancelContinuousTaskNotification(const std::string & label,int32_t notificationId)1414 ErrCode AnsNotification::CancelContinuousTaskNotification(const std::string &label, int32_t notificationId)
1415 {
1416     ANS_LOGI("notificationId:%{public}d", notificationId);
1417     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1418     if (!proxy) {
1419         ANS_LOGE("GetAnsManagerProxy fail.");
1420         return ERR_ANS_SERVICE_NOT_CONNECTED;
1421     }
1422 
1423     return proxy->CancelContinuousTaskNotification(label, notificationId);
1424 }
1425 
IsDistributedEnabled(bool & enabled)1426 ErrCode AnsNotification::IsDistributedEnabled(bool &enabled)
1427 {
1428     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1429     if (!proxy) {
1430         ANS_LOGE("GetAnsManagerProxy fail.");
1431         return ERR_ANS_SERVICE_NOT_CONNECTED;
1432     }
1433 
1434     return proxy->IsDistributedEnabled(enabled);
1435 }
1436 
EnableDistributed(const bool enabled)1437 ErrCode AnsNotification::EnableDistributed(const bool enabled)
1438 {
1439     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1440     if (!proxy) {
1441         ANS_LOGE("GetAnsManagerProxy fail.");
1442         return ERR_ANS_SERVICE_NOT_CONNECTED;
1443     }
1444 
1445     return proxy->EnableDistributed(enabled);
1446 }
1447 
EnableDistributedByBundle(const NotificationBundleOption & bundleOption,const bool enabled)1448 ErrCode AnsNotification::EnableDistributedByBundle(const NotificationBundleOption &bundleOption, const bool enabled)
1449 {
1450     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1451     if (!proxy) {
1452         ANS_LOGE("GetAnsManagerProxy fail.");
1453         return ERR_ANS_SERVICE_NOT_CONNECTED;
1454     }
1455 
1456     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1457     if (bo == nullptr) {
1458         ANS_LOGE("null bundleOption");
1459         return ERR_ANS_INVALID_PARAM;
1460     }
1461     return proxy->EnableDistributedByBundle(bo, enabled);
1462 }
1463 
EnableDistributedSelf(const bool enabled)1464 ErrCode AnsNotification::EnableDistributedSelf(const bool enabled)
1465 {
1466     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1467     if (!proxy) {
1468         ANS_LOGE("GetAnsManagerProxy fail.");
1469         return ERR_ANS_SERVICE_NOT_CONNECTED;
1470     }
1471 
1472     return proxy->EnableDistributedSelf(enabled);
1473 }
1474 
IsDistributedEnableByBundle(const NotificationBundleOption & bundleOption,bool & enabled)1475 ErrCode AnsNotification::IsDistributedEnableByBundle(const NotificationBundleOption &bundleOption, bool &enabled)
1476 {
1477     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1478     if (!proxy) {
1479         ANS_LOGE("GetAnsManagerProxy fail.");
1480         return ERR_ANS_SERVICE_NOT_CONNECTED;
1481     }
1482 
1483     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1484     if (bo == nullptr) {
1485         ANS_LOGE("null bundleOption");
1486         return ERR_ANS_INVALID_PARAM;
1487     }
1488     return proxy->IsDistributedEnableByBundle(bo, enabled);
1489 }
1490 
GetDeviceRemindType(NotificationConstant::RemindType & remindType)1491 ErrCode AnsNotification::GetDeviceRemindType(NotificationConstant::RemindType &remindType)
1492 {
1493     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1494     if (!proxy) {
1495         ANS_LOGE("GetAnsManagerProxy fail.");
1496         return ERR_ANS_SERVICE_NOT_CONNECTED;
1497     }
1498     int32_t remindTypeTemp = -1;
1499     auto ret = proxy->GetDeviceRemindType(remindTypeTemp);
1500     remindType = static_cast<NotificationConstant::RemindType>(remindTypeTemp);
1501     return ret;
1502 }
1503 
ResetAnsManagerProxy()1504 void AnsNotification::ResetAnsManagerProxy()
1505 {}
1506 
Reconnect()1507 void AnsNotification::Reconnect()
1508 {
1509     ANS_LOGD("called");
1510     for (int32_t i = 0; i < MAX_RETRY_TIME; i++) {
1511         // try to connect ans
1512         sptr<IAnsManager> proxy = GetAnsManagerProxy();
1513     if (!proxy) {
1514             // Sleep 1000 milliseconds before reconnect.
1515             std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
1516             ANS_LOGE("get ans proxy fail, try again.");
1517             continue;
1518         }
1519 
1520         ANS_LOGD("get ans proxy success.");
1521         return;
1522     }
1523 }
1524 
GetAnsManagerProxy()1525 sptr<IAnsManager> AnsNotification::GetAnsManagerProxy()
1526 {
1527     sptr<ISystemAbilityManager> systemAbilityManager =
1528         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1529     if (!systemAbilityManager) {
1530         ANS_LOGE("Failed to get system ability mgr.");
1531         return nullptr;
1532     }
1533 
1534     sptr<IRemoteObject> remoteObject =
1535         systemAbilityManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID);
1536     if (!remoteObject) {
1537         ANS_LOGE("Failed to get notification Manager.");
1538         return nullptr;
1539     }
1540 
1541     sptr<IAnsManager> proxy = iface_cast<IAnsManager>(remoteObject);
1542     if ((!proxy) || (!proxy->AsObject())) {
1543         ANS_LOGE("Failed to get notification Manager's proxy");
1544         return nullptr;
1545     }
1546     return proxy;
1547 }
1548 
CanPublishMediaContent(const NotificationRequest & request) const1549 bool AnsNotification::CanPublishMediaContent(const NotificationRequest &request) const
1550 {
1551     if (NotificationContent::Type::MEDIA != request.GetNotificationType()) {
1552         return true;
1553     }
1554 
1555     if (request.GetContent() == nullptr) {
1556         ANS_LOGE("Failed to publish notification with null content.");
1557         return false;
1558     }
1559 
1560     auto media = std::static_pointer_cast<NotificationMediaContent>(request.GetContent()->GetNotificationContent());
1561     if (media == nullptr) {
1562         ANS_LOGE("null media");
1563         return false;
1564     }
1565 
1566     auto showActions = media->GetShownActions();
1567     size_t size = request.GetActionButtons().size();
1568     for (auto it = showActions.begin(); it != showActions.end(); ++it) {
1569         if (*it > size) {
1570             ANS_LOGE("The sequence numbers actions is: %{public}d, the assigned to added action buttons size is: "
1571                      "%{public}zu.", *it, size);
1572             return false;
1573         }
1574     }
1575 
1576     return true;
1577 }
1578 
CanPublishLiveViewContent(const NotificationRequest & request) const1579 bool AnsNotification::CanPublishLiveViewContent(const NotificationRequest &request) const
1580 {
1581     if (!request.IsCommonLiveView()) {
1582         return true;
1583     }
1584 
1585     if (request.GetContent() == nullptr) {
1586         ANS_LOGE("Failed to publish notification with null content.");
1587         return false;
1588     }
1589 
1590     auto content = request.GetContent()->GetNotificationContent();
1591     auto liveView = std::static_pointer_cast<NotificationLiveViewContent>(content);
1592     if (liveView == nullptr) {
1593         ANS_LOGE("null liveView");
1594         return false;
1595     }
1596 
1597     auto status = liveView->GetLiveViewStatus();
1598     if (status >= NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT) {
1599         ANS_LOGE("Invalid status %{public}u.", status);
1600         return false;
1601     }
1602 
1603     return true;
1604 }
1605 
CheckImageSize(const NotificationRequest & request)1606 ErrCode AnsNotification::CheckImageSize(const NotificationRequest &request)
1607 {
1608     auto littleIcon = request.GetLittleIcon();
1609     bool collaborateFlag = request.GetDistributedCollaborate();
1610     if (!collaborateFlag && NotificationRequest::CheckImageOverSizeForPixelMap(littleIcon, MAX_ICON_SIZE)) {
1611         ANS_LOGE("The size of little icon exceeds limit");
1612         return ERR_ANS_ICON_OVER_SIZE;
1613     }
1614 
1615     auto overlayIcon = request.GetOverlayIcon();
1616     if (overlayIcon && NotificationRequest::CheckImageOverSizeForPixelMap(overlayIcon, MAX_ICON_SIZE)) {
1617         ANS_LOGE("The size of overlay icon exceeds limit");
1618         return ERR_ANS_ICON_OVER_SIZE;
1619     }
1620 
1621     ErrCode err = request.CheckImageSizeForContent(collaborateFlag);
1622     if (err != ERR_OK) {
1623         return err;
1624     }
1625 
1626     auto buttons = request.GetActionButtons();
1627     for (auto &btn : buttons) {
1628         if (!btn) {
1629             continue;
1630         }
1631         auto icon = btn->GetIcon();
1632         if (NotificationRequest::CheckImageOverSizeForPixelMap(icon, MAX_ICON_SIZE)) {
1633             ANS_LOGE("The size of icon in ActionButton exceeds limit");
1634             return ERR_ANS_ICON_OVER_SIZE;
1635         }
1636     }
1637 
1638     auto users = request.GetMessageUsers();
1639     for (auto &user : users) {
1640         if (!user) {
1641             continue;
1642         }
1643         auto icon = user->GetPixelMap();
1644         if (NotificationRequest::CheckImageOverSizeForPixelMap(icon, MAX_ICON_SIZE)) {
1645             ANS_LOGE("The size of picture in MessageUser exceeds limit");
1646             return ERR_ANS_ICON_OVER_SIZE;
1647         }
1648     }
1649 
1650     auto bigIcon = request.GetBigIcon();
1651     if (NotificationRequest::CheckImageOverSizeForPixelMap(bigIcon, MAX_ICON_SIZE)) {
1652         request.ResetBigIcon();
1653         ANS_LOGW("The size of big icon exceeds limit");
1654     }
1655 
1656     return ERR_OK;
1657 }
1658 
IsSupportTemplate(const std::string & templateName,bool & support)1659 ErrCode AnsNotification::IsSupportTemplate(const std::string &templateName, bool &support)
1660 {
1661     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1662     if (!proxy) {
1663         ANS_LOGE("GetAnsManagerProxy fail.");
1664         return ERR_ANS_SERVICE_NOT_CONNECTED;
1665     }
1666 
1667     return proxy->IsSupportTemplate(templateName, support);
1668 }
1669 
IsNonDistributedNotificationType(const NotificationContent::Type & type)1670 bool AnsNotification::IsNonDistributedNotificationType(const NotificationContent::Type &type)
1671 {
1672     return ((type == NotificationContent::Type::CONVERSATION) ||
1673         (type == NotificationContent::Type::PICTURE) ||
1674         (type == NotificationContent::Type::LIVE_VIEW));
1675 }
1676 
IsAllowedNotify(const int32_t & userId,bool & allowed)1677 ErrCode AnsNotification::IsAllowedNotify(const int32_t &userId, bool &allowed)
1678 {
1679     if (userId <= SUBSCRIBE_USER_INIT) {
1680         ANS_LOGE("Input userId is invalid.");
1681         return ERR_ANS_INVALID_PARAM;
1682     }
1683 
1684     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1685     if (!proxy) {
1686         ANS_LOGE("GetAnsManagerProxy fail.");
1687         return ERR_ANS_SERVICE_NOT_CONNECTED;
1688     }
1689 
1690     return proxy->IsSpecialUserAllowedNotify(userId, allowed);
1691 }
1692 
SetNotificationsEnabledForAllBundles(const int32_t & userId,bool enabled)1693 ErrCode AnsNotification::SetNotificationsEnabledForAllBundles(const int32_t &userId, bool enabled)
1694 {
1695     if (userId <= SUBSCRIBE_USER_INIT) {
1696         ANS_LOGE("Input userId is invalid.");
1697         return ERR_ANS_INVALID_PARAM;
1698     }
1699 
1700     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1701     if (!proxy) {
1702         ANS_LOGE("GetAnsManagerProxy fail.");
1703         return ERR_ANS_SERVICE_NOT_CONNECTED;
1704     }
1705     return proxy->SetNotificationsEnabledByUser(userId, enabled);
1706 }
1707 
RemoveNotifications(const int32_t & userId)1708 ErrCode AnsNotification::RemoveNotifications(const int32_t &userId)
1709 {
1710     if (userId <= SUBSCRIBE_USER_INIT) {
1711         ANS_LOGE("Input userId is invalid.");
1712         return ERR_ANS_INVALID_PARAM;
1713     }
1714 
1715     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1716     if (!proxy) {
1717         ANS_LOGE("GetAnsManagerProxy fail.");
1718         return ERR_ANS_SERVICE_NOT_CONNECTED;
1719     }
1720 
1721     return proxy->DeleteAllByUser(userId);
1722 }
1723 
SetDoNotDisturbDate(const int32_t & userId,const NotificationDoNotDisturbDate & doNotDisturbDate)1724 ErrCode AnsNotification::SetDoNotDisturbDate(const int32_t &userId,
1725     const NotificationDoNotDisturbDate &doNotDisturbDate)
1726 {
1727     if (userId <= SUBSCRIBE_USER_INIT) {
1728         ANS_LOGE("Input userId is invalid.");
1729         return ERR_ANS_INVALID_PARAM;
1730     }
1731 
1732     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1733     if (!proxy) {
1734         ANS_LOGE("GetAnsManagerProxy fail.");
1735         return ERR_ANS_SERVICE_NOT_CONNECTED;
1736     }
1737 
1738     auto dndDatePtr = new (std::nothrow) NotificationDoNotDisturbDate(doNotDisturbDate);
1739     if (dndDatePtr == nullptr) {
1740         ANS_LOGE("null dndDatePtr");
1741         return ERR_ANS_NO_MEMORY;
1742     }
1743 
1744     sptr<NotificationDoNotDisturbDate> dndDate(dndDatePtr);
1745     if (dndDate == nullptr) {
1746         ANS_LOGE("null dndDate");
1747         return ERR_ANS_INVALID_PARAM;
1748     }
1749     return proxy->SetDoNotDisturbDate(dndDate);
1750 }
1751 
GetDoNotDisturbDate(const int32_t & userId,NotificationDoNotDisturbDate & doNotDisturbDate)1752 ErrCode AnsNotification::GetDoNotDisturbDate(const int32_t &userId, NotificationDoNotDisturbDate &doNotDisturbDate)
1753 {
1754     if (userId <= SUBSCRIBE_USER_INIT) {
1755         ANS_LOGE("Input userId is invalid.");
1756         return ERR_ANS_INVALID_PARAM;
1757     }
1758 
1759     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1760     if (!proxy) {
1761         ANS_LOGE("GetAnsManagerProxy fail.");
1762         return ERR_ANS_SERVICE_NOT_CONNECTED;
1763     }
1764 
1765     sptr<NotificationDoNotDisturbDate> dndDate = nullptr;
1766     auto ret = proxy->GetDoNotDisturbDate(dndDate);
1767     if (ret != ERR_OK) {
1768         ANS_LOGE("Get DoNotDisturbDate failed.");
1769         return ret;
1770     }
1771 
1772     if (!dndDate) {
1773         ANS_LOGE("Invalid DoNotDisturbDate.");
1774         return ERR_ANS_NO_MEMORY;
1775     }
1776 
1777     doNotDisturbDate = *dndDate;
1778     return ret;
1779 }
1780 
SetEnabledForBundleSlot(const NotificationBundleOption & bundleOption,const NotificationConstant::SlotType & slotType,bool enabled,bool isForceControl)1781 ErrCode AnsNotification::SetEnabledForBundleSlot(const NotificationBundleOption &bundleOption,
1782     const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl)
1783 {
1784     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
1785     if (bundleOption.GetBundleName().empty()) {
1786         ANS_LOGE("Invalid bundle name.");
1787         return ERR_ANS_INVALID_PARAM;
1788     }
1789 
1790     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1791     if (!proxy) {
1792         ANS_LOGE("SetEnabledForBundleSlot fail.");
1793         return ERR_ANS_SERVICE_NOT_CONNECTED;
1794     }
1795 
1796     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1797     if (bo == nullptr) {
1798         ANS_LOGE("null bundleOption");
1799         return ERR_ANS_INVALID_PARAM;
1800     }
1801     return proxy->SetEnabledForBundleSlot(bo, slotType, enabled, isForceControl);
1802 }
1803 
GetEnabledForBundleSlot(const NotificationBundleOption & bundleOption,const NotificationConstant::SlotType & slotType,bool & enabled)1804 ErrCode AnsNotification::GetEnabledForBundleSlot(
1805     const NotificationBundleOption &bundleOption, const NotificationConstant::SlotType &slotType, bool &enabled)
1806 {
1807     if (bundleOption.GetBundleName().empty()) {
1808         ANS_LOGE("Invalid bundle name.");
1809         return ERR_ANS_INVALID_PARAM;
1810     }
1811 
1812     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1813     if (!proxy) {
1814         ANS_LOGE("GetEnabledForBundleSlot fail.");
1815         return ERR_ANS_SERVICE_NOT_CONNECTED;
1816     }
1817 
1818     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
1819     if (bo == nullptr) {
1820         ANS_LOGE("null bundleOption");
1821         return ERR_ANS_INVALID_PARAM;
1822     }
1823     return proxy->GetEnabledForBundleSlot(bo, slotType, enabled);
1824 }
1825 
GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType & slotType,bool & enabled)1826 ErrCode AnsNotification::GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType &slotType, bool &enabled)
1827 {
1828     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1829     if (!proxy) {
1830         ANS_LOGE("GetEnabledForBundleSlotSelf fail.");
1831         return ERR_ANS_SERVICE_NOT_CONNECTED;
1832     }
1833 
1834     return proxy->GetEnabledForBundleSlotSelf(slotType, enabled);
1835 }
1836 
ShellDump(const std::string & cmd,const std::string & bundle,int32_t userId,int32_t recvUserId,std::vector<std::string> & dumpInfo)1837 ErrCode AnsNotification::ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId,
1838     int32_t recvUserId, std::vector<std::string> &dumpInfo)
1839 {
1840     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1841     if (!proxy) {
1842         ANS_LOGE("GetAnsManagerProxy fail.");
1843         return ERR_ANS_SERVICE_NOT_CONNECTED;
1844     }
1845 
1846     return proxy->ShellDump(cmd, bundle, userId, recvUserId, dumpInfo);
1847 }
1848 
SetSyncNotificationEnabledWithoutApp(const int32_t userId,const bool enabled)1849 ErrCode AnsNotification::SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled)
1850 {
1851     if (userId <= SUBSCRIBE_USER_INIT) {
1852         ANS_LOGE("Input userId is invalid.");
1853         return ERR_ANS_INVALID_PARAM;
1854     }
1855 
1856     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1857     if (!proxy) {
1858         ANS_LOGE("GetAnsManagerProxy fail.");
1859         return ERR_ANS_SERVICE_NOT_CONNECTED;
1860     }
1861 
1862     return proxy->SetSyncNotificationEnabledWithoutApp(userId, enabled);
1863 }
1864 
GetSyncNotificationEnabledWithoutApp(const int32_t userId,bool & enabled)1865 ErrCode AnsNotification::GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled)
1866 {
1867     if (userId <= SUBSCRIBE_USER_INIT) {
1868         ANS_LOGE("Input userId is invalid.");
1869         return ERR_ANS_INVALID_PARAM;
1870     }
1871 
1872     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1873     if (!proxy) {
1874         ANS_LOGE("GetAnsManagerProxy fail.");
1875         return ERR_ANS_SERVICE_NOT_CONNECTED;
1876     }
1877 
1878     return proxy->GetSyncNotificationEnabledWithoutApp(userId, enabled);
1879 }
1880 
SetBadgeNumber(int32_t badgeNumber,const std::string & instanceKey)1881 ErrCode AnsNotification::SetBadgeNumber(int32_t badgeNumber, const std::string &instanceKey)
1882 {
1883     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1884     if (!proxy) {
1885         ANS_LOGE("SetBadgeNumber fail.");
1886         return ERR_ANS_SERVICE_NOT_CONNECTED;
1887     }
1888     return proxy->SetBadgeNumber(badgeNumber, instanceKey);
1889 }
1890 
SetBadgeNumberByBundle(const NotificationBundleOption & bundleOption,int32_t badgeNumber)1891 ErrCode AnsNotification::SetBadgeNumberByBundle(const NotificationBundleOption &bundleOption, int32_t badgeNumber)
1892 {
1893     if (bundleOption.GetBundleName().empty()) {
1894         ANS_LOGE("Invalid bundle name.");
1895         return ERR_ANS_INVALID_PARAM;
1896     }
1897 
1898     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1899     if (!proxy) {
1900         ANS_LOGE("Unable to connect to ANS service.");
1901         return ERR_ANS_SERVICE_NOT_CONNECTED;
1902     }
1903 
1904     sptr<NotificationBundleOption> bundleInfo(new (std::nothrow) NotificationBundleOption(bundleOption));
1905     if (bundleInfo == nullptr) {
1906         ANS_LOGE("null bundleInfo");
1907         return ERR_ANS_NO_MEMORY;
1908     }
1909     return proxy->SetBadgeNumberByBundle(bundleInfo, badgeNumber);
1910 }
1911 
SetBadgeNumberForDhByBundle(const NotificationBundleOption & bundleOption,int32_t badgeNumber)1912 ErrCode AnsNotification::SetBadgeNumberForDhByBundle(
1913     const NotificationBundleOption &bundleOption, int32_t badgeNumber)
1914 {
1915     if (bundleOption.GetBundleName().empty()) {
1916         ANS_LOGE("Invalid bundle name.");
1917         return ERR_ANS_INVALID_PARAM;
1918     }
1919 
1920     ANS_LOGI("info:%{public}s %{public}d %{public}d",
1921         bundleOption.GetBundleName().c_str(), bundleOption.GetUid(), badgeNumber);
1922 
1923     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1924     if (!proxy) {
1925         ANS_LOGE("Unable to connect to ANS service.");
1926         return ERR_ANS_SERVICE_NOT_CONNECTED;
1927     }
1928 
1929     sptr<NotificationBundleOption> bundleInfo(new (std::nothrow) NotificationBundleOption(bundleOption));
1930     if (bundleInfo == nullptr) {
1931         ANS_LOGE("null bundleInfo");
1932         return ERR_ANS_NO_MEMORY;
1933     }
1934     return proxy->SetBadgeNumberForDhByBundle(bundleInfo, badgeNumber);
1935 }
1936 
GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> & bundleOption)1937 ErrCode AnsNotification::GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> &bundleOption)
1938 {
1939     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1940     if (!proxy) {
1941         ANS_LOGE("Fail to GetAnsManagerProxy.");
1942         return ERR_ANS_SERVICE_NOT_CONNECTED;
1943     }
1944     return proxy->GetAllNotificationEnabledBundles(bundleOption);
1945 }
1946 
GetAllLiveViewEnabledBundles(std::vector<NotificationBundleOption> & bundleOption)1947 ErrCode AnsNotification::GetAllLiveViewEnabledBundles(std::vector<NotificationBundleOption> &bundleOption)
1948 {
1949     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1950     if (!proxy) {
1951         ANS_LOGE("Fail to GetAnsManagerProxy.");
1952         return ERR_ANS_SERVICE_NOT_CONNECTED;
1953     }
1954     return proxy->GetAllLiveViewEnabledBundles(bundleOption);
1955 }
1956 
GetAllDistribuedEnabledBundles(const std::string & deviceType,std::vector<NotificationBundleOption> & bundleOption)1957 ErrCode AnsNotification::GetAllDistribuedEnabledBundles(const std::string& deviceType,
1958     std::vector<NotificationBundleOption> &bundleOption)
1959 {
1960     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1961     if (!proxy) {
1962         ANS_LOGE("Fail to GetAnsManagerProxy.");
1963         return ERR_ANS_SERVICE_NOT_CONNECTED;
1964     }
1965     return proxy->GetAllDistribuedEnabledBundles(deviceType, bundleOption);
1966 }
1967 
RegisterPushCallback(const sptr<IRemoteObject> & pushCallback,const sptr<NotificationCheckRequest> & notificationCheckRequest)1968 ErrCode AnsNotification::RegisterPushCallback(
1969     const sptr<IRemoteObject>& pushCallback, const sptr<NotificationCheckRequest> &notificationCheckRequest)
1970 {
1971     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1972     if (!proxy) {
1973         ANS_LOGE("RegisterPushCallback fail.");
1974         return ERR_ANS_SERVICE_NOT_CONNECTED;
1975     }
1976 
1977     return proxy->RegisterPushCallback(pushCallback, notificationCheckRequest);
1978 }
1979 
UnregisterPushCallback()1980 ErrCode AnsNotification::UnregisterPushCallback()
1981 {
1982     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1983     if (!proxy) {
1984         ANS_LOGE("UnregisterPushCallback fail.");
1985         return ERR_ANS_SERVICE_NOT_CONNECTED;
1986     }
1987 
1988     return proxy->UnregisterPushCallback();
1989 }
1990 
SetAdditionConfig(const std::string & key,const std::string & value)1991 ErrCode AnsNotification::SetAdditionConfig(const std::string &key, const std::string &value)
1992 {
1993     if (key.empty()) {
1994         ANS_LOGE("Set package config fail: key is empty.");
1995         return ERR_ANS_INVALID_PARAM;
1996     }
1997     sptr<IAnsManager> proxy = GetAnsManagerProxy();
1998     if (!proxy) {
1999         ANS_LOGE("Get ans manager proxy fail.");
2000         return ERR_ANS_SERVICE_NOT_CONNECTED;
2001     }
2002 
2003     return proxy->SetAdditionConfig(key, value);
2004 }
2005 
SetDistributedEnabledByBundle(const NotificationBundleOption & bundleOption,const std::string & deviceType,const bool enabled)2006 ErrCode AnsNotification::SetDistributedEnabledByBundle(const NotificationBundleOption &bundleOption,
2007     const std::string &deviceType, const bool enabled)
2008 {
2009     ANS_LOGD("called");
2010     if (bundleOption.GetBundleName().empty() || deviceType.empty()) {
2011         ANS_LOGE("Invalid bundle name.");
2012         return ERR_ANS_INVALID_PARAM;
2013     }
2014 
2015     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2016     if (!proxy) {
2017         ANS_LOGE("SetDistributedEnabledByBundleCallback fail.");
2018         return ERR_ANS_SERVICE_NOT_CONNECTED;
2019     }
2020 
2021     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
2022     if (bo == nullptr) {
2023         ANS_LOGE("null bundleOption");
2024         return ERR_ANS_INVALID_PARAM;
2025     }
2026     return proxy->SetDistributedEnabledByBundle(bo, deviceType, enabled);
2027 }
2028 
SetDistributedBundleOption(const std::vector<DistributedBundleOption> & bundles,const std::string & deviceType)2029 ErrCode AnsNotification::SetDistributedBundleOption(
2030     const std::vector<DistributedBundleOption> &bundles, const std::string &deviceType)
2031 {
2032     ANS_LOGD("called");
2033     if (bundles.empty()) {
2034         ANS_LOGE("Invalid bundles.");
2035         return ERR_ANS_INVALID_PARAM;
2036     }
2037 
2038     if (deviceType.empty()) {
2039         ANS_LOGE("Invalid deviceType.");
2040         return ERR_ANS_INVALID_PARAM;
2041     }
2042     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2043     if (!proxy) {
2044         ANS_LOGE("Get ans manager proxy fail");
2045         return ERR_ANS_SERVICE_NOT_CONNECTED;
2046     }
2047 
2048     std::vector<sptr<DistributedBundleOption>> bundleOptions;
2049     for (auto bundle : bundles) {
2050         sptr<DistributedBundleOption> distributedBundleOption(new (std::nothrow) DistributedBundleOption(bundle));
2051         bundleOptions.emplace_back(distributedBundleOption);
2052     }
2053     return proxy->SetDistributedBundleOption(bundleOptions, deviceType);
2054 }
2055 
SetDistributedEnabled(const std::string & deviceType,const bool & enabled)2056 ErrCode AnsNotification::SetDistributedEnabled(const std::string &deviceType, const bool &enabled)
2057 {
2058     ANS_LOGD("called");
2059     if (deviceType.empty()) {
2060         ANS_LOGE("Invalid deviceType.");
2061         return ERR_ANS_INVALID_PARAM;
2062     }
2063 
2064     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2065     if (!proxy) {
2066         ANS_LOGE("UnregisterPushCallback fail.");
2067         return ERR_ANS_SERVICE_NOT_CONNECTED;
2068     }
2069 
2070     return proxy->SetDistributedEnabled(deviceType, enabled);
2071 }
2072 
IsDistributedEnabled(const std::string & deviceType,bool & enabled)2073 ErrCode AnsNotification::IsDistributedEnabled(const std::string &deviceType, bool &enabled)
2074 {
2075     ANS_LOGD("called");
2076     if (deviceType.empty()) {
2077         ANS_LOGE("Invalid deviceType.");
2078         return ERR_ANS_INVALID_PARAM;
2079     }
2080 
2081     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2082     if (!proxy) {
2083         ANS_LOGE("UnregisterPushCallback fail.");
2084         return ERR_ANS_SERVICE_NOT_CONNECTED;
2085     }
2086 
2087     return proxy->IsDistributedEnabled(deviceType, enabled);
2088 }
2089 
GetDistributedAbility(int32_t & abilityId)2090 ErrCode AnsNotification::GetDistributedAbility(int32_t &abilityId)
2091 {
2092     ANS_LOGD("called");
2093     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2094     if (!proxy) {
2095         ANS_LOGE("UnregisterPushCallback fail.");
2096         return ERR_ANS_SERVICE_NOT_CONNECTED;
2097     }
2098 
2099     return proxy->GetDistributedAbility(abilityId);
2100 }
2101 
GetDistributedAuthStatus(const std::string & deviceType,const std::string & deviceId,int32_t userId,bool & isAuth)2102 ErrCode AnsNotification::GetDistributedAuthStatus(
2103     const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth)
2104 {
2105     ANS_LOGD("called");
2106     if (deviceType.empty() || deviceId.empty()) {
2107         ANS_LOGE("Invalid deviceType or deviceId.");
2108         return ERR_ANS_INVALID_PARAM;
2109     }
2110 
2111     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2112     if (!proxy) {
2113         ANS_LOGE("UnregisterPushCallback fail.");
2114         return ERR_ANS_SERVICE_NOT_CONNECTED;
2115     }
2116 
2117     return proxy->GetDistributedAuthStatus(deviceType, deviceId, userId, isAuth);
2118 }
2119 
SetDistributedAuthStatus(const std::string & deviceType,const std::string & deviceId,int32_t userId,bool isAuth)2120 ErrCode AnsNotification::SetDistributedAuthStatus(
2121     const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth)
2122 {
2123     ANS_LOGD("called");
2124     if (deviceType.empty() || deviceId.empty()) {
2125         ANS_LOGE("Invalid deviceType or deviceId.");
2126         return ERR_ANS_INVALID_PARAM;
2127     }
2128 
2129     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2130     if (!proxy) {
2131         ANS_LOGE("UnregisterPushCallback fail.");
2132         return ERR_ANS_SERVICE_NOT_CONNECTED;
2133     }
2134 
2135     return proxy->SetDistributedAuthStatus(deviceType, deviceId, userId, isAuth);
2136 }
2137 
IsDistributedEnabledByBundle(const NotificationBundleOption & bundleOption,const std::string & deviceType,bool & enabled)2138 ErrCode AnsNotification::IsDistributedEnabledByBundle(const NotificationBundleOption &bundleOption,
2139     const std::string &deviceType, bool &enabled)
2140 {
2141     ANS_LOGD("called");
2142     if (bundleOption.GetBundleName().empty() || deviceType.empty()) {
2143         ANS_LOGE("Invalid bundle name.");
2144         return ERR_ANS_INVALID_PARAM;
2145     }
2146 
2147     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2148     if (!proxy) {
2149         ANS_LOGE("IsDistributedEnabledByBundleCallback fail.");
2150         return ERR_ANS_SERVICE_NOT_CONNECTED;
2151     }
2152 
2153     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
2154     if (bo == nullptr) {
2155         ANS_LOGE("null bundleOption");
2156         return ERR_ANS_INVALID_PARAM;
2157     }
2158     return proxy->IsDistributedEnabledByBundle(bo, deviceType, enabled);
2159 }
2160 
SetSilentReminderEnabled(const NotificationBundleOption & bundleOption,const bool enabled)2161 ErrCode AnsNotification::SetSilentReminderEnabled(const NotificationBundleOption &bundleOption,
2162     const bool enabled)
2163 {
2164     ANS_LOGD("enter");
2165     if (bundleOption.GetBundleName().empty()) {
2166         ANS_LOGE("Invalid bundle name.");
2167         return ERR_ANS_INVALID_PARAM;
2168     }
2169 
2170     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2171     if (!proxy) {
2172         ANS_LOGE("SetSilentReminderEnabledCallback fail.");
2173         return ERR_ANS_SERVICE_NOT_CONNECTED;
2174     }
2175 
2176     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
2177     if (bo == nullptr) {
2178         ANS_LOGE("Fail: bundleOption is empty.");
2179         return ERR_ANS_INVALID_PARAM;
2180     }
2181     return proxy->SetSilentReminderEnabled(bo, enabled);
2182 }
2183 
IsSilentReminderEnabled(const NotificationBundleOption & bundleOption,int32_t & enableStatus)2184 ErrCode AnsNotification::IsSilentReminderEnabled(const NotificationBundleOption &bundleOption,
2185     int32_t &enableStatus)
2186 {
2187     ANS_LOGD("enter");
2188     if (bundleOption.GetBundleName().empty()) {
2189         ANS_LOGE("Invalid bundle name.");
2190         return ERR_ANS_INVALID_PARAM;
2191     }
2192 
2193     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2194     if (!proxy) {
2195         ANS_LOGE("IsSilentReminderEnabledCallback fail.");
2196         return ERR_ANS_SERVICE_NOT_CONNECTED;
2197     }
2198 
2199     sptr<NotificationBundleOption> bo(new (std::nothrow) NotificationBundleOption(bundleOption));
2200     if (bo == nullptr) {
2201         ANS_LOGE("Fail: bundleOption is empty.");
2202         return ERR_ANS_INVALID_PARAM;
2203     }
2204     return proxy->IsSilentReminderEnabled(bo, enableStatus);
2205 }
2206 
SetSmartReminderEnabled(const std::string & deviceType,const bool enabled)2207 ErrCode AnsNotification::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled)
2208 {
2209     ANS_LOGD("called");
2210     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2211     if (!proxy) {
2212         ANS_LOGE("UnregisterPushCallback fail.");
2213         return ERR_ANS_SERVICE_NOT_CONNECTED;
2214     }
2215 
2216     return proxy->SetSmartReminderEnabled(deviceType, enabled);
2217 }
2218 
SetDistributedEnabledBySlot(const NotificationConstant::SlotType & slotType,const std::string & deviceType,const bool enabled)2219 ErrCode AnsNotification::SetDistributedEnabledBySlot(
2220     const NotificationConstant::SlotType &slotType, const std::string &deviceType, const bool enabled)
2221 {
2222     ANS_LOGD("called");
2223     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2224     if (!proxy) {
2225         ANS_LOGE("UnregisterPushCallback fail.");
2226         return ERR_ANS_SERVICE_NOT_CONNECTED;
2227     }
2228 
2229     return proxy->SetDistributedEnabledBySlot(slotType, deviceType, enabled);
2230 }
2231 
IsDistributedEnabledBySlot(const NotificationConstant::SlotType & slotType,const std::string & deviceType,bool & enabled)2232 ErrCode AnsNotification::IsDistributedEnabledBySlot(
2233     const NotificationConstant::SlotType &slotType, const std::string &deviceType, bool &enabled)
2234 {
2235     ANS_LOGD("called");
2236     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2237     if (!proxy) {
2238         ANS_LOGE("UnregisterPushCallback fail.");
2239         return ERR_ANS_SERVICE_NOT_CONNECTED;
2240     }
2241 
2242     return proxy->IsDistributedEnabledBySlot(slotType, deviceType, enabled);
2243 }
2244 
CancelAsBundleWithAgent(const NotificationBundleOption & bundleOption,const int32_t id)2245 ErrCode AnsNotification::CancelAsBundleWithAgent(const NotificationBundleOption &bundleOption, const int32_t id)
2246 {
2247     ANS_LOGI("bundleName:%{public}s,id:%{public}d",
2248         bundleOption.GetBundleName().c_str(), id);
2249     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2250     if (!proxy) {
2251         ANS_LOGE("GetAnsManagerProxy fail.");
2252         return ERR_ANS_SERVICE_NOT_CONNECTED;
2253     }
2254 
2255     sptr<NotificationBundleOption> bundle(new (std::nothrow) NotificationBundleOption(bundleOption));
2256     if (bundle == nullptr) {
2257         ANS_LOGE("null bundle");
2258         return ERR_ANS_INVALID_PARAM;
2259     }
2260     return proxy->CancelAsBundleWithAgent(bundle, id);
2261 }
2262 
IsSmartReminderEnabled(const std::string & deviceType,bool & enabled)2263 ErrCode AnsNotification::IsSmartReminderEnabled(const std::string &deviceType, bool &enabled)
2264 {
2265     ANS_LOGD("called");
2266     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2267     if (!proxy) {
2268         ANS_LOGE("UnregisterPushCallback fail.");
2269         return ERR_ANS_SERVICE_NOT_CONNECTED;
2270     }
2271 
2272     return proxy->IsSmartReminderEnabled(deviceType, enabled);
2273 }
2274 
SetTargetDeviceStatus(const std::string & deviceType,const uint32_t status,const std::string deviceId)2275 ErrCode AnsNotification::SetTargetDeviceStatus(const std::string &deviceType, const uint32_t status,
2276     const std::string deviceId)
2277 {
2278     ANS_LOGD("called");
2279     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2280     if (!proxy) {
2281         ANS_LOGE("UnregisterPushCallback fail.");
2282         return ERR_ANS_SERVICE_NOT_CONNECTED;
2283     }
2284 
2285     return proxy->SetTargetDeviceStatus(deviceType, status, deviceId);
2286 }
2287 
SetTargetDeviceStatus(const std::string & deviceType,const uint32_t status,const uint32_t controlFlag,const std::string deviceId,int32_t userId)2288 ErrCode AnsNotification::SetTargetDeviceStatus(const std::string &deviceType, const uint32_t status,
2289     const uint32_t controlFlag, const std::string deviceId, int32_t userId)
2290 {
2291     ANS_LOGD("called");
2292     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2293     if (!proxy) {
2294         ANS_LOGE("UnregisterPushCallback fail.");
2295         return ERR_ANS_SERVICE_NOT_CONNECTED;
2296     }
2297 
2298     return proxy->SetTargetDeviceStatus(deviceType, status, controlFlag, deviceId, userId);
2299 }
2300 
SetTargetDeviceBundleList(const std::string & deviceType,const std::string & deviceId,int operatorType,const std::vector<std::string> & bundleList,const std::vector<std::string> & labelList)2301 ErrCode AnsNotification::SetTargetDeviceBundleList(const std::string& deviceType, const std::string& deviceId,
2302     int operatorType, const std::vector<std::string>& bundleList, const std::vector<std::string>& labelList)
2303 {
2304     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2305     if (!proxy) {
2306         ANS_LOGE("GetAnsManagerProxy fail.");
2307         return ERR_ANS_SERVICE_NOT_CONNECTED;
2308     }
2309     return proxy->SetTargetDeviceBundleList(deviceType, deviceId, operatorType, bundleList, labelList);
2310 }
2311 
GetMutilDeviceStatus(const std::string & deviceType,const uint32_t status,std::string & deviceId,int32_t & userId)2312 ErrCode AnsNotification::GetMutilDeviceStatus(const std::string &deviceType, const uint32_t status,
2313     std::string& deviceId, int32_t& userId)
2314 {
2315     ANS_LOGD("called");
2316     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2317     if (!proxy) {
2318         ANS_LOGE("GetMutilDeviceStatus fail.");
2319         return ERR_ANS_SERVICE_NOT_CONNECTED;
2320     }
2321 
2322     return proxy->GetMutilDeviceStatus(deviceType, status, deviceId, userId);
2323 }
2324 
GetTargetDeviceBundleList(const std::string & deviceType,const std::string & deviceId,std::vector<std::string> & bundleList,std::vector<std::string> & labelList)2325 ErrCode AnsNotification::GetTargetDeviceBundleList(const std::string& deviceType, const std::string& deviceId,
2326     std::vector<std::string>& bundleList, std::vector<std::string>& labelList)
2327 {
2328     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2329     if (!proxy) {
2330         ANS_LOGE("GetAnsManagerProxy fail.");
2331         return ERR_ANS_SERVICE_NOT_CONNECTED;
2332     }
2333     return proxy->GetTargetDeviceBundleList(deviceType, deviceId, bundleList, labelList);
2334 }
2335 
SetTargetDeviceSwitch(const std::string & deviceType,const std::string & deviceId,bool notificaitonEnable,bool liveViewEnable)2336 ErrCode AnsNotification::SetTargetDeviceSwitch(const std::string& deviceType, const std::string& deviceId,
2337     bool notificaitonEnable, bool liveViewEnable)
2338 {
2339     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2340     if (!proxy) {
2341         ANS_LOGE("GetAnsManagerProxy fail.");
2342         return ERR_ANS_SERVICE_NOT_CONNECTED;
2343     }
2344     return proxy->SetTargetDeviceSwitch(deviceType, deviceId, notificaitonEnable, liveViewEnable);
2345 }
2346 
GetTargetDeviceStatus(const std::string & deviceType,int32_t & status)2347 ErrCode AnsNotification::GetTargetDeviceStatus(const std::string &deviceType, int32_t &status)
2348 {
2349     ANS_LOGD("called");
2350     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2351     if (!proxy) {
2352         ANS_LOGE("UnregisterPushCallback fail.");
2353         return ERR_ANS_SERVICE_NOT_CONNECTED;
2354     }
2355 
2356     return proxy->GetTargetDeviceStatus(deviceType, status);
2357 }
2358 
IsValidTemplate(const NotificationRequest & request) const2359 bool AnsNotification::IsValidTemplate(const NotificationRequest &request) const
2360 {
2361     if (request.GetTemplate() == nullptr) {
2362         return true;
2363     }
2364 
2365     std::string name = request.GetTemplate()->GetTemplateName();
2366     if (strcmp(name.c_str(), DOWNLOAD_TEMPLATE_NAME.c_str()) == 0) {
2367         std::shared_ptr<AAFwk::WantParams> data = request.GetTemplate()->GetTemplateData();
2368         if (data ==nullptr || !data->HasParam(DOWNLOAD_FILENAME) || !data->HasParam(DOWNLOAD_TITLE)) {
2369             ANS_LOGE("No required parameters.");
2370             return false;
2371         }
2372     }
2373 
2374     return true;
2375 }
2376 
IsValidDelayTime(const NotificationRequest & request) const2377 bool AnsNotification::IsValidDelayTime(const NotificationRequest &request)  const
2378 {
2379     return request.GetPublishDelayTime() <= MAX_PUBLISH_DELAY_TIME;
2380 }
2381 
GetDoNotDisturbProfile(int64_t id,sptr<NotificationDoNotDisturbProfile> & profile)2382 ErrCode AnsNotification::GetDoNotDisturbProfile(int64_t id, sptr<NotificationDoNotDisturbProfile> &profile)
2383 {
2384     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2385     if (!proxy) {
2386         ANS_LOGE("Fail to GetAnsManagerProxy.");
2387         return ERR_ANS_SERVICE_NOT_CONNECTED;
2388     }
2389     return proxy->GetDoNotDisturbProfile(id, profile);
2390 }
2391 
AllowUseReminder(const std::string & bundleName,bool & isAllowUseReminder)2392 ErrCode AnsNotification::AllowUseReminder(const std::string& bundleName, bool& isAllowUseReminder)
2393 {
2394     ANS_LOGD("called");
2395     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2396     if (!proxy) {
2397         ANS_LOGE("Fail to GetAnsManagerProxy.");
2398         return ERR_ANS_SERVICE_NOT_CONNECTED;
2399     }
2400 
2401     return proxy->AllowUseReminder(bundleName, isAllowUseReminder);
2402 }
2403 
CreateSubscribeListener(const std::shared_ptr<NotificationSubscriber> & subscriber,sptr<SubscriberListener> & listener)2404 void AnsNotification::CreateSubscribeListener(const std::shared_ptr<NotificationSubscriber> &subscriber,
2405     sptr<SubscriberListener> &listener)
2406 {
2407     std::lock_guard<std::mutex> lock(subscriberMutex_);
2408     auto item = subscribers_.find(subscriber);
2409     if (item != subscribers_.end()) {
2410         listener = item->second;
2411         ANS_LOGD("subscriber has listener");
2412         return;
2413     }
2414     listener = new (std::nothrow) SubscriberListener(subscriber);
2415     if (listener != nullptr) {
2416         subscribers_[subscriber] = listener;
2417         ANS_LOGD("CreateSubscribeListener success");
2418     }
2419 }
2420 
OnServiceDied()2421 void AnsNotification::OnServiceDied()
2422 {
2423     std::lock_guard<std::mutex> lock(subscriberMutex_);
2424     for (auto item : subscribers_) {
2425         item.first->OnDied();
2426     }
2427 }
2428 
2429 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
RegisterSwingCallback(const std::function<void (bool,int)> swingCbFunc)2430 ErrCode AnsNotification::RegisterSwingCallback(const std::function<void(bool, int)> swingCbFunc)
2431 {
2432     ANS_LOGD("called");
2433     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2434     if (!proxy) {
2435         ANS_LOGE("RegisterSwingCallback fail.");
2436         return ERR_ANS_SERVICE_NOT_CONNECTED;
2437     }
2438     swingCallBackService_ = new(std::nothrow) SwingCallBackService(swingCbFunc);
2439     if (swingCallBackService_ == nullptr) {
2440         ANS_LOGE("null swingCallBackService");
2441         return ERR_ANS_INVALID_PARAM;
2442     }
2443     return proxy->RegisterSwingCallback(swingCallBackService_->AsObject());
2444 }
2445 #endif
2446 
UpdateNotificationTimerByUid(const int32_t uid,const bool isPaused)2447 ErrCode AnsNotification::UpdateNotificationTimerByUid(const int32_t uid, const bool isPaused)
2448 {
2449     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2450     if (!proxy) {
2451         ANS_LOGE("UpdateNotificationTimerByUid fail.");
2452         return ERR_ANS_SERVICE_NOT_CONNECTED;
2453     }
2454     return proxy->UpdateNotificationTimerByUid(uid, isPaused);
2455 }
2456 
DisableNotificationFeature(const NotificationDisable & notificationDisable)2457 ErrCode AnsNotification::DisableNotificationFeature(const NotificationDisable &notificationDisable)
2458 {
2459     ANS_LOGD("called");
2460     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2461     if (!proxy) {
2462         ANS_LOGE("DisableNotificationFeature fail");
2463         return ERR_ANS_SERVICE_NOT_CONNECTED;
2464     }
2465     sptr<NotificationDisable> reqPtr = new (std::nothrow) NotificationDisable(notificationDisable);
2466     if (reqPtr == nullptr) {
2467         ANS_LOGE("null reqPtr");
2468         return ERR_ANS_NO_MEMORY;
2469     }
2470     return proxy->DisableNotificationFeature(reqPtr);
2471 }
2472 
DistributeOperation(sptr<NotificationOperationInfo> & operationInfo,const sptr<IAnsOperationCallback> & callback)2473 ErrCode AnsNotification::DistributeOperation(sptr<NotificationOperationInfo>& operationInfo,
2474     const sptr<IAnsOperationCallback> &callback)
2475 {
2476     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
2477     if (operationInfo == nullptr || callback == nullptr) {
2478         ANS_LOGE("null operationInfo or callback");
2479         return ERR_ANS_INVALID_PARAM;
2480     }
2481 
2482     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2483     if (!proxy) {
2484         ANS_LOGE("GetAnsManagerProxy fail.");
2485         return ERR_ANS_SERVICE_NOT_CONNECTED;
2486     }
2487     return proxy->DistributeOperation(operationInfo, callback);
2488 }
2489 
ReplyDistributeOperation(const std::string & hashCode,const int32_t result)2490 ErrCode AnsNotification::ReplyDistributeOperation(const std::string& hashCode, const int32_t result)
2491 {
2492     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
2493 
2494     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2495     if (!proxy) {
2496         ANS_LOGE("GetAnsManagerProxy fail.");
2497         return ERR_ANS_SERVICE_NOT_CONNECTED;
2498     }
2499     return proxy->ReplyDistributeOperation(hashCode, result);
2500 }
2501 
GetNotificationRequestByHashCode(const std::string & hashCode,sptr<NotificationRequest> & notificationRequest)2502 ErrCode AnsNotification::GetNotificationRequestByHashCode(
2503     const std::string& hashCode, sptr<NotificationRequest>& notificationRequest)
2504 {
2505     ANS_LOGI("hashCode:%{public}s", hashCode.c_str());
2506     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
2507 
2508     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2509     if (!proxy) {
2510         ANS_LOGE("GetAnsManagerProxy fail.");
2511         return ERR_ANS_SERVICE_NOT_CONNECTED;
2512     }
2513     return proxy->GetNotificationRequestByHashCode(hashCode, notificationRequest);
2514 }
2515 
SetHashCodeRule(const uint32_t type)2516 ErrCode AnsNotification::SetHashCodeRule(
2517     const uint32_t type)
2518 {
2519     ANS_LOGI("type:%{public}d", type);
2520     NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
2521 
2522     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2523     if (!proxy) {
2524         ANS_LOGE("GetAnsManagerProxy fail.");
2525         return ERR_ANS_SERVICE_NOT_CONNECTED;
2526     }
2527     return proxy->SetHashCodeRule(type);
2528 }
2529 
GetAllNotificationsBySlotType(std::vector<sptr<Notification>> & notifications,const NotificationConstant::SlotType slotType)2530 ErrCode AnsNotification::GetAllNotificationsBySlotType(std::vector<sptr<Notification>> &notifications,
2531     const NotificationConstant::SlotType slotType)
2532 {
2533     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2534     if (!proxy) {
2535         ANS_LOGE("GetAnsManagerProxy fail.");
2536         return ERR_ANS_SERVICE_NOT_CONNECTED;
2537     }
2538     return proxy->GetAllNotificationsBySlotType(notifications, slotType);
2539 }
2540 
GetDistributedDevicelist(std::vector<std::string> & deviceTypes)2541 ErrCode AnsNotification::GetDistributedDevicelist(std::vector<std::string> &deviceTypes)
2542 {
2543     sptr<IAnsManager> proxy = GetAnsManagerProxy();
2544     if (!proxy) {
2545         ANS_LOGE("GetAnsManagerProxy fail.");
2546         return ERR_ANS_SERVICE_NOT_CONNECTED;
2547     }
2548     return proxy->GetDistributedDevicelist(deviceTypes);
2549 }
2550 }  // namespace Notification
2551 }  // namespace OHOS
2552