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 
16 #ifndef REPORT_MANAGER_H
17 #define REPORT_MANAGER_H
18 
19 #include <singleton.h>
20 #include <string>
21 #include <time.h>
22 #include <map>
23 
24 #include "i_locator_callback.h"
25 #include "location.h"
26 #include "request.h"
27 #include <mutex>
28 
29 namespace OHOS {
30 namespace Location {
31 class ReportManager {
32 public:
33     ReportManager();
34     ~ReportManager();
35     bool ReportRemoteCallback(sptr<ILocatorCallback>& locatorCallback, int type, int result);
36     bool OnReportLocation(const std::unique_ptr<Location>& location, std::string abilityName);
37     bool ResultCheck(const std::unique_ptr<Location>& location, const std::shared_ptr<Request>& request);
38     void UpdateCacheLocation(const std::unique_ptr<Location>& location, std::string abilityName);
39     std::unique_ptr<Location> GetLastLocation();
40     std::unique_ptr<Location> GetCacheLocation(const std::shared_ptr<Request>& request);
41     std::unique_ptr<Location> GetPermittedLocation(const std::shared_ptr<Request>& request,
42         const std::unique_ptr<Location>& location);
43     void UpdateRandom();
44     bool IsRequestFuse(const std::shared_ptr<Request>& request);
45     void UpdateLocationByRequest(const uint32_t tokenId, const uint64_t tokenIdEx,
46         std::unique_ptr<Location>& location);
47     bool IsAppBackground(std::string bundleName, uint32_t tokenId, uint64_t tokenIdEx, pid_t uid, pid_t pid);
48     static ReportManager* GetInstance();
49     bool IsCacheGnssLocationValid();
50 
51 private:
52     struct timespec lastUpdateTime_;
53     double offsetRandom_;
54     std::map<int, std::shared_ptr<Location>> lastLocationsMap_;
55     Location cacheGnssLocation_;
56     Location cacheNlpLocation_;
57     std::mutex lastLocationMutex_;
58     std::atomic<int64_t> lastResetRecordTime_;
59     std::unique_ptr<Location> ApproximatelyLocation(const std::unique_ptr<Location>& location);
60     bool ProcessRequestForReport(std::shared_ptr<Request>& request,
61         std::unique_ptr<std::list<std::shared_ptr<Request>>>& deadRequests,
62         const std::unique_ptr<Location>& location, std::string abilityName);
63     void WriteNetWorkReportEvent(std::string abilityName, const std::shared_ptr<Request>& request,
64         const std::unique_ptr<Location>& location);
65     std::unique_ptr<Location> ExecuteReportProcess(std::shared_ptr<Request>& request,
66         std::unique_ptr<Location>& location, std::string abilityName);
67     void UpdateLastLocation(const std::unique_ptr<Location>& location);
68     void LocationReportDelayTimeCheck(const std::unique_ptr<Location>& location,
69         const std::shared_ptr<Request>& request);
70 };
71 } // namespace OHOS
72 } // namespace Location
73 #endif // REPORT_MANAGER_H
74