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