From 29b28befeb660a653d7573217ddea53dbcd37ce9 Mon Sep 17 00:00:00 2001 From: fangJinliang1 Date: Wed, 16 Feb 2022 16:19:19 +0800 Subject: [PATCH] fixed ff40b2c from https://gitee.com/fangJinliang1/notification_ans_standard/pulls/243 get userId interface modify Signed-off-by: fangJinliang1 Change-Id: I56c4d58a8623f291bc9f57b3fe4ffba51c37aec2 Signed-off-by: fangJinliang1 --- .../ans/native/src/notification_request.cpp | 12 ++++--- .../ans/include/notification_preferences.h | 2 +- .../ans/src/advanced_notification_service.cpp | 15 ++++---- services/ans/src/notification_preferences.cpp | 23 ++++-------- .../src/notification_preferences_database.cpp | 36 +++++++++---------- services/ans/src/system_event_observer.cpp | 5 +++ 6 files changed, 43 insertions(+), 50 deletions(-) diff --git a/frameworks/ans/native/src/notification_request.cpp b/frameworks/ans/native/src/notification_request.cpp index e08144147..9501b3c50 100644 --- a/frameworks/ans/native/src/notification_request.cpp +++ b/frameworks/ans/native/src/notification_request.cpp @@ -1542,12 +1542,14 @@ bool NotificationRequest::ConvertObjectsToJson(nlohmann::json &jsonObject) const } jsonObject["distributedOptions"] = optObj; - nlohmann::json flagsObj; - if (!NotificationJsonConverter::ConvertToJosn(notificationFlags_.get(), flagsObj)) { - ANS_LOGE("Cannot convert notificationFlags to JSON"); - return false; + if (notificationFlags_) { + nlohmann::json flagsObj; + if (!NotificationJsonConverter::ConvertToJosn(notificationFlags_.get(), flagsObj)) { + ANS_LOGE("Cannot convert notificationFlags to JSON"); + return false; + } + jsonObject["notificationFlags"] = flagsObj; } - jsonObject["notificationFlags"] = flagsObj; return true; } diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 8a50da7b5..e52587b6b 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -75,6 +75,7 @@ public: ErrCode ClearNotificationInRestoreFactorySettings(); void OnDistributedKvStoreDeathRecipient(); + void InitSettingFromDisturbeDB(); private: ErrCode CheckSlotForCreateSlot(const sptr &bundleOption, @@ -99,7 +100,6 @@ private: ErrCode GetBundleProperty( const sptr &bundleOption, const BundleType &type, T &value) const; std::string GenerateBundleKey(const sptr &bundleOption) const; - bool GetActiveUserId(int& userId); private: NotificationPreferencesInfo preferencesInfo_ {}; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 9f2405361..f0add999e 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -2930,14 +2930,13 @@ ErrCode AdvancedNotificationService::IsSupportTemplate(const std::string& templa bool AdvancedNotificationService::GetActiveUserId(int& userId) { - std::vector osAccountInfos; - OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); - - for (auto iter : osAccountInfos) { - if (iter.GetIsActived()) { - userId = iter.GetLocalId(); - return true; - } + std::vector activeUserId; + OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); + ANS_LOGD("Get active userID size=%{public}u", activeUserId.size()); + if (activeUserId.size() > 0) { + userId = activeUserId[0]; + ANS_LOGD("Return active userId=%{public}d", userId); + return true; } return false; } diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index a9c5e0d4f..7a74eafde 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -27,8 +27,7 @@ namespace OHOS { namespace Notification { NotificationPreferences::NotificationPreferences() { - preferncesDB_ = std::make_unique(); - preferncesDB_->ParseFromDisturbeDB(preferencesInfo_); + InitSettingFromDisturbeDB(); } NotificationPreferences::~NotificationPreferences() @@ -494,20 +493,6 @@ ErrCode NotificationPreferences::SetNotificationsEnabledForBundle( return result; } -bool NotificationPreferences::GetActiveUserId(int& userId) -{ - std::vector osAccountInfos; - OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); - - for (auto iter : osAccountInfos) { - if (iter.GetIsActived()) { - userId = iter.GetLocalId(); - return true; - } - } - return false; -} - ErrCode NotificationPreferences::GetNotificationsEnabled(const int32_t &userId, bool &enabled) { if (userId <= SUBSCRIBE_USER_INIT) { @@ -882,5 +867,11 @@ ErrCode NotificationPreferences::GetTemplateSupported(const std::string& templat inFile.close(); return ERR_OK; } + +void NotificationPreferences::InitSettingFromDisturbeDB() +{ + preferncesDB_ = std::make_unique(); + preferncesDB_->ParseFromDisturbeDB(preferencesInfo_); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 8dbcadc51..d46737e1d 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -1147,45 +1147,41 @@ std::string NotificationPreferencesDatabase::SubUniqueIdentifyFromString( void NotificationPreferencesDatabase::ParseDoNotDisturbType(NotificationPreferencesInfo &info) { - std::vector osAccountInfos; - OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); + std::vector activeUserId; + OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); - for (auto iter : osAccountInfos) { - int userId = iter.GetLocalId(); - NotificationPreferencesDatabase::GetDoNotDisturbType(info, userId); + for (auto iter : activeUserId) { + NotificationPreferencesDatabase::GetDoNotDisturbType(info, iter); } } void NotificationPreferencesDatabase::ParseDoNotDisturbBeginDate(NotificationPreferencesInfo &info) { - std::vector osAccountInfos; - OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); + std::vector activeUserId; + OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); - for (auto iter : osAccountInfos) { - int userId = iter.GetLocalId(); - NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(info, userId); + for (auto iter : activeUserId) { + NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(info, iter); } } void NotificationPreferencesDatabase::ParseDoNotDisturbEndDate(NotificationPreferencesInfo &info) { - std::vector osAccountInfos; - OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); + std::vector activeUserId; + OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); - for (auto iter : osAccountInfos) { - int userId = iter.GetLocalId(); - NotificationPreferencesDatabase::GetDoNotDisturbEndDate(info, userId); + for (auto iter : activeUserId) { + NotificationPreferencesDatabase::GetDoNotDisturbEndDate(info, iter); } } void NotificationPreferencesDatabase::ParseEnableAllNotification(NotificationPreferencesInfo &info) { - std::vector osAccountInfos; - OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); + std::vector activeUserId; + OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(activeUserId); - for (auto iter : osAccountInfos) { - int userId = iter.GetLocalId(); - NotificationPreferencesDatabase::GetEnableAllNotification(info, userId); + for (auto iter : activeUserId) { + NotificationPreferencesDatabase::GetEnableAllNotification(info, iter); } } diff --git a/services/ans/src/system_event_observer.cpp b/services/ans/src/system_event_observer.cpp index c910e4299..3a81be883 100644 --- a/services/ans/src/system_event_observer.cpp +++ b/services/ans/src/system_event_observer.cpp @@ -18,6 +18,7 @@ #include "bundle_constants.h" #include "common_event_manager.h" #include "common_event_support.h" +#include "notification_preferences.h" namespace OHOS { namespace Notification { @@ -29,6 +30,7 @@ SystemEventObserver::SystemEventObserver(const ISystemEvent &callbacks) : callba matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON); matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF); #endif + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills); subscriber_ = std::make_shared( @@ -46,6 +48,7 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) { auto want = data.GetWant(); std::string action = want.GetAction(); + ANS_LOGD("OnReceiveEvent action is %{public}s.", action.c_str()); if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) { if (callbacks_.onBundleRemoved != nullptr) { auto element = want.GetElement(); @@ -64,6 +67,8 @@ void SystemEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &data) callbacks_.onScreenOff(); } #endif + } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) { + NotificationPreferences::GetInstance().InitSettingFromDisturbeDB(); } } } // namespace Notification -- Gitee