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 
16 #include "locator_impl.h"
17 #include "if_system_ability_manager.h"
18 #include "ipc_skeleton.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 #include "common_utils.h"
22 #include "country_code.h"
23 
24 #include "geo_convert_callback_host.h"
25 #include "location_data_rdb_observer.h"
26 #include "location_data_rdb_helper.h"
27 #include "location_data_rdb_manager.h"
28 #include "location_log.h"
29 #include "location_sa_load_manager.h"
30 #include "locator.h"
31 #include "app_identity.h"
32 #include "permission_manager.h"
33 
34 namespace OHOS {
35 namespace Location {
36 constexpr uint32_t WAIT_MS = 1000;
37 std::shared_ptr<LocatorImpl> LocatorImpl::instance_ = nullptr;
38 std::mutex LocatorImpl::locatorMutex_;
39 auto g_locatorImpl = Locator::GetInstance();
40 std::mutex g_resumeFuncMapMutex;
41 std::mutex g_locationCallbackMapMutex;
42 std::mutex g_gnssStatusInfoCallbacksMutex;
43 std::mutex g_nmeaCallbacksMutex;
44 std::shared_ptr<CallbackResumeManager> g_callbackResumer = std::make_shared<CallbackResumeManager>();
45 using CallbackResumeHandle = std::function<void()>;
46 std::map<std::string, CallbackResumeHandle> g_resumeFuncMap;
47 std::map<sptr<ILocatorCallback>, RequestConfig> g_locationCallbackMap;
48 std::set<sptr<IRemoteObject>> g_gnssStatusInfoCallbacks;
49 std::set<sptr<IRemoteObject>> g_nmeaCallbacks;
50 
GetInstance()51 std::shared_ptr<LocatorImpl> LocatorImpl::GetInstance()
52 {
53     if (instance_ == nullptr) {
54         std::unique_lock<std::mutex> lock(locatorMutex_);
55         if (instance_ == nullptr) {
56             std::shared_ptr<LocatorImpl> locator = std::make_shared<LocatorImpl>();
57             instance_ = locator;
58         }
59     }
60     return instance_;
61 }
62 
LocatorImpl()63 LocatorImpl::LocatorImpl()
64 {
65     locationDataManager_ = LocationDataManager::GetInstance();
66 }
67 
~LocatorImpl()68 LocatorImpl::~LocatorImpl()
69 {
70 }
71 
IsLocationEnabled()72 bool LocatorImpl::IsLocationEnabled()
73 {
74     LBSLOGD(LOCATION_NAPI, "IsLocationEnabled");
75     int32_t state = DEFAULT_SWITCH_STATE;
76     state = LocationDataRdbManager::GetSwitchStateFromSysparaForCurrentUser();
77     if (state == DISABLED || state == ENABLED) {
78         return (state == ENABLED);
79     }
80     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
81         return ERRCODE_SERVICE_UNAVAILABLE;
82     }
83     sptr<LocatorProxy> proxy = GetProxy();
84     if (proxy == nullptr) {
85         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
86         return false;
87     }
88     state = proxy->GetSwitchState();
89     return (state == ENABLED);
90 }
91 
ShowNotification()92 void LocatorImpl::ShowNotification()
93 {
94     LBSLOGI(LOCATION_NAPI, "ShowNotification");
95 }
96 
RequestPermission()97 void LocatorImpl::RequestPermission()
98 {
99     LBSLOGI(LOCATION_NAPI, "permission need to be granted");
100 }
101 
RequestEnableLocation()102 void LocatorImpl::RequestEnableLocation()
103 {
104     LBSLOGI(LOCATION_NAPI, "RequestEnableLocation");
105 }
106 
EnableAbility(bool enable)107 void LocatorImpl::EnableAbility(bool enable)
108 {
109     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
110         return;
111     }
112     LocationErrCode errorCode = CheckEdmPolicy(enable);
113     if (errorCode != ERRCODE_SUCCESS) {
114         return;
115     }
116     sptr<LocatorProxy> proxy = GetProxy();
117     if (proxy == nullptr) {
118         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
119         return;
120     }
121     LocationErrCode errCode = proxy->EnableAbilityV9(enable);
122     if (errCode != ERRCODE_SUCCESS) {
123         LBSLOGE(LOCATOR_STANDARD, "%{public}s EnableAbilityV9 failed. %{public}d", __func__, errCode);
124     }
125 }
126 
StartLocating(std::unique_ptr<RequestConfig> & requestConfig,sptr<ILocatorCallback> & callback)127 void LocatorImpl::StartLocating(std::unique_ptr<RequestConfig>& requestConfig,
128     sptr<ILocatorCallback>& callback)
129 {
130     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
131         return;
132     }
133     sptr<LocatorProxy> proxy = GetProxy();
134     if (proxy == nullptr) {
135         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
136         return;
137     }
138     if (IsLocationCallbackRegistered(callback)) {
139         LBSLOGE(LOCATOR_STANDARD, "%{public}s locatorCallback has registered", __func__);
140         return;
141     }
142     AddLocationCallBack(requestConfig, callback);
143     int errCode = proxy->StartLocating(requestConfig, callback, "location.ILocator", 0, 0);
144     if (errCode != ERRCODE_SUCCESS) {
145         RemoveLocationCallBack(callback);
146     }
147 }
148 
StopLocating(sptr<ILocatorCallback> & callback)149 void LocatorImpl::StopLocating(sptr<ILocatorCallback>& callback)
150 {
151     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
152         return;
153     }
154     sptr<LocatorProxy> proxy = GetProxy();
155     if (proxy == nullptr) {
156         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
157         return;
158     }
159     proxy->StopLocating(callback);
160     std::unique_lock<std::mutex> lock(g_locationCallbackMapMutex);
161     auto iter = g_locationCallbackMap.find(callback);
162     if (iter != g_locationCallbackMap.end()) {
163         g_locationCallbackMap.erase(iter);
164     }
165 }
166 
GetCachedLocation()167 std::unique_ptr<Location> LocatorImpl::GetCachedLocation()
168 {
169     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
170         return nullptr;
171     }
172     sptr<LocatorProxy> proxy = GetProxy();
173     if (proxy == nullptr) {
174         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
175         return nullptr;
176     }
177     std::unique_ptr<Location> location = nullptr;
178     MessageParcel reply;
179     proxy->GetCacheLocation(reply);
180     int exception = reply.ReadInt32();
181     if (exception == ERRCODE_PERMISSION_DENIED) {
182         LBSLOGE(LOCATOR_STANDARD, "can not get cached location without location permission.");
183     } else if (exception != ERRCODE_SUCCESS) {
184         LBSLOGE(LOCATOR_STANDARD, "cause some exception happened in lower service.");
185     } else {
186         location = Location::Unmarshalling(reply);
187     }
188 
189     return location;
190 }
191 
RegisterSwitchCallback(const sptr<IRemoteObject> & callback,pid_t uid)192 bool LocatorImpl::RegisterSwitchCallback(const sptr<IRemoteObject>& callback, pid_t uid)
193 {
194     if (locationDataManager_ == nullptr) {
195         return false;
196     }
197     AppIdentity appIdentity;
198     appIdentity.SetTokenId(IPCSkeleton::GetCallingTokenID());
199     appIdentity.SetUid(IPCSkeleton::GetCallingUid());
200     return locationDataManager_->RegisterSwitchCallback(callback, appIdentity) == ERRCODE_SUCCESS;
201 }
202 
UnregisterSwitchCallback(const sptr<IRemoteObject> & callback)203 bool LocatorImpl::UnregisterSwitchCallback(const sptr<IRemoteObject>& callback)
204 {
205     if (locationDataManager_ == nullptr) {
206         return false;
207     }
208     return locationDataManager_->UnregisterSwitchCallback(callback) == ERRCODE_SUCCESS;
209 }
210 
RegisterGnssStatusCallback(const sptr<IRemoteObject> & callback,pid_t uid)211 bool LocatorImpl::RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid)
212 {
213     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
214         return false;
215     }
216     sptr<LocatorProxy> proxy = GetProxy();
217     if (proxy == nullptr) {
218         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
219         return false;
220     }
221     if (IsSatelliteStatusChangeCallbackRegistered(callback)) {
222         LBSLOGE(LOCATOR_STANDARD, "%{public}s callback has registered.", __func__);
223         return false;
224     }
225     AddSatelliteStatusChangeCallBack(callback);
226     proxy->RegisterGnssStatusCallback(callback, DEFAULT_UID);
227     return true;
228 }
229 
UnregisterGnssStatusCallback(const sptr<IRemoteObject> & callback)230 bool LocatorImpl::UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback)
231 {
232     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
233         return false;
234     }
235     sptr<LocatorProxy> proxy = GetProxy();
236     if (proxy == nullptr) {
237         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
238         return false;
239     }
240     proxy->UnregisterGnssStatusCallback(callback);
241     RemoveSatelliteStatusChangeCallBack(callback);
242     return true;
243 }
244 
RegisterNmeaMessageCallback(const sptr<IRemoteObject> & callback,pid_t uid)245 bool LocatorImpl::RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid)
246 {
247     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
248         return false;
249     }
250     sptr<LocatorProxy> proxy = GetProxy();
251     if (proxy == nullptr) {
252         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
253         return false;
254     }
255     if (IsNmeaCallbackRegistered(callback)) {
256         LBSLOGE(LOCATOR_STANDARD, "%{public}s callback has registered.", __func__);
257         return false;
258     }
259     AddNmeaCallBack(callback);
260     proxy->RegisterNmeaMessageCallback(callback, DEFAULT_UID);
261     return true;
262 }
263 
UnregisterNmeaMessageCallback(const sptr<IRemoteObject> & callback)264 bool LocatorImpl::UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback)
265 {
266     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
267         return false;
268     }
269     sptr<LocatorProxy> proxy = GetProxy();
270     if (proxy == nullptr) {
271         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
272         return false;
273     }
274     proxy->UnregisterNmeaMessageCallback(callback);
275     RemoveNmeaCallBack(callback);
276     return true;
277 }
278 
RegisterCountryCodeCallback(const sptr<IRemoteObject> & callback,pid_t uid)279 bool LocatorImpl::RegisterCountryCodeCallback(const sptr<IRemoteObject>& callback, pid_t uid)
280 {
281     auto countryCodeManager = CountryCodeManager::GetInstance();
282     if (countryCodeManager == nullptr) {
283         LBSLOGE(LOCATOR, "%{public}s countryCodeManager is nullptr", __func__);
284         return false;
285     }
286     AppIdentity identity;
287     identity.SetPid(IPCSkeleton::GetCallingPid());
288     identity.SetUid(IPCSkeleton::GetCallingUid());
289     identity.SetTokenId(IPCSkeleton::GetCallingTokenID());
290     identity.SetTokenIdEx(IPCSkeleton::GetCallingFullTokenID());
291     identity.SetFirstTokenId(IPCSkeleton::GetFirstTokenID());
292     std::string bundleName = "";
293     if (!CommonUtils::GetBundleNameByUid(identity.GetUid(), bundleName)) {
294         LBSLOGD(LOCATOR, "Fail to Get bundle name: uid = %{public}d.", identity.GetUid());
295     }
296     identity.SetBundleName(bundleName);
297     countryCodeManager->RegisterCountryCodeCallback(callback, identity);
298     return true;
299 }
300 
UnregisterCountryCodeCallback(const sptr<IRemoteObject> & callback)301 bool LocatorImpl::UnregisterCountryCodeCallback(const sptr<IRemoteObject>& callback)
302 {
303     auto countryCodeManager = CountryCodeManager::GetInstance();
304     if (countryCodeManager == nullptr) {
305         LBSLOGE(LOCATOR, "%{public}s countryCodeManager is nullptr", __func__);
306         return false;
307     }
308     countryCodeManager->UnregisterCountryCodeCallback(callback);
309     return true;
310 }
311 
RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest> & request,sptr<ICachedLocationsCallback> & callback)312 void LocatorImpl::RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest>& request,
313     sptr<ICachedLocationsCallback>& callback)
314 {
315     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
316         return;
317     }
318     sptr<LocatorProxy> proxy = GetProxy();
319     if (proxy == nullptr) {
320         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
321         return;
322     }
323     proxy->RegisterCachedLocationCallback(request, callback, "location.ILocator");
324 }
325 
UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback> & callback)326 void LocatorImpl::UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback>& callback)
327 {
328     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
329         return;
330     }
331     sptr<LocatorProxy> proxy = GetProxy();
332     if (proxy == nullptr) {
333         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
334         return;
335     }
336     proxy->UnregisterCachedLocationCallback(callback);
337 }
338 
IsGeoServiceAvailable()339 bool LocatorImpl::IsGeoServiceAvailable()
340 {
341     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
342         return false;
343     }
344     bool result = false;
345     MessageParcel reply;
346     sptr<LocatorProxy> proxy = GetProxy();
347     if (proxy == nullptr) {
348         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
349         return false;
350     }
351     proxy->IsGeoConvertAvailable(reply);
352     int exception = reply.ReadInt32();
353     if (exception == ERRCODE_PERMISSION_DENIED) {
354         LBSLOGE(LOCATOR_STANDARD, "can not get cached location without location permission.");
355     } else if (exception != ERRCODE_SUCCESS) {
356         LBSLOGE(LOCATOR_STANDARD, "cause some exception happened in lower service.");
357     } else {
358         result = reply.ReadBool();
359     }
360     return result;
361 }
362 
GetAddressByCoordinate(MessageParcel & data,std::list<std::shared_ptr<GeoAddress>> & replyList)363 void LocatorImpl::GetAddressByCoordinate(MessageParcel &data, std::list<std::shared_ptr<GeoAddress>>& replyList)
364 {
365     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
366         return;
367     }
368     sptr<LocatorProxy> proxy = GetProxy();
369     if (proxy == nullptr) {
370         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
371         return;
372     }
373     MessageParcel reply;
374     sptr<GeoConvertCallbackHost> callback = new (std::nothrow) GeoConvertCallbackHost();
375     if (callback == nullptr) {
376         LBSLOGE(LOCATOR_STANDARD, "can not get valid callback.");
377         return;
378     }
379     data.WriteRemoteObject(callback->AsObject());
380     proxy->GetAddressByCoordinate(data, reply);
381     int exception = reply.ReadInt32();
382     if (exception == ERRCODE_PERMISSION_DENIED) {
383         LBSLOGE(LOCATOR_STANDARD, "can not get cached location without location permission.");
384     } else if (exception != ERRCODE_SUCCESS) {
385         LBSLOGE(LOCATOR_STANDARD, "cause some exception happened in lower service.");
386     }
387     replyList = callback->GetResult();
388     uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
389     uint64_t tokenIdEx = IPCSkeleton::GetCallingFullTokenID();
390     bool flag = false;
391     if (PermissionManager::CheckSystemPermission(tokenId, tokenIdEx)) {
392         flag = true;
393     }
394     for (auto iter = replyList.begin(); iter != replyList.end(); ++iter) {
395         auto geoAddress = *iter;
396         geoAddress->SetIsSystemApp(flag);
397     }
398 }
399 
GetAddressByLocationName(MessageParcel & data,std::list<std::shared_ptr<GeoAddress>> & replyList)400 void LocatorImpl::GetAddressByLocationName(MessageParcel &data, std::list<std::shared_ptr<GeoAddress>>& replyList)
401 {
402     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
403         return;
404     }
405     sptr<LocatorProxy> proxy = GetProxy();
406     if (proxy == nullptr) {
407         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
408         return;
409     }
410     MessageParcel reply;
411     sptr<GeoConvertCallbackHost> callback = new (std::nothrow) GeoConvertCallbackHost();
412     if (callback == nullptr) {
413         LBSLOGE(LOCATOR_STANDARD, "can not get valid callback.");
414         reply.WriteInt32(ERRCODE_GEOCODING_FAIL);
415         return;
416     }
417     data.WriteRemoteObject(callback->AsObject());
418     proxy->GetAddressByLocationName(data, reply);
419     int exception = reply.ReadInt32();
420     if (exception == ERRCODE_PERMISSION_DENIED) {
421         LBSLOGE(LOCATOR_STANDARD, "can not get cached location without location permission.");
422     } else if (exception != ERRCODE_SUCCESS) {
423         LBSLOGE(LOCATOR_STANDARD, "cause some exception happened in lower service.");
424     }
425     replyList = callback->GetResult();
426     uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
427     uint64_t tokenIdEx = IPCSkeleton::GetCallingFullTokenID();
428     bool flag = false;
429     if (PermissionManager::CheckSystemPermission(tokenId, tokenIdEx)) {
430         flag = true;
431     }
432     for (auto iter = replyList.begin(); iter != replyList.end(); ++iter) {
433         auto geoAddress = *iter;
434         geoAddress->SetIsSystemApp(flag);
435     }
436 }
437 
IsLocationPrivacyConfirmed(const int type)438 bool LocatorImpl::IsLocationPrivacyConfirmed(const int type)
439 {
440     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
441         return false;
442     }
443     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::IsLocationPrivacyConfirmed()");
444     sptr<LocatorProxy> proxy = GetProxy();
445     if (proxy == nullptr) {
446         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
447         return false;
448     }
449     bool flag = proxy->IsLocationPrivacyConfirmed(type);
450     return flag;
451 }
452 
SetLocationPrivacyConfirmStatus(const int type,bool isConfirmed)453 int LocatorImpl::SetLocationPrivacyConfirmStatus(const int type, bool isConfirmed)
454 {
455     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
456         return false;
457     }
458     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SetLocationPrivacyConfirmStatus()");
459     sptr<LocatorProxy> proxy = GetProxy();
460     if (proxy == nullptr) {
461         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
462         return false;
463     }
464     int flag = proxy->SetLocationPrivacyConfirmStatus(type, isConfirmed);
465     return flag;
466 }
467 
GetCachedGnssLocationsSize()468 int LocatorImpl::GetCachedGnssLocationsSize()
469 {
470     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
471         return -1;
472     }
473     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetCachedGnssLocationsSize()");
474     sptr<LocatorProxy> proxy = GetProxy();
475     if (proxy == nullptr) {
476         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
477         return false;
478     }
479     int size = proxy->GetCachedGnssLocationsSize();
480     return size;
481 }
482 
FlushCachedGnssLocations()483 int LocatorImpl::FlushCachedGnssLocations()
484 {
485     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
486         return -1;
487     }
488     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::FlushCachedGnssLocations()");
489     sptr<LocatorProxy> proxy = GetProxy();
490     if (proxy == nullptr) {
491         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
492         return false;
493     }
494     int res = proxy->FlushCachedGnssLocations();
495     return res;
496 }
497 
SendCommand(std::unique_ptr<LocationCommand> & commands)498 bool LocatorImpl::SendCommand(std::unique_ptr<LocationCommand>& commands)
499 {
500     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
501         return false;
502     }
503     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SendCommand()");
504     sptr<LocatorProxy> proxy = GetProxy();
505     if (proxy == nullptr) {
506         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
507         return false;
508     }
509     proxy->SendCommand(commands);
510     return true;
511 }
512 
GetIsoCountryCode()513 std::shared_ptr<CountryCode> LocatorImpl::GetIsoCountryCode()
514 {
515     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetIsoCountryCode()");
516     auto countryCodeManager = CountryCodeManager::GetInstance();
517     if (countryCodeManager == nullptr) {
518         LBSLOGE(LOCATOR, "%{public}s countryCodeManager is nullptr", __func__);
519         return nullptr;
520     }
521     return countryCodeManager->GetIsoCountryCode();
522 }
523 
EnableLocationMock()524 bool LocatorImpl::EnableLocationMock()
525 {
526     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
527         return false;
528     }
529     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::EnableLocationMock()");
530     sptr<LocatorProxy> proxy = GetProxy();
531     if (proxy == nullptr) {
532         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
533         return false;
534     }
535     bool flag = proxy->EnableLocationMock();
536     return flag;
537 }
538 
DisableLocationMock()539 bool LocatorImpl::DisableLocationMock()
540 {
541     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
542         return false;
543     }
544     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::DisableLocationMock()");
545     sptr<LocatorProxy> proxy = GetProxy();
546     if (proxy == nullptr) {
547         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
548         return false;
549     }
550     bool flag = proxy->DisableLocationMock();
551     return flag;
552 }
553 
SetMockedLocations(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)554 bool LocatorImpl::SetMockedLocations(
555     const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
556 {
557     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
558         return false;
559     }
560     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SetMockedLocations()");
561     sptr<LocatorProxy> proxy = GetProxy();
562     if (proxy == nullptr) {
563         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
564         return false;
565     }
566     bool flag = proxy->SetMockedLocations(timeInterval, location);
567     return flag;
568 }
569 
EnableReverseGeocodingMock()570 bool LocatorImpl::EnableReverseGeocodingMock()
571 {
572     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
573         return false;
574     }
575     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::EnableReverseGeocodingMock()");
576     sptr<LocatorProxy> proxy = GetProxy();
577     if (proxy == nullptr) {
578         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
579         return false;
580     }
581     bool flag = proxy->EnableReverseGeocodingMock();
582     return flag;
583 }
584 
DisableReverseGeocodingMock()585 bool LocatorImpl::DisableReverseGeocodingMock()
586 {
587     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
588         return false;
589     }
590     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::DisableReverseGeocodingMock()");
591     sptr<LocatorProxy> proxy = GetProxy();
592     if (proxy == nullptr) {
593         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
594         return false;
595     }
596     bool flag = proxy->DisableReverseGeocodingMock();
597     return flag;
598 }
599 
SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>> & mockInfo)600 bool LocatorImpl::SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo)
601 {
602     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
603         return false;
604     }
605     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SetReverseGeocodingMockInfo()");
606     sptr<LocatorProxy> proxy = GetProxy();
607     if (proxy == nullptr) {
608         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
609         return false;
610     }
611     bool flag = proxy->SetReverseGeocodingMockInfo(mockInfo);
612     return flag;
613 }
614 
IsLocationEnabledV9(bool & isEnabled)615 LocationErrCode LocatorImpl::IsLocationEnabledV9(bool &isEnabled)
616 {
617     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::IsLocationEnabledV9()");
618     int32_t state = DEFAULT_SWITCH_STATE;
619     state = LocationDataRdbManager::GetSwitchStateFromSysparaForCurrentUser();
620     if (state == DISABLED || state == ENABLED) {
621         isEnabled = (state == ENABLED);
622         return ERRCODE_SUCCESS;
623     }
624     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
625         return ERRCODE_SERVICE_UNAVAILABLE;
626     }
627     sptr<LocatorProxy> proxy = GetProxy();
628     if (proxy == nullptr) {
629         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
630         return ERRCODE_SERVICE_UNAVAILABLE;
631     }
632     state = proxy->GetSwitchState();
633     isEnabled = (state == ENABLED);
634     return ERRCODE_SUCCESS;
635 }
636 
IsLocationEnabledForUser(bool & isEnabled,int32_t userId)637 LocationErrCode LocatorImpl::IsLocationEnabledForUser(bool &isEnabled, int32_t userId)
638 {
639     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::IsLocationEnabledV9()");
640     int32_t state = DEFAULT_SWITCH_STATE;
641     state = LocationDataRdbManager::GetSwitchStateFromSysparaForUser(userId);
642     if (state == DISABLED || state == ENABLED) {
643         isEnabled = (state == ENABLED);
644         return ERRCODE_SUCCESS;
645     }
646     auto ret = LocationDataRdbManager::GetSwitchStateFromDbForUser(state, userId);
647     if (ret != ERRCODE_SUCCESS) {
648         return ERRCODE_SERVICE_UNAVAILABLE;
649     }
650     isEnabled = (state == ENABLED);
651     return ERRCODE_SUCCESS;
652 }
653 
CheckEdmPolicy(bool enable)654 LocationErrCode LocatorImpl::CheckEdmPolicy(bool enable)
655 {
656     std::string policy = "";
657     bool res = CommonUtils::GetEdmPolicy(policy);
658     if (!res || policy.empty()) {
659         LBSLOGE(LOCATOR_STANDARD, "get edm policy failed!");
660         return ERRCODE_SUCCESS;
661     }
662     if (policy == "force_open" && enable == false) {
663         LBSLOGE(LOCATOR_STANDARD, "disable location switch is not allowed");
664         return ERRCODE_EDM_POLICY_ABANDON;
665     } else if (policy == "disallow" && enable == true) {
666         LBSLOGE(LOCATOR_STANDARD, "enable location switch is not allowed");
667         return ERRCODE_EDM_POLICY_ABANDON;
668     }
669     return ERRCODE_SUCCESS;
670 }
671 
672 
EnableAbilityV9(bool enable)673 LocationErrCode LocatorImpl::EnableAbilityV9(bool enable)
674 {
675     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
676         return ERRCODE_SERVICE_UNAVAILABLE;
677     }
678     LocationErrCode errorCode = CheckEdmPolicy(enable);
679     if (errorCode != ERRCODE_SUCCESS) {
680         return errorCode;
681     }
682     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::EnableAbilityV9()");
683     sptr<LocatorProxy> proxy = GetProxy();
684     if (proxy == nullptr) {
685         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
686         return ERRCODE_SERVICE_UNAVAILABLE;
687     }
688     LocationErrCode errCode = proxy->EnableAbilityV9(enable);
689     return errCode;
690 }
691 
EnableAbilityForUser(bool enable,int32_t userId)692 LocationErrCode LocatorImpl::EnableAbilityForUser(bool enable, int32_t userId)
693 {
694     if (!LocationSaLoadManager::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
695         return ERRCODE_SERVICE_UNAVAILABLE;
696     }
697     LocationErrCode errorCode = CheckEdmPolicy(enable);
698     if (errorCode != ERRCODE_SUCCESS) {
699         return errorCode;
700     }
701     sptr<LocatorProxy> proxy = GetProxy();
702     if (proxy == nullptr) {
703         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
704         return ERRCODE_SERVICE_UNAVAILABLE;
705     }
706     LocationErrCode errCode = proxy->EnableAbilityForUser(enable, userId);
707     return errCode;
708 }
709 
StartLocatingV9(std::unique_ptr<RequestConfig> & requestConfig,sptr<ILocatorCallback> & callback)710 LocationErrCode LocatorImpl::StartLocatingV9(std::unique_ptr<RequestConfig>& requestConfig,
711     sptr<ILocatorCallback>& callback)
712 {
713     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
714         return ERRCODE_SERVICE_UNAVAILABLE;
715     }
716     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::StartLocatingV9()");
717     sptr<LocatorProxy> proxy = GetProxy();
718     if (proxy == nullptr) {
719         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
720         return ERRCODE_SERVICE_UNAVAILABLE;
721     }
722     if (IsLocationCallbackRegistered(callback)) {
723         LBSLOGE(LOCATOR_STANDARD, "%{public}s locatorCallback has registered", __func__);
724         return ERRCODE_SERVICE_UNAVAILABLE;
725     }
726     AddLocationCallBack(requestConfig, callback);
727     LocationErrCode errCode = proxy->StartLocatingV9(requestConfig, callback);
728     if (errCode != ERRCODE_SUCCESS) {
729         RemoveLocationCallBack(callback);
730     }
731     return errCode;
732 }
733 
StopLocatingV9(sptr<ILocatorCallback> & callback)734 LocationErrCode LocatorImpl::StopLocatingV9(sptr<ILocatorCallback>& callback)
735 {
736     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
737         return ERRCODE_SERVICE_UNAVAILABLE;
738     }
739     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::StopLocatingV9()");
740     sptr<LocatorProxy> proxy = GetProxy();
741     if (proxy == nullptr) {
742         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
743         return ERRCODE_SERVICE_UNAVAILABLE;
744     }
745     LocationErrCode errCode = proxy->StopLocatingV9(callback);
746     RemoveLocationCallBack(callback);
747     return errCode;
748 }
749 
GetCachedLocationV9(std::unique_ptr<Location> & loc)750 LocationErrCode LocatorImpl::GetCachedLocationV9(std::unique_ptr<Location> &loc)
751 {
752     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
753         return ERRCODE_SERVICE_UNAVAILABLE;
754     }
755     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetCachedLocationV9()");
756     sptr<LocatorProxy> proxy = GetProxy();
757     if (proxy == nullptr) {
758         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
759         return ERRCODE_SERVICE_UNAVAILABLE;
760     }
761     LocationErrCode errCode = proxy->GetCacheLocationV9(loc);
762     return errCode;
763 }
764 
RegisterSwitchCallbackV9(const sptr<IRemoteObject> & callback)765 LocationErrCode LocatorImpl::RegisterSwitchCallbackV9(const sptr<IRemoteObject>& callback)
766 {
767     if (locationDataManager_ == nullptr) {
768         return ERRCODE_SERVICE_UNAVAILABLE;
769     }
770     AppIdentity appIdentity;
771     appIdentity.SetTokenId(IPCSkeleton::GetCallingTokenID());
772     appIdentity.SetUid(IPCSkeleton::GetCallingUid());
773     return locationDataManager_->
774         RegisterSwitchCallback(callback, appIdentity);
775 }
776 
UnregisterSwitchCallbackV9(const sptr<IRemoteObject> & callback)777 LocationErrCode LocatorImpl::UnregisterSwitchCallbackV9(const sptr<IRemoteObject>& callback)
778 {
779     if (locationDataManager_ == nullptr) {
780         return ERRCODE_SERVICE_UNAVAILABLE;
781     }
782     return locationDataManager_->UnregisterSwitchCallback(callback);
783 }
784 
RegisterGnssStatusCallbackV9(const sptr<IRemoteObject> & callback)785 LocationErrCode LocatorImpl::RegisterGnssStatusCallbackV9(const sptr<IRemoteObject>& callback)
786 {
787     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
788         return ERRCODE_SERVICE_UNAVAILABLE;
789     }
790     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::RegisterGnssStatusCallbackV9()");
791     sptr<LocatorProxy> proxy = GetProxy();
792     if (proxy == nullptr) {
793         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
794         return ERRCODE_SERVICE_UNAVAILABLE;
795     }
796     if (IsSatelliteStatusChangeCallbackRegistered(callback)) {
797         LBSLOGE(LOCATOR_STANDARD, "%{public}s callback has registered.", __func__);
798         return ERRCODE_SERVICE_UNAVAILABLE;
799     }
800     AddSatelliteStatusChangeCallBack(callback);
801     LocationErrCode errCode = proxy->RegisterGnssStatusCallbackV9(callback);
802     if (errCode != ERRCODE_SUCCESS) {
803         RemoveSatelliteStatusChangeCallBack(callback);
804     }
805     return errCode;
806 }
807 
UnregisterGnssStatusCallbackV9(const sptr<IRemoteObject> & callback)808 LocationErrCode LocatorImpl::UnregisterGnssStatusCallbackV9(const sptr<IRemoteObject>& callback)
809 {
810     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
811         return ERRCODE_SERVICE_UNAVAILABLE;
812     }
813     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::UnregisterGnssStatusCallbackV9()");
814     sptr<LocatorProxy> proxy = GetProxy();
815     if (proxy == nullptr) {
816         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
817         return ERRCODE_SERVICE_UNAVAILABLE;
818     }
819     LocationErrCode errCode = proxy->UnregisterGnssStatusCallbackV9(callback);
820     RemoveSatelliteStatusChangeCallBack(callback);
821     return errCode;
822 }
823 
RegisterNmeaMessageCallbackV9(const sptr<IRemoteObject> & callback)824 LocationErrCode LocatorImpl::RegisterNmeaMessageCallbackV9(const sptr<IRemoteObject>& callback)
825 {
826     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
827         return ERRCODE_SERVICE_UNAVAILABLE;
828     }
829     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::RegisterNmeaMessageCallbackV9()");
830     sptr<LocatorProxy> proxy = GetProxy();
831     if (proxy == nullptr) {
832         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
833         return ERRCODE_SERVICE_UNAVAILABLE;
834     }
835     if (IsNmeaCallbackRegistered(callback)) {
836         LBSLOGE(LOCATOR_STANDARD, "%{public}s callback has registered.", __func__);
837         return ERRCODE_SERVICE_UNAVAILABLE;
838     }
839     AddNmeaCallBack(callback);
840     LocationErrCode errCode = proxy->RegisterNmeaMessageCallbackV9(callback);
841     if (errCode != ERRCODE_SUCCESS) {
842         RemoveNmeaCallBack(callback);
843     }
844     return errCode;
845 }
846 
UnregisterNmeaMessageCallbackV9(const sptr<IRemoteObject> & callback)847 LocationErrCode LocatorImpl::UnregisterNmeaMessageCallbackV9(const sptr<IRemoteObject>& callback)
848 {
849     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
850         return ERRCODE_SERVICE_UNAVAILABLE;
851     }
852     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::UnregisterNmeaMessageCallbackV9()");
853     sptr<LocatorProxy> proxy = GetProxy();
854     if (proxy == nullptr) {
855         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
856         return ERRCODE_SERVICE_UNAVAILABLE;
857     }
858     LocationErrCode errCode = proxy->UnregisterNmeaMessageCallbackV9(callback);
859     RemoveNmeaCallBack(callback);
860     return errCode;
861 }
862 
RegisterCountryCodeCallbackV9(const sptr<IRemoteObject> & callback)863 LocationErrCode LocatorImpl::RegisterCountryCodeCallbackV9(const sptr<IRemoteObject>& callback)
864 {
865     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::RegisterCountryCodeCallbackV9()");
866     auto countryCodeManager = CountryCodeManager::GetInstance();
867     if (countryCodeManager == nullptr) {
868         LBSLOGE(LOCATOR, "%{public}s countryCodeManager is nullptr", __func__);
869         return ERRCODE_SERVICE_UNAVAILABLE;
870     }
871     AppIdentity identity;
872     identity.SetPid(IPCSkeleton::GetCallingPid());
873     identity.SetUid(IPCSkeleton::GetCallingUid());
874     identity.SetTokenId(IPCSkeleton::GetCallingTokenID());
875     identity.SetTokenIdEx(IPCSkeleton::GetCallingFullTokenID());
876     identity.SetFirstTokenId(IPCSkeleton::GetFirstTokenID());
877     std::string bundleName = "";
878     if (!CommonUtils::GetBundleNameByUid(identity.GetUid(), bundleName)) {
879         LBSLOGD(LOCATOR, "Fail to Get bundle name: uid = %{public}d.", identity.GetUid());
880     }
881     identity.SetBundleName(bundleName);
882     countryCodeManager->RegisterCountryCodeCallback(callback, identity);
883     return ERRCODE_SUCCESS;
884 }
885 
UnregisterCountryCodeCallbackV9(const sptr<IRemoteObject> & callback)886 LocationErrCode LocatorImpl::UnregisterCountryCodeCallbackV9(const sptr<IRemoteObject>& callback)
887 {
888     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::UnregisterCountryCodeCallbackV9()");
889     auto countryCodeManager = CountryCodeManager::GetInstance();
890     if (countryCodeManager == nullptr) {
891         LBSLOGE(LOCATOR, "%{public}s countryCodeManager is nullptr", __func__);
892         return ERRCODE_SERVICE_UNAVAILABLE;
893     }
894     countryCodeManager->UnregisterCountryCodeCallback(callback);
895     return ERRCODE_SUCCESS;
896 }
897 
RegisterCachedLocationCallbackV9(std::unique_ptr<CachedGnssLocationsRequest> & request,sptr<ICachedLocationsCallback> & callback)898 LocationErrCode LocatorImpl::RegisterCachedLocationCallbackV9(std::unique_ptr<CachedGnssLocationsRequest>& request,
899     sptr<ICachedLocationsCallback>& callback)
900 {
901     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
902         return ERRCODE_SERVICE_UNAVAILABLE;
903     }
904     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::RegisterCachedLocationCallbackV9()");
905     sptr<LocatorProxy> proxy = GetProxy();
906     if (proxy == nullptr) {
907         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
908         return ERRCODE_SERVICE_UNAVAILABLE;
909     }
910     LocationErrCode errCode = proxy->RegisterCachedLocationCallbackV9(request, callback, "location.ILocator");
911     return errCode;
912 }
913 
UnregisterCachedLocationCallbackV9(sptr<ICachedLocationsCallback> & callback)914 LocationErrCode LocatorImpl::UnregisterCachedLocationCallbackV9(sptr<ICachedLocationsCallback>& callback)
915 {
916     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
917         return ERRCODE_SERVICE_UNAVAILABLE;
918     }
919     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::UnregisterCachedLocationCallbackV9()");
920     sptr<LocatorProxy> proxy = GetProxy();
921     if (proxy == nullptr) {
922         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
923         return ERRCODE_SERVICE_UNAVAILABLE;
924     }
925     LocationErrCode errCode = proxy->UnregisterCachedLocationCallbackV9(callback);
926     return errCode;
927 }
928 
IsGeoServiceAvailableV9(bool & isAvailable)929 LocationErrCode LocatorImpl::IsGeoServiceAvailableV9(bool &isAvailable)
930 {
931     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
932         return ERRCODE_SERVICE_UNAVAILABLE;
933     }
934     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::IsGeoServiceAvailableV9()");
935     sptr<LocatorProxy> proxy = GetProxy();
936     if (proxy == nullptr) {
937         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
938         return ERRCODE_SERVICE_UNAVAILABLE;
939     }
940     LocationErrCode errCode = proxy->IsGeoConvertAvailableV9(isAvailable);
941     return errCode;
942 }
943 
GetAddressByCoordinateV9(MessageParcel & data,std::list<std::shared_ptr<GeoAddress>> & replyList)944 LocationErrCode LocatorImpl::GetAddressByCoordinateV9(MessageParcel &data,
945     std::list<std::shared_ptr<GeoAddress>>& replyList)
946 {
947     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
948         return ERRCODE_SERVICE_UNAVAILABLE;
949     }
950     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetAddressByCoordinateV9()");
951     sptr<LocatorProxy> proxy = GetProxy();
952     if (proxy == nullptr) {
953         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
954         return ERRCODE_SERVICE_UNAVAILABLE;
955     }
956     sptr<GeoConvertCallbackHost> callback = new (std::nothrow) GeoConvertCallbackHost();
957     if (callback == nullptr) {
958         LBSLOGE(LOCATOR_STANDARD, "can not get valid callback.");
959         return ERRCODE_REVERSE_GEOCODING_FAIL;
960     }
961     data.WriteRemoteObject(callback->AsObject());
962     LocationErrCode errCode = proxy->GetAddressByCoordinateV9(data, replyList);
963     replyList = callback->GetResult();
964     if (replyList.size() == 0) {
965         return ERRCODE_REVERSE_GEOCODING_FAIL;
966     }
967     uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
968     uint64_t tokenIdEx = IPCSkeleton::GetCallingFullTokenID();
969     bool flag = false;
970     if (PermissionManager::CheckSystemPermission(tokenId, tokenIdEx)) {
971         flag = true;
972     }
973     for (auto iter = replyList.begin(); iter != replyList.end(); ++iter) {
974         auto geoAddress = *iter;
975         geoAddress->SetIsSystemApp(flag);
976     }
977     return errCode;
978 }
979 
GetAddressByLocationNameV9(MessageParcel & data,std::list<std::shared_ptr<GeoAddress>> & replyList)980 LocationErrCode LocatorImpl::GetAddressByLocationNameV9(MessageParcel &data,
981     std::list<std::shared_ptr<GeoAddress>>& replyList)
982 {
983     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
984         return ERRCODE_SERVICE_UNAVAILABLE;
985     }
986     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetAddressByLocationNameV9()");
987     sptr<LocatorProxy> proxy = GetProxy();
988     if (proxy == nullptr) {
989         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
990         return ERRCODE_SERVICE_UNAVAILABLE;
991     }
992     MessageParcel reply;
993     sptr<GeoConvertCallbackHost> callback = new (std::nothrow) GeoConvertCallbackHost();
994     if (callback == nullptr) {
995         LBSLOGE(LOCATOR_STANDARD, "can not get valid callback.");
996         reply.WriteInt32(ERRCODE_GEOCODING_FAIL);
997         return ERRCODE_GEOCODING_FAIL;
998     }
999     data.WriteRemoteObject(callback->AsObject());
1000     LocationErrCode errCode = proxy->GetAddressByLocationNameV9(data, replyList);
1001     replyList = callback->GetResult();
1002     if (replyList.size() == 0) {
1003         return ERRCODE_GEOCODING_FAIL;
1004     }
1005     uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
1006     uint64_t tokenIdEx = IPCSkeleton::GetCallingFullTokenID();
1007     bool flag = false;
1008     if (PermissionManager::CheckSystemPermission(tokenId, tokenIdEx)) {
1009         flag = true;
1010     }
1011     for (auto iter = replyList.begin(); iter != replyList.end(); ++iter) {
1012         auto geoAddress = *iter;
1013         geoAddress->SetIsSystemApp(flag);
1014     }
1015     return errCode;
1016 }
1017 
IsLocationPrivacyConfirmedV9(const int type,bool & isConfirmed)1018 LocationErrCode LocatorImpl::IsLocationPrivacyConfirmedV9(const int type, bool &isConfirmed)
1019 {
1020     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1021         return ERRCODE_SERVICE_UNAVAILABLE;
1022     }
1023     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::IsLocationPrivacyConfirmedV9()");
1024     sptr<LocatorProxy> proxy = GetProxy();
1025     if (proxy == nullptr) {
1026         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1027         return ERRCODE_SERVICE_UNAVAILABLE;
1028     }
1029     LocationErrCode errCode = proxy->IsLocationPrivacyConfirmedV9(type, isConfirmed);
1030     return errCode;
1031 }
1032 
SetLocationPrivacyConfirmStatusV9(const int type,bool isConfirmed)1033 LocationErrCode LocatorImpl::SetLocationPrivacyConfirmStatusV9(const int type, bool isConfirmed)
1034 {
1035     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1036         return ERRCODE_SERVICE_UNAVAILABLE;
1037     }
1038     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SetLocationPrivacyConfirmStatusV9()");
1039     sptr<LocatorProxy> proxy = GetProxy();
1040     if (proxy == nullptr) {
1041         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1042         return ERRCODE_SERVICE_UNAVAILABLE;
1043     }
1044     LocationErrCode errCode = proxy->SetLocationPrivacyConfirmStatusV9(type, isConfirmed);
1045     return errCode;
1046 }
1047 
GetCachedGnssLocationsSizeV9(int & size)1048 LocationErrCode LocatorImpl::GetCachedGnssLocationsSizeV9(int &size)
1049 {
1050     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1051         return ERRCODE_SERVICE_UNAVAILABLE;
1052     }
1053     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetCachedGnssLocationsSizeV9()");
1054     sptr<LocatorProxy> proxy = GetProxy();
1055     if (proxy == nullptr) {
1056         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1057         return ERRCODE_SERVICE_UNAVAILABLE;
1058     }
1059     LocationErrCode errCode = proxy->GetCachedGnssLocationsSizeV9(size);
1060     return errCode;
1061 }
1062 
FlushCachedGnssLocationsV9()1063 LocationErrCode LocatorImpl::FlushCachedGnssLocationsV9()
1064 {
1065     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1066         return ERRCODE_SERVICE_UNAVAILABLE;
1067     }
1068     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::FlushCachedGnssLocationsV9()");
1069     sptr<LocatorProxy> proxy = GetProxy();
1070     if (proxy == nullptr) {
1071         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1072         return ERRCODE_SERVICE_UNAVAILABLE;
1073     }
1074     LocationErrCode errCode = proxy->FlushCachedGnssLocationsV9();
1075     return errCode;
1076 }
1077 
SendCommandV9(std::unique_ptr<LocationCommand> & commands)1078 LocationErrCode LocatorImpl::SendCommandV9(std::unique_ptr<LocationCommand>& commands)
1079 {
1080     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1081         return ERRCODE_SERVICE_UNAVAILABLE;
1082     }
1083     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SendCommandV9()");
1084     sptr<LocatorProxy> proxy = GetProxy();
1085     if (proxy == nullptr) {
1086         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1087         return ERRCODE_SERVICE_UNAVAILABLE;
1088     }
1089     LocationErrCode errCode = proxy->SendCommandV9(commands);
1090     return errCode;
1091 }
1092 
GetIsoCountryCodeV9(std::shared_ptr<CountryCode> & countryCode)1093 LocationErrCode LocatorImpl::GetIsoCountryCodeV9(std::shared_ptr<CountryCode>& countryCode)
1094 {
1095     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetIsoCountryCodeV9()");
1096     auto countryCodeManager = CountryCodeManager::GetInstance();
1097     if (countryCodeManager == nullptr) {
1098         LBSLOGE(LOCATOR, "%{public}s countryCodeManager is nullptr", __func__);
1099         return ERRCODE_SERVICE_UNAVAILABLE;
1100     }
1101     countryCode = countryCodeManager->GetIsoCountryCode();
1102     return ERRCODE_SUCCESS;
1103 }
1104 
EnableLocationMockV9()1105 LocationErrCode LocatorImpl::EnableLocationMockV9()
1106 {
1107     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1108         return ERRCODE_SERVICE_UNAVAILABLE;
1109     }
1110     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::EnableLocationMockV9()");
1111     sptr<LocatorProxy> proxy = GetProxy();
1112     if (proxy == nullptr) {
1113         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1114         return ERRCODE_SERVICE_UNAVAILABLE;
1115     }
1116     LocationErrCode errCode = proxy->EnableLocationMockV9();
1117     return errCode;
1118 }
1119 
DisableLocationMockV9()1120 LocationErrCode LocatorImpl::DisableLocationMockV9()
1121 {
1122     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1123         return ERRCODE_SERVICE_UNAVAILABLE;
1124     }
1125     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::DisableLocationMockV9()");
1126     sptr<LocatorProxy> proxy = GetProxy();
1127     if (proxy == nullptr) {
1128         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1129         return ERRCODE_SERVICE_UNAVAILABLE;
1130     }
1131     LocationErrCode errCode = proxy->DisableLocationMockV9();
1132     return errCode;
1133 }
1134 
SetMockedLocationsV9(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)1135 LocationErrCode LocatorImpl::SetMockedLocationsV9(
1136     const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
1137 {
1138     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1139         return ERRCODE_SERVICE_UNAVAILABLE;
1140     }
1141     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SetMockedLocationsV9()");
1142     sptr<LocatorProxy> proxy = GetProxy();
1143     if (proxy == nullptr) {
1144         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1145         return ERRCODE_SERVICE_UNAVAILABLE;
1146     }
1147     LocationErrCode errCode = proxy->SetMockedLocationsV9(timeInterval, location);
1148     return errCode;
1149 }
1150 
EnableReverseGeocodingMockV9()1151 LocationErrCode LocatorImpl::EnableReverseGeocodingMockV9()
1152 {
1153     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1154         return ERRCODE_SERVICE_UNAVAILABLE;
1155     }
1156     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::EnableReverseGeocodingMockV9()");
1157     sptr<LocatorProxy> proxy = GetProxy();
1158     if (proxy == nullptr) {
1159         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1160         return ERRCODE_SERVICE_UNAVAILABLE;
1161     }
1162     LocationErrCode errCode = proxy->EnableReverseGeocodingMockV9();
1163     return errCode;
1164 }
1165 
DisableReverseGeocodingMockV9()1166 LocationErrCode LocatorImpl::DisableReverseGeocodingMockV9()
1167 {
1168     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1169         return ERRCODE_SERVICE_UNAVAILABLE;
1170     }
1171     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::DisableReverseGeocodingMockV9()");
1172     sptr<LocatorProxy> proxy = GetProxy();
1173     if (proxy == nullptr) {
1174         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1175         return ERRCODE_SERVICE_UNAVAILABLE;
1176     }
1177     LocationErrCode errCode = proxy->DisableReverseGeocodingMockV9();
1178     return errCode;
1179 }
1180 
SetReverseGeocodingMockInfoV9(std::vector<std::shared_ptr<GeocodingMockInfo>> & mockInfo)1181 LocationErrCode LocatorImpl::SetReverseGeocodingMockInfoV9(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo)
1182 {
1183     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1184         return ERRCODE_SERVICE_UNAVAILABLE;
1185     }
1186     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SetReverseGeocodingMockInfoV9()");
1187     sptr<LocatorProxy> proxy = GetProxy();
1188     if (proxy == nullptr) {
1189         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1190         return ERRCODE_SERVICE_UNAVAILABLE;
1191     }
1192     LocationErrCode errCode = proxy->SetReverseGeocodingMockInfoV9(mockInfo);
1193     return errCode;
1194 }
1195 
ProxyForFreeze(std::set<int> pidList,bool isProxy)1196 LocationErrCode LocatorImpl::ProxyForFreeze(std::set<int> pidList, bool isProxy)
1197 {
1198     if (!LocationSaLoadManager::CheckIfSystemAbilityAvailable(LOCATION_LOCATOR_SA_ID)) {
1199         LBSLOGI(LOCATOR_STANDARD, "%{public}s locator sa is not available.", __func__);
1200         isServerExist_ = false;
1201         return ERRCODE_SUCCESS;
1202     }
1203     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1204         LBSLOGE(LOCATOR_STANDARD, "%{public}s init locator sa failed", __func__);
1205         isServerExist_ = false;
1206         return ERRCODE_SERVICE_UNAVAILABLE;
1207     }
1208     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::ProxyForFreeze()");
1209     sptr<LocatorProxy> proxy = GetProxy();
1210     if (proxy == nullptr) {
1211         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1212         return ERRCODE_SERVICE_UNAVAILABLE;
1213     }
1214     LocationErrCode errCode = proxy->ProxyForFreeze(pidList, isProxy);
1215     return errCode;
1216 }
1217 
ResetAllProxy()1218 LocationErrCode LocatorImpl::ResetAllProxy()
1219 {
1220     if (!LocationSaLoadManager::CheckIfSystemAbilityAvailable(LOCATION_LOCATOR_SA_ID)) {
1221         LBSLOGI(LOCATOR_STANDARD, "%{public}s, no need reset proxy", __func__);
1222         isServerExist_ = false;
1223         return ERRCODE_SUCCESS;
1224     }
1225     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1226         isServerExist_ = false;
1227         return ERRCODE_SERVICE_UNAVAILABLE;
1228     }
1229     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::ResetAllProxy()");
1230     sptr<LocatorProxy> proxy = GetProxy();
1231     if (proxy == nullptr) {
1232         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1233         return ERRCODE_SERVICE_UNAVAILABLE;
1234     }
1235     LocationErrCode errCode = proxy->ResetAllProxy();
1236     return errCode;
1237 }
1238 
RegisterLocatingRequiredDataCallback(std::unique_ptr<LocatingRequiredDataConfig> & dataConfig,sptr<ILocatingRequiredDataCallback> & callback)1239 LocationErrCode LocatorImpl::RegisterLocatingRequiredDataCallback(
1240     std::unique_ptr<LocatingRequiredDataConfig>& dataConfig, sptr<ILocatingRequiredDataCallback>& callback)
1241 {
1242     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1243         return ERRCODE_SERVICE_UNAVAILABLE;
1244     }
1245     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::%{public}s", __func__);
1246     sptr<LocatorProxy> proxy = GetProxy();
1247     if (proxy == nullptr) {
1248         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1249         return ERRCODE_SERVICE_UNAVAILABLE;
1250     }
1251     return proxy->RegisterLocatingRequiredDataCallback(dataConfig, callback);
1252 }
1253 
UnRegisterLocatingRequiredDataCallback(sptr<ILocatingRequiredDataCallback> & callback)1254 LocationErrCode LocatorImpl::UnRegisterLocatingRequiredDataCallback(sptr<ILocatingRequiredDataCallback>& callback)
1255 {
1256     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1257         return ERRCODE_SERVICE_UNAVAILABLE;
1258     }
1259     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::%{public}s", __func__);
1260     sptr<LocatorProxy> proxy = GetProxy();
1261     if (proxy == nullptr) {
1262         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1263         return ERRCODE_SERVICE_UNAVAILABLE;
1264     }
1265     return proxy->UnRegisterLocatingRequiredDataCallback(callback);
1266 }
1267 
SubscribeLocationError(sptr<ILocatorCallback> & callback)1268 LocationErrCode LocatorImpl::SubscribeLocationError(sptr<ILocatorCallback>& callback)
1269 {
1270     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1271         return ERRCODE_SERVICE_UNAVAILABLE;
1272     }
1273     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::StartLocatingV9()");
1274     sptr<LocatorProxy> proxy = GetProxy();
1275     if (proxy == nullptr) {
1276         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1277         return ERRCODE_SERVICE_UNAVAILABLE;
1278     }
1279     LocationErrCode errCode = proxy->SubscribeLocationError(callback);
1280     if (errCode != ERRCODE_SUCCESS) {
1281         LBSLOGE(LOCATOR_STANDARD, "SubscribeLocationError failed.");
1282     }
1283     return errCode;
1284 }
1285 
UnSubscribeLocationError(sptr<ILocatorCallback> & callback)1286 LocationErrCode LocatorImpl::UnSubscribeLocationError(sptr<ILocatorCallback>& callback)
1287 {
1288     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1289         return ERRCODE_SERVICE_UNAVAILABLE;
1290     }
1291     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::StopLocatingV9()");
1292     sptr<LocatorProxy> proxy = GetProxy();
1293     if (proxy == nullptr) {
1294         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1295         return ERRCODE_SERVICE_UNAVAILABLE;
1296     }
1297     LocationErrCode errCode = proxy->UnSubscribeLocationError(callback);
1298     if (errCode != ERRCODE_SUCCESS) {
1299         LBSLOGE(LOCATOR_STANDARD, "UnSubscribeLocationError failed.");
1300     }
1301     return errCode;
1302 }
1303 
GetCurrentWifiBssidForLocating(std::string & bssid)1304 LocationErrCode LocatorImpl::GetCurrentWifiBssidForLocating(std::string& bssid)
1305 {
1306     if (!SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1307         return ERRCODE_SERVICE_UNAVAILABLE;
1308     }
1309     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetCurrentWifiBssidForLocating()");
1310     sptr<LocatorProxy> proxy = GetProxy();
1311     if (proxy == nullptr) {
1312         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1313         return ERRCODE_SERVICE_UNAVAILABLE;
1314     }
1315     LocationErrCode errCode = proxy->GetCurrentWifiBssidForLocating(bssid);
1316     return errCode;
1317 }
1318 
ResetLocatorProxy(const wptr<IRemoteObject> & remote)1319 void LocatorImpl::ResetLocatorProxy(const wptr<IRemoteObject> &remote)
1320 {
1321     if (remote == nullptr) {
1322         LBSLOGE(LOCATOR_STANDARD, "%{public}s: remote is nullptr.", __func__);
1323         return;
1324     }
1325     if (client_ == nullptr || !isServerExist_) {
1326         LBSLOGE(LOCATOR_STANDARD, "%{public}s: proxy is nullptr.", __func__);
1327         return;
1328     }
1329     if (remote.promote() != nullptr) {
1330         remote.promote()->RemoveDeathRecipient(recipient_);
1331     }
1332     isServerExist_ = false;
1333     if (g_callbackResumer != nullptr && !IsCallbackResuming()) {
1334         // only the first request will be handled
1335         UpdateCallbackResumingState(true);
1336         // wait for remote died finished
1337         std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS));
1338         if (HasGnssNetworkRequest() && SaLoadWithStatistic::InitLocationSa(LOCATION_LOCATOR_SA_ID)) {
1339             g_callbackResumer->ResumeCallback();
1340         }
1341         UpdateCallbackResumingState(false);
1342     }
1343 }
1344 
GetProxy()1345 sptr<LocatorProxy> LocatorImpl::GetProxy()
1346 {
1347     std::unique_lock<std::mutex> lock(mutex_);
1348     if (client_ != nullptr && isServerExist_) {
1349         return client_;
1350     }
1351 
1352     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1353     if (sam == nullptr) {
1354         LBSLOGE(LOCATOR_STANDARD, "%{public}s: get samgr failed.", __func__);
1355         return nullptr;
1356     }
1357     sptr<IRemoteObject> obj = sam->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
1358     if (obj == nullptr) {
1359         LBSLOGE(LOCATOR_STANDARD, "%{public}s: get remote service failed.", __func__);
1360         return nullptr;
1361     }
1362     recipient_ = sptr<LocatorDeathRecipient>(new (std::nothrow) LocatorDeathRecipient(*this));
1363     if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(recipient_))) {
1364         LBSLOGE(LOCATOR_STANDARD, "%{public}s: deathRecipient add failed.", __func__);
1365         return nullptr;
1366     }
1367     isServerExist_ = true;
1368     client_ = sptr<LocatorProxy>(new (std::nothrow) LocatorProxy(obj));
1369     LBSLOGI(LOCATOR_STANDARD, "%{public}s: create locator proxy", __func__);
1370     if (saStatusListener_ == nullptr) {
1371         saStatusListener_ = sptr<LocatorSystemAbilityListener>(new LocatorSystemAbilityListener());
1372     }
1373     int32_t result = sam->SubscribeSystemAbility(static_cast<int32_t>(LOCATION_LOCATOR_SA_ID), saStatusListener_);
1374     if (result != ERR_OK) {
1375         LBSLOGE(LOCATOR, "%{public}s SubcribeSystemAbility result is %{public}d!", __func__, result);
1376     }
1377     return client_;
1378 }
1379 
UpdateCallbackResumingState(bool state)1380 void LocatorImpl::UpdateCallbackResumingState(bool state)
1381 {
1382     std::unique_lock<std::mutex> lock(resumeMutex_);
1383     isCallbackResuming_ = state;
1384 }
1385 
IsCallbackResuming()1386 bool LocatorImpl::IsCallbackResuming()
1387 {
1388     std::unique_lock<std::mutex> lock(resumeMutex_);
1389     return isCallbackResuming_;
1390 }
1391 
IsLocationCallbackRegistered(const sptr<ILocatorCallback> & callback)1392 bool LocatorImpl::IsLocationCallbackRegistered(const sptr<ILocatorCallback>& callback)
1393 {
1394     if (callback == nullptr) {
1395         return true;
1396     }
1397     std::unique_lock<std::mutex> lock(g_locationCallbackMapMutex);
1398     for (auto iter = g_locationCallbackMap.begin(); iter != g_locationCallbackMap.end(); iter++) {
1399         auto locatorCallback = iter->first;
1400         if (locatorCallback == nullptr) {
1401             continue;
1402         }
1403         if (locatorCallback == callback) {
1404             return true;
1405         }
1406     }
1407     return false;
1408 }
1409 
IsSatelliteStatusChangeCallbackRegistered(const sptr<IRemoteObject> & callback)1410 bool LocatorImpl::IsSatelliteStatusChangeCallbackRegistered(const sptr<IRemoteObject>& callback)
1411 {
1412     if (callback == nullptr) {
1413         return true;
1414     }
1415     std::unique_lock<std::mutex> lock(g_gnssStatusInfoCallbacksMutex);
1416     for (auto gnssStatusCallback : g_gnssStatusInfoCallbacks) {
1417         if (gnssStatusCallback == callback) {
1418             return true;
1419         }
1420     }
1421     return false;
1422 }
1423 
IsNmeaCallbackRegistered(const sptr<IRemoteObject> & callback)1424 bool LocatorImpl::IsNmeaCallbackRegistered(const sptr<IRemoteObject>& callback)
1425 {
1426     if (callback == nullptr) {
1427         return true;
1428     }
1429     std::unique_lock<std::mutex> lock(g_nmeaCallbacksMutex);
1430     for (auto nmeaCallback : g_nmeaCallbacks) {
1431         if (nmeaCallback == callback) {
1432             return true;
1433         }
1434     }
1435     return false;
1436 }
1437 
AddLocationCallBack(std::unique_ptr<RequestConfig> & requestConfig,sptr<ILocatorCallback> & callback)1438 void LocatorImpl::AddLocationCallBack(std::unique_ptr<RequestConfig>& requestConfig,
1439     sptr<ILocatorCallback>& callback)
1440 {
1441     std::unique_lock<std::mutex> lock(g_locationCallbackMapMutex);
1442     g_locationCallbackMap.insert(std::make_pair(callback, *requestConfig));
1443 }
1444 
RemoveLocationCallBack(sptr<ILocatorCallback> & callback)1445 void LocatorImpl::RemoveLocationCallBack(sptr<ILocatorCallback>& callback)
1446 {
1447     std::unique_lock<std::mutex> lock(g_locationCallbackMapMutex);
1448     auto iter = g_locationCallbackMap.find(callback);
1449     if (iter != g_locationCallbackMap.end()) {
1450         g_locationCallbackMap.erase(iter);
1451     }
1452 }
1453 
AddSatelliteStatusChangeCallBack(const sptr<IRemoteObject> & callback)1454 void LocatorImpl::AddSatelliteStatusChangeCallBack(const sptr<IRemoteObject>& callback)
1455 {
1456     std::unique_lock<std::mutex> lock(g_gnssStatusInfoCallbacksMutex);
1457     g_gnssStatusInfoCallbacks.insert(callback);
1458 }
1459 
RemoveSatelliteStatusChangeCallBack(const sptr<IRemoteObject> & callback)1460 void LocatorImpl::RemoveSatelliteStatusChangeCallBack(const sptr<IRemoteObject>& callback)
1461 {
1462     std::unique_lock<std::mutex> lock(g_gnssStatusInfoCallbacksMutex);
1463     for (auto iter = g_gnssStatusInfoCallbacks.begin(); iter != g_gnssStatusInfoCallbacks.end(); iter++) {
1464         if (callback == *iter) {
1465             g_gnssStatusInfoCallbacks.erase(callback);
1466             break;
1467         }
1468     }
1469 }
1470 
AddNmeaCallBack(const sptr<IRemoteObject> & callback)1471 void LocatorImpl::AddNmeaCallBack(const sptr<IRemoteObject>& callback)
1472 {
1473     std::unique_lock<std::mutex> lock(g_nmeaCallbacksMutex);
1474     g_nmeaCallbacks.insert(callback);
1475 }
1476 
RemoveNmeaCallBack(const sptr<IRemoteObject> & callback)1477 void LocatorImpl::RemoveNmeaCallBack(const sptr<IRemoteObject>& callback)
1478 {
1479     std::unique_lock<std::mutex> lock(g_nmeaCallbacksMutex);
1480     for (auto iter = g_nmeaCallbacks.begin(); iter != g_nmeaCallbacks.end(); iter++) {
1481         if (callback == *iter) {
1482             g_nmeaCallbacks.erase(callback);
1483             break;
1484         }
1485     }
1486 }
1487 
HasGnssNetworkRequest()1488 bool LocatorImpl::HasGnssNetworkRequest()
1489 {
1490     bool ret = false;
1491     std::unique_lock<std::mutex> lock(g_locationCallbackMapMutex);
1492     for (auto iter = g_locationCallbackMap.begin(); iter != g_locationCallbackMap.end(); iter++) {
1493         auto locatorCallback = iter->first;
1494         if (locatorCallback == nullptr || iter->first == nullptr) {
1495             continue;
1496         }
1497         auto requestConfig = std::make_unique<RequestConfig>();
1498         requestConfig->Set(iter->second);
1499         if (requestConfig->GetScenario() != SCENE_NO_POWER &&
1500             (requestConfig->GetScenario() != SCENE_UNSET ||
1501             requestConfig->GetPriority() != PRIORITY_UNSET)) {
1502             ret = true;
1503             break;
1504         }
1505     }
1506     return ret;
1507 }
1508 
SetIsServerExist(bool isServerExist)1509 void LocatorImpl::SetIsServerExist(bool isServerExist)
1510 {
1511     isServerExist_ = isServerExist;
1512 }
1513 
InitResumeCallbackFuncMap()1514 void CallbackResumeManager::InitResumeCallbackFuncMap()
1515 {
1516     std::unique_lock<std::mutex> lock(g_resumeFuncMapMutex);
1517     if (g_resumeFuncMap.size() != 0) {
1518         return;
1519     }
1520     g_resumeFuncMap.insert(std::make_pair("satelliteStatusChange", [this]() { return ResumeGnssStatusCallback(); }));
1521     g_resumeFuncMap.insert(std::make_pair("nmeaMessage", [this]() { return ResumeNmeaMessageCallback(); }));
1522     g_resumeFuncMap.insert(std::make_pair("locationChange", [this]() { return ResumeLocating(); }));
1523 }
1524 
ResumeCallback()1525 void CallbackResumeManager::ResumeCallback()
1526 {
1527     InitResumeCallbackFuncMap();
1528     std::unique_lock<std::mutex> lock(g_resumeFuncMapMutex);
1529     for (auto iter = g_resumeFuncMap.begin(); iter != g_resumeFuncMap.end(); iter++) {
1530         auto resumeFunc = iter->second;
1531         resumeFunc();
1532     }
1533 }
1534 
ResumeGnssStatusCallback()1535 void CallbackResumeManager::ResumeGnssStatusCallback()
1536 {
1537     sptr<LocatorProxy> proxy = g_locatorImpl->GetProxy();
1538     if (proxy == nullptr) {
1539         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1540         return ;
1541     }
1542     std::unique_lock<std::mutex> lock(g_gnssStatusInfoCallbacksMutex);
1543     for (auto gnssStatusCallback : g_gnssStatusInfoCallbacks) {
1544         if (gnssStatusCallback == nullptr) {
1545             continue;
1546         }
1547         proxy->RegisterGnssStatusCallbackV9(gnssStatusCallback);
1548     }
1549 }
1550 
ResumeNmeaMessageCallback()1551 void CallbackResumeManager::ResumeNmeaMessageCallback()
1552 {
1553     sptr<LocatorProxy> proxy = g_locatorImpl->GetProxy();
1554     if (proxy == nullptr) {
1555         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1556         return;
1557     }
1558     std::unique_lock<std::mutex> lock(g_nmeaCallbacksMutex);
1559     for (auto nmeaCallback : g_nmeaCallbacks) {
1560         if (nmeaCallback == nullptr) {
1561             continue;
1562         }
1563         proxy->RegisterNmeaMessageCallbackV9(nmeaCallback);
1564     }
1565 }
1566 
ResumeLocating()1567 void CallbackResumeManager::ResumeLocating()
1568 {
1569     sptr<LocatorProxy> proxy = g_locatorImpl->GetProxy();
1570     if (proxy == nullptr) {
1571         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1572         return;
1573     }
1574     std::unique_lock<std::mutex> lock(g_locationCallbackMapMutex);
1575     for (auto iter = g_locationCallbackMap.begin(); iter != g_locationCallbackMap.end(); iter++) {
1576         auto locatorCallback = iter->first;
1577         if (locatorCallback == nullptr || iter->first == nullptr) {
1578             continue;
1579         }
1580         auto requestConfig = std::make_unique<RequestConfig>();
1581         requestConfig->Set(iter->second);
1582         LBSLOGI(LOCATOR_STANDARD, "ResumeLocating requestConfig = %{public}s", requestConfig->ToString().c_str());
1583         proxy->StartLocatingV9(requestConfig, locatorCallback);
1584     }
1585 }
1586 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)1587 void LocatorSystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
1588 {
1589     LBSLOGD(LOCATOR_STANDARD, "%{public}s enter", __func__);
1590     std::unique_lock<std::mutex> lock(mutex_);
1591     if (needResume_) {
1592         if (g_callbackResumer != nullptr && !g_locatorImpl->HasGnssNetworkRequest()) {
1593             g_callbackResumer->ResumeCallback();
1594         }
1595         needResume_ = false;
1596     }
1597 }
1598 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)1599 void LocatorSystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
1600 {
1601     LBSLOGD(LOCATOR_STANDARD, "%{public}s enter", __func__);
1602     auto locatorImpl = LocatorImpl::GetInstance();
1603     if (locatorImpl != nullptr) {
1604         locatorImpl->SetIsServerExist(false);
1605     }
1606     std::unique_lock<std::mutex> lock(mutex_);
1607     needResume_ = true;
1608 }
1609 }  // namespace Location
1610 }  // namespace OHOS
1611