• 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_log.h"
28 #include "location_sa_load_manager.h"
29 
30 namespace OHOS {
31 namespace Location {
32 constexpr uint32_t WAIT_MS = 1000;
33 std::shared_ptr<LocatorImpl> LocatorImpl::instance_ = nullptr;
34 std::mutex LocatorImpl::locatorMutex_;
35 
GetInstance()36 std::shared_ptr<LocatorImpl> LocatorImpl::GetInstance()
37 {
38     if (instance_ == nullptr) {
39         std::unique_lock<std::mutex> lock(locatorMutex_);
40         if (instance_ == nullptr) {
41             std::shared_ptr<LocatorImpl> locator = std::make_shared<LocatorImpl>();
42             instance_ = locator;
43         }
44     }
45     return instance_;
46 }
47 
LocatorImpl()48 LocatorImpl::LocatorImpl()
49 {
50     locationDataManager_ = DelayedSingleton<LocationDataManager>::GetInstance();
51     auto locationDataRdbHelper = DelayedSingleton<LocationDataRdbHelper>::GetInstance();
52     auto dataRdbObserver =  sptr<LocationDataRdbObserver>(new (std::nothrow) LocationDataRdbObserver());
53     Uri locationDataEnableUri(LOCATION_DATA_URI);
54     if (locationDataRdbHelper != nullptr) {
55         locationDataRdbHelper->RegisterDataObserver(locationDataEnableUri, dataRdbObserver);
56     }
57     countryCodeManager_ = DelayedSingleton<CountryCodeManager>::GetInstance();
58     if (countryCodeManager_ != nullptr) {
59         countryCodeManager_->ReSubscribeEvent();
60     }
61 }
62 
~LocatorImpl()63 LocatorImpl::~LocatorImpl()
64 {
65     if (countryCodeManager_ != nullptr) {
66         countryCodeManager_->ReUnsubscribeEvent();
67     }
68 }
69 
Init()70 bool LocatorImpl::Init()
71 {
72     auto instance = DelayedSingleton<LocationSaLoadManager>::GetInstance();
73     if (instance == nullptr || instance->LoadLocationSa(LOCATION_LOCATOR_SA_ID) != ERRCODE_SUCCESS) {
74         LBSLOGE(LOCATOR_STANDARD, "locator sa load failed.");
75         return false;
76     }
77     LBSLOGI(LOCATOR_STANDARD, "init successfully");
78     return true;
79 }
80 
IsLocationEnabled()81 bool LocatorImpl::IsLocationEnabled()
82 {
83     bool flag = false;
84     if (locationDataManager_ == nullptr) {
85         return false;
86     }
87     locationDataManager_->QuerySwitchState(flag);
88     return flag;
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 (!Init()) {
109         return;
110     }
111     sptr<LocatorProxy> proxy = GetProxy();
112     if (proxy == nullptr) {
113         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
114         return;
115     }
116     LocationErrCode errCode = proxy->EnableAbilityV9(enable);
117     // cache the value
118     if (errCode == ERRCODE_SUCCESS) {
119         if (locationDataManager_ != nullptr) {
120             locationDataManager_->SetCachedSwitchState(enable ? ENABLED : DISABLED);
121         }
122     }
123 }
124 
StartLocating(std::unique_ptr<RequestConfig> & requestConfig,sptr<ILocatorCallback> & callback)125 void LocatorImpl::StartLocating(std::unique_ptr<RequestConfig>& requestConfig,
126     sptr<ILocatorCallback>& callback)
127 {
128     if (!Init()) {
129         return;
130     }
131     sptr<LocatorProxy> proxy = GetProxy();
132     if (proxy == nullptr) {
133         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
134         return;
135     }
136     proxy->StartLocating(requestConfig, callback, "location.ILocator", 0, 0);
137 }
138 
StopLocating(sptr<ILocatorCallback> & callback)139 void LocatorImpl::StopLocating(sptr<ILocatorCallback>& callback)
140 {
141     if (!Init()) {
142         return;
143     }
144     sptr<LocatorProxy> proxy = GetProxy();
145     if (proxy == nullptr) {
146         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
147         return;
148     }
149     proxy->StopLocating(callback);
150 }
151 
GetCachedLocation()152 std::unique_ptr<Location> LocatorImpl::GetCachedLocation()
153 {
154     if (!CommonUtils::CheckIfSystemAbilityAvailable(LOCATION_LOCATOR_SA_ID)) {
155         LBSLOGI(LOCATOR_STANDARD, "%{public}s, no need GetCachedLocation", __func__);
156         return nullptr;
157     }
158     if (!Init()) {
159         return nullptr;
160     }
161     sptr<LocatorProxy> proxy = GetProxy();
162     if (proxy == nullptr) {
163         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
164         return nullptr;
165     }
166     std::unique_ptr<Location> location = nullptr;
167     MessageParcel reply;
168     proxy->GetCacheLocation(reply);
169     int exception = reply.ReadInt32();
170     if (exception == ERRCODE_PERMISSION_DENIED) {
171         LBSLOGE(LOCATOR_STANDARD, "can not get cached location without location permission.");
172     } else if (exception != ERRCODE_SUCCESS) {
173         LBSLOGE(LOCATOR_STANDARD, "cause some exception happened in lower service.");
174     } else {
175         location = Location::Unmarshalling(reply);
176     }
177 
178     return location;
179 }
180 
RegisterSwitchCallback(const sptr<IRemoteObject> & callback,pid_t uid)181 bool LocatorImpl::RegisterSwitchCallback(const sptr<IRemoteObject>& callback, pid_t uid)
182 {
183     if (locationDataManager_ == nullptr) {
184         return false;
185     }
186     return locationDataManager_->RegisterSwitchCallback(callback, IPCSkeleton::GetCallingUid()) == ERRCODE_SUCCESS;
187 }
188 
UnregisterSwitchCallback(const sptr<IRemoteObject> & callback)189 bool LocatorImpl::UnregisterSwitchCallback(const sptr<IRemoteObject>& callback)
190 {
191     if (locationDataManager_ == nullptr) {
192         return false;
193     }
194     return locationDataManager_->UnregisterSwitchCallback(callback) == ERRCODE_SUCCESS;
195 }
196 
RegisterGnssStatusCallback(const sptr<IRemoteObject> & callback,pid_t uid)197 bool LocatorImpl::RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid)
198 {
199     if (!Init()) {
200         return false;
201     }
202     sptr<LocatorProxy> proxy = GetProxy();
203     if (proxy == nullptr) {
204         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
205         return false;
206     }
207     proxy->RegisterGnssStatusCallback(callback, DEFAULT_UID);
208     return true;
209 }
210 
UnregisterGnssStatusCallback(const sptr<IRemoteObject> & callback)211 bool LocatorImpl::UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback)
212 {
213     if (!Init()) {
214         return false;
215     }
216     sptr<LocatorProxy> proxy = GetProxy();
217     if (proxy == nullptr) {
218         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
219         return false;
220     }
221     proxy->UnregisterGnssStatusCallback(callback);
222     return true;
223 }
224 
RegisterNmeaMessageCallback(const sptr<IRemoteObject> & callback,pid_t uid)225 bool LocatorImpl::RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid)
226 {
227     if (!Init()) {
228         return false;
229     }
230     sptr<LocatorProxy> proxy = GetProxy();
231     if (proxy == nullptr) {
232         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
233         return false;
234     }
235     proxy->RegisterNmeaMessageCallback(callback, DEFAULT_UID);
236     return true;
237 }
238 
UnregisterNmeaMessageCallback(const sptr<IRemoteObject> & callback)239 bool LocatorImpl::UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback)
240 {
241     if (!Init()) {
242         return false;
243     }
244     sptr<LocatorProxy> proxy = GetProxy();
245     if (proxy == nullptr) {
246         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
247         return false;
248     }
249     proxy->UnregisterNmeaMessageCallback(callback);
250     return true;
251 }
252 
RegisterCountryCodeCallback(const sptr<IRemoteObject> & callback,pid_t uid)253 bool LocatorImpl::RegisterCountryCodeCallback(const sptr<IRemoteObject>& callback, pid_t uid)
254 {
255     if (countryCodeManager_ == nullptr) {
256         LBSLOGE(LOCATOR, "%{public}s countryCodeManager_ is nullptr", __func__);
257         return false;
258     }
259     countryCodeManager_->RegisterCountryCodeCallback(callback, uid);
260     return true;
261 }
262 
UnregisterCountryCodeCallback(const sptr<IRemoteObject> & callback)263 bool LocatorImpl::UnregisterCountryCodeCallback(const sptr<IRemoteObject>& callback)
264 {
265     if (countryCodeManager_ == nullptr) {
266         LBSLOGE(LOCATOR, "%{public}s countryCodeManager_ is nullptr", __func__);
267         return false;
268     }
269     countryCodeManager_->UnregisterCountryCodeCallback(callback);
270     return true;
271 }
272 
RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest> & request,sptr<ICachedLocationsCallback> & callback)273 void LocatorImpl::RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest>& request,
274     sptr<ICachedLocationsCallback>& callback)
275 {
276     if (!Init()) {
277         return;
278     }
279     sptr<LocatorProxy> proxy = GetProxy();
280     if (proxy == nullptr) {
281         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
282         return;
283     }
284     proxy->RegisterCachedLocationCallback(request, callback, "location.ILocator");
285 }
286 
UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback> & callback)287 void LocatorImpl::UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback>& callback)
288 {
289     if (!Init()) {
290         return;
291     }
292     sptr<LocatorProxy> proxy = GetProxy();
293     if (proxy == nullptr) {
294         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
295         return;
296     }
297     proxy->UnregisterCachedLocationCallback(callback);
298 }
299 
IsGeoServiceAvailable()300 bool LocatorImpl::IsGeoServiceAvailable()
301 {
302     if (!Init()) {
303         return false;
304     }
305     bool result = false;
306     MessageParcel reply;
307     sptr<LocatorProxy> proxy = GetProxy();
308     if (proxy == nullptr) {
309         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
310         return false;
311     }
312     proxy->IsGeoConvertAvailable(reply);
313     int exception = reply.ReadInt32();
314     if (exception == ERRCODE_PERMISSION_DENIED) {
315         LBSLOGE(LOCATOR_STANDARD, "can not get cached location without location permission.");
316     } else if (exception != ERRCODE_SUCCESS) {
317         LBSLOGE(LOCATOR_STANDARD, "cause some exception happened in lower service.");
318     } else {
319         result = reply.ReadBool();
320     }
321     return result;
322 }
323 
GetAddressByCoordinate(MessageParcel & data,std::list<std::shared_ptr<GeoAddress>> & replyList)324 void LocatorImpl::GetAddressByCoordinate(MessageParcel &data, std::list<std::shared_ptr<GeoAddress>>& replyList)
325 {
326     if (!Init()) {
327         return;
328     }
329     sptr<LocatorProxy> proxy = GetProxy();
330     if (proxy == nullptr) {
331         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
332         return;
333     }
334     MessageParcel reply;
335     proxy->GetAddressByCoordinate(data, reply);
336     int exception = reply.ReadInt32();
337     if (exception == ERRCODE_PERMISSION_DENIED) {
338         LBSLOGE(LOCATOR_STANDARD, "can not get cached location without location permission.");
339     } else if (exception != ERRCODE_SUCCESS) {
340         LBSLOGE(LOCATOR_STANDARD, "cause some exception happened in lower service.");
341     } else {
342         int resultSize = reply.ReadInt32();
343         if (resultSize > GeoAddress::MAX_RESULT) {
344             resultSize = GeoAddress::MAX_RESULT;
345         }
346         for (int i = 0; i < resultSize; i++) {
347             replyList.push_back(GeoAddress::Unmarshalling(reply));
348         }
349     }
350 }
351 
GetAddressByLocationName(MessageParcel & data,std::list<std::shared_ptr<GeoAddress>> & replyList)352 void LocatorImpl::GetAddressByLocationName(MessageParcel &data, std::list<std::shared_ptr<GeoAddress>>& replyList)
353 {
354     if (!Init()) {
355         return;
356     }
357     sptr<LocatorProxy> proxy = GetProxy();
358     if (proxy == nullptr) {
359         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
360         return;
361     }
362     MessageParcel reply;
363     proxy->GetAddressByLocationName(data, reply);
364     int exception = reply.ReadInt32();
365     if (exception == ERRCODE_PERMISSION_DENIED) {
366         LBSLOGE(LOCATOR_STANDARD, "can not get cached location without location permission.");
367     } else if (exception != ERRCODE_SUCCESS) {
368         LBSLOGE(LOCATOR_STANDARD, "cause some exception happened in lower service.");
369     } else {
370         int resultSize = reply.ReadInt32();
371         if (resultSize > GeoAddress::MAX_RESULT) {
372             resultSize = GeoAddress::MAX_RESULT;
373         }
374         for (int i = 0; i < resultSize; i++) {
375             replyList.push_back(GeoAddress::Unmarshalling(reply));
376         }
377     }
378 }
379 
IsLocationPrivacyConfirmed(const int type)380 bool LocatorImpl::IsLocationPrivacyConfirmed(const int type)
381 {
382     if (!Init()) {
383         return false;
384     }
385     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::IsLocationPrivacyConfirmed()");
386     sptr<LocatorProxy> proxy = GetProxy();
387     if (proxy == nullptr) {
388         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
389         return false;
390     }
391     bool flag = proxy->IsLocationPrivacyConfirmed(type);
392     return flag;
393 }
394 
SetLocationPrivacyConfirmStatus(const int type,bool isConfirmed)395 int LocatorImpl::SetLocationPrivacyConfirmStatus(const int type, bool isConfirmed)
396 {
397     if (!Init()) {
398         return false;
399     }
400     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SetLocationPrivacyConfirmStatus()");
401     sptr<LocatorProxy> proxy = GetProxy();
402     if (proxy == nullptr) {
403         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
404         return false;
405     }
406     int flag = proxy->SetLocationPrivacyConfirmStatus(type, isConfirmed);
407     return flag;
408 }
409 
GetCachedGnssLocationsSize()410 int LocatorImpl::GetCachedGnssLocationsSize()
411 {
412     if (!Init()) {
413         return -1;
414     }
415     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetCachedGnssLocationsSize()");
416     sptr<LocatorProxy> proxy = GetProxy();
417     if (proxy == nullptr) {
418         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
419         return false;
420     }
421     int size = proxy->GetCachedGnssLocationsSize();
422     return size;
423 }
424 
FlushCachedGnssLocations()425 int LocatorImpl::FlushCachedGnssLocations()
426 {
427     if (!Init()) {
428         return -1;
429     }
430     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::FlushCachedGnssLocations()");
431     sptr<LocatorProxy> proxy = GetProxy();
432     if (proxy == nullptr) {
433         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
434         return false;
435     }
436     int res = proxy->FlushCachedGnssLocations();
437     return res;
438 }
439 
SendCommand(std::unique_ptr<LocationCommand> & commands)440 bool LocatorImpl::SendCommand(std::unique_ptr<LocationCommand>& commands)
441 {
442     if (!Init()) {
443         return false;
444     }
445     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SendCommand()");
446     sptr<LocatorProxy> proxy = GetProxy();
447     if (proxy == nullptr) {
448         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
449         return false;
450     }
451     proxy->SendCommand(commands);
452     return true;
453 }
454 
AddFence(std::unique_ptr<GeofenceRequest> & request)455 bool LocatorImpl::AddFence(std::unique_ptr<GeofenceRequest>& request)
456 {
457     if (!Init()) {
458         return false;
459     }
460     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::AddFence()");
461     sptr<LocatorProxy> proxy = GetProxy();
462     if (proxy == nullptr) {
463         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
464         return false;
465     }
466     proxy->AddFence(request);
467     return true;
468 }
469 
RemoveFence(std::unique_ptr<GeofenceRequest> & request)470 bool LocatorImpl::RemoveFence(std::unique_ptr<GeofenceRequest>& request)
471 {
472     if (!Init()) {
473         return false;
474     }
475     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::RemoveFence()");
476     sptr<LocatorProxy> proxy = GetProxy();
477     if (proxy == nullptr) {
478         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
479         return false;
480     }
481     proxy->RemoveFence(request);
482     return true;
483 }
484 
GetIsoCountryCode()485 std::shared_ptr<CountryCode> LocatorImpl::GetIsoCountryCode()
486 {
487     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetIsoCountryCode()");
488     if (countryCodeManager_ == nullptr) {
489         LBSLOGE(LOCATOR, "%{public}s countryCodeManager_ is nullptr", __func__);
490         return nullptr;
491     }
492     return countryCodeManager_->GetIsoCountryCode();
493 }
494 
EnableLocationMock()495 bool LocatorImpl::EnableLocationMock()
496 {
497     if (!Init()) {
498         return false;
499     }
500     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::EnableLocationMock()");
501     sptr<LocatorProxy> proxy = GetProxy();
502     if (proxy == nullptr) {
503         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
504         return false;
505     }
506     bool flag = proxy->EnableLocationMock();
507     return flag;
508 }
509 
DisableLocationMock()510 bool LocatorImpl::DisableLocationMock()
511 {
512     if (!Init()) {
513         return false;
514     }
515     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::DisableLocationMock()");
516     sptr<LocatorProxy> proxy = GetProxy();
517     if (proxy == nullptr) {
518         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
519         return false;
520     }
521     bool flag = proxy->DisableLocationMock();
522     return flag;
523 }
524 
SetMockedLocations(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)525 bool LocatorImpl::SetMockedLocations(
526     const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
527 {
528     if (!Init()) {
529         return false;
530     }
531     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SetMockedLocations()");
532     sptr<LocatorProxy> proxy = GetProxy();
533     if (proxy == nullptr) {
534         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
535         return false;
536     }
537     bool flag = proxy->SetMockedLocations(timeInterval, location);
538     return flag;
539 }
540 
EnableReverseGeocodingMock()541 bool LocatorImpl::EnableReverseGeocodingMock()
542 {
543     if (!Init()) {
544         return false;
545     }
546     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::EnableReverseGeocodingMock()");
547     sptr<LocatorProxy> proxy = GetProxy();
548     if (proxy == nullptr) {
549         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
550         return false;
551     }
552     bool flag = proxy->EnableReverseGeocodingMock();
553     return flag;
554 }
555 
DisableReverseGeocodingMock()556 bool LocatorImpl::DisableReverseGeocodingMock()
557 {
558     if (!Init()) {
559         return false;
560     }
561     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::DisableReverseGeocodingMock()");
562     sptr<LocatorProxy> proxy = GetProxy();
563     if (proxy == nullptr) {
564         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
565         return false;
566     }
567     bool flag = proxy->DisableReverseGeocodingMock();
568     return flag;
569 }
570 
SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>> & mockInfo)571 bool LocatorImpl::SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo)
572 {
573     if (!Init()) {
574         return false;
575     }
576     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SetReverseGeocodingMockInfo()");
577     sptr<LocatorProxy> proxy = GetProxy();
578     if (proxy == nullptr) {
579         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
580         return false;
581     }
582     bool flag = proxy->SetReverseGeocodingMockInfo(mockInfo);
583     return flag;
584 }
585 
ProxyUidForFreeze(int32_t uid,bool isProxy)586 bool LocatorImpl::ProxyUidForFreeze(int32_t uid, bool isProxy)
587 {
588     if (!CommonUtils::CheckIfSystemAbilityAvailable(LOCATION_LOCATOR_SA_ID)) {
589         LBSLOGI(LOCATOR_STANDARD, "%{public}s, no need freeze", __func__);
590         return true;
591     }
592     if (!Init()) {
593         return false;
594     }
595     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::ProxyUid()");
596     sptr<LocatorProxy> proxy = GetProxy();
597     if (proxy == nullptr) {
598         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
599         return false;
600     }
601     bool flag = proxy->ProxyUidForFreeze(uid, isProxy);
602     return flag;
603 }
604 
ResetAllProxy()605 bool LocatorImpl::ResetAllProxy()
606 {
607     if (!CommonUtils::CheckIfSystemAbilityAvailable(LOCATION_LOCATOR_SA_ID)) {
608         LBSLOGI(LOCATOR_STANDARD, "%{public}s, no need reset proxy", __func__);
609         return true;
610     }
611     if (!Init()) {
612         return false;
613     }
614     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::ResetAllProxy()");
615     sptr<LocatorProxy> proxy = GetProxy();
616     if (proxy == nullptr) {
617         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
618         return false;
619     }
620     bool flag = proxy->ResetAllProxy();
621     return flag;
622 }
623 
IsLocationEnabledV9(bool & isEnabled)624 LocationErrCode LocatorImpl::IsLocationEnabledV9(bool &isEnabled)
625 {
626     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::IsLocationEnabledV9()");
627     if (locationDataManager_ == nullptr) {
628         return ERRCODE_NOT_SUPPORTED;
629     }
630     LocationErrCode errCode = locationDataManager_->QuerySwitchState(isEnabled);
631     return errCode;
632 }
633 
EnableAbilityV9(bool enable)634 LocationErrCode LocatorImpl::EnableAbilityV9(bool enable)
635 {
636     if (!Init()) {
637         return ERRCODE_SERVICE_UNAVAILABLE;
638     }
639     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::EnableAbilityV9()");
640     sptr<LocatorProxy> proxy = GetProxy();
641     if (proxy == nullptr) {
642         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
643         return ERRCODE_SERVICE_UNAVAILABLE;
644     }
645     LocationErrCode errCode = proxy->EnableAbilityV9(enable);
646     // cache the value
647     if (errCode == ERRCODE_SUCCESS) {
648         if (locationDataManager_ != nullptr) {
649             locationDataManager_->SetCachedSwitchState(enable ? ENABLED : DISABLED);
650         }
651     }
652     return errCode;
653 }
654 
StartLocatingV9(std::unique_ptr<RequestConfig> & requestConfig,sptr<ILocatorCallback> & callback)655 LocationErrCode LocatorImpl::StartLocatingV9(std::unique_ptr<RequestConfig>& requestConfig,
656     sptr<ILocatorCallback>& callback)
657 {
658     if (!Init()) {
659         return ERRCODE_SERVICE_UNAVAILABLE;
660     }
661     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::StartLocatingV9()");
662     sptr<LocatorProxy> proxy = GetProxy();
663     if (proxy == nullptr) {
664         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
665         return ERRCODE_SERVICE_UNAVAILABLE;
666     }
667     LocationErrCode errCode = proxy->StartLocatingV9(requestConfig, callback);
668     return errCode;
669 }
670 
StopLocatingV9(sptr<ILocatorCallback> & callback)671 LocationErrCode LocatorImpl::StopLocatingV9(sptr<ILocatorCallback>& callback)
672 {
673     if (!Init()) {
674         return ERRCODE_SERVICE_UNAVAILABLE;
675     }
676     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::StopLocatingV9()");
677     sptr<LocatorProxy> proxy = GetProxy();
678     if (proxy == nullptr) {
679         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
680         return ERRCODE_SERVICE_UNAVAILABLE;
681     }
682     LocationErrCode errCode = proxy->StopLocatingV9(callback);
683     return errCode;
684 }
685 
GetCachedLocationV9(std::unique_ptr<Location> & loc)686 LocationErrCode LocatorImpl::GetCachedLocationV9(std::unique_ptr<Location> &loc)
687 {
688     if (!CommonUtils::CheckIfSystemAbilityAvailable(LOCATION_LOCATOR_SA_ID)) {
689         LBSLOGI(LOCATOR_STANDARD, "%{public}s, no need GetCachedLocationV9", __func__);
690         return ERRCODE_SERVICE_UNAVAILABLE;
691     }
692     if (!Init()) {
693         return ERRCODE_SERVICE_UNAVAILABLE;
694     }
695     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetCachedLocationV9()");
696     sptr<LocatorProxy> proxy = GetProxy();
697     if (proxy == nullptr) {
698         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
699         return ERRCODE_SERVICE_UNAVAILABLE;
700     }
701     LocationErrCode errCode = proxy->GetCacheLocationV9(loc);
702     return errCode;
703 }
704 
RegisterSwitchCallbackV9(const sptr<IRemoteObject> & callback)705 LocationErrCode LocatorImpl::RegisterSwitchCallbackV9(const sptr<IRemoteObject>& callback)
706 {
707     if (locationDataManager_ == nullptr) {
708         return ERRCODE_SERVICE_UNAVAILABLE;
709     }
710     return locationDataManager_->
711         RegisterSwitchCallback(callback, IPCSkeleton::GetCallingUid());
712 }
713 
UnregisterSwitchCallbackV9(const sptr<IRemoteObject> & callback)714 LocationErrCode LocatorImpl::UnregisterSwitchCallbackV9(const sptr<IRemoteObject>& callback)
715 {
716     if (locationDataManager_ == nullptr) {
717         return ERRCODE_SERVICE_UNAVAILABLE;
718     }
719     return locationDataManager_->UnregisterSwitchCallback(callback);
720 }
721 
RegisterGnssStatusCallbackV9(const sptr<IRemoteObject> & callback)722 LocationErrCode LocatorImpl::RegisterGnssStatusCallbackV9(const sptr<IRemoteObject>& callback)
723 {
724     if (!Init()) {
725         return ERRCODE_SERVICE_UNAVAILABLE;
726     }
727     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::RegisterGnssStatusCallbackV9()");
728     sptr<LocatorProxy> proxy = GetProxy();
729     if (proxy == nullptr) {
730         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
731         return ERRCODE_SERVICE_UNAVAILABLE;
732     }
733     LocationErrCode errCode = proxy->RegisterGnssStatusCallbackV9(callback);
734     return errCode;
735 }
736 
UnregisterGnssStatusCallbackV9(const sptr<IRemoteObject> & callback)737 LocationErrCode LocatorImpl::UnregisterGnssStatusCallbackV9(const sptr<IRemoteObject>& callback)
738 {
739     if (!Init()) {
740         return ERRCODE_SERVICE_UNAVAILABLE;
741     }
742     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::UnregisterGnssStatusCallbackV9()");
743     sptr<LocatorProxy> proxy = GetProxy();
744     if (proxy == nullptr) {
745         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
746         return ERRCODE_SERVICE_UNAVAILABLE;
747     }
748     LocationErrCode errCode = proxy->UnregisterGnssStatusCallbackV9(callback);
749     return errCode;
750 }
751 
RegisterNmeaMessageCallbackV9(const sptr<IRemoteObject> & callback)752 LocationErrCode LocatorImpl::RegisterNmeaMessageCallbackV9(const sptr<IRemoteObject>& callback)
753 {
754     if (!Init()) {
755         return ERRCODE_SERVICE_UNAVAILABLE;
756     }
757     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::RegisterNmeaMessageCallbackV9()");
758     sptr<LocatorProxy> proxy = GetProxy();
759     if (proxy == nullptr) {
760         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
761         return ERRCODE_SERVICE_UNAVAILABLE;
762     }
763     LocationErrCode errCode = proxy->RegisterNmeaMessageCallbackV9(callback);
764     return errCode;
765 }
766 
UnregisterNmeaMessageCallbackV9(const sptr<IRemoteObject> & callback)767 LocationErrCode LocatorImpl::UnregisterNmeaMessageCallbackV9(const sptr<IRemoteObject>& callback)
768 {
769     if (!Init()) {
770         return ERRCODE_SERVICE_UNAVAILABLE;
771     }
772     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::UnregisterNmeaMessageCallbackV9()");
773     sptr<LocatorProxy> proxy = GetProxy();
774     if (proxy == nullptr) {
775         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
776         return ERRCODE_SERVICE_UNAVAILABLE;
777     }
778     LocationErrCode errCode = proxy->UnregisterNmeaMessageCallbackV9(callback);
779     return errCode;
780 }
781 
RegisterCountryCodeCallbackV9(const sptr<IRemoteObject> & callback)782 LocationErrCode LocatorImpl::RegisterCountryCodeCallbackV9(const sptr<IRemoteObject>& callback)
783 {
784     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::RegisterCountryCodeCallbackV9()");
785     if (countryCodeManager_ == nullptr) {
786         LBSLOGE(LOCATOR, "%{public}s countryCodeManager_ is nullptr", __func__);
787         return ERRCODE_SERVICE_UNAVAILABLE;
788     }
789     countryCodeManager_->RegisterCountryCodeCallback(callback, 0);
790     return ERRCODE_SUCCESS;
791 }
792 
UnregisterCountryCodeCallbackV9(const sptr<IRemoteObject> & callback)793 LocationErrCode LocatorImpl::UnregisterCountryCodeCallbackV9(const sptr<IRemoteObject>& callback)
794 {
795     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::UnregisterCountryCodeCallbackV9()");
796     if (countryCodeManager_ == nullptr) {
797         LBSLOGE(LOCATOR, "%{public}s countryCodeManager_ is nullptr", __func__);
798         return ERRCODE_SERVICE_UNAVAILABLE;
799     }
800     countryCodeManager_->UnregisterCountryCodeCallback(callback);
801     return ERRCODE_SUCCESS;
802 }
803 
RegisterCachedLocationCallbackV9(std::unique_ptr<CachedGnssLocationsRequest> & request,sptr<ICachedLocationsCallback> & callback)804 LocationErrCode LocatorImpl::RegisterCachedLocationCallbackV9(std::unique_ptr<CachedGnssLocationsRequest>& request,
805     sptr<ICachedLocationsCallback>& callback)
806 {
807     if (!Init()) {
808         return ERRCODE_SERVICE_UNAVAILABLE;
809     }
810     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::RegisterCachedLocationCallbackV9()");
811     sptr<LocatorProxy> proxy = GetProxy();
812     if (proxy == nullptr) {
813         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
814         return ERRCODE_SERVICE_UNAVAILABLE;
815     }
816     LocationErrCode errCode = proxy->RegisterCachedLocationCallbackV9(request, callback, "location.ILocator");
817     return errCode;
818 }
819 
UnregisterCachedLocationCallbackV9(sptr<ICachedLocationsCallback> & callback)820 LocationErrCode LocatorImpl::UnregisterCachedLocationCallbackV9(sptr<ICachedLocationsCallback>& callback)
821 {
822     if (!Init()) {
823         return ERRCODE_SERVICE_UNAVAILABLE;
824     }
825     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::UnregisterCachedLocationCallbackV9()");
826     sptr<LocatorProxy> proxy = GetProxy();
827     if (proxy == nullptr) {
828         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
829         return ERRCODE_SERVICE_UNAVAILABLE;
830     }
831     LocationErrCode errCode = proxy->UnregisterCachedLocationCallbackV9(callback);
832     return errCode;
833 }
834 
IsGeoServiceAvailableV9(bool & isAvailable)835 LocationErrCode LocatorImpl::IsGeoServiceAvailableV9(bool &isAvailable)
836 {
837     if (!Init()) {
838         return ERRCODE_SERVICE_UNAVAILABLE;
839     }
840     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::IsGeoServiceAvailableV9()");
841     sptr<LocatorProxy> proxy = GetProxy();
842     if (proxy == nullptr) {
843         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
844         return ERRCODE_SERVICE_UNAVAILABLE;
845     }
846     LocationErrCode errCode = proxy->IsGeoConvertAvailableV9(isAvailable);
847     return errCode;
848 }
849 
GetAddressByCoordinateV9(MessageParcel & data,std::list<std::shared_ptr<GeoAddress>> & replyList)850 LocationErrCode LocatorImpl::GetAddressByCoordinateV9(MessageParcel &data,
851     std::list<std::shared_ptr<GeoAddress>>& replyList)
852 {
853     if (!Init()) {
854         return ERRCODE_SERVICE_UNAVAILABLE;
855     }
856     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetAddressByCoordinateV9()");
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->GetAddressByCoordinateV9(data, replyList);
863     return errCode;
864 }
865 
GetAddressByLocationNameV9(MessageParcel & data,std::list<std::shared_ptr<GeoAddress>> & replyList)866 LocationErrCode LocatorImpl::GetAddressByLocationNameV9(MessageParcel &data,
867     std::list<std::shared_ptr<GeoAddress>>& replyList)
868 {
869     if (!Init()) {
870         return ERRCODE_SERVICE_UNAVAILABLE;
871     }
872     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetAddressByLocationNameV9()");
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->GetAddressByLocationNameV9(data, replyList);
879     return errCode;
880 }
881 
IsLocationPrivacyConfirmedV9(const int type,bool & isConfirmed)882 LocationErrCode LocatorImpl::IsLocationPrivacyConfirmedV9(const int type, bool &isConfirmed)
883 {
884     if (!Init()) {
885         return ERRCODE_SERVICE_UNAVAILABLE;
886     }
887     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::IsLocationPrivacyConfirmedV9()");
888     sptr<LocatorProxy> proxy = GetProxy();
889     if (proxy == nullptr) {
890         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
891         return ERRCODE_SERVICE_UNAVAILABLE;
892     }
893     LocationErrCode errCode = proxy->IsLocationPrivacyConfirmedV9(type, isConfirmed);
894     return errCode;
895 }
896 
SetLocationPrivacyConfirmStatusV9(const int type,bool isConfirmed)897 LocationErrCode LocatorImpl::SetLocationPrivacyConfirmStatusV9(const int type, bool isConfirmed)
898 {
899     if (!Init()) {
900         return ERRCODE_SERVICE_UNAVAILABLE;
901     }
902     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SetLocationPrivacyConfirmStatusV9()");
903     sptr<LocatorProxy> proxy = GetProxy();
904     if (proxy == nullptr) {
905         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
906         return ERRCODE_SERVICE_UNAVAILABLE;
907     }
908     LocationErrCode errCode = proxy->SetLocationPrivacyConfirmStatusV9(type, isConfirmed);
909     return errCode;
910 }
911 
GetCachedGnssLocationsSizeV9(int & size)912 LocationErrCode LocatorImpl::GetCachedGnssLocationsSizeV9(int &size)
913 {
914     if (!Init()) {
915         return ERRCODE_SERVICE_UNAVAILABLE;
916     }
917     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetCachedGnssLocationsSizeV9()");
918     sptr<LocatorProxy> proxy = GetProxy();
919     if (proxy == nullptr) {
920         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
921         return ERRCODE_SERVICE_UNAVAILABLE;
922     }
923     LocationErrCode errCode = proxy->GetCachedGnssLocationsSizeV9(size);
924     return errCode;
925 }
926 
FlushCachedGnssLocationsV9()927 LocationErrCode LocatorImpl::FlushCachedGnssLocationsV9()
928 {
929     if (!Init()) {
930         return ERRCODE_SERVICE_UNAVAILABLE;
931     }
932     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::FlushCachedGnssLocationsV9()");
933     sptr<LocatorProxy> proxy = GetProxy();
934     if (proxy == nullptr) {
935         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
936         return ERRCODE_SERVICE_UNAVAILABLE;
937     }
938     LocationErrCode errCode = proxy->FlushCachedGnssLocationsV9();
939     return errCode;
940 }
941 
SendCommandV9(std::unique_ptr<LocationCommand> & commands)942 LocationErrCode LocatorImpl::SendCommandV9(std::unique_ptr<LocationCommand>& commands)
943 {
944     if (!Init()) {
945         return ERRCODE_SERVICE_UNAVAILABLE;
946     }
947     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SendCommandV9()");
948     sptr<LocatorProxy> proxy = GetProxy();
949     if (proxy == nullptr) {
950         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
951         return ERRCODE_SERVICE_UNAVAILABLE;
952     }
953     LocationErrCode errCode = proxy->SendCommandV9(commands);
954     return errCode;
955 }
956 
AddFenceV9(std::unique_ptr<GeofenceRequest> & request)957 LocationErrCode LocatorImpl::AddFenceV9(std::unique_ptr<GeofenceRequest>& request)
958 {
959     if (!Init()) {
960         return ERRCODE_SERVICE_UNAVAILABLE;
961     }
962     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::AddFenceV9()");
963     sptr<LocatorProxy> proxy = GetProxy();
964     if (proxy == nullptr) {
965         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
966         return ERRCODE_SERVICE_UNAVAILABLE;
967     }
968     LocationErrCode errCode = proxy->AddFenceV9(request);
969     return errCode;
970 }
971 
RemoveFenceV9(std::unique_ptr<GeofenceRequest> & request)972 LocationErrCode LocatorImpl::RemoveFenceV9(std::unique_ptr<GeofenceRequest>& request)
973 {
974     if (!Init()) {
975         return ERRCODE_SERVICE_UNAVAILABLE;
976     }
977     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::RemoveFenceV9()");
978     sptr<LocatorProxy> proxy = GetProxy();
979     if (proxy == nullptr) {
980         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
981         return ERRCODE_SERVICE_UNAVAILABLE;
982     }
983     LocationErrCode errCode = proxy->RemoveFenceV9(request);
984     return errCode;
985 }
986 
GetIsoCountryCodeV9(std::shared_ptr<CountryCode> & countryCode)987 LocationErrCode LocatorImpl::GetIsoCountryCodeV9(std::shared_ptr<CountryCode>& countryCode)
988 {
989     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::GetIsoCountryCodeV9()");
990     if (countryCodeManager_ == nullptr) {
991         LBSLOGE(LOCATOR, "%{public}s countryCodeManager_ is nullptr", __func__);
992         return ERRCODE_SERVICE_UNAVAILABLE;
993     }
994     countryCode = countryCodeManager_->GetIsoCountryCode();
995     return ERRCODE_SUCCESS;
996 }
997 
EnableLocationMockV9()998 LocationErrCode LocatorImpl::EnableLocationMockV9()
999 {
1000     if (!Init()) {
1001         return ERRCODE_SERVICE_UNAVAILABLE;
1002     }
1003     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::EnableLocationMockV9()");
1004     sptr<LocatorProxy> proxy = GetProxy();
1005     if (proxy == nullptr) {
1006         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1007         return ERRCODE_SERVICE_UNAVAILABLE;
1008     }
1009     LocationErrCode errCode = proxy->EnableLocationMockV9();
1010     return errCode;
1011 }
1012 
DisableLocationMockV9()1013 LocationErrCode LocatorImpl::DisableLocationMockV9()
1014 {
1015     if (!Init()) {
1016         return ERRCODE_SERVICE_UNAVAILABLE;
1017     }
1018     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::DisableLocationMockV9()");
1019     sptr<LocatorProxy> proxy = GetProxy();
1020     if (proxy == nullptr) {
1021         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1022         return ERRCODE_SERVICE_UNAVAILABLE;
1023     }
1024     LocationErrCode errCode = proxy->DisableLocationMockV9();
1025     return errCode;
1026 }
1027 
SetMockedLocationsV9(const int timeInterval,const std::vector<std::shared_ptr<Location>> & location)1028 LocationErrCode LocatorImpl::SetMockedLocationsV9(
1029     const int timeInterval, const std::vector<std::shared_ptr<Location>> &location)
1030 {
1031     if (!Init()) {
1032         return ERRCODE_SERVICE_UNAVAILABLE;
1033     }
1034     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SetMockedLocationsV9()");
1035     sptr<LocatorProxy> proxy = GetProxy();
1036     if (proxy == nullptr) {
1037         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1038         return ERRCODE_SERVICE_UNAVAILABLE;
1039     }
1040     LocationErrCode errCode = proxy->SetMockedLocationsV9(timeInterval, location);
1041     return errCode;
1042 }
1043 
EnableReverseGeocodingMockV9()1044 LocationErrCode LocatorImpl::EnableReverseGeocodingMockV9()
1045 {
1046     if (!Init()) {
1047         return ERRCODE_SERVICE_UNAVAILABLE;
1048     }
1049     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::EnableReverseGeocodingMockV9()");
1050     sptr<LocatorProxy> proxy = GetProxy();
1051     if (proxy == nullptr) {
1052         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1053         return ERRCODE_SERVICE_UNAVAILABLE;
1054     }
1055     LocationErrCode errCode = proxy->EnableReverseGeocodingMockV9();
1056     return errCode;
1057 }
1058 
DisableReverseGeocodingMockV9()1059 LocationErrCode LocatorImpl::DisableReverseGeocodingMockV9()
1060 {
1061     if (!Init()) {
1062         return ERRCODE_SERVICE_UNAVAILABLE;
1063     }
1064     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::DisableReverseGeocodingMockV9()");
1065     sptr<LocatorProxy> proxy = GetProxy();
1066     if (proxy == nullptr) {
1067         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1068         return ERRCODE_SERVICE_UNAVAILABLE;
1069     }
1070     LocationErrCode errCode = proxy->DisableReverseGeocodingMockV9();
1071     return errCode;
1072 }
1073 
SetReverseGeocodingMockInfoV9(std::vector<std::shared_ptr<GeocodingMockInfo>> & mockInfo)1074 LocationErrCode LocatorImpl::SetReverseGeocodingMockInfoV9(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo)
1075 {
1076     if (!Init()) {
1077         return ERRCODE_SERVICE_UNAVAILABLE;
1078     }
1079     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::SetReverseGeocodingMockInfoV9()");
1080     sptr<LocatorProxy> proxy = GetProxy();
1081     if (proxy == nullptr) {
1082         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1083         return ERRCODE_SERVICE_UNAVAILABLE;
1084     }
1085     LocationErrCode errCode = proxy->SetReverseGeocodingMockInfoV9(mockInfo);
1086     return errCode;
1087 }
1088 
ProxyUidForFreezeV9(int32_t uid,bool isProxy)1089 LocationErrCode LocatorImpl::ProxyUidForFreezeV9(int32_t uid, bool isProxy)
1090 {
1091     if (!CommonUtils::CheckIfSystemAbilityAvailable(LOCATION_LOCATOR_SA_ID)) {
1092         LBSLOGI(LOCATOR_STANDARD, "%{public}s, no need freeze", __func__);
1093         return ERRCODE_SUCCESS;
1094     }
1095     if (!Init()) {
1096         return ERRCODE_SERVICE_UNAVAILABLE;
1097     }
1098     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::ProxyUidForFreezeV9()");
1099     sptr<LocatorProxy> proxy = GetProxy();
1100     if (proxy == nullptr) {
1101         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1102         return ERRCODE_SERVICE_UNAVAILABLE;
1103     }
1104     LocationErrCode errCode = proxy->ProxyUidForFreezeV9(uid, isProxy);
1105     return errCode;
1106 }
1107 
ResetAllProxyV9()1108 LocationErrCode LocatorImpl::ResetAllProxyV9()
1109 {
1110     if (!CommonUtils::CheckIfSystemAbilityAvailable(LOCATION_LOCATOR_SA_ID)) {
1111         LBSLOGI(LOCATOR_STANDARD, "%{public}s, no need reset proxy", __func__);
1112         return ERRCODE_SUCCESS;
1113     }
1114     if (!Init()) {
1115         return ERRCODE_SERVICE_UNAVAILABLE;
1116     }
1117     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::ResetAllProxyV9()");
1118     sptr<LocatorProxy> proxy = GetProxy();
1119     if (proxy == nullptr) {
1120         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1121         return ERRCODE_SERVICE_UNAVAILABLE;
1122     }
1123     LocationErrCode errCode = proxy->ResetAllProxyV9();
1124     return errCode;
1125 }
1126 
RegisterLocatingRequiredDataCallback(std::unique_ptr<LocatingRequiredDataConfig> & dataConfig,sptr<ILocatingRequiredDataCallback> & callback)1127 LocationErrCode LocatorImpl::RegisterLocatingRequiredDataCallback(
1128     std::unique_ptr<LocatingRequiredDataConfig>& dataConfig, sptr<ILocatingRequiredDataCallback>& callback)
1129 {
1130     if (!Init()) {
1131         return ERRCODE_SERVICE_UNAVAILABLE;
1132     }
1133     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::%{public}s", __func__);
1134     sptr<LocatorProxy> proxy = GetProxy();
1135     if (proxy == nullptr) {
1136         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1137         return ERRCODE_SERVICE_UNAVAILABLE;
1138     }
1139     return proxy->RegisterLocatingRequiredDataCallback(dataConfig, callback);
1140 }
1141 
UnRegisterLocatingRequiredDataCallback(sptr<ILocatingRequiredDataCallback> & callback)1142 LocationErrCode LocatorImpl::UnRegisterLocatingRequiredDataCallback(sptr<ILocatingRequiredDataCallback>& callback)
1143 {
1144     if (!Init()) {
1145         return ERRCODE_SERVICE_UNAVAILABLE;
1146     }
1147     LBSLOGD(LOCATOR_STANDARD, "LocatorImpl::%{public}s", __func__);
1148     sptr<LocatorProxy> proxy = GetProxy();
1149     if (proxy == nullptr) {
1150         LBSLOGE(LOCATOR_STANDARD, "%{public}s get proxy failed.", __func__);
1151         return ERRCODE_SERVICE_UNAVAILABLE;
1152     }
1153     return proxy->UnRegisterLocatingRequiredDataCallback(callback);
1154 }
1155 
ResetLocatorProxy(const wptr<IRemoteObject> & remote)1156 void LocatorImpl::ResetLocatorProxy(const wptr<IRemoteObject> &remote)
1157 {
1158     if (remote == nullptr) {
1159         LBSLOGE(LOCATOR_STANDARD, "%{public}s: remote is nullptr.", __func__);
1160         return;
1161     }
1162     if (client_ == nullptr || !isServerExist_) {
1163         LBSLOGE(LOCATOR_STANDARD, "%{public}s: proxy is nullptr.", __func__);
1164         return;
1165     }
1166     if (remote.promote() != nullptr) {
1167         remote.promote()->RemoveDeathRecipient(recipient_);
1168     }
1169     isServerExist_ = false;
1170     if (resumer_ != nullptr && !IsCallbackResuming()) {
1171         // only the first request will be handled
1172         UpdateCallbackResumingState(true);
1173         // wait for remote died finished
1174         std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS));
1175         resumer_->ResumeCallback();
1176         UpdateCallbackResumingState(false);
1177     }
1178 }
1179 
GetProxy()1180 sptr<LocatorProxy> LocatorImpl::GetProxy()
1181 {
1182     std::unique_lock<std::mutex> lock(mutex_);
1183     if (client_ != nullptr && isServerExist_) {
1184         LBSLOGI(LOCATOR_STANDARD, "get proxy success.");
1185         return client_;
1186     }
1187 
1188     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1189     if (sam == nullptr) {
1190         LBSLOGE(LOCATOR_STANDARD, "%{public}s: get samgr failed.", __func__);
1191         return nullptr;
1192     }
1193     sptr<IRemoteObject> obj = sam->CheckSystemAbility(LOCATION_LOCATOR_SA_ID);
1194     if (obj == nullptr) {
1195         LBSLOGE(LOCATOR_STANDARD, "%{public}s: get remote service failed.", __func__);
1196         return nullptr;
1197     }
1198     recipient_ = sptr<LocatorDeathRecipient>(new (std::nothrow) LocatorDeathRecipient(*this));
1199     if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(recipient_))) {
1200         LBSLOGE(LOCATOR_STANDARD, "%{public}s: deathRecipient add failed.", __func__);
1201         return nullptr;
1202     }
1203     isServerExist_ = true;
1204     client_ = sptr<LocatorProxy>(new (std::nothrow) LocatorProxy(obj));
1205     return client_;
1206 }
1207 
SetResumer(std::shared_ptr<ICallbackResumeManager> resumer)1208 void LocatorImpl::SetResumer(std::shared_ptr<ICallbackResumeManager> resumer)
1209 {
1210     if (resumer_ == nullptr) {
1211         resumer_ = resumer;
1212     }
1213 }
1214 
UpdateCallbackResumingState(bool state)1215 void LocatorImpl::UpdateCallbackResumingState(bool state)
1216 {
1217     std::unique_lock<std::mutex> lock(resumeMutex_);
1218     isCallbackResuming_ = state;
1219 }
1220 
IsCallbackResuming()1221 bool LocatorImpl::IsCallbackResuming()
1222 {
1223     std::unique_lock<std::mutex> lock(resumeMutex_);
1224     return isCallbackResuming_;
1225 }
1226 }  // namespace Location
1227 }  // namespace OHOS
1228