diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index 14e002917c4c96c477f83879bc839a002730f5d5..9d3d4b81ad05bacbc2ca1fe7c97af220c88780d4 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -131,12 +131,6 @@ ohos_source_set("ans_service_sources") { "src/utils/notifictaion_load_utils.cpp", ] - sources += [ - "src/slot_manager/AddSlots.cpp", - "src/slot_manager/setNotificationEnableSlot.cpp", - "src/slot_manager/slot_manager.cpp", - ] - deps = [ "${frameworks_module_ans_path}:ans_innerkits", "${frameworks_module_ans_path}:ans_manager_stub", diff --git a/services/ans/src/slot_manager/AddSlots.cpp b/services/ans/src/slot_manager/AddSlots.cpp deleted file mode 100644 index 8ce65c814f2cde7728395ac81d9e4d6248d7b2a7..0000000000000000000000000000000000000000 --- a/services/ans/src/slot_manager/AddSlots.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "slot_manager.h" - -#include -#include -#include - -#include "access_token_helper.h" -#include "ans_inner_errors.h" -#include "ans_log_wrapper.h" -#include "ans_permission_def.h" -#include "errors.h" -#include "common_event_manager.h" -#include "common_event_support.h" -#include "hitrace_meter_adapter.h" -#include "os_account_manager_helper.h" -#include "ipc_skeleton.h" -#ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED -#include "smart_reminder_center.h" -#endif - -#include "advanced_notification_inline.cpp" -#include "notification_extension_wrapper.h" -#include "notification_analytics_util.h" - -namespace OHOS { -namespace Notification { -ErrCode SlotManager::AddSlots(MessageParcel &data, MessageParcel &reply) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - std::vector> slots; - if (!ReadParcelableVector(slots, data)) { - ANS_LOGE("[HandleAddSlots] fail: read slotsSize failed"); - return ERR_ANS_PARCELABLE_FAILED; - } - - ErrCode result = AddSlotsSyncQue(slots); - if (!reply.WriteInt32(result)) { - ANS_LOGE("[HandleAddSlots] fail: write result failed, ErrCode=%{public}d", result); - return ERR_ANS_PARCELABLE_FAILED; - } - return ERR_OK; -} - -ErrCode SlotManager::AddSlotsSyncQue(const std::vector> &slots) -{ - sptr bundleOption = AdvancedNotificationService::GenerateBundleOption(); - if (bundleOption == nullptr) { - return ERR_ANS_INVALID_BUNDLE; - } - - auto excuteQueue = AdvancedNotificationService::GetInstance()->GetNotificationSvrQueue(); - if (excuteQueue == nullptr) { - ANS_LOGE("Serial queue is invalid."); - return ERR_ANS_INVALID_PARAM; - } - - ErrCode result; - ffrt::task_handle handler = excuteQueue->submit_h(std::bind([&]() { - result = AddSlotsInner(slots, bundleOption); - })); - excuteQueue->wait(handler); - return result; -} - -ErrCode SlotManager::AddSlotsInner( - const std::vector> &slots, sptr bundleOption) -{ - if (slots.size() == 0) { - return ERR_ANS_INVALID_PARAM; - } - - ErrCode result = ERR_OK; - std::vector> addSlots; - for (auto slot : slots) { - sptr originalSlot; - result = NotificationPreferences::GetInstance()->GetNotificationSlot(bundleOption, - slot->GetType(), originalSlot); - if ((result == ERR_OK) && (originalSlot != nullptr)) { - continue; - } - - GenerateSlotReminderMode(slot, bundleOption, true); - addSlots.push_back(slot); - } - - if (addSlots.size() == 0) { - result = ERR_OK; - } else { - result = NotificationPreferences::GetInstance()->AddNotificationSlots(bundleOption, addSlots); - } - return result; -} - -} // namespace Notification -} // namespace OHOS diff --git a/services/ans/src/slot_manager/setNotificationEnableSlot.cpp b/services/ans/src/slot_manager/setNotificationEnableSlot.cpp deleted file mode 100644 index b85cdbe0706bafa9b2e5dd996c1879e3ec6fb1a2..0000000000000000000000000000000000000000 --- a/services/ans/src/slot_manager/setNotificationEnableSlot.cpp +++ /dev/null @@ -1,205 +0,0 @@ -/* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "slot_manager.h" - -#include "advanced_notification_service.h" - -#include -#include -#include - -#include "access_token_helper.h" -#include "ans_inner_errors.h" -#include "ans_log_wrapper.h" -#include "ans_permission_def.h" -#include "errors.h" -#include "common_event_manager.h" -#include "common_event_support.h" -#include "hitrace_meter_adapter.h" -#include "os_account_manager_helper.h" -#include "ipc_skeleton.h" -#ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED -#include "smart_reminder_center.h" -#endif - -#include "advanced_notification_inline.cpp" -#include "notification_extension_wrapper.h" -#include "notification_analytics_util.h" -#include "event_report.h" - -namespace OHOS { -namespace Notification { -ErrCode SlotManager::SetEnabledForBundleSlot(MessageParcel &data, MessageParcel &reply) -{ - HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); - sptr bundleOption = data.ReadStrongParcelable(); - if (bundleOption == nullptr) { - ANS_LOGE("[HandleSetEnabledForBundleSlot] fail: read bundle failed."); - return ERR_ANS_PARCELABLE_FAILED; - } - - int32_t type = 0; - if (!data.ReadInt32(type)) { - ANS_LOGE("[HandleSetEnabledForBundleSlot] fail: read slot type failed."); - return ERR_ANS_PARCELABLE_FAILED; - } - NotificationConstant::SlotType slotType = static_cast(type); - - bool enabled = false; - if (!data.ReadBool(enabled)) { - ANS_LOGE("[HandleSetEnabledForBundleSlot] fail: read enabled failed."); - return ERR_ANS_PARCELABLE_FAILED; - } - - bool isForceControl = false; - if (!data.ReadBool(isForceControl)) { - ANS_LOGE("[HandleSetEnabledForBundleSlot] fail: read isForceControl failed."); - return ERR_ANS_PARCELABLE_FAILED; - } - - ANS_LOGD("slotType: %{public}d, enabled: %{public}d, isForceControl: %{public}d", - slotType, enabled, isForceControl); - HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_5, EventBranchId::BRANCH_4); - message.Message(bundleOption->GetBundleName() + "_" +std::to_string(bundleOption->GetUid()) + - " slotType: " + std::to_string(static_cast(slotType)) + - " enabled: " +std::to_string(enabled) + "isForceControl" + std::to_string(isForceControl)); - - ErrCode result = SetEnabledForBundleSlotSyncQue(bundleOption, slotType, enabled, isForceControl); - if (!reply.WriteInt32(result)) { - ANS_LOGE("[HandleSetEnabledForBundleSlot] fail: write result failed, ErrCode=%{public}d", result); - return ERR_ANS_PARCELABLE_FAILED; - } - - SendEnableNotificationSlotHiSysEvent(bundleOption, slotType, enabled, result); - message.ErrorCode(result); - NotificationAnalyticsUtil::ReportModifyEvent(message); - ANS_LOGI("%{public}s_%{public}d, SetEnabledForBundleSlot successful.", - bundleOption->GetBundleName().c_str(), bundleOption->GetUid()); - return result; -} - -ErrCode SlotManager::SetEnabledForBundleSlotSyncQue( - const sptr &bundleOption, - const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl) -{ - sptr bundle = AdvancedNotificationService::GenerateValidBundleOption(bundleOption); - if (bundle == nullptr) { - return ERR_ANS_INVALID_BUNDLE; - } - - auto excuteQueue = AdvancedNotificationService::GetInstance()->GetNotificationSvrQueue(); - if (excuteQueue == nullptr) { - ANS_LOGE("Serial queue is invalid."); - return ERR_ANS_INVALID_PARAM; - } - ErrCode result; - ffrt::task_handle handler = excuteQueue->submit_h(std::bind([&]() { - result = SetEnabledForBundleSlotInner(bundleOption, bundle, slotType, enabled, isForceControl); - })); - excuteQueue->wait(handler); - return result; -} - -ErrCode SlotManager::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); - } - ANS_LOGE("Set enable slot: GetNotificationSlot failed"); - return result; -} - -void SlotManager::SendEnableNotificationSlotHiSysEvent( - const sptr &bundleOption, const NotificationConstant::SlotType &slotType, - bool enabled, ErrCode errCode) -{ - if (bundleOption == nullptr) { - return; - } - - EventInfo eventInfo; - eventInfo.bundleName = bundleOption->GetBundleName(); - eventInfo.uid = bundleOption->GetUid(); - eventInfo.slotType = slotType; - eventInfo.enable = enabled; - if (errCode != ERR_OK) { - eventInfo.errCode = errCode; - EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT_ERROR, eventInfo); - } else { - EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT, eventInfo); - } -} - -ErrCode SlotManager::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 = AdvancedNotificationService::GetInstance()->CheckApiCompatibility(bundle); - AdvancedNotificationService::GetInstance()->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()) { - AdvancedNotificationService::GetInstance()->RemoveNotificationBySlot( - bundle, slot, NotificationConstant::DISABLE_SLOT_REASON_DELETE); - } else { - if (!slot->GetForceControl() && !allowed) { - AdvancedNotificationService::GetInstance()->RemoveNotificationBySlot( - bundle, slot, NotificationConstant::DISABLE_NOTIFICATION_REASON_DELETE); - } - } - - AdvancedNotificationService::GetInstance()->PublishSlotChangeCommonEvent(bundle); - return result; -} -} // namespace Notification -} // namespace OHOS diff --git a/services/ans/src/slot_manager/slot_manager.cpp b/services/ans/src/slot_manager/slot_manager.cpp deleted file mode 100644 index 73296d1aa2fd20f8f26754702e579552db073d4b..0000000000000000000000000000000000000000 --- a/services/ans/src/slot_manager/slot_manager.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "slot_manager.h" - -#include -#include -#include - -#include "access_token_helper.h" -#include "ans_inner_errors.h" -#include "ans_log_wrapper.h" -#include "ans_permission_def.h" -#include "errors.h" -#include "common_event_manager.h" -#include "common_event_support.h" -#include "hitrace_meter_adapter.h" -#include "os_account_manager_helper.h" -#include "ipc_skeleton.h" -#ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED -#include "smart_reminder_center.h" -#endif - -#include "advanced_notification_inline.cpp" -#include "notification_config_parse.h" -#include "notification_extension_wrapper.h" -#include "notification_analytics_util.h" - -namespace OHOS { -namespace Notification { -SlotManager::SlotManager() = default; -SlotManager::~SlotManager() = default; -int32_t SlotManager::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) -{ - ErrCode result = CheckInterfacePermission(code); - if (result != ERR_OK) { - if (!reply.WriteInt32(result)) { - return ERR_ANS_PARCELABLE_FAILED; - } - return ERR_OK; - } - - switch (code) { - case static_cast(NotificationInterfaceCode::ADD_SLOTS): { - result = AddSlots(data, reply); - break; - } - case static_cast(NotificationInterfaceCode::SET_ENABLED_FOR_BUNDLE_SLOT): { - result = SetEnabledForBundleSlot(data, reply); - break; - } - default: { - ANS_LOGE("[OnRemoteRequest] fail: unknown code!"); - return ERR_ANS_INVALID_PARAM; - } - } - if (SUCCEEDED(result)) { - return NO_ERROR; - } - - return result; -} - -int32_t SlotManager::CheckInterfacePermission(uint32_t code) -{ - switch (code) { - case static_cast(NotificationInterfaceCode::ADD_SLOTS): - case static_cast(NotificationInterfaceCode::SET_ENABLED_FOR_BUNDLE_SLOT): { - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - return ERR_ANS_PERMISSION_DENIED; - } - - return ERR_OK; - } - default: { - ANS_LOGE("[OnRemoteRequest] fail: unknown code!"); - return ERR_ANS_INVALID_PARAM; - } - } -} - -void SlotManager::GenerateSlotReminderMode(const sptr &slot, - const sptr &bundle, bool isSpecifiedSlot, uint32_t defaultSlotFlags) -{ - uint32_t slotFlags = defaultSlotFlags; - auto ret = NotificationPreferences::GetInstance()->GetNotificationSlotFlagsForBundle(bundle, slotFlags); - if (ret != ERR_OK) { - ANS_LOGI("Failed to get slotflags for bundle, use default slotflags."); - } - - auto configSlotReminderMode = - DelayedSingleton::GetInstance()->GetConfigSlotReminderModeByType(slot->GetType()); - if (isSpecifiedSlot) { - slot->SetReminderMode(configSlotReminderMode & slotFlags & slot->GetReminderMode()); - } else { - slot->SetReminderMode(configSlotReminderMode & slotFlags); - } - - std::string bundleName = (bundle == nullptr) ? "" : bundle->GetBundleName(); - ANS_LOGI("The reminder mode of %{public}d is %{public}d in %{public}s,specifiedSlot:%{public}d default:%{public}u", - slot->GetType(), slot->GetReminderMode(), bundleName.c_str(), isSpecifiedSlot, defaultSlotFlags); -} -} // namespace Notification -} // namespace OHOS