1 /*
2  * Copyright (c) 2024 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 LOCATOR_CALLBACK_H
17 #define LOCATOR_CALLBACK_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <string>
22 #include <vector>
23 #include "i_locator_callback.h"
24 #include "locator_callback_napi.h"
25 #include "location.h"
26 #include "geolocationmanager_utils.h"
27 
28 namespace OHOS {
29 namespace GeoLocationManager {
30 class LocatorCallback : public IRemoteStub<Location::ILocatorCallback> {
31 public:
32     LocatorCallback();
33     LocatorCallback(int64_t callbackId);
34     virtual ~LocatorCallback();
35     virtual int OnRemoteRequest(uint32_t code,
36         MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
37 
38     void OnLocationReport(const std::unique_ptr<Location::Location>& location) override;
39     void OnLocatingStatusChange(const int status) override;
40     void OnErrorReport(const int errorCode) override;
41     void InitLatch();
42     bool IsSingleLocationRequest();
43     void CountDown();
44     void Wait(int time);
45     int GetCount();
46     void SetCount(int count);
47 
GetFixNumber() const48     inline int GetFixNumber() const
49     {
50         return fixNumber_;
51     }
52 
SetFixNumber(const int fixNumber)53     inline void SetFixNumber(const int fixNumber)
54     {
55         fixNumber_ = fixNumber;
56     }
57 
SetLocationPriority(const int locationPriority)58     inline void SetLocationPriority(const int locationPriority)
59     {
60         locationPriority_ = locationPriority;
61     }
62 
GetLocationPriority()63     inline int GetLocationPriority()
64     {
65         return locationPriority_;
66     }
67 
GetSingleLocation()68     inline std::shared_ptr<Location::Location> GetSingleLocation()
69     {
70         std::unique_lock<std::mutex> guard(mutex_);
71         return singleLocation_;
72     }
73 
GetCallBackId()74     int64_t GetCallBackId()
75     {
76         return callbackId_;
77     };
78 
79     bool NeedSetSingleLocation(const std::unique_ptr<Location::Location>& location);
80     bool IfReportAccuracyLocation();
81     void SetSingleLocation(const std::unique_ptr<Location::Location>& location);
82 
83 private:
84     int64_t callbackId_ = -1;
85     std::function<void(const std::unique_ptr<Location::Location>& location)> callback_ = nullptr;
86     int fixNumber_;
87     std::mutex mutex_;
88     Location::CountDownLatch* latch_;
89     std::shared_ptr<Location::Location> singleLocation_;
90     int locationPriority_;
91     bool inHdArea_;
92     bool callbackValid_;
93 };
94 }
95 }
96 
97 #endif // LOCATOR_CALLBACK_H