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 #ifndef LOCATION_PROXY_ADAPTER_IMPL_H
17 #define LOCATION_PROXY_ADAPTER_IMPL_H
18 
19 #include <map>
20 
21 #if defined(NWEB_LOCATION_ENABLE)
22 #include "i_locator_callback.h"
23 #include "location.h"
24 #include "request_config.h"
25 #endif
26 
27 #include "location_adapter.h"
28 
29 namespace OHOS::NWeb {
30 class LocationRequestConfigImpl : public LocationRequestConfig {
31 public:
32     LocationRequestConfigImpl();
33     virtual ~LocationRequestConfigImpl() = default;
34 
35     void SetScenario(int32_t scenario) override;
36     void SetFixNumber(int32_t number) override;
37     void SetMaxAccuracy(int32_t maxAccuary) override;
38     void SetDistanceInterval(int32_t disInterval) override;
39     void SetTimeInterval(int32_t timeInterval) override;
40     void SetPriority(int32_t priority) override;
41 #if defined(NWEB_LOCATION_ENABLE)
42     std::unique_ptr<OHOS::Location::RequestConfig>& GetConfig();
43 private:
44     std::unique_ptr<OHOS::Location::RequestConfig> config_;
45 #endif
46 };
47 
48 class LocationInfoImpl : public LocationInfo {
49 public:
50 #if defined(NWEB_LOCATION_ENABLE)
51     LocationInfoImpl() = delete;
52     explicit LocationInfoImpl(std::unique_ptr<OHOS::Location::Location>& location);
53 #endif
54     virtual ~LocationInfoImpl() = default;
55 
56     double GetLatitude() override;
57     double GetLongitude() override;
58     double GetAltitude() override;
59     float GetAccuracy() override;
60     float GetSpeed() override;
61     double GetDirection() override;
62     int64_t GetTimeStamp() override;
63     int64_t GetTimeSinceBoot() override;
64     std::vector<std::string> GetAdditions() override;
65 #if defined(NWEB_LOCATION_ENABLE)
66     std::unique_ptr<OHOS::Location::Location>& GetLocation();
67 private:
68     std::unique_ptr<OHOS::Location::Location> location_;
69 #endif
70 };
71 
72 #if defined(NWEB_LOCATION_ENABLE)
73 using LocatorCallbackMap =
74     std::map<int32_t, sptr<OHOS::Location::ILocatorCallback>>;
75 using IsEnableLocationFuncType = bool(*)(bool& isEnabled);
76 using EnableAbilityFuncType = bool(*)(bool enable);
77 using StartLocatingFuncType = bool(*)(
78     std::unique_ptr<OHOS::Location::RequestConfig>& requestConfig,
79     OHOS::sptr<OHOS::Location::ILocatorCallback>& callback);
80 using StopLocatingFuncType = bool(*)(
81     OHOS::sptr<OHOS::Location::ILocatorCallback>& callback);
82 #endif
83 
84 class LocationProxyAdapterImpl : public LocationProxyAdapter {
85 public:
86     LocationProxyAdapterImpl();
87     virtual ~LocationProxyAdapterImpl() = default;
88 
89     int32_t StartLocating(
90         std::shared_ptr<LocationRequestConfig> requestConfig,
91         std::shared_ptr<LocationCallbackAdapter> callback) override;
92     bool StopLocating(int32_t callbackId) override;
93     bool EnableAbility(bool isEnabled) override;
94     bool IsLocationEnabled() override;
95 #if defined(NWEB_LOCATION_ENABLE)
96 private:
97     static void* wrapperHandle_;
98     static IsEnableLocationFuncType isEnableLocationFunc_;
99     static EnableAbilityFuncType enableAbilityFunc_;
100     static StartLocatingFuncType startLocatingFunc_;
101     static StopLocatingFuncType stopLocatingFunc_;
102     LocatorCallbackMap reg_;
103 #endif
104 };
105 }
106 
107 #endif
108