diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index ec0c7506ac162b9d5659c1663bb6ebfe989daf5b..aa1d209c1bd3529c4db8e61dfd0049cdb93a5b7e 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -105,6 +105,7 @@ ohos_source_set("ans_service_sources") { "src/disturb_manager/get_do_not_disturb_profile.cpp", "src/disturb_manager/remove_do_not_disturb_profiles.cpp", "src/disturb_manager/set_do_not_disturb_date.cpp", + "src/enable_manager/enable_manager.cpp", "src/event_report.cpp", "src/liveview_all_scenarios_extension_wrapper.cpp", "src/notification_dialog.cpp", diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index 4b749402d169825cefcb0550fe263b380f8e974e..073e60037b00ae95abed9b32322e15d261e6778e 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -74,13 +74,8 @@ constexpr const char *FOCUS_MODE_REPEAT_CALLERS_ENABLE = "1"; constexpr const char *CONTACT_DATA = "datashare:///com.ohos.contactsdataability/contacts/contact_data?Proxy=true"; constexpr const char *SUPPORT_INTEGELLIGENT_SCENE = "true"; constexpr int32_t OPERATION_TYPE_COMMON_EVENT = 4; -const static std::string BUNDLE_NAME_ZYT = "com.zhuoyi.appstore.lite"; -const static std::string BUNDLE_NAME_ABROAD = "com.easy.abroad"; -const static std::string INSTALL_SOURCE_EASYABROAD = "com.easy.abroad"; -constexpr int32_t ZERO_USER_ID = 0; constexpr int32_t CLEAR_SLOT_FROM_AVSEESAION = 1; constexpr int32_t CLEAR_SLOT_FROM_RSS = 2; -constexpr const char *SAMPLE_MEACHINE = "const.dfx.enable_retail"; ErrCode AdvancedNotificationService::SetDefaultNotificationEnabled( const sptr &bundleOption, bool enabled) @@ -898,418 +893,6 @@ ErrCode AdvancedNotificationService::DeleteAll() return result; } -ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId, - const sptr &callback) -{ - return RequestEnableNotification(deviceId, callback, nullptr); -} - -ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId, - const sptr &callback, - const sptr &callerToken) -{ - HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_13, EventBranchId::BRANCH_4); - message.Message(" de:" + deviceId); - ANS_LOGD("%{public}s", __FUNCTION__); - if (callback == nullptr) { - ANS_LOGE("callback == nullptr"); - message.ErrorCode(ERR_ANS_INVALID_PARAM); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERR_ANS_INVALID_PARAM; - } - sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - ANS_LOGE("bundleOption is nullptr."); - return ERROR_INTERNAL_ERROR; - } - return CommonRequestEnableNotification(deviceId, callback, callerToken, bundleOption, false, false); -} - -ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string& bundleName, int32_t uid) -{ - ANS_LOGI("RequestEnableNotification bundleName = %{public}s uid = %{public}d", bundleName.c_str(), uid); - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - return ERR_ANS_PERMISSION_DENIED; - } - if (bundleName == BUNDLE_NAME_ZYT || bundleName == BUNDLE_NAME_ABROAD) { - ANS_LOGI("RequestEnableNotification zyt or abroad"); - return ERR_ANS_NOT_ALLOWED; - } - - AppExecFwk::BundleInfo bundleInfo; - bool ret = BundleManagerHelper::GetInstance()->GetBundleInfoV9(bundleName, - static_cast(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION), - bundleInfo, ZERO_USER_ID); - bool easyAbroad = false; - if (bundleInfo.applicationInfo.installSource == INSTALL_SOURCE_EASYABROAD) { - ANS_LOGI("RequestEnableNotification abroad app"); - easyAbroad = true; - } - sptr bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid); - if (bundleOption == nullptr) { - ANS_LOGE("bundleOption is nullptr."); - return ERROR_INTERNAL_ERROR; - } - return CommonRequestEnableNotification("", nullptr, nullptr, bundleOption, true, easyAbroad); -} - -ErrCode AdvancedNotificationService::CommonRequestEnableNotification(const std::string &deviceId, - const sptr &callback, - const sptr &callerToken, - const sptr bundleOption, - const bool innerLake, - const bool easyAbroad) -{ - ANS_LOGI("%{public}s", __FUNCTION__); - ErrCode result = ERR_OK; - // To get the permission - bool allowedNotify = false; - HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_8, EventBranchId::BRANCH_5); - if (bundleOption == nullptr) { - ANS_LOGE("bundleOption is nullptr."); - return ERROR_INTERNAL_ERROR; - } - message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) + - " deviceId:" + deviceId); - result = IsAllowedNotifySelf(bundleOption, allowedNotify); - if (result != ERR_OK) { - ANS_LOGE("Not allowed notify self"); - message.ErrorCode(result).Append(" Allow failed"); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERROR_INTERNAL_ERROR; - } - ANS_LOGI("allowedNotify = %{public}d, bundle = %{public}s", allowedNotify, - bundleOption->GetBundleName().c_str()); - if (allowedNotify) { - message.ErrorCode(ERR_OK).Append(" Allow success"); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERR_OK; - } - // Check to see if it has been popover before - bool hasPopped = false; - result = GetHasPoppedDialog(bundleOption, hasPopped); - if (result != ERR_OK) { - ANS_LOGE("Get has popped dialog failed."); - message.ErrorCode(result).Append(" Get dialog failed."); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERROR_INTERNAL_ERROR; - } - if (hasPopped) { - ANS_LOGE("Has popped is true."); - message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Has popped"); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERR_ANS_NOT_ALLOWED; - } - if (GetSystemBoolParameter(SAMPLE_MEACHINE, false)) { - return ERR_ANS_NOT_ALLOWED; - } - - if (!CreateDialogManager()) { - ANS_LOGE("Create dialog manager failed."); - message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Create dialog failed"); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERROR_INTERNAL_ERROR; - } - - result = dialogManager_->RequestEnableNotificationDailog(bundleOption, - callback, callerToken, innerLake, easyAbroad); - if (result == ERR_OK) { - result = ERR_ANS_DIALOG_POP_SUCCEEDED; - } - - ANS_LOGI("%{public}s_%{public}d, deviceId: %{public}s, Request enable notification dailog result: %{public}d", - bundleOption->GetBundleName().c_str(), bundleOption->GetUid(), deviceId.c_str(), result); - message.ErrorCode(result); - if (!innerLake || result == ERR_ANS_DIALOG_POP_SUCCEEDED) { - NotificationAnalyticsUtil::ReportModifyEvent(message); - } - return result; -} - -ErrCode AdvancedNotificationService::SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) -{ - return ERR_INVALID_OPERATION; -} - -ErrCode AdvancedNotificationService::SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - ANS_LOGD("VerifyNativeToken and IsSystemApp is false."); - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - return ERR_ANS_PERMISSION_DENIED; - } - - int32_t userId = SUBSCRIBE_USER_INIT; - if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { - return ERR_ANS_GET_ACTIVE_USER_FAILED; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalidity."); - return ERR_ANS_INVALID_PARAM; - } - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - if (deviceId.empty()) { - // Local device - result = NotificationPreferences::GetInstance()->SetNotificationsEnabled(userId, enabled); - } else { - // Remote device - } - })); - notificationSvrQueue_->wait(handler); - return result; -} - -ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundle( - const std::string &deviceId, const sptr &bundleOption, bool enabled) -{ - HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_13, EventBranchId::BRANCH_5); - HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); - ANS_LOGD("%{public}s", __FUNCTION__); - if (bundleOption == nullptr) { - ANS_LOGE("BundleOption is null."); - message.ErrorCode(ERR_ANS_INVALID_BUNDLE); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERR_ANS_INVALID_BUNDLE; - } - - message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) + - " en:" + std::to_string(enabled) + - " dId:" + deviceId); - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - ANS_LOGE("IsSystemApp is false."); - message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).BranchId(BRANCH_6); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERR_ANS_NON_SYSTEM_APP; - } - - int32_t callingUid = IPCSkeleton::GetCallingUid(); - if (callingUid != ANS_UID && !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - ANS_LOGE("Permission Denied."); - message.ErrorCode(ERR_ANS_PERMISSION_DENIED).BranchId(BRANCH_7); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERR_ANS_PERMISSION_DENIED; - } - - sptr bundle = GenerateValidBundleOption(bundleOption); - if (bundle == nullptr) { - message.ErrorCode(ERR_ANS_INVALID_BUNDLE).BranchId(BRANCH_8); - NotificationAnalyticsUtil::ReportModifyEvent(message); - ANS_LOGE(" Bundle is nullptr."); - return ERR_ANS_INVALID_BUNDLE; - } - - sptr bundleData = new (std::nothrow) - EnabledNotificationCallbackData(bundle->GetBundleName(), bundle->GetUid(), enabled); - if (bundleData == nullptr) { - ANS_LOGE("Failed to create EnabledNotificationCallbackData instance"); - return ERR_NO_MEMORY; - } - - ErrCode result = ERR_OK; - if (deviceId.empty()) { - // Local device - result = NotificationPreferences::GetInstance()->SetNotificationsEnabledForBundle(bundle, enabled); - if (result == ERR_OK) { - if (!enabled) { - result = RemoveAllNotificationsForDisable(bundle); - } - SetSlotFlagsTrustlistsAsBundle(bundle); - NotificationSubscriberManager::GetInstance()->NotifyEnabledNotificationChanged(bundleData); - PublishSlotChangeCommonEvent(bundle); - } - } else { - // Remote device - } - - ANS_LOGI("%{public}s_%{public}d, deviceId: %{public}s, enable: %{public}s, " - "Set notifications enabled for special bundle result: %{public}d", bundleOption->GetBundleName().c_str(), - bundleOption->GetUid(), deviceId.c_str(), std::to_string(enabled).c_str(), result); - message.ErrorCode(result).BranchId(BRANCH_9); - NotificationAnalyticsUtil::ReportModifyEvent(message); - SendEnableNotificationHiSysEvent(bundleOption, enabled, result); - return result; -} - -ErrCode AdvancedNotificationService::IsAllowedNotify(bool &allowed) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - ANS_LOGD("AccessTokenHelper::CheckPermission is false"); - return ERR_ANS_PERMISSION_DENIED; - } - - int32_t userId = SUBSCRIBE_USER_INIT; - if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { - return ERR_ANS_GET_ACTIVE_USER_FAILED; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalid."); - return ERR_ANS_INVALID_PARAM; - } - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - allowed = false; - result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed); - })); - notificationSvrQueue_->wait(handler); - return result; -} - -ErrCode AdvancedNotificationService::IsAllowedNotifySelf(bool &allowed) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - return ERR_ANS_INVALID_BUNDLE; - } - return IsAllowedNotifySelf(bundleOption, allowed); -} - -ErrCode AdvancedNotificationService::CanPopEnableNotificationDialog( - const sptr &callback, bool &canPop, std::string &bundleName) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - canPop = false; - ErrCode result = ERR_OK; - sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - ANS_LOGE("bundleOption is nullptr."); - return ERR_ANS_INVALID_BUNDLE; - } - // To get the permission - bool allowedNotify = false; - HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_2, EventBranchId::BRANCH_2); - message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) + - " canPop:" + std::to_string(canPop)); - result = IsAllowedNotifySelf(bundleOption, allowedNotify); - if (result != ERR_OK) { - ANS_LOGE("Not allowed Notify self."); - message.ErrorCode(result).Append(" Not Allow"); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERROR_INTERNAL_ERROR; - } - ANS_LOGI("allowedNotify = %{public}d, bundle = %{public}s", allowedNotify, - bundleOption->GetBundleName().c_str()); - if (allowedNotify) { - message.ErrorCode(ERR_OK).Append(" Allow success"); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERR_OK; - } - // Check to see if it has been popover before - bool hasPopped = false; - result = GetHasPoppedDialog(bundleOption, hasPopped); - if (result != ERR_OK) { - ANS_LOGE("Get has popped dialog failed. result: %{public}d", result); - message.ErrorCode(result).Append(" Has popped"); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERROR_INTERNAL_ERROR; - } - if (hasPopped) { - ANS_LOGE("Has popped is true."); - message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Haspopped true"); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERR_ANS_NOT_ALLOWED; - } - if (GetSystemBoolParameter(SAMPLE_MEACHINE, false)) { - return ERR_ANS_NOT_ALLOWED; - } - - if (!CreateDialogManager()) { - ANS_LOGE("Create dialog manager failed."); - message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Create dialog failed"); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERROR_INTERNAL_ERROR; - } - result = dialogManager_->AddDialogInfo(bundleOption, callback); - if (result != ERR_OK) { - ANS_LOGI("AddDialogInfo result: %{public}d", result); - message.ErrorCode(result).Append(" AddDialogInfo"); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return result; - } - - canPop = true; - bundleName = bundleOption->GetBundleName(); - ANS_LOGI("%{public}s_%{public}d, canPop: %{public}s, CanPopEnableNotificationDialog result: %{public}d", - bundleOption->GetBundleName().c_str(), bundleOption->GetUid(), std::to_string(canPop).c_str(), result); - message.ErrorCode(result).Append(" CanPopEnableNotificationDialog end"); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERR_OK; -} - -ErrCode AdvancedNotificationService::RemoveEnableNotificationDialog() -{ - ANS_LOGD("%{public}s", __FUNCTION__); - ErrCode result = ERR_OK; - sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - ANS_LOGE("bundleOption == nullptr"); - return ERR_ANS_INVALID_BUNDLE; - } - return RemoveEnableNotificationDialog(bundleOption); -} - -ErrCode AdvancedNotificationService::RemoveEnableNotificationDialog(const sptr &bundleOption) -{ - ANS_LOGI("RemoveEnableNotificationDialog %{public}s, %{public}d", - bundleOption->GetBundleName().c_str(), - bundleOption->GetUid()); - if (!CreateDialogManager()) { - return ERROR_INTERNAL_ERROR; - } - std::unique_ptr dialogInfoRemoved = nullptr; - dialogManager_->RemoveDialogInfoByBundleOption(bundleOption, dialogInfoRemoved); - return ERR_OK; -} - -ErrCode AdvancedNotificationService::IsAllowedNotifySelf(const sptr &bundleOption, - bool &allowed) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - if (bundleOption == nullptr) { - return ERR_ANS_INVALID_BUNDLE; - } - - int32_t userId = SUBSCRIBE_USER_INIT; - if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { - ANS_LOGD("GetActiveUserId is false"); - return ERR_ANS_GET_ACTIVE_USER_FAILED; - } - - ErrCode result = ERR_OK; - allowed = false; - result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed); - if (result == ERR_OK && allowed) { - result = NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundleOption, allowed); - if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { - result = ERR_OK; - // FA model app can publish notification without user confirm - allowed = CheckApiCompatibility(bundleOption); - SetDefaultNotificationEnabled(bundleOption, allowed); - } - } - return result; -} - ErrCode AdvancedNotificationService::IsAllowedNotifyForBundle(const sptr &bundleOption, bool &allowed) { @@ -1338,57 +921,6 @@ ErrCode AdvancedNotificationService::IsAllowedNotifyForBundle(const sptr &bundleOption, bool &allowed) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - ANS_LOGE("Not system application"); - return ERR_ANS_NON_SYSTEM_APP; - } - - int32_t callingUid = IPCSkeleton::GetCallingUid(); - if (callingUid != ANS_UID && !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - return ERR_ANS_PERMISSION_DENIED; - } - - sptr targetBundle = nullptr; - if (isSubsystem) { - if (bundleOption != nullptr) { - targetBundle = GenerateValidBundleOption(bundleOption); - } - } else { - ErrCode result = GetAppTargetBundle(bundleOption, targetBundle); - if (result != ERR_OK) { - return result; - } - } - - if (targetBundle == nullptr) { - return ERR_ANS_INVALID_BUNDLE; - } - - int32_t userId = SUBSCRIBE_USER_INIT; - if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { - return ERR_ANS_GET_ACTIVE_USER_FAILED; - } - - ErrCode result = ERR_OK; - allowed = false; - result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed); - if (result == ERR_OK && allowed) { - result = NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(targetBundle, allowed); - if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { - result = ERR_OK; - allowed = CheckApiCompatibility(targetBundle); - SetNotificationsEnabledForSpecialBundle("", bundleOption, allowed); - } - } - return result; -} - ErrCode AdvancedNotificationService::PublishContinuousTaskNotification(const sptr &request) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -2279,60 +1811,6 @@ ErrCode AdvancedNotificationService::RemoveNotificationFromRecordList( return result; } -ErrCode AdvancedNotificationService::IsSpecialUserAllowedNotify(int32_t userId, bool &allowed) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - ANS_LOGD("Failed to checkPermission"); - return ERR_ANS_PERMISSION_DENIED; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("NotificationSvrQueue_ is nullptr."); - return ERR_ANS_INVALID_PARAM; - } - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - allowed = false; - result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed); - })); - notificationSvrQueue_->wait(handler); - return result; -} - -ErrCode AdvancedNotificationService::SetNotificationsEnabledByUser(int32_t userId, bool enabled) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - 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; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is ineffectiveness."); - return ERR_ANS_INVALID_PARAM; - } - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - result = NotificationPreferences::GetInstance()->SetNotificationsEnabled(userId, enabled); - })); - notificationSvrQueue_->wait(handler); - return result; -} - void AdvancedNotificationService::UpdateUnifiedGroupInfo(const std::string &key, std::shared_ptr &groupInfo) { diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 8d4967b380aa48e3d96960e774957348e7d9365f..fb0ffd5cc80e4c760146473721b92ac8901fd6b8 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1264,11 +1264,6 @@ ErrCode AdvancedNotificationService::GetActiveNotificationNums(uint64_t &num) return ERR_OK; } -ErrCode AdvancedNotificationService::CanPublishAsBundle(const std::string &representativeBundle, bool &canPublish) -{ - return ERR_INVALID_OPERATION; -} - ErrCode AdvancedNotificationService::GetBundleImportance(int32_t &importance) { ANS_LOGD("%{public}s", __FUNCTION__); diff --git a/services/ans/src/advanced_notification_slot_service.cpp b/services/ans/src/advanced_notification_slot_service.cpp index d092822f1a23eeb85225e551a7f03fd32d3ef34c..139234002fa1069e8712d594faa5a44b561a9e77 100644 --- a/services/ans/src/advanced_notification_slot_service.cpp +++ b/services/ans/src/advanced_notification_slot_service.cpp @@ -921,36 +921,6 @@ ErrCode AdvancedNotificationService::GetEnabledForBundleSlot( return result; } -ErrCode AdvancedNotificationService::GetAllNotificationEnabledBundles( - std::vector &bundleOption) -{ - ANS_LOGD("Called."); - if (!AccessTokenHelper::IsSystemApp()) { - ANS_LOGE("Is not system app."); - return ERR_ANS_NON_SYSTEM_APP; - } - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - ANS_LOGE("Permission denied."); - return ERR_ANS_PERMISSION_DENIED; - } - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is invalid."); - return ERR_ANS_INVALID_PARAM; - } - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { - ANS_LOGD("ffrt enter!"); - result = NotificationPreferences::GetInstance()->GetAllNotificationEnabledBundles(bundleOption); - if (result != ERR_OK) { - ANS_LOGE("Get all notification enable status failed"); - return; - } - })); - notificationSvrQueue_->wait(handler); - - return result; -} - ErrCode AdvancedNotificationService::GetAllLiveViewEnabledBundles( std::vector &bundleOption) { diff --git a/services/ans/src/enable_manager/enable_manager.cpp b/services/ans/src/enable_manager/enable_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..010a85219571cf397fae68a9836417f7bb341435 --- /dev/null +++ b/services/ans/src/enable_manager/enable_manager.cpp @@ -0,0 +1,597 @@ +/* + * Copyright (c) 2025 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 "advanced_notification_service.h" + +#include "accesstoken_kit.h" +#include "access_token_helper.h" +#include "ans_const_define.h" +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" +#include "ans_permission_def.h" + +#include "bundle_manager_helper.h" +#include "hitrace_meter_adapter.h" +#include "ipc_skeleton.h" + +#include "notification_preferences.h" +#include "notification_bundle_option.h" +#include "notification_analytics_util.h" +#include "os_account_manager_helper.h" + +namespace OHOS { +namespace Notification { + +constexpr int32_t ANS_USERID = 5523; +const static std::string BUNDLE_NAME_ZYT = "com.zhuoyi.appstore.lite"; +const static std::string BUNDLE_NAME_ABROAD = "com.easy.abroad"; +const static std::string INSTALL_SOURCE_EASYABROAD = "com.easy.abroad"; +constexpr int32_t ZERO_USER_ID = 0; +constexpr const char *SAMPLE_MEACHINE = "const.dfx.enable_retail"; + +ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId, + const sptr &callback) +{ + return RequestEnableNotification(deviceId, callback, nullptr); +} + +ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId, + const sptr &callback, + const sptr &callerToken) +{ + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_13, EventBranchId::BRANCH_4); + message.Message(" de:" + deviceId); + ANS_LOGD("%{public}s", __FUNCTION__); + if (callback == nullptr) { + ANS_LOGE("callback == nullptr"); + message.ErrorCode(ERR_ANS_INVALID_PARAM); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERR_ANS_INVALID_PARAM; + } + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + ANS_LOGE("bundleOption is nullptr."); + return ERROR_INTERNAL_ERROR; + } + return CommonRequestEnableNotification(deviceId, callback, callerToken, bundleOption, false, false); +} + +ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string& bundleName, int32_t uid) +{ + ANS_LOGI("RequestEnableNotification bundleName = %{public}s uid = %{public}d", bundleName.c_str(), uid); + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + if (bundleName == BUNDLE_NAME_ZYT || bundleName == BUNDLE_NAME_ABROAD) { + ANS_LOGI("RequestEnableNotification zyt or abroad"); + return ERR_ANS_NOT_ALLOWED; + } + + AppExecFwk::BundleInfo bundleInfo; + bool ret = BundleManagerHelper::GetInstance()->GetBundleInfoV9(bundleName, + static_cast(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION), + bundleInfo, ZERO_USER_ID); + bool easyAbroad = false; + if (bundleInfo.applicationInfo.installSource == INSTALL_SOURCE_EASYABROAD) { + ANS_LOGI("RequestEnableNotification abroad app"); + easyAbroad = true; + } + sptr bundleOption = new (std::nothrow) NotificationBundleOption(bundleName, uid); + if (bundleOption == nullptr) { + ANS_LOGE("bundleOption is nullptr."); + return ERROR_INTERNAL_ERROR; + } + return CommonRequestEnableNotification("", nullptr, nullptr, bundleOption, true, easyAbroad); +} + +ErrCode AdvancedNotificationService::CommonRequestEnableNotification(const std::string &deviceId, + const sptr &callback, + const sptr &callerToken, + const sptr bundleOption, + const bool innerLake, + const bool easyAbroad) +{ + ANS_LOGI("%{public}s", __FUNCTION__); + ErrCode result = ERR_OK; + // To get the permission + bool allowedNotify = false; + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_8, EventBranchId::BRANCH_5); + if (bundleOption == nullptr) { + ANS_LOGE("bundleOption is nullptr."); + return ERROR_INTERNAL_ERROR; + } + message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) + + " deviceId:" + deviceId); + result = IsAllowedNotifySelf(bundleOption, allowedNotify); + if (result != ERR_OK) { + ANS_LOGE("Not allowed notify self"); + message.ErrorCode(result).Append(" Allow failed"); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERROR_INTERNAL_ERROR; + } + ANS_LOGI("allowedNotify = %{public}d, bundle = %{public}s", allowedNotify, + bundleOption->GetBundleName().c_str()); + if (allowedNotify) { + message.ErrorCode(ERR_OK).Append(" Allow success"); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERR_OK; + } + // Check to see if it has been popover before + bool hasPopped = false; + result = GetHasPoppedDialog(bundleOption, hasPopped); + if (result != ERR_OK) { + ANS_LOGE("Get has popped dialog failed."); + message.ErrorCode(result).Append(" Get dialog failed."); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERROR_INTERNAL_ERROR; + } + if (hasPopped) { + ANS_LOGE("Has popped is true."); + message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Has popped"); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERR_ANS_NOT_ALLOWED; + } + if (GetSystemBoolParameter(SAMPLE_MEACHINE, false)) { + return ERR_ANS_NOT_ALLOWED; + } + + if (!CreateDialogManager()) { + ANS_LOGE("Create dialog manager failed."); + message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Create dialog failed"); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERROR_INTERNAL_ERROR; + } + + result = dialogManager_->RequestEnableNotificationDailog(bundleOption, + callback, callerToken, innerLake, easyAbroad); + if (result == ERR_OK) { + result = ERR_ANS_DIALOG_POP_SUCCEEDED; + } + + ANS_LOGI("%{public}s_%{public}d, deviceId: %{public}s, Request enable notification dailog result: %{public}d", + bundleOption->GetBundleName().c_str(), bundleOption->GetUid(), deviceId.c_str(), result); + message.ErrorCode(result); + if (!innerLake || result == ERR_ANS_DIALOG_POP_SUCCEEDED) { + NotificationAnalyticsUtil::ReportModifyEvent(message); + } + return result; +} + +ErrCode AdvancedNotificationService::SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) +{ + return ERR_INVALID_OPERATION; +} + +ErrCode AdvancedNotificationService::SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGD("VerifyNativeToken and IsSystemApp is false."); + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + int32_t userId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalidity."); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + if (deviceId.empty()) { + // Local device + result = NotificationPreferences::GetInstance()->SetNotificationsEnabled(userId, enabled); + } else { + // Remote device + } + })); + notificationSvrQueue_->wait(handler); + return result; +} + +ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundle( + const std::string &deviceId, const sptr &bundleOption, bool enabled) +{ + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_13, EventBranchId::BRANCH_5); + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + ANS_LOGD("%{public}s", __FUNCTION__); + if (bundleOption == nullptr) { + ANS_LOGE("BundleOption is null."); + message.ErrorCode(ERR_ANS_INVALID_BUNDLE); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERR_ANS_INVALID_BUNDLE; + } + + message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) + + " en:" + std::to_string(enabled) + + " dId:" + deviceId); + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGE("IsSystemApp is false."); + message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).BranchId(BRANCH_6); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERR_ANS_NON_SYSTEM_APP; + } + + int32_t callingUid = IPCSkeleton::GetCallingUid(); + if (callingUid != ANS_USERID && !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGE("Permission Denied."); + message.ErrorCode(ERR_ANS_PERMISSION_DENIED).BranchId(BRANCH_7); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERR_ANS_PERMISSION_DENIED; + } + + sptr bundle = GenerateValidBundleOption(bundleOption); + if (bundle == nullptr) { + message.ErrorCode(ERR_ANS_INVALID_BUNDLE).BranchId(BRANCH_8); + NotificationAnalyticsUtil::ReportModifyEvent(message); + ANS_LOGE(" Bundle is nullptr."); + return ERR_ANS_INVALID_BUNDLE; + } + + sptr bundleData = new (std::nothrow) + EnabledNotificationCallbackData(bundle->GetBundleName(), bundle->GetUid(), enabled); + if (bundleData == nullptr) { + ANS_LOGE("Failed to create EnabledNotificationCallbackData instance"); + return ERR_NO_MEMORY; + } + + ErrCode result = ERR_OK; + if (deviceId.empty()) { + // Local device + result = NotificationPreferences::GetInstance()->SetNotificationsEnabledForBundle(bundle, enabled); + if (result == ERR_OK) { + if (!enabled) { + result = RemoveAllNotificationsForDisable(bundle); + } + SetSlotFlagsTrustlistsAsBundle(bundle); + NotificationSubscriberManager::GetInstance()->NotifyEnabledNotificationChanged(bundleData); + PublishSlotChangeCommonEvent(bundle); + } + } else { + // Remote device + } + + ANS_LOGI("%{public}s_%{public}d, deviceId: %{public}s, enable: %{public}s, " + "Set notifications enabled for special bundle result: %{public}d", bundleOption->GetBundleName().c_str(), + bundleOption->GetUid(), deviceId.c_str(), std::to_string(enabled).c_str(), result); + message.ErrorCode(result).BranchId(BRANCH_9); + NotificationAnalyticsUtil::ReportModifyEvent(message); + SendEnableNotificationHiSysEvent(bundleOption, enabled, result); + return result; +} + +ErrCode AdvancedNotificationService::CanPopEnableNotificationDialog( + const sptr &callback, bool &canPop, std::string &bundleName) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + canPop = false; + ErrCode result = ERR_OK; + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + ANS_LOGE("bundleOption is nullptr."); + return ERR_ANS_INVALID_BUNDLE; + } + // To get the permission + bool allowedNotify = false; + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_2, EventBranchId::BRANCH_2); + message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) + + " canPop:" + std::to_string(canPop)); + result = IsAllowedNotifySelf(bundleOption, allowedNotify); + if (result != ERR_OK) { + ANS_LOGE("Not allowed Notify self."); + message.ErrorCode(result).Append(" Not Allow"); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERROR_INTERNAL_ERROR; + } + ANS_LOGI("allowedNotify = %{public}d, bundle = %{public}s", allowedNotify, + bundleOption->GetBundleName().c_str()); + if (allowedNotify) { + message.ErrorCode(ERR_OK).Append(" Allow success"); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERR_OK; + } + // Check to see if it has been popover before + bool hasPopped = false; + result = GetHasPoppedDialog(bundleOption, hasPopped); + if (result != ERR_OK) { + ANS_LOGE("Get has popped dialog failed. result: %{public}d", result); + message.ErrorCode(result).Append(" Has popped"); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERROR_INTERNAL_ERROR; + } + if (hasPopped) { + ANS_LOGE("Has popped is true."); + message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Haspopped true"); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERR_ANS_NOT_ALLOWED; + } + if (GetSystemBoolParameter(SAMPLE_MEACHINE, false)) { + return ERR_ANS_NOT_ALLOWED; + } + + if (!CreateDialogManager()) { + ANS_LOGE("Create dialog manager failed."); + message.ErrorCode(ERR_ANS_NOT_ALLOWED).Append(" Create dialog failed"); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERROR_INTERNAL_ERROR; + } + result = dialogManager_->AddDialogInfo(bundleOption, callback); + if (result != ERR_OK) { + ANS_LOGI("AddDialogInfo result: %{public}d", result); + message.ErrorCode(result).Append(" AddDialogInfo"); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return result; + } + + canPop = true; + bundleName = bundleOption->GetBundleName(); + ANS_LOGI("%{public}s_%{public}d, canPop: %{public}s, CanPopEnableNotificationDialog result: %{public}d", + bundleOption->GetBundleName().c_str(), bundleOption->GetUid(), std::to_string(canPop).c_str(), result); + message.ErrorCode(result).Append(" CanPopEnableNotificationDialog end"); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERR_OK; +} + +ErrCode AdvancedNotificationService::RemoveEnableNotificationDialog() +{ + ANS_LOGD("%{public}s", __FUNCTION__); + ErrCode result = ERR_OK; + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + ANS_LOGE("bundleOption == nullptr"); + return ERR_ANS_INVALID_BUNDLE; + } + return RemoveEnableNotificationDialog(bundleOption); +} + +ErrCode AdvancedNotificationService::RemoveEnableNotificationDialog(const sptr &bundleOption) +{ + ANS_LOGI("RemoveEnableNotificationDialog %{public}s, %{public}d", + bundleOption->GetBundleName().c_str(), + bundleOption->GetUid()); + if (!CreateDialogManager()) { + return ERROR_INTERNAL_ERROR; + } + std::unique_ptr dialogInfoRemoved = nullptr; + dialogManager_->RemoveDialogInfoByBundleOption(bundleOption, dialogInfoRemoved); + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetAllNotificationEnabledBundles( + std::vector &bundleOption) +{ + ANS_LOGD("Called."); + if (!AccessTokenHelper::IsSystemApp()) { + ANS_LOGE("Is not system app."); + return ERR_ANS_NON_SYSTEM_APP; + } + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGE("Permission denied."); + return ERR_ANS_PERMISSION_DENIED; + } + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + result = NotificationPreferences::GetInstance()->GetAllNotificationEnabledBundles(bundleOption); + if (result != ERR_OK) { + ANS_LOGE("Get all notification enable status failed"); + return; + } + })); + notificationSvrQueue_->wait(handler); + + return result; +} + +ErrCode AdvancedNotificationService::SetNotificationsEnabledByUser(int32_t userId, bool enabled) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + 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; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is ineffectiveness."); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + result = NotificationPreferences::GetInstance()->SetNotificationsEnabled(userId, enabled); + })); + notificationSvrQueue_->wait(handler); + return result; +} + +ErrCode AdvancedNotificationService::IsAllowedNotify(bool &allowed) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGD("AccessTokenHelper::CheckPermission is false"); + return ERR_ANS_PERMISSION_DENIED; + } + + int32_t userId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + allowed = false; + result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed); + })); + notificationSvrQueue_->wait(handler); + return result; +} + +ErrCode AdvancedNotificationService::IsAllowedNotifySelf(bool &allowed) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + return IsAllowedNotifySelf(bundleOption, allowed); +} + +ErrCode AdvancedNotificationService::IsAllowedNotifySelf(const sptr &bundleOption, + bool &allowed) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + + int32_t userId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { + ANS_LOGD("GetActiveUserId is false"); + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + + ErrCode result = ERR_OK; + allowed = false; + result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed); + if (result == ERR_OK && allowed) { + result = NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(bundleOption, allowed); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + // FA model app can publish notification without user confirm + allowed = CheckApiCompatibility(bundleOption); + SetDefaultNotificationEnabled(bundleOption, allowed); + } + } + return result; +} + +ErrCode AdvancedNotificationService::IsSpecialUserAllowedNotify(int32_t userId, bool &allowed) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGD("Failed to checkPermission"); + return ERR_ANS_PERMISSION_DENIED; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("NotificationSvrQueue_ is nullptr."); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + ANS_LOGD("ffrt enter!"); + allowed = false; + result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed); + })); + notificationSvrQueue_->wait(handler); + return result; +} + +ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify( + const sptr &bundleOption, bool &allowed) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGE("Not system application"); + return ERR_ANS_NON_SYSTEM_APP; + } + + int32_t callingUid = IPCSkeleton::GetCallingUid(); + if (callingUid != ANS_USERID && !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + sptr targetBundle = nullptr; + if (isSubsystem) { + if (bundleOption != nullptr) { + targetBundle = GenerateValidBundleOption(bundleOption); + } + } else { + ErrCode result = GetAppTargetBundle(bundleOption, targetBundle); + if (result != ERR_OK) { + return result; + } + } + + if (targetBundle == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + + int32_t userId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId) != ERR_OK) { + return ERR_ANS_GET_ACTIVE_USER_FAILED; + } + + ErrCode result = ERR_OK; + allowed = false; + result = NotificationPreferences::GetInstance()->GetNotificationsEnabled(userId, allowed); + if (result == ERR_OK && allowed) { + result = NotificationPreferences::GetInstance()->GetNotificationsEnabledForBundle(targetBundle, allowed); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + allowed = CheckApiCompatibility(targetBundle); + SetNotificationsEnabledForSpecialBundle("", bundleOption, allowed); + } + } + return result; +} + +ErrCode AdvancedNotificationService::CanPublishAsBundle(const std::string &representativeBundle, bool &canPublish) +{ + return ERR_INVALID_OPERATION; +} + +} // Notification +} // OHOS