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