• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "settings_data_manager.h"
17 
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20 
21 #include "distributed_device_profile_constants.h"
22 #include "distributed_device_profile_errors.h"
23 #include "distributed_device_profile_log.h"
24 
25 namespace OHOS {
26 namespace DistributedDeviceProfile {
27 IMPLEMENT_SINGLE_INSTANCE(SettingsDataManager)
28 namespace {
29 const std::string TAG = "SettingsDataManager";
30 constexpr int32_t USERID_HELPER_NUMBER = 100;
31 const std::string SETTING_URI_PROXY = "datashare:///com.ohos.settingsdata/entry/settingsdata/";
32 const std::string SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
33 const std::string URI_PROXY_SUFFIX = "?Proxy=true";
34 const std::string SETTINGSDATA_GLOBAL = "SETTINGSDATA";
35 const std::string SETTINGS_GENERAL_DEVICE_NAME = "settings.general.device_name";
36 const std::string SETTINGSDATA_SYSTEM = "USER_SETTINGSDATA_";
37 const std::string SETTINGSDATA_SECURE = "USER_SETTINGSDATA_SECURE_";
38 const std::string SETTINGS_GENERAL_DISPLAY_DEVICE_NAME = "settings.general.display_device_name";
39 const std::string SETTINGS_GENERAL_USER_DEFINED_DEVICE_NAME = "settings.general.user_defined_device_name";
40 
41 const std::string SETTING_COLUMN_VALUE = "VALUE";
42 const std::string SETTING_COLUMN_KEYWORD = "KEYWORD";
43 }
44 
Init()45 int32_t SettingsDataManager::Init()
46 {
47     GetRemoteObj();
48     return DP_SUCCESS;
49 }
50 
UnInit()51 int32_t SettingsDataManager::UnInit()
52 {
53     std::lock_guard<std::mutex> lock(remoteObjMtx_);
54     remoteObj_ = nullptr;
55     return DP_SUCCESS;
56 }
57 
GetUserDefinedDeviceName(int32_t userId,std::string & deviceName)58 int32_t SettingsDataManager::GetUserDefinedDeviceName(int32_t userId, std::string &deviceName)
59 {
60     return GetValue(SETTINGSDATA_SECURE, userId, SETTINGS_GENERAL_USER_DEFINED_DEVICE_NAME, deviceName);
61 }
62 
SetUserDefinedDeviceName(const std::string & deviceName,int32_t userId)63 int32_t SettingsDataManager::SetUserDefinedDeviceName(const std::string &deviceName, int32_t userId)
64 {
65     return SetValue(SETTINGSDATA_SECURE, userId, SETTINGS_GENERAL_USER_DEFINED_DEVICE_NAME, deviceName);
66 }
67 
GetDisplayDeviceName(int32_t userId,std::string & deviceName)68 int32_t SettingsDataManager::GetDisplayDeviceName(int32_t userId, std::string &deviceName)
69 {
70     return GetValue(SETTINGSDATA_SECURE, userId, SETTINGS_GENERAL_DISPLAY_DEVICE_NAME, deviceName);
71 }
72 
SetDisplayDeviceName(const std::string & deviceName,int32_t userId)73 int32_t SettingsDataManager::SetDisplayDeviceName(const std::string &deviceName, int32_t userId)
74 {
75     return SetValue(SETTINGSDATA_SECURE, userId, SETTINGS_GENERAL_DISPLAY_DEVICE_NAME, deviceName);
76 }
77 
GetDeviceName(std::string & deviceName)78 int32_t SettingsDataManager::GetDeviceName(std::string &deviceName)
79 {
80     return GetValue(SETTINGSDATA_GLOBAL, 0, SETTINGS_GENERAL_DEVICE_NAME, deviceName);
81 }
82 
GetRemoteObj()83 sptr<IRemoteObject> SettingsDataManager::GetRemoteObj()
84 {
85     std::lock_guard<std::mutex> lock(remoteObjMtx_);
86     if (remoteObj_ != nullptr) {
87         return remoteObj_;
88     }
89     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
90     if (samgr == nullptr) {
91         HILOGE("get sa manager return nullptr");
92         return nullptr;
93     }
94     auto remoteObj = samgr->GetSystemAbility(DISTRIBUTED_DEVICE_PROFILE_SA_ID);
95     if (remoteObj == nullptr) {
96         HILOGE("get system ability failed, id=%{public}d", DISTRIBUTED_DEVICE_PROFILE_SA_ID);
97         return nullptr;
98     }
99     remoteObj_ = remoteObj;
100     return remoteObj_;
101 }
102 
GetValue(const std::string & tableName,int32_t userId,const std::string & key,std::string & value)103 int32_t SettingsDataManager::GetValue(const std::string &tableName, int32_t userId,
104     const std::string &key, std::string &value)
105 {
106     std::string proxyUri = GetProxyUriStr(tableName, userId);
107     auto helper = CreateDataShareHelper(proxyUri);
108     if (helper == nullptr) {
109         HILOGE("helper is nullptr");
110         return DP_NULLPTR;
111     }
112     std::vector<std::string> columns = { SETTING_COLUMN_VALUE };
113     DataShare::DataSharePredicates predicates;
114     predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
115     Uri uri = MakeUri(proxyUri, key);
116     auto resultSet = helper->Query(uri, predicates, columns);
117     ReleaseDataShareHelper(helper);
118     if (resultSet == nullptr) {
119         HILOGE("Query failed key=%{public}s", key.c_str());
120         return DP_NULLPTR;
121     }
122     int32_t count = ROWCOUNT_INIT;
123     resultSet->GetRowCount(count);
124     if (count == 0) {
125         HILOGW("no value, key=%{public}s", key.c_str());
126         resultSet->Close();
127         return DP_SUCCESS;
128     }
129     int32_t index = 0;
130     resultSet->GoToRow(index);
131     int32_t ret = resultSet->GetString(index, value);
132     if (ret != DataShare::E_OK) {
133         HILOGE("get value failed, ret=%{public}d", ret);
134         resultSet->Close();
135         return ret;
136     }
137     resultSet->Close();
138     return DP_SUCCESS;
139 }
140 
SetValue(const std::string & tableName,int32_t userId,const std::string & key,const std::string & value)141 int32_t SettingsDataManager::SetValue(const std::string &tableName, int32_t userId,
142     const std::string &key, const std::string &value)
143 {
144     std::string proxyUri = GetProxyUriStr(tableName, userId);
145     auto helper = CreateDataShareHelper(proxyUri);
146     if (helper == nullptr) {
147         HILOGE("helper is nullptr");
148         return DP_NULLPTR;
149     }
150 
151     DataShare::DataShareValuesBucket val;
152     val.Put(SETTING_COLUMN_KEYWORD, key);
153     val.Put(SETTING_COLUMN_VALUE, value);
154     Uri uri = MakeUri(proxyUri, key);
155     DataShare::DataSharePredicates predicates;
156     predicates.EqualTo(SETTING_COLUMN_KEYWORD, key);
157     int32_t ret = helper->Update(uri, predicates, val);
158     if (ret <= 0) {
159         HILOGW("Update failed, ret=%{public}d", ret);
160         ret = helper->Insert(uri, val);
161     }
162     ReleaseDataShareHelper(helper);
163     if (ret <= 0) {
164         HILOGE("set value failed, ret=%{public}d", ret);
165         return ret;
166     }
167     return ret;
168 }
169 
CreateDataShareHelper(const std::string & proxyUri)170 std::shared_ptr<DataShare::DataShareHelper> SettingsDataManager::CreateDataShareHelper(const std::string &proxyUri)
171 {
172     if (proxyUri.empty()) {
173         HILOGE("proxyUri is empty");
174         return nullptr;
175     }
176     auto [ret, helper] = DataShare::DataShareHelper::Create(GetRemoteObj(), proxyUri, SETTINGS_DATA_EXT_URI);
177     if (ret != 0) {
178         HILOGE("create helper failed ret %{public}d", ret);
179         return nullptr;
180     }
181     return helper;
182 }
183 
GetProxyUriStr(const std::string & tableName,int32_t userId)184 std::string SettingsDataManager::GetProxyUriStr(const std::string &tableName, int32_t userId)
185 {
186     if (userId < USERID_HELPER_NUMBER) {
187         userId = USERID_HELPER_NUMBER;
188     }
189     if (tableName == SETTINGSDATA_GLOBAL) {
190         return SETTING_URI_PROXY + SETTINGSDATA_GLOBAL + URI_PROXY_SUFFIX;
191     } else {
192         return SETTING_URI_PROXY + tableName + std::to_string(userId) + URI_PROXY_SUFFIX;
193     }
194 }
195 
MakeUri(const std::string & proxyUri,const std::string & key)196 Uri SettingsDataManager::MakeUri(const std::string &proxyUri, const std::string &key)
197 {
198     if (proxyUri.empty() || key.empty()) {
199         HILOGE("Invalid parameter.");
200     }
201     Uri uri(proxyUri + "&key=" + key);
202     return uri;
203 }
204 
ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> helper)205 bool SettingsDataManager::ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> helper)
206 {
207     if (helper == nullptr) {
208         HILOGE("helper is nullptr");
209         return false;
210     }
211     if (!helper->Release()) {
212         HILOGE("release helper fail");
213         return false;
214     }
215     return true;
216 }
217 } // namespace DistributedDeviceProfile
218 } // namespace OHOS
219