1 /*
2 * Copyright (C) 2022 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_config_manager.h"
16
17 #include <fstream>
18
19 #include "common_utils.h"
20 #include "constant_definition.h"
21 #include "location_log.h"
22
23 #include "parameter.h"
24
25 namespace OHOS {
26 namespace Location {
GetInstance()27 LocationConfigManager &LocationConfigManager::GetInstance()
28 {
29 static LocationConfigManager gLocationConfigManager;
30 return gLocationConfigManager;
31 }
32
LocationConfigManager()33 LocationConfigManager::LocationConfigManager()
34 {
35 mLocationSwitchState = STATE_CLOSE;
36 mPrivacyTypeState[PRIVACY_TYPE_OTHERS] = STATE_CLOSE; // for OTHERS
37 mPrivacyTypeState[PRIVACY_TYPE_STARTUP] = STATE_CLOSE; // for STARTUP
38 mPrivacyTypeState[PRIVACY_TYPE_CORE_LOCATION] = STATE_CLOSE; // for CORE_LOCATION
39 }
40
~LocationConfigManager()41 LocationConfigManager::~LocationConfigManager()
42 {
43 }
44
Init()45 int LocationConfigManager::Init()
46 {
47 LBSLOGI(LOCATOR, "LocationConfigManager::Init");
48 if (!IsExistFile(GetLocationSwitchConfigPath())) {
49 CreateFile(GetLocationSwitchConfigPath(), "0");
50 }
51 return 0;
52 }
53
IsExistFile(const std::string & filename)54 bool LocationConfigManager::IsExistFile(const std::string& filename)
55 {
56 bool bExist = false;
57 std::fstream ioFile;
58 char path[PATH_MAX + 1] = {0x00};
59 if (strlen(filename.c_str()) > PATH_MAX || realpath(filename.c_str(), path) == NULL) {
60 return false;
61 }
62 ioFile.open(path, std::ios::in);
63 if (ioFile) {
64 bExist = true;
65 } else {
66 return false;
67 }
68 ioFile.clear();
69 ioFile.close();
70 LBSLOGD(LOCATOR, "IsExistFile = %{public}d", bExist ? 1 : 0);
71 return bExist;
72 }
73
CreateFile(const std::string & filename,const std::string & filedata)74 bool LocationConfigManager::CreateFile(const std::string& filename, const std::string& filedata)
75 {
76 LBSLOGD(LOCATOR, "CreateFile");
77 std::ofstream outFile;
78 outFile.open(filename.c_str());
79 if (!outFile) {
80 LBSLOGE(LOCATOR, "file open failed");
81 return false;
82 }
83 outFile.flush();
84 outFile << filedata << std::endl;
85 outFile.clear();
86 outFile.close();
87 return true;
88 }
89
GetLocationSwitchConfigPath()90 std::string LocationConfigManager::GetLocationSwitchConfigPath()
91 {
92 int userId = 0;
93 bool ret = CommonUtils::GetCurrentUserId(userId);
94 if (!ret) {
95 LBSLOGE(LOCATOR, "GetCurrentUserId failed");
96 }
97 std::string filePath = LOCATION_DIR + SWITCH_CONFIG_NAME + "_" + std::to_string(userId) + ".conf";
98 return filePath;
99 }
100
GetPrivacyTypeConfigPath(const int type)101 std::string LocationConfigManager::GetPrivacyTypeConfigPath(const int type)
102 {
103 int userId = 0;
104 bool ret = CommonUtils::GetCurrentUserId(userId);
105 if (!ret) {
106 LBSLOGE(LOCATOR, "GetCurrentUserId failed");
107 }
108 std::string filePath;
109 switch (type) {
110 case PRIVACY_TYPE_OTHERS: {
111 filePath = "others_";
112 break;
113 }
114 case PRIVACY_TYPE_STARTUP: {
115 filePath = "startup_";
116 break;
117 }
118 case PRIVACY_TYPE_CORE_LOCATION: {
119 filePath = "core_location_";
120 break;
121 }
122 default: {
123 filePath = "";
124 break;
125 }
126 }
127 return LOCATION_DIR + PRIVACY_CONFIG_NAME + "_" + filePath + std::to_string(userId) + ".conf";
128 }
129
GetLocationSwitchState()130 int LocationConfigManager::GetLocationSwitchState()
131 {
132 std::unique_lock<std::mutex> lock(mutex_);
133 if (!IsExistFile(GetLocationSwitchConfigPath())) {
134 CreateFile(GetLocationSwitchConfigPath(), "0");
135 }
136 std::ifstream fs(GetLocationSwitchConfigPath());
137 if (!fs.is_open()) {
138 LBSLOGE(LOCATOR, "LocationConfigManager: fs.is_open false, return");
139 return -1;
140 }
141 std::string line;
142 while (std::getline(fs, line)) {
143 if (line.empty()) {
144 break;
145 }
146 if (line[0] == '0') {
147 mLocationSwitchState = STATE_CLOSE;
148 } else if (line[0] == '1') {
149 mLocationSwitchState = STATE_OPEN;
150 }
151 break;
152 }
153 fs.clear();
154 fs.close();
155 return mLocationSwitchState;
156 }
157
GetStringParameter(const std::string & type,std::string & value)158 bool LocationConfigManager::GetStringParameter(const std::string& type, std::string& value)
159 {
160 char result[MAX_BUFF_SIZE] = {0};
161 auto res = GetParameter(type.c_str(), "", result, MAX_BUFF_SIZE);
162 if (res <= 0) {
163 LBSLOGE(LOCATOR, "%{public}s get para value failed, res: %{public}d",
164 __func__, res);
165 return false;
166 }
167 value = result;
168 return true;
169 }
170
GetNlpServiceName(std::string & name)171 bool LocationConfigManager::GetNlpServiceName(std::string& name)
172 {
173 return GetStringParameter(NLP_SERVICE_NAME, name);
174 }
175
GetNlpAbilityName(std::string & name)176 bool LocationConfigManager::GetNlpAbilityName(std::string& name)
177 {
178 return GetStringParameter(NLP_ABILITY_NAME, name);
179 }
180
GetGeocodeServiceName(std::string & name)181 bool LocationConfigManager::GetGeocodeServiceName(std::string& name)
182 {
183 return GetStringParameter(GEOCODE_SERVICE_NAME, name);
184 }
185
GetGeocodeAbilityName(std::string & name)186 bool LocationConfigManager::GetGeocodeAbilityName(std::string& name)
187 {
188 return GetStringParameter(GEOCODE_ABILITY_NAME, name);
189 }
190
SetLocationSwitchState(int state)191 int LocationConfigManager::SetLocationSwitchState(int state)
192 {
193 std::unique_lock<std::mutex> lock(mutex_);
194 if (!IsExistFile(GetLocationSwitchConfigPath())) {
195 CreateFile(GetLocationSwitchConfigPath(), "0");
196 }
197 std::fstream fs(GetLocationSwitchConfigPath());
198 if (state != STATE_CLOSE && state != STATE_OPEN) {
199 LBSLOGE(LOCATOR, "LocationConfigManager:SetLocationSwitchState state = %{public}d, return", state);
200 return -1;
201 }
202 if (!fs.is_open()) {
203 LBSLOGE(LOCATOR, "LocationConfigManager: fs.is_open false, return");
204 return -1;
205 }
206 std::string content = "1";
207 if (state == STATE_CLOSE) {
208 content = "0";
209 }
210 fs.write(content.c_str(), content.length());
211 fs.clear();
212 fs.close();
213 mLocationSwitchState = state;
214 return 0;
215 }
216
GetPrivacyTypeState(const int type,bool & isConfirmed)217 LocationErrCode LocationConfigManager::GetPrivacyTypeState(const int type, bool& isConfirmed)
218 {
219 if (type < PRIVACY_TYPE_OTHERS || type > PRIVACY_TYPE_CORE_LOCATION) {
220 LBSLOGI(LOCATOR, "GetPrivacyTypeState,invalid types");
221 isConfirmed = false;
222 return ERRCODE_INVALID_PARAM;
223 }
224 std::unique_lock<std::mutex> lock(mutex_);
225 if (!IsExistFile(GetPrivacyTypeConfigPath(type))) {
226 CreateFile(GetPrivacyTypeConfigPath(type), "0");
227 }
228 std::ifstream fs(GetPrivacyTypeConfigPath(type));
229 if (!fs.is_open()) {
230 LBSLOGE(LOCATOR, "LocationConfigManager: fs.is_open false, return");
231 isConfirmed = false;
232 return ERRCODE_SERVICE_UNAVAILABLE;
233 }
234 std::string line;
235 while (std::getline(fs, line)) {
236 if (line.empty()) {
237 break;
238 }
239 if (line[0] == '0') {
240 mPrivacyTypeState[type] = STATE_CLOSE;
241 } else if (line[0] == '1') {
242 mPrivacyTypeState[type] = STATE_OPEN;
243 }
244 break;
245 }
246 fs.clear();
247 fs.close();
248 isConfirmed = (mPrivacyTypeState[type] == STATE_OPEN) ? true : false;
249 return ERRCODE_SUCCESS;
250 }
251
SetPrivacyTypeState(const int type,bool isConfirmed)252 LocationErrCode LocationConfigManager::SetPrivacyTypeState(const int type, bool isConfirmed)
253 {
254 if (type < PRIVACY_TYPE_OTHERS || type > PRIVACY_TYPE_CORE_LOCATION) {
255 LBSLOGE(LOCATOR, "SetPrivacyTypeState,invalid types");
256 return ERRCODE_INVALID_PARAM;
257 }
258 std::unique_lock<std::mutex> lock(mutex_);
259 if (!IsExistFile(GetPrivacyTypeConfigPath(type))) {
260 CreateFile(GetPrivacyTypeConfigPath(type), "0");
261 }
262 std::fstream fs(GetPrivacyTypeConfigPath(type));
263 if (!fs.is_open()) {
264 LBSLOGE(LOCATOR, "LocationConfigManager: fs.is_open false, return");
265 return ERRCODE_SERVICE_UNAVAILABLE;
266 }
267 std::string content = "0";
268 if (isConfirmed) {
269 content = "1";
270 }
271 fs.write(content.c_str(), content.length());
272 fs.clear();
273 fs.close();
274 mPrivacyTypeState[type] = isConfirmed ? 1 : 0;
275 return ERRCODE_SUCCESS;
276 }
277 } // namespace Location
278 } // namespace OHOS
279