diff --git a/frameworks/ans/IAnsManager.idl b/frameworks/ans/IAnsManager.idl index 0b3437b65236156b0af3110af0671e1276a019b2..d7ec4e81fee23918075aa3e5ceb071a460e363f7 100644 --- a/frameworks/ans/IAnsManager.idl +++ b/frameworks/ans/IAnsManager.idl @@ -263,6 +263,16 @@ interface OHOS.Notification.IAnsManager { void SetDistributedEnabledByBundle( [in] sptr bundleOption, [in] String deviceType, [in] boolean enabled); + void SetDistributedEnabled([in] String deviceType, [in] boolean enabled); + + void IsDistributedEnabled([in] String deviceType, [out] boolean enabled); + + void GetDistributedAbility([out] int abilityId); + + void GetDistributedAuthStatus([in] String deviceType, [in] String deviceId, [in] int userId, [out] boolean isAuth); + + void SetDistributedAuthStatus([in] String deviceType, [in] String deviceId, [in] int userId, [in] boolean isAuth); + void IsSmartReminderEnabled([in] String deviceType, [out] boolean enabled); void SetSmartReminderEnabled([in] String deviceType, [in] boolean enabled); diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index b687d449a018a6ec4ddc2d7ddd091a4d24dbfaf1..3bd88a0cec42230cc5dc4868929c1f84c182bafd 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -590,6 +590,35 @@ ErrCode NotificationHelper::IsDistributedEnabledByBundle(const NotificationBundl deviceType, enabled); } +ErrCode NotificationHelper::SetDistributedEnabled(const std::string &deviceType, const bool &enabled) +{ + return DelayedSingleton::GetInstance()->SetDistributedEnabled(deviceType, enabled); +} + +ErrCode NotificationHelper::IsDistributedEnabled(const std::string &deviceType, bool &enabled) +{ + return DelayedSingleton::GetInstance()->IsDistributedEnabled(deviceType, enabled); +} + +ErrCode NotificationHelper::GetDistributedAbility(int32_t &abilityId) +{ + return DelayedSingleton::GetInstance()->GetDistributedAbility(abilityId); +} + +ErrCode NotificationHelper::GetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth) +{ + return DelayedSingleton::GetInstance()->GetDistributedAuthStatus( + deviceType, deviceId, userId, isAuth); +} + +ErrCode NotificationHelper::SetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth) +{ + return DelayedSingleton::GetInstance()->SetDistributedAuthStatus( + deviceType, deviceId, userId, isAuth); +} + ErrCode NotificationHelper::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled) { return DelayedSingleton::GetInstance()->SetSmartReminderEnabled(deviceType, enabled); diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index 2f6a79e9faefacfa38c67181378c22eccd2964ae..18e13d5fedf34506a5a95eba6ed8a82969c29a0a 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -1117,6 +1117,56 @@ public: ErrCode IsDistributedEnabledByBundle( const NotificationBundleOption &bundleOption, const std::string &deviceType, bool &enabled); + /** + * @brief Configuring Whether to Synchronize Common Notifications to Target Devices. + * + * @param deviceType Target device type. + * @param enabled Whether to Synchronize Common Notifications to Target Devices. + * @return Returns configuring Whether to Synchronize Common Notifications to Target Devices result. + */ + ErrCode SetDistributedEnabled(const std::string &deviceType, const bool &enabled); + + /** + * @brief Querying Whether to Synchronize Common Devices to Target Devices. + * + * @param deviceType Target device type. + * @param enabled Whether to Synchronize Common Notifications to Target Devices. + * @return Returns Whether to Synchronize Common Notifications to Target Devices result. + */ + ErrCode IsDistributedEnabled(const std::string &deviceType, bool &enabled); + + /** + * @brief Obtains the set of supported distributed abilities. + * + * @param abilityId The set of supported distributed abilities. + * @return Returns result in Obtains the set of supported distributed abilities. + */ + ErrCode GetDistributedAbility(int32_t &abilityId); + + /** + * @brief Get the target device's authorization status. + * + * @param deviceType Type of the target device whose status you want to set. + * @param deviceId The id of the target device. + * @param userId The userid of the target device. + * @param isAuth Return The authorization status. + * @return Returns get result. + */ + ErrCode GetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth); + + /** + * @brief Set the target device's authorization status. + * + * @param deviceType Type of the target device whose status you want to set. + * @param deviceId The id of the target device. + * @param userId The userid of the target device. + * @param isAuth The authorization status. + * @return Returns set result. + */ + ErrCode SetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth); + /** * @brief Get Enable smartphone to collaborate with other devices for intelligent reminders * diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 9c03b1277496b02015c669984f3a052e7d4651cc..6bd054f9c1026c60affca7704cc3e9380dc9cd4a 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -2013,6 +2013,88 @@ ErrCode AnsNotification::SetDistributedEnabledByBundle(const NotificationBundleO return proxy->SetDistributedEnabledByBundle(bo, deviceType, enabled); } +ErrCode AnsNotification::SetDistributedEnabled(const std::string &deviceType, const bool &enabled) +{ + ANS_LOGD("enter"); + if (deviceType.empty()) { + ANS_LOGE("Invalid deviceType."); + return ERR_ANS_INVALID_PARAM; + } + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("UnregisterPushCallback fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return proxy->SetDistributedEnabled(deviceType, enabled); +} + +ErrCode AnsNotification::IsDistributedEnabled(const std::string &deviceType, bool &enabled) +{ + ANS_LOGD("enter"); + if (deviceType.empty()) { + ANS_LOGE("Invalid deviceType."); + return ERR_ANS_INVALID_PARAM; + } + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("UnregisterPushCallback fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return proxy->IsDistributedEnabled(deviceType, enabled); +} + +ErrCode AnsNotification::GetDistributedAbility(int32_t &abilityId) +{ + ANS_LOGD("enter"); + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("UnregisterPushCallback fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return proxy->GetDistributedAbility(abilityId); +} + +ErrCode AnsNotification::GetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth) +{ + ANS_LOGD("enter"); + if (deviceType.empty() || deviceId.empty()) { + ANS_LOGE("Invalid deviceType or deviceId."); + return ERR_ANS_INVALID_PARAM; + } + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("UnregisterPushCallback fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return proxy->GetDistributedAuthStatus(deviceType, deviceId, userId, isAuth); +} + +ErrCode AnsNotification::SetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth) +{ + ANS_LOGD("enter"); + if (deviceType.empty() || deviceId.empty()) { + ANS_LOGE("Invalid deviceType or deviceId."); + return ERR_ANS_INVALID_PARAM; + } + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("UnregisterPushCallback fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return proxy->SetDistributedAuthStatus(deviceType, deviceId, userId, isAuth); +} + ErrCode AnsNotification::IsDistributedEnabledByBundle(const NotificationBundleOption &bundleOption, const std::string &deviceType, bool &enabled) { diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index 4c435cf26543c10429092b72491cfca5b8fb7eed..540706e7c12a0a6d085a6811bd6acb485420881b 100644 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -421,6 +421,33 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode SetDistributedEnabled(const std::string &deviceType, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsDistributedEnabled(const std::string &deviceType, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetDistributedAbility(int32_t &abilityId) + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth) + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth) + { + return ERR_ANS_INVALID_PARAM; + } + ErrCode GetDeviceRemindType(int32_t& remindTypeInt) override { return ERR_ANS_INVALID_PARAM; diff --git a/interfaces/inner_api/notification_constant.h b/interfaces/inner_api/notification_constant.h index e338bab563109cc7d48d8d5410f9815d5adf70de..914bc78cfa68bb8d2fc7b1769afb1e3e35a71fb4 100644 --- a/interfaces/inner_api/notification_constant.h +++ b/interfaces/inner_api/notification_constant.h @@ -145,6 +145,35 @@ public: CLOSE }; + enum class ENABLE_STATUS { + /** + * the device type notification switch never been set. + */ + ENABLE_NONE, + + /** + * the device type notification switch is enabled + */ + ENABLE_TRUE, + + /** + * the device type notification switch is disable + */ + ENABLE_FALSE + }; + + enum class DANS_SUPPORT_STATUS { + /** + * unsupport the set of distributed abilities. + */ + UNSUPPORT = -1, + + /** + * support the set of distributed abilities. + */ + SUPPORT + }; + static const int32_t DEFAULT_REASON_DELETE = 0; /** diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 3689b9f6f079d906df99f54e8abee1458d9fe817..7696cdda560dbaca6dcc51bc6751ca113f54e416 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -1095,6 +1095,56 @@ public: static ErrCode IsDistributedEnabledByBundle( const NotificationBundleOption &bundleOption, const std::string &deviceType, bool &enabled); + /** + * @brief Configuring Whether to Synchronize Common Notifications to Target Devices. + * + * @param deviceType Target device type. + * @param enabled Whether to Synchronize Common Notifications to Target Devices. + * @return Returns configuring Whether to Synchronize Common Notifications to Target Devices result. + */ + static ErrCode SetDistributedEnabled(const std::string &deviceType, const bool &enabled); + + /** + * @brief Querying Whether to Synchronize Common Devices to Target Devices. + * + * @param deviceType Target device type. + * @param enabled Whether to Synchronize Common Notifications to Target Devices. + * @return Returns Whether to Synchronize Common Notifications to Target Devices result. + */ + static ErrCode IsDistributedEnabled(const std::string &deviceType, bool &enabled); + + /** + * @brief Obtains the set of supported distributed abilities. + * + * @param abilityId The set of supported distributed abilities. + * @return Returns result in Obtains the set of supported distributed abilities. + */ + static ErrCode GetDistributedAbility(int32_t &abilityId); + + /** + * @brief Get the target device's authorization status. + * + * @param deviceType Type of the target device whose status you want to set. + * @param deviceId The id of the target device. + * @param userId The userid of the target device. + * @param isAuth Return The authorization status. + * @return Returns get result. + */ + static ErrCode GetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth); + + /** + * @brief Set the target device's authorization status. + * + * @param deviceType Type of the target device whose status you want to set. + * @param deviceId The id of the target device. + * @param userId The userid of the target device. + * @param isAuth The authorization status. + * @return Returns set result. + */ + static ErrCode SetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth); + /** * @brief Get Enable smartphone to collaborate with other devices for intelligent reminders * diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index d2055b223d43b9d2849c8a6af45e110a36dbc6cd..220e0f2ac9ec6d91f530f7cd86454ee5f92c9fd4 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1048,6 +1048,56 @@ public: ErrCode IsDistributedEnabledByBundle(const sptr &bundleOption, const std::string &deviceType, bool &enabled) override; + /** + * @brief configuring Whether to Synchronize Common Notifications to Target Devices. + * + * @param deviceType Target device type. + * @param enabled Whether to Synchronize Common Notifications to Target Devices. + * @return Returns configuring Whether to Synchronize Common Notifications to Target Devices result. + */ + ErrCode SetDistributedEnabled(const std::string &deviceType, const bool enabled) override; + + /** + * @brief querying Whether to Synchronize Common Devices to Target Devices. + * + * @param deviceType Target device type. + * @param enabled Whether to Synchronize Common Notifications to Target Devices. + * @return Returns Whether to Synchronize Common Notifications to Target Devices result. + */ + ErrCode IsDistributedEnabled(const std::string &deviceType, bool &enabled) override; + + /** + * @brief Obtains the set of supported distributed abilities. + * + * @param abilityId The set of supported distributed abilities. + * @return Returns result in Obtains the set of supported distributed abilities. + */ + ErrCode GetDistributedAbility(int32_t &abilityId) override; + + /** + * @brief Get the target device's authorization status. + * + * @param deviceType Type of the target device whose status you want to set. + * @param deviceId The id of the target device. + * @param userId The userid of the target device. + * @param isAuth Return The authorization status. + * @return Returns get result. + */ + ErrCode GetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth) override; + + /** + * @brief Set the target device's authorization status. + * + * @param deviceType Type of the target device whose status you want to set. + * @param deviceId The id of the target device. + * @param userId The userid of the target device. + * @param isAuth The authorization status. + * @return Returns set result. + */ + ErrCode SetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth) override; + /** * @brief Get Enable smartphone to collaborate with other devices for intelligent reminders * diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index ec1dfc7e1d9b425df74e87621f0ac347a1169a07..29e10cdafb0cc28ac67380da8d350cc68da2726c 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -356,6 +356,50 @@ public: ErrCode IsDistributedEnabledByBundle(const sptr &bundleOption, const std::string &deviceType, bool &enabled); + /** + * @brief Configuring Whether to Synchronize Common Notifications to Target Devices. + * + * @param deviceType Target device type. + * @param enabled Whether to Synchronize Common Notifications to Target Devices. + * @return Returns configuring Whether to Synchronize Common Notifications to Target Devices result. + */ + ErrCode SetDistributedEnabled( + const std::string &deviceType, const NotificationConstant::ENABLE_STATUS &enableStatus); + + /** + * @brief Querying Whether to Synchronize Common Devices to Target Devices. + * + * @param deviceType Target device type. + * @param enabled Whether to Synchronize Common Notifications to Target Devices. + * @return Returns Whether to Synchronize Common Notifications to Target Devices result. + */ + ErrCode IsDistributedEnabled( + const std::string &deviceType, NotificationConstant::ENABLE_STATUS &enableStatus); + + /** + * @brief Get the target device's authorization status. + * + * @param deviceType Type of the target device whose status you want to set. + * @param deviceId The id of the target device. + * @param userId The userid of the target device. + * @param isAuth Return The authorization status. + * @return Returns get result. + */ + ErrCode GetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth); + + /** + * @brief Set the target device's authorization status. + * + * @param deviceType Type of the target device whose status you want to set. + * @param deviceId The id of the target device. + * @param userId The userid of the target device. + * @param isAuth The authorization status. + * @return Returns set result. + */ + ErrCode SetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth); + /** * @brief Set the channel switch for collaborative reminders. The caller must have system permissions to call this method. diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index 427f2ef8beb0a0f960b6f091b65a782fb6ee3cb0..941a6d6c6c6ce1a26ad162ac164003965e023da2 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -110,6 +110,48 @@ public: bool GetDistributedEnabledForBundle(const std::string deviceType, const NotificationPreferencesInfo::BundleInfo &bundleInfo, bool &enabled); + /** + * @brief Put distributed enable notification in the of bundle into disturbe DB. + * + * @param deviceType Indicates device type. + * @param enabled Indicates to whether to enabled + * @return Return true on success, false on failure. + */ + bool PutDistributedEnabled(const std::string &deviceType, const NotificationConstant::ENABLE_STATUS &enabled); + + /** + * @brief get distributed enable notification in the of bundle into disturbe DB. + * + * @param deviceType Indicates device type. + * @param enabled Indicates to whether to enabled + * @return Return true on success, false on failure. + */ + bool GetDistributedEnabled(const std::string &deviceType, NotificationConstant::ENABLE_STATUS &enabled); + + /** + * @brief Get the target device's authorization status. + * + * @param deviceType Type of the target device whose status you want to set. + * @param deviceId The id of the target device. + * @param userId The userid of the target device. + * @param isAuth Return The authorization status. + * @return Returns get result. + */ + ErrCode GetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth); + + /** + * @brief Set the target device's authorization status. + * + * @param deviceType Type of the target device whose status you want to set. + * @param deviceId The id of the target device. + * @param userId The userid of the target device. + * @param isAuth The authorization status. + * @return Returns set result. + */ + ErrCode SetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth); + /** * @brief Put smart reminder enable notification in the of bundle into disturbe DB. * @@ -407,6 +449,8 @@ private: std::string GenerateBundleLablel(const std::string &deviceType, const int32_t userId) const; std::string GenerateBundleLablel(const NotificationConstant::SlotType &slotType, const std::string &deviceType, const int32_t userId) const; + std::string GenerateBundleLablel( + const std::string &deviceType, const std::string &deviceId, const int32_t userId) const; void GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId); void GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId); void GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId); diff --git a/services/ans/include/os_account_manager_helper.h b/services/ans/include/os_account_manager_helper.h index 72d1fc7006a9b7bf87a24072046ca1f8ac1079d3..6635a9b8c3e602d49d090dec5426bcd746413c0f 100644 --- a/services/ans/include/os_account_manager_helper.h +++ b/services/ans/include/os_account_manager_helper.h @@ -86,6 +86,11 @@ public: * @return Returns result. */ ErrCode GetAllActiveOsAccount(std::vector &userIds); + + /** + * Get private status from osAccount. + */ + ErrCode GetOsAccountPrivateStatus(bool &isPrivate); }; } // namespace OHOS } // namespace Notification diff --git a/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp b/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp index f00cebb96a2183c9f9a0f26548bd6712d33759f3..18eb9b8facf010d17e981be22b72742a94b5964f 100644 --- a/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp +++ b/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp @@ -27,6 +27,8 @@ #include "ipc_skeleton.h" #include "notification_constant.h" +#include "os_account_info.h" +#include "os_account_manager.h" #include "os_account_manager_helper.h" #include "hitrace_meter_adapter.h" #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED @@ -42,6 +44,8 @@ namespace OHOS { namespace Notification { +using namespace OHOS::AccountSA; + ErrCode AdvancedNotificationService::IsDistributedEnabled(bool &enabled) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -617,6 +621,80 @@ ErrCode AdvancedNotificationService::IsDistributedEnabledByBundle(const sptrIsDistributedEnabledByBundle(bundle, deviceType, enabled); } +ErrCode AdvancedNotificationService::SetDistributedEnabled(const std::string &deviceType, const bool enabled) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + if (!AccessTokenHelper::IsSystemApp()) { + ANS_LOGD("IsSystemApp is bogus."); + return ERR_ANS_NON_SYSTEM_APP; + } + + return NotificationPreferences::GetInstance()->SetDistributedEnabled(deviceType, + enabled? NotificationConstant::ENABLE_STATUS::ENABLE_TRUE : NotificationConstant::ENABLE_STATUS::ENABLE_FALSE); +} + +ErrCode AdvancedNotificationService::IsDistributedEnabled(const std::string &deviceType, bool &enabled) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + if (!AccessTokenHelper::IsSystemApp()) { + ANS_LOGD("IsSystemApp is bogus."); + return ERR_ANS_NON_SYSTEM_APP; + } + + NotificationConstant::ENABLE_STATUS enableStatus; + ErrCode errResult = NotificationPreferences::GetInstance()->IsDistributedEnabled(deviceType, enableStatus); + enabled = enableStatus == NotificationConstant::ENABLE_STATUS::ENABLE_TRUE; + return errResult; +} + +ErrCode AdvancedNotificationService::GetDistributedAbility(int32_t &abilityId) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGD("IsSystemApp is bogus."); + return ERR_ANS_NON_SYSTEM_APP; + } + + abilityId = static_cast(NotificationConstant::DANS_SUPPORT_STATUS::UNSUPPORT); + bool isPrivate = false; + ErrCode result = OsAccountManagerHelper::GetInstance().GetOsAccountPrivateStatus(isPrivate); + if (result != ERR_OK || isPrivate) { + return result; + } + abilityId = static_cast(NotificationConstant::DANS_SUPPORT_STATUS::SUPPORT); + return result; +} + +ErrCode AdvancedNotificationService::GetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGD("IsSystemApp is bogus."); + return ERR_ANS_NON_SYSTEM_APP; + } + + return NotificationPreferences::GetInstance()->GetDistributedAuthStatus(deviceType, deviceId, userId, isAuth); +} + +ErrCode AdvancedNotificationService::SetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGD("IsSystemApp is bogus."); + return ERR_ANS_NON_SYSTEM_APP; + } + + return NotificationPreferences::GetInstance()->SetDistributedAuthStatus(deviceType, deviceId, userId, isAuth); +} + ErrCode AdvancedNotificationService::GetDeviceRemindType(int32_t& remindTypeInt) { ANS_LOGD("%{public}s", __FUNCTION__); diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 761cd7ba0820ef7d9cb274009ace2ea75b9532bd..13404d66f6ebf42c39ad07bd75a5f2de1cbb5dcb 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -1098,6 +1098,47 @@ ErrCode NotificationPreferences::IsDistributedEnabledByBundle(const sptr lock(preferenceMutex_); + bool storeDBResult = true; + storeDBResult = preferncesDB_->PutDistributedEnabled(deviceType, enableStatus); + return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; +} + +ErrCode NotificationPreferences::IsDistributedEnabled( + const std::string &deviceType, NotificationConstant::ENABLE_STATUS &enableStatus) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + std::lock_guard lock(preferenceMutex_); + bool storeDBResult = true; + enableStatus = NotificationConstant::ENABLE_STATUS::ENABLE_NONE; + storeDBResult = preferncesDB_->GetDistributedEnabled(deviceType, enableStatus); + return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; +} + +ErrCode NotificationPreferences::GetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + std::lock_guard lock(preferenceMutex_); + bool storeDBResult = true; + storeDBResult = preferncesDB_->GetDistributedAuthStatus(deviceType, deviceId, userId, isAuth); + return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; +} + +ErrCode NotificationPreferences::SetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + std::lock_guard lock(preferenceMutex_); + bool storeDBResult = true; + storeDBResult = preferncesDB_->SetDistributedAuthStatus(deviceType, deviceId, userId, isAuth); + return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; +} + ErrCode NotificationPreferences::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled) { ANS_LOGD("%{public}s", __FUNCTION__); diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index c4dbf4dc692d0f0ebeceec62ec1424303377738c..ea04217b4e52879ed998fe3b7c801f8f2510b6cb 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -253,6 +253,16 @@ const static int32_t DISTRIBUTED_KEY_NUM = 4; const static int32_t DISTRIBUTED_KEY_BUNDLE_INDEX = 1; const static int32_t DISTRIBUTED_KEY_UID_INDEX = 2; +/** + * Indicates that distributed notification switch. + */ +static const char* const KEY_DISTRIBUTED_NOTIFICATION_SWITCH = "distributedNotificationSwitch"; + +/** + * Indicates the target device's authorization status. + */ +static const char* const KEY_ENABLE_DISTRIBUTED_AUTH_STATUS = "enabledDistributedAuthStatus"; + NotificationPreferencesDatabase::NotificationPreferencesDatabase() { NotificationRdbConfig notificationRdbConfig; @@ -2067,6 +2077,17 @@ std::string NotificationPreferencesDatabase::GenerateBundleLablel( bundleInfo.GetBundleUid())).append(KEY_MIDDLE_LINE).append(deviceType)); } +std::string NotificationPreferencesDatabase::GenerateBundleLablel( + const std::string &deviceType, + const std::string &deviceId, + const int32_t userId) const +{ + return std::string(KEY_ENABLE_DISTRIBUTED_AUTH_STATUS).append(KEY_MIDDLE_LINE) + .append(deviceType).append(KEY_MIDDLE_LINE) + .append(deviceId).append(KEY_MIDDLE_LINE) + .append(std::to_string(userId)); +} + template int32_t NotificationPreferencesDatabase::PutDataToDB(const std::string &key, const T &value, const int32_t &userId) { @@ -2079,6 +2100,95 @@ int32_t NotificationPreferencesDatabase::PutDataToDB(const std::string &key, con return result; } +bool NotificationPreferencesDatabase::PutDistributedEnabled( + const std::string &deviceType, const NotificationConstant::ENABLE_STATUS &enabled) +{ + ANS_LOGD("%{public}s, deviceType: %{public}s, enabled: %{public}d", + __FUNCTION__, deviceType.c_str(), static_cast(enabled)); + int32_t userId = SUBSCRIBE_USER_INIT; + OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId); + if (userId == SUBSCRIBE_USER_INIT) { + ANS_LOGE("Current user acquisition failed"); + return false; + } + std::string key = std::string(KEY_DISTRIBUTED_NOTIFICATION_SWITCH).append(KEY_MIDDLE_LINE).append( + deviceType).append(KEY_MIDDLE_LINE).append(std::to_string(userId)); + int32_t result = PutDataToDB(key, static_cast(enabled), userId); + ANS_LOGD("key: %{public}s, result: %{public}d", key.c_str(), result); + return (result == NativeRdb::E_OK); +} + +bool NotificationPreferencesDatabase::GetDistributedEnabled( + const std::string &deviceType, NotificationConstant::ENABLE_STATUS &enabled) +{ + ANS_LOGD("%{public}s, deviceType: %{public}s", __FUNCTION__, deviceType.c_str()); + int32_t userId = SUBSCRIBE_USER_INIT; + OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId); + if (userId == SUBSCRIBE_USER_INIT) { + ANS_LOGE("Current user acquisition failed"); + return false; + } + std::string key = std::string(KEY_DISTRIBUTED_NOTIFICATION_SWITCH).append(KEY_MIDDLE_LINE).append( + deviceType).append(KEY_MIDDLE_LINE).append(std::to_string(userId)); + bool result = false; + enabled = NotificationConstant::ENABLE_STATUS::ENABLE_NONE; + GetValueFromDisturbeDB(key, userId, [&](const int32_t &status, std::string &value) { + switch (status) { + case NativeRdb::E_EMPTY_VALUES_BUCKET: { + result = true; + break; + } + case NativeRdb::E_OK: { + result = true; + enabled = static_cast(StringToInt(value)); + break; + } + default: + result = false; + break; + } + }); + return result; +} + +ErrCode NotificationPreferencesDatabase::GetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth) +{ + ANS_LOGD("%{public}s, deviceType: %{public}s, deviceId: %{public}s, userId: %{public}d", + __FUNCTION__, deviceType.c_str(), deviceId.c_str(), userId); + std::string key = GenerateBundleLablel(deviceType, deviceId, userId); + bool result = false; + isAuth = false; + GetValueFromDisturbeDB(key, userId, [&](const int32_t &status, std::string &value) { + switch (status) { + case NativeRdb::E_EMPTY_VALUES_BUCKET: { + result = true; + break; + } + case NativeRdb::E_OK: { + result = true; + isAuth = StringToInt(value) != 0; + break; + } + default: + result = false; + break; + } + }); + return result; +} + +ErrCode NotificationPreferencesDatabase::SetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth) +{ + ANS_LOGD("%{public}s, deviceType: %{public}s, deviceId: %{public}s, userId: %{public}d", + __FUNCTION__, deviceType.c_str(), deviceId.c_str(), userId); + std::string key = GenerateBundleLablel(deviceType, deviceId, userId); + int32_t result = PutDataToDB(key, static_cast(isAuth), userId); + ANS_LOGD("key: %{public}s, result: %{public}d", key.c_str(), result); + return (result == NativeRdb::E_OK); +} + bool NotificationPreferencesDatabase::GetDistributedEnabledForBundle(const std::string deviceType, const NotificationPreferencesInfo::BundleInfo &bundleInfo, bool &enabled) { diff --git a/services/ans/src/os_account_manager_helper.cpp b/services/ans/src/os_account_manager_helper.cpp index 1355bfec8b98ffdc81db235e1d4af51444868dee..ac4233f50fa89737ff86a0377e295f70de80abd2 100644 --- a/services/ans/src/os_account_manager_helper.cpp +++ b/services/ans/src/os_account_manager_helper.cpp @@ -120,5 +120,24 @@ bool OsAccountManagerHelper::IsSystemAccount(int32_t userId) { return userId >= AccountSA::Constants::START_USER_ID && userId <= AccountSA::Constants::MAX_USER_ID; } + +ErrCode OsAccountManagerHelper::GetOsAccountPrivateStatus(bool &isPrivate) +{ + int32_t userId = 0; + ErrCode querryRes = AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId); + if (querryRes != ERR_OK) { + ANS_LOGE("GetForegroundOsAccountLocalId fail, querryRes: %{public}d", querryRes); + return querryRes; + } + AccountSA::OsAccountType type; + querryRes = AccountSA::OsAccountManager::GetOsAccountType(userId, type); + if (querryRes != ERR_OK) { + ANS_LOGE("GetOsAccountTypef fail, querryRes: %{public}d", querryRes); + return querryRes; + } + ANS_LOGI("GetOsAccountTypef, type: %{public}d", type); + isPrivate = type == AccountSA::OsAccountType::PRIVATE; + return querryRes; +} } } diff --git a/tools/test/mock/mock_ans_manager_stub.h b/tools/test/mock/mock_ans_manager_stub.h index 2b1a7b37bac8716894fd1a24b42e077622671ff2..f886c0e74de48686fceb81155caf6aa66c584e65 100644 --- a/tools/test/mock/mock_ans_manager_stub.h +++ b/tools/test/mock/mock_ans_manager_stub.h @@ -553,6 +553,33 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode SetDistributedEnabled(const std::string &deviceType, bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsDistributedEnabled(const std::string &deviceType, bool &enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetDistributedAbility(int32_t &abilityId) + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool &isAuth) + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode SetDistributedAuthStatus( + const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth) + { + return ERR_ANS_INVALID_PARAM; + } + ErrCode IsSmartReminderEnabled(const std::string& deviceType, bool& enabled) override { return ERR_ANS_INVALID_PARAM;