• 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 
16 #include "request.h"
17 #include "common_utils.h"
18 #include "constant_definition.h"
19 
20 namespace OHOS {
21 namespace Location {
Request()22 Request::Request()
23 {
24     this->pid_ = -1;
25     this->uid_ = -1;
26     this->tokenId_ = 0;
27     this->firstTokenId_ = 0;
28     this->packageName_ = "";
29     this->isRequesting_ = false;
30     requestConfig_ = new (std::nothrow) RequestConfig();
31     lastLocation_ = new (std::nothrow) Location();
32     isUsingLocationPerm_ = false;
33     isUsingBackgroundPerm_ = false;
34     isUsingApproximatelyPerm_ = false;
35 }
36 
~Request()37 Request::~Request() {}
38 
SetRequestConfig(RequestConfig & requestConfig)39 void Request::SetRequestConfig(RequestConfig& requestConfig)
40 {
41     if (this->requestConfig_ == nullptr) {
42         return;
43     }
44     this->requestConfig_->Set(requestConfig);
45 }
46 
SetLocatorCallBack(const sptr<ILocatorCallback> & callback)47 void Request::SetLocatorCallBack(const sptr<ILocatorCallback>& callback)
48 {
49     this->callBack_ = callback;
50 }
51 
GetRequestConfig()52 sptr<RequestConfig> Request::GetRequestConfig()
53 {
54     return requestConfig_;
55 }
56 
GetLocatorCallBack()57 sptr<ILocatorCallback> Request::GetLocatorCallBack()
58 {
59     return callBack_;
60 }
61 
SetUid(pid_t uid)62 void Request::SetUid(pid_t uid)
63 {
64     this->uid_ = uid;
65 }
66 
GetUid()67 pid_t Request::GetUid()
68 {
69     return uid_;
70 }
71 
SetPid(pid_t pid)72 void Request::SetPid(pid_t pid)
73 {
74     this->pid_ = pid;
75 }
76 
GetPid()77 pid_t Request::GetPid()
78 {
79     return pid_;
80 }
81 
SetTokenId(uint32_t tokenId)82 void Request::SetTokenId(uint32_t tokenId)
83 {
84     this->tokenId_ = tokenId;
85 }
86 
GetTokenId()87 uint32_t Request::GetTokenId()
88 {
89     return tokenId_;
90 }
91 
SetFirstTokenId(uint32_t firstTokenId)92 void Request::SetFirstTokenId(uint32_t firstTokenId)
93 {
94     this->firstTokenId_ = firstTokenId;
95 }
96 
GetFirstTokenId()97 uint32_t Request::GetFirstTokenId()
98 {
99     return firstTokenId_;
100 }
101 
SetPackageName(std::string packageName)102 void Request::SetPackageName(std::string packageName)
103 {
104     this->packageName_ = packageName;
105 }
106 
GetPackageName()107 std::string Request::GetPackageName()
108 {
109     return packageName_;
110 }
111 
GetIsRequesting()112 bool Request::GetIsRequesting()
113 {
114     return isRequesting_;
115 }
116 
SetRequesting(bool state)117 void Request::SetRequesting(bool state)
118 {
119     this->isRequesting_ = state;
120 }
121 
GetLastLocation()122 sptr<Location> Request::GetLastLocation()
123 {
124     return lastLocation_;
125 }
126 
GetUuid()127 std::string Request::GetUuid()
128 {
129     return uuid_;
130 }
131 
SetUuid(std::string uuid)132 void Request::SetUuid(std::string uuid)
133 {
134     this->uuid_ = uuid;
135 }
136 
SetLastLocation(const std::unique_ptr<Location> & location)137 void Request::SetLastLocation(const std::unique_ptr<Location>& location)
138 {
139     if (this->lastLocation_ == nullptr) {
140         return;
141     }
142     this->lastLocation_->SetLatitude(location->GetLatitude());
143     this->lastLocation_->SetLongitude(location->GetLongitude());
144     this->lastLocation_->SetAltitude(location->GetAltitude());
145     this->lastLocation_->SetAccuracy(location->GetAccuracy());
146     this->lastLocation_->SetSpeed(location->GetSpeed());
147     this->lastLocation_->SetDirection(location->GetDirection());
148     this->lastLocation_->SetTimeStamp(location->GetTimeStamp());
149     this->lastLocation_->SetTimeSinceBoot(location->GetTimeSinceBoot());
150 }
151 
GetProxyName(std::shared_ptr<std::list<std::string>> proxys)152 void Request::GetProxyName(std::shared_ptr<std::list<std::string>> proxys)
153 {
154     if (requestConfig_ == nullptr || proxys == nullptr) {
155         return;
156     }
157     switch (requestConfig_->GetScenario()) {
158         case SCENE_NAVIGATION:
159         case SCENE_TRAJECTORY_TRACKING:
160         case SCENE_CAR_HAILING: {
161             proxys->push_back(GNSS_ABILITY);
162             break;
163         }
164         case SCENE_DAILY_LIFE_SERVICE: {
165             proxys->push_back(NETWORK_ABILITY);
166             break;
167         }
168         case SCENE_NO_POWER: {
169             proxys->push_back(PASSIVE_ABILITY);
170             break;
171         }
172         case SCENE_UNSET: {
173             GetProxyNameByPriority(proxys);
174             break;
175         }
176         default:
177             break;
178     }
179 }
180 
GetProxyNameByPriority(std::shared_ptr<std::list<std::string>> proxys)181 void Request::GetProxyNameByPriority(std::shared_ptr<std::list<std::string>> proxys)
182 {
183     if (requestConfig_ == nullptr || proxys == nullptr) {
184         return;
185     }
186     switch (requestConfig_->GetPriority()) {
187         case PRIORITY_ACCURACY:
188             proxys->push_back(GNSS_ABILITY);
189             break;
190         case PRIORITY_LOW_POWER:
191             proxys->push_back(NETWORK_ABILITY);
192             break;
193         case PRIORITY_FAST_FIRST_FIX:
194             proxys->push_back(GNSS_ABILITY);
195             proxys->push_back(NETWORK_ABILITY);
196             break;
197         default:
198             break;
199     }
200 }
201 
GetLocationPermState()202 bool Request::GetLocationPermState()
203 {
204     return isUsingLocationPerm_;
205 }
206 
GetBackgroundPermState()207 bool Request::GetBackgroundPermState()
208 {
209     return isUsingBackgroundPerm_;
210 }
211 
GetApproximatelyPermState()212 bool Request::GetApproximatelyPermState()
213 {
214     return isUsingApproximatelyPerm_;
215 }
216 
SetLocationPermState(bool state)217 void Request::SetLocationPermState(bool state)
218 {
219     isUsingLocationPerm_ = state;
220 }
221 
SetBackgroundPermState(bool state)222 void Request::SetBackgroundPermState(bool state)
223 {
224     isUsingBackgroundPerm_ = state;
225 }
226 
SetApproximatelyPermState(bool state)227 void Request::SetApproximatelyPermState(bool state)
228 {
229     isUsingApproximatelyPerm_ = state;
230 }
231 
ToString() const232 std::string Request::ToString() const
233 {
234     if (requestConfig_ == nullptr) {
235         return "";
236     }
237     std::string str = "[request config: " + requestConfig_->ToString() +
238         "] from pid:" + std::to_string(pid_) +
239         ", uid:" + std::to_string(uid_) +
240         ", tokenId:" + std::to_string(tokenId_) +
241         ", firstTokenId:" + std::to_string(firstTokenId_) +
242         ", uuid:" + uuid_ + ", packageName:" + packageName_;
243     return str;
244 }
245 } // namespace Location
246 } // namespace OHOS