• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "location_data_rdb_manager.h"
25 #include "ipc_skeleton.h"
26 #include "nlohmann/json.hpp"
27 #include "ui_extension_ability_connection.h"
28 
29 namespace OHOS {
30 namespace Location {
31 const int UNKNOW_ERROR = -1;
32 const int MAX_SIZE = 100;
33 const char* LOCATION_PRIVACY_MODE = "persist.location.privacy_mode";
34 constexpr const char* LOCATION_GNSS_ENABLE_STATE = "persist.location.gnss_enable_state";
35 constexpr const char* LOCATION_NLP_ENABLE_STATE = "persist.location.nlp_enable_state";
GetInstance()36 LocationConfigManager* LocationConfigManager::GetInstance()
37 {
38     static LocationConfigManager gLocationConfigManager;
39     return &gLocationConfigManager;
40 }
41 
LocationConfigManager()42 LocationConfigManager::LocationConfigManager()
43 {
44     mLocationSwitchState = STATE_CLOSE;
45     mPrivacyTypeState[PRIVACY_TYPE_OTHERS] = STATE_CLOSE; // for OTHERS
46     mPrivacyTypeState[PRIVACY_TYPE_STARTUP] = STATE_CLOSE; // for STARTUP
47     mPrivacyTypeState[PRIVACY_TYPE_CORE_LOCATION] = STATE_CLOSE; // for CORE_LOCATION
48 }
49 
~LocationConfigManager()50 LocationConfigManager::~LocationConfigManager()
51 {
52 }
53 
Init()54 int LocationConfigManager::Init()
55 {
56     LBSLOGI(LOCATOR, "LocationConfigManager::Init");
57     if (!IsExistFile(GetLocationSwitchConfigPath())) {
58         CreateFile(GetLocationSwitchConfigPath(), "0");
59     }
60     return 0;
61 }
62 
IsExistFile(const std::string & filename)63 bool LocationConfigManager::IsExistFile(const std::string& filename)
64 {
65     bool bExist = false;
66     std::fstream ioFile;
67     char path[PATH_MAX + 1] = {0x00};
68     if (strlen(filename.c_str()) > PATH_MAX || realpath(filename.c_str(), path) == NULL) {
69         return false;
70     }
71     ioFile.open(path, std::ios::in);
72     if (ioFile) {
73         bExist = true;
74     } else {
75         return false;
76     }
77     ioFile.clear();
78     ioFile.close();
79     LBSLOGD(LOCATOR, "IsExistFile = %{public}d", bExist ? 1 : 0);
80     return bExist;
81 }
82 
CreateFile(const std::string & filename,const std::string & filedata)83 bool LocationConfigManager::CreateFile(const std::string& filename, const std::string& filedata)
84 {
85     LBSLOGD(LOCATOR, "CreateFile");
86     std::ofstream outFile;
87     outFile.open(filename.c_str());
88     if (!outFile) {
89         LBSLOGE(LOCATOR, "file open failed");
90         return false;
91     }
92     outFile.flush();
93     outFile << filedata << std::endl;
94     outFile.clear();
95     outFile.close();
96     return true;
97 }
98 
GetLocationSwitchConfigPath()99 std::string LocationConfigManager::GetLocationSwitchConfigPath()
100 {
101     int userId = 0;
102     bool ret = CommonUtils::GetCurrentUserId(userId);
103     if (!ret) {
104         LBSLOGE(LOCATOR, "%{public}s GetCurrentUserId failed", __func__);
105     }
106     std::string filePath = LOCATION_DIR + SWITCH_CONFIG_NAME + "_" + std::to_string(userId) + ".conf";
107     return filePath;
108 }
109 
GetPrivacyTypeConfigPath(const int type)110 std::string LocationConfigManager::GetPrivacyTypeConfigPath(const int type)
111 {
112     int userId = 0;
113     bool ret = CommonUtils::GetCurrentUserId(userId);
114     if (!ret) {
115         LBSLOGE(LOCATOR, "%{public}s GetCurrentUserId failed", __func__);
116     }
117     std::string filePath;
118     switch (type) {
119         case PRIVACY_TYPE_OTHERS: {
120             filePath = "others_";
121             break;
122         }
123         case PRIVACY_TYPE_STARTUP: {
124             filePath = "startup_";
125             break;
126         }
127         case PRIVACY_TYPE_CORE_LOCATION: {
128             filePath = "core_location_";
129             break;
130         }
131         default: {
132             filePath = "";
133             break;
134         }
135     }
136     return LOCATION_DIR + PRIVACY_CONFIG_NAME + "_" + filePath + std::to_string(userId) + ".conf";
137 }
138 
GetLocationSwitchState()139 int LocationConfigManager::GetLocationSwitchState()
140 {
141     std::unique_lock<std::mutex> lock(mutex_);
142     if (!IsExistFile(GetLocationSwitchConfigPath())) {
143         CreateFile(GetLocationSwitchConfigPath(), "0");
144     }
145     std::ifstream fs(GetLocationSwitchConfigPath());
146     if (!fs.is_open()) {
147         LBSLOGE(LOCATOR, "LocationConfigManager: fs.is_open false, return");
148         return -1;
149     }
150     std::string line;
151     while (std::getline(fs, line)) {
152         if (line.empty()) {
153             break;
154         }
155         if (line[0] == '0') {
156             mLocationSwitchState = STATE_CLOSE;
157         } else if (line[0] == '1') {
158             mLocationSwitchState = STATE_OPEN;
159         }
160         break;
161     }
162     fs.clear();
163     fs.close();
164     return mLocationSwitchState;
165 }
166 
GetStringParameter(const std::string & type,std::string & value)167 bool LocationConfigManager::GetStringParameter(const std::string& type, std::string& value)
168 {
169     char result[MAX_BUFF_SIZE] = {0};
170     auto res = GetParameter(type.c_str(), "", result, MAX_BUFF_SIZE);
171     if (res <= 0) {
172         LBSLOGE(LOCATOR, "%{public}s get para value failed, res: %{public}d",
173             __func__, res);
174         return false;
175     }
176     value = result;
177     return true;
178 }
179 
GetIntParameter(const std::string & type)180 int LocationConfigManager::GetIntParameter(const std::string& type)
181 {
182     char result[MAX_BUFF_SIZE] = {0};
183     std::string value = "";
184     auto res = GetParameter(type.c_str(), "", result, MAX_BUFF_SIZE);
185     if (res < 0 || strlen(result) == 0) {
186         LBSLOGE(LOCATOR, "%{public}s get para value failed, res: %{public}d",
187             __func__, res);
188         return UNKNOW_ERROR;
189     }
190     value = result;
191     for (auto ch : value) {
192         if (std::isdigit(ch) == 0) {
193             LBSLOGE(LOCATOR, "wrong para");
194             return UNKNOW_ERROR;
195         }
196     }
197     if (value.size() == 0) {
198         return UNKNOW_ERROR;
199     }
200     return std::stoi(value);
201 }
202 
GetSettingsBundleName(std::string & name)203 bool LocationConfigManager::GetSettingsBundleName(std::string& name)
204 {
205     return GetStringParameter(SETTINGS_BUNDLE_NAME, name);
206 }
207 
GetNlpServiceName(std::string & name)208 bool LocationConfigManager::GetNlpServiceName(std::string& name)
209 {
210     return GetStringParameter(NLP_SERVICE_NAME, name);
211 }
212 
GetNlpAbilityName(std::string & name)213 bool LocationConfigManager::GetNlpAbilityName(std::string& name)
214 {
215     return GetStringParameter(NLP_ABILITY_NAME, name);
216 }
217 
GetGeocodeServiceName(std::string & name)218 bool LocationConfigManager::GetGeocodeServiceName(std::string& name)
219 {
220     return GetStringParameter(GEOCODE_SERVICE_NAME, name);
221 }
222 
GetGeocodeAbilityName(std::string & name)223 bool LocationConfigManager::GetGeocodeAbilityName(std::string& name)
224 {
225     return GetStringParameter(GEOCODE_ABILITY_NAME, name);
226 }
227 
GetSuplMode()228 int LocationConfigManager::GetSuplMode()
229 {
230     return GetIntParameter(SUPL_MODE_NAME);
231 }
232 
GetAgnssServerAddr(std::string & name)233 bool LocationConfigManager::GetAgnssServerAddr(std::string& name)
234 {
235     return GetStringParameter(AGNSS_SERVER_ADDR, name);
236 }
237 
GetAgnssServerPort()238 int LocationConfigManager::GetAgnssServerPort()
239 {
240     return GetIntParameter(AGNSS_SERVER_PORT);
241 }
242 
GetGnssEnableState()243 int LocationConfigManager::GetGnssEnableState()
244 {
245     int gnssEnableState = GetIntParameter(LOCATION_GNSS_ENABLE_STATE);
246     if (gnssEnableState == UNKNOW_ERROR) {
247         return 1;
248     }
249     return gnssEnableState;
250 }
251 
GetNlpEnableState()252 int LocationConfigManager::GetNlpEnableState()
253 {
254     int nlpEnableState = GetIntParameter(LOCATION_NLP_ENABLE_STATE);
255     if (nlpEnableState == UNKNOW_ERROR) {
256         return 1;
257     }
258     return nlpEnableState;
259 }
260 
SetLocationSwitchState(int state)261 int LocationConfigManager::SetLocationSwitchState(int state)
262 {
263     std::unique_lock<std::mutex> lock(mutex_);
264     if (!IsExistFile(GetLocationSwitchConfigPath())) {
265         CreateFile(GetLocationSwitchConfigPath(), "0");
266     }
267     std::fstream fs(GetLocationSwitchConfigPath());
268     if (state != STATE_CLOSE && state != STATE_OPEN) {
269         LBSLOGE(LOCATOR, "LocationConfigManager:SetLocationSwitchState state = %{public}d, return", state);
270         return -1;
271     }
272     if (!fs.is_open()) {
273         LBSLOGE(LOCATOR, "LocationConfigManager: fs.is_open false, return");
274         return -1;
275     }
276     std::string content = "1";
277     if (state == STATE_CLOSE) {
278         content = "0";
279     }
280     fs.write(content.c_str(), content.length());
281     fs.clear();
282     fs.close();
283     mLocationSwitchState = state;
284     return 0;
285 }
286 
GetPrivacyTypeState(const int type,bool & isConfirmed)287 LocationErrCode LocationConfigManager::GetPrivacyTypeState(const int type, bool& isConfirmed)
288 {
289     int status = 0;
290     if (!LocationDataRdbManager::GetLocationEnhanceStatus(status)) {
291         return ERRCODE_SERVICE_UNAVAILABLE;
292     }
293     isConfirmed = (status == 1);
294     return ERRCODE_SUCCESS;
295 }
296 
SetCachePrivacyType(int value)297 bool LocationConfigManager::SetCachePrivacyType(int value)
298 {
299     char valueArray[MAX_SIZE] = {0};
300     (void)sprintf_s(valueArray, sizeof(valueArray), "%d", value);
301     int res = SetParameter(LOCATION_PRIVACY_MODE, valueArray);
302     if (res < 0) {
303         LBSLOGE(COMMON_UTILS, "%{public}s failed, res: %{public}d", __func__, res);
304         return false;
305     }
306     return true;
307 }
308 
SetPrivacyTypeState(const int type,bool isConfirmed)309 LocationErrCode LocationConfigManager::SetPrivacyTypeState(const int type, bool isConfirmed)
310 {
311     int status = isConfirmed ? 1 : 0;
312     if (!LocationDataRdbManager::SetLocationEnhanceStatus(status)) {
313         return ERRCODE_SERVICE_UNAVAILABLE;
314     }
315     return ERRCODE_SUCCESS;
316 }
317 
GenerateStartCommand()318 std::string LocationConfigManager::GenerateStartCommand()
319 {
320     nlohmann::json param;
321     std::string uiType = "sysDialog/common";
322     param["ability.want.params.uiExtensionType"] = uiType;
323     std::string cmdData = param.dump();
324     LBSLOGD(GNSS, "cmdData is: %{public}s.", cmdData.c_str());
325     return cmdData;
326 }
327 
OpenPrivacyDialog()328 void LocationConfigManager::OpenPrivacyDialog()
329 {
330     LBSLOGI(LOCATOR, "ConnectExtension");
331     AAFwk::Want want;
332     std::string bundleName = "com.ohos.sceneboard";
333     std::string abilityName = "com.ohos.sceneboard.systemdialog";
334     want.SetElementName(bundleName, abilityName);
335     std::string connectStr = GenerateStartCommand();
336     ConnectExtensionAbility(want, connectStr);
337 }
338 
ConnectExtensionAbility(const AAFwk::Want & want,const std::string & commandStr)339 void LocationConfigManager::ConnectExtensionAbility(const AAFwk::Want &want, const std::string &commandStr)
340 {
341     std::string bundleName = "com.ohos.locationdialog";
342     std::string abilityName = "LocationPrivacyExtAbility";
343     sptr<UIExtensionAbilityConnection> connection(
344         new (std::nothrow) UIExtensionAbilityConnection(commandStr, bundleName, abilityName));
345     if (connection == nullptr) {
346         LBSLOGE(LOCATOR, "connect UIExtensionAbilityConnection fail");
347         return;
348     }
349 
350     std::string identity = IPCSkeleton::ResetCallingIdentity();
351     auto ret =
352         AAFwk::ExtensionManagerClient::GetInstance().ConnectServiceExtensionAbility(want, connection, nullptr, -1);
353     LBSLOGI(LOCATOR, "connect service extension ability result = %{public}d", ret);
354     IPCSkeleton::SetCallingIdentity(identity);
355     return;
356 }
357 }  // namespace Location
358 }  // namespace OHOS
359