From 432974156007c18a034921c60f65e27029191e8c Mon Sep 17 00:00:00 2001 From: zhengzhuolan Date: Tue, 9 Sep 2025 10:07:28 +0800 Subject: [PATCH] Add extension subscription interfaces to dist db Signed-off-by: zhengzhuolan --- ...tification_extension_subscription_info.cpp | 148 ++++++++++++++ interfaces/inner_api/notification_constant.h | 4 + ...notification_extension_subscription_info.h | 138 +++++++++++++ .../ans/include/notification_preferences.h | 14 ++ .../notification_preferences_database.h | 9 + .../include/notification_preferences_info.h | 16 ++ services/ans/include/preferences_constant.h | 1 + services/ans/src/notification_preferences.cpp | 181 ++++++++++++++++++ .../src/notification_preferences_database.cpp | 110 +++++++++++ .../ans/src/notification_preferences_info.cpp | 132 +++++++++++++ 10 files changed, 753 insertions(+) create mode 100644 frameworks/ans/src/notification_extension_subscription_info.cpp create mode 100644 interfaces/inner_api/notification_extension_subscription_info.h diff --git a/frameworks/ans/src/notification_extension_subscription_info.cpp b/frameworks/ans/src/notification_extension_subscription_info.cpp new file mode 100644 index 000000000..da6fe5e68 --- /dev/null +++ b/frameworks/ans/src/notification_extension_subscription_info.cpp @@ -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. + */ + +#include +#include "ans_log_wrapper.h" +#include "notification_extension_subscription_info.h" +#include "parcel.h" + +namespace OHOS { +namespace Notification { +NotificationExtensionSubscriptionInfo::NotificationExtensionSubscriptionInfo( + const std::string& addr, const NotificationConstant::SubscribeType type) + : addr_(addr), type_(type) +{} + +NotificationExtensionSubscriptionInfo::~NotificationExtensionSubscriptionInfo() +{} + +std::string NotificationExtensionSubscriptionInfo::GetAddr() const +{ + return addr_; +} + +void NotificationExtensionSubscriptionInfo::SetAddr(const std::string& addr) +{ + addr_ = addr; +} + +bool NotificationExtensionSubscriptionInfo::IsHfp() const +{ + return isHfp_; +} + +void NotificationExtensionSubscriptionInfo::SetHfp(const bool& hfp) +{ + isHfp_ = hfp; +} + +NotificationConstant::SubscribeType NotificationExtensionSubscriptionInfo::GetType() const +{ + return type_; +} + +void NotificationExtensionSubscriptionInfo::SetType(const NotificationConstant::SubscribeType type) +{ + type_ = type; +} + +std::string NotificationExtensionSubscriptionInfo::Dump() +{ + return "NotificationExtensionSubscriptionInfo{ " + "addr = " + addr_ + + ", type = " + std::to_string(static_cast(type_)) + + " }"; +} + +bool NotificationExtensionSubscriptionInfo::Marshalling(Parcel& parcel) const +{ + if (!parcel.WriteString(addr_)) { + ANS_LOGE("Failed to write address"); + return false; + } + + if (!parcel.WriteInt32(static_cast(type_))) { + ANS_LOGE("Failed to write type"); + return false; + } + + return true; +} + +NotificationExtensionSubscriptionInfo *NotificationExtensionSubscriptionInfo::Unmarshalling(Parcel& parcel) +{ + auto pNotificationExtensionSubscriptionInfo = new (std::nothrow) NotificationExtensionSubscriptionInfo(); + if (pNotificationExtensionSubscriptionInfo && !pNotificationExtensionSubscriptionInfo->ReadFromParcel(parcel)) { + delete pNotificationExtensionSubscriptionInfo; + pNotificationExtensionSubscriptionInfo = nullptr; + } + + return pNotificationExtensionSubscriptionInfo; +} + +bool NotificationExtensionSubscriptionInfo::ReadFromParcel(Parcel& parcel) +{ + if (!parcel.ReadString(addr_)) { + ANS_LOGE("Failed to read address"); + return false; + } + + type_ = static_cast(parcel.ReadInt32()); + + return true; +} + +bool NotificationExtensionSubscriptionInfo::ToJson(nlohmann::json& jsonObject) const +{ + jsonObject["addr"] = addr_; + jsonObject["isHfp"] = isHfp_; + jsonObject["type"] = static_cast(type_); + + return true; +} + +NotificationExtensionSubscriptionInfo* NotificationExtensionSubscriptionInfo::FromJson(const nlohmann::json& jsonObject) +{ + if (jsonObject.is_null() or !jsonObject.is_object()) { + ANS_LOGE("Invalid JSON object"); + return nullptr; + } + + auto *pDistributedBundleOption = new (std::nothrow) NotificationExtensionSubscriptionInfo(); + if (pDistributedBundleOption == nullptr) { + ANS_LOGE("null pDistributedBundleOption"); + return nullptr; + } + + const auto& jsonEnd = jsonObject.cend(); + + if (jsonObject.find("addr") != jsonEnd && jsonObject.at("addr").is_string()) { + pDistributedBundleOption->addr_ = jsonObject.at("addr").get(); + } + + if (jsonObject.find("isHfp") != jsonEnd && jsonObject.at("isHfp").is_boolean()) { + pDistributedBundleOption->isHfp_ = jsonObject.at("isHfp").get(); + } + + if (jsonObject.find("type") != jsonEnd && jsonObject.at("type").is_number_integer()) { + auto typeValue = jsonObject.at("type").get(); + pDistributedBundleOption->type_ = static_cast(typeValue); + } + + return pDistributedBundleOption; +} + +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/notification_constant.h b/interfaces/inner_api/notification_constant.h index 501d91b4a..6f9bd307e 100644 --- a/interfaces/inner_api/notification_constant.h +++ b/interfaces/inner_api/notification_constant.h @@ -187,6 +187,10 @@ public: SUPPORT }; + enum class SubscribeType { + BLUETOOTH = 0 + }; + static const int32_t DEFAULT_REASON_DELETE = 0; /** diff --git a/interfaces/inner_api/notification_extension_subscription_info.h b/interfaces/inner_api/notification_extension_subscription_info.h new file mode 100644 index 000000000..343043907 --- /dev/null +++ b/interfaces/inner_api/notification_extension_subscription_info.h @@ -0,0 +1,138 @@ +/* + * 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 BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_EXTENSION_SUBSCRIPTION_INFO_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_EXTENSION_SUBSCRIPTION_INFO_H + +#include "notification_constant.h" +#include "notification_json_convert.h" +#include "parcel.h" + +namespace OHOS { +namespace Notification { +class NotificationExtensionSubscriptionInfo : public Parcelable, public NotificationJsonConvertionBase { +public: + NotificationExtensionSubscriptionInfo() = default; + + /** + * @brief A constructor used to create a NotificationExtensionSubscriptionInfo instance based on the address and + * type. + * + * @param addr Indicates the address. + * @param type Indicates the type. + */ + NotificationExtensionSubscriptionInfo(const std::string& addr, const NotificationConstant::SubscribeType type); + + virtual ~NotificationExtensionSubscriptionInfo(); + + /** + * @brief Get addr. + * + * @return Returns addr. + */ + std::string GetAddr() const; + + /** + * @brief Sets addr. + * + * @param addr addr info. + */ + void SetAddr(const std::string& addr); + + /** + * @brief Get hfp. + * + * @return Returns hfp value. + */ + bool IsHfp() const; + + /** + * @brief Sets hfp. + * + * @param hfp hfp status. + */ + void SetHfp(const bool& hfp); + + /** + * @brief Get type. + * + * @return Returns type value. + */ + NotificationConstant::SubscribeType GetType() const; + + /** + * @brief Sets type. + * + * @param type type value. + */ + void SetType(const NotificationConstant::SubscribeType type); + + /** + * @brief Returns a string representation of the object. + * + * @return Returns a string representation of the object. + */ + std::string Dump(); + + /** + * @brief Marshal a object into a Parcel. + * + * @param parcel Indicates the object into the parcel + * @return Returns true if succeed; returns false otherwise. + */ + virtual bool Marshalling(Parcel& parcel) const override; + + /** + * @brief Unmarshal object from a Parcel. + * + * @param parcel Indicates the parcel object. + * @return Returns the NotificationExtensionSubscriptionInfo + */ + static NotificationExtensionSubscriptionInfo *Unmarshalling(Parcel& parcel); + + /** + * @brief Converts a notification extension subscription info object into a Json. + * + * @param jsonObject Indicates the Json object. + * @return Returns true if succeed; returns false otherwise. + */ + bool ToJson(nlohmann::json& jsonObject) const override; + + /** + * @brief Creates a notification extension subscription info object from a Json. + * + * @param jsonObject Indicates the Json object. + * @return Returns the NotificationExtensionSubscriptionInfo. + */ + static NotificationExtensionSubscriptionInfo *FromJson(const nlohmann::json& jsonObject); + +private: + /** + * @brief Read data from a Parcel. + * + * @param parcel Indicates the parcel object. + * @return Returns true if read success; returns false otherwise. + */ + bool ReadFromParcel(Parcel& parcel); + +private: + std::string addr_ {}; + bool isHfp_ = false; + NotificationConstant::SubscribeType type_ = NotificationConstant::SubscribeType::BLUETOOTH; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_EXTENSION_SUBSCRIPTION_INFO_H \ No newline at end of file diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index b7b6b0c17..acc402839 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -574,6 +574,20 @@ public: bool SetLiveViewRebuildFlag(int32_t userId); ErrCode InitBundlesInfo(int32_t userId, std::unordered_map& bundlesMap); void GetAllLiveViewBundles(std::vector>& bundleOption); + ErrCode GetExtensionSubscriptionEnabled(const sptr &bundleOption, bool &enabled); + ErrCode SetExtensionSubscriptionEnabled(const sptr &bundleOption, bool enabled); + ErrCode GetExtensionSubscriptionInfos(const sptr& bundleOption, + std::vector>& infos); + ErrCode SetExtensionSubscriptionInfos(const sptr& bundleOption, + const std::vector>& infos); + ErrCode ClearExtensionSubscriptionInfos(const sptr& bundleOption); + ErrCode GetExtensionSubscriptionBundles( + const sptr& bundleOption, std::vector>& bundles); + ErrCode AddExtensionSubscriptionBundles( + const sptr& bundleOption, const std::vector>& bundles); + ErrCode RemoveExtensionSubscriptionBundles( + const sptr& bundleOption, const std::vector>& bundles); + private: bool GetBundleInfo(NotificationPreferencesInfo &preferencesInfo, const sptr &bundleOption, NotificationPreferencesInfo::BundleInfo &info) const; diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index fe92e48d8..1064576db 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -339,6 +339,9 @@ public: int32_t SetKvToDb(const std::string &key, const std::string &value, const int32_t &userId); int32_t SetByteToDb(const std::string &key, const std::vector &value, const int32_t &userId); int32_t GetKvFromDb(const std::string &key, std::string &value, const int32_t &userId); + bool PutExtensionSubscriptionEnabled(const NotificationPreferencesInfo::BundleInfo& bundleInfo); + bool PutExtensionSubscriptionInfos(const NotificationPreferencesInfo::BundleInfo& bundleInfo); + bool PutExtensionSubscriptionBundles(const NotificationPreferencesInfo::BundleInfo& bundleInfo); #ifdef ENABLE_ANS_PRIVILEGED_MESSAGE_EXT_WRAPPER int32_t GetKvFromDb(const std::string &key, std::string &value, const int32_t &userId, int32_t &retCode); #endif @@ -504,6 +507,12 @@ private: void ParseSlotAuthorizedStatus(sptr &slot, const std::string &value) const; void ParseSlotAuthHitnCnt(sptr &slot, const std::string &value) const; void ParseSlotReminderMode(sptr &slot, const std::string &value) const; + void ParseBundleExtensionSubscriptionEnabled( + NotificationPreferencesInfo::BundleInfo& bundleInfo, const std::string& value) const; + void ParseBundleExtensionSubscriptionInfos( + NotificationPreferencesInfo::BundleInfo& bundleInfo, const std::string& value) const; + void ParseBundleExtensionSubscriptionBundles( + NotificationPreferencesInfo::BundleInfo& bundleInfo, const std::string& value) const; bool UpdateCloneToDisturbeDB(const int32_t &userId, const std::unordered_map values); diff --git a/services/ans/include/notification_preferences_info.h b/services/ans/include/notification_preferences_info.h index 158441557..068081946 100644 --- a/services/ans/include/notification_preferences_info.h +++ b/services/ans/include/notification_preferences_info.h @@ -22,6 +22,7 @@ #include "notification_bundle_option.h" #include "notification_do_not_disturb_date.h" +#include "notification_extension_subscription_info.h" #include "notification_slot.h" #include "preferences_constant.h" #include "advanced_notification_service.h" @@ -226,6 +227,18 @@ public: int32_t GetBundleUid() const; void SetSlotEnabled(NotificationConstant::SlotType slotType, bool enabled); bool GetSlotEnabled(NotificationConstant::SlotType slotType, bool &enabled) const; + bool GetExtensionSubscriptionEnabled() const; + void SetExtensionSubscriptionEnabled(bool enabled); + const std::vector>& GetExtensionSubscriptionInfos() const; + std::string GetExtensionSubscriptionInfosJson() const; + void SetExtensionSubscriptionInfos(const std::vector>& infos); + bool SetExtensionSubscriptionInfosFromJson(const std::string& json); + void GetExtensionSubscriptionBundles(std::vector>& bundles) const; + std::string GetExtensionSubscriptionBundlesJson() const; + void SetExtensionSubscriptionBundles(const std::vector>& bundles); + bool SetExtensionSubscriptionBundlesFromJson(const std::string& json); + void AddExtensionSubscriptionBundles(const std::vector>& bundles); + void RemoveExtensionSubscriptionBundles(const std::vector>& bundles); private: std::string bundleName_; @@ -239,6 +252,9 @@ public: bool hasPoppedDialog_ = BUNDLE_POPPED_DIALOG; std::map> slots_; std::map slotFlagsMap_; + bool enabledExtensionSubscription_ = false; + std::vector> extensionSubscriptionInfos_; + std::map> extensionSubscriptionBundles_; }; /* diff --git a/services/ans/include/preferences_constant.h b/services/ans/include/preferences_constant.h index 21e8728a4..6514400fd 100644 --- a/services/ans/include/preferences_constant.h +++ b/services/ans/include/preferences_constant.h @@ -61,6 +61,7 @@ enum class BundleType { BUNDLE_ENABLE_NOTIFICATION_USER_OPTION, BUNDLE_POPPED_DIALOG_TYPE, BUNDLE_SLOTFLGS_TYPE, + BUNDLE_EXTENSION_SUBSCRIPTION_ENABLED_TYPE, }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 38b662af9..32f1a3da6 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -1048,6 +1048,10 @@ ErrCode NotificationPreferences::SaveBundleProperty(NotificationPreferencesInfo: bundleInfo.SetSlotFlags(value); storeDBResult = preferncesDB_->PutSlotFlags(bundleInfo, value); break; + case BundleType::BUNDLE_EXTENSION_SUBSCRIPTION_ENABLED_TYPE: + bundleInfo.SetExtensionSubscriptionEnabled(value); + storeDBResult = preferncesDB_->PutExtensionSubscriptionEnabled(bundleInfo); + break; default: break; } @@ -1083,6 +1087,9 @@ ErrCode NotificationPreferences::GetBundleProperty( value = bundleInfo.GetSlotFlags(); ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:GetSlotFlags."); break; + case BundleType::BUNDLE_EXTENSION_SUBSCRIPTION_ENABLED_TYPE: + value = bundleInfo.GetExtensionSubscriptionEnabled(); + break; default: result = ERR_ANS_INVALID_PARAM; break; @@ -1727,6 +1734,180 @@ ErrCode NotificationPreferences::GetDistributedDevicelist(std::vector& bundleOption, bool& enabled) +{ + ANS_LOGD("called"); + if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { + return ERR_ANS_INVALID_PARAM; + } + auto result = GetBundleProperty(bundleOption, BundleType::BUNDLE_EXTENSION_SUBSCRIPTION_ENABLED_TYPE, enabled); + if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { + enabled = false; + result = ERR_OK; + } + return result; +} + +ErrCode NotificationPreferences::SetExtensionSubscriptionEnabled( + const sptr& bundleOption, bool enabled) +{ + ANS_LOGD("called"); + if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { + return ERR_ANS_INVALID_PARAM; + } + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + ErrCode result = SetBundleProperty( + preferencesInfo, bundleOption, BundleType::BUNDLE_EXTENSION_SUBSCRIPTION_ENABLED_TYPE, enabled); + if (result == ERR_OK) { + preferencesInfo_ = preferencesInfo; + } + return result; +} + +ErrCode NotificationPreferences::GetExtensionSubscriptionInfos(const sptr& bundleOption, + std::vector>& infos) +{ + ANS_LOGD("called"); + if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { + ANS_LOGE("Invalid bundle option"); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_OK; + NotificationPreferencesInfo::BundleInfo bundleInfo; + std::lock_guard lock(preferenceMutex_); + if (GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) { + infos = bundleInfo.GetExtensionSubscriptionInfos(); + } else { + ANS_LOGW("Notification bundle does not exsit."); + infos.clear(); + } + return ERR_OK; +} + + ErrCode NotificationPreferences::SetExtensionSubscriptionInfos(const sptr& bundleOption, + const std::vector>& infos) +{ + ANS_LOGD("called"); + if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { + ANS_LOGE("Invalid bundle option"); + return ERR_ANS_INVALID_PARAM; + } + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + NotificationPreferencesInfo::BundleInfo bundleInfo; + if (!GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) { + bundleInfo.SetBundleName(bundleOption->GetBundleName()); + bundleInfo.SetBundleUid(bundleOption->GetUid()); + NotificationConstant::SWITCH_STATE defaultState = CheckApiCompatibility(bundleOption) ? + NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON : + NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF; + bundleInfo.SetEnableNotification(defaultState); + } + bundleInfo.SetExtensionSubscriptionInfos(infos); + if (preferncesDB_ == nullptr) { + ANS_LOGE("the prefernces db is nullptr"); + return ERR_ANS_SERVICE_NOT_READY; + } + if (preferncesDB_->PutExtensionSubscriptionInfos(bundleInfo)) { + preferencesInfo.SetBundleInfo(bundleInfo); + return ERR_OK; + } else { + return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; + } +} + +ErrCode NotificationPreferences::ClearExtensionSubscriptionInfos(const sptr& bundleOption) +{ + ANS_LOGD("called"); + return SetExtensionSubscriptionInfos(bundleOption, std::vector>()); +} + +ErrCode NotificationPreferences::GetExtensionSubscriptionBundles( + const sptr& bundleOption, std::vector>& bundles) +{ + ANS_LOGD("called"); + if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { + ANS_LOGE("Invalid bundle option"); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_OK; + NotificationPreferencesInfo::BundleInfo bundleInfo; + std::lock_guard lock(preferenceMutex_); + if (GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) { + bundleInfo.GetExtensionSubscriptionBundles(bundles); + } else { + ANS_LOGW("Notification bundle does not exsit."); + bundles.clear(); + } + return ERR_OK; +} + +ErrCode NotificationPreferences::AddExtensionSubscriptionBundles( + const sptr& bundleOption, const std::vector>& bundles) +{ + ANS_LOGD("called"); + if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { + ANS_LOGE("Invalid bundle option"); + return ERR_ANS_INVALID_PARAM; + } + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + NotificationPreferencesInfo::BundleInfo bundleInfo; + if (!GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) { + bundleInfo.SetBundleName(bundleOption->GetBundleName()); + bundleInfo.SetBundleUid(bundleOption->GetUid()); + NotificationConstant::SWITCH_STATE defaultState = CheckApiCompatibility(bundleOption) ? + NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON : + NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF; + bundleInfo.SetEnableNotification(defaultState); + } + bundleInfo.AddExtensionSubscriptionBundles(bundles); + if (preferncesDB_ == nullptr) { + ANS_LOGE("the prefernces db is nullptr"); + return ERR_ANS_SERVICE_NOT_READY; + } + if (preferncesDB_->PutExtensionSubscriptionBundles(bundleInfo)) { + preferencesInfo.SetBundleInfo(bundleInfo); + return ERR_OK; + } else { + return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; + } +} + +ErrCode NotificationPreferences::RemoveExtensionSubscriptionBundles( + const sptr& bundleOption, const std::vector>& bundles) +{ + ANS_LOGD("called"); + if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { + ANS_LOGE("Invalid bundle option"); + return ERR_ANS_INVALID_PARAM; + } + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + NotificationPreferencesInfo::BundleInfo bundleInfo; + if (!GetBundleInfo(preferencesInfo_, bundleOption, bundleInfo)) { + bundleInfo.SetBundleName(bundleOption->GetBundleName()); + bundleInfo.SetBundleUid(bundleOption->GetUid()); + NotificationConstant::SWITCH_STATE defaultState = CheckApiCompatibility(bundleOption) ? + NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_ON : + NotificationConstant::SWITCH_STATE::SYSTEM_DEFAULT_OFF; + bundleInfo.SetEnableNotification(defaultState); + } + bundleInfo.RemoveExtensionSubscriptionBundles(bundles); + if (preferncesDB_ == nullptr) { + ANS_LOGE("the prefernces db is nullptr"); + return ERR_ANS_SERVICE_NOT_READY; + } + if (preferncesDB_->PutExtensionSubscriptionBundles(bundleInfo)) { + preferencesInfo.SetBundleInfo(bundleInfo); + return ERR_OK; + } else { + return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; + } +} + ErrCode NotificationPreferences::SetSubscriberExistFlag(const std::string& deviceType, bool existFlag) { ANS_LOGD("%{public}s", __FUNCTION__); diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index e95c31de1..52d9d9160 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -233,6 +233,10 @@ const static std::string KEY_SLOT_AUTH_HINT_CNT = "authHintCnt"; */ const static std::string KEY_REMINDER_MODE = "reminderMode"; +const static std::string KEY_EXTENSION_SUBSCRIPTION_ENABLED = "enableExtensionSubscription"; +const static std::string KEY_EXTENSION_SUBSCRIPTION_INFO = "extensionSubscriptionInfo"; +const static std::string KEY_EXTENSION_SUBSCRIPTION_BUNDLES = "extensionSubscriptionBundles"; + constexpr char RELATIONSHIP_JSON_KEY_SERVICE[] = "service"; constexpr char RELATIONSHIP_JSON_KEY_APP[] = "app"; @@ -729,6 +733,12 @@ bool NotificationPreferencesDatabase::PutBundlePropertyValueToDisturbeDB( std::to_string(bundleInfo.GetHasPoppedDialog()), values); GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_UID), std::to_string(bundleInfo.GetBundleUid()), values); + GenerateEntry(GenerateBundleKey(bundleKey, KEY_EXTENSION_SUBSCRIPTION_ENABLED), + std::to_string(bundleInfo.GetExtensionSubscriptionEnabled()), values); + GenerateEntry(GenerateBundleKey(bundleKey, KEY_EXTENSION_SUBSCRIPTION_INFO), + bundleInfo.GetExtensionSubscriptionInfosJson(), values); + GenerateEntry(GenerateBundleKey(bundleKey, KEY_EXTENSION_SUBSCRIPTION_BUNDLES), + bundleInfo.GetExtensionSubscriptionBundlesJson(), values); if (!CheckRdbStore()) { ANS_LOGE("null RdbStore"); return false; @@ -1066,6 +1076,9 @@ int32_t NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( ANS_LOGD("Into BUNDLE_SLOTFLGS_TYPE:GenerateBundleKey."); keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SLOTFLGS_TYPE); break; + case BundleType::BUNDLE_EXTENSION_SUBSCRIPTION_ENABLED_TYPE: + keyStr = GenerateBundleKey(bundleKey, KEY_EXTENSION_SUBSCRIPTION_ENABLED); + break; default: break; } @@ -1259,6 +1272,15 @@ void NotificationPreferencesDatabase::ParseBundlePropertyFromDisturbeDB( if (typeStr.compare(KEY_BUNDLE_SLOTFLGS_TYPE) == 0) { return ParseBundleSlotFlags(bundleInfo, valueStr); } + if (typeStr.compare(KEY_EXTENSION_SUBSCRIPTION_ENABLED) == 0) { + return ParseBundleExtensionSubscriptionEnabled(bundleInfo, valueStr); + } + if (typeStr.compare(KEY_EXTENSION_SUBSCRIPTION_INFO) == 0) { + return ParseBundleExtensionSubscriptionInfos(bundleInfo, valueStr); + } + if (typeStr.compare(KEY_EXTENSION_SUBSCRIPTION_BUNDLES) == 0) { + return ParseBundleExtensionSubscriptionBundles(bundleInfo, valueStr); + } } void NotificationPreferencesDatabase::ParseSlot(const std::string &findString, sptr &slot, @@ -1646,6 +1668,27 @@ void NotificationPreferencesDatabase::ParseSlotReminderMode( slot->SetReminderMode(reminderMode); } +void NotificationPreferencesDatabase::ParseBundleExtensionSubscriptionEnabled( + NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const +{ + ANS_LOGD("ParseBundleExtensionSubscriptionEnabled bundle enabled is %{public}s.", value.c_str()); + bundleInfo.SetExtensionSubscriptionEnabled(static_cast(StringToInt(value))); +} + +void NotificationPreferencesDatabase::ParseBundleExtensionSubscriptionInfos( + NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const +{ + ANS_LOGD("ParseBundleExtensionSubscriptionInfos bundle infos: %{public}s.", value.c_str()); + bundleInfo.SetExtensionSubscriptionInfosFromJson(value); +} + +void NotificationPreferencesDatabase::ParseBundleExtensionSubscriptionBundles( + NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const +{ + ANS_LOGD("ParseBundleExtensionSubscriptionBundles bundle bundels: %{public}s.", value.c_str()); + bundleInfo.SetExtensionSubscriptionBundlesFromJson(value); +} + std::string NotificationPreferencesDatabase::GenerateBundleLablel( const NotificationPreferencesInfo::BundleInfo &bundleInfo) const { @@ -2596,6 +2639,73 @@ bool NotificationPreferencesDatabase::IsSmartReminderEnabled(const std::string d return result; } +bool NotificationPreferencesDatabase::PutExtensionSubscriptionEnabled( + const NotificationPreferencesInfo::BundleInfo& bundleInfo) +{ + if (bundleInfo.GetBundleName().empty()) { + ANS_LOGE("Bundle name is null."); + return false; + } + + if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) { + return false; + } + + std::string bundleKey = GenerateBundleLablel(bundleInfo); + int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_EXTENSION_SUBSCRIPTION_ENABLED_TYPE, + bundleInfo.GetExtensionSubscriptionEnabled(), bundleInfo.GetBundleUid()); + return (result == NativeRdb::E_OK); +} + +bool NotificationPreferencesDatabase::PutExtensionSubscriptionInfos( + const NotificationPreferencesInfo::BundleInfo& bundleInfo) +{ + if (bundleInfo.GetBundleName().empty()) { + ANS_LOGE("Bundle name is null."); + return false; + } + + if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) { + return false; + } + + if (!CheckRdbStore()) { + ANS_LOGE("null RdbStore"); + return false; + } + std::string bundleKey = GenerateBundleLablel(bundleInfo); + int32_t userId = -1; + OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId); + int32_t result = rdbDataManager_->InsertData(GenerateBundleKey(bundleKey, KEY_EXTENSION_SUBSCRIPTION_INFO), + bundleInfo.GetExtensionSubscriptionInfosJson(), userId); + return (result == NativeRdb::E_OK); +} + + +bool NotificationPreferencesDatabase::PutExtensionSubscriptionBundles( + const NotificationPreferencesInfo::BundleInfo& bundleInfo) +{ + if (bundleInfo.GetBundleName().empty()) { + ANS_LOGE("Bundle name is null."); + return false; + } + + if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) { + return false; + } + + if (!CheckRdbStore()) { + ANS_LOGE("null RdbStore"); + return false; + } + std::string bundleKey = GenerateBundleLablel(bundleInfo); + int32_t userId = -1; + OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleInfo.GetBundleUid(), userId); + int32_t result = rdbDataManager_->InsertData(GenerateBundleKey(bundleKey, KEY_EXTENSION_SUBSCRIPTION_BUNDLES), + bundleInfo.GetExtensionSubscriptionBundlesJson(), userId); + return (result == NativeRdb::E_OK); +} + bool NotificationPreferencesDatabase::SetDistributedEnabledBySlot( const NotificationConstant::SlotType &slotType, const std::string &deviceType, const bool enabled) { diff --git a/services/ans/src/notification_preferences_info.cpp b/services/ans/src/notification_preferences_info.cpp index 9eb121215..cf6731baa 100644 --- a/services/ans/src/notification_preferences_info.cpp +++ b/services/ans/src/notification_preferences_info.cpp @@ -222,6 +222,138 @@ int32_t NotificationPreferencesInfo::BundleInfo::GetBundleUid() const return uid_; } +bool NotificationPreferencesInfo::BundleInfo::GetExtensionSubscriptionEnabled() const +{ + return enabledExtensionSubscription_; +} + +void NotificationPreferencesInfo::BundleInfo::SetExtensionSubscriptionEnabled(bool enabled) +{ + enabledExtensionSubscription_ = enabled; +} + +const std::vector>& + NotificationPreferencesInfo::BundleInfo::GetExtensionSubscriptionInfos() const +{ + return extensionSubscriptionInfos_; +} + +std::string NotificationPreferencesInfo::BundleInfo::GetExtensionSubscriptionInfosJson() const +{ + auto jsonObject = nlohmann::json::array(); + for (const auto& item : extensionSubscriptionInfos_) { + nlohmann::json jsonNode; + item->ToJson(jsonNode); + jsonObject.emplace_back(jsonNode); + } + return jsonObject.dump(); +} + +void NotificationPreferencesInfo::BundleInfo::SetExtensionSubscriptionInfos( + const std::vector>& infos) +{ + extensionSubscriptionInfos_ = infos; +} + +bool NotificationPreferencesInfo::BundleInfo::SetExtensionSubscriptionInfosFromJson(const std::string& json) +{ + if (json.empty() || !nlohmann::json::accept(json)) { + ANS_LOGE("Invalid json string"); + return false; + } + nlohmann::json jsonObject = nlohmann::json::parse(json, nullptr, false); + if (jsonObject.is_null() || jsonObject.empty()) { + ANS_LOGE("Invalid JSON object"); + return false; + } + if (jsonObject.is_discarded() || !jsonObject.is_array()) { + ANS_LOGE("Parse extension subscription info list failed due to data is discarded or not array"); + return false; + } + + extensionSubscriptionInfos_.clear(); + for (const auto &item : jsonObject) { + extensionSubscriptionInfos_.emplace_back(NotificationExtensionSubscriptionInfo::FromJson(item)); + } + + return true; +} + +void NotificationPreferencesInfo::BundleInfo::GetExtensionSubscriptionBundles( + std::vector>& bundles) const +{ + bundles.clear(); + for (const auto& item : extensionSubscriptionBundles_) { + bundles.emplace_back(item.second); + } +} + +std::string NotificationPreferencesInfo::BundleInfo::GetExtensionSubscriptionBundlesJson() const +{ + std::vector> bundles; + GetExtensionSubscriptionBundles(bundles); + auto jsonObject = nlohmann::json::array(); + for (const auto& item : bundles) { + nlohmann::json jsonNode; + item->ToJson(jsonNode); + jsonObject.emplace_back(jsonNode); + } + return jsonObject.dump(); +} + +void NotificationPreferencesInfo::BundleInfo::SetExtensionSubscriptionBundles( + const std::vector>& bundles) +{ + extensionSubscriptionBundles_.clear(); + AddExtensionSubscriptionBundles(bundles); +} + +bool NotificationPreferencesInfo::BundleInfo::SetExtensionSubscriptionBundlesFromJson(const std::string& json) +{ + if (json.empty() || !nlohmann::json::accept(json)) { + ANS_LOGE("Invalid json string"); + return false; + } + nlohmann::json jsonObject = nlohmann::json::parse(json, nullptr, false); + if (jsonObject.is_null() || jsonObject.empty()) { + ANS_LOGE("Invalid JSON object"); + return false; + } + if (jsonObject.is_discarded() || !jsonObject.is_array()) { + ANS_LOGE("Parse extension subscription bundle list failed due to data is discarded or not array"); + return false; + } + + std::vector> bundles; + for (const auto &item : jsonObject) { + bundles.emplace_back(NotificationBundleOption::FromJson(item)); + } + SetExtensionSubscriptionBundles(bundles); + + return true; +} + +void NotificationPreferencesInfo::BundleInfo::AddExtensionSubscriptionBundles( + const std::vector>& bundles) +{ + for (const auto& item : bundles) { + std::string bundleKey = item->GetBundleName() + std::to_string(item->GetUid()); + extensionSubscriptionBundles_.insert_or_assign(bundleKey, item); + } +} + +void NotificationPreferencesInfo::BundleInfo::RemoveExtensionSubscriptionBundles( + const std::vector>& bundles) +{ + for (const auto& item : bundles) { + std::string bundleKey = item->GetBundleName() + std::to_string(item->GetUid()); + auto iter = extensionSubscriptionBundles_.find(bundleKey); + if (iter != extensionSubscriptionBundles_.end()) { + extensionSubscriptionBundles_.erase(iter); + } + } +} + void NotificationPreferencesInfo::SetBundleInfo(BundleInfo &info) { std::string bundleKey = info.GetBundleName().append(std::to_string(info.GetBundleUid())); -- Gitee