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