diff --git a/services/ans/include/advanced_datashare_helper.h b/services/ans/include/advanced_datashare_helper.h index 7490c0ef1a90a9394929e74f0dbd6d413fb70c13..85addcf07f0f57c9121a0423fe123963794096c0 100644 --- a/services/ans/include/advanced_datashare_helper.h +++ b/services/ans/include/advanced_datashare_helper.h @@ -37,22 +37,27 @@ public: ~AdvancedDatashareHelper() = default; bool Query(Uri &uri, const std::string &key, std::string &value); bool isRepeatCall(const std::string &phoneNumber); - ErrCode QueryContact(Uri &uri, const std::string &phoneNumber, const std::string &policy); + ErrCode QueryContact(Uri &uri, const std::string &phoneNumber, + const std::string &policy, const std::string &profileId); std::string GetFocusModeEnableUri(const int32_t &userId) const; std::string GetFocusModeProfileUri(const int32_t &userId) const; std::string GetFocusModeCallPolicyUri(const int32_t &userId) const; std::string GetFocusModeRepeatCallUri(const int32_t &userId) const; + std::string GetIntelligentUri(const int32_t &userId) const; std::string GetUnifiedGroupEnableUri() const; private: enum ContactPolicy { ALLOW_FAVORITE_CONTACTS = 4, ALLOW_SPECIFIED_CONTACTS = 5, + FORBID_SPECIFIED_CONTACTS = 6, }; std::shared_ptr CreateDataShareHelper(); std::shared_ptr CreateContactDataShareHelper(std::string uri); - bool dealWithContactResult(std::shared_ptr helper, - std::shared_ptr resultSet, const std::string &policy); + std::shared_ptr CreateIntelligentDataShareHelper(std::string uri); + std::shared_ptr GetContactResultSet(Uri &uri, const std::string &phoneNumber, + const std::string &policy, const std::string &profileId); + bool dealWithContactResult(std::shared_ptr resultSet, const std::string &policy); }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index d025f4af93a61a25c4b4f9fb2b1351069b77f79e..56e10eb1c22dd7fa1ed41b111aa563b49d89dd3f 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1224,6 +1224,7 @@ private: ALLOW_EXISTING_CONTACTS = 3, ALLOW_FAVORITE_CONTACTS = 4, ALLOW_SPECIFIED_CONTACTS = 5, + FORBID_SPECIFIED_CONTACTS = 6, }; AdvancedNotificationService(); diff --git a/services/ans/src/advanced_datashare_helper.cpp b/services/ans/src/advanced_datashare_helper.cpp index 516b55b5f8e4a8498a3b82e46e41aadc1e7918b1..0db5e08e9f68affdd2604fbb21e72deb1f07d390 100644 --- a/services/ans/src/advanced_datashare_helper.cpp +++ b/services/ans/src/advanced_datashare_helper.cpp @@ -15,11 +15,13 @@ #include "advanced_datashare_helper.h" +#include "ans_const_define.h" #include "ans_log_wrapper.h" #include "if_system_ability_manager.h" #include "iservice_registry.h" #include "message_parcel.h" #include "os_account_manager.h" +#include "os_account_manager_helper.h" #include "singleton.h" #include "system_ability_definition.h" #include "ipc_skeleton.h" @@ -42,6 +44,10 @@ constexpr const char *FOCUS_MODE_REPEAT_CALLERS_ENABLE_URI = "?Proxy=true&key=fo constexpr const char *UNIFIED_GROUP_ENABLE_URI = "?Proxy=true&key=unified_group_enable"; constexpr const char *CONTACT_URI = "datashare:///com.ohos.contactsdataability"; constexpr const char *CALLLOG_URI = "datashare:///com.ohos.calllogability"; +constexpr const char *INTELLIGENT_SCENE_DATA = + "datashare:///com.huawei.hmos.intelligentscene/phone/IntelligentScene/CONTACT_DATA?Proxy=true&user="; +constexpr const char *INTELLIGENT_URI = "datashareproxy://com.huawei.hmos.intelligentscene?user="; +constexpr const char *INTELLIGENT_EXTENTION_URI = "datashare://com.huawei.hmos.intelligentscene.DataAbility"; constexpr const char *CALL_SUBSECTION = "datashare:///com.ohos.calllogability/calls/calllog?Proxy=true"; constexpr const char *PHONE_NUMBER = "phone_number"; constexpr const char *IS_DELETED = "is_deleted"; @@ -50,16 +56,20 @@ constexpr const char *DETAIL_INFO = "detail_info"; constexpr const char *FORMAT_PHONE_NUMBER = "format_phone_number"; constexpr const char *FAVORITE = "favorite"; constexpr const char *FOCUS_MODE_LIST = "focus_mode_list"; +constexpr const char *MODE_ID = "modeId"; constexpr const char *ADVANCED_DATA_COLUMN_KEYWORD = "KEYWORD"; constexpr const char *ADVANCED_DATA_COLUMN_VALUE = "VALUE"; constexpr const char *CALL_DIRECTION = "call_direction"; constexpr const char *CREATE_TIME = "create_time"; +constexpr const char *WHITE_LIST = "1"; +constexpr const char *BLACK_LIST = "2"; constexpr const unsigned int PHONE_NUMBER_LENGTH = 7; constexpr const unsigned int MAX_TIME_INTERVAL = 15 * 60; constexpr const int TYPE_ID_FIVE = 5; constexpr const int ERROR_QUERY_INFO_FAILED = -1; constexpr const int QUERY_INFO_SUCCESS = 1; std::vector QUERY_CONTACT_COLUMN_LIST = {FORMAT_PHONE_NUMBER, FAVORITE, FOCUS_MODE_LIST, DETAIL_INFO}; +std::vector QUERY_INTELLIGENT_COLUMN_LIST = {FORMAT_PHONE_NUMBER, FOCUS_MODE_LIST, DETAIL_INFO}; } // namespace AdvancedDatashareHelper::AdvancedDatashareHelper() { @@ -96,6 +106,32 @@ std::shared_ptr AdvancedDatashareHelper::CreateConta return DataShare::DataShareHelper::Creator(remoteObj, uri); } +std::shared_ptr AdvancedDatashareHelper::CreateIntelligentDataShareHelper(std::string uri) +{ + sptr saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saManager == nullptr) { + ANS_LOGE("The sa manager is nullptr."); + return nullptr; + } + sptr remoteObj = saManager->GetSystemAbility(ADVANCED_NOTIFICATION_SERVICE_ABILITY_ID); + if (remoteObj == nullptr) { + ANS_LOGE("The remoteObj is nullptr."); + return nullptr; + } + int32_t userId = SUBSCRIBE_USER_INIT; + if (OsAccountManagerHelper::GetInstance()->GetCurrentActiveUserId(userId) != ERR_OK) { + ANS_LOGE("Get current active user id failed."); + return nullptr; + } + uri = uri + std::to_string(userId); + auto [error, helper] = DataShare::DataShareHelper::Create(remoteObj, uri, INTELLIGENT_EXTENTION_URI); + if (error != DataShare::E_OK) { + ANS_LOGE("Create intelligent data share helper failed."); + return nullptr; + } + return helper; +} + bool AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, std::string &value) { std::shared_ptr dataShareHelper = CreateDataShareHelper(); @@ -127,35 +163,24 @@ bool AdvancedDatashareHelper::Query(Uri &uri, const std::string &key, std::strin return true; } -ErrCode AdvancedDatashareHelper::QueryContact(Uri &uri, const std::string &phoneNumber, const std::string &policy) +ErrCode AdvancedDatashareHelper::QueryContact(Uri &uri, const std::string &phoneNumber, const std::string &policy, + const std::string &profileId) { std::string identity = IPCSkeleton::ResetCallingIdentity(); - std::shared_ptr helper = CreateContactDataShareHelper(CONTACT_URI); - if (helper == nullptr) { - ANS_LOGE("The data share helper is nullptr."); - return ERROR_QUERY_INFO_FAILED; - } - DataShare::DataSharePredicates predicates; - predicates.EqualTo(IS_DELETED, 0); - predicates.EqualTo(TYPE_ID, TYPE_ID_FIVE); - if (phoneNumber.size() >= PHONE_NUMBER_LENGTH) { - predicates.EndsWith(DETAIL_INFO, - phoneNumber.substr(phoneNumber.size() - PHONE_NUMBER_LENGTH, phoneNumber.size())); - } else { - predicates.EqualTo(DETAIL_INFO, phoneNumber); - } - auto resultSet = helper->Query(uri, predicates, QUERY_CONTACT_COLUMN_LIST); - IPCSkeleton::SetCallingIdentity(identity); + auto resultSet = GetContactResultSet(uri, phoneNumber, policy, profileId); if (resultSet == nullptr) { ANS_LOGE("Query error, resultSet is null."); - helper->Release(); return ERROR_QUERY_INFO_FAILED; } + IPCSkeleton::SetCallingIdentity(identity); int isFound = 0; int rowCount = 0; resultSet->GetRowCount(rowCount); if (rowCount <= 0) { ANS_LOGI("Query success, but rowCount is 0."); + if (atoi(policy.c_str() == ContactPolicy::FORBID_SPECIFIED_CONTACTS)) { + isFound = 1; + } } else { int resultId = -1; #ifdef ENABLE_ANS_TELEPHONY_CUST_WRAPPER @@ -164,7 +189,7 @@ ErrCode AdvancedDatashareHelper::QueryContact(Uri &uri, const std::string &phone #endif if ((phoneNumber.size() >= PHONE_NUMBER_LENGTH && resultSet->GoToRow(resultId) == DataShare::E_OK) || (phoneNumber.size() < PHONE_NUMBER_LENGTH && resultSet->GoToFirstRow() == DataShare::E_OK)) { - isFound = dealWithContactResult(helper, resultSet, policy) ? QUERY_INFO_SUCCESS : ERR_OK; + isFound = dealWithContactResult(resultSet, policy) ? QUERY_INFO_SUCCESS : ERR_OK; } } resultSet->Close(); @@ -172,8 +197,53 @@ ErrCode AdvancedDatashareHelper::QueryContact(Uri &uri, const std::string &phone return isFound; } -bool AdvancedDatashareHelper::dealWithContactResult(std::shared_ptr helper, - std::shared_ptr resultSet, const std::string &policy) +std::shared_ptr AdvancedDatashareHelper::GetContactResultSet(Uri &uri, + const std::string &phoneNumber, const std::string &policy, const std::string &profileId) +{ + std::shared_ptr helper; + std::shared_ptr resultSet; + if (atoi(policy.c_str()) == ContactPolicy::ALLOW_SPECIFIED_CONTACTS || + atoi(policy.c_str()) == ContactPolicy::FORBID_SPECIFIED_CONTACTS) { + helper = CreateContactDataShareHelper(INTELLIGENT_URI); + if (helper == nullptr) { + ANS_LOGE("The data share helper is nullptr."); + return nullptr; + } + std::string focusModeList = atoi(policy.c_str()) == ContactPolicy::ALLOW_SPECIFIED_CONTACTS ? WHITE_LIST : BLACK_LIST; + ANS_LOGI("GetContactResultSet: profileId: %{public}s, focusModeList: %{public}s", profileId.c_str(), focusModeList.c_str()); + DataShare::DataSharePredicates predicates; + predicates.EqualTo(MODE_ID, profileId); + predicates.EqualTo(FOCUS_MODE_LIST, focusModeList); + if (phoneNumber.size() >= PHONE_NUMBER_LENGTH) { + predicates.EndsWith(DETAIL_INFO, + phoneNumber.substr(phoneNumber.size() - PHONE_NUMBER_LENGTH, phoneNumber.size())); + } else { + predicates.EqualTo(DETAIL_INFO, phoneNumber); + } + resultSet = helper->Query(uri, predicates, QUERY_INTELLIGENT_COLUMN_LIST); + } else { + helper = CreateContactDataShareHelper(CONTACT_URI); + if (helper == nullptr) { + ANS_LOGE("The data share helper is nullptr."); + return nullptr; + } + DataShare::DataSharePredicates predicates; + predicates.EqualTo(IS_DELETED, 0); + predicates.EqualTo(TYPE_ID, TYPE_ID_FIVE); + if (phoneNumber.size() >= PHONE_NUMBER_LENGTH) { + predicates.EndsWith(DETAIL_INFO, + phoneNumber.substr(phoneNumber.size() - PHONE_NUMBER_LENGTH, phoneNumber.size())); + } else { + predicates.EqualTo(DETAIL_INFO, phoneNumber); + } + resultSet = helper->Query(uri, predicates, QUERY_CONTACT_COLUMN_LIST); + } + helper->Release(); + return resultSet; +} + +bool AdvancedDatashareHelper::dealWithContactResult(std::shared_ptr resultSet, + const std::string &policy) { bool isNoNeedSilent = false; int32_t columnIndex; @@ -192,19 +262,27 @@ bool AdvancedDatashareHelper::dealWithContactResult(std::shared_ptrGetColumnIndex(FOCUS_MODE_LIST, columnIndex); - resultSet->GetString(columnIndex, focus_mode_list); - if (focus_mode_list.empty() || focus_mode_list.c_str()[0] == '0') { - isNoNeedSilent = false; - } - if (focus_mode_list.c_str()[0] == '1') { - isNoNeedSilent = true; - break; - } - } while (resultSet->GoToNextRow() == DataShare::E_OK); - ANS_LOGI("dealWithContactResult: focus_mode_list = %{public}s", focus_mode_list.c_str()); - break; + case ContactPolicy::FORBID_SPECIFIED_CONTACTS: + { + do { + resultSet->GetColumnIndex(FOCUS_MODE_LIST, columnIndex); + resultSet->GetString(columnIndex, focus_mode_list); + if (focus_mode_list.empty() || focus_mode_list.c_str()[0] == '0') { + isNoNeedSilent = false; + } + if (focus_mode_list.c_str()[0] == '1') { + isNoNeedSilent = true; + break; + } + if (focus_mode_list.c_str()[0] == '2') { + isNoNeedSilent = false; + break; + } + } while (resultSet->GoToNextRow() == DataShare::E_OK); + ANS_LOGI("dealWithContactResult: focus_mode_list = %{public}s", focus_mode_list.c_str()); + break; + } + default: isNoNeedSilent = true; break; @@ -271,5 +349,10 @@ std::string AdvancedDatashareHelper::GetFocusModeRepeatCallUri(const int32_t &us { return USER_SETTINGS_DATA_URI + std::to_string(userId) + FOCUS_MODE_REPEAT_CALLERS_ENABLE_URI; } + +std::string AdvancedDatashareHelper::GetIntelligentUri(const int32_t &userId) const +{ + return INTELLIGENT_SCENE_DATA + std::to_string(userId); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index 9cc3be49ee3d15ff1944aacbbf937a137e5c821c..fae97ef2c73dd198dbc8b5c0e850d7b0f2268d6a 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -570,7 +570,7 @@ ErrCode AdvancedNotificationService::CancelAsBundle( return CancelAsBundle(bundleOption, notificationId, userId); } -ErrCode AdvancedNotificationService::CancelAsBundle( +ErrCode AdvancedNotificationService::CancelAsBundle( int32_t notificationId, const std::string &representativeBundle, int32_t userId) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -2011,6 +2011,12 @@ ErrCode AdvancedNotificationService::CheckNeedSilent( return 1; } } + std::string profileId; + Uri profileIdUri(datashareHelper->GetFocusModeProfileUri(userId)); + bool profileId_ret = datashareHelper->Query(profileIdUri, KEY_FOCUS_MODE_PROFILE_ID, profileId); + if (!profileId_ret) { + ANS_LOGE("Query focus mode profile id fail."); + } switch (atoi(policy.c_str())) { case ContactPolicy::FORBID_EVERYONE: break; @@ -2019,9 +2025,17 @@ ErrCode AdvancedNotificationService::CheckNeedSilent( break; case ContactPolicy::ALLOW_EXISTING_CONTACTS: case ContactPolicy::ALLOW_FAVORITE_CONTACTS: + { + Uri uri(CONTACT_DATA); + isNeedSilent = datashareHelper->QueryContact(uri, phoneNumber, policy, profileId); + break; + } case ContactPolicy::ALLOW_SPECIFIED_CONTACTS: - Uri uri(CONTACT_DATA); - isNeedSilent = datashareHelper->QueryContact(uri, phoneNumber, policy); + case ContactPolicy::FORBID_SPECIFIED_CONTACTS: + { + Uri intelligentUri(datashareHelper->GetIntelligentUri(userId)); + isNeedSilent = datashareHelper->QueryContact(intelligentUri, phoneNumber, policy, profileId); + } break; } ANS_LOGI("IsNeedSilentInDoNotDisturbMode: %{public}d", isNeedSilent);