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 LOCATOR_EVENT_MANAGER_H
17 #define LOCATOR_EVENT_MANAGER_H
18 
19 #include <list>
20 #include <map>
21 #include <singleton.h>
22 #include <string>
23 
24 #include "event_handler.h"
25 #include "event_runner.h"
26 
27 #include "request.h"
28 
29 namespace OHOS {
30 namespace Location {
31 constexpr uint32_t EVENT_SEND_HIVIEW_MESSAGE = 0x0001;
32 constexpr uint32_t EVENT_SEND_DAILY_REPORT = 0x0002;
33 
34 constexpr uint32_t DAILY_INTERVAL = 24 * 60 * 60 * 1000;
35 constexpr uint32_t COUNT_MAX = 500;
36 constexpr uint32_t INVALID_VALUE = 0xFFFFFFFF;
37 constexpr uint32_t DFT_APP_MAX_SIZE = 10;
38 constexpr uint32_t DFT_TOP_REQUEST_UPLOAD_MAX_SIZE = 5;
39 
40 struct AppRequestCount {
41     std::string packageName;
42     std::vector<int> requestType;
43     std::vector<int> count;
44 };
45 
46 class DftEvent {
47 public:
48     void PutInt(const std::string& name, int value);
49     int GetInt(const std::string& name);
50 private:
51     std::map<std::string, int> intValues_;
52     std::mutex mutex_;
53 };
54 
55 class DftHandler : public AppExecFwk::EventHandler {
56 public:
57     explicit DftHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
58     ~DftHandler() override;
59     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override;
60 };
61 
62 class LocatorDftManager {
63 public:
64     static LocatorDftManager* GetInstance();
65     LocatorDftManager();
66     ~LocatorDftManager();
67     void Init();
68     void IpcCallingErr(int error);
69     void LocationSessionStart(std::shared_ptr<Request> request);
70     void DistributionSessionStart();
71     void DistributionDisconnect();
72     void SendDistributionDailyCount();
73     void SendRequestDailyCount();
74 private:
75     std::shared_ptr<AppRequestCount> GetTopRequest();
76 
77     std::shared_ptr<DftHandler> handler_;
78     std::mutex mutex_;
79     std::list<std::shared_ptr<AppRequestCount>> appRequests_;
80     uint32_t distributeSissionCnt_;
81     uint32_t distributeDisconnectCnt_;
82 };
83 } // namespace Location
84 } // namespace OHOS
85 #endif // LOCATOR_EVENT_MANAGER_H
86