From bd1aa3a7fc9893b7845f6b1afe5f27973e2412f1 Mon Sep 17 00:00:00 2001 From: "yangjun (CI)" Date: Wed, 21 May 2025 15:17:13 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=8B=86=E5=88=86=E5=A4=A7=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E5=B0=86=E8=A7=92=E6=A0=87=E7=9B=B8=E5=85=B3=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8B=86=E5=88=86=E5=88=B0=E7=8B=AC=E7=AB=8B?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=AE=A1=E7=90=86=202.=E5=B0=86include=20.cp?= =?UTF-8?q?p=E6=9B=BF=E6=8D=A2=E6=88=90include=20.h=EF=BC=8C=E4=B8=BA?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=86=B2=E7=AA=81=EF=BC=8C=E6=9A=82=E6=97=B6?= =?UTF-8?q?=E4=BF=9D=E7=95=99=E5=8E=9Fcpp=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangjun --- services/ans/BUILD.gn | 1 + .../include/advanced_notification_inline.h | 148 ++++++++ .../advanced_notification_event_service.cpp | 2 +- ...dvanced_notification_live_view_service.cpp | 2 +- .../live_publish_process.cpp | 2 +- .../advanced_notification_publish_service.cpp | 292 +--------------- ...advanced_notification_reminder_service.cpp | 2 +- .../ans/src/advanced_notification_service.cpp | 2 +- .../advanced_notification_slot_service.cpp | 2 +- ...vanced_notification_subscriber_service.cpp | 2 +- .../ans/src/advanced_notification_utils.cpp | 2 +- .../ans/src/badge_manager/badge_manager.cpp | 324 ++++++++++++++++++ .../add_do_not_disturb_profiles.cpp | 2 +- .../get_do_not_disturb_date.cpp | 2 +- .../get_do_not_disturb_profile.cpp | 2 +- .../set_do_not_disturb_date.cpp | 2 +- .../src/notification_subscriber_manager.cpp | 2 +- 17 files changed, 487 insertions(+), 304 deletions(-) create mode 100644 services/ans/include/advanced_notification_inline.h create mode 100644 services/ans/src/badge_manager/badge_manager.cpp diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index 4b98f9caa..ec0c7506a 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -82,6 +82,7 @@ ohos_source_set("ans_service_sources") { "src/advanced_notification_slot_service.cpp", "src/advanced_notification_subscriber_service.cpp", "src/advanced_notification_utils.cpp", + "src/badge_manager/badge_manager.cpp", "src/bundle_manager_helper.cpp", "src/clone/dh_notification_clone_bundle_service.cpp", "src/clone/notification_clone_bundle_info.cpp", diff --git a/services/ans/include/advanced_notification_inline.h b/services/ans/include/advanced_notification_inline.h new file mode 100644 index 000000000..6658d1d12 --- /dev/null +++ b/services/ans/include/advanced_notification_inline.h @@ -0,0 +1,148 @@ +/* + * 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. + */ + +#ifndef NOTIFICATION_ADVANCED_NOTIFICATION_INLINE_H +#define NOTIFICATION_ADVANCED_NOTIFICATION_INLINE_H + +#include "ans_log_wrapper.h" +#include "access_token_helper.h" +#include "ans_permission_def.h" +#include "bundle_manager_helper.h" +#include "ipc_skeleton.h" +#include "os_account_manager_helper.h" +#include "notification_preferences.h" +#include "notification_analytics_util.h" + +namespace OHOS { +namespace Notification { +inline std::string GetClientBundleNameByUid(int32_t callingUid) +{ + std::string bundle; + + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager != nullptr) { + bundle = bundleManager->GetBundleNameByUid(callingUid); + } + + return bundle; +} + +inline std::string GetClientBundleName() +{ + int32_t callingUid = IPCSkeleton::GetCallingUid(); + return GetClientBundleNameByUid(callingUid); +} + +inline int32_t CheckUserIdParams(const int userId) +{ + if (userId != SUBSCRIBE_USER_INIT && !OsAccountManagerHelper::GetInstance().CheckUserExists(userId)) { + return ERROR_USER_NOT_EXIST; + } + return ERR_OK; +} + +inline int64_t ResetSeconds(int64_t date) +{ + auto milliseconds = std::chrono::milliseconds(date); + auto tp = std::chrono::time_point(milliseconds); + auto tp_minutes = std::chrono::time_point_cast(tp); + auto duration = std::chrono::duration_cast(tp_minutes.time_since_epoch()); + return duration.count(); +} + +inline int64_t GetCurrentTime() +{ + auto now = std::chrono::system_clock::now(); + auto duration = std::chrono::duration_cast(now.time_since_epoch()); + return duration.count(); +} + +inline tm GetLocalTime(time_t time) +{ + struct tm ret = {0}; + localtime_r(&time, &ret); + return ret; +} + +inline ErrCode CheckPictureSize(const sptr &request) +{ + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_1, EventBranchId::BRANCH_1); + auto result = request->CheckImageSizeForContent(); + if (result != ERR_OK) { + ANS_LOGE("Check image size failed."); + message.ErrorCode(result).Message("Check image size failed."); + NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message); + return result; + } + + if (request->CheckImageOverSizeForPixelMap(request->GetLittleIcon(), MAX_ICON_SIZE)) { + message.ErrorCode(ERR_ANS_ICON_OVER_SIZE).Message("Check little image size failed."); + NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message); + return ERR_ANS_ICON_OVER_SIZE; + } + + if (request->CheckImageOverSizeForPixelMap(request->GetOverlayIcon(), MAX_ICON_SIZE)) { + message.ErrorCode(ERR_ANS_ICON_OVER_SIZE).Message("Check overlay size failed."); + NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message); + return ERR_ANS_ICON_OVER_SIZE; + } + + if (request->CheckImageOverSizeForPixelMap(request->GetBigIcon(), MAX_ICON_SIZE)) { + request->ResetBigIcon(); + ANS_LOGI("Check big image size over limit"); + } + + return ERR_OK; +} + +inline OHOS::Notification::HaMetaMessage AddInformationInMessage( + OHOS::Notification::HaMetaMessage haMetaMessage, const int32_t reason, + std::string message) +{ + message += "reason:" + std::to_string(reason) + "."; + + std::string bundleName; + int32_t callingUid = IPCSkeleton::GetCallingUid(); + message += "uid:" + std::to_string(callingUid) + "."; + bundleName = GetClientBundleNameByUid(callingUid); + + haMetaMessage = haMetaMessage.AgentBundleName(bundleName); + haMetaMessage = haMetaMessage.Message(message); + return haMetaMessage; +} + + +inline void ReportDeleteFailedEventPush(OHOS::Notification::HaMetaMessage haMetaMessage, + const int32_t reason, std::string message) +{ + haMetaMessage = AddInformationInMessage(haMetaMessage, reason, message); + NotificationAnalyticsUtil::ReportDeleteFailedEvent(haMetaMessage); +} + +inline void ReportDeleteFailedEventPushByNotification(const sptr ¬ification, + OHOS::Notification::HaMetaMessage haMetaMessage, const int32_t reason, + std::string message) +{ + if (notification == nullptr) { + ANS_LOGW("report notificaiton is null"); + return; + } + haMetaMessage = AddInformationInMessage(haMetaMessage, reason, message); + NotificationAnalyticsUtil::ReportDeleteFailedEvent( + notification->GetNotificationRequestPoint(), haMetaMessage); +} +} // namespace Notification +} // namespace OHOS +#endif // NOTIFICATION_ADVANCED_NOTIFICATION_INLINE_H \ No newline at end of file diff --git a/services/ans/src/advanced_notification_event_service.cpp b/services/ans/src/advanced_notification_event_service.cpp index 90379426a..93836412c 100644 --- a/services/ans/src/advanced_notification_event_service.cpp +++ b/services/ans/src/advanced_notification_event_service.cpp @@ -33,7 +33,7 @@ #include "hitrace_meter_adapter.h" #include "ipc_skeleton.h" -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" namespace OHOS { namespace Notification { diff --git a/services/ans/src/advanced_notification_live_view_service.cpp b/services/ans/src/advanced_notification_live_view_service.cpp index 5366181e6..83c712360 100644 --- a/services/ans/src/advanced_notification_live_view_service.cpp +++ b/services/ans/src/advanced_notification_live_view_service.cpp @@ -30,7 +30,7 @@ #include "os_account_manager_helper.h" #include "time_service_client.h" #include "notification_timer_info.h" -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" #include #include #include "notification_analytics_util.h" diff --git a/services/ans/src/advanced_notification_publish/live_publish_process.cpp b/services/ans/src/advanced_notification_publish/live_publish_process.cpp index 47218f284..18bd8d688 100644 --- a/services/ans/src/advanced_notification_publish/live_publish_process.cpp +++ b/services/ans/src/advanced_notification_publish/live_publish_process.cpp @@ -25,7 +25,7 @@ #include "os_account_manager_helper.h" #include "ans_status.h" -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" namespace OHOS { namespace Notification { diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index 8f7556ddb..4b749402d 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -44,7 +44,7 @@ #include "ans_convert_enum.h" #include "notification_analytics_util.h" -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" #include "notification_analytics_util.h" #include "advanced_datashare_helper.h" #include "advanced_datashare_helper_ext.h" @@ -78,7 +78,6 @@ 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 BADGE_NUM_LIMIT = 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"; @@ -702,30 +701,6 @@ ErrCode AdvancedNotificationService::PublishAsBundleWithMaxCapacity( return PublishAsBundle(notification, representativeBundle); } -ErrCode AdvancedNotificationService::SetNotificationBadgeNum(int32_t num) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - ANS_LOGD("BundleOption is null."); - return ERR_ANS_INVALID_BUNDLE; - } - - 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!"); - result = NotificationPreferences::GetInstance()->SetTotalBadgeNums(bundleOption, num); - })); - notificationSvrQueue_->wait(handler); - return result; -} - ErrCode AdvancedNotificationService::Delete(const std::string &key, int32_t removeReason) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -923,138 +898,6 @@ ErrCode AdvancedNotificationService::DeleteAll() return result; } -ErrCode AdvancedNotificationService::SetShowBadgeEnabledForBundle( - const sptr &bundleOption, bool enabled) -{ - HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_13, EventBranchId::BRANCH_0); - if (bundleOption == nullptr) { - ANS_LOGE("BundleOption is null."); - NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(ERR_ANS_INVALID_BUNDLE)); - return ERR_ANS_INVALID_BUNDLE; - } - - message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) + - " en" + std::to_string(enabled)); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - ANS_LOGE("IsSystemApp is false."); - message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).BranchId(BRANCH_1); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - ANS_LOGE("Permission Denied."); - message.ErrorCode(ERR_ANS_PERMISSION_DENIED).BranchId(BRANCH_2); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return ERR_ANS_PERMISSION_DENIED; - } - - sptr bundle = GenerateValidBundleOption(bundleOption); - if (bundle == nullptr) { - ANS_LOGE("Bundle is nullptr."); - return ERR_ANS_INVALID_BUNDLE; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("NotificationSvrQueue_ 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()->SetShowBadge(bundle, enabled); - if (result == ERR_OK) { - HandleBadgeEnabledChanged(bundle, enabled); - } - })); - notificationSvrQueue_->wait(handler); - ANS_LOGI("%{public}s_%{public}d, enabled: %{public}s, Set show badge enabled for bundle result: %{public}d", - bundleOption->GetBundleName().c_str(), bundleOption->GetUid(), std::to_string(enabled).c_str(), result); - message.ErrorCode(result).BranchId(BRANCH_3); - NotificationAnalyticsUtil::ReportModifyEvent(message); - return result; -} - -void AdvancedNotificationService::HandleBadgeEnabledChanged( - const sptr &bundleOption, bool enabled) -{ - sptr enabledData = new (std::nothrow) - EnabledNotificationCallbackData(bundleOption->GetBundleName(), bundleOption->GetUid(), enabled); - if (enabledData == nullptr) { - ANS_LOGE("Failed to create badge enabled data object."); - return; - } - - NotificationSubscriberManager::GetInstance()->NotifyBadgeEnabledChanged(enabledData); -} - -ErrCode AdvancedNotificationService::GetShowBadgeEnabledForBundle( - const sptr &bundleOption, bool &enabled) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - ANS_LOGD("VerifyNativeToken is bogus."); - return ERR_ANS_NON_SYSTEM_APP; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - return ERR_ANS_PERMISSION_DENIED; - } - - sptr bundle = GenerateValidBundleOption(bundleOption); - if (bundle == nullptr) { - ANS_LOGD("Failed to generateValidBundleOption."); - return ERR_ANS_INVALID_BUNDLE; - } - - 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()->IsShowBadge(bundle, enabled); - if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { - result = ERR_OK; - enabled = true; - } - })); - notificationSvrQueue_->wait(handler); - return result; -} - -ErrCode AdvancedNotificationService::GetShowBadgeEnabled(bool &enabled) -{ - ANS_LOGD("%{public}s", __FUNCTION__); - - sptr bundleOption = GenerateBundleOption(); - if (bundleOption == nullptr) { - return ERR_ANS_INVALID_BUNDLE; - } - - if (notificationSvrQueue_ == nullptr) { - ANS_LOGE("Serial queue is ineffective."); - 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()->IsShowBadge(bundleOption, enabled); - if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { - result = ERR_OK; - enabled = true; - } - })); - notificationSvrQueue_->wait(handler); - return result; -} - ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId, const sptr &callback) { @@ -2704,139 +2547,6 @@ ErrCode AdvancedNotificationService::PublishNotificationBySa(const sptr badgeData = new (std::nothrow) BadgeNumberCallbackData( - bundleName, instanceKey, callingUid, badgeNumber); - if (badgeData == nullptr) { - ANS_LOGE("Failed to create BadgeNumberCallbackData."); - return ERR_ANS_NO_MEMORY; - } - - ffrt::task_handle handler = notificationSvrQueue_->submit_h([&]() { - ANS_LOGD("ffrt enter!"); - NotificationSubscriberManager::GetInstance()->SetBadgeNumber(badgeData); - }); - notificationSvrQueue_->wait(handler); - return ERR_OK; -} - -ErrCode AdvancedNotificationService::SetBadgeNumberForDhByBundle( - const sptr &bundleOption, int32_t badgeNumber) -{ - if (bundleOption == nullptr) { - ANS_LOGE("SetBadgeNumberForDhByBundle bundleOption is null"); - return ERR_ANS_INVALID_PARAM; - } - if (bundleOption->GetBundleName().empty()) { - ANS_LOGE("SetBadgeNumberForDhByBundle Invalid bundle name."); - return ERR_ANS_INVALID_PARAM; - } - if (bundleOption->GetUid() <= DEFAULT_UID) { - ANS_LOGE("SetBadgeNumberForDhByBundle invalid uid"); - return ERR_ANS_INVALID_PARAM; - } - if (badgeNumber < BADGE_NUM_LIMIT) { - ANS_LOGE("SetBadgeNumberForDhByBundle invalid badgeNumber"); - return ERR_ANS_INVALID_PARAM; - } - ANS_LOGI("SetBadgeNumberForDhByBundle bundleName = %{public}s uid = %{public}d", - bundleOption->GetBundleName().c_str(), bundleOption->GetUid()); - HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_6); - message.Message(bundleOption->GetBundleName() + "_" +std::to_string(bundleOption->GetUid()) + - " badgeNumber: " + std::to_string(badgeNumber)); - if (notificationSvrQueue_ == nullptr) { - return ERR_ANS_INVALID_PARAM; - } - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).Append(" Not system app."); - NotificationAnalyticsUtil::ReportModifyEvent(message); - ANS_LOGE("Not system app."); - return ERR_ANS_NON_SYSTEM_APP; - } - ErrCode result = ERR_OK; - ffrt::task_handle handler = notificationSvrQueue_->submit_h([&]() { - ANS_LOGD("ffrt enter!"); - sptr badgeData = new (std::nothrow) BadgeNumberCallbackData( - bundleOption->GetBundleName(), bundleOption->GetUid(), badgeNumber); - if (badgeData == nullptr) { - ANS_LOGE("Failed to create badge number callback data."); - result = ERR_ANS_NO_MEMORY; - } - NotificationSubscriberManager::GetInstance()->SetBadgeNumber(badgeData); - }); - notificationSvrQueue_->wait(handler); - return result; -} - -ErrCode AdvancedNotificationService::SetBadgeNumberByBundle( - const sptr &bundleOption, int32_t badgeNumber) -{ - if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { - return ERR_ANS_INVALID_PARAM; - } - HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_6); - message.Message(bundleOption->GetBundleName() + "_" +std::to_string(bundleOption->GetUid()) + - " badgeNumber: " + std::to_string(badgeNumber)); - if (notificationSvrQueue_ == nullptr) { - return ERR_ANS_INVALID_PARAM; - } - - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { - message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).Append(" Not system app."); - NotificationAnalyticsUtil::ReportModifyEvent(message); - ANS_LOGE("Not system app."); - return ERR_ANS_NON_SYSTEM_APP; - } - - sptr bundle = bundleOption; - ErrCode result = CheckBundleOptionValid(bundle); - if (result != ERR_OK) { - ANS_LOGE("Bundle is invalid."); - return result; - } - - if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) { - std::string bundleName = GetClientBundleName(); - if (bundleName.empty()) { - ANS_LOGE("Failed to get client bundle name."); - return result; - } - bool isAgent = false; - isAgent = IsAgentRelationship(bundleName, bundle->GetBundleName()); - if (!isAgent) { - message.ErrorCode(ERR_ANS_NO_AGENT_SETTING).Append(" No agent setting."); - NotificationAnalyticsUtil::ReportModifyEvent(message); - ANS_LOGE("No agent setting."); - return ERR_ANS_NO_AGENT_SETTING; - } - } - - ffrt::task_handle handler = notificationSvrQueue_->submit_h([&]() { - ANS_LOGD("ffrt enter!"); - sptr badgeData = new (std::nothrow) BadgeNumberCallbackData( - bundle->GetBundleName(), bundle->GetUid(), badgeNumber); - if (badgeData == nullptr) { - ANS_LOGE("Failed to create badge number callback data."); - result = ERR_ANS_NO_MEMORY; - } - NotificationSubscriberManager::GetInstance()->SetBadgeNumber(badgeData); - }); - notificationSvrQueue_->wait(handler); - return result; -} - ErrCode AdvancedNotificationService::SubscribeLocalLiveView( const sptr &subscriber, const bool isNative) { diff --git a/services/ans/src/advanced_notification_reminder_service.cpp b/services/ans/src/advanced_notification_reminder_service.cpp index 9459af034..29ae7b59e 100644 --- a/services/ans/src/advanced_notification_reminder_service.cpp +++ b/services/ans/src/advanced_notification_reminder_service.cpp @@ -39,7 +39,7 @@ #include "distributed_screen_status_manager.h" #endif -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" namespace OHOS { namespace Notification { diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 336c43cc3..8d4967b38 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -73,7 +73,7 @@ #include "distributed_screen_status_manager.h" #endif -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" #include "advanced_datashare_helper_ext.h" #include "notification_analytics_util.h" #include "advanced_notification_flow_control_service.h" diff --git a/services/ans/src/advanced_notification_slot_service.cpp b/services/ans/src/advanced_notification_slot_service.cpp index 889fd118b..d092822f1 100644 --- a/services/ans/src/advanced_notification_slot_service.cpp +++ b/services/ans/src/advanced_notification_slot_service.cpp @@ -33,7 +33,7 @@ #include "smart_reminder_center.h" #endif -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" #include "notification_config_parse.h" #include "notification_extension_wrapper.h" #include "notification_analytics_util.h" diff --git a/services/ans/src/advanced_notification_subscriber_service.cpp b/services/ans/src/advanced_notification_subscriber_service.cpp index 64cf49ffa..a10934909 100644 --- a/services/ans/src/advanced_notification_subscriber_service.cpp +++ b/services/ans/src/advanced_notification_subscriber_service.cpp @@ -35,7 +35,7 @@ #include "distributed_screen_status_manager.h" #endif -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" #include "notification_analytics_util.h" namespace OHOS { diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index d30e15aa4..327c53182 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -47,7 +47,7 @@ #include "distributed_database.h" #endif -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" #include "notification_analytics_util.h" #include "notification_clone_disturb_service.h" #include "notification_clone_bundle_service.h" diff --git a/services/ans/src/badge_manager/badge_manager.cpp b/services/ans/src/badge_manager/badge_manager.cpp new file mode 100644 index 000000000..1d77989b0 --- /dev/null +++ b/services/ans/src/badge_manager/badge_manager.cpp @@ -0,0 +1,324 @@ +/* + * 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 "errors.h" +#include "ans_log_wrapper.h" +#include "access_token_helper.h" +#include "notification_preferences.h" +#include "notification_analytics_util.h" +#include "badge_number_callback_data.h" +#include "advanced_notification_inline.h" +#include "notification_subscriber_manager.h" +#include "enabled_notification_callback_data.h" + +#include "ipc_skeleton.h" + +namespace OHOS { +namespace Notification { + +constexpr int32_t BADGE_NUM_LIMIT = 0; + +ErrCode AdvancedNotificationService::SetNotificationBadgeNum(int32_t num) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + ANS_LOGD("BundleOption is null."); + return ERR_ANS_INVALID_BUNDLE; + } + + 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!"); + result = NotificationPreferences::GetInstance()->SetTotalBadgeNums(bundleOption, num); + })); + notificationSvrQueue_->wait(handler); + return result; +} + +ErrCode AdvancedNotificationService::SetShowBadgeEnabledForBundle( + const sptr &bundleOption, bool enabled) +{ + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_13, EventBranchId::BRANCH_0); + if (bundleOption == nullptr) { + ANS_LOGE("BundleOption is null."); + NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(ERR_ANS_INVALID_BUNDLE)); + return ERR_ANS_INVALID_BUNDLE; + } + + message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) + + " en" + std::to_string(enabled)); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGE("IsSystemApp is false."); + message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).BranchId(BRANCH_1); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGE("Permission Denied."); + message.ErrorCode(ERR_ANS_PERMISSION_DENIED).BranchId(BRANCH_2); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return ERR_ANS_PERMISSION_DENIED; + } + + sptr bundle = GenerateValidBundleOption(bundleOption); + if (bundle == nullptr) { + ANS_LOGE("Bundle is nullptr."); + return ERR_ANS_INVALID_BUNDLE; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("NotificationSvrQueue_ 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()->SetShowBadge(bundle, enabled); + if (result == ERR_OK) { + HandleBadgeEnabledChanged(bundle, enabled); + } + })); + notificationSvrQueue_->wait(handler); + ANS_LOGI("%{public}s_%{public}d, enabled: %{public}s, Set show badge enabled for bundle result: %{public}d", + bundleOption->GetBundleName().c_str(), bundleOption->GetUid(), std::to_string(enabled).c_str(), result); + message.ErrorCode(result).BranchId(BRANCH_3); + NotificationAnalyticsUtil::ReportModifyEvent(message); + return result; +} + +void AdvancedNotificationService::HandleBadgeEnabledChanged( + const sptr &bundleOption, bool enabled) +{ + sptr enabledData = new (std::nothrow) + EnabledNotificationCallbackData(bundleOption->GetBundleName(), bundleOption->GetUid(), enabled); + if (enabledData == nullptr) { + ANS_LOGE("Failed to create badge enabled data object."); + return; + } + + NotificationSubscriberManager::GetInstance()->NotifyBadgeEnabledChanged(enabledData); +} + +ErrCode AdvancedNotificationService::GetShowBadgeEnabledForBundle( + const sptr &bundleOption, bool &enabled) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGD("VerifyNativeToken is bogus."); + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + sptr bundle = GenerateValidBundleOption(bundleOption); + if (bundle == nullptr) { + ANS_LOGD("Failed to generateValidBundleOption."); + return ERR_ANS_INVALID_BUNDLE; + } + + 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()->IsShowBadge(bundle, enabled); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + enabled = true; + } + })); + notificationSvrQueue_->wait(handler); + return result; +} + +ErrCode AdvancedNotificationService::GetShowBadgeEnabled(bool &enabled) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is ineffective."); + 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()->IsShowBadge(bundleOption, enabled); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + result = ERR_OK; + enabled = true; + } + })); + notificationSvrQueue_->wait(handler); + return result; +} + +ErrCode AdvancedNotificationService::SetBadgeNumber(int32_t badgeNumber, const std::string &instanceKey) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalid."); + return ERR_ANS_INVALID_PARAM; + } + int32_t callingUid = IPCSkeleton::GetCallingUid(); + std::string bundleName = GetClientBundleName(); + ANS_LOGD("SetBadgeNumber receive instanceKey:%{public}s", instanceKey.c_str()); + sptr badgeData = new (std::nothrow) BadgeNumberCallbackData( + bundleName, instanceKey, callingUid, badgeNumber); + if (badgeData == nullptr) { + ANS_LOGE("Failed to create BadgeNumberCallbackData."); + return ERR_ANS_NO_MEMORY; + } + + ffrt::task_handle handler = notificationSvrQueue_->submit_h([&]() { + ANS_LOGD("ffrt enter!"); + NotificationSubscriberManager::GetInstance()->SetBadgeNumber(badgeData); + }); + notificationSvrQueue_->wait(handler); + return ERR_OK; +} + +ErrCode AdvancedNotificationService::SetBadgeNumberForDhByBundle( + const sptr &bundleOption, int32_t badgeNumber) +{ + if (bundleOption == nullptr) { + ANS_LOGE("SetBadgeNumberForDhByBundle bundleOption is null"); + return ERR_ANS_INVALID_PARAM; + } + if (bundleOption->GetBundleName().empty()) { + ANS_LOGE("SetBadgeNumberForDhByBundle Invalid bundle name."); + return ERR_ANS_INVALID_PARAM; + } + if (bundleOption->GetUid() <= DEFAULT_UID) { + ANS_LOGE("SetBadgeNumberForDhByBundle invalid uid"); + return ERR_ANS_INVALID_PARAM; + } + if (badgeNumber < BADGE_NUM_LIMIT) { + ANS_LOGE("SetBadgeNumberForDhByBundle invalid badgeNumber"); + return ERR_ANS_INVALID_PARAM; + } + ANS_LOGI("SetBadgeNumberForDhByBundle bundleName = %{public}s uid = %{public}d", + bundleOption->GetBundleName().c_str(), bundleOption->GetUid()); + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_6); + message.Message(bundleOption->GetBundleName() + "_" +std::to_string(bundleOption->GetUid()) + + " badgeNumber: " + std::to_string(badgeNumber)); + if (notificationSvrQueue_ == nullptr) { + return ERR_ANS_INVALID_PARAM; + } + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).Append(" Not system app."); + NotificationAnalyticsUtil::ReportModifyEvent(message); + ANS_LOGE("Not system app."); + return ERR_ANS_NON_SYSTEM_APP; + } + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h([&]() { + ANS_LOGD("ffrt enter!"); + sptr badgeData = new (std::nothrow) BadgeNumberCallbackData( + bundleOption->GetBundleName(), bundleOption->GetUid(), badgeNumber); + if (badgeData == nullptr) { + ANS_LOGE("Failed to create badge number callback data."); + result = ERR_ANS_NO_MEMORY; + } + NotificationSubscriberManager::GetInstance()->SetBadgeNumber(badgeData); + }); + notificationSvrQueue_->wait(handler); + return result; +} + +ErrCode AdvancedNotificationService::SetBadgeNumberByBundle( + const sptr &bundleOption, int32_t badgeNumber) +{ + if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { + return ERR_ANS_INVALID_PARAM; + } + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_7, EventBranchId::BRANCH_6); + message.Message(bundleOption->GetBundleName() + "_" +std::to_string(bundleOption->GetUid()) + + " badgeNumber: " + std::to_string(badgeNumber)); + if (notificationSvrQueue_ == nullptr) { + return ERR_ANS_INVALID_PARAM; + } + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + message.ErrorCode(ERR_ANS_NON_SYSTEM_APP).Append(" Not system app."); + NotificationAnalyticsUtil::ReportModifyEvent(message); + ANS_LOGE("Not system app."); + return ERR_ANS_NON_SYSTEM_APP; + } + + sptr bundle = bundleOption; + ErrCode result = CheckBundleOptionValid(bundle); + if (result != ERR_OK) { + ANS_LOGE("Bundle is invalid."); + return result; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) { + std::string bundleName = GetClientBundleName(); + if (bundleName.empty()) { + ANS_LOGE("Failed to get client bundle name."); + return result; + } + bool isAgent = false; + isAgent = IsAgentRelationship(bundleName, bundle->GetBundleName()); + if (!isAgent) { + message.ErrorCode(ERR_ANS_NO_AGENT_SETTING).Append(" No agent setting."); + NotificationAnalyticsUtil::ReportModifyEvent(message); + ANS_LOGE("No agent setting."); + return ERR_ANS_NO_AGENT_SETTING; + } + } + + ffrt::task_handle handler = notificationSvrQueue_->submit_h([&]() { + ANS_LOGD("ffrt enter!"); + sptr badgeData = new (std::nothrow) BadgeNumberCallbackData( + bundle->GetBundleName(), bundle->GetUid(), badgeNumber); + if (badgeData == nullptr) { + ANS_LOGE("Failed to create badge number callback data."); + result = ERR_ANS_NO_MEMORY; + } + NotificationSubscriberManager::GetInstance()->SetBadgeNumber(badgeData); + }); + notificationSvrQueue_->wait(handler); + return result; +} +} // Notification +} // OHOS \ No newline at end of file diff --git a/services/ans/src/disturb_manager/add_do_not_disturb_profiles.cpp b/services/ans/src/disturb_manager/add_do_not_disturb_profiles.cpp index feef26117..004da946b 100644 --- a/services/ans/src/disturb_manager/add_do_not_disturb_profiles.cpp +++ b/services/ans/src/disturb_manager/add_do_not_disturb_profiles.cpp @@ -23,7 +23,7 @@ #include "notification_preferences.h" #include "os_account_manager_helper.h" -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" namespace OHOS { namespace Notification { ErrCode DisturbManager::HandleAddDoNotDisturbProfiles(MessageParcel &data, MessageParcel &reply) diff --git a/services/ans/src/disturb_manager/get_do_not_disturb_date.cpp b/services/ans/src/disturb_manager/get_do_not_disturb_date.cpp index 286584514..e09a3a637 100644 --- a/services/ans/src/disturb_manager/get_do_not_disturb_date.cpp +++ b/services/ans/src/disturb_manager/get_do_not_disturb_date.cpp @@ -23,7 +23,7 @@ #include "notification_preferences.h" #include "os_account_manager_helper.h" -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" namespace OHOS { namespace Notification { ErrCode DisturbManager::HandleGetDoNotDisturbDate(MessageParcel &data, MessageParcel &reply) diff --git a/services/ans/src/disturb_manager/get_do_not_disturb_profile.cpp b/services/ans/src/disturb_manager/get_do_not_disturb_profile.cpp index 68c9d3438..9f1db29c7 100644 --- a/services/ans/src/disturb_manager/get_do_not_disturb_profile.cpp +++ b/services/ans/src/disturb_manager/get_do_not_disturb_profile.cpp @@ -23,7 +23,7 @@ #include "notification_preferences.h" #include "os_account_manager_helper.h" -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" namespace OHOS { namespace Notification { ErrCode DisturbManager::HandleGetDoNotDisturbProfile(MessageParcel &data, MessageParcel &reply) diff --git a/services/ans/src/disturb_manager/set_do_not_disturb_date.cpp b/services/ans/src/disturb_manager/set_do_not_disturb_date.cpp index 50f300666..fb6a198f5 100644 --- a/services/ans/src/disturb_manager/set_do_not_disturb_date.cpp +++ b/services/ans/src/disturb_manager/set_do_not_disturb_date.cpp @@ -23,7 +23,7 @@ #include "notification_preferences.h" #include "os_account_manager_helper.h" -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" namespace OHOS { namespace Notification { diff --git a/services/ans/src/notification_subscriber_manager.cpp b/services/ans/src/notification_subscriber_manager.cpp index ada9bf207..01a0fb257 100644 --- a/services/ans/src/notification_subscriber_manager.cpp +++ b/services/ans/src/notification_subscriber_manager.cpp @@ -33,7 +33,7 @@ #include "advanced_notification_service.h" #include "notification_analytics_util.h" -#include "advanced_notification_inline.cpp" +#include "advanced_notification_inline.h" #include "liveview_all_scenarios_extension_wrapper.h" namespace OHOS { -- Gitee