|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Handles position requests with LocationListener |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "ctrackingpositioner.h" |
|
21 #include "ctimeouttimer.h" |
|
22 #include "logger.h" |
|
23 |
|
24 using namespace java::location; |
|
25 |
|
26 // UNNAMED LOCAL NAMESPACE |
|
27 namespace |
|
28 { |
|
29 // Terminal multiplier = 1.25 (divided by 1000) |
|
30 const TInt KIntervalTerminal = 1250; |
|
31 // Network multiplier = 4.0 (divided by 1000) |
|
32 const TInt KIntervalNetwork = 4000; |
|
33 // Timeout is never less than 1 sec |
|
34 const TInt KMinTimeout = 1000000; |
|
35 } |
|
36 |
|
37 // ============================ MEMBER FUNCTIONS =============================== |
|
38 |
|
39 // ----------------------------------------------------------------------------- |
|
40 // CTrackingPositioner::CTrackingPositioner |
|
41 // C++ default constructor can NOT contain any code, that |
|
42 // might leave. |
|
43 // ----------------------------------------------------------------------------- |
|
44 // |
|
45 CTrackingPositioner::CTrackingPositioner( |
|
46 LocationFunctionServer* aFunctionSource) : |
|
47 CPositionerBase(aFunctionSource) |
|
48 { |
|
49 } |
|
50 |
|
51 // ----------------------------------------------------------------------------- |
|
52 // CTrackingPositioner::ConstructL |
|
53 // Symbian 2nd phase constructor can leave. |
|
54 // ----------------------------------------------------------------------------- |
|
55 // |
|
56 void CTrackingPositioner::ConstructL(RPositionServer& aServer, |
|
57 TPositionModuleId aModuleId, |
|
58 TPositionModuleInfo::TCapabilities aCapabilities) |
|
59 { |
|
60 JELOG2(EJavaLocation); |
|
61 BaseConstructL(aServer, aModuleId, aCapabilities); |
|
62 // Must use own timer for timeouts due to different behavior of timeouts in MLFW |
|
63 iTimer = CTimeoutTimer::NewL(TCallBack(&StaticCallBack, this)); |
|
64 } |
|
65 |
|
66 // ----------------------------------------------------------------------------- |
|
67 // CTrackingPositioner::NewL |
|
68 // Two-phased constructor. |
|
69 // ----------------------------------------------------------------------------- |
|
70 // |
|
71 CTrackingPositioner* CTrackingPositioner::NewL( |
|
72 LocationFunctionServer* aFunctionSource, RPositionServer& aServer, |
|
73 TPositionModuleId aModuleId, |
|
74 TPositionModuleInfo::TCapabilities aCapabilities) |
|
75 { |
|
76 JELOG2(EJavaLocation); |
|
77 CTrackingPositioner* self = |
|
78 new(ELeave) CTrackingPositioner(aFunctionSource); |
|
79 |
|
80 CleanupStack::PushL(self); |
|
81 self->ConstructL(aServer, aModuleId, aCapabilities); |
|
82 CleanupStack::Pop(self); |
|
83 |
|
84 return self; |
|
85 } |
|
86 |
|
87 // Destructor |
|
88 CTrackingPositioner::~CTrackingPositioner() |
|
89 { |
|
90 JELOG2(EJavaLocation); |
|
91 Cancel(); |
|
92 delete iTimer; |
|
93 } |
|
94 |
|
95 // ----------------------------------------------------------------------------- |
|
96 // CTrackingPositioner::StartTracking |
|
97 // (other items were commented in a header). |
|
98 // ----------------------------------------------------------------------------- |
|
99 // |
|
100 TInt CTrackingPositioner::StartTracking(TInt aInterval, TInt aTimeout, |
|
101 TInt aMaxAge) |
|
102 { |
|
103 JELOG2(EJavaLocation); |
|
104 mJni = mFunctionServer->getValidJniEnv(); |
|
105 mPeer = mFunctionServer->getPeer(); |
|
106 |
|
107 jclass peerClass = (*mJni).GetObjectClass(mPeer); |
|
108 |
|
109 //Get Method ID of Azimuth Data Callback |
|
110 mLocationUpdatedMethod = mJni->GetMethodID(peerClass, "complete", "(I)V"); |
|
111 |
|
112 //Check if all the JNI inits have succeeded |
|
113 if (NULL == mLocationUpdatedMethod) |
|
114 { |
|
115 return KErrGeneral; |
|
116 } |
|
117 |
|
118 TInt64 maxAge = 0; |
|
119 TPositionUpdateOptions updateOptions; |
|
120 |
|
121 iPositioner.SetUpdateOptions(updateOptions); |
|
122 |
|
123 if (aInterval > 0) |
|
124 { |
|
125 iInterval = MAKE_TINT64(0, aInterval) * 1000000; |
|
126 iTimeout = (aTimeout > 0) ? MAKE_TINT64(0, aTimeout) * 1000000 : Min( |
|
127 iTimeToFix * 2, iInterval); |
|
128 |
|
129 // Subtract 1 so that maxage is always smaller than interval |
|
130 maxAge = (aMaxAge > 0) ? MAKE_TINT64(0, aMaxAge) * 1000000 - 1 |
|
131 : iInterval / 2; |
|
132 } |
|
133 else |
|
134 { |
|
135 // Default interval = time to fix * factor |
|
136 // (different for network and terminal based modules) |
|
137 const TInt factor = iNetworkBased ? KIntervalNetwork |
|
138 : KIntervalTerminal; |
|
139 iInterval = (iTimeToFix * factor) / 1000; |
|
140 |
|
141 iTimeout = Min(iTimeToFix * 2, iInterval); |
|
142 maxAge = iInterval / 2; |
|
143 } |
|
144 |
|
145 iTimeout = Max(iTimeout, KMinTimeout); |
|
146 updateOptions.SetUpdateInterval(TTimeIntervalMicroSeconds(iInterval)); |
|
147 updateOptions.SetMaxUpdateAge(TTimeIntervalMicroSeconds(maxAge)); |
|
148 |
|
149 TInt err = iPositioner.SetUpdateOptions(updateOptions); |
|
150 if (err == KErrNone) |
|
151 { |
|
152 UpdatePosition(); |
|
153 |
|
154 // Must use own timer due to different behavior of timeouts in MLFW |
|
155 iTimer->TimeoutAfter(TTimeIntervalMicroSeconds(iInterval)); |
|
156 } |
|
157 |
|
158 return err; |
|
159 } |
|
160 |
|
161 // ----------------------------------------------------------------------------- |
|
162 // CTrackingPositioner::StopTracking |
|
163 // (other items were commented in a header). |
|
164 // ----------------------------------------------------------------------------- |
|
165 // |
|
166 void CTrackingPositioner::StopTracking() |
|
167 { |
|
168 JELOG2(EJavaLocation); |
|
169 iTimer->Cancel(); |
|
170 Cancel(); |
|
171 } |
|
172 |
|
173 // ----------------------------------------------------------------------------- |
|
174 // CTrackingPositioner::SetDefaultValues |
|
175 // (other items were commented in a header). |
|
176 // ----------------------------------------------------------------------------- |
|
177 // |
|
178 void CTrackingPositioner::SetDefaultValues( |
|
179 const TTimeIntervalMicroSeconds& aTimeToFix, TBool aNetworkBased) |
|
180 { |
|
181 JELOG2(EJavaLocation); |
|
182 iTimeToFix = aTimeToFix.Int64(); |
|
183 iNetworkBased = aNetworkBased; |
|
184 } |
|
185 |
|
186 // ----------------------------------------------------------------------------- |
|
187 // CTrackingPositioner::RunL |
|
188 // (other items were commented in a header). |
|
189 // ----------------------------------------------------------------------------- |
|
190 // |
|
191 void CTrackingPositioner::RunL() |
|
192 { |
|
193 JELOG2(EJavaLocation); |
|
194 |
|
195 TInt err = iStatus.Int(); |
|
196 |
|
197 switch (err) |
|
198 { |
|
199 case KErrNone: |
|
200 { |
|
201 iTimer->Cancel(); |
|
202 |
|
203 mJni = mFunctionServer->getValidJniEnv(); |
|
204 mPeer = mFunctionServer->getPeer(); |
|
205 |
|
206 (*mJni).CallVoidMethod(mPeer, mLocationUpdatedMethod, err); |
|
207 |
|
208 UpdatePosition(); |
|
209 iTimer->TimeoutAfter(iInterval + iTimeout); |
|
210 break; |
|
211 } |
|
212 case KErrOverflow: // Fallthrough |
|
213 case KErrPositionBufferOverflow: |
|
214 { |
|
215 iTimer->Cancel(); |
|
216 // CPositionerBase handles buffer size errors |
|
217 HandleBufferSizeErrorL(err); |
|
218 // Buffer increased successfully. Make a new request |
|
219 UpdatePosition(); |
|
220 // Set time out timer since tracking must not be stopped |
|
221 // in any case if the listener has been set from Java-side |
|
222 iTimer->TimeoutAfter(iInterval + iTimeout); |
|
223 break; |
|
224 } |
|
225 default: |
|
226 { |
|
227 // Do not handle other error situations. Time out will take |
|
228 // care if the location request didn't succeed |
|
229 break; |
|
230 } |
|
231 } |
|
232 } |
|
233 |
|
234 // ----------------------------------------------------------------------------- |
|
235 // CTrackingPositioner::DoCancel |
|
236 // (other items were commented in a header). |
|
237 // ----------------------------------------------------------------------------- |
|
238 // |
|
239 void CTrackingPositioner::DoCancel() |
|
240 { |
|
241 JELOG2(EJavaLocation); |
|
242 iPositioner.CancelRequest(EPositionerNotifyPositionUpdate); |
|
243 } |
|
244 |
|
245 // ----------------------------------------------------------------------------- |
|
246 // CTrackingPositioner::RunError |
|
247 // (other items were commented in a header). |
|
248 // ----------------------------------------------------------------------------- |
|
249 // |
|
250 TInt CTrackingPositioner::RunError(TInt aError) |
|
251 { |
|
252 JELOG2(EJavaLocation); |
|
253 mJni = mFunctionServer->getValidJniEnv(); |
|
254 mPeer = mFunctionServer->getPeer(); |
|
255 |
|
256 (*mJni).CallVoidMethod(mPeer, mLocationUpdatedMethod, aError); |
|
257 |
|
258 iTimer->TimeoutAfter(iInterval); |
|
259 // RunL caused a leave, but we still have to acquire next location fix |
|
260 // even though it will propably fail. Just try to keep tracking |
|
261 if (!IsActive()) |
|
262 { |
|
263 // Last position update failed, request a new position |
|
264 UpdatePosition(); |
|
265 } |
|
266 return KErrNone; |
|
267 } |
|
268 |
|
269 // ----------------------------------------------------------------------------- |
|
270 // CTrackingPositioner::CallBack |
|
271 // (other items were commented in a header). |
|
272 // ----------------------------------------------------------------------------- |
|
273 // |
|
274 void CTrackingPositioner::CallBack() |
|
275 { |
|
276 JELOG2(EJavaLocation); |
|
277 mJni = mFunctionServer->getValidJniEnv(); |
|
278 mPeer = mFunctionServer->getPeer(); |
|
279 |
|
280 (*mJni).CallVoidMethod(mPeer, mLocationUpdatedMethod, KErrTimedOut); |
|
281 |
|
282 iTimer->TimeoutAfter(iInterval); |
|
283 |
|
284 if (!IsActive()) |
|
285 { |
|
286 // Last position update failed, request a new position |
|
287 UpdatePosition(); |
|
288 } |
|
289 } |
|
290 |
|
291 // ----------------------------------------------------------------------------- |
|
292 // CTrackingPositioner::StaticCallBack |
|
293 // (other items were commented in a header). |
|
294 // ----------------------------------------------------------------------------- |
|
295 // |
|
296 TInt CTrackingPositioner::StaticCallBack(TAny* aTrackingPositioner) |
|
297 { |
|
298 JELOG2(EJavaLocation); |
|
299 CTrackingPositioner* positioner = |
|
300 reinterpret_cast<CTrackingPositioner*>(aTrackingPositioner); |
|
301 positioner->CallBack(); |
|
302 return KErrNone; |
|
303 } |
|
304 |
|
305 // End of File |