1b1b8bc3fSopenharmony_ci/* 2b1b8bc3fSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License. 5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at 6b1b8bc3fSopenharmony_ci * 7b1b8bc3fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8b1b8bc3fSopenharmony_ci * 9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and 13b1b8bc3fSopenharmony_ci * limitations under the License. 14b1b8bc3fSopenharmony_ci */ 15b1b8bc3fSopenharmony_ci 16b1b8bc3fSopenharmony_ci#include "net_datashare_utils.h" 17b1b8bc3fSopenharmony_ci 18b1b8bc3fSopenharmony_ci#include <vector> 19b1b8bc3fSopenharmony_ci 20b1b8bc3fSopenharmony_ci#include "net_manager_constants.h" 21b1b8bc3fSopenharmony_ci#include "net_mgr_log_wrapper.h" 22b1b8bc3fSopenharmony_ci 23b1b8bc3fSopenharmony_cinamespace OHOS { 24b1b8bc3fSopenharmony_cinamespace NetManagerStandard { 25b1b8bc3fSopenharmony_cinamespace { 26b1b8bc3fSopenharmony_ciconstexpr const char *SETTINGS_DATASHARE_URI = 27b1b8bc3fSopenharmony_ci "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true"; 28b1b8bc3fSopenharmony_ciconstexpr const char *SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility"; 29b1b8bc3fSopenharmony_ciconstexpr const char *SETTINGS_DATA_COLUMN_KEYWORD = "KEYWORD"; 30b1b8bc3fSopenharmony_ciconstexpr const char *SETTINGS_DATA_COLUMN_VALUE = "VALUE"; 31b1b8bc3fSopenharmony_ci 32b1b8bc3fSopenharmony_ciconstexpr int INVALID_VALUE = -1; 33b1b8bc3fSopenharmony_ci} // namespace 34b1b8bc3fSopenharmony_ci 35b1b8bc3fSopenharmony_ciNetDataShareHelperUtils::NetDataShareHelperUtils() {} 36b1b8bc3fSopenharmony_ci 37b1b8bc3fSopenharmony_cistd::shared_ptr<DataShare::DataShareHelper> NetDataShareHelperUtils::CreateDataShareHelper() 38b1b8bc3fSopenharmony_ci{ 39b1b8bc3fSopenharmony_ci sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 40b1b8bc3fSopenharmony_ci if (saManager == nullptr) { 41b1b8bc3fSopenharmony_ci NETMGR_LOG_E("NetDataShareHelperUtils GetSystemAbilityManager failed."); 42b1b8bc3fSopenharmony_ci return nullptr; 43b1b8bc3fSopenharmony_ci } 44b1b8bc3fSopenharmony_ci sptr<IRemoteObject> remoteObj = saManager->GetSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID); 45b1b8bc3fSopenharmony_ci if (remoteObj == nullptr) { 46b1b8bc3fSopenharmony_ci NETMGR_LOG_E("NetDataShareHelperUtils GetSystemAbility Service Failed."); 47b1b8bc3fSopenharmony_ci return nullptr; 48b1b8bc3fSopenharmony_ci } 49b1b8bc3fSopenharmony_ci return DataShare::DataShareHelper::Creator(remoteObj, SETTINGS_DATASHARE_URI, SETTINGS_DATA_EXT_URI); 50b1b8bc3fSopenharmony_ci} 51b1b8bc3fSopenharmony_ci 52b1b8bc3fSopenharmony_ciint32_t NetDataShareHelperUtils::Query(Uri &uri, const std::string &key, std::string &value) 53b1b8bc3fSopenharmony_ci{ 54b1b8bc3fSopenharmony_ci std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataShareHelper(); 55b1b8bc3fSopenharmony_ci if (dataShareHelper == nullptr) { 56b1b8bc3fSopenharmony_ci NETMGR_LOG_E("dataShareHelper is nullptr"); 57b1b8bc3fSopenharmony_ci return NETMANAGER_ERROR; 58b1b8bc3fSopenharmony_ci } 59b1b8bc3fSopenharmony_ci DataShare::DataSharePredicates predicates; 60b1b8bc3fSopenharmony_ci std::vector<std::string> columns; 61b1b8bc3fSopenharmony_ci predicates.EqualTo(SETTINGS_DATA_COLUMN_KEYWORD, key); 62b1b8bc3fSopenharmony_ci auto result = dataShareHelper->Query(uri, predicates, columns); 63b1b8bc3fSopenharmony_ci if (result == nullptr) { 64b1b8bc3fSopenharmony_ci NETMGR_LOG_E("DataShareHelper query error, result is null"); 65b1b8bc3fSopenharmony_ci dataShareHelper->Release(); 66b1b8bc3fSopenharmony_ci return NETMANAGER_ERROR; 67b1b8bc3fSopenharmony_ci } 68b1b8bc3fSopenharmony_ci 69b1b8bc3fSopenharmony_ci if (result->GoToFirstRow() != DataShare::E_OK) { 70b1b8bc3fSopenharmony_ci result->Close(); 71b1b8bc3fSopenharmony_ci dataShareHelper->Release(); 72b1b8bc3fSopenharmony_ci return NETMANAGER_ERROR; 73b1b8bc3fSopenharmony_ci } 74b1b8bc3fSopenharmony_ci 75b1b8bc3fSopenharmony_ci int columnIndex; 76b1b8bc3fSopenharmony_ci result->GetColumnIndex(SETTINGS_DATA_COLUMN_VALUE, columnIndex); 77b1b8bc3fSopenharmony_ci result->GetString(columnIndex, value); 78b1b8bc3fSopenharmony_ci result->Close(); 79b1b8bc3fSopenharmony_ci dataShareHelper->Release(); 80b1b8bc3fSopenharmony_ci NETMGR_LOG_I("DataShareHelper query success,value[%{public}s]", value.c_str()); 81b1b8bc3fSopenharmony_ci return NETMANAGER_SUCCESS; 82b1b8bc3fSopenharmony_ci} 83b1b8bc3fSopenharmony_ci 84b1b8bc3fSopenharmony_ciint32_t NetDataShareHelperUtils::Insert(Uri &uri, const std::string &key, const std::string &value) 85b1b8bc3fSopenharmony_ci{ 86b1b8bc3fSopenharmony_ci std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataShareHelper(); 87b1b8bc3fSopenharmony_ci if (dataShareHelper == nullptr) { 88b1b8bc3fSopenharmony_ci NETMGR_LOG_E("dataShareHelper is nullptr"); 89b1b8bc3fSopenharmony_ci return NETMANAGER_ERROR; 90b1b8bc3fSopenharmony_ci } 91b1b8bc3fSopenharmony_ci DataShare::DataShareValuesBucket valuesBucket; 92b1b8bc3fSopenharmony_ci DataShare::DataShareValueObject keyObj(key); 93b1b8bc3fSopenharmony_ci DataShare::DataShareValueObject valueObj(value); 94b1b8bc3fSopenharmony_ci valuesBucket.Put(SETTINGS_DATA_COLUMN_KEYWORD, keyObj); 95b1b8bc3fSopenharmony_ci valuesBucket.Put(SETTINGS_DATA_COLUMN_VALUE, valueObj); 96b1b8bc3fSopenharmony_ci int32_t result = dataShareHelper->Insert(uri, valuesBucket); 97b1b8bc3fSopenharmony_ci if (result == INVALID_VALUE) { 98b1b8bc3fSopenharmony_ci NETMGR_LOG_E("DataShareHelper insert failed, insert result:%{public}d", result); 99b1b8bc3fSopenharmony_ci dataShareHelper->Release(); 100b1b8bc3fSopenharmony_ci return NETMANAGER_ERROR; 101b1b8bc3fSopenharmony_ci } 102b1b8bc3fSopenharmony_ci dataShareHelper->NotifyChange(uri); 103b1b8bc3fSopenharmony_ci dataShareHelper->Release(); 104b1b8bc3fSopenharmony_ci NETMGR_LOG_I("DataShareHelper insert success"); 105b1b8bc3fSopenharmony_ci return NETMANAGER_SUCCESS; 106b1b8bc3fSopenharmony_ci} 107b1b8bc3fSopenharmony_ci 108b1b8bc3fSopenharmony_ciint32_t NetDataShareHelperUtils::Update(Uri &uri, const std::string &key, const std::string &value) 109b1b8bc3fSopenharmony_ci{ 110b1b8bc3fSopenharmony_ci std::shared_ptr<DataShare::DataShareHelper> dataShareHelper = CreateDataShareHelper(); 111b1b8bc3fSopenharmony_ci if (dataShareHelper == nullptr) { 112b1b8bc3fSopenharmony_ci NETMGR_LOG_E("dataShareHelper is nullptr"); 113b1b8bc3fSopenharmony_ci return NETMANAGER_ERROR; 114b1b8bc3fSopenharmony_ci } 115b1b8bc3fSopenharmony_ci std::string queryValue; 116b1b8bc3fSopenharmony_ci int32_t ret = Query(uri, key, queryValue); 117b1b8bc3fSopenharmony_ci if (ret == NETMANAGER_ERROR) { 118b1b8bc3fSopenharmony_ci dataShareHelper->Release(); 119b1b8bc3fSopenharmony_ci return Insert(uri, key, value); 120b1b8bc3fSopenharmony_ci } 121b1b8bc3fSopenharmony_ci 122b1b8bc3fSopenharmony_ci DataShare::DataShareValuesBucket valuesBucket; 123b1b8bc3fSopenharmony_ci DataShare::DataShareValueObject valueObj(value); 124b1b8bc3fSopenharmony_ci valuesBucket.Put(SETTINGS_DATA_COLUMN_VALUE, valueObj); 125b1b8bc3fSopenharmony_ci DataShare::DataSharePredicates predicates; 126b1b8bc3fSopenharmony_ci predicates.EqualTo(SETTINGS_DATA_COLUMN_KEYWORD, key); 127b1b8bc3fSopenharmony_ci int32_t result = dataShareHelper->Update(uri, predicates, valuesBucket); 128b1b8bc3fSopenharmony_ci if (result == INVALID_VALUE) { 129b1b8bc3fSopenharmony_ci dataShareHelper->Release(); 130b1b8bc3fSopenharmony_ci return NETMANAGER_ERROR; 131b1b8bc3fSopenharmony_ci } 132b1b8bc3fSopenharmony_ci dataShareHelper->NotifyChange(uri); 133b1b8bc3fSopenharmony_ci dataShareHelper->Release(); 134b1b8bc3fSopenharmony_ci NETMGR_LOG_I("DataShareHelper update success"); 135b1b8bc3fSopenharmony_ci return NETMANAGER_SUCCESS; 136b1b8bc3fSopenharmony_ci} 137b1b8bc3fSopenharmony_ci} // namespace NetManagerStandard 138b1b8bc3fSopenharmony_ci} // namespace OHOS 139