merge with master
[framework/osp/locations.git] / src / FLoc_LocationImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <new>
19 #include <unique_ptr.h>
20 #include <FBaseColArrayList.h>
21 #include <FBaseSysLog.h>
22 #include <FBaseDouble.h>
23 #include <FBaseDouble.h>
24 #include <FBaseUtilMath.h>
25 #include <FLocCoordinates.h>
26 #include <FLocLocation.h>
27 #include "FLoc_LocationImpl.h"
28 #include "FLoc_Types.h"
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Base::Collection;
32 using namespace Tizen::Base::Utility;
33 using namespace std;
34
35 namespace Tizen { namespace Locations
36 {
37
38 _LocationImpl::_LocationImpl(void)
39         : __speed(Tizen::Locations::NaN)
40         , __course(Tizen::Locations::NaN)
41         , __horizontalAccuracy(Tizen::Locations::NaN)
42         , __verticalAccuracy(Tizen::Locations::NaN)
43         , __timestamp(0)
44         , __isLocationValid(false)
45         , __isDenied(false)
46 {
47 }
48
49 _LocationImpl::_LocationImpl(const _LocationImpl& rhs)
50         : __coordinate(rhs.__coordinate)
51         , __speed(rhs.__speed)
52         , __course(rhs.__course)
53         , __horizontalAccuracy(rhs.__horizontalAccuracy)
54         , __verticalAccuracy(rhs.__verticalAccuracy)
55         , __timestamp(rhs.__timestamp)
56         , __locationMethod(rhs.__locationMethod)
57         , __satelliteInformation(rhs.__satelliteInformation)
58         , __isLocationValid(rhs.__isLocationValid)
59         , __isDenied(rhs.__isDenied)
60 {
61 }
62
63 _LocationImpl::~_LocationImpl(void)
64 {
65 }
66
67 bool
68 _LocationImpl::Equals(const _LocationImpl& rhs) const
69 {
70         return *this == rhs;
71 }
72
73 int
74 _LocationImpl::GetHashCode(void) const
75 {
76         int hashCode = 0;
77
78         hashCode += __coordinate.GetHashCode();
79         hashCode += Double::GetHashCode(__speed) * 37;
80         hashCode += Double::GetHashCode(__course) * 37;
81         hashCode += Double::GetHashCode(__horizontalAccuracy) * 37;
82         hashCode += Double::GetHashCode(__verticalAccuracy) * 37;
83         hashCode += __locationMethod.GetHashCode();
84         hashCode += __satelliteInformation.GetHashCode();
85         hashCode *= (__isLocationValid ? 37 : 17);
86
87         if (hashCode < 0)
88         {
89                 hashCode *= (-1);
90         }
91
92         return hashCode;
93 }
94
95 DateTime
96 _LocationImpl::GetTimestamp(void) const
97 {
98         TimeSpan timespan(__timestamp);
99         DateTime dateTime;
100
101         dateTime.SetValue(1970, 1, 1);
102         dateTime.Add(timespan);
103
104         SysLog(NID_LOC, "The location timeStamp is (%ls)", dateTime.ToString().GetPointer());
105
106         return dateTime;
107 }
108
109 Tizen::Base::String
110 _LocationImpl::GetExtraInfo(const Tizen::Base::String& key) const
111 {
112         String reqValue = L"";
113
114         if (key.Equals(L"location_method", true))
115         {
116                 reqValue = __locationMethod;
117         }
118         else if (key.Equals(L"satellite", true))
119         {
120                 reqValue = __satelliteInformation;
121         }
122
123         SysLog(NID_LOC, "Requested information is '%ls' for the key '%ls'.", reqValue.GetPointer(), key.GetPointer());
124
125         return reqValue;
126 }
127
128 _LocationImpl*
129 _LocationImpl::GetInstance(Location& obj)
130 {
131         return obj.__pImpl;
132 }
133
134 const _LocationImpl*
135 _LocationImpl::GetInstance(const Location& obj)
136 {
137         return obj.__pImpl;
138 }
139
140 Location*
141 _LocationImpl::GetLocationInstanceN(void)
142 {
143         Location* pLoc = new (std::nothrow) Location();
144         SysTryReturn(NID_LOC, pLoc != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
145         return pLoc;
146 }
147
148 Location
149 _LocationImpl::GetLocationInstance(void)
150 {
151         Location invalidLoc;
152
153         return invalidLoc;
154 }
155
156 result
157 _LocationImpl::SetCoordinates(const Coordinates& coordinate)
158 {
159         result r = E_SUCCESS;
160
161         r = __coordinate.Set(coordinate.GetLatitude(), coordinate.GetLongitude(), coordinate.GetAltitude());
162         SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Setting the latitude/longitude value failed.", GetErrorMessage(r));
163
164         return E_SUCCESS;
165 }
166
167 void
168 _LocationImpl::SetExtraInfo(const Tizen::Base::String& key, const Tizen::Base::String& value)
169 {
170         if (key.Equals(L"location_method", true))
171         {
172                 __locationMethod = value;
173         }
174         else if (key.Equals(L"satellite", true))
175         {
176                 __satelliteInformation = value;
177         }
178 }
179
180 _LocationImpl&
181 _LocationImpl::operator =(const _LocationImpl& rhs)
182 {
183         if (this == &rhs)
184         {
185                 return *this;
186         }
187
188         __coordinate = rhs.__coordinate;
189         __speed = rhs.__speed;
190         __course = rhs.__course;
191         __horizontalAccuracy = rhs.__horizontalAccuracy;
192         __verticalAccuracy = rhs.__verticalAccuracy;
193         __timestamp = rhs.__timestamp;
194         __locationMethod = rhs.__locationMethod;
195         __satelliteInformation = rhs.__satelliteInformation;
196         __isLocationValid = rhs.__isLocationValid;
197
198         return *this;
199 }
200
201 bool
202 _LocationImpl::operator ==(const _LocationImpl& rhs) const
203 {
204
205         if (!__coordinate.Equals(rhs.__coordinate))
206         {
207                 return false;
208         }
209
210         if (__timestamp != rhs.__timestamp)
211         {
212                 return false;
213         }
214
215         if (__locationMethod != rhs.__locationMethod)
216         {
217                 return false;
218         }
219
220         if (!__satelliteInformation.Equals(rhs.__satelliteInformation))
221         {
222                 return false;
223         }
224
225         if (__isLocationValid != rhs.__isLocationValid)
226         {
227                 return false;
228         }
229
230         bool speedCheck = false;
231         bool courseCheck = false;
232         bool horAccCheck = false;
233         bool verAccCheck = false;
234
235         if (Double::IsNaN(__speed) || Double::IsNaN(rhs.__speed))
236         {
237                 if (Double::IsNaN(__speed) && Double::IsNaN(rhs.__speed))
238                 {
239                         speedCheck = true;
240                 }
241         }
242         else if (Double::Compare(__speed, rhs.__speed) == 0)
243         {
244                 speedCheck = true;
245         }
246
247         if (Double::IsNaN(__course) || Double::IsNaN(rhs.__course))
248         {
249                 if (Double::IsNaN(__course) && Double::IsNaN(rhs.__course))
250                 {
251                         courseCheck = true;
252                 }
253         }
254         else if (Double::Compare(__course, rhs.__course) == 0)
255         {
256                 courseCheck = true;
257         }
258
259         if (Double::IsNaN(__horizontalAccuracy) || Double::IsNaN(rhs.__horizontalAccuracy))
260         {
261                 if (Double::IsNaN(__horizontalAccuracy) && Double::IsNaN(rhs.__horizontalAccuracy))
262                 {
263                         horAccCheck = true;
264                 }
265         }
266         else if (Double::Compare(__horizontalAccuracy, rhs.__horizontalAccuracy) == 0)
267         {
268                 horAccCheck = true;
269         }
270
271         if (Double::IsNaN(__verticalAccuracy) || Double::IsNaN(rhs.__verticalAccuracy))
272         {
273                 if (Double::IsNaN(__verticalAccuracy) && Double::IsNaN(rhs.__verticalAccuracy))
274                 {
275                         verAccCheck = true;
276                 }
277         }
278         else if (Double::Compare(__verticalAccuracy, rhs.__verticalAccuracy) == 0)
279         {
280                 verAccCheck = true;
281         }
282
283         return (speedCheck && courseCheck && horAccCheck && verAccCheck);
284 }
285
286 }}