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