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