diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 0722b0a9133a7f2759f5bbbbf05a9d938cd98f65..1e8673c2dddda5ad6c13dbac40ef30bc58d8fb8e 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 384c34820893d54b53302d48e34d835052db541b..c2df4f1608dc320cbca6d520e6160c6bfddbe9e3 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 0a906f4ebbd5cee36868ed5b5735431cd146b949..fe56c92ae52f8d26d63dc28a73ea9b26bf532d40 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 72a6b8616e83a6b164eec4400ff46cb6ded85c99..36f25944fb39fc95272153b5907f023c202592de 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 be1d52d0b4ceb6040eaa58998e7c78293b807cfa..f38c6270a8b5e920ecd4106d0460f3376021c870 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 2e2e68cbf0c7dc40d6df796713af1d4713fc5664..78b67399367b4bf2ba13b3a85214dc663c37841c 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_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index c922ac1eed73d648e7921edc99008f66a2983d52..b9e03e40fb83d74782361f22cd9b95de3ddcca8a 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."); diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index a350e8f9aefc7f6ab16ab04fb3efaeb28db294d0..ee14e411349151368fca40d0256e5a3d10a1b5e9 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 db13e6eb806f02a36746d2e5d2c2cfc1c7227eb0..d67cb48599aae4679ff6d1677db2b4152f779a34 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 dfb2da3889ef56fd9d216a0854cf8a6d51941671..23974a338f91d51776691302433acf86dc1b5eb9 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 8d2c39b9254a85d094cb51b0ff71ae141ffe50bc..d9ac2ca4fd6e91fa950be4e1c3f32892a4b5dfcf 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 92bfd457a35701b69d64ac75693520c4dbc568a2..20f49339bbea471274d129692280c624d4c3acf8 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 3625c2d7989d5f3eede822f52bc2bd85b6b5f6d6..192438b34423e6254f8a6a52b9b654e1f8efad42 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 fbfded21e6085ea3594c71d9b923c4bf330e2b93..50d49e1a14d0d0f475982c0b3496d5a1c1323732 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 a0cffd382311c087891193894b5c09cc6083cea8..a3262ea72e951ab85a6588b28dd74bbf0ef4eda3 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 7c04ef608b8a36b83ef46497c109ab571c624ec4..799c63fd225b9805685410244925ffa3b7f173d5 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -1915,8 +1915,12 @@ 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 { + ANS_LOGE("The specified bundle name was not found."); + return ERR_ANS_INVALID_BUNDLE; + } } if (bundleOption->GetUid() > 0) {