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