From 128d9eda6429e5612509f82b0a626ddbd892bda0 Mon Sep 17 00:00:00 2001 From: xdongs Date: Fri, 14 Mar 2025 17:37:38 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=B8=9C=E6=B9=96=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=A7=92=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xdongs --- frameworks/ans/src/notification_helper.cpp | 6 +++ .../core/include/ans_manager_interface.h | 10 +++++ frameworks/core/include/ans_manager_proxy.h | 10 +++++ frameworks/core/include/ans_manager_stub.h | 11 +++++ frameworks/core/include/ans_notification.h | 9 ++++ ..._notification_service_ipc_interface_code.h | 1 + frameworks/core/src/ans_manager_stub.cpp | 26 +++++++++++ .../core/src/ans_manager_stub_invalid.cpp | 7 +++ frameworks/core/src/ans_notification.cpp | 22 ++++++++++ .../src/manager/ans_manager_proxy_bundle.cpp | 35 +++++++++++++++ .../ans_notification_branch_test.cpp | 6 +++ interfaces/inner_api/notification_helper.h | 9 ++++ .../include/advanced_notification_service.h | 10 +++++ .../advanced_notification_publish_service.cpp | 44 +++++++++++++++++++ .../ans/src/advanced_notification_utils.cpp | 10 ++++- 15 files changed, 214 insertions(+), 2 deletions(-) diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 0722b0a91..1e8673c2d 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -525,6 +525,12 @@ ErrCode NotificationHelper::SetBadgeNumberByBundle(const NotificationBundleOptio return DelayedSingleton::GetInstance()->SetBadgeNumberByBundle(bundleOption, badgeNumber); } +ErrCode NotificationHelper::SetBadgeNumberForDhByBundle( + const NotificationBundleOption &bundleOption, int32_t badgeNumber) +{ + return DelayedSingleton::GetInstance()->SetBadgeNumberForDhByBundle(bundleOption, badgeNumber); +} + ErrCode NotificationHelper::GetAllNotificationEnabledBundles(std::vector &bundleOption) { return DelayedSingleton::GetInstance()->GetAllNotificationEnabledBundles(bundleOption); diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index 384c34820..c2df4f160 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -837,6 +837,16 @@ public: */ virtual ErrCode SetBadgeNumberByBundle(const sptr &bundleOption, int32_t badgeNumber) = 0; + /** + * @brief Set badge number for dh by bundle. + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param badgeNumber The badge number. + * @return Returns set badge number by bundle result. + */ + virtual ErrCode SetBadgeNumberForDhByBundle( + const sptr &bundleOption, int32_t badgeNumber) = 0; + /** * @brief Obtains the number of slotFlags. * diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index 0a906f4eb..fe56c92ae 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -825,6 +825,16 @@ public: */ ErrCode SetBadgeNumberByBundle(const sptr &bundleOption, int32_t badgeNumber) override; + /** + * @brief Set badge number for dh by bundle. + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param badgeNumber The badge number. + * @return Returns set badge number by bundle result. + */ + ErrCode SetBadgeNumberForDhByBundle( + const sptr &bundleOption, int32_t badgeNumber) override; + /** * @brief Get the slotFlags of slot. * diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index 72a6b8616..36f25944f 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -827,6 +827,16 @@ public: virtual ErrCode SetBadgeNumberByBundle( const sptr &bundleOption, int32_t badgeNumber) override; + /** + * @brief Set badge number for dh by bundle. + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param badgeNumber The badge number. + * @return Returns set badge number by bundle result. + */ + virtual ErrCode SetBadgeNumberForDhByBundle( + const sptr &bundleOption, int32_t badgeNumber) override; + /** * @brief Get slotFlags. * @@ -1069,6 +1079,7 @@ private: ErrCode HandleDistributedGetEnabledWithoutApp(MessageParcel &data, MessageParcel &reply); ErrCode HandleSetBadgeNumber(MessageParcel &data, MessageParcel &reply); ErrCode HandleSetBadgeNumberByBundle(MessageParcel &data, MessageParcel &reply); + ErrCode HandleSetBadgeNumberForDhByBundle(MessageParcel &data, MessageParcel &reply); ErrCode HandleRegisterPushCallback(MessageParcel &data, MessageParcel &reply); ErrCode HandleUnregisterPushCallback(MessageParcel &data, MessageParcel &reply); ErrCode HandleSubscribeLocalLiveView(MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index be1d52d0b..f38c6270a 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -1083,6 +1083,15 @@ public: */ ErrCode SetBadgeNumberByBundle(const NotificationBundleOption &bundleOption, int32_t badgeNumber); + /** + * @brief Set badge number for dh by bundle. + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param badgeNumber The badge number. + * @return Returns set badge number by bundle result. + */ + ErrCode SetBadgeNumberForDhByBundle(const NotificationBundleOption &bundleOption, int32_t badgeNumber); + /** * @brief Obtains allow notification application list. * diff --git a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h index 2e2e68cbf..78b673993 100644 --- a/frameworks/core/include/distributed_notification_service_ipc_interface_code.h +++ b/frameworks/core/include/distributed_notification_service_ipc_interface_code.h @@ -111,6 +111,7 @@ namespace Notification { GET_SYNC_NOTIFICATION_ENABLED_WITHOUT_APP, SET_BADGE_NUMBER, SET_BADGE_NUMBER_BY_BUNDLE, + SET_BADGE_NUMBER_FOR_DH_BY_BUNDLE, REGISTER_PUSH_CALLBACK, UNREGISTER_PUSH_CALLBACK, // ans_subscriber_interface diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index a350e8f9a..ee14e4113 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -379,6 +379,10 @@ int32_t AnsManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Mess result = HandleSetBadgeNumberByBundle(data, reply); break; } + case static_cast(NotificationInterfaceCode::SET_BADGE_NUMBER_FOR_DH_BY_BUNDLE): { + result = HandleSetBadgeNumberForDhByBundle(data, reply); + break; + } case static_cast(NotificationInterfaceCode::GET_ALL_NOTIFICATION_ENABLE_STATUS): { result = HandleGetAllNotificationEnableStatus(data, reply); break; @@ -2397,6 +2401,28 @@ ErrCode AnsManagerStub::HandleSetBadgeNumberByBundle(MessageParcel &data, Messag return result; } +ErrCode AnsManagerStub::HandleSetBadgeNumberForDhByBundle(MessageParcel &data, MessageParcel &reply) +{ + ANS_LOGD("Called."); + sptr bundleOption = data.ReadParcelable(); + if (bundleOption == nullptr) { + ANS_LOGE("Read bundle option failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + int32_t badgeNumber = 0; + if (!data.ReadInt32(badgeNumber)) { + ANS_LOGE("Read badge number failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + ErrCode result = SetBadgeNumberForDhByBundle(bundleOption, badgeNumber); + if (!reply.WriteInt32(result)) { + ANS_LOGE("Write result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + return result; +} + ErrCode AnsManagerStub::HandleGetAllNotificationEnableStatus(MessageParcel &data, MessageParcel &reply) { std::vector bundleOption; diff --git a/frameworks/core/src/ans_manager_stub_invalid.cpp b/frameworks/core/src/ans_manager_stub_invalid.cpp index db13e6eb8..d67cb4859 100644 --- a/frameworks/core/src/ans_manager_stub_invalid.cpp +++ b/frameworks/core/src/ans_manager_stub_invalid.cpp @@ -595,6 +595,13 @@ ErrCode AnsManagerStub::SetBadgeNumberByBundle(const sptr &bundleOption, int32_t badgeNumber) +{ + ANS_LOGD("Called."); + return ERR_INVALID_OPERATION; +} + ErrCode AnsManagerStub::GetAllNotificationEnabledBundles(std::vector &bundleOption) { ANS_LOGE("AnsManagerStub::GetAllNotificationEnabledBundles called!"); diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index dfb2da388..23974a338 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -1830,6 +1830,28 @@ ErrCode AnsNotification::SetBadgeNumberByBundle(const NotificationBundleOption & return proxy->SetBadgeNumberByBundle(bundleInfo, badgeNumber); } +ErrCode AnsNotification::SetBadgeNumberForDhByBundle( + const NotificationBundleOption &bundleOption, int32_t badgeNumber) +{ + if (bundleOption.GetBundleName().empty()) { + ANS_LOGE("Invalid bundle name."); + return ERR_ANS_INVALID_PARAM; + } + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("Unable to connect to ANS service."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + sptr bundleInfo(new (std::nothrow) NotificationBundleOption(bundleOption)); + if (bundleInfo == nullptr) { + ANS_LOGE("Unable to create new bundle info."); + return ERR_ANS_NO_MEMORY; + } + return proxy->SetBadgeNumberForDhByBundle(bundleInfo, badgeNumber); +} + ErrCode AnsNotification::GetAllNotificationEnabledBundles(std::vector &bundleOption) { sptr proxy = GetAnsManagerProxy(); diff --git a/frameworks/core/src/manager/ans_manager_proxy_bundle.cpp b/frameworks/core/src/manager/ans_manager_proxy_bundle.cpp index 8d2c39b92..d9ac2ca4f 100644 --- a/frameworks/core/src/manager/ans_manager_proxy_bundle.cpp +++ b/frameworks/core/src/manager/ans_manager_proxy_bundle.cpp @@ -749,6 +749,41 @@ ErrCode AnsManagerProxy::SetBadgeNumberByBundle(const sptr &bundleOption, int32_t badgeNumber) +{ + if (bundleOption == nullptr) { + ANS_LOGE("Bundle is empty."); + return ERR_ANS_INVALID_PARAM; + } + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("Write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + if (!data.WriteParcelable(bundleOption)) { + ANS_LOGE("Write bundle option failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + if (!data.WriteInt32(badgeNumber)) { + ANS_LOGE("Write badge number failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = { MessageOption::TF_SYNC }; + ErrCode result = InnerTransact(NotificationInterfaceCode::SET_BADGE_NUMBER_FOR_DH_BY_BUNDLE, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("Transact error code is: %{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + if (!reply.ReadInt32(result)) { + ANS_LOGE("Read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + return result; +} + ErrCode AnsManagerProxy::GetAllNotificationEnabledBundles(std::vector &bundleOption) { ANS_LOGD("Called."); diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index 92bfd457a..20f49339b 100644 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -512,6 +512,12 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode SetBadgeNumberForDhByBundle( + const sptr& bundleOption, int32_t badgeNumber) override + { + return ERR_ANS_INVALID_PARAM; + } + ErrCode GetSlotFlagsAsBundle(const sptr& bundleOption, uint32_t &slotFlags) override { return ERR_ANS_INVALID_PARAM; diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 3625c2d79..192438b34 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -1000,6 +1000,15 @@ public: */ static ErrCode SetBadgeNumberByBundle(const NotificationBundleOption &bundleOption, int32_t badgeNumber); + /** + * @brief Set badge number for dh by bundle. + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param badgeNumber The badge number. + * @return Returns set badge number by bundle result. + */ + static ErrCode SetBadgeNumberForDhByBundle(const NotificationBundleOption &bundleOption, int32_t badgeNumber); + /** * @brief Obtains allow notification application list. * diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index fbfded21e..50d49e1a1 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -981,6 +981,16 @@ public: */ ErrCode SetBadgeNumberByBundle(const sptr &bundleOption, int32_t badgeNumber) override; + /** + * @brief Set badge number for dh by bundle. + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param badgeNumber The badge number. + * @return Returns set badge number by bundle result. + */ + ErrCode SetBadgeNumberForDhByBundle( + const sptr &bundleOption, int32_t badgeNumber) override; + /** * @brief Obtains allow notification application list. * diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index a0cffd382..a3262ea72 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -67,6 +67,7 @@ constexpr int32_t TYPE_CODE_DOWNLOAD = 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 int32_t OPERATION_TYPE_COMMON_EVENT = 4; +constexpr int32_t BADGE_NUM_LIMIT = 0; ErrCode AdvancedNotificationService::SetDefaultNotificationEnabled( const sptr &bundleOption, bool enabled) @@ -2486,6 +2487,49 @@ ErrCode AdvancedNotificationService::SetBadgeNumber(int32_t badgeNumber, const s return ERR_OK; } +ErrCode AdvancedNotificationService::SetBadgeNumberForDhByBundle( + const sptr &bundleOption, int32_t badgeNumber) +{ + if (bundleOption == nullptr || bundleOption->GetBundleName().empty() || + bundleOption->GetUid() <= DEFAULT_UID) { + ANS_LOGE("SetBadgeNumberForDhByBundle invalid bundleOption"); + 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) { diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index 7c04ef608..56a15ea41 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -1915,8 +1915,14 @@ ErrCode AdvancedNotificationService::CheckBundleOptionValid(sptrGetDefaultUidByBundleName(bundleOption->GetBundleName(), activeUserId); if (uid == -1) { - ANS_LOGE("The specified bundle name was not found."); - return ERR_ANS_INVALID_BUNDLE; + if (bundleOption->GetUid() > DEFAULT_UID) { + uid = bundleOption->GetUid(); + } else { + message.ErrorCode(ERR_ANS_INVALID_BUNDLE).Append("Bundle name was not found."); + NotificationAnalyticsUtil::ReportModifyEvent(message); + ANS_LOGE("The specified bundle name was not found."); + return ERR_ANS_INVALID_BUNDLE; + } } if (bundleOption->GetUid() > 0) { -- Gitee From 8087b02f8c15cf63aebbc895c1996f9562c70994 Mon Sep 17 00:00:00 2001 From: xdongs Date: Wed, 26 Mar 2025 10:06:42 +0800 Subject: [PATCH 2/3] message Signed-off-by: xdongs --- services/ans/src/advanced_notification_utils.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index 56a15ea41..799c63fd2 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -1918,8 +1918,6 @@ ErrCode AdvancedNotificationService::CheckBundleOptionValid(sptrGetUid() > DEFAULT_UID) { uid = bundleOption->GetUid(); } else { - message.ErrorCode(ERR_ANS_INVALID_BUNDLE).Append("Bundle name was not found."); - NotificationAnalyticsUtil::ReportModifyEvent(message); ANS_LOGE("The specified bundle name was not found."); return ERR_ANS_INVALID_BUNDLE; } -- Gitee From d2223932e7dd6b4096ec088b3eb154158f063922 Mon Sep 17 00:00:00 2001 From: xdongs Date: Wed, 26 Mar 2025 10:42:06 +0800 Subject: [PATCH 3/3] proxy Signed-off-by: xdongs --- frameworks/core/src/ans_manager_proxy.cpp | 35 +++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index c922ac1ee..b9e03e40f 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -2025,6 +2025,41 @@ ErrCode AnsManagerProxy::SetBadgeNumberByBundle(const sptr &bundleOption, int32_t badgeNumber) +{ + if (bundleOption == nullptr) { + ANS_LOGE("Bundle is empty."); + return ERR_ANS_INVALID_PARAM; + } + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("Write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + if (!data.WriteParcelable(bundleOption)) { + ANS_LOGE("Write bundle option failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + if (!data.WriteInt32(badgeNumber)) { + ANS_LOGE("Write badge number failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = { MessageOption::TF_SYNC }; + ErrCode result = InnerTransact(NotificationInterfaceCode::SET_BADGE_NUMBER_FOR_DH_BY_BUNDLE, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("Transact error code is: %{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + if (!reply.ReadInt32(result)) { + ANS_LOGE("Read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + return result; +} + ErrCode AnsManagerProxy::GetAllNotificationEnabledBundles(std::vector &bundleOption) { ANS_LOGD("Called."); -- Gitee