• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "location_data_rdb_manager.h"
16 
17 #include "common_utils.h"
18 #include "location_data_rdb_helper.h"
19 #include "parameter.h"
20 #include <nlohmann/json.hpp>
21 
22 namespace OHOS {
23 namespace Location {
24 const int DEFAULT_USERID = 100;
25 const int MAX_SIZE = 100;
26 std::mutex LocationDataRdbManager::mutex_;
27 const std::string LOCATION_ENHANCE_STATUS = "location_enhance_status";
28 std::mutex LocationDataRdbManager::gnssSessionStateMutex_;
GetLocationDataUriByCurrentUserId(std::string key)29 std::string LocationDataRdbManager::GetLocationDataUriByCurrentUserId(std::string key)
30 {
31     int userId = 0;
32     if (!CommonUtils::GetCurrentUserId(userId)) {
33         userId = DEFAULT_USERID;
34     }
35     std::string uri = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_" +
36         std::to_string(userId) +
37         "?Proxy=true&key=" + key;
38     return uri;
39 }
40 
GetLocationDataUriForUser(std::string key,int32_t userId)41 std::string LocationDataRdbManager::GetLocationDataUriForUser(std::string key, int32_t userId)
42 {
43     std::string uri = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_" +
44         std::to_string(userId) +
45         "?Proxy=true&key=" + key;
46     return uri;
47 }
48 
GetLocationDataSecureUri(std::string key)49 std::string LocationDataRdbManager::GetLocationDataSecureUri(std::string key)
50 {
51     int userId = 0;
52     if (!CommonUtils::GetCurrentUserId(userId)) {
53         userId = DEFAULT_USERID;
54     }
55     std::string uri = "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_" +
56         std::to_string(userId) +
57         "?Proxy=true&key=" + key;
58     return uri;
59 }
60 
QuerySwitchState()61 int LocationDataRdbManager::QuerySwitchState()
62 {
63     int res = LocationDataRdbManager::GetSwitchStateFromSysparaForCurrentUser();
64     if (res == DISABLED || res == ENABLED) {
65         return res;
66     }
67     int32_t state = DEFAULT_SWITCH_STATE;
68     Uri locationDataEnableUri(GetLocationDataUriByCurrentUserId("location_enable"));
69     LocationErrCode errCode = LocationDataRdbHelper::GetInstance()->
70         GetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, state);
71     if (errCode != ERRCODE_SUCCESS) {
72         LBSLOGE(COMMON_UTILS, "%{public}s: query state failed, errcode = %{public}d", __func__, errCode);
73         return DEFAULT_SWITCH_STATE;
74     }
75     if (res == DEFAULT_SWITCH_STATE && state != DEFAULT_SWITCH_STATE) {
76         LocationDataRdbManager::SetSwitchStateToSysparaForCurrentUser(state);
77     }
78     return state;
79 }
80 
SetSwitchStateToDb(int modeValue)81 LocationErrCode LocationDataRdbManager::SetSwitchStateToDb(int modeValue)
82 {
83     Uri locationDataEnableUri(GetLocationDataUriByCurrentUserId("location_enable"));
84     return LocationDataRdbHelper::GetInstance()->
85         SetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, modeValue);
86 }
87 
SetSwitchStateToDbForUser(int modeValue,int32_t userId)88 LocationErrCode LocationDataRdbManager::SetSwitchStateToDbForUser(int modeValue, int32_t userId)
89 {
90     Uri locationDataEnableUri(GetLocationDataUriForUser("location_enable", userId));
91     return LocationDataRdbHelper::GetInstance()->
92         SetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, modeValue);
93 }
94 
GetSwitchStateFromDbForUser(int32_t & state,int32_t userId)95 LocationErrCode LocationDataRdbManager::GetSwitchStateFromDbForUser(int32_t& state, int32_t userId)
96 {
97     Uri locationDataEnableUri(GetLocationDataUriForUser("location_enable", userId));
98     return LocationDataRdbHelper::GetInstance()->
99         GetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, state);
100 }
101 
SetLocationWorkingState(int32_t state)102 bool LocationDataRdbManager::SetLocationWorkingState(int32_t state)
103 {
104     Uri locationWorkingStateUri(GetLocationDataUriByCurrentUserId(LOCATION_WORKING_STATE));
105     LocationErrCode errCode = LocationDataRdbHelper::GetInstance()->
106         SetValue(locationWorkingStateUri, LOCATION_WORKING_STATE, state);
107     if (errCode != ERRCODE_SUCCESS) {
108         LBSLOGE(COMMON_UTILS, "%{public}s: can not set value to db, errcode = %{public}d", __func__, errCode);
109         return false;
110     }
111     return true;
112 }
113 
GetLocationWorkingState(int32_t & state)114 bool LocationDataRdbManager::GetLocationWorkingState(int32_t& state)
115 {
116     Uri locationWorkingStateUri(GetLocationDataUriByCurrentUserId(LOCATION_WORKING_STATE));
117     LocationErrCode errCode = LocationDataRdbHelper::GetInstance()->
118         GetValue(locationWorkingStateUri, LOCATION_WORKING_STATE, state);
119     if (errCode != ERRCODE_SUCCESS) {
120         LBSLOGE(COMMON_UTILS, "%{public}s: can not get value, errcode = %{public}d", __func__, errCode);
121         return false;
122     }
123     return true;
124 }
125 
SetGnssSessionState(int32_t state,std::string uri,std::string colName)126 bool LocationDataRdbManager::SetGnssSessionState(int32_t state, std::string uri, std::string colName)
127 {
128     std::unique_lock<std::mutex> lock(gnssSessionStateMutex_);
129     Uri gnssSessionStateUri(uri);
130     LocationErrCode errCode = LocationDataRdbHelper::GetInstance()->
131         SetValue(gnssSessionStateUri, colName, state);
132     if (errCode != ERRCODE_SUCCESS) {
133         LBSLOGE(COMMON_UTILS, "%{public}s: can not set value to db, errcode = %{public}d", __func__, errCode);
134         return false;
135     }
136     return true;
137 }
138 
GetSwitchStateFromSysparaForCurrentUser()139 int LocationDataRdbManager::GetSwitchStateFromSysparaForCurrentUser()
140 {
141     int32_t userId = 0;
142     if (!CommonUtils::GetCurrentUserId(userId)) {
143         userId = DEFAULT_USERID;
144     }
145     return GetSwitchStateFromSysparaForUser(userId);
146 }
147 
SetSwitchStateToSysparaForCurrentUser(int value)148 bool LocationDataRdbManager::SetSwitchStateToSysparaForCurrentUser(int value)
149 {
150     int32_t userId = 0;
151     if (!CommonUtils::GetCurrentUserId(userId)) {
152         userId = DEFAULT_USERID;
153     }
154     return SetSwitchStateToSysparaForUser(value, userId);
155 }
156 
GetSwitchStateFromSysparaForUser(int32_t userId)157 int LocationDataRdbManager::GetSwitchStateFromSysparaForUser(int32_t userId)
158 {
159     char result[MAX_SIZE] = {0};
160     std::string value = "";
161     std::unique_lock<std::mutex> lock(mutex_);
162     auto res = GetParameter(LOCATION_SWITCH_MODE, "", result, MAX_SIZE);
163     if (res < 0 || strlen(result) == 0) {
164         LBSLOGE(COMMON_UTILS, "%{public}s get para value failed, res: %{public}d", __func__, res);
165         return DEFAULT_SWITCH_STATE;
166     }
167     value = result;
168     nlohmann::json switchInfo = nlohmann::json::parse(value, nullptr, false);
169     if (switchInfo.is_discarded()) {
170         LBSLOGE(COMMON_UTILS, "switchInfo parse failed");
171         return DEFAULT_SWITCH_STATE;
172     }
173     if (!switchInfo.contains(std::to_string(userId))) {
174         LBSLOGE(COMMON_UTILS, "userId switch %{public}d is not exist", userId);
175         return DEFAULT_SWITCH_STATE;
176     }
177     auto jsonItem = switchInfo.at(std::to_string(userId));
178     if (!jsonItem.is_number()) {
179         LBSLOGE(COMMON_UTILS, "switch state is invalid");
180         return DEFAULT_SWITCH_STATE;
181     }
182     auto state = jsonItem.get<int>();
183     return state;
184 }
185 
SetSwitchStateToSysparaForUser(int value,int32_t userId)186 bool LocationDataRdbManager::SetSwitchStateToSysparaForUser(int value, int32_t userId)
187 {
188     char result[MAX_SIZE] = {0};
189     std::unique_lock<std::mutex> lock(mutex_);
190     nlohmann::json oldSwitchInfo;
191     auto res = GetParameter(LOCATION_SWITCH_MODE, "", result, MAX_SIZE);
192     if (res < 0 || strlen(result) == 0) {
193         // If there is no value in sysparam, go on and write it.
194         LBSLOGI(COMMON_UTILS, "%{public}s get para value failed, res: %{public}d", __func__, res);
195     } else {
196         std::string SwitchStr = result;
197         oldSwitchInfo = nlohmann::json::parse(SwitchStr, nullptr, false);
198         if (oldSwitchInfo.is_discarded()) {
199             LBSLOGI(COMMON_UTILS, "switchInfo parse failed");
200             // If there is no valid value in sysparam, go on and overwrite it.
201         }
202     }
203     nlohmann::json newSwitchInfo;
204     std::vector<int> activeIds;
205     // copy oldSwitchInfo to newSwitchInfo
206     if (CommonUtils::GetAllUserId(activeIds)) {
207         for (auto && [key, state] : oldSwitchInfo.items()) {
208             if (IsUserIdInActiveIds(activeIds, key)) {
209                 newSwitchInfo[key] = state;
210             }
211         }
212     }
213     newSwitchInfo[std::to_string(userId)] = value;
214     std::string newSwitchStr = newSwitchInfo.dump();
215     char valueArray[MAX_SIZE] = {0};
216     auto ret = sprintf_s(valueArray, sizeof(valueArray), "%s", newSwitchStr.c_str());
217     if (ret <= 0) {
218         LBSLOGE(COMMON_UTILS, "sprintf_s failed, ret: %{public}d", ret);
219         return false;
220     }
221     res = SetParameter(LOCATION_SWITCH_MODE, valueArray);
222     if (res < 0) {
223         LBSLOGE(COMMON_UTILS, "%{public}s failed, res: %{public}d", __func__, res);
224         return false;
225     }
226     return true;
227 }
228 
SyncSwitchStatus()229 void LocationDataRdbManager::SyncSwitchStatus()
230 {
231     int dbState = DEFAULT_SWITCH_STATE;
232     Uri locationDataEnableUri(GetLocationDataUriByCurrentUserId("location_enable"));
233     LocationErrCode errCode = LocationDataRdbHelper::GetInstance()->
234         GetValue(locationDataEnableUri, LOCATION_DATA_COLUMN_ENABLE, dbState);
235     if (errCode != ERRCODE_SUCCESS) {
236         // It needs to be updated when it is the default, and there is no need to return.
237         LBSLOGE(COMMON_UTILS, "%{public}s: query state failed, errcode = %{public}d", __func__, errCode);
238     }
239     int sysparaState = LocationDataRdbManager::GetSwitchStateFromSysparaForCurrentUser();
240     if (sysparaState == DEFAULT_SWITCH_STATE && dbState != DEFAULT_SWITCH_STATE) {
241         LocationDataRdbManager::SetSwitchStateToSysparaForCurrentUser(dbState);
242     } else if (sysparaState != DEFAULT_SWITCH_STATE && dbState != sysparaState) {
243         LocationDataRdbManager::SetSwitchStateToDb(sysparaState);
244     }
245 }
246 
SetLocationEnhanceStatus(int32_t state)247 bool LocationDataRdbManager::SetLocationEnhanceStatus(int32_t state)
248 {
249     Uri locationWorkingStateUri(GetLocationDataSecureUri(LOCATION_ENHANCE_STATUS));
250     LocationErrCode errCode = LocationDataRdbHelper::GetInstance()->
251         SetValue(locationWorkingStateUri, LOCATION_ENHANCE_STATUS, state);
252     if (errCode != ERRCODE_SUCCESS) {
253         LBSLOGE(COMMON_UTILS,
254             "can not set value, key = %{public}s, errcode = %{public}d", LOCATION_ENHANCE_STATUS.c_str(), errCode);
255         return false;
256     }
257     return true;
258 }
259 
GetLocationEnhanceStatus(int32_t & state)260 bool LocationDataRdbManager::GetLocationEnhanceStatus(int32_t& state)
261 {
262     Uri locationWorkingStateUri(GetLocationDataSecureUri(LOCATION_ENHANCE_STATUS));
263     LocationErrCode errCode = LocationDataRdbHelper::GetInstance()->
264         GetValue(locationWorkingStateUri, LOCATION_ENHANCE_STATUS, state);
265     if (errCode != ERRCODE_SUCCESS) {
266         LBSLOGE(COMMON_UTILS,
267             "can not get value, key = %{public}s, errcode = %{public}d", LOCATION_ENHANCE_STATUS.c_str(), errCode);
268         return false;
269     }
270     return true;
271 }
272 
IsUserIdInActiveIds(std::vector<int> activeIds,std::string userId)273 bool LocationDataRdbManager::IsUserIdInActiveIds(std::vector<int> activeIds, std::string userId)
274 {
275     for (auto id : activeIds) {
276         if (std::to_string(id).compare(userId) == 0) {
277             return true;
278         }
279     }
280     return false;
281 }
282 } // namespace Location
283 } // namespace OHOS