• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "geofence_request.h"
17 #include <parcel.h>
18 #include "common_utils.h"
19 #ifdef NOTIFICATION_ENABLE
20 #include "notification_request.h"
21 #endif
22 #include "iremote_object.h"
23 #include "want_agent.h"
24 
25 namespace OHOS {
26 namespace Location {
GeofenceRequest()27 GeofenceRequest::GeofenceRequest()
28 {
29     callback_ = nullptr;
30     scenario_ = -1;
31     fenceId_ = -1;
32     uid_ = 0;
33     appAliveStatus_ = true;
34 }
35 
GeofenceRequest(GeofenceRequest & geofenceRequest)36 GeofenceRequest::GeofenceRequest(GeofenceRequest& geofenceRequest)
37 {
38     this->SetGeofence(geofenceRequest.GetGeofence());
39     this->SetScenario(geofenceRequest.GetScenario());
40     this->SetWantAgent(geofenceRequest.GetWantAgent());
41     this->SetGeofenceTransitionEventList(geofenceRequest.GetGeofenceTransitionEventList());
42 #ifdef NOTIFICATION_ENABLE
43     this->SetNotificationRequestList(geofenceRequest.GetNotificationRequestList());
44 #endif
45     this->SetGeofenceTransitionCallback(geofenceRequest.GetGeofenceTransitionCallback());
46     this->SetFenceId(geofenceRequest.GetFenceId());
47     this->SetBundleName(geofenceRequest.GetBundleName());
48 }
49 
~GeofenceRequest()50 GeofenceRequest::~GeofenceRequest() {}
51 
GetGeofence()52 GeoFence GeofenceRequest::GetGeofence()
53 {
54     return geofence_;
55 }
56 
SetGeofence(GeoFence geofence)57 void GeofenceRequest::SetGeofence(GeoFence geofence)
58 {
59     geofence_ = geofence;
60 }
61 
GetScenario()62 int GeofenceRequest::GetScenario()
63 {
64     return scenario_;
65 }
66 
SetScenario(int scenario)67 void GeofenceRequest::SetScenario(int scenario)
68 {
69     scenario_ = scenario;
70 }
71 
SetWantAgent(const AbilityRuntime::WantAgent::WantAgent wantAgent)72 void GeofenceRequest::SetWantAgent(const AbilityRuntime::WantAgent::WantAgent wantAgent)
73 {
74     wantAgent_ = wantAgent;
75 }
76 
GetWantAgent()77 AbilityRuntime::WantAgent::WantAgent GeofenceRequest::GetWantAgent()
78 {
79     return wantAgent_;
80 }
81 
GetGeofenceTransitionEventList()82 std::vector<GeofenceTransitionEvent> GeofenceRequest::GetGeofenceTransitionEventList()
83 {
84     std::unique_lock<std::mutex> lock(geofenceRequestMutex_);
85     return transitionStatusList_;
86 }
87 
SetGeofenceTransitionEvent(GeofenceTransitionEvent status)88 void GeofenceRequest::SetGeofenceTransitionEvent(GeofenceTransitionEvent status)
89 {
90     std::unique_lock<std::mutex> lock(geofenceRequestMutex_);
91     transitionStatusList_.push_back(status);
92 }
93 
SetGeofenceTransitionEventList(std::vector<GeofenceTransitionEvent> statusList)94 void GeofenceRequest::SetGeofenceTransitionEventList(std::vector<GeofenceTransitionEvent> statusList)
95 {
96     std::unique_lock<std::mutex> lock(geofenceRequestMutex_);
97     for (auto it = statusList.begin(); it != statusList.end(); ++it) {
98         transitionStatusList_.push_back(*it);
99     }
100 }
101 
102 #ifdef NOTIFICATION_ENABLE
GetNotificationRequestList()103 std::vector<OHOS::Notification::NotificationRequest> GeofenceRequest::GetNotificationRequestList()
104 {
105     std::unique_lock<std::mutex> lock(geofenceRequestMutex_);
106     return notificationRequestList_;
107 }
108 
SetNotificationRequest(OHOS::Notification::NotificationRequest request)109 void GeofenceRequest::SetNotificationRequest(OHOS::Notification::NotificationRequest request)
110 {
111     std::unique_lock<std::mutex> lock(geofenceRequestMutex_);
112     notificationRequestList_.push_back(request);
113 }
114 
SetNotificationRequestList(std::vector<OHOS::Notification::NotificationRequest> requestList)115 void GeofenceRequest::SetNotificationRequestList(std::vector<OHOS::Notification::NotificationRequest> requestList)
116 {
117     std::unique_lock<std::mutex> lock(geofenceRequestMutex_);
118     for (auto it = requestList.begin(); it != requestList.end(); ++it) {
119         notificationRequestList_.push_back(*it);
120     }
121 }
122 #endif
123 
SetGeofenceTransitionCallback(const sptr<IRemoteObject> & callback)124 void GeofenceRequest::SetGeofenceTransitionCallback(const sptr<IRemoteObject>& callback)
125 {
126     callback_ = callback;
127 }
128 
GetGeofenceTransitionCallback()129 sptr<IRemoteObject> GeofenceRequest::GetGeofenceTransitionCallback()
130 {
131     return callback_;
132 }
133 
GetFenceId()134 int GeofenceRequest::GetFenceId()
135 {
136     return fenceId_;
137 }
138 
SetFenceId(int fenceId)139 void GeofenceRequest::SetFenceId(int fenceId)
140 {
141     fenceId_ = fenceId;
142 }
143 
GetBundleName()144 const std::string& GeofenceRequest::GetBundleName()
145 {
146     return bundleName_;
147 }
148 
SetBundleName(const std::string & bundleName)149 void GeofenceRequest::SetBundleName(const std::string& bundleName)
150 {
151     bundleName_ = bundleName;
152 }
153 
GetUid()154 int32_t GeofenceRequest::GetUid()
155 {
156     return uid_;
157 }
158 
SetUid(int32_t uid)159 void GeofenceRequest::SetUid(int32_t uid)
160 {
161     uid_ = uid;
162 }
163 
GetAppAliveStatus()164 bool GeofenceRequest::GetAppAliveStatus()
165 {
166     return appAliveStatus_;
167 }
168 
GetRequestExpirationTime()169 int64_t GeofenceRequest::GetRequestExpirationTime()
170 {
171     return requestExpirationTime_;
172 }
173 
SetRequestExpirationTime(int64_t requestExpirationTime)174 void GeofenceRequest::SetRequestExpirationTime(int64_t requestExpirationTime)
175 {
176     requestExpirationTime_ = requestExpirationTime;
177 }
178 
SetAppAliveStatus(bool appAliveStatus)179 void GeofenceRequest::SetAppAliveStatus(bool appAliveStatus)
180 {
181     appAliveStatus_ = appAliveStatus;
182 }
183 
ReadFromParcel(Parcel & data)184 void GeofenceRequest::ReadFromParcel(Parcel& data)
185 {
186     std::unique_lock<std::mutex> lock(geofenceRequestMutex_);
187     scenario_ = data.ReadInt32();
188     geofence_.latitude = data.ReadDouble();
189     geofence_.longitude = data.ReadDouble();
190     geofence_.radius = data.ReadDouble();
191     geofence_.expiration = data.ReadDouble();
192     geofence_.coordinateSystemType = static_cast<CoordinateSystemType>(data.ReadInt32());
193     int monitorGeofenceTransitionSize = data.ReadInt32();
194     if (monitorGeofenceTransitionSize > MAX_TRANSITION_SIZE) {
195         LBSLOGE(LOCATOR, "fence transition list size should not be greater than 3");
196         return;
197     }
198     for (int i = 0; i < monitorGeofenceTransitionSize; i++) {
199         transitionStatusList_.push_back(static_cast<GeofenceTransitionEvent>(data.ReadInt32()));
200     }
201 #ifdef NOTIFICATION_ENABLE
202     int requestSize = data.ReadInt32();
203     if (requestSize > MAX_NOTIFICATION_REQUEST_LIST_SIZE) {
204         LBSLOGE(LOCATOR, "request size should not be greater than 3");
205         return;
206     }
207     for (int i = 0; i < requestSize; i++) {
208         auto request = OHOS::Notification::NotificationRequest::Unmarshalling(data);
209         if (request != nullptr) {
210             notificationRequestList_.push_back(*request);
211             delete request;
212         }
213     }
214 #endif
215     callback_ = data.ReadObject<IRemoteObject>();
216     bundleName_ = data.ReadString();
217     uid_ = data.ReadInt32();
218     auto wantAgent = data.ReadParcelable<AbilityRuntime::WantAgent::WantAgent>();
219     if (wantAgent != nullptr) {
220         wantAgent_ = *(wantAgent);
221         delete wantAgent;
222     }
223 }
224 
Marshalling(Parcel & parcel) const225 bool GeofenceRequest::Marshalling(Parcel& parcel) const
226 {
227     std::unique_lock<std::mutex> lock(geofenceRequestMutex_);
228     parcel.WriteInt32(scenario_);
229     parcel.WriteDouble(geofence_.latitude);
230     parcel.WriteDouble(geofence_.longitude);
231     parcel.WriteDouble(geofence_.radius);
232     parcel.WriteDouble(geofence_.expiration);
233     parcel.WriteInt32(static_cast<int>(geofence_.coordinateSystemType));
234     if (transitionStatusList_.size() > MAX_TRANSITION_SIZE) {
235         LBSLOGE(LOCATOR, "fence transition list size should not be greater than 3");
236         return false;
237     }
238     parcel.WriteInt32(transitionStatusList_.size());
239     for (size_t i = 0; i < transitionStatusList_.size(); i++) {
240         parcel.WriteInt32(static_cast<int>(transitionStatusList_[i]));
241     }
242 #ifdef NOTIFICATION_ENABLE
243     if (notificationRequestList_.size() > MAX_NOTIFICATION_REQUEST_LIST_SIZE) {
244         LBSLOGE(LOCATOR, "request size should not be greater than 3");
245         return false;
246     }
247     parcel.WriteInt32(notificationRequestList_.size());
248     for (size_t i = 0; i < notificationRequestList_.size(); i++) {
249         notificationRequestList_[i].Marshalling(parcel);
250     }
251 #endif
252     parcel.WriteRemoteObject(callback_);
253     parcel.WriteString(bundleName_);
254     parcel.WriteInt32(uid_);
255     parcel.WriteParcelable(&wantAgent_);
256     return true;
257 }
258 
Unmarshalling(Parcel & parcel)259 std::shared_ptr<GeofenceRequest> GeofenceRequest::Unmarshalling(Parcel& parcel)
260 {
261     std::shared_ptr<GeofenceRequest> geofenceRequest = std::make_shared<GeofenceRequest>();
262     geofenceRequest->ReadFromParcel(parcel);
263     return geofenceRequest;
264 }
265 } // namespace Location
266 } // namespace OHOS