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