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