diff --git a/interfaces/inner_api/notification_constant.h b/interfaces/inner_api/notification_constant.h index 6be92c235f2fad724af99cc8d2d9cd188bef55a7..a964c3d206a6e0894c7b18a4cd36665f8eb3460e 100644 --- a/interfaces/inner_api/notification_constant.h +++ b/interfaces/inner_api/notification_constant.h @@ -180,6 +180,14 @@ public: * The key indicates input source. */ static const std::string EXTRA_INPUTS_SOURCE; + + // rdb + constexpr static const char* NOTIFICATION_RDB_NAME = "/notificationdb.db"; + constexpr static const char* NOTIFICATION_RDB_TABLE_NAME = "notification_table"; + constexpr static const char* NOTIFICATION_MANAGER_SERVICE_PATH = "/data/service/el1/public/database/notification_service"; + constexpr static const char* NOTIFICATION_JOURNAL_MODE = "WAL"; + constexpr static const char* NOTIFICATION_SYNC_MODE = "MODE_OFF"; + constexpr static int32_t NOTIFICATION_RDB_VERSION = 1; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index ec8fdfebfe57dd69d087d6934f7c5a6caf19a90b..1a59afa5022b646bd2735a12f16eeee5f7588e4d 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -34,6 +34,7 @@ ohos_shared_library("libans") { "src/notification_preferences.cpp", "src/notification_preferences_database.cpp", "src/notification_preferences_info.cpp", + "src/notification_rdb_data_mgr.cpp", "src/notification_slot_filter.cpp", "src/notification_subscriber_manager.cpp", "src/permission_filter.cpp", diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index e2123a89ed9cc8fc0ac812cf8b1a46942f402a98..af7cd3f806b85fac901ada0b4a79947282e1146d 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -18,8 +18,9 @@ #include #include +#include -#include "distributed_kv_data_manager.h" +#include "notification_rdb_data_mgr.h" #include "notification_preferences_info.h" namespace OHOS { @@ -166,29 +167,26 @@ public: bool RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid); private: - void TryTwice(const std::function &func) const; - DistributedKv::Status GetKvStore(); - void CloseKvStore(); - bool CheckKvStore(); + bool CheckRdbStore(); bool CheckBundle(const std::string &bundleName, const int32_t &bundleUid); bool PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo); template - DistributedKv::Status PutBundlePropertyToDisturbeDB( + int32_t PutBundlePropertyToDisturbeDB( const std::string &bundleKey, const BundleType &type, const T &t); bool PutBundleToDisturbeDB( const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo); - void GetValueFromDisturbeDB(const std::string &key, std::function function); + void GetValueFromDisturbeDB(const std::string &key, std::function function); void GetValueFromDisturbeDB(const std::string &key, - std::function function); + std::function function); bool SlotToEntry(const std::string &bundleName, const int32_t &bundleUid, const sptr &slot, - std::vector &entries); + std::unordered_map &values); void GenerateSlotEntry(const std::string &bundleKey, const sptr &slot, - std::vector &entries) const; + std::unordered_map &values) const; void GenerateEntry( - const std::string &key, const std::string &value, std::vector &entry) const; + const std::string &key, const std::string &value, std::unordered_map &values) const; std::string FindLastString(const std::string &findString, const std::string &inputString) const; std::string SubUniqueIdentifyFromString(const std::string &findString, const std::string &keyStr) const; @@ -202,11 +200,11 @@ private: std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const; void ParseBundleFromDistureDB( - NotificationPreferencesInfo &info, const std::vector &entries); + NotificationPreferencesInfo &info, const std::unordered_map &entries); void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, - const DistributedKv::Entry &entry); + const std::pair &entry); void ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, - const std::string &bundleKey, const DistributedKv::Entry &entry); + const std::string &bundleKey, const std::pair &entry); void ParseDoNotDisturbType(NotificationPreferencesInfo &info); void ParseDoNotDisturbBeginDate(NotificationPreferencesInfo &info); void ParseDoNotDisturbEndDate(NotificationPreferencesInfo &info); @@ -223,7 +221,7 @@ private: NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleUid(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseSlot( - const std::string &findString, sptr &slot, const DistributedKv::Entry &entry); + const std::string &findString, sptr &slot, const std::pair &entry); void ParseSlotDescription(sptr &slot, const std::string &value) const; void ParseSlotLevel(sptr &slot, const std::string &value) const; void ParseSlotShowBadge(sptr &slot, const std::string &value) const; @@ -249,11 +247,8 @@ private: NotificationPreferencesInfo::BundleInfo &, std::string &)>> bundleMap_; - const DistributedKv::AppId appId_ {APP_ID}; - const DistributedKv::StoreId storeId_ {STORE_ID}; - std::shared_ptr kvStorePtr_ = nullptr; - DistributedKv::DistributedKvDataManager dataManager_; + std::shared_ptr rdbDataManager_; }; -} // namespace Notification -} // namespace OHOS +} // namespace Notification +} // namespace OHOS #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H diff --git a/services/ans/include/notification_rdb_data_mgr.h b/services/ans/include/notification_rdb_data_mgr.h new file mode 100644 index 0000000000000000000000000000000000000000..d89e811d11258718f048f5e508730ad43cf7cbb1 --- /dev/null +++ b/services/ans/include/notification_rdb_data_mgr.h @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2022 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_SERVICES_ANS_INCLUDE_NOTIFICATION_RDB_DATA_MGR_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_RDB_DATA_MGR_H + +#include +#include +#include +#include "notification_constant.h" +#include "rdb_errno.h" +#include "rdb_helper.h" +#include "rdb_open_callback.h" +#include "rdb_store_config.h" + +namespace OHOS { +namespace Notification { +struct NotificationRdbConfig { + std::string dbPath { NotificationConstant::NOTIFICATION_MANAGER_SERVICE_PATH }; + std::string dbName { NotificationConstant::NOTIFICATION_RDB_NAME }; + std::string tableName { NotificationConstant::NOTIFICATION_RDB_TABLE_NAME }; + std::string journalMode { NotificationConstant::NOTIFICATION_JOURNAL_MODE }; + std::string syncMode { NotificationConstant::NOTIFICATION_SYNC_MODE }; + int32_t version { NotificationConstant::NOTIFICATION_RDB_VERSION }; +}; +class RdbStoreDataCallBackNotificationStorage : public NativeRdb::RdbOpenCallback { +public: + + RdbStoreDataCallBackNotificationStorage(const NotificationRdbConfig ¬ificationRdbConfig); + + virtual ~RdbStoreDataCallBackNotificationStorage(); + + int32_t OnCreate(NativeRdb::RdbStore &rdbStore) override; + + int32_t OnUpgrade(NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) override; + + int32_t OnDowngrade(NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) override; + + int32_t OnOpen(NativeRdb::RdbStore &rdbStore) override; + + int32_t onCorruption(std::string databaseFile) override; +private: + NotificationRdbConfig notificationRdbConfig_; + bool hasTableInit_ = false; +}; + +/** + * @class NotificationDataMgr + * Notification Data Manager. + */ +class NotificationDataMgr { +public: + + NotificationDataMgr(const NotificationRdbConfig ¬ificationRdbConfig); + + int32_t Init(); + + int32_t Destroy(); + + /** + * @brief Insert data in DB. + * @param key The data Key. + * @return Returns ERR_OK on success, others on failure. + */ + int32_t InsertData(const std::string &key, const std::string &value); + + /** + * @brief Insert batch data in DB. + * @param key The data Key. + * @return Returns ERR_OK on success, others on failure. + */ + int32_t InsertBatchData(const std::unordered_map &values); + + /** + * @brief Delete data in DB. + * @param key The data Key. + * @return Returns ERR_OK on success, others on failure. + */ + int32_t DeleteData(const std::string &key); + + /** + * @brief Delete batch data in DB. + * @param key The data Key. + * @return Returns ERR_OK on success, others on failure. + */ + int32_t DeleteBathchData(const std::vector &keys); + + /** + * @brief Query data in DB. + * @return Returns ERR_OK on success, others on failure. + */ + int32_t QueryData(const std::string &key, std::string &value); + + /** + * @brief Query data begin whith key in DB. + * @return Returns ERR_OK on success, others on failure. + */ + int32_t QueryDataBeginWithKey(const std::string &key, std::unordered_map &values); + + /** + * @brief Query all data in DB. + * @return Returns ERR_OK on success, others on failure. + */ + int32_t QueryAllData(std::unordered_map &values); + +private: + NotificationRdbConfig notificationRdbConfig_; + std::shared_ptr rdbStore_; + mutable std::mutex rdbStorePtrMutex_; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_NOTIFICATION_RDB_DATA_MGR_H diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index b8b64fcbbd171989f328daf847f558032f6cfdef..0f909fd6c9937c62ab2b74db2915318d3c7b5f3b 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 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 @@ -173,7 +173,6 @@ const static std::string KEY_SLOT_ENABLE_BYPASS_DND = "enableBypassDnd"; */ const static std::string KEY_SLOT_ENABLED = "enabled"; -constexpr char KV_STORE_PATH[] = "/data/service/el1/public/database/notification_service"; const std::map &, std::string &)>> @@ -282,55 +281,24 @@ const std::map(notificationRdbConfig); + rdbDataManager_->Init(); + ANS_LOGI("Notification Rdb is created"); } NotificationPreferencesDatabase::~NotificationPreferencesDatabase() { - CloseKvStore(); + ANS_LOGI("Notification Rdb is deleted"); } -void NotificationPreferencesDatabase::TryTwice(const std::function &func) const +bool NotificationPreferencesDatabase::CheckRdbStore() { - DistributedKv::Status status = func(); - if (status != DistributedKv::Status::SUCCESS) { - status = func(); - ANS_LOGW("Distribute database error and try to call again, result = %{public}d.", status); - } -} - -DistributedKv::Status NotificationPreferencesDatabase::GetKvStore() -{ - DistributedKv::Options options = { - .createIfMissing = true, - .encrypt = false, - .autoSync = false, - .area = DistributedKv::EL1, - .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION, - .baseDir = KV_STORE_PATH - }; - auto status = dataManager_.GetSingleKvStore(options, appId_, storeId_, kvStorePtr_); - if (status != DistributedKv::Status::SUCCESS) { - ANS_LOGE("Return error: %{public}d.", status); - } else { - ANS_LOGI("Get kvStore success."); - } - return status; -} - -void NotificationPreferencesDatabase::CloseKvStore() -{ - ANS_LOGI("Close kvStore success."); - dataManager_.CloseKvStore(appId_, kvStorePtr_); -} - -bool NotificationPreferencesDatabase::CheckKvStore() -{ - if (kvStorePtr_ != nullptr) { + int32_t result = rdbDataManager_->Init(); + if (result == NativeRdb::E_OK) { return true; } - TryTwice([this] { return GetKvStore(); }); - return kvStorePtr_ != nullptr; + return false; } bool NotificationPreferencesDatabase::PutSlotsToDisturbeDB( @@ -348,21 +316,20 @@ bool NotificationPreferencesDatabase::PutSlotsToDisturbeDB( return false; } - std::vector entries; + std::unordered_map values; for (auto iter : slots) { - bool result = SlotToEntry(bundleName, bundleUid, iter, entries); + bool result = SlotToEntry(bundleName, bundleUid, iter, values); if (!result) { return result; } } - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } - DistributedKv::Status status = kvStorePtr_->PutBatch(entries); - CloseKvStore(); - return (status == DistributedKv::Status::SUCCESS); + int32_t result = rdbDataManager_->InsertBatchData(values); + return (result == NativeRdb::E_OK); } bool NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( @@ -373,20 +340,20 @@ bool NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( return false; } - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } - + std::string values; std::string bundleKeyStr = KEY_BUNDLE_LABEL + GenerateBundleLablel(bundleInfo); bool result = false; - GetValueFromDisturbeDB(bundleKeyStr, [&](const DistributedKv::Status &status, DistributedKv::Value &value) { + GetValueFromDisturbeDB(bundleKeyStr, [&](const int32_t &status, std::string &value) { switch (status) { - case DistributedKv::Status::KEY_NOT_FOUND: { + case NativeRdb::E_ERROR: { result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo); break; } - case DistributedKv::Status::SUCCESS: { + case NativeRdb::E_OK: { ANS_LOGE("Current bundle has exsited."); break; } @@ -394,7 +361,6 @@ bool NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( break; } }); - CloseKvStore(); return result; } @@ -411,9 +377,9 @@ bool NotificationPreferencesDatabase::PutShowBadge( } std::string bundleKey = GenerateBundleLablel(bundleInfo); - DistributedKv::Status status = + int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable); - return (status == DistributedKv::Status::SUCCESS); + return (result == NativeRdb::E_OK); } bool NotificationPreferencesDatabase::PutImportance( @@ -429,9 +395,9 @@ bool NotificationPreferencesDatabase::PutImportance( } std::string bundleKey = GenerateBundleLablel(bundleInfo); - DistributedKv::Status status = + int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_IMPORTANCE_TYPE, importance); - return (status == DistributedKv::Status::SUCCESS); + return (result == NativeRdb::E_OK); } bool NotificationPreferencesDatabase::PutTotalBadgeNums( @@ -446,9 +412,9 @@ bool NotificationPreferencesDatabase::PutTotalBadgeNums( return false; } std::string bundleKey = GenerateBundleLablel(bundleInfo); - DistributedKv::Status status = + int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum); - return (status == DistributedKv::Status::SUCCESS); + return (result == NativeRdb::E_OK); } bool NotificationPreferencesDatabase::PutPrivateNotificationsAllowed( @@ -463,10 +429,10 @@ bool NotificationPreferencesDatabase::PutPrivateNotificationsAllowed( return false; } std::string bundleKey = GenerateBundleLablel(bundleInfo); - DistributedKv::Status status = + int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_PRIVATE_ALLOWED_TYPE, allow); - return (status == DistributedKv::Status::SUCCESS); + return (result == NativeRdb::E_OK); } bool NotificationPreferencesDatabase::PutNotificationsEnabledForBundle( @@ -483,26 +449,24 @@ bool NotificationPreferencesDatabase::PutNotificationsEnabledForBundle( } std::string bundleKey = GenerateBundleLablel(bundleInfo); - DistributedKv::Status status = + int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled); - return (status == DistributedKv::Status::SUCCESS); + return (result == NativeRdb::E_OK); } bool NotificationPreferencesDatabase::PutNotificationsEnabled(const int32_t &userId, const bool &enabled) { - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } std::string typeKey = std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId)); - DistributedKv::Key enableKey(typeKey); - DistributedKv::Value enableValue(std::to_string(enabled)); - DistributedKv::Status status = kvStorePtr_->Put(enableKey, enableValue); - CloseKvStore(); - if (status != DistributedKv::Status::SUCCESS) { - ANS_LOGE("Store enable notification failed. %{public}d", status); + std::string enableValue = std::to_string(enabled); + int32_t result = rdbDataManager_->InsertData(typeKey, enableValue); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Store enable notification failed. %{public}d", result); return false; } return true; @@ -521,9 +485,9 @@ bool NotificationPreferencesDatabase::PutHasPoppedDialog( } std::string bundleKey = GenerateBundleLablel(bundleInfo); - DistributedKv::Status status = + int32_t result = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_POPPED_DIALOG_TYPE, hasPopped); - return (status == DistributedKv::Status::SUCCESS); + return (result == NativeRdb::E_OK); } bool NotificationPreferencesDatabase::PutDoNotDisturbDate( @@ -534,39 +498,32 @@ bool NotificationPreferencesDatabase::PutDoNotDisturbDate( return false; } - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } - DistributedKv::Entry type; std::string typeKey = std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId)); - type.key = DistributedKv::Key(typeKey); - type.value = DistributedKv::Value(std::to_string((int)date->GetDoNotDisturbType())); + std::string typeValue = std::to_string((int)date->GetDoNotDisturbType()); - DistributedKv::Entry beginDate; std::string beginDateKey = std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); - beginDate.key = DistributedKv::Key(beginDateKey); - beginDate.value = DistributedKv::Value(std::to_string(date->GetBeginDate())); + std::string beginDateValue = std::to_string(date->GetBeginDate()); - DistributedKv::Entry endDate; std::string endDateKey = std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); - endDate.key = DistributedKv::Key(endDateKey); - endDate.value = DistributedKv::Value(std::to_string(date->GetEndDate())); + std::string endDateValue = std::to_string(date->GetEndDate()); - std::vector entries = { - type, - beginDate, - endDate, + std::unordered_map values = { + {typeKey, typeValue}, + {beginDateKey, beginDateValue}, + {endDateKey, endDateValue}, }; - DistributedKv::Status status = kvStorePtr_->PutBatch(entries); - CloseKvStore(); - if (status != DistributedKv::Status::SUCCESS) { - ANS_LOGE("Store DoNotDisturbDate failed. %{public}d", status); + int32_t result = rdbDataManager_->InsertBatchData(values); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Store DoNotDisturbDate failed. %{public}d", result); return false; } @@ -574,42 +531,31 @@ bool NotificationPreferencesDatabase::PutDoNotDisturbDate( } void NotificationPreferencesDatabase::GetValueFromDisturbeDB( - const std::string &key, std::function callback) + const std::string &key, std::function callback) { - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); - return; - } - - DistributedKv::Status status; - DistributedKv::Value value; - DistributedKv::Key getKey(key); - status = kvStorePtr_->Get(getKey, value); - if (status != DistributedKv::Status::SUCCESS) { - ANS_LOGE("Get value failed, use default value. error code is %{public}d", status); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return; } - - if (value.Empty()) { - ANS_LOGE("Get value is empty, use default value. error code is %{public}d", value.Empty()); + std::string value; + int32_t result = rdbDataManager_->QueryData(key, value); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Get value failed, use default value. error code is %{public}d", result); return; } callback(value); } void NotificationPreferencesDatabase::GetValueFromDisturbeDB( - const std::string &key, std::function callback) + const std::string &key, std::function callback) { - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return; } - - DistributedKv::Status status; - DistributedKv::Value value; - DistributedKv::Key getKey(key); - status = kvStorePtr_->Get(getKey, value); - callback(status, value); + std::string value; + int32_t result = rdbDataManager_->QueryData(key, value); + callback(result, value); } bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleName, const int32_t &bundleUid) @@ -617,16 +563,16 @@ bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleName, std::string bundleKeyStr = KEY_BUNDLE_LABEL + bundleName + std::to_string(bundleUid); ANS_LOGD("CheckBundle bundleKeyStr %{public}s", bundleKeyStr.c_str()); bool result = true; - GetValueFromDisturbeDB(bundleKeyStr, [&](const DistributedKv::Status &status, DistributedKv::Value &value) { + GetValueFromDisturbeDB(bundleKeyStr, [&](const int32_t &status, std::string &value) { switch (status) { - case DistributedKv::Status::KEY_NOT_FOUND: { + case NativeRdb::E_ERROR: { NotificationPreferencesInfo::BundleInfo bundleInfo; bundleInfo.SetBundleName(bundleName); bundleInfo.SetBundleUid(bundleUid); result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo); break; } - case DistributedKv::Status::SUCCESS: { + case NativeRdb::E_OK: { result = true; break; } @@ -641,33 +587,33 @@ bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleName, bool NotificationPreferencesDatabase::PutBundlePropertyValueToDisturbeDB( const NotificationPreferencesInfo::BundleInfo &bundleInfo) { - std::vector entries; + std::unordered_map values; std::string bundleKey = bundleInfo.GetBundleName().append(std::to_string(bundleInfo.GetBundleUid())); - GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_NAME), bundleInfo.GetBundleName(), entries); + GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_NAME), bundleInfo.GetBundleName(), values); GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_BADGE_TOTAL_NUM), std::to_string(bundleInfo.GetBadgeTotalNum()), - entries); + values); GenerateEntry( - GenerateBundleKey(bundleKey, KEY_BUNDLE_IMPORTANCE), std::to_string(bundleInfo.GetImportance()), entries); + GenerateBundleKey(bundleKey, KEY_BUNDLE_IMPORTANCE), std::to_string(bundleInfo.GetImportance()), values); GenerateEntry( - GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE), std::to_string(bundleInfo.GetIsShowBadge()), entries); + GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE), std::to_string(bundleInfo.GetIsShowBadge()), values); GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_PRIVATE_ALLOWED), std::to_string(bundleInfo.GetIsPrivateAllowed()), - entries); + values); GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION), std::to_string(bundleInfo.GetEnableNotification()), - entries); + values); GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_POPPED_DIALOG), std::to_string(bundleInfo.GetHasPoppedDialog()), - entries); - GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_UID), std::to_string(bundleInfo.GetBundleUid()), entries); - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + values); + GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_UID), std::to_string(bundleInfo.GetBundleUid()), values); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } - DistributedKv::Status status = kvStorePtr_->PutBatch(entries); - if (status != DistributedKv::Status::SUCCESS) { - ANS_LOGE("Store bundle failed. %{public}d", status); + int32_t result = rdbDataManager_->InsertBatchData(values); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Store bundle failed. %{public}d", result); return false; } return true; @@ -681,62 +627,57 @@ bool NotificationPreferencesDatabase::ParseFromDisturbeDB(NotificationPreference ParseDoNotDisturbEndDate(info); ParseEnableAllNotification(info); - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } - DistributedKv::Status status; - std::vector entries; - status = kvStorePtr_->GetEntries(DistributedKv::Key(KEY_BUNDLE_LABEL), entries); - if (status != DistributedKv::Status::SUCCESS) { + std::unordered_map values; + int32_t result = rdbDataManager_->QueryDataBeginWithKey(KEY_BUNDLE_LABEL, values); + if (result != NativeRdb::E_OK) { ANS_LOGE("Get Bundle Info failed."); return false; } - ParseBundleFromDistureDB(info, entries); - CloseKvStore(); + ParseBundleFromDistureDB(info, values); return true; } bool NotificationPreferencesDatabase::RemoveAllDataFromDisturbeDB() { ANS_LOGD("%{public}s", __FUNCTION__); - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } - DistributedKv::Status status = dataManager_.DeleteKvStore(appId_, storeId_, KV_STORE_PATH); - return (status == DistributedKv::Status::SUCCESS); + int32_t result = rdbDataManager_->Destroy(); + return (result == NativeRdb::E_OK); } bool NotificationPreferencesDatabase::RemoveBundleFromDisturbeDB(const std::string &bundleKey) { ANS_LOGD("%{public}s", __FUNCTION__); - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } - DistributedKv::Status status; - std::vector entries; - status = kvStorePtr_->GetEntries( - DistributedKv::Key(KEY_ANS_BUNDLE + KEY_UNDER_LINE + bundleKey + KEY_UNDER_LINE), entries); + std::unordered_map values; + int32_t result = rdbDataManager_->QueryDataBeginWithKey( + (KEY_ANS_BUNDLE + KEY_UNDER_LINE + bundleKey + KEY_UNDER_LINE), values); - if (status != DistributedKv::Status::SUCCESS) { + if (result != NativeRdb::E_OK) { ANS_LOGE("Get Bundle Info failed."); - CloseKvStore(); return false; } - std::vector keys; - for (auto iter : entries) { - keys.push_back(iter.key); + std::vector keys; + for (auto iter : values) { + keys.push_back(iter.first); } - DistributedKv::Key bundleDBKey(KEY_BUNDLE_LABEL + KEY_BUNDLE_NAME + KEY_UNDER_LINE + bundleKey); + std::string bundleDBKey = KEY_BUNDLE_LABEL + KEY_BUNDLE_NAME + KEY_UNDER_LINE + bundleKey; keys.push_back(bundleDBKey); - status = kvStorePtr_->DeleteBatch(keys); - CloseKvStore(); - if (status != DistributedKv::Status::SUCCESS) { + result = rdbDataManager_->DeleteBathchData(keys); + if (result != NativeRdb::E_OK) { ANS_LOGE("delete bundle Info failed."); return false; } @@ -753,33 +694,29 @@ bool NotificationPreferencesDatabase::RemoveSlotFromDisturbeDB( return false; } - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } - DistributedKv::Status status; - std::vector slotentries; + std::unordered_map values; std::string slotType = std::to_string(type); - status = - kvStorePtr_->GetEntries(DistributedKv::Key(GenerateSlotKey(bundleKey, slotType) + KEY_UNDER_LINE), slotentries); - if (status != DistributedKv::Status::SUCCESS) { - CloseKvStore(); + int32_t result = rdbDataManager_->QueryDataBeginWithKey((GenerateSlotKey(bundleKey, slotType) + KEY_UNDER_LINE), values); + if (result != NativeRdb::E_OK) { return false; } - std::vector keys; - for (auto iter : slotentries) { - keys.push_back(iter.key); + std::vector keys; + for (auto iter : values) { + keys.push_back(iter.first); } - status = kvStorePtr_->DeleteBatch(keys); - CloseKvStore(); - if (status != DistributedKv::Status::SUCCESS) { + result = rdbDataManager_->DeleteBathchData(keys); + if (result != NativeRdb::E_OK) { ANS_LOGE("delete bundle Info failed."); return false; } - ANS_LOGD("%{public}s remove slot status %{public}d", __FUNCTION__, status); + ANS_LOGD("%{public}s remove slot result %{public}d", __FUNCTION__, result); return true; } @@ -791,38 +728,35 @@ bool NotificationPreferencesDatabase::RemoveAllSlotsFromDisturbeDB(const std::st return false; } - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } - DistributedKv::Status status; - std::vector slotsEntries; - status = kvStorePtr_->GetEntries(DistributedKv::Key(GenerateSlotKey(bundleKey) + KEY_UNDER_LINE), slotsEntries); - if (status != DistributedKv::Status::SUCCESS) { - CloseKvStore(); + std::unordered_map values; + int32_t result = rdbDataManager_->QueryDataBeginWithKey((GenerateSlotKey(bundleKey) + KEY_UNDER_LINE), values); + if (result != NativeRdb::E_OK) { return false; } - std::vector keys; - for (auto iter : slotsEntries) { - keys.push_back(iter.key); + std::vector keys; + for (auto iter : values) { + keys.push_back(iter.first); } - status = kvStorePtr_->DeleteBatch(keys); - CloseKvStore(); - ANS_LOGD("%{public}s remove all slots status %{public}d", __FUNCTION__, status); - return (status == DistributedKv::Status::SUCCESS); + result = rdbDataManager_->DeleteBathchData(keys); + ANS_LOGD("%{public}s remove all slots result %{public}d", __FUNCTION__, result); + return (result == NativeRdb::E_OK); } bool NotificationPreferencesDatabase::StoreDeathRecipient() { ANS_LOGW("distribute remote died"); - kvStorePtr_ = nullptr; + rdbDataManager_ = nullptr; return true; } template -DistributedKv::Status NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( +int32_t NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( const std::string &bundleKey, const BundleType &type, const T &t) { std::string keyStr; @@ -848,29 +782,26 @@ DistributedKv::Status NotificationPreferencesDatabase::PutBundlePropertyToDistur default: break; } - DistributedKv::Key key(keyStr); - DistributedKv::Value value(std::to_string(t)); - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); - return DistributedKv::Status::ERROR; + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; } - DistributedKv::Status status = kvStorePtr_->Put(key, value); - CloseKvStore(); - return status; + std::string valueStr = std::to_string(t); + int32_t result = rdbDataManager_->InsertData(keyStr, valueStr); + return result; } bool NotificationPreferencesDatabase::PutBundleToDisturbeDB( const std::string &bundleKey, const NotificationPreferencesInfo::BundleInfo &bundleInfo) { - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } ANS_LOGD("Key not fund, so create a bundle, bundle key is %{public}s.", bundleKey.c_str()); - DistributedKv::Key bundleDBKey(bundleKey); - DistributedKv::Value bundleValue(GenerateBundleLablel(bundleInfo)); - if (kvStorePtr_->Put(bundleDBKey, bundleValue) != DistributedKv::Status::SUCCESS) { + int32_t result = rdbDataManager_->InsertData(bundleKey, GenerateBundleLablel(bundleInfo)); + if (result != NativeRdb::E_OK) { ANS_LOGE("Store bundle name to db is failed."); return false; } @@ -878,23 +809,17 @@ bool NotificationPreferencesDatabase::PutBundleToDisturbeDB( if (!PutBundlePropertyValueToDisturbeDB(bundleInfo)) { return false; } - return true; } void NotificationPreferencesDatabase::GenerateEntry( - const std::string &key, const std::string &value, std::vector &entries) const + const std::string &key, const std::string &value, std::unordered_map &values) const { - DistributedKv::Entry entry; - DistributedKv::Key dbKey(key); - DistributedKv::Value dbValue(value); - entry.key = dbKey; - entry.value = dbValue; - entries.push_back(entry); + values.emplace(key, value); } bool NotificationPreferencesDatabase::SlotToEntry(const std::string &bundleName, const int32_t &bundleUid, - const sptr &slot, std::vector &entries) + const sptr &slot, std::unordered_map &values) { if (slot == nullptr) { ANS_LOGE("Notification slot is nullptr."); @@ -906,57 +831,53 @@ bool NotificationPreferencesDatabase::SlotToEntry(const std::string &bundleName, } std::string bundleKey = bundleName + std::to_string(bundleUid); - GenerateSlotEntry(bundleKey, slot, entries); + GenerateSlotEntry(bundleKey, slot, values); return true; } void NotificationPreferencesDatabase::GenerateSlotEntry(const std::string &bundleKey, - const sptr &slot, std::vector &entries) const + const sptr &slot, std::unordered_map &values) const { std::string slotType = std::to_string(slot->GetType()); - GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_TYPE), std::to_string(slot->GetType()), entries); - GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ID), slot->GetId(), entries); - GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_NAME), slot->GetName(), entries); - GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_DESCRIPTION), slot->GetDescription(), entries); - GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LEVEL), std::to_string(slot->GetLevel()), entries); + GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_TYPE), std::to_string(slot->GetType()), values); + GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ID), slot->GetId(), values); + GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_NAME), slot->GetName(), values); + GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_DESCRIPTION), slot->GetDescription(), values); + GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LEVEL), std::to_string(slot->GetLevel()), values); GenerateEntry( - GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SHOW_BADGE), std::to_string(slot->IsShowBadge()), entries); + GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SHOW_BADGE), std::to_string(slot->IsShowBadge()), values); GenerateEntry( - GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_LIGHT), std::to_string(slot->CanEnableLight()), entries); + GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_LIGHT), std::to_string(slot->CanEnableLight()), values); GenerateEntry( - GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_VRBRATION), std::to_string(slot->CanVibrate()), entries); + GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_VRBRATION), std::to_string(slot->CanVibrate()), values); GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LED_LIGHT_COLOR), - std::to_string(slot->GetLedLightColor()), - entries); + std::to_string(slot->GetLedLightColor()), values); GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_LOCKSCREEN_VISIBLENESS), - std::to_string(static_cast(slot->GetLockScreenVisibleness())), - entries); - GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SOUND), slot->GetSound().ToString(), entries); + std::to_string(static_cast(slot->GetLockScreenVisibleness())), values); + GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_SOUND), slot->GetSound().ToString(), values); GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLE_BYPASS_DND), - std::to_string(slot->IsEnableBypassDnd()), - entries); + std::to_string(slot->IsEnableBypassDnd()), values); GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_VIBRATION_STYLE), - VectorToString(slot->GetVibrationStyle()), - entries); - GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLED), std::to_string(slot->GetEnable()), entries); + VectorToString(slot->GetVibrationStyle()), values); + GenerateEntry(GenerateSlotKey(bundleKey, slotType, KEY_SLOT_ENABLED), std::to_string(slot->GetEnable()), values); } void NotificationPreferencesDatabase::ParseBundleFromDistureDB( - NotificationPreferencesInfo &info, const std::vector &entries) + NotificationPreferencesInfo &info, const std::unordered_map &values) { - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return; } - for (auto item : entries) { - std::string bundleKey = item.value.ToString(); + for (auto item : values) { + std::string bundleKey = item.second; ANS_LOGD("Bundle name is %{public}s.", bundleKey.c_str()); - std::vector bundleEntries; - kvStorePtr_->GetEntries(DistributedKv::Key(GenerateBundleKey(bundleKey)), bundleEntries); + std::unordered_map bundleEntries; + rdbDataManager_->QueryDataBeginWithKey((GenerateBundleKey(bundleKey)), bundleEntries); ANS_LOGD("Bundle key is %{public}s.", GenerateBundleKey(bundleKey).c_str()); NotificationPreferencesInfo::BundleInfo bunldeInfo; for (auto bundleEntry : bundleEntries) { - if (IsSlotKey(GenerateBundleKey(bundleKey), bundleEntry.key.ToString())) { + if (IsSlotKey(GenerateBundleKey(bundleKey), bundleEntry.first)) { ParseSlotFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry); } else { ParseBundlePropertyFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry); @@ -968,9 +889,9 @@ void NotificationPreferencesDatabase::ParseBundleFromDistureDB( } void NotificationPreferencesDatabase::ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, - const std::string &bundleKey, const DistributedKv::Entry &entry) + const std::string &bundleKey, const std::pair &entry) { - std::string slotKey = entry.key.ToString(); + std::string slotKey = entry.first; std::string typeStr = SubUniqueIdentifyFromString(GenerateSlotKey(bundleKey) + KEY_UNDER_LINE, slotKey); NotificationConstant::SlotType slotType = static_cast(StringToInt(typeStr)); sptr slot = nullptr; @@ -984,11 +905,11 @@ void NotificationPreferencesDatabase::ParseSlotFromDisturbeDB(NotificationPrefer void NotificationPreferencesDatabase::ParseBundlePropertyFromDisturbeDB( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, - const DistributedKv::Entry &entry) + const std::pair &entry) { - std::string typeStr = FindLastString(GenerateBundleKey(bundleKey), entry.key.ToString()); - std::string valueStr = entry.value.ToString(); - ANS_LOGD("DB key = %{public}s , type str %{public}s", entry.key.ToString().c_str(), typeStr.c_str()); + std::string typeStr = FindLastString(GenerateBundleKey(bundleKey), entry.first); + std::string valueStr = entry.second; + ANS_LOGD("DB key = %{public}s , type str %{public}s", entry.first.c_str(), typeStr.c_str()); auto iter = bundleMap_.find(typeStr); if (iter != bundleMap_.end()) { @@ -998,14 +919,14 @@ void NotificationPreferencesDatabase::ParseBundlePropertyFromDisturbeDB( } void NotificationPreferencesDatabase::ParseSlot( - const std::string &findString, sptr &slot, const DistributedKv::Entry &entry) + const std::string &findString, sptr &slot, const std::pair &entry) { - std::string typeStr = FindLastString(findString, entry.key.ToString()); - std::string valueStr = entry.value.ToString(); + std::string typeStr = FindLastString(findString, entry.first); + std::string valueStr = entry.second; ANS_LOGD("db key = %{public}s , %{public}s : %{public}s ", - entry.key.ToString().c_str(), + entry.first.c_str(), typeStr.c_str(), - entry.value.ToString().c_str()); + entry.second.c_str()); auto iter = slotMap_.find(typeStr); if (iter != slotMap_.end()) { @@ -1015,7 +936,7 @@ void NotificationPreferencesDatabase::ParseSlot( if (!typeStr.compare(KEY_SLOT_VIBRATION_STYLE)) { GetValueFromDisturbeDB(findString + KEY_SLOT_ENABLE_VRBRATION, - [&](DistributedKv::Value &value) { ParseSlotEnableVrbration(slot, value.ToString()); }); + [&](std::string &value) { ParseSlotEnableVrbration(slot, value); }); } } @@ -1334,17 +1255,17 @@ void NotificationPreferencesDatabase::GetDoNotDisturbType(NotificationPreference std::string key = std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId)); GetValueFromDisturbeDB( - key, [&](const DistributedKv::Status &status, DistributedKv::Value &value) { + key, [&](const int32_t &status, std::string &value) { sptr disturbDate = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); info.GetDoNotDisturbDate(userId, disturbDate); - if (status == DistributedKv::Status::KEY_NOT_FOUND) { + if (status == NativeRdb::E_ERROR) { PutDoNotDisturbDate(userId, disturbDate); - } else if (status == DistributedKv::Status::SUCCESS) { - if (!value.ToString().empty()) { + } else if (status == NativeRdb::E_OK) { + if (!value.empty()) { if (disturbDate != nullptr) { disturbDate->SetDoNotDisturbType( - (NotificationConstant::DoNotDisturbType)StringToInt(value.ToString())); + (NotificationConstant::DoNotDisturbType)StringToInt(value)); } } } else { @@ -1359,16 +1280,16 @@ void NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(NotificationPrefe std::string key = std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); GetValueFromDisturbeDB( - key, [&](const DistributedKv::Status &status, DistributedKv::Value &value) { + key, [&](const int32_t &status, std::string &value) { sptr disturbDate = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); info.GetDoNotDisturbDate(userId, disturbDate); - if (status == DistributedKv::Status::KEY_NOT_FOUND) { + if (status == NativeRdb::E_ERROR) { PutDoNotDisturbDate(userId, disturbDate); - } else if (status == DistributedKv::Status::SUCCESS) { - if (!value.ToString().empty()) { + } else if (status == NativeRdb::E_OK) { + if (!value.empty()) { if (disturbDate != nullptr) { - disturbDate->SetBeginDate(StringToInt64(value.ToString())); + disturbDate->SetBeginDate(StringToInt64(value)); } } } else { @@ -1383,16 +1304,16 @@ void NotificationPreferencesDatabase::GetDoNotDisturbEndDate(NotificationPrefere std::string key = std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); GetValueFromDisturbeDB( - key, [&](const DistributedKv::Status &status, DistributedKv::Value &value) { + key, [&](const int32_t &status, std::string &value) { sptr disturbDate = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); info.GetDoNotDisturbDate(userId, disturbDate); - if (status == DistributedKv::Status::KEY_NOT_FOUND) { + if (status == NativeRdb::E_ERROR) { PutDoNotDisturbDate(userId, disturbDate); - } else if (status == DistributedKv::Status::SUCCESS) { - if (!value.ToString().empty()) { + } else if (status == NativeRdb::E_OK) { + if (!value.empty()) { if (disturbDate != nullptr) { - disturbDate->SetEndDate(StringToInt64(value.ToString())); + disturbDate->SetEndDate(StringToInt64(value)); } } } else { @@ -1407,17 +1328,17 @@ void NotificationPreferencesDatabase::GetEnableAllNotification(NotificationPrefe std::string key = std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId)); GetValueFromDisturbeDB( - key, [&](const DistributedKv::Status &status, DistributedKv::Value &value) { - if (status == DistributedKv::Status::KEY_NOT_FOUND) { + key, [&](const int32_t &status, std::string &value) { + if (status == NativeRdb::E_ERROR) { bool enable = true; if (!info.GetEnabledAllNotification(userId, enable)) { info.SetEnabledAllNotification(userId, enable); ANS_LOGW("Enable setting not found, default true."); } PutNotificationsEnabled(userId, enable); - } else if (status == DistributedKv::Status::SUCCESS) { - if (!value.ToString().empty()) { - info.SetEnabledAllNotification(userId, static_cast(StringToInt(value.ToString()))); + } else if (status == NativeRdb::E_OK) { + if (!value.empty()) { + info.SetEnabledAllNotification(userId, static_cast(StringToInt(value))); } } else { ANS_LOGW("Parse enable all notification failed, use default value."); @@ -1428,17 +1349,15 @@ void NotificationPreferencesDatabase::GetEnableAllNotification(NotificationPrefe bool NotificationPreferencesDatabase::RemoveNotificationEnable(const int32_t userId) { ANS_LOGD("%{public}s", __FUNCTION__); - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } std::string key = std::string(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId)); - DistributedKv::Key enableKey(key); - DistributedKv::Status status = kvStorePtr_->Delete(enableKey); - CloseKvStore(); - if (status != DistributedKv::Status::SUCCESS) { + int32_t result = rdbDataManager_->DeleteData(key); + if (result != NativeRdb::E_OK) { ANS_LOGE("delete bundle Info failed."); return false; } @@ -1450,8 +1369,8 @@ bool NotificationPreferencesDatabase::RemoveNotificationEnable(const int32_t use bool NotificationPreferencesDatabase::RemoveDoNotDisturbDate(const int32_t userId) { ANS_LOGD("%{public}s", __FUNCTION__); - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } @@ -1462,15 +1381,14 @@ bool NotificationPreferencesDatabase::RemoveDoNotDisturbDate(const int32_t userI std::string endDateKey = std::string(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); - std::vector keys = { - DistributedKv::Key(typeKey), - DistributedKv::Key(beginDateKey), - DistributedKv::Key(endDateKey) + std::vector keys = { + typeKey, + beginDateKey, + endDateKey }; - - DistributedKv::Status status = kvStorePtr_->DeleteBatch(keys); - CloseKvStore(); - if (status != DistributedKv::Status::SUCCESS) { + + int32_t result = rdbDataManager_->DeleteBathchData(keys); + if (result != NativeRdb::E_OK) { ANS_LOGE("delete DoNotDisturb date failed."); return false; } @@ -1481,16 +1399,14 @@ bool NotificationPreferencesDatabase::RemoveDoNotDisturbDate(const int32_t userI bool NotificationPreferencesDatabase::RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid) { - if (!CheckKvStore()) { - ANS_LOGE("KvStore is nullptr."); + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); return false; } std::string key = KEY_BUNDLE_LABEL + bundleName + std::to_string(uid); - DistributedKv::Key enableKey(key); - DistributedKv::Status status = kvStorePtr_->Delete(enableKey); - CloseKvStore(); - if (status != DistributedKv::Status::SUCCESS) { + int32_t result = rdbDataManager_->DeleteData(key); + if (result != NativeRdb::E_OK) { ANS_LOGE("Delete ans bundle db info failed, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid); return false; } diff --git a/services/ans/src/notification_rdb_data_mgr.cpp b/services/ans/src/notification_rdb_data_mgr.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b218b68d73604e8a8519ef9f6739bd0e0024f10 --- /dev/null +++ b/services/ans/src/notification_rdb_data_mgr.cpp @@ -0,0 +1,342 @@ +/* + * Copyright (c) 2022 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 "notification_rdb_data_mgr.h" + +#include "ans_log_wrapper.h" + +namespace OHOS { +namespace Notification { +namespace { +const std::string NOTIFICATION_KEY = "KEY"; +const std::string NOTIFICATION_VALUE = "VALUE"; +const int32_t NOTIFICATION_KEY_INDEX = 0; +const int32_t NOTIFICATION_VALUE_INDEX = 1; +} // namespace +RdbStoreDataCallBackNotificationStorage::RdbStoreDataCallBackNotificationStorage( + const NotificationRdbConfig ¬ificationRdbConfig): notificationRdbConfig_(notificationRdbConfig) +{ + ANS_LOGI("create rdb store callback instance"); +} + +RdbStoreDataCallBackNotificationStorage::~RdbStoreDataCallBackNotificationStorage() +{ + ANS_LOGI("destroy rdb store callback instance"); +} + +int32_t RdbStoreDataCallBackNotificationStorage::OnCreate(NativeRdb::RdbStore &rdbStore) +{ + ANS_LOGI("OnCreate"); + return NativeRdb::E_OK; +} + +int32_t RdbStoreDataCallBackNotificationStorage::OnUpgrade( + NativeRdb::RdbStore &rdbStore, int32_t oldVersion, int32_t newVersion) +{ + ANS_LOGI("OnUpgrade currentVersion: %{plubic}d, targetVersion: %{plubic}d", + oldVersion, newVersion); + return NativeRdb::E_OK; +} + +int32_t RdbStoreDataCallBackNotificationStorage::OnDowngrade( + NativeRdb::RdbStore &rdbStore, int currentVersion, int targetVersion) +{ + ANS_LOGI("OnDowngrade currentVersion: %{plubic}d, targetVersion: %{plubic}d", + currentVersion, targetVersion); + return NativeRdb::E_OK; +} + +int32_t RdbStoreDataCallBackNotificationStorage::OnOpen(NativeRdb::RdbStore &rdbStore) +{ + ANS_LOGI("OnOpen"); + int ret = NativeRdb::E_OK; + if (hasTableInit_) { + return ret; + } + std::string createTableSql = "CREATE TABLE IF NOT EXISTS " + notificationRdbConfig_.tableName + + " (KEY TEXT NOT NULL PRIMARY KEY, VALUE TEXT NOT NULL);"; + ret = rdbStore.ExecuteSql(createTableSql); + if (ret == NativeRdb::E_OK) { + hasTableInit_ = true; + } + return ret; +} + +int32_t RdbStoreDataCallBackNotificationStorage::onCorruption(std::string databaseFile) +{ + return NativeRdb::E_OK; +} + +NotificationDataMgr::NotificationDataMgr(const NotificationRdbConfig ¬ificationRdbConfig) + : notificationRdbConfig_(notificationRdbConfig) +{ + ANS_LOGI("create notification rdb data manager"); +} + +int32_t NotificationDataMgr::Init() +{ + ANS_LOGI("Create rdbStore"); + + int32_t ret = NativeRdb::E_OK; + + NativeRdb::RdbStoreConfig rdbStoreConfig( + notificationRdbConfig_.dbPath + notificationRdbConfig_.dbName, + NativeRdb::StorageMode::MODE_DISK, + false, + std::vector(), + notificationRdbConfig_.journalMode, + notificationRdbConfig_.syncMode); + RdbStoreDataCallBackNotificationStorage rdbDataCallBack_(notificationRdbConfig_); + rdbStore_ = NativeRdb::RdbHelper::GetRdbStore(rdbStoreConfig, notificationRdbConfig_.version, rdbDataCallBack_, ret); + if (rdbStore_ == nullptr) { + ANS_LOGE("notification rdb init fail"); + return NativeRdb::E_ERROR; + } + return NativeRdb::E_OK; +} + +int32_t NotificationDataMgr::Destroy() +{ + ANS_LOGI("Destory rdbStore"); + + if (rdbStore_ == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + + rdbStore_ = nullptr; + int32_t ret = NativeRdb::RdbHelper::DeleteRdbStore(notificationRdbConfig_.dbPath + notificationRdbConfig_.dbName); + if (ret != NativeRdb::E_OK) { + ANS_LOGE("failed to destroy db store"); + return NativeRdb::E_ERROR; + } + ANS_LOGI("destroy db store successfully"); + return NativeRdb::E_OK; +} + +int32_t NotificationDataMgr::InsertData(const std::string &key, const std::string &value) +{ + ANS_LOGI("InsertData start"); + if (rdbStore_ == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + { + std::lock_guard lock(rdbStorePtrMutex_); + int64_t rowId = -1; + NativeRdb::ValuesBucket valuesBucket; + valuesBucket.PutString(NOTIFICATION_KEY, key); + valuesBucket.PutString(NOTIFICATION_VALUE, value); + int32_t ret = rdbStore_->InsertWithConflictResolution( + rowId, notificationRdbConfig_.tableName, valuesBucket, NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE); + if (ret != NativeRdb::E_OK) { + ANS_LOGE("Insert operation failed, result: %{public}d, key=%{public}s.", ret, key.c_str()); + return NativeRdb::E_ERROR; + } + } + return NativeRdb::E_OK; +} + +int32_t NotificationDataMgr::InsertBatchData(const std::unordered_map &values) +{ + ANS_LOGI("InsertBatchData start"); + if (rdbStore_ == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + { + std::lock_guard lock(rdbStorePtrMutex_); + int64_t rowId = -1; + for (auto &value : values) { + NativeRdb::ValuesBucket valuesBucket; + valuesBucket.PutString(NOTIFICATION_KEY, value.first); + valuesBucket.PutString(NOTIFICATION_VALUE, value.second); + int32_t ret = rdbStore_->InsertWithConflictResolution( + rowId, notificationRdbConfig_.tableName, valuesBucket, NativeRdb::ConflictResolution::ON_CONFLICT_REPLACE); + if (ret != NativeRdb::E_OK) { + ANS_LOGE("Insert batch operation failed, result: %{public}d.", ret); + return NativeRdb::E_ERROR; + } + } + } + return NativeRdb::E_OK; +} + +int32_t NotificationDataMgr::DeleteData(const std::string &key) +{ + ANS_LOGI("DeleteData start"); + if (rdbStore_ == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + { + std::lock_guard lock(rdbStorePtrMutex_); + int32_t rowId = -1; + NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName); + absRdbPredicates.EqualTo(NOTIFICATION_KEY, key); + int32_t ret = rdbStore_->Delete(rowId, absRdbPredicates); + if (ret != NativeRdb::E_OK) { + ANS_LOGE("Delete operation failed, result: %{public}d, key=%{public}s.", + ret, key.c_str()); + return NativeRdb::E_ERROR; + } + } + return NativeRdb::E_OK; +} + +int32_t NotificationDataMgr::DeleteBathchData(const std::vector &keys) +{ + ANS_LOGI("Delete Bathch Data start"); + if (rdbStore_ == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + { + std::lock_guard lock(rdbStorePtrMutex_); + int32_t rowId = -1; + NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName); + for (auto key : keys) { + rowId = -1; + absRdbPredicates.EqualTo(NOTIFICATION_KEY, key); + int32_t ret = rdbStore_->Delete(rowId, absRdbPredicates); + if (ret != NativeRdb::E_OK) { + ANS_LOGE("Delete Batch operation failed, result: %{public}d, key=%{public}s.", + ret, key.c_str()); + return NativeRdb::E_ERROR; + } + } + } + return NativeRdb::E_OK; +} + +int32_t NotificationDataMgr::QueryData(const std::string &key, std::string &value) +{ + ANS_LOGI("QueryData start"); + if (rdbStore_ == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + { + std::lock_guard lock(rdbStorePtrMutex_); + NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName); + absRdbPredicates.EqualTo(NOTIFICATION_KEY, key); + auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector()); + if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) { + ANS_LOGI("absSharedResultSet failed"); + return NativeRdb::E_ERROR; + } + + int32_t ret = absSharedResultSet->GoToFirstRow(); + if (ret != NativeRdb::E_OK) { + ANS_LOGI("GoToFirstRow failed.It is empty!, key=%{public}s", key.c_str()); + return NativeRdb::E_OK; + } + ret = absSharedResultSet->GetString(NOTIFICATION_VALUE_INDEX, value); + if (ret != NativeRdb::E_OK) { + ANS_LOGE("GetString value failed"); + return NativeRdb::E_ERROR; + } + } + return NativeRdb::E_OK; +} + +int32_t NotificationDataMgr::QueryDataBeginWithKey(const std::string &key, std::unordered_map &values) +{ + ANS_LOGI("QueryData BeginWithKey start"); + if (rdbStore_ == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + { + std::lock_guard lock(rdbStorePtrMutex_); + NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName); + absRdbPredicates.BeginsWith(NOTIFICATION_KEY, key); + auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector()); + if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) { + ANS_LOGE("absSharedResultSet failed"); + return NativeRdb::E_ERROR; + } + + int32_t ret = absSharedResultSet->GoToFirstRow(); + if (ret != NativeRdb::E_OK) { + ANS_LOGI("GoToFirstRow failed.It is empty!, key=%{public}s", key.c_str()); + return NativeRdb::E_OK; + } + + do { + std::string resultKey; + ret = absSharedResultSet->GetString(NOTIFICATION_KEY_INDEX, resultKey); + if (ret != NativeRdb::E_OK) { + ANS_LOGE("GetString key failed"); + return NativeRdb::E_ERROR; + } + + std::string resultValue; + ret = absSharedResultSet->GetString(NOTIFICATION_VALUE_INDEX, resultValue); + if (ret != NativeRdb::E_OK) { + ANS_LOGE("GetString value failed"); + return NativeRdb::E_ERROR; + } + + values.emplace(resultKey, resultValue); + } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK); + } + + return NativeRdb::E_OK; +} + +int32_t NotificationDataMgr::QueryAllData(std::unordered_map &datas) +{ + ANS_LOGI("QueryAllData start"); + if (rdbStore_ == nullptr) { + ANS_LOGE("notification rdb is null"); + return NativeRdb::E_ERROR; + } + { + std::lock_guard lock(rdbStorePtrMutex_); + NativeRdb::AbsRdbPredicates absRdbPredicates(notificationRdbConfig_.tableName); + auto absSharedResultSet = rdbStore_->Query(absRdbPredicates, std::vector()); + if (absSharedResultSet == nullptr || !absSharedResultSet->HasBlock()) { + ANS_LOGE("absSharedResultSet failed"); + return NativeRdb::E_ERROR; + } + + int32_t ret = absSharedResultSet->GoToFirstRow(); + if (ret != NativeRdb::E_OK) { + ANS_LOGI("GoToFirstRow failed. It is empty!"); + return NativeRdb::E_OK; + } + + do { + std::string resultKey; + ret = absSharedResultSet->GetString(NOTIFICATION_KEY_INDEX, resultKey); + if (ret != NativeRdb::E_OK) { + ANS_LOGE("GetString key failed"); + return NativeRdb::E_ERROR; + } + + std::string resultValue; + ret = absSharedResultSet->GetString(NOTIFICATION_VALUE_INDEX, resultValue); + if (ret != NativeRdb::E_OK) { + ANS_LOGE("GetString value failed"); + return NativeRdb::E_ERROR; + } + + datas.emplace(resultKey, resultValue); + } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK); + } + return NativeRdb::E_OK; +} +} +} \ No newline at end of file diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index b6e5e6c1ed8d633239f497d2bd581dd71e478d62..1819184404dd80cdbe34248fa807ee01d95cd7ad 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -37,6 +37,7 @@ ohos_unittest("reminder_unit_test") { "${services_path}/ans/src/notification_preferences.cpp", "${services_path}/ans/src/notification_preferences_database.cpp", "${services_path}/ans/src/notification_preferences_info.cpp", + "${services_path}/ans/src/notification_rdb_data_mgr.cpp", "${services_path}/ans/src/notification_slot_filter.cpp", "${services_path}/ans/src/notification_subscriber_manager.cpp", "${services_path}/ans/src/permission_filter.cpp", @@ -130,6 +131,7 @@ ohos_unittest("ans_unit_test") { "${services_path}/ans/src/notification_preferences.cpp", "${services_path}/ans/src/notification_preferences_database.cpp", "${services_path}/ans/src/notification_preferences_info.cpp", + "${services_path}/ans/src/notification_rdb_data_mgr.cpp", "${services_path}/ans/src/notification_slot_filter.cpp", "${services_path}/ans/src/notification_subscriber_manager.cpp", "${services_path}/ans/src/permission_filter.cpp", @@ -137,9 +139,6 @@ ohos_unittest("ans_unit_test") { "${services_path}/ans/src/reminder_event_manager.cpp", "${services_path}/ans/src/reminder_timer_info.cpp", "${services_path}/ans/src/system_event_observer.cpp", - "advanced_notification_service_ability_test.cpp", - "advanced_notification_service_test.cpp", - "bundle_manager_helper_test.cpp", "mock/blob.cpp", "mock/distributed_kv_data_manager.cpp", "mock/mock_access_token_helper.cpp", @@ -148,13 +147,7 @@ ohos_unittest("ans_unit_test") { "mock/mock_event_handler.cpp", "mock/mock_ipc.cpp", "mock/mock_single_kv_store.cpp", - "notification_hisysevent_test.cpp", "notification_preferences_database_test.cpp", - "notification_preferences_test.cpp", - "notification_slot_filter_test.cpp", - "notification_subscriber_manager_test.cpp", - "permission_filter_test.cpp", - "system_event_observer_test.cpp", ] configs = [ "//commonlibrary/c_utils/base:utils_config" ] diff --git a/services/ans/test/unittest/notification_preferences_database_test.cpp b/services/ans/test/unittest/notification_preferences_database_test.cpp index ef89347c3ec076931feac4a6f7a507d5ac48c356..3bb3f8c8c4c6fb7317c901ad26ad84b91d002e1f 100644 --- a/services/ans/test/unittest/notification_preferences_database_test.cpp +++ b/services/ans/test/unittest/notification_preferences_database_test.cpp @@ -365,9 +365,8 @@ HWTEST_F(NotificationPreferencesDatabaseTest, RemoveSlotFromDisturbeDB_00100, Fu sptr slot1 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); slots.push_back(slot1); EXPECT_TRUE(preferncesDB_->PutSlotsToDisturbeDB(bundleName_, bundleUid_, slots)); - EXPECT_TRUE(preferncesDB_->RemoveSlotFromDisturbeDB( - bundleName_, OHOS::Notification::NotificationConstant::SlotType::SOCIAL_COMMUNICATION)); + bundleName_ + std::to_string(bundleUid_), OHOS::Notification::NotificationConstant::SlotType::SOCIAL_COMMUNICATION)); } /** @@ -391,16 +390,6 @@ HWTEST_F(NotificationPreferencesDatabaseTest, StoreDeathRecipient_00100, Functio EXPECT_TRUE(preferncesDB_->StoreDeathRecipient()); } -/** - * @tc.name : GetKvStore_00100 - * @tc.number : - * @tc.desc : Open disturbe DB, return is SUCCESS. - */ -HWTEST_F(NotificationPreferencesDatabaseTest, GetKvStore_00100, Function | SmallTest | Level1) -{ - EXPECT_EQ(OHOS::DistributedKv::Status::SUCCESS, preferncesDB_->GetKvStore()); -} - /** * @tc.name : CheckKvStore_00100 * @tc.number : @@ -408,7 +397,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, GetKvStore_00100, Function | Small */ HWTEST_F(NotificationPreferencesDatabaseTest, CheckKvStore_00100, Function | SmallTest | Level1) { - EXPECT_TRUE(preferncesDB_->CheckKvStore()); + EXPECT_TRUE(preferncesDB_->CheckRdbStore()); } /** @@ -430,8 +419,8 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutBundlePropertyValueToDisturbeDB HWTEST_F(NotificationPreferencesDatabaseTest, ChangeSlotToEntry_00100, Function | SmallTest | Level1) { sptr slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); - std::vector entries; - EXPECT_TRUE(preferncesDB_->SlotToEntry(bundleName_, bundleUid_, slot, entries)); + std::unordered_map values; + EXPECT_TRUE(preferncesDB_->SlotToEntry(bundleName_, bundleUid_, slot, values)); } /** @@ -504,11 +493,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseBundlePropertyFromDisturbeDB_ bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_name"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_name"; + entry.second = "1"; preferncesDB_->ParseBundlePropertyFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -522,11 +509,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseBundlePropertyFromDisturbeDB_ bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_importance"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_importance"; + entry.second = "1"; preferncesDB_->ParseBundlePropertyFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -540,11 +525,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseBundlePropertyFromDisturbeDB_ bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_showBadge"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_showBadge"; + entry.second = "1"; preferncesDB_->ParseBundlePropertyFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -558,11 +541,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseBundlePropertyFromDisturbeDB_ bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_badgeTotalNum"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_badgeTotalNum"; + entry.second = "1"; preferncesDB_->ParseBundlePropertyFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -576,11 +557,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseBundlePropertyFromDisturbeDB_ bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_privateAllowed"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_privateAllowed"; + entry.second = "1"; preferncesDB_->ParseBundlePropertyFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -594,11 +573,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseBundlePropertyFromDisturbeDB_ bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_enabledNotification"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_enabledNotification"; + entry.second = "1"; preferncesDB_->ParseBundlePropertyFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -612,11 +589,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseBundlePropertyFromDisturbeDB_ bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_poppedDialog"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_poppedDialog"; + entry.second = "1"; preferncesDB_->ParseBundlePropertyFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -630,11 +605,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseBundlePropertyFromDisturbeDB_ bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_uid"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_uid"; + entry.second = "1"; preferncesDB_->ParseBundlePropertyFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -648,11 +621,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_00100, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_id"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_id"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -666,11 +637,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_00200, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_name"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_name"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -684,11 +653,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_00300, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_description"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_description"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -702,11 +669,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_00400, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_level"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_level"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -720,11 +685,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_00500, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_showBadge"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_showBadge"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -738,11 +701,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_00600, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_enableLight"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_enableLight"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -756,11 +717,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_00700, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_enableVibration"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_enableVibration"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -774,11 +733,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_00800, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_ledLightColor"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_ledLightColor"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -792,11 +749,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_00900, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_lockscreenVisibleness"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_lockscreenVisibleness"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -810,11 +765,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_01000, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_sound"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_sound"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -828,11 +781,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_01100, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_vibrationSytle"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_vibrationSytle"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -846,11 +797,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_01200, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_enableBypassDnd"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_enableBypassDnd"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -864,11 +813,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ParseSlotFromDisturbeDB_01300, Fun bundleInfo.SetBundleName(bundleName_); bundleInfo.SetBundleUid(bundleUid_); std::string bundleKey = "bundleKey"; - DistributedKv::Entry entry; - DistributedKv::Key dbKey("ans_bundle_bundleKey_slot_type_1_enabled"); - DistributedKv::Value dbValue("1"); - entry.key = dbKey; - entry.value = dbValue; + std::pair entry; + entry.first = "ans_bundle_bundleKey_slot_type_1_enabled"; + entry.second = "1"; preferncesDB_->ParseSlotFromDisturbeDB(bundleInfo, bundleKey, entry); } @@ -932,8 +879,8 @@ HWTEST_F(NotificationPreferencesDatabaseTest, RemoveAllSlotsFromDisturbeDB_00200 */ HWTEST_F(NotificationPreferencesDatabaseTest, ChangeSlotToEntry_00200, Function | SmallTest | Level1) { - std::vector entries; - EXPECT_EQ(preferncesDB_->SlotToEntry(bundleName_, bundleUid_, nullptr, entries), false); + std::unordered_map values; + EXPECT_EQ(preferncesDB_->SlotToEntry(bundleName_, bundleUid_, nullptr, values), false); } /** diff --git a/services/test/moduletest/BUILD.gn b/services/test/moduletest/BUILD.gn index f61ff50735b1250f9440a29e772e7ff540629780..c0532b51b92be4ed2fd00cbbbf4490419d1b5108 100644 --- a/services/test/moduletest/BUILD.gn +++ b/services/test/moduletest/BUILD.gn @@ -36,6 +36,7 @@ ohos_moduletest("ans_module_test") { "${services_path}/ans/src/notification_preferences.cpp", "${services_path}/ans/src/notification_preferences_database.cpp", "${services_path}/ans/src/notification_preferences_info.cpp", + "${services_path}/ans/src/notification_rdb_data_mgr.cpp", "${services_path}/ans/src/notification_slot_filter.cpp", "${services_path}/ans/src/notification_subscriber_manager.cpp", "${services_path}/ans/src/permission_filter.cpp",