• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "advanced_notification_service.h"
17 
18 #include <functional>
19 #include <iomanip>
20 #include <sstream>
21 
22 #include "access_token_helper.h"
23 #include "ans_inner_errors.h"
24 #include "ans_log_wrapper.h"
25 #include "ans_permission_def.h"
26 #include "errors.h"
27 #include "common_event_manager.h"
28 #include "common_event_support.h"
29 #include "hitrace_meter_adapter.h"
30 #include "os_account_manager_helper.h"
31 #include "ipc_skeleton.h"
32 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
33 #include "smart_reminder_center.h"
34 #endif
35 
36 #include "advanced_notification_inline.cpp"
37 #include "notification_config_parse.h"
38 #include "notification_extension_wrapper.h"
39 #include "notification_analytics_util.h"
40 #include "liveview_all_scenarios_extension_wrapper.h"
41 
42 namespace OHOS {
43 namespace Notification {
44 namespace {
45     constexpr char KEY_NAME[] = "AGGREGATE_CONFIG";
46     constexpr char CTRL_LIST_KEY_NAME[] = "NOTIFICATION_CTL_LIST_PKG";
47     constexpr char CALL_UI_BUNDLE[] = "com.ohos.callui";
48 }
49 
AddSlots(const std::vector<sptr<NotificationSlot>> & slots)50 ErrCode AdvancedNotificationService::AddSlots(const std::vector<sptr<NotificationSlot>> &slots)
51 {
52     ANS_LOGD("%{public}s", __FUNCTION__);
53 
54     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
55     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
56         return ERR_ANS_NON_SYSTEM_APP;
57     }
58 
59     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
60         return ERR_ANS_PERMISSION_DENIED;
61     }
62 
63     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
64     if (bundleOption == nullptr) {
65         return ERR_ANS_INVALID_BUNDLE;
66     }
67 
68     if (slots.size() == 0) {
69         return ERR_ANS_INVALID_PARAM;
70     }
71 
72     if (notificationSvrQueue_ == nullptr) {
73         ANS_LOGE("Serial queue is invalid.");
74         return ERR_ANS_INVALID_PARAM;
75     }
76     ErrCode result = ERR_OK;
77     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
78         ANS_LOGD("ffrt enter!");
79         std::vector<sptr<NotificationSlot>> addSlots;
80         for (auto slot : slots) {
81             sptr<NotificationSlot> originalSlot;
82             result =NotificationPreferences::GetInstance()->GetNotificationSlot(bundleOption,
83                 slot->GetType(), originalSlot);
84             if ((result == ERR_OK) && (originalSlot != nullptr)) {
85                 continue;
86             }
87 
88             GenerateSlotReminderMode(slot, bundleOption, true);
89             addSlots.push_back(slot);
90         }
91 
92         if (addSlots.size() == 0) {
93             result = ERR_OK;
94         } else {
95             result = NotificationPreferences::GetInstance()->AddNotificationSlots(bundleOption, addSlots);
96         }
97     }));
98     notificationSvrQueue_->wait(handler);
99     return result;
100 }
101 
GetSlots(std::vector<sptr<NotificationSlot>> & slots)102 ErrCode AdvancedNotificationService::GetSlots(std::vector<sptr<NotificationSlot>> &slots)
103 {
104     ANS_LOGD("%{public}s", __FUNCTION__);
105 
106     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
107     if (bundleOption == nullptr) {
108         return ERR_ANS_INVALID_BUNDLE;
109     }
110 
111     if (notificationSvrQueue_ == nullptr) {
112         ANS_LOGE("NotificationSvrQueue_ is nullptr.");
113         return ERR_ANS_INVALID_PARAM;
114     }
115     ErrCode result = ERR_OK;
116     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
117         ANS_LOGD("ffrt enter!");
118         result = NotificationPreferences::GetInstance()->GetNotificationAllSlots(bundleOption, slots);
119         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
120             result = ERR_OK;
121             slots.clear();
122         }
123     }));
124     notificationSvrQueue_->wait(handler);
125     return result;
126 }
127 
GetSlotsByBundle(const sptr<NotificationBundleOption> & bundleOption,std::vector<sptr<NotificationSlot>> & slots)128 ErrCode AdvancedNotificationService::GetSlotsByBundle(
129     const sptr<NotificationBundleOption> &bundleOption, std::vector<sptr<NotificationSlot>> &slots)
130 {
131     ANS_LOGD("%{public}s", __FUNCTION__);
132 
133     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
134     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
135         ANS_LOGD("IsSystemApp is false.");
136         return ERR_ANS_NON_SYSTEM_APP;
137     }
138 
139     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
140         return ERR_ANS_PERMISSION_DENIED;
141     }
142 
143     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
144     if (bundle == nullptr) {
145         ANS_LOGD("GenerateValidBundleOption failed.");
146         return ERR_ANS_INVALID_BUNDLE;
147     }
148 
149     if (notificationSvrQueue_ == nullptr) {
150         ANS_LOGE("Serial queue is invalid.");
151         return ERR_ANS_INVALID_PARAM;
152     }
153     ErrCode result = ERR_OK;
154     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
155         ANS_LOGD("ffrt enter!");
156         result = NotificationPreferences::GetInstance()->GetNotificationAllSlots(bundle, slots);
157         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
158             result = ERR_OK;
159             slots.clear();
160         }
161     }));
162 
163     notificationSvrQueue_->wait(handler);
164     return result;
165 }
166 
GetSlotByBundle(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,sptr<NotificationSlot> & slot)167 ErrCode AdvancedNotificationService::GetSlotByBundle(
168     const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType,
169     sptr<NotificationSlot> &slot)
170 {
171     ANS_LOGD("%{public}s", __FUNCTION__);
172 
173     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
174     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
175         ANS_LOGD("IsSystemApp is false.");
176         return ERR_ANS_NON_SYSTEM_APP;
177     }
178 
179     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
180         return ERR_ANS_PERMISSION_DENIED;
181     }
182 
183     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
184     if (bundleOption == nullptr) {
185         ANS_LOGD("Failed to generateBundleOption.");
186         return ERR_ANS_INVALID_BUNDLE;
187     }
188 
189     if (notificationSvrQueue_ == nullptr) {
190         ANS_LOGE("Serial queue is invalid.");
191         return ERR_ANS_INVALID_PARAM;
192     }
193     ErrCode result = ERR_OK;
194     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
195         ANS_LOGD("ffrt enter!");
196         result = NotificationPreferences::GetInstance()->GetNotificationSlot(bundle, slotType, slot);
197     }));
198     notificationSvrQueue_->wait(handler);
199     if (slot != nullptr) {
200         ANS_LOGD("GetSlotByBundle, authStatus: %{public}d), authHintCnt: %{public}d",
201             slot->GetAuthorizedStatus(), slot->GetAuthHintCnt());
202     }
203     return result;
204 }
205 
UpdateSlots(const sptr<NotificationBundleOption> & bundleOption,const std::vector<sptr<NotificationSlot>> & slots)206 ErrCode AdvancedNotificationService::UpdateSlots(
207     const sptr<NotificationBundleOption> &bundleOption, const std::vector<sptr<NotificationSlot>> &slots)
208 {
209     ANS_LOGD("%{public}s", __FUNCTION__);
210 
211     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
212     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
213         return ERR_ANS_NON_SYSTEM_APP;
214     }
215 
216     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
217         ANS_LOGD("AccessTokenHelper::CheckPermission is false.");
218         return ERR_ANS_PERMISSION_DENIED;
219     }
220 
221     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
222     if (bundle == nullptr) {
223         return ERR_ANS_INVALID_BUNDLE;
224     }
225 
226     if (notificationSvrQueue_ == nullptr) {
227         ANS_LOGE("notificationSvrQueue_ is nullptr.");
228         return ERR_ANS_INVALID_PARAM;
229     }
230     ErrCode result = ERR_OK;
231     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
232         ANS_LOGD("ffrt enter!");
233         result = NotificationPreferences::GetInstance()->UpdateNotificationSlots(bundle, slots);
234         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
235             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
236         }
237     }));
238     notificationSvrQueue_->wait(handler);
239 
240     if (result == ERR_OK) {
241         PublishSlotChangeCommonEvent(bundle);
242     }
243 
244     return result;
245 }
246 
RemoveAllSlots()247 ErrCode AdvancedNotificationService::RemoveAllSlots()
248 {
249     ANS_LOGD("%{public}s", __FUNCTION__);
250 
251     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
252     if (bundleOption == nullptr) {
253         ANS_LOGD("GenerateBundleOption defeat.");
254         return ERR_ANS_INVALID_BUNDLE;
255     }
256 
257     if (notificationSvrQueue_ == nullptr) {
258         ANS_LOGE("Serial queue is invalid.");
259         return ERR_ANS_INVALID_PARAM;
260     }
261     ErrCode result = ERR_OK;
262     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
263         ANS_LOGD("ffrt enter!");
264         sptr<NotificationSlot> liveViewSlot;
265 
266         bool isLiveViewSlotExist = true;
267         // retain liveview slot before removeNotificationAllSlots
268         if (NotificationPreferences::GetInstance()->GetNotificationSlot(
269             bundleOption, NotificationConstant::SlotType::LIVE_VIEW, liveViewSlot) != ERR_OK) {
270             isLiveViewSlotExist = false;
271         }
272 
273         result = NotificationPreferences::GetInstance()->RemoveNotificationAllSlots(bundleOption);
274         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
275             result = ERR_OK;
276         }
277 
278         if (!isLiveViewSlotExist) {
279             return;
280         }
281         // retain liveview slot when caller is not sa or systemapp
282         if ((result == ERR_OK) &&
283             (IsAllowedRemoveSlot(bundleOption, NotificationConstant::SlotType::LIVE_VIEW) != ERR_OK)) {
284             std::vector<sptr<NotificationSlot>> slots;
285 
286             slots.push_back(liveViewSlot);
287             (void)NotificationPreferences::GetInstance()->AddNotificationSlots(bundleOption, slots);
288         }
289     }));
290     notificationSvrQueue_->wait(handler);
291     return result;
292 }
293 
AddSlotByType(NotificationConstant::SlotType slotType)294 ErrCode AdvancedNotificationService::AddSlotByType(NotificationConstant::SlotType slotType)
295 {
296     ANS_LOGD("%{public}s", __FUNCTION__);
297 
298     if (!AccessTokenHelper::IsSystemApp() && slotType == NotificationConstant::SlotType::EMERGENCY_INFORMATION) {
299         ANS_LOGE("Non system app used illegal slot type.");
300         return ERR_ANS_INVALID_PARAM;
301     }
302 
303     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
304     if (bundleOption == nullptr) {
305         return ERR_ANS_INVALID_BUNDLE;
306     }
307 
308     if (notificationSvrQueue_ == nullptr) {
309         ANS_LOGE("Serial queue is invalidity.");
310         return ERR_ANS_INVALID_PARAM;
311     }
312     ErrCode result = ERR_OK;
313     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
314         ANS_LOGD("ffrt enter!");
315         sptr<NotificationSlot> slot;
316         result = NotificationPreferences::GetInstance()->GetNotificationSlot(bundleOption, slotType, slot);
317         if ((result == ERR_OK) && (slot != nullptr)) {
318             return;
319         }
320 
321         slot = new (std::nothrow) NotificationSlot(slotType);
322         if (slot == nullptr) {
323             ANS_LOGE("Failed to create NotificationSlot instance");
324             return;
325         }
326 
327         GenerateSlotReminderMode(slot, bundleOption);
328         std::vector<sptr<NotificationSlot>> slots;
329         slots.push_back(slot);
330         result = NotificationPreferences::GetInstance()->AddNotificationSlots(bundleOption, slots);
331     }));
332     notificationSvrQueue_->wait(handler);
333     return result;
334 }
335 
GetEnabledForBundleSlotSelf(const NotificationConstant::SlotType & slotType,bool & enabled)336 ErrCode AdvancedNotificationService::GetEnabledForBundleSlotSelf(
337     const NotificationConstant::SlotType &slotType, bool &enabled)
338 {
339     ANS_LOGD("slotType: %{public}d", slotType);
340 
341     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
342     if (bundleOption == nullptr) {
343         return ERR_ANS_INVALID_BUNDLE;
344     }
345 
346     if (notificationSvrQueue_ == nullptr) {
347         ANS_LOGE("Serial queue is invalid.");
348         return ERR_ANS_INVALID_PARAM;
349     }
350     ErrCode result = ERR_OK;
351     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
352         ANS_LOGD("ffrt enter!");
353         sptr<NotificationSlot> slot;
354         result = NotificationPreferences::GetInstance()->GetNotificationSlot(bundleOption, slotType, slot);
355         if (result != ERR_OK) {
356             ANS_LOGE("Get enable slot self: GetNotificationSlot failed");
357             return;
358         }
359         if (slot == nullptr) {
360             ANS_LOGW("Get enable slot: object is null, enabled default true");
361             enabled = true;
362             result = ERR_OK;
363             return;
364         }
365         enabled = slot->GetEnable();
366     }));
367     notificationSvrQueue_->wait(handler);
368 
369     return result;
370 }
371 
GetSlotFlagsAsBundle(const sptr<NotificationBundleOption> & bundleOption,uint32_t & slotFlags)372 ErrCode AdvancedNotificationService::GetSlotFlagsAsBundle(const sptr<NotificationBundleOption> &bundleOption,
373     uint32_t &slotFlags)
374 {
375     ANS_LOGD("%{public}s", __FUNCTION__);
376     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
377     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
378         return ERR_ANS_NON_SYSTEM_APP;
379     }
380 
381     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
382         return ERR_ANS_PERMISSION_DENIED;
383     }
384 
385     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
386     if (bundle == nullptr) {
387         ANS_LOGD("Bundle is null.");
388         return ERR_ANS_INVALID_BUNDLE;
389     }
390 
391     if (notificationSvrQueue_ == nullptr) {
392         ANS_LOGE("Serial queue is invalid.");
393         return ERR_ANS_INVALID_PARAM;
394     }
395     ErrCode result = ERR_OK;
396     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
397         ANS_LOGD("ffrt enter!");
398         result = NotificationPreferences::GetInstance()->GetNotificationSlotFlagsForBundle(bundle, slotFlags);
399         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
400             result = ERR_OK;
401             slotFlags = DEFAULT_SLOT_FLAGS;
402         }
403     }));
404     notificationSvrQueue_->wait(handler);
405 
406     return result;
407 }
408 
SetSlotFlagsAsBundle(const sptr<NotificationBundleOption> & bundleOption,uint32_t slotFlags)409 ErrCode AdvancedNotificationService::SetSlotFlagsAsBundle(const sptr<NotificationBundleOption> &bundleOption,
410     uint32_t slotFlags)
411 {
412     ANS_LOGD("%{public}s", __FUNCTION__);
413     if (bundleOption == nullptr) {
414         ANS_LOGE("BundleOption is null.");
415         return ERR_ANS_INVALID_BUNDLE;
416     }
417 
418     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_8, EventBranchId::BRANCH_2);
419     message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) +
420             " slotFlags:" + std::to_string(slotFlags));
421     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
422     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
423         ANS_LOGE("IsSystemApp is false.");
424         message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).Append(" Not SystemApp");
425         NotificationAnalyticsUtil::ReportModifyEvent(message);
426         return ERR_ANS_NON_SYSTEM_APP;
427     }
428 
429     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
430         ANS_LOGE("Permission denied.");
431         message.ErrorCode(ERR_ANS_PERMISSION_DENIED).Append(" Permission denied");
432         NotificationAnalyticsUtil::ReportModifyEvent(message);
433         return ERR_ANS_PERMISSION_DENIED;
434     }
435 
436     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
437     if (bundle == nullptr) {
438         ANS_LOGE("Bundle is null.");
439         return ERR_ANS_INVALID_BUNDLE;
440     }
441 
442     if (notificationSvrQueue_ == nullptr) {
443         ANS_LOGE("Serial queue is invalidity.");
444         return ERR_ANS_INVALID_PARAM;
445     }
446     ErrCode result = ERR_OK;
447     ffrt::task_handle handler = notificationSvrQueue_->submit_h(
448         std::bind([&]() {
449             result = NotificationPreferences::GetInstance()->SetNotificationSlotFlagsForBundle(bundle, slotFlags);
450             if (result != ERR_OK) {
451                 return;
452             }
453             ANS_LOGI("Set slotflags %{public}d to %{public}s.", slotFlags, bundle->GetBundleName().c_str());
454             result = UpdateSlotReminderModeBySlotFlags(bundle, slotFlags);
455         }));
456     notificationSvrQueue_->wait(handler);
457     ANS_LOGI("%{public}s_%{public}d, slotFlags: %{public}d, SetSlotFlagsAsBundle result: %{public}d",
458         bundleOption->GetBundleName().c_str(), bundleOption->GetUid(), slotFlags, result);
459     message.ErrorCode(result);
460     NotificationAnalyticsUtil::ReportModifyEvent(message);
461     return result;
462 }
463 
AssignValidNotificationSlot(const std::shared_ptr<NotificationRecord> & record,const sptr<NotificationBundleOption> & bundleOption)464 ErrCode AdvancedNotificationService::AssignValidNotificationSlot(const std::shared_ptr<NotificationRecord> &record,
465     const sptr<NotificationBundleOption> &bundleOption)
466 {
467     sptr<NotificationSlot> slot;
468     NotificationConstant::SlotType slotType = record->request->GetSlotType();
469     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_3).SlotType(slotType);
470     ErrCode result = NotificationPreferences::GetInstance()->GetNotificationSlot(bundleOption, slotType, slot);
471     if ((result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) ||
472         (result == ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST)) {
473         slot = new (std::nothrow) NotificationSlot(slotType);
474         if (slot == nullptr) {
475             ANS_LOGE("Failed to create NotificationSlot instance");
476             return ERR_NO_MEMORY;
477         }
478 
479         GenerateSlotReminderMode(slot, bundleOption);
480         if (record->request->IsSystemLiveView()) {
481             ANS_LOGI("System live view no need add sloty.");
482             result = ERR_OK;
483         } else {
484             std::vector<sptr<NotificationSlot>> slots;
485             slots.push_back(slot);
486             result = NotificationPreferences::GetInstance()->AddNotificationSlots(bundleOption, slots);
487         }
488     }
489     if (result == ERR_OK) {
490         std::string bundleName = bundleOption->GetBundleName();
491         if (slot != nullptr &&
492             (bundleName == CALL_UI_BUNDLE || slot->GetEnable() ||
493             (record->request->GetAgentBundle() != nullptr && record->request->IsSystemLiveView()) ||
494             (slot->GetType() == NotificationConstant::SlotType::LIVE_VIEW &&
495             DelayedSingleton<NotificationConfigParse>::GetInstance()->IsLiveViewEnabled(bundleName)))) {
496             record->slot = slot;
497         } else {
498             result = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_ENABLED;
499             ANS_LOGE("Type[%{public}d] slot enable closed", slotType);
500         }
501     }
502     if (result != ERR_OK) {
503         message.ErrorCode(result).Message("assign slot failed");
504         NotificationAnalyticsUtil::ReportPublishFailedEvent(record->request, message);
505     }
506     return result;
507 }
508 
UpdateSlotReminderModeBySlotFlags(const sptr<NotificationBundleOption> & bundle,uint32_t slotFlags)509 ErrCode AdvancedNotificationService::UpdateSlotReminderModeBySlotFlags(
510     const sptr<NotificationBundleOption> &bundle, uint32_t slotFlags)
511 {
512     std::vector<sptr<NotificationSlot>> slots;
513     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_3, EventBranchId::BRANCH_1);
514     ErrCode ret = NotificationPreferences::GetInstance()->GetNotificationAllSlots(bundle, slots);
515     if (ret != ERR_OK) {
516         message.Message("Failed to get slots by bundle, ret:" + std::to_string(ret), true);
517         NotificationAnalyticsUtil::ReportModifyEvent(message);
518         return ret;
519     }
520 
521     message.BundleName((bundle == nullptr) ? "" : bundle->GetBundleName());
522     if (slots.empty()) {
523         message.Message("The bundle has no slots.", true);
524         NotificationAnalyticsUtil::ReportModifyEvent(message);
525         return ERR_OK;
526     }
527 
528     for (auto slot : slots) {
529         auto configSlotReminderMode =
530             DelayedSingleton<NotificationConfigParse>::GetInstance()->GetConfigSlotReminderModeByType(slot->GetType());
531         slot->SetReminderMode(slotFlags & configSlotReminderMode);
532         std::string bundleName = (bundle == nullptr) ? "" : bundle->GetBundleName();
533         ANS_LOGD("Update reminderMode of %{public}d in %{public}s, value is %{public}d.",
534             slot->GetType(), bundleName.c_str(), slot->GetReminderMode());
535     }
536 
537     ret = NotificationPreferences::GetInstance()->UpdateNotificationSlots(bundle, slots);
538     if (ret == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
539         ret = ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST;
540     }
541     return ret;
542 }
543 
GenerateSlotReminderMode(const sptr<NotificationSlot> & slot,const sptr<NotificationBundleOption> & bundle,bool isSpecifiedSlot,uint32_t defaultSlotFlags)544 void AdvancedNotificationService::GenerateSlotReminderMode(const sptr<NotificationSlot> &slot,
545     const sptr<NotificationBundleOption> &bundle, bool isSpecifiedSlot, uint32_t defaultSlotFlags)
546 {
547     uint32_t slotFlags = defaultSlotFlags;
548     auto ret = NotificationPreferences::GetInstance()->GetNotificationSlotFlagsForBundle(bundle, slotFlags);
549     if (ret != ERR_OK) {
550         ANS_LOGI("Failed to get slotflags for bundle, use default slotflags.");
551     }
552 
553     auto configSlotReminderMode =
554         DelayedSingleton<NotificationConfigParse>::GetInstance()->GetConfigSlotReminderModeByType(slot->GetType());
555     if (isSpecifiedSlot) {
556         slot->SetReminderMode(configSlotReminderMode & slotFlags & slot->GetReminderMode());
557     } else {
558         slot->SetReminderMode(configSlotReminderMode & slotFlags);
559     }
560 
561     std::string bundleName = (bundle == nullptr) ? "" : bundle->GetBundleName();
562     ANS_LOGI("The reminder mode of %{public}d is %{public}d in %{public}s,specifiedSlot:%{public}d default:%{public}u",
563         slot->GetType(), slot->GetReminderMode(), bundleName.c_str(), isSpecifiedSlot, defaultSlotFlags);
564 }
565 
GetDefaultSlotFlags(const sptr<NotificationRequest> & request)566 uint32_t AdvancedNotificationService::GetDefaultSlotFlags(const sptr<NotificationRequest> &request)
567 {
568     auto flags = DEFAULT_SLOT_FLAGS;
569     uint32_t notificationControlFlags = request->GetNotificationControlFlags();
570     // SA publish own's notification with banner
571     if ((notificationControlFlags & NotificationConstant::ReminderFlag::SA_SELF_BANNER_FLAG) != 0) {
572         ANS_LOGI("Creator:%{public}s %{public}d,Owner: %{public}s %{public}d, controlFlags:%{public}d",
573             request->GetCreatorBundleName().c_str(), request->GetCreatorUid(), request->GetOwnerBundleName().c_str(),
574             request->GetOwnerUid(), request->GetNotificationControlFlags());
575     }
576     if (((notificationControlFlags & NotificationConstant::ReminderFlag::SA_SELF_BANNER_FLAG) != 0) &&
577         (request->GetCreatorUid() == IPCSkeleton::GetCallingUid() && request->GetCreatorBundleName().empty() &&
578         request->GetOwnerBundleName().empty())) {
579         return (flags |= NotificationConstant::ReminderFlag::BANNER_FLAG);
580     }
581 
582     return flags;
583 }
584 
SetRequestBySlotType(const sptr<NotificationRequest> & request,const sptr<NotificationBundleOption> & bundleOption)585 void AdvancedNotificationService::SetRequestBySlotType(const sptr<NotificationRequest> &request,
586     const sptr<NotificationBundleOption> &bundleOption)
587 {
588     ANS_LOGD("Called.");
589     NotificationConstant::SlotType type = request->GetSlotType();
590     auto flags = std::make_shared<NotificationFlags>();
591 
592     sptr<NotificationSlot> slot;
593     NotificationConstant::SlotType slotType = request->GetSlotType();
594     ErrCode result = NotificationPreferences::GetInstance()->GetNotificationSlot(bundleOption, slotType, slot);
595     if (slot == nullptr) {
596         slot = new (std::nothrow) NotificationSlot(slotType);
597         if (slot == nullptr) {
598             ANS_LOGE("Failed to create NotificationSlot instance");
599             return;
600         }
601         uint32_t slotFlags = GetDefaultSlotFlags(request);
602         GenerateSlotReminderMode(slot, bundleOption, false, slotFlags);
603     }
604 
605     auto slotReminderMode = slot->GetReminderMode();
606     if ((slotReminderMode & NotificationConstant::ReminderFlag::SOUND_FLAG) != 0) {
607         flags->SetSoundEnabled(NotificationConstant::FlagStatus::OPEN);
608     }
609 
610     if ((slotReminderMode & NotificationConstant::ReminderFlag::LOCKSCREEN_FLAG) != 0) {
611         flags->SetLockScreenVisblenessEnabled(true);
612     }
613 
614     if ((slotReminderMode & NotificationConstant::ReminderFlag::BANNER_FLAG) != 0) {
615         flags->SetBannerEnabled(true);
616     }
617 
618     if ((slotReminderMode & NotificationConstant::ReminderFlag::LIGHTSCREEN_FLAG) != 0) {
619         flags->SetLightScreenEnabled(true);
620     }
621 
622     if ((slotReminderMode & NotificationConstant::ReminderFlag::VIBRATION_FLAG) != 0) {
623         flags->SetVibrationEnabled(NotificationConstant::FlagStatus::OPEN);
624     }
625 
626     if ((slotReminderMode & NotificationConstant::ReminderFlag::STATUSBAR_ICON_FLAG) != 0) {
627         flags->SetStatusIconEnabled(true);
628     }
629 
630     request->SetFlags(flags);
631     if (request->IsCommonLiveView()) {
632         LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->UpdateLiveviewReminderFlags(request);
633         LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->UpdateLiveviewVoiceContent(request);
634     }
635     ANS_LOGI("SetFlags-GetRemindMode, notificationKey = %{public}s flags = %{public}d",
636         request->GetKey().c_str(), flags->GetReminderFlags());
637 #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED
638     DelayedSingleton<SmartReminderCenter>::GetInstance()->ReminderDecisionProcess(request);
639 #endif
640     ANS_LOGI("classification:%{public}s", request->GetClassification().c_str());
641 }
642 
GetSlotByType(const NotificationConstant::SlotType & slotType,sptr<NotificationSlot> & slot)643 ErrCode AdvancedNotificationService::GetSlotByType(
644     const NotificationConstant::SlotType &slotType, sptr<NotificationSlot> &slot)
645 {
646     ANS_LOGD("%{public}s", __FUNCTION__);
647 
648     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
649     if (bundleOption == nullptr) {
650         ANS_LOGD("Failed to generateBundleOption.");
651         return ERR_ANS_INVALID_BUNDLE;
652     }
653 
654     if (notificationSvrQueue_ == nullptr) {
655         ANS_LOGE("Serial queue is invalid.");
656         return ERR_ANS_INVALID_PARAM;
657     }
658     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
659         ANS_LOGD("ffrt enter!");
660         NotificationPreferences::GetInstance()->GetNotificationSlot(bundleOption, slotType, slot);
661     }));
662     notificationSvrQueue_->wait(handler);
663     // if get slot failed, it still return ok.
664     return ERR_OK;
665 }
666 
RemoveSlotByType(const NotificationConstant::SlotType & slotType)667 ErrCode AdvancedNotificationService::RemoveSlotByType(const NotificationConstant::SlotType &slotType)
668 {
669     ANS_LOGD("%{public}s", __FUNCTION__);
670 
671     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
672     if (bundleOption == nullptr) {
673         return ERR_ANS_INVALID_BUNDLE;
674     }
675 
676     if (notificationSvrQueue_ == nullptr) {
677         ANS_LOGE("notificationSvrQueue_ is nullptr.");
678         return ERR_ANS_INVALID_PARAM;
679     }
680 
681     ErrCode result = ERR_OK;
682     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
683         ANS_LOGD("ffrt enter!");
684         result = IsAllowedRemoveSlot(bundleOption, slotType);
685         if (result != ERR_OK) {
686             ANS_LOGE("Liveview slot cann't remove.");
687             return;
688         }
689 
690         NotificationPreferences::GetInstance()->RemoveNotificationSlot(bundleOption, slotType);
691     }));
692     notificationSvrQueue_->wait(handler);
693     // if remove slot failed, it still return ok.
694     return result;
695 }
696 
GetSlotNumAsBundle(const sptr<NotificationBundleOption> & bundleOption,uint64_t & num)697 ErrCode AdvancedNotificationService::GetSlotNumAsBundle(
698     const sptr<NotificationBundleOption> &bundleOption, uint64_t &num)
699 {
700     ANS_LOGD("%{public}s", __FUNCTION__);
701 
702     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
703     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
704         return ERR_ANS_NON_SYSTEM_APP;
705     }
706 
707     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
708         return ERR_ANS_PERMISSION_DENIED;
709     }
710 
711     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
712     if (bundle == nullptr) {
713         ANS_LOGD("Bundle is null.");
714         return ERR_ANS_INVALID_BUNDLE;
715     }
716 
717     if (notificationSvrQueue_ == nullptr) {
718         ANS_LOGE("Serial queue is invalid.");
719         return ERR_ANS_INVALID_PARAM;
720     }
721     ErrCode result = ERR_OK;
722     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
723         ANS_LOGD("ffrt enter!");
724         result = NotificationPreferences::GetInstance()->GetNotificationSlotsNumForBundle(bundle, num);
725         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
726             result = ERR_OK;
727             num = 0;
728         }
729     }));
730     notificationSvrQueue_->wait(handler);
731 
732     return result;
733 }
734 
AddSlotThenPublishEvent(const sptr<NotificationSlot> & slot,const sptr<NotificationBundleOption> & bundle,bool enabled,bool isForceControl)735 ErrCode AdvancedNotificationService::AddSlotThenPublishEvent(
736     const sptr<NotificationSlot> &slot,
737     const sptr<NotificationBundleOption> &bundle,
738     bool enabled, bool isForceControl)
739 {
740     bool allowed = false;
741     ErrCode result = NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundle, allowed);
742     if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
743         result = ERR_OK;
744         allowed = CheckApiCompatibility(bundle);
745         SetDefaultNotificationEnabled(bundle, allowed);
746     }
747 
748     slot->SetEnable(enabled);
749     slot->SetForceControl(isForceControl);
750     slot->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED);
751     std::vector<sptr<NotificationSlot>> slots;
752     slots.push_back(slot);
753     result = NotificationPreferences::GetInstance()->AddNotificationSlots(bundle, slots);
754     if (result != ERR_OK) {
755         ANS_LOGE("Set enable slot: AddNotificationSlot failed");
756         return result;
757     }
758 
759     if (!slot->GetEnable()) {
760         RemoveNotificationBySlot(bundle, slot, NotificationConstant::DISABLE_SLOT_REASON_DELETE);
761     } else {
762         if (!slot->GetForceControl() && !allowed) {
763             RemoveNotificationBySlot(bundle, slot, NotificationConstant::DISABLE_NOTIFICATION_REASON_DELETE);
764         }
765     }
766 
767     PublishSlotChangeCommonEvent(bundle);
768     return result;
769 }
770 
SetEnabledForBundleSlotInner(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationBundleOption> & bundle,const NotificationConstant::SlotType & slotType,bool enabled,bool isForceControl)771 ErrCode AdvancedNotificationService::SetEnabledForBundleSlotInner(
772     const sptr<NotificationBundleOption> &bundleOption,
773     const sptr<NotificationBundleOption> &bundle,
774     const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl)
775 {
776     sptr<NotificationSlot> slot;
777     ErrCode result = NotificationPreferences::GetInstance()->GetNotificationSlot(bundle, slotType, slot);
778     if (result == ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST ||
779         result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
780         slot = new (std::nothrow) NotificationSlot(slotType);
781         if (slot == nullptr) {
782             ANS_LOGE("Failed to create NotificationSlot ptr.");
783             return ERR_ANS_NO_MEMORY;
784         }
785         GenerateSlotReminderMode(slot, bundleOption);
786         return AddSlotThenPublishEvent(slot, bundle, enabled, isForceControl);
787     } else if ((result == ERR_OK) && (slot != nullptr)) {
788         if (slot->GetEnable() == enabled && slot->GetForceControl() == isForceControl) {
789             slot->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED);
790             std::vector<sptr<NotificationSlot>> slots;
791             slots.push_back(slot);
792             return NotificationPreferences::GetInstance()->AddNotificationSlots(bundle, slots);
793         }
794         NotificationPreferences::GetInstance()->RemoveNotificationSlot(bundle, slotType);
795         return AddSlotThenPublishEvent(slot, bundle, enabled, isForceControl);
796     }
797     ANS_LOGE("Set enable slot: GetNotificationSlot failed");
798     return result;
799 }
800 
SetEnabledForBundleSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,bool enabled,bool isForceControl)801 ErrCode AdvancedNotificationService::SetEnabledForBundleSlot(const sptr<NotificationBundleOption> &bundleOption,
802     const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl)
803 {
804     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
805     ANS_LOGD("slotType: %{public}d, enabled: %{public}d, isForceControl: %{public}d",
806         slotType, enabled, isForceControl);
807     ErrCode result = CheckCommonParams();
808     if (result != ERR_OK) {
809         return result;
810     }
811 
812     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
813     if (bundle == nullptr) {
814         return ERR_ANS_INVALID_BUNDLE;
815     }
816 
817     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_4);
818     message.Message(bundleOption->GetBundleName() + "_" +std::to_string(bundleOption->GetUid()) +
819         " slotType: " + std::to_string(static_cast<uint32_t>(slotType)) +
820         " enabled: " +std::to_string(enabled) + "isForceControl" + std::to_string(isForceControl));
821     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
822         result = SetEnabledForBundleSlotInner(bundleOption, bundle, slotType, enabled, isForceControl);
823     }));
824     notificationSvrQueue_->wait(handler);
825 
826     SendEnableNotificationSlotHiSysEvent(bundleOption, slotType, enabled, result);
827     message.ErrorCode(result);
828     NotificationAnalyticsUtil::ReportModifyEvent(message);
829     ANS_LOGI("%{public}s_%{public}d, SetEnabledForBundleSlot successful.",
830         bundleOption->GetBundleName().c_str(), bundleOption->GetUid());
831     return result;
832 }
833 
GetEnabledForBundleSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,bool & enabled)834 ErrCode AdvancedNotificationService::GetEnabledForBundleSlot(
835     const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType, bool &enabled)
836 {
837     ANS_LOGD("slotType: %{public}d", slotType);
838 
839     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
840     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
841         ANS_LOGD("VerifyNativeToken and isSystemApp failed.");
842         return ERR_ANS_NON_SYSTEM_APP;
843     }
844 
845     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
846         return ERR_ANS_PERMISSION_DENIED;
847     }
848 
849     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
850     if (bundle == nullptr) {
851         return ERR_ANS_INVALID_BUNDLE;
852     }
853 
854     if (notificationSvrQueue_ == nullptr) {
855         ANS_LOGE("Serial queue is invalid.");
856         return ERR_ANS_INVALID_PARAM;
857     }
858     ErrCode result = ERR_OK;
859     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
860         ANS_LOGD("ffrt enter!");
861         sptr<NotificationSlot> slot;
862         result = NotificationPreferences::GetInstance()->GetNotificationSlot(bundle, slotType, slot);
863         if (result != ERR_OK) {
864             ANS_LOGE("Get enable slot: GetNotificationSlot failed");
865             return;
866         }
867         if (slot == nullptr) {
868             ANS_LOGW("Get enable slot: object is null, enabled default true");
869             enabled = true;
870             result = ERR_OK;
871             return;
872         }
873         enabled = slot->GetEnable();
874     }));
875     notificationSvrQueue_->wait(handler);
876 
877     return result;
878 }
879 
GetAllNotificationEnabledBundles(std::vector<NotificationBundleOption> & bundleOption)880 ErrCode AdvancedNotificationService::GetAllNotificationEnabledBundles(
881     std::vector<NotificationBundleOption> &bundleOption)
882 {
883     ANS_LOGD("Called.");
884     if (!AccessTokenHelper::IsSystemApp()) {
885         ANS_LOGE("Is not system app.");
886         return ERR_ANS_NON_SYSTEM_APP;
887     }
888     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
889         ANS_LOGE("Permission denied.");
890         return ERR_ANS_PERMISSION_DENIED;
891     }
892     if (notificationSvrQueue_ == nullptr) {
893         ANS_LOGE("Serial queue is invalid.");
894         return ERR_ANS_INVALID_PARAM;
895     }
896     ErrCode result = ERR_OK;
897     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
898         ANS_LOGD("ffrt enter!");
899         result = NotificationPreferences::GetInstance()->GetAllNotificationEnabledBundles(bundleOption);
900         if (result != ERR_OK) {
901             ANS_LOGE("Get all notification enable status failed");
902             return;
903         }
904     }));
905     notificationSvrQueue_->wait(handler);
906 
907     return result;
908 }
909 
GetAllLiveViewEnabledBundles(std::vector<NotificationBundleOption> & bundleOption)910 ErrCode AdvancedNotificationService::GetAllLiveViewEnabledBundles(
911     std::vector<NotificationBundleOption> &bundleOption)
912 {
913     ANS_LOGD("Called.");
914     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
915         ANS_LOGE("Permission denied.");
916         return ERR_ANS_PERMISSION_DENIED;
917     }
918     if (notificationSvrQueue_ == nullptr) {
919         ANS_LOGE("Serial queue is invalid.");
920         return ERR_ANS_INVALID_PARAM;
921     }
922     int32_t userId = 100;
923     OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId);
924     ErrCode result = ERR_OK;
925     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&, userId]() {
926         ANS_LOGD("ffrt enter!");
927         result = NotificationPreferences::GetInstance()->GetAllLiveViewEnabledBundles(userId, bundleOption);
928         if (result != ERR_OK) {
929             ANS_LOGE("Get all notification enable status failed");
930             return;
931         }
932     }));
933     notificationSvrQueue_->wait(handler);
934 
935     return result;
936 }
937 
GetAllDistribuedEnabledBundles(const std::string & deviceType,std::vector<NotificationBundleOption> & bundleOption)938 ErrCode AdvancedNotificationService::GetAllDistribuedEnabledBundles(
939     const std::string& deviceType, std::vector<NotificationBundleOption> &bundleOption)
940 {
941     ANS_LOGD("Called.");
942     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
943         ANS_LOGE("Permission denied.");
944         return ERR_ANS_PERMISSION_DENIED;
945     }
946     if (notificationSvrQueue_ == nullptr) {
947         ANS_LOGE("Serial queue is invalid.");
948         return ERR_ANS_INVALID_PARAM;
949     }
950 
951     int32_t userId = 100;
952     OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId);
953     ErrCode result = ERR_OK;
954     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&, userId, deviceType]() {
955         ANS_LOGD("ffrt enter!");
956         result = NotificationPreferences::GetInstance()->GetAllDistribuedEnabledBundles(userId,
957             deviceType, bundleOption);
958         if (result != ERR_OK) {
959             ANS_LOGE("Get all notification enable status failed");
960             return;
961         }
962     }));
963     notificationSvrQueue_->wait(handler);
964 
965     return result;
966 }
967 
PublishSlotChangeCommonEvent(const sptr<NotificationBundleOption> & bundleOption)968 bool AdvancedNotificationService::PublishSlotChangeCommonEvent(const sptr<NotificationBundleOption> &bundleOption)
969 {
970     if (bundleOption == nullptr) {
971         return false;
972     }
973     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
974     ANS_LOGD("bundle [%{public}s : %{public}d]", bundleOption->GetBundleName().c_str(), bundleOption->GetUid());
975 
976     EventFwk::Want want;
977     AppExecFwk::ElementName element;
978     element.SetBundleName(bundleOption->GetBundleName());
979     want.SetElement(element);
980     want.SetParam(AppExecFwk::Constants::UID, bundleOption->GetUid());
981     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SLOT_CHANGE);
982     EventFwk::CommonEventData commonData {want};
983     if (!EventFwk::CommonEventManager::PublishCommonEvent(commonData)) {
984         ANS_LOGE("PublishCommonEvent failed");
985         return false;
986     }
987 
988     return true;
989 }
990 
SetAdditionConfig(const std::string & key,const std::string & value)991 ErrCode AdvancedNotificationService::SetAdditionConfig(const std::string &key, const std::string &value)
992 {
993     ANS_LOGD("SetAdditionConfig called (%{public}s, %{public}s).", key.c_str(), value.c_str());
994     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_8, EventBranchId::BRANCH_1);
995     message.Message(" key:" + key + " value" + value);
996     if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) {
997         ANS_LOGE("Permission denied.");
998         message.ErrorCode(ERR_ANS_PERMISSION_DENIED).Append(" Permission denied");
999         NotificationAnalyticsUtil::ReportModifyEvent(message);
1000         return ERR_ANS_PERMISSION_DENIED;
1001     }
1002 
1003     if (notificationSvrQueue_ == nullptr) {
1004         ANS_LOGE("Serial queue is invalid.");
1005         return ERR_ANS_INVALID_PARAM;
1006     }
1007 
1008     if (key == RING_TRUST_PKG_KEY) {
1009         std::lock_guard<std::mutex> lock(soundPermissionInfo_->dbMutex_);
1010         soundPermissionInfo_->needUpdateCache_ = true;
1011     }
1012 
1013     bool isSyncConfig = (strcmp(key.c_str(), KEY_NAME) == 0 ||
1014         strcmp(key.c_str(), CTRL_LIST_KEY_NAME) == 0);
1015     if (isSyncConfig) {
1016 #ifdef ENABLE_ANS_EXT_WRAPPER
1017     ErrCode sync_result = EXTENTION_WRAPPER->SyncAdditionConfig(key, value);
1018     if (sync_result != ERR_OK) {
1019         ANS_LOGE("Sync addition config result: %{public}d, key: %{public}s, value: %{public}s",
1020             sync_result, key.c_str(), value.c_str());
1021         message.ErrorCode(sync_result).Append(" Sync failed");
1022         NotificationAnalyticsUtil::ReportModifyEvent(message);
1023         return sync_result;
1024     }
1025 #endif
1026     }
1027     ErrCode result = ERR_OK;
1028     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
1029         ANS_LOGD("ffrt enter!");
1030         result = NotificationPreferences::GetInstance()->SetKvToDb(key, value, SUBSCRIBE_USER_INIT);
1031     }));
1032     notificationSvrQueue_->wait(handler);
1033     ANS_LOGI("Set addition config result: %{public}d, key: %{public}s, value: %{public}s",
1034         result, key.c_str(), value.c_str());
1035     message.ErrorCode(result);
1036     NotificationAnalyticsUtil::ReportModifyEvent(message);
1037     return result;
1038 }
1039 
IsAgentRelationship(const std::string & agentBundleName,const std::string & sourceBundleName)1040 bool AdvancedNotificationService::IsAgentRelationship(const std::string &agentBundleName,
1041     const std::string &sourceBundleName)
1042 {
1043     return NotificationPreferences::GetInstance()->IsAgentRelationship(agentBundleName, sourceBundleName);
1044 }
1045 }  // namespace Notification
1046 }  // namespace OHOS
1047