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 #include "locator_event_manager.h"
17
18 #include <vector>
19
20 #include "constant_definition.h"
21
22 namespace OHOS {
23 namespace Location {
PutInt(const std::string & name,int value)24 void DftEvent::PutInt(const std::string& name, int value)
25 {
26 std::unique_lock<std::mutex> lock(mutex_);
27 intValues_.emplace(name, value);
28 }
29
GetInt(const std::string & name)30 int DftEvent::GetInt(const std::string& name)
31 {
32 std::unique_lock<std::mutex> lock(mutex_);
33 auto it = intValues_.find(name);
34 if (it == intValues_.end()) {
35 return 0;
36 }
37 return it->second;
38 }
39
DftHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner)40 DftHandler::DftHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner) : EventHandler(runner) {}
41
~DftHandler()42 DftHandler::~DftHandler() {}
43
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)44 void DftHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
45 {
46 }
47
LocatorDftManager()48 LocatorDftManager::LocatorDftManager()
49 {
50 distributeSissionCnt_ = 0;
51 distributeDisconnectCnt_ = 0;
52
53 handler_ = std::make_shared<DftHandler>(AppExecFwk::EventRunner::Create(true));
54 }
55
~LocatorDftManager()56 LocatorDftManager::~LocatorDftManager() {}
57
Init()58 void LocatorDftManager::Init()
59 {
60 if (handler_ != nullptr) {
61 handler_->SendHighPriorityEvent(EVENT_SEND_DAILY_REPORT, 0, DAILY_INTERVAL);
62 }
63 }
64
IpcCallingErr(int error)65 void LocatorDftManager::IpcCallingErr(int error)
66 {
67 }
68
LocationSessionStart(std::shared_ptr<Request> request)69 void LocatorDftManager::LocationSessionStart(std::shared_ptr<Request> request)
70 {
71 std::shared_ptr<AppRequestCount> requestCount = nullptr;
72 if (request == nullptr) {
73 return;
74 }
75
76 std::unique_lock<std::mutex> lock(mutex_);
77 for (auto appRequest : appRequests_) {
78 if (appRequest->packageName.compare(request->GetPackageName()) == 0) {
79 requestCount = appRequest;
80 break;
81 }
82 }
83 if (requestCount == nullptr) {
84 requestCount = std::make_shared<AppRequestCount>();
85 requestCount->packageName = request->GetPackageName();
86 requestCount->requestType = std::vector<int>(DFT_APP_MAX_SIZE);
87 requestCount->count = std::vector<int>(DFT_APP_MAX_SIZE);
88 appRequests_.push_back(requestCount);
89 }
90
91 unsigned index = INVALID_VALUE;
92 if (request->GetRequestConfig() == nullptr) {
93 return;
94 }
95 int scenario = request->GetRequestConfig()->GetScenario();
96 int priority = request->GetRequestConfig()->GetPriority();
97 for (unsigned i = 0; i < requestCount->requestType.size(); i++) {
98 if (requestCount->requestType.at(i) == scenario || requestCount->requestType.at(i) == priority) {
99 index = i;
100 break;
101 }
102 }
103 if (index != INVALID_VALUE) {
104 requestCount->count.at(index)++;
105 } else {
106 if (scenario != SCENE_UNSET) {
107 requestCount->requestType.at(0) = scenario;
108 } else if (priority != PRIORITY_UNSET) {
109 requestCount->requestType.at(0) = priority;
110 } else {
111 return;
112 }
113 requestCount->count.at(0)++;
114 }
115 }
116
DistributionDisconnect()117 void LocatorDftManager::DistributionDisconnect()
118 {
119 if (distributeDisconnectCnt_ < COUNT_MAX) {
120 distributeDisconnectCnt_++;
121 }
122 }
123
DistributionSessionStart()124 void LocatorDftManager::DistributionSessionStart()
125 {
126 if (distributeSissionCnt_ < COUNT_MAX) {
127 distributeSissionCnt_++;
128 }
129 }
130
SendDistributionDailyCount()131 void LocatorDftManager::SendDistributionDailyCount()
132 {
133 }
134
SendRequestDailyCount()135 void LocatorDftManager::SendRequestDailyCount()
136 {
137 }
138
GetTopRequest()139 std::shared_ptr<AppRequestCount> LocatorDftManager::GetTopRequest()
140 {
141 std::shared_ptr<AppRequestCount> topRequest;
142 int topSum = 0;
143 std::unique_lock<std::mutex> lock(mutex_);
144 for (auto appRequest : appRequests_) {
145 int sum = 0;
146 for (unsigned i = 0; i < appRequest->count.size(); i++) {
147 sum = sum + appRequest->count.at(i);
148 }
149 if (sum > topSum) {
150 topSum = sum;
151 topRequest = appRequest;
152 }
153 }
154 return topRequest;
155 }
156 } // namespace Location
157 } // namespace OHOS