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