diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 137f033e379b489934d304013de59a1f72617658..e1583b8c2d058fec0016d2afbc39e188d6cc5935 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1397,7 +1397,13 @@ private: bool IsSystemUser(int32_t userId); ErrCode UpdateFlowCtrl(const std::shared_ptr &record); ErrCode PublishFlowControlInner(const std::shared_ptr &record); - + ErrCode SetEnabledForBundleSlotInner(const sptr &bundleOption, + const sptr &bundle, + const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl); + ErrCode AddSlotThenPublishEvent( + const sptr &slot, + const sptr &bundle, + bool enabled, bool isForceControl); private: static sptr instance_; static std::mutex instanceMutex_; diff --git a/services/ans/src/advanced_notification_slot_service.cpp b/services/ans/src/advanced_notification_slot_service.cpp index 0b74eb7d7b44deddb9077ffa7d57f01561a40f8c..fa837a0c1c1a61f350f0439f2184a52c4aa17e24 100644 --- a/services/ans/src/advanced_notification_slot_service.cpp +++ b/services/ans/src/advanced_notification_slot_service.cpp @@ -714,6 +714,74 @@ ErrCode AdvancedNotificationService::GetSlotNumAsBundle( return result; } +ErrCode AdvancedNotificationService::AddSlotThenPublishEvent( + const sptr &slot, + const sptr &bundle, + bool enabled, bool isForceControl) +{ + bool allowed = false; + ErrCode result = NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundle, allowed); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + allowed = CheckApiCompatibility(bundle); + SetDefaultNotificationEnabled(bundle, allowed); + } + + slot->SetEnable(enabled); + slot->SetForceControl(isForceControl); + slot->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED); + std::vector> slots; + slots.push_back(slot); + result = NotificationPreferences::GetInstance()->AddNotificationSlots(bundle, slots); + if (result != ERR_OK) { + ANS_LOGE("Set enable slot: AddNotificationSlot failed"); + return result; + } + + if (!slot->GetEnable()) { + RemoveNotificationBySlot(bundle, slot, NotificationConstant::DISABLE_SLOT_REASON_DELETE); + } else { + if (!slot->GetForceControl() && !allowed) { + RemoveNotificationBySlot(bundle, slot, NotificationConstant::DISABLE_NOTIFICATION_REASON_DELETE); + } + } + + PublishSlotChangeCommonEvent(bundle); + return result; +} + +ErrCode AdvancedNotificationService::SetEnabledForBundleSlotInner( + const sptr &bundleOption, + const sptr &bundle, + const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl) +{ + sptr slot; + ErrCode result = NotificationPreferences::GetInstance()->GetNotificationSlot(bundle, slotType, slot); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST || + result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + slot = new (std::nothrow) NotificationSlot(slotType); + if (slot == nullptr) { + ANS_LOGE("Failed to create NotificationSlot ptr."); + return ERR_ANS_NO_MEMORY; + } + GenerateSlotReminderMode(slot, bundleOption); + return AddSlotThenPublishEvent(slot, bundle, enabled, isForceControl); + } else if ((result == ERR_OK) && (slot != nullptr)) { + if (slot->GetEnable() == enabled && slot->GetForceControl() == isForceControl) { + slot->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED); + std::vector> slots; + slots.push_back(slot); + return NotificationPreferences::GetInstance()->AddNotificationSlots(bundle, slots); + } + NotificationPreferences::GetInstance()->RemoveNotificationSlot(bundle, slotType); + return AddSlotThenPublishEvent(slot, bundle, enabled, isForceControl); + } else { + ANS_LOGE("Set enable slot: GetNotificationSlot failed"); + return result; + } + +} + ErrCode AdvancedNotificationService::SetEnabledForBundleSlot(const sptr &bundleOption, const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl) { @@ -731,62 +799,8 @@ ErrCode AdvancedNotificationService::SetEnabledForBundleSlot(const sptrsubmit_h(std::bind([&]() { - sptr slot; - result = NotificationPreferences::GetInstance()->GetNotificationSlot(bundle, slotType, slot); - if (result == ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST || - result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { - slot = new (std::nothrow) NotificationSlot(slotType); - if (slot == nullptr) { - ANS_LOGE("Failed to create NotificationSlot ptr."); - result = ERR_ANS_NO_MEMORY; - return; - } - GenerateSlotReminderMode(slot, bundleOption); - } else if ((result == ERR_OK) && (slot != nullptr)) { - if (slot->GetEnable() == enabled && slot->GetForceControl() == isForceControl) { - // 设置authorizedStatus为已授权 - slot->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED); - std::vector> slots; - slots.push_back(slot); - result = NotificationPreferences::GetInstance()->AddNotificationSlots(bundle, slots); - return; - } - NotificationPreferences::GetInstance()->RemoveNotificationSlot(bundle, slotType); - } else { - ANS_LOGE("Set enable slot: GetNotificationSlot failed"); - return; - } - bool allowed = false; - result = NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundle, allowed); - if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { - result = ERR_OK; - allowed = CheckApiCompatibility(bundle); - SetDefaultNotificationEnabled(bundle, allowed); - } - - slot->SetEnable(enabled); - slot->SetForceControl(isForceControl); - // 设置authorizedStatus为已授权 - slot->SetAuthorizedStatus(NotificationSlot::AuthorizedStatus::AUTHORIZED); - std::vector> slots; - slots.push_back(slot); - result = NotificationPreferences::GetInstance()->AddNotificationSlots(bundle, slots); - if (result != ERR_OK) { - ANS_LOGE("Set enable slot: AddNotificationSlot failed"); - return; - } - - if (!slot->GetEnable()) { - RemoveNotificationBySlot(bundle, slot, NotificationConstant::DISABLE_SLOT_REASON_DELETE); - } else { - if (!slot->GetForceControl() && !allowed) { - RemoveNotificationBySlot(bundle, slot, NotificationConstant::DISABLE_NOTIFICATION_REASON_DELETE); - } - } - - PublishSlotChangeCommonEvent(bundle); + result = SetEnabledForBundleSlotInner(bundleOption, bundle, slotType, enabled, isForceControl); })); notificationSvrQueue_->wait(handler);