4fd801447ae5e3ffb1ae9bd6f91ce484fc6afa10
[platform/framework/native/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         return hashCode;
92 }
93
94 DateTime
95 _LocationImpl::GetTimestamp(void) const
96 {
97         TimeSpan timespan(__timestamp);
98         DateTime dateTime;
99
100         dateTime.SetValue(1970, 1, 1);
101         dateTime.Add(timespan);
102         SysSecureLog(NID_LOC, "The location timeStamp is (%ls)", dateTime.ToString().GetPointer());
103         return dateTime;
104 }
105
106 Tizen::Base::String
107 _LocationImpl::GetExtraInfo(const Tizen::Base::String& key) const
108 {
109         String reqValue = L"";
110
111         if (key.Equals(L"location_method", true))
112         {
113                 reqValue = __locationMethod;
114         }
115         else if (key.Equals(L"satellite", true))
116         {
117                 reqValue = __satelliteInformation;
118         }
119
120         SysLog(NID_LOC, "Requested information is '%ls' for the key '%ls'.", reqValue.GetPointer(), key.GetPointer());
121         return reqValue;
122 }
123
124 _LocationImpl*
125 _LocationImpl::GetInstance(Location& obj)
126 {
127         return obj.__pImpl;
128 }
129
130 const _LocationImpl*
131 _LocationImpl::GetInstance(const Location& obj)
132 {
133         return obj.__pImpl;
134 }
135
136 Location*
137 _LocationImpl::GetLocationInstanceN(void)
138 {
139         Location* pLoc = new (std::nothrow) Location();
140         SysTryReturn(NID_LOC, pLoc != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
141         return pLoc;
142 }
143
144 Location
145 _LocationImpl::GetLocationInstance(void)
146 {
147         Location invalidLoc;
148         return invalidLoc;
149 }
150
151 result
152 _LocationImpl::SetCoordinates(const Coordinates& coordinate)
153 {
154         result r = __coordinate.Set(coordinate.GetLatitude(), coordinate.GetLongitude(), coordinate.GetAltitude());
155         SysTryReturn(NID_LOC, r == E_SUCCESS, r, r, "[%s] Setting the latitude/longitude value failed.", GetErrorMessage(r));
156         return E_SUCCESS;
157 }
158
159 void
160 _LocationImpl::SetExtraInfo(const Tizen::Base::String& key, const Tizen::Base::String& value)
161 {
162         if (key.Equals(L"location_method", true))
163         {
164                 __locationMethod = value;
165         }
166         else if (key.Equals(L"satellite", true))
167         {
168                 __satelliteInformation = value;
169         }
170 }
171
172 _LocationImpl&
173 _LocationImpl::operator =(const _LocationImpl& rhs)
174 {
175         if (this == &rhs)
176         {
177                 return *this;
178         }
179
180         __coordinate = rhs.__coordinate;
181         __speed = rhs.__speed;
182         __course = rhs.__course;
183         __horizontalAccuracy = rhs.__horizontalAccuracy;
184         __verticalAccuracy = rhs.__verticalAccuracy;
185         __timestamp = rhs.__timestamp;
186         __locationMethod = rhs.__locationMethod;
187         __satelliteInformation = rhs.__satelliteInformation;
188         __isLocationValid = rhs.__isLocationValid;
189
190         return *this;
191 }
192
193 bool
194 _LocationImpl::operator ==(const _LocationImpl& rhs) const
195 {
196
197         if (!__coordinate.Equals(rhs.__coordinate))
198         {
199                 return false;
200         }
201         if (__timestamp != rhs.__timestamp)
202         {
203                 return false;
204         }
205         if (__locationMethod != rhs.__locationMethod)
206         {
207                 return false;
208         }
209         if (!__satelliteInformation.Equals(rhs.__satelliteInformation))
210         {
211                 return false;
212         }
213         if (__isLocationValid != rhs.__isLocationValid)
214         {
215                 return false;
216         }
217
218         bool speedCheck = false;
219         bool courseCheck = false;
220         bool horAccCheck = false;
221         bool verAccCheck = false;
222
223         if (Double::IsNaN(__speed) || Double::IsNaN(rhs.__speed))
224         {
225                 if (Double::IsNaN(__speed) && Double::IsNaN(rhs.__speed))
226                 {
227                         speedCheck = true;
228                 }
229         }
230         else if (Double::Compare(__speed, rhs.__speed) == 0)
231         {
232                 speedCheck = true;
233         }
234
235         if (Double::IsNaN(__course) || Double::IsNaN(rhs.__course))
236         {
237                 if (Double::IsNaN(__course) && Double::IsNaN(rhs.__course))
238                 {
239                         courseCheck = true;
240                 }
241         }
242         else if (Double::Compare(__course, rhs.__course) == 0)
243         {
244                 courseCheck = true;
245         }
246
247         if (Double::IsNaN(__horizontalAccuracy) || Double::IsNaN(rhs.__horizontalAccuracy))
248         {
249                 if (Double::IsNaN(__horizontalAccuracy) && Double::IsNaN(rhs.__horizontalAccuracy))
250                 {
251                         horAccCheck = true;
252                 }
253         }
254         else if (Double::Compare(__horizontalAccuracy, rhs.__horizontalAccuracy) == 0)
255         {
256                 horAccCheck = true;
257         }
258
259         if (Double::IsNaN(__verticalAccuracy) || Double::IsNaN(rhs.__verticalAccuracy))
260         {
261                 if (Double::IsNaN(__verticalAccuracy) && Double::IsNaN(rhs.__verticalAccuracy))
262                 {
263                         verAccCheck = true;
264                 }
265         }
266         else if (Double::Compare(__verticalAccuracy, rhs.__verticalAccuracy) == 0)
267         {
268                 verAccCheck = true;
269         }
270         return (speedCheck && courseCheck && horAccCheck && verAccCheck);
271 }
272
273 }}