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