diff --git a/frameworks/ans/native/src/notification_request.cpp b/frameworks/ans/native/src/notification_request.cpp index e08144147c3d114ddf43801598098df204be4bbc..52c4d5d6e72b05a056ebabd4af5b101c2d3072fb 100644 --- a/frameworks/ans/native/src/notification_request.cpp +++ b/frameworks/ans/native/src/notification_request.cpp @@ -1542,6 +1542,9 @@ bool NotificationRequest::ConvertObjectsToJson(nlohmann::json &jsonObject) const } jsonObject["distributedOptions"] = optObj; + if (!notificationFlags_) { + return false; + } nlohmann::json flagsObj; if (!NotificationJsonConverter::ConvertToJosn(notificationFlags_.get(), flagsObj)) { ANS_LOGE("Cannot convert notificationFlags to JSON"); diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 8a50da7b58f1502f6e4c5c898604e4146b5bd1f4..5920b76f980f063cf369e63fc6c04b3097936006 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 InitSettingFromDisturbDB(); 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 ec56db5b775408e4b0ccf63be0b6e74fe203a41f..d3712755a6ce8e0de90983f911c4e55ca2101d93 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -2917,14 +2917,12 @@ 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); + 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 a9c5e0d4f953e66af576d0dc4b86cf775bf4c9e0..38729309f170d1e7aeb28418451f388fe6a1b515 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_); + InitSettingFromDisturbDB(); } 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,13 @@ ErrCode NotificationPreferences::GetTemplateSupported(const std::string& templat inFile.close(); return ERR_OK; } + +void NotificationPreferences::InitSettingFromDisturbDB() +{ + if (!preferncesDB_) { + 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 8dbcadc514f92e3d034d5fc1c0a5992689f91d21..d46737e1d294f27408c10daaaf6450186f407cdc 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 c910e429943bf0babdc76a03d03c30f7d2b6eb81..64df9b4db8f602d32f3f16b947407f2f84b5cc24 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().InitSettingFromDisturbDB(); } } } // namespace Notification