diff --git a/frameworks/ans/core/common/include/ans_const_define.h b/frameworks/ans/core/common/include/ans_const_define.h index e066999762eb827cbf31a7a195aa743c0ce55eb2..c498d2bcbb5992effdf8c4baf752db9731f724de 100644 --- a/frameworks/ans/core/common/include/ans_const_define.h +++ b/frameworks/ans/core/common/include/ans_const_define.h @@ -32,6 +32,8 @@ constexpr uint32_t MAX_ICON_SIZE = 50 * 1024; constexpr uint32_t MAX_PICTURE_SIZE = 2 * 1024 * 1024; constexpr bool SUPPORT_DO_NOT_DISTRUB = true; constexpr uint32_t SYSTEM_SERVICE_UID = 1000; +constexpr int32_t SUBSCRIBE_USER_INIT = -1; +constexpr int32_t SUBSCRIBE_USER_ALL = -2; // Default sound for notification const static Uri DEFAULT_NOTIFICATION_SOUND("file://system/etc/Light.ogg"); diff --git a/frameworks/ans/native/src/notification.cpp b/frameworks/ans/native/src/notification.cpp index 40636e8c99a0a254b54589ece67de1ba844c8c70..e0ab103e8cb22e83c1ebba90fd8dec3d6aa2095a 100644 --- a/frameworks/ans/native/src/notification.cpp +++ b/frameworks/ans/native/src/notification.cpp @@ -182,6 +182,14 @@ NotificationConstant::SourceType Notification::GetSourceType() const return sourceType_; } +int32_t Notification::GetUserId() const +{ + if (request_ == nullptr) { + return 0; + } + return request_->GetCreatorUserId(); +} + bool Notification::MarshallingBool(Parcel &parcel) const { if (!parcel.WriteBool(enableLight_)) { diff --git a/frameworks/ans/native/src/notification_request.cpp b/frameworks/ans/native/src/notification_request.cpp index 0bce17ed3faa54971031d03d8d093bd6168fcac6..10343c0864f591bbc276bd5876ca9e7641074b98 100644 --- a/frameworks/ans/native/src/notification_request.cpp +++ b/frameworks/ans/native/src/notification_request.cpp @@ -719,6 +719,16 @@ std::string NotificationRequest::GetLabel() const return label_; } +void NotificationRequest::SetCreatorUserId(int32_t userId) +{ + creatorUserId_ = userId; +} + +int32_t NotificationRequest::GetCreatorUserId() const +{ + return creatorUserId_; +} + std::string NotificationRequest::Dump() { return "NotificationRequest{ " diff --git a/frameworks/ans/native/src/notification_subscribe_info.cpp b/frameworks/ans/native/src/notification_subscribe_info.cpp index 0f70d047cd848f5c0bfd7179f76406e8868afc75..50cd3a934de9c142bb65bd57214775326a1263d9 100644 --- a/frameworks/ans/native/src/notification_subscribe_info.cpp +++ b/frameworks/ans/native/src/notification_subscribe_info.cpp @@ -45,6 +45,16 @@ std::vector NotificationSubscribeInfo::GetAppNames() const return appNames_; } +void NotificationSubscribeInfo::AddAppUserId(const int userId) +{ + userId_ = userId; +} + +int32_t NotificationSubscribeInfo::GetAppUserId() const +{ + return userId_; +} + bool NotificationSubscribeInfo::Marshalling(Parcel &parcel) const { // write appNames_ diff --git a/interfaces/innerkits/ans/native/include/notification.h b/interfaces/innerkits/ans/native/include/notification.h index e2e71ea27e6473072f6cd6376eaba29e22d76659..1d40d79d768e427e7d7fd3bdccc1d6b7cc89d8c8 100644 --- a/interfaces/innerkits/ans/native/include/notification.h +++ b/interfaces/innerkits/ans/native/include/notification.h @@ -189,6 +189,13 @@ public: */ NotificationConstant::SourceType GetSourceType() const; + /** + * @brief Obtains the UserId of the notification creator. + * + * @return Return the UserId of the notification creator. + */ + int32_t GetUserId() const; + /** * @brief Dumps a string representation of the object. * diff --git a/interfaces/innerkits/ans/native/include/notification_request.h b/interfaces/innerkits/ans/native/include/notification_request.h index 74b26955014ebfd9e511b0f43daed2a682eed584..f5efaff822a3a626cde897cb90617fab38a5b1aa 100644 --- a/interfaces/innerkits/ans/native/include/notification_request.h +++ b/interfaces/innerkits/ans/native/include/notification_request.h @@ -843,6 +843,18 @@ public: */ std::string GetLabel() const; + /** + * Sets the UserId of the notification creator. + * @param userId the UserId of the notification creator. + */ + void SetCreatorUserId(int32_t userId); + + /** + * Obtains the UserId of the notification creator. + * @return the UserId of the notification creator. + */ + int32_t GetCreatorUserId() const; + /** * Returns a string representation of the object. * @return a string representation of the object. @@ -914,6 +926,7 @@ private: pid_t creatorPid_ {0}; pid_t creatorUid_ {0}; + int32_t creatorUserId_ {-1}; std::string settingsText_ {}; std::string creatorBundleName_ {}; diff --git a/interfaces/innerkits/ans/native/include/notification_subscribe_info.h b/interfaces/innerkits/ans/native/include/notification_subscribe_info.h index 7043ae687ff95aa0a1f4d1dc3b9027d039115c87..7da904f412bc51169575290fe6375e245be6b0d2 100644 --- a/interfaces/innerkits/ans/native/include/notification_subscribe_info.h +++ b/interfaces/innerkits/ans/native/include/notification_subscribe_info.h @@ -64,6 +64,20 @@ public: **/ std::vector GetAppNames() const; + /** + * @brief application current userid. + * + * @param appNames Indicates the userid of application. + **/ + void AddAppUserId(const int32_t userId); + + /** + * @brief Obtains the userid of application. + * + * @return Returns the userid of application. + **/ + int32_t GetAppUserId() const; + /** * @brief Marshals a NotificationSubscribeInfo object into a Parcel. * @@ -90,6 +104,7 @@ private: private: std::vector appNames_ {}; + int32_t userId_ {-1}; }; } // namespace Notification } // namespace OHOS diff --git a/interfaces/kits/napi/ans/src/subscribe.cpp b/interfaces/kits/napi/ans/src/subscribe.cpp index e0083a426920c4bbec1cab740f5e22671345b378..a0a5b2cd04364da4ef552e4ec7af97b3fcd1badf 100644 --- a/interfaces/kits/napi/ans/src/subscribe.cpp +++ b/interfaces/kits/napi/ans/src/subscribe.cpp @@ -1050,6 +1050,7 @@ napi_value Subscribe(napi_env env, napi_callback_info info) ANS_LOGI("Subscribe with NotificationSubscribeInfo"); OHOS::Notification::NotificationSubscribeInfo subscribeInfo; subscribeInfo.AddAppNames(asynccallbackinfo->subscriberInfo.bundleNames); + subscribeInfo.AddAppUserId(asynccallbackinfo->subscriberInfo.userId); asynccallbackinfo->info.errorCode = NotificationHelper::SubscribeNotification(*(asynccallbackinfo->objectInfo), subscribeInfo); } else { diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index 72614388f26fa28b0dd72142ccb1b8d5cd47b483..3af77500ade5304fc254fd96455fc9cd031a67ab 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -25,6 +25,10 @@ config("public_ans_config") { "//foundation/appexecfwk/standard/interfaces/innerkits/libeventhandler/include", "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", + "//base/account/os_account/interfaces/innerkits/osaccount/native/include", + "//base/account/os_account/frameworks/common/database/include/", + "//base/account/os_account/frameworks/common/account_error/include", + "//base/account/os_account/frameworks/common/log/include", ] defines = [ "APP_LOG_TAG = \"Ans\"", @@ -60,6 +64,7 @@ ohos_shared_library("libans") { "${core_path}:ans_core", "${frameworks_path}/ans/native:ans_innerkits", "${frameworks_path}/wantagent:wantagent_innerkits", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", "//base/miscservices/time/services:time_service", "//foundation/aafwk/standard/services/abilitymgr:abilityms", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index b53211b491fe8cca63ddc1ff6d5301fb40dece19..6d0397203218fc1913a1337ade42ab81f3469b32 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -159,6 +159,7 @@ private: void AdjustDateForDndTypeOnce(int64_t &beginDate, int64_t &endDate); bool CheckPermission(const std::string &bundleName); ErrCode PrepereContinuousTaskNotificationRequest(const sptr &request, const int &uid); + bool GetActiveUserId(int& userId); private: static sptr instance_; diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index ef0af0a2f036b516c76777a2b0c4cb2c568fb7eb..04ef9233cd0113c01f06ab8a044454624be52cc2 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -64,10 +64,12 @@ public: ErrCode SetPrivateNotificationsAllowed(const sptr &bundleOption, const bool allow); ErrCode GetNotificationsEnabledForBundle(const sptr &bundleOption, bool &enabled); ErrCode SetNotificationsEnabledForBundle(const sptr &bundleOption, const bool enabled); - ErrCode GetNotificationsEnabled(bool &enabled); - ErrCode SetNotificationsEnabled(const bool &enabled); - ErrCode GetDoNotDisturbDate(sptr &date); - ErrCode SetDoNotDisturbDate(const sptr date); + ErrCode GetNotificationsEnabled(const sptr &bundleOption, bool &enabled); + ErrCode SetNotificationsEnabled(const sptr &bundleOption, const bool &enabled); + ErrCode GetDoNotDisturbDate(const sptr &bundleOption, + sptr &date); + ErrCode SetDoNotDisturbDate(const sptr &bundleOption, + const sptr date); ErrCode GetTemplateSupported(const std::string& templateName, bool &support); ErrCode ClearNotificationInRestoreFactorySettings(); @@ -87,16 +89,17 @@ private: const sptr &slot, NotificationPreferencesInfo &preferencesInfo) const; ErrCode CheckGroupForUpdateSlotGroup(const sptr &bundleOption, const sptr &group, NotificationPreferencesInfo &preferencesInfo) const; - template + template ErrCode SetBundleProperty(NotificationPreferencesInfo &preferencesInfo, const sptr &bundleOption, const BundleType &type, const T &value); - template + template ErrCode SaveBundleProperty(NotificationPreferencesInfo::BundleInfo &bundleInfo, const sptr &bundleOption, const BundleType &type, const T &value); - template + template 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/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index 0787c62f255d9a29452e43bcd8bb17351814a292..6b531e4515ecd4242f30a8888b049db1954909ff 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -28,17 +28,20 @@ class NotificationPreferencesDatabase final { public: NotificationPreferencesDatabase(); ~NotificationPreferencesDatabase(); - bool PutSlotsToDisturbeDB(const std::string &bundleKey, const std::vector> &slots); - bool PutGroupsToDisturbeDB(const std::string &bundleKey, const std::vector> &groups); + bool PutSlotsToDisturbeDB( + const std::string &bundleName, const int &bundleUid, const std::vector> &slots); + bool PutGroupsToDisturbeDB( + const std::string &bundleName, const int &bundleUid, const std::vector> &groups); bool PutBundlePropertyToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo); - bool PutShowBadge(const std::string &bundleKey, const bool &enable); - bool PutImportance(const std::string &bundleKey, const int &importance); - bool PutTotalBadgeNums(const std::string &bundleKey, const int &totalBadgeNum); - bool PutPrivateNotificationsAllowed(const std::string &bundleKey, const bool &allow); - bool PutNotificationsEnabledForBundle(const std::string &bundleKey, const bool &enabled); - bool PutNotificationsEnabled(const bool &enabled); - bool PutDoNotDisturbDate(const sptr &date); + bool PutShowBadge(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable); + bool PutImportance(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int &importance); + bool PutTotalBadgeNums(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int &totalBadgeNum); + bool PutPrivateNotificationsAllowed(const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &allow); + bool PutNotificationsEnabledForBundle( + const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled); + bool PutNotificationsEnabled(const int32_t &userId, const bool &enabled); + bool PutDoNotDisturbDate(const int32_t &userId, const sptr &date); bool ParseFromDisturbeDB(NotificationPreferencesInfo &info); @@ -56,9 +59,9 @@ private: void CloseKvStore(); bool CheckKvStore(); - bool CheckBundle(const std::string &bundleKey); + bool CheckBundle(const std::string &bundleName, const int &bundleUid); bool PutBundlePropertyValueToDisturbeDB(const NotificationPreferencesInfo::BundleInfo &bundleInfo); - template + template OHOS::DistributedKv::Status PutBundlePropertyToDisturbeDB( const std::string &bundleKey, const BundleType &type, const T &t); bool PutBundleToDisturbeDB( @@ -70,9 +73,9 @@ private: bool GetRemoveGroupKeysFromDisturbeDB( const std::string &bundleKey, const std::string &groupId, std::vector &keys); - bool SlotToEntry(const std::string &bundleKey, const sptr &slot, + bool SlotToEntry(const std::string &bundleName, const int &bundleUid, const sptr &slot, std::vector &entries); - bool GroupToEntry(const std::string &bundleKey, const sptr &group, + bool GroupToEntry(const std::string &bundleName, const int &bundleUid, const sptr &group, std::vector &entries); void GenerateGroupEntry(const std::string &bundleKey, const sptr &group, std::vector &entries) const; @@ -112,9 +115,11 @@ private: void ParseBundleImportance(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleShowBadge(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleBadgeNum(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; - void ParseBundlePrivateAllowed(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; + void ParseBundlePrivateAllowed( + NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; void ParseBundleEnableNotification( 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 OHOS::DistributedKv::Entry &entry); void ParseSlotGroupId(sptr &slot, const std::string &value) const; @@ -129,6 +134,12 @@ private: void ParseSlotVibrationSytle(sptr &slot, const std::string &value) const; void ParseSlotEnableBypassDnd(sptr &slot, const std::string &value) const; + std::string GenerateBundleLablel(const NotificationPreferencesInfo::BundleInfo &bundleInfo) const; + void GetDoNotDisturbType(NotificationPreferencesInfo &info, int userId); + void GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int userId); + void GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int userId); + void GetEnableAllNotification(NotificationPreferencesInfo &info, int userId); + static const std::map &, std::string &)>> slotMap_; @@ -143,4 +154,4 @@ private: }; } // namespace Notification } // namespace OHOS -#endif // BASE_NOTIFICATION_ANS_STANDARD_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H \ No newline at end of file +#endif // BASE_NOTIFICATION_ANS_STANDARD_SERVICES_NOTIFICATION_PREFERENCES_DATABASE_H diff --git a/services/ans/include/notification_preferences_info.h b/services/ans/include/notification_preferences_info.h index ac53056ce5bee27ef07fd7c53072f6acd9f3cbd5..943252dc58036f4fa90477da57ec426f749f3d59 100644 --- a/services/ans/include/notification_preferences_info.h +++ b/services/ans/include/notification_preferences_info.h @@ -80,19 +80,21 @@ public: {} ~NotificationPreferencesInfo() {} - void SetEnabledAllNotification(const bool &value); - bool GetEnabledAllNotification() const; - void SetDoNotDisturbDate(const sptr &date); - sptr GetDoNotDisturbDate() const; void SetBundleInfo(const BundleInfo &info); bool GetBundleInfo(const sptr &bundleOption, BundleInfo &info) const; bool RemoveBundleInfo(const sptr &bundleOption); bool IsExsitBundleInfo(const sptr &bundleOption) const; void ClearBundleInfo(); + void SetDoNotDisturbDate(const int32_t &userId, + const sptr &doNotDisturbDate); + bool GetDoNotDisturbDate(const int32_t &userId, + sptr &doNotDisturbDate) const; + void SetEnabledAllNotification(const int32_t &userId, const bool &enable); + bool GetEnabledAllNotification(const int32_t &userId, bool &enable) const; private: - bool isEnabledAllNotification_ = true; - sptr doNotDisturbDate_ = new NotificationDoNotDisturbDate(); + std::map isEnabledAllNotification_; + std::map> doNotDisturbDate_; std::map infos_; }; } // namespace Notification diff --git a/services/ans/include/notification_subscriber_manager.h b/services/ans/include/notification_subscriber_manager.h index 477acb7476a591ed14c781cfa3bcbf00b240f827..f6e2ae64531e82389a465593f32ad15f8b599679 100644 --- a/services/ans/include/notification_subscriber_manager.h +++ b/services/ans/include/notification_subscriber_manager.h @@ -28,6 +28,7 @@ #include "singleton.h" #include "ans_subscriber_interface.h" +#include "notification_bundle_option.h" #include "notification_constant.h" #include "notification_request.h" #include "notification_sorting_map.h" @@ -54,7 +55,8 @@ private: std::shared_ptr FindSubscriberRecord(const wptr &object); std::shared_ptr FindSubscriberRecord(const sptr &subscriber); std::shared_ptr CreateSubscriberRecord(const sptr &subscriber); - void AddRecordInfo(std::shared_ptr &record, const sptr &subscribeInfo); + void AddRecordInfo( + std::shared_ptr &record, const sptr &subscribeInfo); void RemoveRecordInfo( std::shared_ptr &record, const sptr &subscribeInfo); ErrCode AddSubscriberInner( diff --git a/services/ans/include/preferences_constant.h b/services/ans/include/preferences_constant.h index 64419b6535b857ca8ea830be59cd2ebe655b7079..b5430c697dbc41372481a6455bac9aa86055aec5 100644 --- a/services/ans/include/preferences_constant.h +++ b/services/ans/include/preferences_constant.h @@ -32,6 +32,7 @@ const static std::string KEY_BUNDLE_SHOW_BADGE = "showBadge"; const static std::string KEY_BUNDLE_BADGE_TOTAL_NUM = "badgeTotalNum"; const static std::string KEY_BUNDLE_PRIVATE_ALLOWED = "privateAllowed"; const static std::string KEY_BUNDLE_ENABLE_NOTIFICATION = "enabledNotification"; +const static std::string KEY_BUNDLE_UID = "uid"; const static std::string KEY_GROUP = "group"; const static std::string KEY_GROUP_ID = "id"; diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index b29831f2c75a8d5c900aa25ddb859419f83b695e..2110f89f60f485a506c4aeab2ce5893e69bf1cbc 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -30,6 +30,7 @@ #include "notification_slot.h" #include "notification_slot_filter.h" #include "notification_subscriber_manager.h" +#include "os_account_manager.h" #include "permission_filter.h" #include "reminder_data_manager.h" @@ -157,6 +158,10 @@ inline ErrCode PrepereNotificationRequest(const sptr &reque int pid = IPCSkeleton::GetCallingPid(); request->SetCreatorUid(uid); request->SetCreatorPid(pid); + + int userId = SUBSCRIBE_USER_INIT; + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId); + request->SetCreatorUserId(userId); ErrCode result = CheckPictureSize(request); @@ -1136,11 +1141,16 @@ ErrCode AdvancedNotificationService::GetAllActiveNotifications(std::vectorPostSyncTask(std::bind([&]() { notifications.clear(); for (auto record : notificationList_) { - notifications.push_back(record->notification); + if (record->notification != nullptr && record->notification->GetUserId() == userId) { + notifications.push_back(record->notification); + } } })); return result; @@ -1191,10 +1201,15 @@ ErrCode AdvancedNotificationService::SetNotificationsEnabledForAllBundles(const } ErrCode result = ERR_OK; + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + handler_->PostSyncTask(std::bind([&]() { if (deviceId.empty()) { // Local device - result = NotificationPreferences::GetInstance().SetNotificationsEnabled(enabled); + result = NotificationPreferences::GetInstance().SetNotificationsEnabled(bundleOption, enabled); } else { // Remote device } @@ -1241,9 +1256,14 @@ ErrCode AdvancedNotificationService::IsAllowedNotify(bool &allowed) } ErrCode result = ERR_OK; + sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + handler_->PostSyncTask(std::bind([&]() { allowed = false; - result = NotificationPreferences::GetInstance().GetNotificationsEnabled(allowed); + result = NotificationPreferences::GetInstance().GetNotificationsEnabled(bundleOption, allowed); })); return result; } @@ -1280,7 +1300,7 @@ ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify( ErrCode result = ERR_OK; handler_->PostSyncTask(std::bind([&]() { allowed = false; - result = NotificationPreferences::GetInstance().GetNotificationsEnabled(allowed); + result = NotificationPreferences::GetInstance().GetNotificationsEnabled(bundleOption, allowed); if (result == ERR_OK && allowed) { result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(targetBundle, allowed); if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) { @@ -2019,8 +2039,13 @@ ErrCode AdvancedNotificationService::SetDoNotDisturbDate(const sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + handler_->PostSyncTask(std::bind([&]() { - result = NotificationPreferences::GetInstance().SetDoNotDisturbDate(newConfig); + result = NotificationPreferences::GetInstance().SetDoNotDisturbDate(bundleOption, newConfig); if (result == ERR_OK) { NotificationSubscriberManager::GetInstance()->NotifyDoNotDisturbDateChanged(newConfig); } @@ -2034,10 +2059,14 @@ ErrCode AdvancedNotificationService::GetDoNotDisturbDate(sptr bundleOption = GenerateBundleOption(); + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } handler_->PostSyncTask(std::bind([&]() { sptr currentConfig = nullptr; - result = NotificationPreferences::GetInstance().GetDoNotDisturbDate(currentConfig); + result = NotificationPreferences::GetInstance().GetDoNotDisturbDate(bundleOption, currentConfig); if (result == ERR_OK) { int64_t now = GetCurrentTime(); switch (currentConfig->GetDoNotDisturbType()) { @@ -2045,7 +2074,7 @@ ErrCode AdvancedNotificationService::GetDoNotDisturbDate(sptr= currentConfig->GetEndDate()) { date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); - NotificationPreferences::GetInstance().SetDoNotDisturbDate(date); + NotificationPreferences::GetInstance().SetDoNotDisturbDate(bundleOption, date); } else { date = currentConfig; } @@ -2099,5 +2128,18 @@ ErrCode AdvancedNotificationService::IsSupportTemplate(const std::string& templa })); return result; } +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; + } + } + return false; +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index a8fcbaf9b93474d2f53cf696e72b3e2cd68607e2..4dd6b400855747c82bd40459731103674ecf162c 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -21,6 +21,7 @@ #include "ans_inner_errors.h" #include "ans_log_wrapper.h" #include "nlohmann/json.hpp" +#include "os_account_manager.h" namespace OHOS { namespace Notification { @@ -54,7 +55,8 @@ ErrCode NotificationPreferences::AddNotificationSlots( } } - if (result == ERR_OK && (!preferncesDB_->PutSlotsToDisturbeDB(GenerateBundleKey(bundleOption), slots))) { + if (result == ERR_OK && + (!preferncesDB_->PutSlotsToDisturbeDB(bundleOption->GetBundleName(), bundleOption->GetUid(), slots))) { return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; } @@ -81,7 +83,8 @@ ErrCode NotificationPreferences::AddNotificationSlotGroups( } } - if (result == ERR_OK && (!preferncesDB_->PutGroupsToDisturbeDB(GenerateBundleKey(bundleOption), groups))) { + if (result == ERR_OK && + (!preferncesDB_->PutGroupsToDisturbeDB(bundleOption->GetBundleName(), bundleOption->GetUid(), groups))) { return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; } @@ -223,7 +226,8 @@ ErrCode NotificationPreferences::UpdateNotificationSlots( } } - if (result == ERR_OK && (!preferncesDB_->PutSlotsToDisturbeDB(GenerateBundleKey(bundleOption), slots))) { + if (result == ERR_OK && + (!preferncesDB_->PutSlotsToDisturbeDB(bundleOption->GetBundleName(), bundleOption->GetUid(), slots))) { return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; } @@ -251,7 +255,8 @@ ErrCode NotificationPreferences::UpdateNotificationSlotGroups( } } - if (result == ERR_OK && (!preferncesDB_->PutGroupsToDisturbeDB(GenerateBundleKey(bundleOption), groups))) { + if (result == ERR_OK && + (!preferncesDB_->PutGroupsToDisturbeDB(bundleOption->GetBundleName(), bundleOption->GetUid(), groups))) { return ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; } @@ -489,18 +494,51 @@ ErrCode NotificationPreferences::SetNotificationsEnabledForBundle( return result; } -ErrCode NotificationPreferences::GetNotificationsEnabled(bool &enabled) +bool NotificationPreferences::GetActiveUserId(int& userId) { - enabled = preferencesInfo_.GetEnabledAllNotification(); - return ERR_OK; + 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 sptr &bundleOption, bool &enabled) +{ + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_PARAM; + } + + int userId = SUBSCRIBE_USER_INIT; + (void)GetActiveUserId(userId); + + ErrCode result = ERR_OK; + if (!preferencesInfo_.GetEnabledAllNotification(userId, enabled)) { + result = ERR_ANS_INVALID_PARAM; + } + return result; } -ErrCode NotificationPreferences::SetNotificationsEnabled(const bool &enabled) +ErrCode NotificationPreferences::SetNotificationsEnabled( + const sptr &bundleOption, const bool &enabled) { + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_PARAM; + } + + int userId = SUBSCRIBE_USER_INIT; + (void)GetActiveUserId(userId); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; - preferencesInfo.SetEnabledAllNotification(enabled); + preferencesInfo.SetEnabledAllNotification(userId, enabled); ErrCode result = ERR_OK; - if (!preferncesDB_->PutNotificationsEnabled(enabled)) { + if (!preferncesDB_->PutNotificationsEnabled(userId, enabled)) { result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; } @@ -510,18 +548,39 @@ ErrCode NotificationPreferences::SetNotificationsEnabled(const bool &enabled) return result; } -ErrCode NotificationPreferences::GetDoNotDisturbDate(sptr &date) +ErrCode NotificationPreferences::GetDoNotDisturbDate( + const sptr &bundleOption, sptr &date) { - date = preferencesInfo_.GetDoNotDisturbDate(); - return ERR_OK; + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_PARAM; + } + + int userId = SUBSCRIBE_USER_INIT; + (void)GetActiveUserId(userId); + + ErrCode result = ERR_OK; + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; + if (!preferencesInfo.GetDoNotDisturbDate(userId, date)) { + result = ERR_ANS_INVALID_PARAM; + } + return result; } -ErrCode NotificationPreferences::SetDoNotDisturbDate(const sptr date) +ErrCode NotificationPreferences::SetDoNotDisturbDate( + const sptr &bundleOption, const sptr date) { + if (bundleOption == nullptr) { + return ERR_ANS_INVALID_PARAM; + } + + int userId = SUBSCRIBE_USER_INIT; + (void)GetActiveUserId(userId); + NotificationPreferencesInfo preferencesInfo = preferencesInfo_; - preferencesInfo.SetDoNotDisturbDate(date); + preferencesInfo.SetDoNotDisturbDate(userId, date); + ErrCode result = ERR_OK; - if (!preferncesDB_->PutDoNotDisturbDate(date)) { + if (!preferncesDB_->PutDoNotDisturbDate(userId, date)) { result = ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; } @@ -706,23 +765,23 @@ ErrCode NotificationPreferences::SaveBundleProperty(NotificationPreferencesInfo: switch (type) { case BundleType::BUNDLE_IMPORTANCE_TYPE: bundleInfo.SetImportance(value); - storeDBResult = preferncesDB_->PutImportance(bundleOption->GetBundleName(), value); + storeDBResult = preferncesDB_->PutImportance(bundleInfo, value); break; case BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE: bundleInfo.SetBadgeTotalNum(value); - storeDBResult = preferncesDB_->PutTotalBadgeNums(bundleOption->GetBundleName(), value); + storeDBResult = preferncesDB_->PutTotalBadgeNums(bundleInfo, value); break; case BundleType::BUNDLE_SHOW_BADGE_TYPE: bundleInfo.SetIsShowBadge(value); - storeDBResult = preferncesDB_->PutShowBadge(bundleOption->GetBundleName(), value); + storeDBResult = preferncesDB_->PutShowBadge(bundleInfo, value); break; case BundleType::BUNDLE_PRIVATE_ALLOWED_TYPE: bundleInfo.SetIsPrivateAllowed(value); - storeDBResult = preferncesDB_->PutPrivateNotificationsAllowed(bundleOption->GetBundleName(), value); + storeDBResult = preferncesDB_->PutPrivateNotificationsAllowed(bundleInfo, value); break; case BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE: bundleInfo.SetEnableNotification(value); - storeDBResult = preferncesDB_->PutNotificationsEnabledForBundle(bundleOption->GetBundleName(), value); + storeDBResult = preferncesDB_->PutNotificationsEnabledForBundle(bundleInfo, value); break; default: break; diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 809f7f71ae6263bd27e01f9c5e4c2672ecf64ec1..8d40c0e77ada883de3ba1630953887bcc7ecfeb2 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -14,7 +14,9 @@ */ #include "notification_preferences_database.h" +#include "ans_const_define.h" #include "ans_log_wrapper.h" +#include "os_account_manager.h" #include "uri.h" namespace OHOS { @@ -112,6 +114,11 @@ const std::map> &slots) + const std::string &bundleName, const int &bundleUid, const std::vector> &slots) { ANS_LOGD("%{public}s", __FUNCTION__); - if (bundleKey.empty()) { + if (bundleName.empty()) { ANS_LOGE("Bundle name is null."); return false; } @@ -181,7 +188,7 @@ bool NotificationPreferencesDatabase::PutSlotsToDisturbeDB( bool result = true; std::vector entries; for (auto iter : slots) { - result = SlotToEntry(bundleKey, iter, entries); + result = SlotToEntry(bundleName, bundleUid, iter, entries); if (!result) { return result; } @@ -196,9 +203,9 @@ bool NotificationPreferencesDatabase::PutSlotsToDisturbeDB( } bool NotificationPreferencesDatabase::PutGroupsToDisturbeDB( - const std::string &bundleKey, const std::vector> &groups) + const std::string &bundleName, const int &bundleUid, const std::vector> &groups) { - if (bundleKey.empty()) { + if (bundleName.empty()) { ANS_LOGE("Bundle name is null."); return false; } @@ -211,7 +218,7 @@ bool NotificationPreferencesDatabase::PutGroupsToDisturbeDB( bool result = true; std::vector entries; for (auto iter : groups) { - result = GroupToEntry(bundleKey, iter, entries); + result = GroupToEntry(bundleName, bundleUid, iter, entries); if (!result) { return result; } @@ -238,12 +245,12 @@ bool NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( return false; } - std::string bundleKeyStr = KEY_BUNDLE_LABEL + bundleInfo.GetBundleName(); + std::string bundleKeyStr = KEY_BUNDLE_LABEL + GenerateBundleLablel(bundleInfo); bool result = false; GetValueFromDisturbeDB(bundleKeyStr, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { switch (status) { case OHOS::DistributedKv::Status::KEY_NOT_FOUND: { - result = PutBundleToDisturbeDB(bundleInfo.GetBundleName(), bundleInfo); + result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo); break; } case OHOS::DistributedKv::Status::SUCCESS: { @@ -257,62 +264,71 @@ bool NotificationPreferencesDatabase::PutBundlePropertyToDisturbeDB( return result; } -bool NotificationPreferencesDatabase::PutShowBadge(const std::string &bundleKey, const bool &enable) +bool NotificationPreferencesDatabase::PutShowBadge( + const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enable) { - if (bundleKey.empty()) { + if (bundleInfo.GetBundleName().empty()) { ANS_LOGE("Bundle name is null."); return false; } - if (!CheckBundle(bundleKey)) { + if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) { return false; } + + std::string bundleKey = GenerateBundleLablel(bundleInfo); OHOS::DistributedKv::Status status = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_SHOW_BADGE_TYPE, enable); return (status == OHOS::DistributedKv::Status::SUCCESS); } -bool NotificationPreferencesDatabase::PutImportance(const std::string &bundleKey, const int &importance) +bool NotificationPreferencesDatabase::PutImportance( + const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int &importance) { - if (bundleKey.empty()) { + if (bundleInfo.GetBundleName().empty()) { ANS_LOGE("Bundle name is null."); return false; } - if (!CheckBundle(bundleKey)) { + if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) { return false; } + + std::string bundleKey = GenerateBundleLablel(bundleInfo); OHOS::DistributedKv::Status status = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_IMPORTANCE_TYPE, importance); return (status == OHOS::DistributedKv::Status::SUCCESS); } -bool NotificationPreferencesDatabase::PutTotalBadgeNums(const std::string &bundleKey, const int &totalBadgeNum) +bool NotificationPreferencesDatabase::PutTotalBadgeNums( + const NotificationPreferencesInfo::BundleInfo &bundleInfo, const int &totalBadgeNum) { - if (bundleKey.empty()) { + if (bundleInfo.GetBundleName().empty()) { ANS_LOGE("Bundle name is null."); return false; } - if (!CheckBundle(bundleKey)) { + if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) { return false; } - + std::string bundleKey = GenerateBundleLablel(bundleInfo); OHOS::DistributedKv::Status status = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_BADGE_TOTAL_NUM_TYPE, totalBadgeNum); return (status == OHOS::DistributedKv::Status::SUCCESS); } -bool NotificationPreferencesDatabase::PutPrivateNotificationsAllowed(const std::string &bundleKey, const bool &allow) +bool NotificationPreferencesDatabase::PutPrivateNotificationsAllowed( + const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &allow) { - if (bundleKey.empty()) { + if (bundleInfo.GetBundleName().empty()) { ANS_LOGE("Bundle name is null."); return false; } - if (!CheckBundle(bundleKey)) { + if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) { return false; } + std::string bundleKey = GenerateBundleLablel(bundleInfo); OHOS::DistributedKv::Status status = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_PRIVATE_ALLOWED_TYPE, allow); @@ -320,30 +336,33 @@ bool NotificationPreferencesDatabase::PutPrivateNotificationsAllowed(const std:: } bool NotificationPreferencesDatabase::PutNotificationsEnabledForBundle( - const std::string &bundleKey, const bool &enabled) + const NotificationPreferencesInfo::BundleInfo &bundleInfo, const bool &enabled) { - if (bundleKey.empty()) { + if (bundleInfo.GetBundleName().empty()) { ANS_LOGE("Bundle name is null."); return false; } - if (!CheckBundle(bundleKey)) { + if (!CheckBundle(bundleInfo.GetBundleName(), bundleInfo.GetBundleUid())) { return false; } + std::string bundleKey = GenerateBundleLablel(bundleInfo); OHOS::DistributedKv::Status status = PutBundlePropertyToDisturbeDB(bundleKey, BundleType::BUNDLE_ENABLE_NOTIFICATION_TYPE, enabled); return (status == OHOS::DistributedKv::Status::SUCCESS); } -bool NotificationPreferencesDatabase::PutNotificationsEnabled(const bool &enabled) +bool NotificationPreferencesDatabase::PutNotificationsEnabled(const int32_t &userId, const bool &enabled) { if (!CheckKvStore()) { ANS_LOGE("KvStore is nullptr."); return false; } - OHOS::DistributedKv::Key enableKey(KEY_ENABLE_ALL_NOTIFICATION); + std::string typeKey = + std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId)); + OHOS::DistributedKv::Key enableKey(typeKey); OHOS::DistributedKv::Value enableValue(std::to_string(enabled)); OHOS::DistributedKv::Status status; status = kvStorePtr_->Put(enableKey, enableValue); @@ -354,7 +373,8 @@ bool NotificationPreferencesDatabase::PutNotificationsEnabled(const bool &enable return true; } -bool NotificationPreferencesDatabase::PutDoNotDisturbDate(const sptr &date) +bool NotificationPreferencesDatabase::PutDoNotDisturbDate( + const int32_t &userId, const sptr &date) { if (date == nullptr) { ANS_LOGE("Invalid date."); @@ -367,15 +387,21 @@ bool NotificationPreferencesDatabase::PutDoNotDisturbDate(const sptrGetDoNotDisturbType())); OHOS::DistributedKv::Entry beginDate; - beginDate.key = OHOS::DistributedKv::Key(KEY_DO_NOT_DISTURB_BEGIN_DATE); + std::string beginDateKey = + std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); + beginDate.key = OHOS::DistributedKv::Key(beginDateKey); beginDate.value = OHOS::DistributedKv::Value(std::to_string(date->GetBeginDate())); OHOS::DistributedKv::Entry endDate; - endDate.key = OHOS::DistributedKv::Key(KEY_DO_NOT_DISTURB_END_DATE); + std::string endDateKey = + std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); + endDate.key = OHOS::DistributedKv::Key(endDateKey); endDate.value = OHOS::DistributedKv::Value(std::to_string(date->GetEndDate())); std::vector entries = { @@ -432,16 +458,18 @@ void NotificationPreferencesDatabase::GetValueFromDisturbeDB( funcion(status, value); } -bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleKey) +bool NotificationPreferencesDatabase::CheckBundle(const std::string &bundleName, const int &bundleUid) { - std::string bundleKeyStr = KEY_BUNDLE_LABEL + bundleKey; + 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, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { switch (status) { case OHOS::DistributedKv::Status::KEY_NOT_FOUND: { NotificationPreferencesInfo::BundleInfo bundleInfo; - bundleInfo.SetBundleName(bundleKey); - result = PutBundleToDisturbeDB(bundleKey, bundleInfo); + bundleInfo.SetBundleName(bundleName); + bundleInfo.SetBundleUid(bundleUid); + result = PutBundleToDisturbeDB(bundleKeyStr, bundleInfo); break; } case OHOS::DistributedKv::Status::SUCCESS: { @@ -460,8 +488,8 @@ bool NotificationPreferencesDatabase::PutBundlePropertyValueToDisturbeDB( const NotificationPreferencesInfo::BundleInfo &bundleInfo) { std::vector entries; - std::string bundleKey = bundleInfo.GetBundleName(); - GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_NAME), bundleKey, entries); + 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_BADGE_TOTAL_NUM), std::to_string(bundleInfo.GetBadgeTotalNum()), entries); @@ -475,6 +503,7 @@ bool NotificationPreferencesDatabase::PutBundlePropertyValueToDisturbeDB( GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_ENABLE_NOTIFICATION), std::to_string(bundleInfo.GetEnableNotification()), entries); + GenerateEntry(GenerateBundleKey(bundleKey, KEY_BUNDLE_UID), std::to_string(bundleInfo.GetBundleUid()), entries); if (!CheckKvStore()) { ANS_LOGE("KvStore is nullptr."); return false; @@ -727,8 +756,8 @@ bool NotificationPreferencesDatabase::PutBundleToDisturbeDB( } ANS_LOGD("Key not fund, so create a bundle, bundle key is %{public}s.", bundleKey.c_str()); - OHOS::DistributedKv::Key bundleDBKey(KEY_BUNDLE_LABEL + bundleKey); - OHOS::DistributedKv::Value bundleValue(bundleKey); + OHOS::DistributedKv::Key bundleDBKey(bundleKey); + OHOS::DistributedKv::Value bundleValue(GenerateBundleLablel(bundleInfo)); if (kvStorePtr_->Put(bundleDBKey, bundleValue) != OHOS::DistributedKv::Status::SUCCESS) { ANS_LOGE("Store bundle name to db is failed."); return false; @@ -752,18 +781,19 @@ void NotificationPreferencesDatabase::GenerateEntry( entries.push_back(entry); } -bool NotificationPreferencesDatabase::SlotToEntry( - const std::string &bundleKey, const sptr &slot, std::vector &entries) +bool NotificationPreferencesDatabase::SlotToEntry(const std::string &bundleName, const int &bundleUid, + const sptr &slot, std::vector &entries) { if (slot == nullptr) { ANS_LOGE("Notification group is nullptr."); return false; } - if (!CheckBundle(bundleKey)) { + if (!CheckBundle(bundleName, bundleUid)) { return false; } + std::string bundleKey = bundleName + std::to_string(bundleUid); GenerateSlotEntry(bundleKey, slot, entries); return true; } @@ -799,7 +829,7 @@ void NotificationPreferencesDatabase::GenerateSlotEntry(const std::string &bundl entries); } -bool NotificationPreferencesDatabase::GroupToEntry(const std::string &bundleKey, +bool NotificationPreferencesDatabase::GroupToEntry(const std::string &bundleName, const int &bundleUid, const sptr &group, std::vector &entries) { @@ -808,10 +838,11 @@ bool NotificationPreferencesDatabase::GroupToEntry(const std::string &bundleKey, return false; } - if (!CheckBundle(bundleKey)) { + if (!CheckBundle(bundleName, bundleUid)) { return false; } + std::string bundleKey = bundleName + std::to_string(bundleUid); GenerateGroupEntry(bundleKey, group, entries); return true; } @@ -1087,69 +1118,46 @@ std::string NotificationPreferencesDatabase::SubUniqueIdentifyFromString( void NotificationPreferencesDatabase::ParseDoNotDisturbType(NotificationPreferencesInfo &info) { - GetValueFromDisturbeDB( - KEY_DO_NOT_DISTURB_TYPE, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { - if (status == OHOS::DistributedKv::Status::KEY_NOT_FOUND) { - PutDoNotDisturbDate(info.GetDoNotDisturbDate()); - } else if (status == OHOS::DistributedKv::Status::SUCCESS) { - if (!value.ToString().empty()) { - auto date = info.GetDoNotDisturbDate(); - date->SetDoNotDisturbType((NotificationConstant::DoNotDisturbType)StringToInt(value.ToString())); - } - } else { - ANS_LOGW("Parse disturbe mode failed, use defalut value."); - } - }); + std::vector osAccountInfos; + OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); + + for (auto iter : osAccountInfos) { + int userId = iter.GetLocalId(); + NotificationPreferencesDatabase::GetDoNotDisturbType(info, userId); + } } void NotificationPreferencesDatabase::ParseDoNotDisturbBeginDate(NotificationPreferencesInfo &info) { - GetValueFromDisturbeDB( - KEY_DO_NOT_DISTURB_BEGIN_DATE, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { - if (status == OHOS::DistributedKv::Status::KEY_NOT_FOUND) { - PutDoNotDisturbDate(info.GetDoNotDisturbDate()); - } else if (status == OHOS::DistributedKv::Status::SUCCESS) { - if (!value.ToString().empty()) { - auto date = info.GetDoNotDisturbDate(); - date->SetBeginDate(StringToInt64(value.ToString())); - } - } else { - ANS_LOGW("Parse disturbe start time failed, use defalut value."); - } - }); + std::vector osAccountInfos; + OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); + + for (auto iter : osAccountInfos) { + int userId = iter.GetLocalId(); + NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(info, userId); + } } void NotificationPreferencesDatabase::ParseDoNotDisturbEndDate(NotificationPreferencesInfo &info) { - GetValueFromDisturbeDB( - KEY_DO_NOT_DISTURB_END_DATE, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { - if (status == OHOS::DistributedKv::Status::KEY_NOT_FOUND) { - PutDoNotDisturbDate(info.GetDoNotDisturbDate()); - } else if (status == OHOS::DistributedKv::Status::SUCCESS) { - if (!value.ToString().empty()) { - auto date = info.GetDoNotDisturbDate(); - date->SetEndDate(StringToInt64(value.ToString())); - } - } else { - ANS_LOGW("Parse disturbe end time failed, use defalut value."); - } - }); + std::vector osAccountInfos; + OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); + + for (auto iter : osAccountInfos) { + int userId = iter.GetLocalId(); + NotificationPreferencesDatabase::GetDoNotDisturbEndDate(info, userId); + } } void NotificationPreferencesDatabase::ParseEnableAllNotification(NotificationPreferencesInfo &info) { - GetValueFromDisturbeDB( - KEY_ENABLE_ALL_NOTIFICATION, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { - if (status == OHOS::DistributedKv::Status::KEY_NOT_FOUND) { - PutNotificationsEnabled(info.GetEnabledAllNotification()); - } else if (status == OHOS::DistributedKv::Status::SUCCESS) { - if (!value.ToString().empty()) { - info.SetEnabledAllNotification(static_cast(StringToInt(value.ToString()))); - } - } else { - ANS_LOGW("Parse enable all notification failed, use defalut value."); - } - }); + std::vector osAccountInfos; + OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(osAccountInfos); + + for (auto iter : osAccountInfos) { + int userId = iter.GetLocalId(); + NotificationPreferencesDatabase::GetEnableAllNotification(info, userId); + } } void NotificationPreferencesDatabase::ParseGroupDescription( @@ -1206,6 +1214,13 @@ void NotificationPreferencesDatabase::ParseBundleEnableNotification( bundleInfo.SetEnableNotification(static_cast(StringToInt(value))); } +void NotificationPreferencesDatabase::ParseBundleUid( + NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const +{ + ANS_LOGD("SetBundleUid uuid is %{public}s.", value.c_str()); + bundleInfo.SetBundleUid(StringToInt(value)); +} + void NotificationPreferencesDatabase::ParseSlotGroupId(sptr &slot, const std::string &value) const { ANS_LOGD("ParseSlotGroupId slot group id is %{public}s.", value.c_str()); @@ -1291,5 +1306,104 @@ void NotificationPreferencesDatabase::ParseSlotEnableBypassDnd( bool enable = static_cast(StringToInt(value)); slot->EnableBypassDnd(enable); } + +std::string NotificationPreferencesDatabase::GenerateBundleLablel( + const NotificationPreferencesInfo::BundleInfo &bundleInfo) const +{ + return bundleInfo.GetBundleName().append(std::to_string(bundleInfo.GetBundleUid())); +} +void NotificationPreferencesDatabase::GetDoNotDisturbType(NotificationPreferencesInfo &info, int userId) +{ + std::string key = + std::string().append(KEY_DO_NOT_DISTURB_TYPE).append(KEY_UNDER_LINE).append(std::to_string(userId)); + GetValueFromDisturbeDB( + key, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { + sptr disturbDate = + new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + info.GetDoNotDisturbDate(userId, disturbDate); + if (status == OHOS::DistributedKv::Status::KEY_NOT_FOUND) { + PutDoNotDisturbDate(userId, disturbDate); + } else if (status == OHOS::DistributedKv::Status::SUCCESS) { + if (!value.ToString().empty()) { + if (disturbDate != nullptr) { + disturbDate->SetDoNotDisturbType( + (NotificationConstant::DoNotDisturbType)StringToInt(value.ToString())); + } + } + } else { + ANS_LOGW("Parse disturbe mode failed, use defalut value."); + } + info.SetDoNotDisturbDate(userId, disturbDate); + }); +} + +void NotificationPreferencesDatabase::GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int userId) +{ + std::string key = + std::string().append(KEY_DO_NOT_DISTURB_BEGIN_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); + GetValueFromDisturbeDB( + key, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { + sptr disturbDate = + new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + info.GetDoNotDisturbDate(userId, disturbDate); + if (status == OHOS::DistributedKv::Status::KEY_NOT_FOUND) { + PutDoNotDisturbDate(userId, disturbDate); + } else if (status == OHOS::DistributedKv::Status::SUCCESS) { + if (!value.ToString().empty()) { + if (disturbDate != nullptr) { + disturbDate->SetBeginDate(StringToInt64(value.ToString())); + } + } + } else { + ANS_LOGW("Parse disturbe start time failed, use defalut value."); + } + info.SetDoNotDisturbDate(userId, disturbDate); + }); +} + +void NotificationPreferencesDatabase::GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int userId) +{ + std::string key = + std::string().append(KEY_DO_NOT_DISTURB_END_DATE).append(KEY_UNDER_LINE).append(std::to_string(userId)); + GetValueFromDisturbeDB( + key, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { + sptr disturbDate = + new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); + info.GetDoNotDisturbDate(userId, disturbDate); + if (status == OHOS::DistributedKv::Status::KEY_NOT_FOUND) { + PutDoNotDisturbDate(userId, disturbDate); + } else if (status == OHOS::DistributedKv::Status::SUCCESS) { + if (!value.ToString().empty()) { + if (disturbDate != nullptr) { + disturbDate->SetEndDate(StringToInt64(value.ToString())); + } + } + } else { + ANS_LOGW("Parse disturbe end time failed, use defalut value."); + } + info.SetDoNotDisturbDate(userId, disturbDate); + }); +} + +void NotificationPreferencesDatabase::GetEnableAllNotification(NotificationPreferencesInfo &info, int userId) +{ + std::string key = + std::string().append(KEY_ENABLE_ALL_NOTIFICATION).append(KEY_UNDER_LINE).append(std::to_string(userId)); + GetValueFromDisturbeDB( + key, [&](OHOS::DistributedKv::Status &status, OHOS::DistributedKv::Value &value) { + if (status == OHOS::DistributedKv::Status::KEY_NOT_FOUND) { + bool enable = true; + info.GetEnabledAllNotification(userId, enable); + PutNotificationsEnabled(userId, enable); + } else if (status == OHOS::DistributedKv::Status::SUCCESS) { + if (!value.ToString().empty()) { + info.SetEnabledAllNotification(userId, static_cast(StringToInt(value.ToString()))); + } + } else { + ANS_LOGW("Parse enable all notification failed, use defalut value."); + } + }); +} + } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/notification_preferences_info.cpp b/services/ans/src/notification_preferences_info.cpp index f58a968b7ba62b7c712d208c2d324082888cdfd8..586288ac323ca216e3b71f50051294929fb90815 100644 --- a/services/ans/src/notification_preferences_info.cpp +++ b/services/ans/src/notification_preferences_info.cpp @@ -227,26 +227,6 @@ int NotificationPreferencesInfo::BundleInfo::GetBundleUid() const return uid_; } -void NotificationPreferencesInfo::SetEnabledAllNotification(const bool &value) -{ - isEnabledAllNotification_ = value; -} - -bool NotificationPreferencesInfo::GetEnabledAllNotification() const -{ - return isEnabledAllNotification_; -} - -void NotificationPreferencesInfo::SetDoNotDisturbDate(const sptr &date) -{ - doNotDisturbDate_ = date; -} - -sptr NotificationPreferencesInfo::GetDoNotDisturbDate() const -{ - return doNotDisturbDate_; -} - void NotificationPreferencesInfo::SetBundleInfo(const BundleInfo &info) { std::string bundleKey = info.GetBundleName().append(std::to_string(info.GetBundleUid())); @@ -290,5 +270,37 @@ void NotificationPreferencesInfo::ClearBundleInfo() { infos_.clear(); } + +void NotificationPreferencesInfo::SetDoNotDisturbDate(const int32_t &userId, + const sptr &doNotDisturbDate) +{ + doNotDisturbDate_.insert_or_assign(userId, doNotDisturbDate); +} + +bool NotificationPreferencesInfo::GetDoNotDisturbDate(const int32_t &userId, + sptr &doNotDisturbDate) const +{ + auto iter = doNotDisturbDate_.find(userId); + if (iter != doNotDisturbDate_.end()) { + doNotDisturbDate = iter->second; + return true; + } + return false; +} + +void NotificationPreferencesInfo::SetEnabledAllNotification(const int32_t &userId, const bool &enable) +{ + isEnabledAllNotification_.insert_or_assign(userId, enable); +} + +bool NotificationPreferencesInfo::GetEnabledAllNotification(const int32_t &userId, bool &enable) const +{ + auto iter = isEnabledAllNotification_.find(userId); + if (iter != isEnabledAllNotification_.end()) { + enable = iter->second; + return true; + } + return false; +} } // namespace Notification -} // namespace OHOS \ No newline at end of file +} // namespace OHOS diff --git a/services/ans/src/notification_subscriber_manager.cpp b/services/ans/src/notification_subscriber_manager.cpp index a37ece9e200854d3fe9ef7d4b62606fe5cce89be..86d467ffa89bb53cff47d77121b7d2494acbbfb4 100644 --- a/services/ans/src/notification_subscriber_manager.cpp +++ b/services/ans/src/notification_subscriber_manager.cpp @@ -19,8 +19,11 @@ #include #include +#include "ans_const_define.h" #include "ans_inner_errors.h" #include "ans_log_wrapper.h" +#include "ipc_skeleton.h" +#include "os_account_manager.h" #include "remote_death_recipient.h" namespace OHOS { @@ -29,6 +32,7 @@ struct NotificationSubscriberManager::SubscriberRecord { sptr subscriber {nullptr}; std::set bundleList_ {}; bool subscribedAll {false}; + int32_t userId {SUBSCRIBE_USER_INIT}; }; NotificationSubscriberManager::NotificationSubscriberManager() @@ -52,9 +56,23 @@ ErrCode NotificationSubscriberManager::AddSubscriber( return ERR_ANS_INVALID_PARAM; } + sptr subInfo = subscribeInfo; + if (subInfo == nullptr) { + subInfo = new (std::nothrow) NotificationSubscribeInfo(); + if (subInfo == nullptr) { + ANS_LOGE("Failed to create NotificationSubscribeInfo ptr."); + return ERR_ANS_NO_MEMORY; + } + + int userId = SUBSCRIBE_USER_INIT; + int uid = IPCSkeleton::GetCallingUid(); + OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId); + subInfo->AddAppUserId(userId); + } + ErrCode result = ERR_ANS_TASK_ERR; - handler_->PostSyncTask(std::bind([this, &subscriber, &subscribeInfo, &result]() { - result = this->AddSubscriberInner(subscriber, subscribeInfo); + handler_->PostSyncTask(std::bind([this, &subscriber, &subInfo, &result]() { + result = this->AddSubscriberInner(subscriber, subInfo); }), AppExecFwk::EventQueue::Priority::HIGH); return result; @@ -188,6 +206,7 @@ void NotificationSubscriberManager::AddRecordInfo( for (auto bundle : subscribeInfo->GetAppNames()) { record->bundleList_.insert(bundle); } + record->userId = subscribeInfo->GetAppUserId(); } else { record->bundleList_.clear(); record->subscribedAll = true; @@ -266,7 +285,8 @@ void NotificationSubscriberManager::NotifyConsumedInner( auto BundleNames = notification->GetBundleName(); auto iter = std::find(record->bundleList_.begin(), record->bundleList_.end(), BundleNames); - if (!record->subscribedAll == (iter != record->bundleList_.end())) { + if (!record->subscribedAll == (iter != record->bundleList_.end()) && + (notification->GetUserId() == record->userId || notification->GetUserId() == SUBSCRIBE_USER_ALL)) { record->subscriber->OnConsumed(notification, notificationMap); record->subscriber->OnConsumed(notification); } @@ -280,7 +300,8 @@ void NotificationSubscriberManager::NotifyCanceledInner( auto BundleNames = notification->GetBundleName(); auto iter = std::find(record->bundleList_.begin(), record->bundleList_.end(), BundleNames); - if (!record->subscribedAll == (iter != record->bundleList_.end())) { + if (!record->subscribedAll == (iter != record->bundleList_.end()) && + (notification->GetUserId() == record->userId || notification->GetUserId() == SUBSCRIBE_USER_ALL)) { record->subscriber->OnCanceled(notification, notificationMap, deleteReason); record->subscriber->OnCanceled(notification); } diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index fbb6437500e0fdfc4bd8d51dfc41ebfa2fa3ce22..d29b10ee98212b72f64be5c305b387912281fa2c 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -31,6 +31,10 @@ ohos_unittest("ans_unit_test") { "//utils/system/safwk/native/include", "/${services_path}/ans/include", "${services_path}/ans/test/unittest/mock/include", + "//base/account/os_account/interfaces/innerkits/osaccount/native/include", + "//base/account/os_account/frameworks/common/database/include/", + "//base/account/os_account/frameworks/common/account_error/include", + "//base/account/os_account/frameworks/common/log/include", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/autils", "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/log", @@ -77,6 +81,7 @@ ohos_unittest("ans_unit_test") { "${core_path}:ans_core", "${frameworks_path}/ans/native:ans_innerkits", "${frameworks_path}/wantagent:wantagent_innerkits", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", "//base/miscservices/time/services:time_service", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/distributeddata:distributeddata_inner", "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri", diff --git a/services/ans/test/unittest/notification_preferences_database_test.cpp b/services/ans/test/unittest/notification_preferences_database_test.cpp index b294f18e74bf7b02abbf99326214ccc9b404e36d..e4857df84887d65b3e6b5bf94f2146cd7b9e7e28 100644 --- a/services/ans/test/unittest/notification_preferences_database_test.cpp +++ b/services/ans/test/unittest/notification_preferences_database_test.cpp @@ -29,6 +29,8 @@ public: void TearDown() {}; const std::string bundleName_ = "bundleName"; + const int bundleUid_ = 2001; + int32_t userId = 100; std::unique_ptr preferncesDB_ = std::make_unique(); }; @@ -45,7 +47,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutSlotsToDisturbeDB_00100, Functi sptr slot2 = new NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER); slots.push_back(slot1); slots.push_back(slot2); - EXPECT_TRUE(preferncesDB_->PutSlotsToDisturbeDB(bundleName_, slots)); + EXPECT_TRUE(preferncesDB_->PutSlotsToDisturbeDB(bundleName_, bundleUid_, slots)); } /** @@ -60,7 +62,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutSlotsToDisturbeDB_00200, Functi sptr slot2 = new NotificationSlot(NotificationConstant::SlotType::SERVICE_REMINDER); slots.push_back(slot1); slots.push_back(slot2); - EXPECT_FALSE(preferncesDB_->PutSlotsToDisturbeDB(std::string(), slots)); + EXPECT_FALSE(preferncesDB_->PutSlotsToDisturbeDB(std::string(), 0, slots)); } /** @@ -71,7 +73,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutSlotsToDisturbeDB_00200, Functi HWTEST_F(NotificationPreferencesDatabaseTest, PutSlotsToDisturbeDB_00300, Function | SmallTest | Level1) { std::vector> slots; - EXPECT_FALSE(preferncesDB_->PutSlotsToDisturbeDB(bundleName_, slots)); + EXPECT_FALSE(preferncesDB_->PutSlotsToDisturbeDB(bundleName_, bundleUid_, slots)); } /** @@ -85,7 +87,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutGroupsToDisturbeDB_00100, Funct std::vector> groups; groups.push_back(slotGroup); - EXPECT_TRUE(preferncesDB_->PutGroupsToDisturbeDB(bundleName_, groups)); + EXPECT_TRUE(preferncesDB_->PutGroupsToDisturbeDB(bundleName_, bundleUid_, groups)); } /** @@ -98,7 +100,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutGroupsToDisturbeDB_00200, Funct sptr slotGroup = new NotificationSlotGroup("id", "name"); std::vector> groups; groups.push_back(slotGroup); - EXPECT_FALSE(preferncesDB_->PutGroupsToDisturbeDB(std::string(), groups)); + EXPECT_FALSE(preferncesDB_->PutGroupsToDisturbeDB(std::string(), 0, groups)); } /** @@ -109,7 +111,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutGroupsToDisturbeDB_00200, Funct HWTEST_F(NotificationPreferencesDatabaseTest, PutGroupsToDisturbeDB_00300, Function | SmallTest | Level1) { std::vector> groups; - EXPECT_FALSE(preferncesDB_->PutGroupsToDisturbeDB(bundleName_, groups)); + EXPECT_FALSE(preferncesDB_->PutGroupsToDisturbeDB(bundleName_, bundleUid_, groups)); } /** @@ -119,8 +121,11 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutGroupsToDisturbeDB_00300, Funct */ HWTEST_F(NotificationPreferencesDatabaseTest, PutShowBadge_00100, Function | SmallTest | Level1) { - EXPECT_TRUE(preferncesDB_->PutShowBadge(bundleName_, true)); - EXPECT_TRUE(preferncesDB_->PutShowBadge(bundleName_, false)); + NotificationPreferencesInfo::BundleInfo bundleInfo; + bundleInfo.SetBundleName(bundleName_); + bundleInfo.SetBundleUid(bundleUid_); + EXPECT_TRUE(preferncesDB_->PutShowBadge(bundleInfo, true)); + EXPECT_TRUE(preferncesDB_->PutShowBadge(bundleInfo, false)); } /** @@ -130,7 +135,9 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutShowBadge_00100, Function | Sma */ HWTEST_F(NotificationPreferencesDatabaseTest, PutShowBadge_00200, Function | SmallTest | Level1) { - EXPECT_FALSE(preferncesDB_->PutShowBadge(std::string(), false)); + NotificationPreferencesInfo::BundleInfo bundleInfo; + bundleInfo.SetBundleName(std::string()); + EXPECT_FALSE(preferncesDB_->PutShowBadge(bundleInfo, false)); } /** @@ -140,18 +147,22 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutShowBadge_00200, Function | Sma */ HWTEST_F(NotificationPreferencesDatabaseTest, PutImportance_00100, Function | SmallTest | Level1) { + NotificationPreferencesInfo::BundleInfo bundleInfo; + bundleInfo.SetBundleName(bundleName_); + bundleInfo.SetBundleUid(bundleUid_); + EXPECT_TRUE( - preferncesDB_->PutImportance(bundleName_, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_NONE)); + preferncesDB_->PutImportance(bundleInfo, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_NONE)); EXPECT_TRUE( - preferncesDB_->PutImportance(bundleName_, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_MIN)); + preferncesDB_->PutImportance(bundleInfo, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_MIN)); EXPECT_TRUE( - preferncesDB_->PutImportance(bundleName_, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_LOW)); + preferncesDB_->PutImportance(bundleInfo, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_LOW)); EXPECT_TRUE(preferncesDB_->PutImportance( - bundleName_, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_DEFAULT)); + bundleInfo, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_DEFAULT)); EXPECT_TRUE( - preferncesDB_->PutImportance(bundleName_, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_HIGH)); + preferncesDB_->PutImportance(bundleInfo, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_HIGH)); EXPECT_TRUE(preferncesDB_->PutImportance( - bundleName_, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_UNDEFINED)); + bundleInfo, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_UNDEFINED)); } /** @@ -161,8 +172,12 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutImportance_00100, Function | Sm */ HWTEST_F(NotificationPreferencesDatabaseTest, PutImportance_00200, Function | SmallTest | Level1) { - EXPECT_FALSE(preferncesDB_->PutImportance( - std::string(), OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_NONE)); + NotificationPreferencesInfo::BundleInfo bundleInfo; + bundleInfo.SetBundleName(std::string()); + bundleInfo.SetBundleUid(0); + + EXPECT_FALSE( + preferncesDB_->PutImportance(bundleInfo, OHOS::Notification::NotificationSlot::NotificationLevel::LEVEL_NONE)); } /** @@ -172,7 +187,10 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutImportance_00200, Function | Sm */ HWTEST_F(NotificationPreferencesDatabaseTest, PutTotalBadgeNums_00100, Function | SmallTest | Level1) { - EXPECT_TRUE(preferncesDB_->PutTotalBadgeNums(bundleName_, 0)); + NotificationPreferencesInfo::BundleInfo bundleInfo; + bundleInfo.SetBundleName(bundleName_); + bundleInfo.SetBundleUid(bundleUid_); + EXPECT_TRUE(preferncesDB_->PutTotalBadgeNums(bundleInfo, 0)); } /** @@ -182,7 +200,10 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutTotalBadgeNums_00100, Function */ HWTEST_F(NotificationPreferencesDatabaseTest, PutTotalBadgeNums_00200, Function | SmallTest | Level1) { - EXPECT_FALSE(preferncesDB_->PutTotalBadgeNums(std::string(), 0)); + NotificationPreferencesInfo::BundleInfo bundleInfo; + bundleInfo.SetBundleName(std::string()); + bundleInfo.SetBundleUid(bundleUid_); + EXPECT_FALSE(preferncesDB_->PutTotalBadgeNums(bundleInfo, 0)); } /** @@ -192,8 +213,11 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutTotalBadgeNums_00200, Function */ HWTEST_F(NotificationPreferencesDatabaseTest, PutPrivateNotificationsAllowed_00100, Function | SmallTest | Level1) { - EXPECT_TRUE(preferncesDB_->PutPrivateNotificationsAllowed(bundleName_, true)); - EXPECT_TRUE(preferncesDB_->PutPrivateNotificationsAllowed(bundleName_, true)); + NotificationPreferencesInfo::BundleInfo bundleInfo; + bundleInfo.SetBundleName(bundleName_); + bundleInfo.SetBundleUid(bundleUid_); + EXPECT_TRUE(preferncesDB_->PutPrivateNotificationsAllowed(bundleInfo, true)); + EXPECT_TRUE(preferncesDB_->PutPrivateNotificationsAllowed(bundleInfo, true)); } /** @@ -203,7 +227,10 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutPrivateNotificationsAllowed_001 */ HWTEST_F(NotificationPreferencesDatabaseTest, PutPrivateNotificationsAllowed_00200, Function | SmallTest | Level1) { - EXPECT_FALSE(preferncesDB_->PutPrivateNotificationsAllowed(std::string(), false)); + NotificationPreferencesInfo::BundleInfo bundleInfo; + bundleInfo.SetBundleName(std::string()); + bundleInfo.SetBundleUid(bundleUid_); + EXPECT_FALSE(preferncesDB_->PutPrivateNotificationsAllowed(bundleInfo, false)); } /** @@ -213,8 +240,11 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutPrivateNotificationsAllowed_002 */ HWTEST_F(NotificationPreferencesDatabaseTest, PutNotificationsEnabledForBundle_00100, Function | SmallTest | Level1) { - EXPECT_TRUE(preferncesDB_->PutNotificationsEnabledForBundle(bundleName_, true)); - EXPECT_TRUE(preferncesDB_->PutNotificationsEnabledForBundle(bundleName_, false)); + NotificationPreferencesInfo::BundleInfo bundleInfo; + bundleInfo.SetBundleName(bundleName_); + bundleInfo.SetBundleUid(bundleUid_); + EXPECT_TRUE(preferncesDB_->PutNotificationsEnabledForBundle(bundleInfo, true)); + EXPECT_TRUE(preferncesDB_->PutNotificationsEnabledForBundle(bundleInfo, false)); } /** @@ -224,7 +254,10 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutNotificationsEnabledForBundle_0 */ HWTEST_F(NotificationPreferencesDatabaseTest, PutNotificationsEnabledForBundle_00200, Function | SmallTest | Level1) { - EXPECT_FALSE(preferncesDB_->PutNotificationsEnabledForBundle(std::string(), false)); + NotificationPreferencesInfo::BundleInfo bundleInfo; + bundleInfo.SetBundleName(std::string()); + bundleInfo.SetBundleUid(bundleUid_); + EXPECT_FALSE(preferncesDB_->PutNotificationsEnabledForBundle(bundleInfo, false)); } /** @@ -234,8 +267,8 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutNotificationsEnabledForBundle_0 */ HWTEST_F(NotificationPreferencesDatabaseTest, PutNotificationsEnabled_00100, Function | SmallTest | Level1) { - EXPECT_TRUE(preferncesDB_->PutNotificationsEnabled(true)); - EXPECT_TRUE(preferncesDB_->PutNotificationsEnabled(false)); + EXPECT_TRUE(preferncesDB_->PutNotificationsEnabled(userId, true)); + EXPECT_TRUE(preferncesDB_->PutNotificationsEnabled(userId, false)); } /** @@ -247,7 +280,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutDoNotDisturbDate_00100, Functio { sptr date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0); - EXPECT_TRUE(preferncesDB_->PutDoNotDisturbDate(date)); + EXPECT_TRUE(preferncesDB_->PutDoNotDisturbDate(userId, date)); } /** @@ -265,7 +298,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutDoNotDisturbDate_00200, Functio int64_t endDate = endDuration.count(); sptr date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::ONCE, beginDate, endDate); - EXPECT_TRUE(preferncesDB_->PutDoNotDisturbDate(date)); + EXPECT_TRUE(preferncesDB_->PutDoNotDisturbDate(userId, date)); } /** @@ -284,7 +317,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutDoNotDisturbDate_00300, Functio sptr date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::DAILY, beginDate, endDate); - EXPECT_TRUE(preferncesDB_->PutDoNotDisturbDate(date)); + EXPECT_TRUE(preferncesDB_->PutDoNotDisturbDate(userId, date)); } /** @@ -303,7 +336,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutDoNotDisturbDate_00400, Functio sptr date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::CLEARLY, beginDate, endDate); - EXPECT_TRUE(preferncesDB_->PutDoNotDisturbDate(date)); + EXPECT_TRUE(preferncesDB_->PutDoNotDisturbDate(userId, date)); } /** @@ -313,7 +346,10 @@ HWTEST_F(NotificationPreferencesDatabaseTest, PutDoNotDisturbDate_00400, Functio */ HWTEST_F(NotificationPreferencesDatabaseTest, ParseFromDisturbeDB_00100, Function | SmallTest | Level1) { - EXPECT_TRUE(preferncesDB_->PutPrivateNotificationsAllowed(bundleName_, true)); + NotificationPreferencesInfo::BundleInfo bundleInfo; + bundleInfo.SetBundleName(bundleName_); + bundleInfo.SetBundleUid(bundleUid_); + EXPECT_TRUE(preferncesDB_->PutPrivateNotificationsAllowed(bundleInfo, true)); NotificationPreferencesInfo info; EXPECT_TRUE(preferncesDB_->ParseFromDisturbeDB(info)); } @@ -335,7 +371,10 @@ HWTEST_F(NotificationPreferencesDatabaseTest, RemoveAllDataFromDisturbeDB_00100, */ HWTEST_F(NotificationPreferencesDatabaseTest, RemoveBundleFromDisturbeDB_00100, Function | SmallTest | Level1) { - EXPECT_TRUE(preferncesDB_->PutTotalBadgeNums(bundleName_, 0)); + NotificationPreferencesInfo::BundleInfo bundleInfo; + bundleInfo.SetBundleName(bundleName_); + bundleInfo.SetBundleUid(bundleUid_); + EXPECT_TRUE(preferncesDB_->PutTotalBadgeNums(bundleInfo, 0)); EXPECT_EQ(true, preferncesDB_->RemoveBundleFromDisturbeDB(bundleName_)); } @@ -359,7 +398,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, RemoveSlotFromDisturbeDB_00100, Fu std::vector> slots; sptr slot1 = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); slots.push_back(slot1); - EXPECT_TRUE(preferncesDB_->PutSlotsToDisturbeDB(bundleName_, slots)); + EXPECT_TRUE(preferncesDB_->PutSlotsToDisturbeDB(bundleName_, bundleUid_, slots)); EXPECT_TRUE(preferncesDB_->RemoveSlotFromDisturbeDB( bundleName_, OHOS::Notification::NotificationConstant::SlotType::SOCIAL_COMMUNICATION)); @@ -386,7 +425,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, RemoveGroupsFromDisturbeDB_00100, sptr slotGroup = new NotificationSlotGroup("id", "name"); std::vector> groups; groups.push_back(slotGroup); - EXPECT_TRUE(preferncesDB_->PutGroupsToDisturbeDB(bundleName_, groups)); + EXPECT_TRUE(preferncesDB_->PutGroupsToDisturbeDB(bundleName_, bundleUid_, groups)); std::vector groupIds; groupIds.push_back("id"); EXPECT_TRUE(preferncesDB_->RemoveGroupsFromDisturbeDB(bundleName_, groupIds)); @@ -467,7 +506,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ChangeSlotToEntry_00100, Function { sptr slot = new NotificationSlot(NotificationConstant::SlotType::SOCIAL_COMMUNICATION); std::vector entries; - EXPECT_TRUE(preferncesDB_->SlotToEntry(bundleName_, slot, entries)); + EXPECT_TRUE(preferncesDB_->SlotToEntry(bundleName_, bundleUid_, slot, entries)); } /** @@ -479,7 +518,8 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ChangeGroupToEntry_00100, Function { sptr slotGroup = new NotificationSlotGroup("id", "name"); std::vector entries; - EXPECT_TRUE(preferncesDB_->GroupToEntry(bundleName_, slotGroup, entries)); + + EXPECT_TRUE(preferncesDB_->GroupToEntry(bundleName_, bundleUid_, slotGroup, entries)); } /** @@ -489,7 +529,7 @@ HWTEST_F(NotificationPreferencesDatabaseTest, ChangeGroupToEntry_00100, Function */ HWTEST_F(NotificationPreferencesDatabaseTest, CheckBundle_00100, Function | SmallTest | Level1) { - EXPECT_EQ(true, preferncesDB_->CheckBundle(bundleName_)); + EXPECT_EQ(true, preferncesDB_->CheckBundle(bundleName_, bundleUid_)); } } // namespace Notification } // namespace OHOS diff --git a/services/ans/test/unittest/notification_preferences_test.cpp b/services/ans/test/unittest/notification_preferences_test.cpp index f1ec114e4ba29a4e1e2055d39d392bbb81503cd4..c909c9b04fae979823be2b5321d32466c6ede70a 100644 --- a/services/ans/test/unittest/notification_preferences_test.cpp +++ b/services/ans/test/unittest/notification_preferences_test.cpp @@ -1017,7 +1017,7 @@ HWTEST_F(NotificationPreferencesTest, NotificationPreferencesTest_02900, Functio */ HWTEST_F(NotificationPreferencesTest, SetNotificationsEnabled_00100, Function | SmallTest | Level1) { - EXPECT_EQ((int)NotificationPreferences::GetInstance().SetNotificationsEnabled(true), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetNotificationsEnabled(bundleOption_, true), (int)ERR_OK); } /** @@ -1027,9 +1027,9 @@ HWTEST_F(NotificationPreferencesTest, SetNotificationsEnabled_00100, Function | */ HWTEST_F(NotificationPreferencesTest, GetNotificationsEnabled_00100, Function | SmallTest | Level1) { - EXPECT_EQ((int)NotificationPreferences::GetInstance().SetNotificationsEnabled(true), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetNotificationsEnabled(bundleOption_, true), (int)ERR_OK); bool enable = false; - EXPECT_EQ((int)NotificationPreferences::GetInstance().GetNotificationsEnabled(enable), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetNotificationsEnabled(bundleOption_, enable), (int)ERR_OK); EXPECT_TRUE(enable); } @@ -1049,7 +1049,7 @@ HWTEST_F(NotificationPreferencesTest, SetDoNotDisturbDate_00100, Function | Smal sptr date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::ONCE, beginDate, endDate); - EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDoNotDisturbDate(date), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDoNotDisturbDate(bundleOption_, date), (int)ERR_OK); } /** @@ -1067,13 +1067,58 @@ HWTEST_F(NotificationPreferencesTest, GetDoNotDisturbDate_00100, Function | Smal int64_t endDate = endDuration.count(); sptr date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::DAILY, beginDate, endDate); - EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDoNotDisturbDate(date), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDoNotDisturbDate(bundleOption_, date), (int)ERR_OK); sptr getDate; - EXPECT_EQ((int)NotificationPreferences::GetInstance().GetDoNotDisturbDate(getDate), (int)ERR_OK); + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetDoNotDisturbDate(bundleOption_, getDate), (int)ERR_OK); EXPECT_EQ(getDate->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::DAILY); EXPECT_EQ(getDate->GetBeginDate(), beginDate); EXPECT_EQ(getDate->GetEndDate(), endDate); } + +/** + * @tc.number : GetNotificationsEnabled_00200 + * @tc.name : + * @tc.desc : Same user can get enable setting, different user can not get. + */ +HWTEST_F(NotificationPreferencesTest, GetNotificationsEnabled_00200, Function | SmallTest | Level1) +{ + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetNotificationsEnabled(bundleOption_, true), (int)ERR_OK); + bool enable = false; + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetNotificationsEnabled(bundleOption_, enable), (int)ERR_OK); + EXPECT_TRUE(enable); + + enable = false; + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetNotificationsEnabled(bundleOption_, enable), (int)ERR_OK); + EXPECT_FALSE(enable); +} + +/** + * @tc.number : GetDoNotDisturbDate_00200 + * @tc.name : + * @tc.desc : Same user can get DoNotDisturbDate setting, different user can not get. + */ +HWTEST_F(NotificationPreferencesTest, GetDoNotDisturbDate_00200, Function | SmallTest | Level1) +{ + std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now(); + auto beginDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + int64_t beginDate = beginDuration.count(); + timePoint += std::chrono::hours(1); + auto endDuration = std::chrono::duration_cast(timePoint.time_since_epoch()); + int64_t endDate = endDuration.count(); + sptr date = + new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::DAILY, beginDate, endDate); + EXPECT_EQ((int)NotificationPreferences::GetInstance().SetDoNotDisturbDate(bundleOption_, date), (int)ERR_OK); + + sptr getDate; + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetDoNotDisturbDate(bundleOption_, getDate), (int)ERR_OK); + EXPECT_EQ(getDate->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::DAILY); + EXPECT_EQ(getDate->GetBeginDate(), beginDate); + EXPECT_EQ(getDate->GetEndDate(), endDate); + + sptr getExsitDate; + EXPECT_EQ((int)NotificationPreferences::GetInstance().GetDoNotDisturbDate( + noExsitbundleOption_, getExsitDate), (int)ERR_ANS_INVALID_PARAM); +} } // namespace Notification } // namespace OHOS diff --git a/services/test/moduletest/BUILD.gn b/services/test/moduletest/BUILD.gn index 93dbc3c8226cb71c245c74483d8fb10f0b715053..217cb3f368201d7c2c0ba90ebfde5f365a6f55dc 100644 --- a/services/test/moduletest/BUILD.gn +++ b/services/test/moduletest/BUILD.gn @@ -27,6 +27,10 @@ ohos_moduletest("ans_module_test") { "//utils/system/safwk/native/include", "${services_path}/ans/include", "${services_path}/test/moduletest/mock/include", + "//base/account/os_account/interfaces/innerkits/osaccount/native/include", + "//base/account/os_account/frameworks/common/database/include/", + "//base/account/os_account/frameworks/common/account_error/include", + "//base/account/os_account/frameworks/common/log/include", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/autils", "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include", "//foundation/distributeddatamgr/distributeddatamgr/services/distributeddataservice/adapter/include/log", @@ -69,6 +73,7 @@ ohos_moduletest("ans_module_test") { "${core_path}:ans_core", "${frameworks_path}/ans/native:ans_innerkits", "${frameworks_path}/wantagent:wantagent_innerkits", + "//base/account/os_account/frameworks/osaccount/native:os_account_innerkits", "//base/miscservices/time/services:time_service", "//foundation/aafwk/standard/services/abilitymgr:abilityms", "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base:appexecfwk_base",