• 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 #ifndef LOCATOR_IMPL_H
16 #define LOCATOR_IMPL_H
17 
18 #include <vector>
19 
20 #include "iremote_object.h"
21 
22 #include "constant_definition.h"
23 #include "country_code.h"
24 #include "country_code_manager.h"
25 #include "geo_address.h"
26 #include "geo_coding_mock_info.h"
27 #include "i_cached_locations_callback.h"
28 #include "locator_proxy.h"
29 #include "i_locating_required_data_callback.h"
30 #include "locating_required_data_config.h"
31 #include "location_data_manager.h"
32 namespace OHOS {
33 namespace Location {
34 class ICallbackResumeManager {
35 public:
36     virtual ~ICallbackResumeManager() = default;
37     virtual void ResumeCallback() = 0;
38 };
39 
40 class LocatorImpl {
41 public:
42     static std::shared_ptr<LocatorImpl> GetInstance();
43     explicit LocatorImpl();
44     ~LocatorImpl();
45     bool Init();
46     bool IsLocationEnabled();
47     void ShowNotification();
48     void RequestPermission();
49     void RequestEnableLocation();
50     void EnableAbility(bool enable);
51     void StartLocating(std::unique_ptr<RequestConfig>& requestConfig,
52         sptr<ILocatorCallback>& callback);
53     void StopLocating(sptr<ILocatorCallback>& callback);
54     std::unique_ptr<Location> GetCachedLocation();
55     bool RegisterSwitchCallback(const sptr<IRemoteObject>& callback, pid_t uid);
56     bool UnregisterSwitchCallback(const sptr<IRemoteObject>& callback);
57     bool RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid);
58     bool UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback);
59     bool RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid);
60     bool UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback);
61     bool RegisterCountryCodeCallback(const sptr<IRemoteObject>& callback, pid_t uid);
62     bool UnregisterCountryCodeCallback(const sptr<IRemoteObject>& callback);
63     void RegisterCachedLocationCallback(std::unique_ptr<CachedGnssLocationsRequest>& request,
64         sptr<ICachedLocationsCallback>& callback);
65     void UnregisterCachedLocationCallback(sptr<ICachedLocationsCallback>& callback);
66     bool IsGeoServiceAvailable();
67     void GetAddressByCoordinate(MessageParcel &data, std::list<std::shared_ptr<GeoAddress>>& replyList);
68     void GetAddressByLocationName(MessageParcel &data, std::list<std::shared_ptr<GeoAddress>>& replyList);
69     bool IsLocationPrivacyConfirmed(const int type);
70     int SetLocationPrivacyConfirmStatus(const int type, bool isConfirmed);
71     int GetCachedGnssLocationsSize();
72     int FlushCachedGnssLocations();
73     bool SendCommand(std::unique_ptr<LocationCommand>& commands);
74     bool AddFence(std::unique_ptr<GeofenceRequest>& request);
75     bool RemoveFence(std::unique_ptr<GeofenceRequest>& request);
76     std::shared_ptr<CountryCode> GetIsoCountryCode();
77     bool EnableLocationMock();
78     bool DisableLocationMock();
79     bool SetMockedLocations(
80         const int timeInterval, const std::vector<std::shared_ptr<Location>> &location);
81     bool EnableReverseGeocodingMock();
82     bool DisableReverseGeocodingMock();
83     bool SetReverseGeocodingMockInfo(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo);
84     bool ProxyUidForFreeze(int32_t uid, bool isProxy);
85     bool ResetAllProxy();
86 
87     LocationErrCode IsLocationEnabledV9(bool &isEnabled);
88     LocationErrCode EnableAbilityV9(bool enable);
89     LocationErrCode StartLocatingV9(std::unique_ptr<RequestConfig>& requestConfig,
90         sptr<ILocatorCallback>& callback);
91     LocationErrCode StopLocatingV9(sptr<ILocatorCallback>& callback);
92     LocationErrCode GetCachedLocationV9(std::unique_ptr<Location> &loc);
93     LocationErrCode RegisterSwitchCallbackV9(const sptr<IRemoteObject>& callback);
94     LocationErrCode UnregisterSwitchCallbackV9(const sptr<IRemoteObject>& callback);
95     LocationErrCode RegisterGnssStatusCallbackV9(const sptr<IRemoteObject>& callback);
96     LocationErrCode UnregisterGnssStatusCallbackV9(const sptr<IRemoteObject>& callback);
97     LocationErrCode RegisterNmeaMessageCallbackV9(const sptr<IRemoteObject>& callback);
98     LocationErrCode UnregisterNmeaMessageCallbackV9(const sptr<IRemoteObject>& callback);
99     LocationErrCode RegisterCountryCodeCallbackV9(const sptr<IRemoteObject>& callback);
100     LocationErrCode UnregisterCountryCodeCallbackV9(const sptr<IRemoteObject>& callback);
101     LocationErrCode RegisterCachedLocationCallbackV9(std::unique_ptr<CachedGnssLocationsRequest>& request,
102         sptr<ICachedLocationsCallback>& callback);
103     LocationErrCode UnregisterCachedLocationCallbackV9(sptr<ICachedLocationsCallback>& callback);
104     LocationErrCode IsGeoServiceAvailableV9(bool &isAvailable);
105     LocationErrCode GetAddressByCoordinateV9(MessageParcel &data,
106         std::list<std::shared_ptr<GeoAddress>>& replyList);
107     LocationErrCode GetAddressByLocationNameV9(MessageParcel &data,
108         std::list<std::shared_ptr<GeoAddress>>& replyList);
109     LocationErrCode IsLocationPrivacyConfirmedV9(const int type, bool &isConfirmed);
110     LocationErrCode SetLocationPrivacyConfirmStatusV9(const int type, bool isConfirmed);
111     LocationErrCode GetCachedGnssLocationsSizeV9(int &size);
112     LocationErrCode FlushCachedGnssLocationsV9();
113     LocationErrCode SendCommandV9(std::unique_ptr<LocationCommand>& commands);
114     LocationErrCode AddFenceV9(std::unique_ptr<GeofenceRequest>& request);
115     LocationErrCode RemoveFenceV9(std::unique_ptr<GeofenceRequest>& request);
116     LocationErrCode GetIsoCountryCodeV9(std::shared_ptr<CountryCode>& countryCode);
117     LocationErrCode EnableLocationMockV9();
118     LocationErrCode DisableLocationMockV9();
119     LocationErrCode SetMockedLocationsV9(
120         const int timeInterval, const std::vector<std::shared_ptr<Location>> &location);
121     LocationErrCode EnableReverseGeocodingMockV9();
122     LocationErrCode DisableReverseGeocodingMockV9();
123     LocationErrCode SetReverseGeocodingMockInfoV9(std::vector<std::shared_ptr<GeocodingMockInfo>>& mockInfo);
124     LocationErrCode ProxyUidForFreezeV9(int32_t uid, bool isProxy);
125     LocationErrCode ResetAllProxyV9();
126     LocationErrCode RegisterLocatingRequiredDataCallback(std::unique_ptr<LocatingRequiredDataConfig>& dataConfig,
127         sptr<ILocatingRequiredDataCallback>& callback);
128     LocationErrCode UnRegisterLocatingRequiredDataCallback(sptr<ILocatingRequiredDataCallback>& callback);
129     void ResetLocatorProxy(const wptr<IRemoteObject> &remote);
130     void SetResumer(std::shared_ptr<ICallbackResumeManager> resumer);
131 
132 private:
133     class LocatorDeathRecipient : public IRemoteObject::DeathRecipient {
134     public:
LocatorDeathRecipient(LocatorImpl & impl)135         explicit LocatorDeathRecipient(LocatorImpl &impl) : impl_(impl) {}
136         ~LocatorDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)137         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
138         {
139             impl_.ResetLocatorProxy(remote);
140         }
141     private:
142         LocatorImpl &impl_;
143     };
144 
145 private:
146     sptr<LocatorProxy> GetProxy();
147     bool IsCallbackResuming();
148     void UpdateCallbackResumingState(bool state);
149 
150     sptr<LocatorProxy> client_ { nullptr };
151     sptr<IRemoteObject::DeathRecipient> recipient_ { nullptr };
152     std::shared_ptr<ICallbackResumeManager> resumer_ { nullptr };
153     std::shared_ptr<LocationDataManager> locationDataManager_ { nullptr };
154     bool isServerExist_ = false;
155     bool isCallbackResuming_ = false;
156     std::mutex mutex_;
157     std::mutex resumeMutex_;
158     static std::mutex locatorMutex_;
159     static std::shared_ptr<LocatorImpl> instance_;
160     std::shared_ptr<CountryCodeManager> countryCodeManager_ = nullptr;
161 };
162 }  // namespace Location
163 }  // namespace OHOS
164 #endif // LOCATOR_IMPL_H
165