1 /*
2 * Copyright (c) 2023 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 "location_proxy_adapter_impl.h"
17
18 #include <dlfcn.h>
19
20 #include "common_utils.h"
21 #include "constant_definition.h"
22 #include "location_adapter.h"
23 #include "location_callback_adapter_impl.h"
24 #include "nweb_log.h"
25
26 using namespace OHOS::Location;
27 namespace {
28 #ifdef __aarch64__
29 const std::string NWEB_WRAPPER_SO_PATH = "/system/lib64/libnweb_ohos_wrapper.z.so";
30 #else
31 const std::string NWEB_WRAPPER_SO_PATH = "/system/lib/libnweb_ohos_wrapper.z.so";
32 #endif
ConvertScenario(int32_t scenario)33 int32_t ConvertScenario(int32_t scenario)
34 {
35 int32_t ret = OHOS::NWeb::LocationRequestConfig::Scenario::UNSET;
36 switch (scenario) {
37 case OHOS::NWeb::LocationRequestConfig::Scenario::UNSET:
38 ret = OHOS::Location::SCENE_UNSET;
39 break;
40 case OHOS::NWeb::LocationRequestConfig::Scenario::NAVIGATION:
41 ret = OHOS::Location::SCENE_NAVIGATION;
42 break;
43 case OHOS::NWeb::LocationRequestConfig::Scenario::TRAJECTORY_TRACKING:
44 ret = OHOS::Location::SCENE_TRAJECTORY_TRACKING;
45 break;
46 case OHOS::NWeb::LocationRequestConfig::Scenario::CAR_HAILING:
47 ret = OHOS::Location::SCENE_CAR_HAILING;
48 break;
49 case OHOS::NWeb::LocationRequestConfig::Scenario::DAILY_LIFE_SERVICE:
50 ret = OHOS::Location::SCENE_DAILY_LIFE_SERVICE;
51 break;
52 case OHOS::NWeb::LocationRequestConfig::Scenario::NO_POWER:
53 ret = OHOS::Location::SCENE_NO_POWER;
54 break;
55 default:
56 break;
57 }
58 return ret;
59 }
60
ConvertPriority(int32_t priority)61 int32_t ConvertPriority(int32_t priority)
62 {
63 int32_t ret = OHOS::NWeb::LocationRequestConfig::Priority::PRIORITY_UNSET;
64 switch (priority) {
65 case OHOS::NWeb::LocationRequestConfig::Priority::PRIORITY_UNSET:
66 ret = OHOS::Location::PRIORITY_UNSET;
67 break;
68 case OHOS::NWeb::LocationRequestConfig::Priority::PRIORITY_ACCURACY:
69 ret = OHOS::Location::PRIORITY_ACCURACY;
70 break;
71 case OHOS::NWeb::LocationRequestConfig::Priority::PRIORITY_LOW_POWER:
72 ret = OHOS::Location::PRIORITY_LOW_POWER;
73 break;
74 case OHOS::NWeb::LocationRequestConfig::Priority::PRIORITY_FAST_FIRST_FIX:
75 ret = OHOS::Location::PRIORITY_FAST_FIRST_FIX;
76 break;
77 default:
78 break;
79 }
80 return ret;
81 }
82 }
83
84 namespace OHOS::NWeb {
GetInstance()85 LocationInstance& LocationInstance::GetInstance()
86 {
87 static LocationInstance instance;
88 return instance;
89 }
90
CreateLocationProxyAdapter()91 std::unique_ptr<LocationProxyAdapter> LocationInstance::CreateLocationProxyAdapter()
92 {
93 return std::make_unique<LocationProxyAdapterImpl>();
94 }
95
CreateLocationRequestConfig()96 std::unique_ptr<LocationRequestConfig> LocationInstance::CreateLocationRequestConfig()
97 {
98 return std::make_unique<LocationRequestConfigImpl>();
99 }
100
LocationRequestConfigImpl()101 LocationRequestConfigImpl::LocationRequestConfigImpl()
102 {
103 config_ = std::make_unique<OHOS::Location::RequestConfig>();
104 }
105
SetScenario(int32_t scenario)106 void LocationRequestConfigImpl::SetScenario(int32_t scenario)
107 {
108 if (config_ == nullptr) {
109 return;
110 }
111 config_->SetScenario(ConvertScenario(scenario));
112 }
113
SetFixNumber(int32_t number)114 void LocationRequestConfigImpl::SetFixNumber(int32_t number)
115 {
116 if (config_ == nullptr) {
117 return;
118 }
119 config_->SetFixNumber(number);
120 }
121
SetMaxAccuracy(int32_t maxAccuary)122 void LocationRequestConfigImpl::SetMaxAccuracy(int32_t maxAccuary)
123 {
124 if (config_ == nullptr) {
125 return;
126 }
127 config_->SetMaxAccuracy(maxAccuary);
128 }
129
SetDistanceInterval(int32_t disInterval)130 void LocationRequestConfigImpl::SetDistanceInterval(int32_t disInterval)
131 {
132 if (config_ == nullptr) {
133 return;
134 }
135 config_->SetDistanceInterval(disInterval);
136 }
137
SetTimeInterval(int32_t timeInterval)138 void LocationRequestConfigImpl::SetTimeInterval(int32_t timeInterval)
139 {
140 if (config_ == nullptr) {
141 return;
142 }
143 config_->SetTimeInterval(timeInterval);
144 }
145
SetPriority(int32_t priority)146 void LocationRequestConfigImpl::SetPriority(int32_t priority)
147 {
148 if (config_ == nullptr) {
149 return;
150 }
151 config_->SetPriority(ConvertPriority(priority));
152 }
153
GetConfig()154 std::unique_ptr<OHOS::Location::RequestConfig>& LocationRequestConfigImpl::GetConfig()
155 {
156 return config_;
157 }
158
LocationInfoImpl(std::unique_ptr<OHOS::Location::Location> & location)159 LocationInfoImpl::LocationInfoImpl(std::unique_ptr<OHOS::Location::Location>& location)
160 : location_(std::move(location)) {}
161
GetLatitude() const162 double LocationInfoImpl::GetLatitude() const
163 {
164 if (location_ == nullptr) {
165 return 0;
166 }
167 return location_->GetLatitude();
168 }
169
GetLongitude() const170 double LocationInfoImpl::GetLongitude() const
171 {
172 if (location_ == nullptr) {
173 return 0;
174 }
175 return location_->GetLongitude();
176 }
177
GetAltitude() const178 double LocationInfoImpl::GetAltitude() const
179 {
180 if (location_ == nullptr) {
181 return 0;
182 }
183 return location_->GetAltitude();
184 }
185
GetAccuracy() const186 float LocationInfoImpl::GetAccuracy() const
187 {
188 if (location_ == nullptr) {
189 return 0;
190 }
191 return location_->GetAccuracy();
192 }
193
GetSpeed() const194 float LocationInfoImpl::GetSpeed() const
195 {
196 if (location_ == nullptr) {
197 return 0;
198 }
199 return location_->GetSpeed();
200 }
201
GetDirection() const202 double LocationInfoImpl::GetDirection() const
203 {
204 if (location_ == nullptr) {
205 return 0;
206 }
207 return location_->GetDirection();
208 }
209
GetTimeStamp() const210 int64_t LocationInfoImpl::GetTimeStamp() const
211 {
212 if (location_ == nullptr) {
213 return 0;
214 }
215 return location_->GetTimeStamp();
216 }
217
GetTimeSinceBoot() const218 int64_t LocationInfoImpl::GetTimeSinceBoot() const
219 {
220 if (location_ == nullptr) {
221 return 0;
222 }
223 return location_->GetTimeSinceBoot();
224 }
225
GetAdditions() const226 std::string LocationInfoImpl::GetAdditions() const
227 {
228 if (location_ == nullptr) {
229 return "";
230 }
231 return location_->GetAdditions();
232 }
233
GetLocation()234 std::unique_ptr<OHOS::Location::Location>& LocationInfoImpl::GetLocation()
235 {
236 return location_;
237 }
238
239 void* LocationProxyAdapterImpl::wrapperHandle_;
240 IsEnableLocationFuncType LocationProxyAdapterImpl::isEnableLocationFunc_;
241 EnableAbilityFuncType LocationProxyAdapterImpl::enableAbilityFunc_;
242 StartLocatingFuncType LocationProxyAdapterImpl::startLocatingFunc_;
243 StopLocatingFuncType LocationProxyAdapterImpl::stopLocatingFunc_;
244
LocationProxyAdapterImpl()245 LocationProxyAdapterImpl::LocationProxyAdapterImpl()
246 {
247 if (!wrapperHandle_) {
248 wrapperHandle_ = dlopen(NWEB_WRAPPER_SO_PATH.c_str(), RTLD_NOW);
249 }
250 if (wrapperHandle_) {
251 if (!isEnableLocationFunc_) {
252 isEnableLocationFunc_ = reinterpret_cast<IsEnableLocationFuncType>(
253 dlsym(wrapperHandle_, "IsLocationEnable"));
254 }
255 if (!enableAbilityFunc_) {
256 enableAbilityFunc_ = reinterpret_cast<EnableAbilityFuncType>(
257 dlsym(wrapperHandle_, "EnableAbility"));
258 }
259 if (!startLocatingFunc_) {
260 startLocatingFunc_ = reinterpret_cast<StartLocatingFuncType>(
261 dlsym(wrapperHandle_, "StartLocating"));
262 }
263 if (!stopLocatingFunc_) {
264 stopLocatingFunc_ = reinterpret_cast<StopLocatingFuncType>(
265 dlsym(wrapperHandle_, "StopLocating"));
266 }
267 }
268 }
269
StartLocating(std::unique_ptr<LocationRequestConfig> & requestConfig,std::shared_ptr<LocationCallbackAdapter> callback)270 bool LocationProxyAdapterImpl::StartLocating(
271 std::unique_ptr<LocationRequestConfig>& requestConfig,
272 std::shared_ptr<LocationCallbackAdapter> callback)
273 {
274 if (!startLocatingFunc_ || !callback) {
275 WVLOG_E("get Locator::GetInstance() failed or callback is nullptr");
276 return false;
277 }
278 sptr<OHOS::Location::ILocatorCallback> iCallback =
279 sptr<OHOS::Location::ILocatorCallback>(new LocationCallbackImpl(callback));
280 LocatorCallbackMap::iterator iter = reg_.find(callback.get());
281 if (iter != reg_.end()) {
282 WVLOG_E("StartLocating failed, callback already in map");
283 return false;
284 }
285 bool ret = startLocatingFunc_(
286 reinterpret_cast<LocationRequestConfigImpl*>(requestConfig.get())->GetConfig(),
287 iCallback);
288 if (!ret) {
289 WVLOG_E("StartLocating failed, errcode:%{public}d", ret);
290 return false;
291 }
292
293 reg_.emplace(std::make_pair(callback.get(), iCallback));
294 return true;
295 }
296
StopLocating(std::shared_ptr<LocationCallbackAdapter> callback)297 bool LocationProxyAdapterImpl::StopLocating(
298 std::shared_ptr<LocationCallbackAdapter> callback)
299 {
300 if (!stopLocatingFunc_ || !callback) {
301 WVLOG_E("get Locator::GetInstance() failed or callback is nullptr");
302 return false;
303 }
304 LocatorCallbackMap::iterator iter = reg_.find(callback.get());
305 if (iter == reg_.end()) {
306 WVLOG_E("StopLocating failed due to reg_ not find iCallback");
307 return false;
308 }
309 bool ret = stopLocatingFunc_(iter->second);
310 reg_.erase(iter);
311 return ret;
312 }
313
EnableAbility(bool isEnabled)314 bool LocationProxyAdapterImpl::EnableAbility(bool isEnabled)
315 {
316 if (!enableAbilityFunc_) {
317 WVLOG_E("get Locator::GetInstance() failed");
318 return false;
319 }
320 return enableAbilityFunc_(isEnabled);
321 }
322
IsLocationEnabled()323 bool LocationProxyAdapterImpl::IsLocationEnabled()
324 {
325 if (!isEnableLocationFunc_) {
326 WVLOG_E("get Locator::GetInstance() failed");
327 return false;
328 }
329 bool isEnabled = false;
330 return isEnableLocationFunc_(isEnabled) ? isEnabled : false;
331 }
332 }
333