From a8e91b99ff244253898feac44feca7455e7ff848 Mon Sep 17 00:00:00 2001 From: luojingsong Date: Mon, 28 Oct 2024 16:25:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=99=BA=E8=83=BD=E6=8F=90=E9=86=92?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: luojingsong Change-Id: I5d83dada1c42a7370d7094b94758d5a7db075799 --- .../ans/src/notification_subscriber.cpp | 4 +- .../include/advanced_notification_service.h | 1 + .../ans/include/notification_preferences.h | 10 ++++ .../notification_preferences_database.h | 3 +- services/ans/include/smart_reminder_center.h | 2 +- .../ans/src/advanced_notification_service.cpp | 1 + .../ans/src/advanced_notification_utils.cpp | 57 +++++++++++++++++++ services/ans/src/notification_preferences.cpp | 17 ++++++ .../src/notification_preferences_database.cpp | 45 ++++++++++++++- .../smart_reminder_center.cpp | 14 ++--- 10 files changed, 137 insertions(+), 17 deletions(-) diff --git a/frameworks/ans/src/notification_subscriber.cpp b/frameworks/ans/src/notification_subscriber.cpp index 128d91878..51852b790 100644 --- a/frameworks/ans/src/notification_subscriber.cpp +++ b/frameworks/ans/src/notification_subscriber.cpp @@ -56,8 +56,8 @@ bool NotificationSubscriber::ProcessSyncDecision( } auto flagIter = flagsMap->find(deviceType); if (flagIter != flagsMap->end() && flagIter->second != nullptr) { - ANS_LOGI("SetFlags-before filte, notificationKey = %{public}s flagIter flags = %{public}d", - request->GetKey().c_str(), flagIter->second->GetReminderFlags()); + ANS_LOGI("SetFlags-before filte, notificationKey = %{public}s flagIter flags = %{public}d deviceType = %{public}s", + request->GetKey().c_str(), flagIter->second->GetReminderFlags(), deviceType.c_str()); std::shared_ptr tempFlags = request->GetFlags(); tempFlags->SetSoundEnabled(DowngradeReminder(tempFlags->IsSoundEnabled(), flagIter->second->IsSoundEnabled())); tempFlags->SetVibrationEnabled( diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 3a478e746..f8abe7a24 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1413,6 +1413,7 @@ private: const sptr &slot, const sptr &bundle, bool enabled, bool isForceControl); + void ResetDistributedEnabled(); private: static sptr instance_; static std::mutex instanceMutex_; diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index a5ead6fd1..8d8e80aa0 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -326,6 +326,15 @@ public: */ ErrCode IsSmartReminderEnabled(const std::string &deviceType, bool &enabled); + /** + * @brief Get old distributedEnabled used to reset. + * + * @param values Indicates the data of query result. + * @param userId Indicates the user of old data in. + * @return Returns get old distributedEnabled result. + */ + ErrCode GetOldDistributedEnabled(std::unordered_map &values, const int32_t userId); + /** * @brief Set Enable smartphone to collaborate with other devices for intelligent reminders * @@ -377,6 +386,7 @@ public: int32_t GetBatchKvsFromDb( const std::string &key, std::unordered_map &values, const int32_t &userId); int32_t DeleteKvFromDb(const std::string &key, const int &userId); + int32_t DeleteBatchKvFromDb(const std::vector &keys, const int &userId); ErrCode GetDoNotDisturbProfile(int32_t profileId, int32_t userId, sptr &profile); bool CheckDoNotDisturbProfileID(int32_t profileId); void RemoveDoNotDisturbProfileTrustList(int32_t userId, const sptr &bundleOption); diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index 74d7e4d40..b204023c6 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -231,11 +231,12 @@ public: int32_t GetBatchKvsFromDb( const std::string &key, std::unordered_map &values, const int32_t &userId); int32_t DeleteKvFromDb(const std::string &key, const int32_t &userId); + int32_t DeleteBatchKvFromDb(const std::vector &keys, const int &userId); int32_t DropUserTable(const int32_t userId); bool UpdateBundlePropertyToDisturbeDB(int32_t userId, const NotificationPreferencesInfo::BundleInfo &bundleInfo); bool UpdateBundleSlotToDisturbeDB(int32_t userId, const std::string &bundleName, const int32_t &bundleUid, const std::vector> &slots); - + int32_t GetDistributedDataByOldKey(std::unordered_map &values, const int32_t userId); private: bool CheckRdbStore(); diff --git a/services/ans/include/smart_reminder_center.h b/services/ans/include/smart_reminder_center.h index 8fb58b260..d9b09cd29 100644 --- a/services/ans/include/smart_reminder_center.h +++ b/services/ans/include/smart_reminder_center.h @@ -78,7 +78,7 @@ private: const sptr &request, vector> &reminderAffecteds) const; void GetDeviceStatusByType(const string &deviceType, bitset &bitStatus) const; - bool IsNeedSynergy(const string &deviceType, const string &ownerBundleName) const; + bool IsNeedSynergy(const string &deviceType, const string &ownerBundleName, int32_t ownerUid) const; map> currentReminderMethods_; map>>> reminderMethods_; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 4428945ce..75553d4c4 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -305,6 +305,7 @@ AdvancedNotificationService::AdvancedNotificationService() NotificationSubscriberManager::GetInstance()->RegisterOnSubscriberAddCallback(callback); RecoverLiveViewFromDb(); + ResetDistributedEnabled(); ISystemEvent iSystemEvent = { std::bind(&AdvancedNotificationService::OnBundleRemoved, this, std::placeholders::_1), diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index f25fd804e..7cdfd5552 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -39,6 +39,7 @@ #include "notification_timer_info.h" #include "time_service_client.h" #include "notification_extension_wrapper.h" +#include "string_utils.h" #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED #include "distributed_notification_manager.h" @@ -78,6 +79,11 @@ constexpr char RECENT_NOTIFICATION_OPTION[] = "recent"; constexpr char HIDUMPER_ERR_MSG[] = "error: unknown option.\nThe arguments are illegal and you can enter '-h' for help."; constexpr int32_t MAIN_USER_ID = 100; +constexpr int32_t FIRST_USERID = 0; +constexpr char KEY_TABLE_VERSION[] = "tableVersion"; +constexpr char SPLIT_FLAG[] = "-"; +constexpr int32_t KEYWORD_SIZE = 4; +constexpr int32_t MIN_VERSION = 1; const std::unordered_map HIDUMPER_CMD_MAP = { { "--help", HELP_NOTIFICATION_OPTION }, { "--active", ACTIVE_NOTIFICATION_OPTION }, @@ -1946,5 +1952,56 @@ bool AdvancedNotificationService::AllowUseReminder(const std::string& bundleName return true; #endif } + +void AdvancedNotificationService::ResetDistributedEnabled() +{ + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("notificationSvrQueue is nullptr"); + } + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([=](){ + std::string value; + NotificationPreferences::GetInstance()->GetKvFromDb(KEY_TABLE_VERSION, value, FIRST_USERID); + if (!value.empty()) { + return; + } + ANS_LOGI("start ResetDistributedEnabled"); + std::unordered_map oldValues; + if (NotificationPreferences::GetInstance()->GetOldDistributedEnabled(oldValues, FIRST_USERID) != ERR_OK + || oldValues.empty()) + { + NotificationPreferences::GetInstance()->SetKvToDb( + KEY_TABLE_VERSION, std::to_string(MIN_VERSION), FIRST_USERID); + return; + } + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + std::vector delKeys; + for (auto iter : oldValues) { + std::vector keywordVector; + StringUtils::Split(iter.first, SPLIT_FLAG, keywordVector); + delKeys.push_back(iter.first); + if (keywordVector.size() != KEYWORD_SIZE) { + continue; + } + std::string bundleName = keywordVector[1]; + int32_t activeUserId = atoi(keywordVector[2].c_str()); + std::string deviceType = keywordVector[3]; + bool enabled = atoi(iter.second.c_str()); + int32_t uid = bundleManager->GetDefaultUidByBundleName(bundleName, activeUserId); + if (uid <= 0) { + continue; + } + sptr bundleOption = new NotificationBundleOption(bundleName, uid); + ErrCode result = NotificationPreferences::GetInstance()->SetDistributedEnabledByBundle( + bundleOption, deviceType, enabled); + if (result != ERR_OK) { + ANS_LOGE("SetDistributeEnabled failed! key:%{public}s, uid:%{public}d", + iter.first.c_str(), uid); + } + } + NotificationPreferences::GetInstance()->DeleteBatchKvFromDb(delKeys, FIRST_USERID); + NotificationPreferences::GetInstance()->SetKvToDb( + KEY_TABLE_VERSION, std::to_string(MIN_VERSION), FIRST_USERID); + })); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index a58e7cc3c..1801b1600 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -1028,6 +1028,15 @@ ErrCode NotificationPreferences::SetDistributedEnabledByBundle(const sptr &values, + const int32_t userId) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + std::lock_guard lock(preferenceMutex_); + int32_t storeDBResult = preferncesDB_->GetDistributedDataByOldKey(values, userId); + return storeDBResult == NativeRdb::E_OK ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; +} + ErrCode NotificationPreferences::IsDistributedEnabledByBundle(const sptr &bundleOption, const std::string &deviceType, bool &enabled) { @@ -1209,6 +1218,14 @@ int32_t NotificationPreferences::DeleteKvFromDb(const std::string &key, const in return preferncesDB_->DeleteKvFromDb(key, userId); } +int32_t NotificationPreferences::DeleteBatchKvFromDb(const std::vector &keys, const int32_t &userId) +{ + if (preferncesDB_ == nullptr) { + return ERR_ANS_SERVICE_NOT_READY; + } + return preferncesDB_->DeleteBatchKvFromDb(keys, userId); +} + bool NotificationPreferences::IsAgentRelationship(const std::string &agentBundleName, const std::string &sourceBundleName) { diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 0ef032f8c..e1b948bd5 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -100,10 +100,15 @@ const static std::string KEY_BUNDLE_BADGE_TOTAL_NUM = "badgeTotalNum"; */ const static std::string KEY_BUNDLE_ENABLE_NOTIFICATION = "enabledNotification"; +/** + * Indicates that old disturbe key which bundle enable notification. + */ +const static std::string OLD_KEY_BUNDLE_DISTRIBUTED_ENABLE_NOTIFICATION = "enabledNotificationDistributed"; + /** * Indicates that disturbe key which bundle enable notification. */ -const static std::string KEY_BUNDLE_DISTRIBUTED_ENABLE_NOTIFICATION = "enabledNotificationDistributed"; +const static std::string KEY_ENABLE_BUNDLE_DISTRIBUTED_NOTIFICATION = "enabledDistributedNotification"; /** * Indicates that disturbe key which bundle enable notification. @@ -1653,7 +1658,7 @@ bool NotificationPreferencesDatabase::RemoveEnabledDbByBundleName(std::string bu } int32_t userId = -1; OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(bundleUid, userId); - std::string key = std::string(KEY_BUNDLE_DISTRIBUTED_ENABLE_NOTIFICATION).append( + std::string key = std::string(KEY_ENABLE_BUNDLE_DISTRIBUTED_NOTIFICATION).append( KEY_MIDDLE_LINE).append(std::string(bundleName).append(KEY_MIDDLE_LINE)); ANS_LOGD("key is %{public}s", key.c_str()); int32_t result = NativeRdb::E_OK; @@ -1796,6 +1801,23 @@ int32_t NotificationPreferencesDatabase::DeleteKvFromDb(const std::string &key, return NativeRdb::E_OK; } +int32_t NotificationPreferencesDatabase::DeleteBatchKvFromDb(const std::vector &keys, + const int32_t &userId) +{ + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return NativeRdb::E_ERROR; + } + + int32_t result = rdbDataManager_->DeleteBathchData(keys, userId); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Delete key-value failed, result %{public}d.", result); + return NativeRdb::E_ERROR; + } + + return NativeRdb::E_OK; +} + int32_t NotificationPreferencesDatabase::DropUserTable(const int32_t userId) { if (!CheckRdbStore()) { @@ -1811,6 +1833,23 @@ int32_t NotificationPreferencesDatabase::DropUserTable(const int32_t userId) return NativeRdb::E_OK; } +int32_t NotificationPreferencesDatabase::GetDistributedDataByOldKey( + std::unordered_map &values, const int32_t userId) +{ + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return NativeRdb::E_ERROR; + } + + int32_t result = rdbDataManager_->QueryDataBeginWithKey(OLD_KEY_BUNDLE_DISTRIBUTED_ENABLE_NOTIFICATION, + values, userId); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Get distributedDataByOldKey failed, result %{public}d.", result); + return NativeRdb::E_ERROR; + } + return NativeRdb::E_OK; +} + bool NotificationPreferencesDatabase::IsAgentRelationship(const std::string &agentBundleName, const std::string &sourceBundleName) { @@ -1869,7 +1908,7 @@ bool NotificationPreferencesDatabase::PutDistributedEnabledForBundle(const std:: std::string NotificationPreferencesDatabase::GenerateBundleLablel( const NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &deviceType) const { - return std::string(KEY_BUNDLE_DISTRIBUTED_ENABLE_NOTIFICATION).append(KEY_MIDDLE_LINE).append( + return std::string(KEY_ENABLE_BUNDLE_DISTRIBUTED_NOTIFICATION).append(KEY_MIDDLE_LINE).append( std::string(bundleInfo.GetBundleName()).append(KEY_MIDDLE_LINE).append(std::to_string( bundleInfo.GetBundleUid())).append(KEY_MIDDLE_LINE).append(deviceType)); } diff --git a/services/ans/src/notification_smart_reminder/smart_reminder_center.cpp b/services/ans/src/notification_smart_reminder/smart_reminder_center.cpp index e1d136555..dab4bbcda 100644 --- a/services/ans/src/notification_smart_reminder/smart_reminder_center.cpp +++ b/services/ans/src/notification_smart_reminder/smart_reminder_center.cpp @@ -270,7 +270,7 @@ void SmartReminderCenter::HandleReminderMethods( GetDeviceStatusByType(deviceType, bitStatus); bool enabledAffectedBy = true; if (deviceType.compare(NotificationConstant::CURRENT_DEVICE_TYPE) != 0) { - if (IsNeedSynergy(deviceType, request->GetOwnerBundleName())) { + if (IsNeedSynergy(deviceType, request->GetOwnerBundleName(), request->GetOwnerUid())) { validDevices.insert(deviceType); } else { enabledAffectedBy = false; @@ -295,20 +295,14 @@ void SmartReminderCenter::HandleReminderMethods( } } -bool SmartReminderCenter::IsNeedSynergy(const string &deviceType, const string &ownerBundleName) const +bool SmartReminderCenter::IsNeedSynergy(const string &deviceType, const string &ownerBundleName, int32_t ownerUid) const { bool isEnable = true; if (NotificationPreferences::GetInstance()->IsSmartReminderEnabled(deviceType, isEnable) != ERR_OK || !isEnable) { return false; } - int32_t userId = -1; - int32_t result = - OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(IPCSkeleton::GetCallingUid(), userId); - if (result != ERR_OK) { - ANS_LOGE("HandleReminderMethods GetOsAccountLocalIdFromUid fail, code = %{public}d.", result); - return false; - } - sptr bundleOption = new (std::nothrow) NotificationBundleOption(ownerBundleName, userId); + + sptr bundleOption = new (std::nothrow) NotificationBundleOption(ownerBundleName, ownerUid); if (NotificationPreferences::GetInstance()->IsDistributedEnabledByBundle( bundleOption, deviceType, isEnable) != ERR_OK || !isEnable) { return false; -- Gitee From d28b7fbc64fb60493e90524015ff64be5f5106e0 Mon Sep 17 00:00:00 2001 From: luojingsong Date: Mon, 28 Oct 2024 16:58:47 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=99=BA=E8=83=BD=E6=8F=90=E9=86=92?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: luojingsong Change-Id: Ia7bea3f8876bb6e47f2c81528dc797c0273fded7 --- frameworks/ans/src/notification_subscriber.cpp | 5 +++-- services/ans/src/advanced_notification_utils.cpp | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/frameworks/ans/src/notification_subscriber.cpp b/frameworks/ans/src/notification_subscriber.cpp index 51852b790..346342245 100644 --- a/frameworks/ans/src/notification_subscriber.cpp +++ b/frameworks/ans/src/notification_subscriber.cpp @@ -54,10 +54,11 @@ bool NotificationSubscriber::ProcessSyncDecision( if (flagsMap == nullptr || flagsMap->size() <= 0) { return true; } + ANS_LOGI("SetFlags filte, deviceType = %{public}s", deviceType.c_str()); auto flagIter = flagsMap->find(deviceType); if (flagIter != flagsMap->end() && flagIter->second != nullptr) { - ANS_LOGI("SetFlags-before filte, notificationKey = %{public}s flagIter flags = %{public}d deviceType = %{public}s", - request->GetKey().c_str(), flagIter->second->GetReminderFlags(), deviceType.c_str()); + ANS_LOGI("SetFlags-before filte, notificationKey = %{public}s flagIter flags = %{public}d", + request->GetKey().c_str(), flagIter->second->GetReminderFlags()); std::shared_ptr tempFlags = request->GetFlags(); tempFlags->SetSoundEnabled(DowngradeReminder(tempFlags->IsSoundEnabled(), flagIter->second->IsSoundEnabled())); tempFlags->SetVibrationEnabled( diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index 7cdfd5552..197ae0632 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -1958,7 +1958,7 @@ void AdvancedNotificationService::ResetDistributedEnabled() if (notificationSvrQueue_ == nullptr) { ANS_LOGE("notificationSvrQueue is nullptr"); } - ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([=](){ + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([=]() { std::string value; NotificationPreferences::GetInstance()->GetKvFromDb(KEY_TABLE_VERSION, value, FIRST_USERID); if (!value.empty()) { @@ -1990,7 +1990,8 @@ void AdvancedNotificationService::ResetDistributedEnabled() if (uid <= 0) { continue; } - sptr bundleOption = new NotificationBundleOption(bundleName, uid); + sptr bundleOption = + new NotificationBundleOption(bundleName, uid); ErrCode result = NotificationPreferences::GetInstance()->SetDistributedEnabledByBundle( bundleOption, deviceType, enabled); if (result != ERR_OK) { -- Gitee