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