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