|
1 /* |
|
2 * Copyright (c) 2009 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: |
|
15 * |
|
16 */ |
|
17 import javax.microedition.midlet.*; |
|
18 import javax.microedition.lcdui.*; |
|
19 import javax.microedition.location.*; |
|
20 import java.util.*; |
|
21 |
|
22 public class ViperTest extends MIDlet implements CommandListener, |
|
23 LocationListener |
|
24 { |
|
25 private Command iExitCmd = new Command("Quit", Command.EXIT, 1); |
|
26 |
|
27 private Command iSelectCmd = new Command("Choose Provider", Command.SCREEN, |
|
28 1); |
|
29 |
|
30 private Command iGetLocationCmd = new Command("Get location", |
|
31 Command.SCREEN, 1); |
|
32 |
|
33 private Command iGetLastKnownLocationCmd = new Command( |
|
34 "Last known location", Command.SCREEN, 1); |
|
35 |
|
36 private Command iStartTrackingCmd = new Command("Start tracking", |
|
37 Command.SCREEN, 1); |
|
38 |
|
39 private Command iStopTrackingCmd = new Command("Stop tracking", |
|
40 Command.SCREEN, 1); |
|
41 |
|
42 private Command iGetStatusCmd = new Command("Get status", Command.SCREEN, 1); |
|
43 |
|
44 private Command iRunTestCmd1 = new Command("Run tests", Command.SCREEN, 1); |
|
45 |
|
46 private Command iRunTestCmd2 = new Command("Run LP tests", Command.SCREEN, |
|
47 1); |
|
48 |
|
49 private Command iPreferencesCmd = new Command("Set preferences", |
|
50 Command.SCREEN, 1); |
|
51 |
|
52 private Display iDisplay; |
|
53 |
|
54 private Form iForm; |
|
55 |
|
56 private ViperPreferences iPrefs; |
|
57 |
|
58 private ProviderSelecter iSelecter = new ProviderSelecter(); |
|
59 |
|
60 private LocationProvider iLocationProvider = null; |
|
61 |
|
62 private static int iN = 0; |
|
63 |
|
64 private long iLastUpdate; |
|
65 |
|
66 private long iLastTimestamp; |
|
67 |
|
68 public ViperTest() |
|
69 { |
|
70 } |
|
71 |
|
72 protected void destroyApp(boolean unconditional) |
|
73 { |
|
74 System.out.println("ViperTest.destroyApp"); |
|
75 iLocationProvider = null; |
|
76 iForm = null; |
|
77 // iPrefs = null; |
|
78 iDisplay = null; |
|
79 } |
|
80 |
|
81 protected void pauseApp() |
|
82 { |
|
83 System.out.println("pauseApp"); |
|
84 iDisplay.setCurrent(null); |
|
85 } |
|
86 |
|
87 protected void startApp() |
|
88 { |
|
89 iForm = new Form("ViperTest"); |
|
90 iForm.addCommand(iExitCmd); |
|
91 iForm.addCommand(iRunTestCmd1); |
|
92 iForm.addCommand(iRunTestCmd2); |
|
93 iForm.addCommand(iSelectCmd); |
|
94 iForm.addCommand(iGetLocationCmd); |
|
95 iForm.addCommand(iGetLastKnownLocationCmd); |
|
96 iForm.addCommand(iGetStatusCmd); |
|
97 iForm.addCommand(iStartTrackingCmd); |
|
98 iForm.addCommand(iPreferencesCmd); |
|
99 iForm.append("LAPI version: " |
|
100 + System.getProperty("microedition.location.version")); |
|
101 iDisplay = Display.getDisplay(this); |
|
102 iDisplay.setCurrent(iForm); |
|
103 iForm.setCommandListener(this); |
|
104 iPrefs = new ViperPreferences(iDisplay, iForm); |
|
105 } |
|
106 |
|
107 public void commandAction(Command c, Displayable d) |
|
108 { |
|
109 try |
|
110 { |
|
111 handleCommand(c); |
|
112 } |
|
113 catch (Exception e) |
|
114 { |
|
115 iForm.append("Exception: " + e); |
|
116 } |
|
117 } |
|
118 |
|
119 public void locationUpdated(LocationProvider provider, Location location) |
|
120 { |
|
121 if (provider != iLocationProvider) |
|
122 { |
|
123 System.out.println("provider = " + provider); |
|
124 System.out.println("iLocationProvider = " + iLocationProvider); |
|
125 } |
|
126 |
|
127 long now = System.currentTimeMillis(); |
|
128 long timeDiff = now - iLastUpdate; |
|
129 iLastUpdate = now; |
|
130 |
|
131 iForm.deleteAll(); |
|
132 |
|
133 iForm.append("Tracking #" + (++iN) + "\n"); |
|
134 showLocation(location); |
|
135 |
|
136 iForm.append("Last update = " + timeDiff + " (ms) ago\n"); |
|
137 |
|
138 System.out.println("------ locationUpdated #" + iN + " ------"); |
|
139 System.out.println("Time of fix: " + time(now)); |
|
140 System.out.println("Last update = " + timeDiff + " (ms) ago"); |
|
141 |
|
142 long ts = location.getTimestamp(); |
|
143 System.out.println("Timestamp: " + time(ts)); |
|
144 if (location.isValid()) |
|
145 { |
|
146 QualifiedCoordinates qc = location.getQualifiedCoordinates(); |
|
147 System.out.println("Lat: " + format(qc.getLatitude()) + ", Lon: " |
|
148 + format(qc.getLongitude())); |
|
149 System.out.println("Timestamp diff = " + (ts - iLastTimestamp) |
|
150 + " (ms)"); |
|
151 } |
|
152 else |
|
153 { |
|
154 System.out.println("Invalid location!"); |
|
155 } |
|
156 iLastTimestamp = ts; |
|
157 } |
|
158 |
|
159 public void providerStateChanged(LocationProvider provider, int newState) |
|
160 { |
|
161 iForm.deleteAll(); |
|
162 iForm.append("State changed\n"); |
|
163 showState(newState); |
|
164 } |
|
165 |
|
166 private void handleCommand(Command c) throws Exception |
|
167 { |
|
168 iForm.deleteAll(); |
|
169 |
|
170 if (c == iExitCmd) |
|
171 { |
|
172 notifyDestroyed(); |
|
173 } |
|
174 else if (c == iSelectCmd) |
|
175 { |
|
176 System.out.println("Choose a LocationProvider"); |
|
177 iDisplay.setCurrent(iSelecter); |
|
178 } |
|
179 else if (c == iPreferencesCmd) |
|
180 { |
|
181 iDisplay.setCurrent(iPrefs); |
|
182 } |
|
183 else if (c == iRunTestCmd1) |
|
184 { |
|
185 iForm.append("Running unit tests\n"); |
|
186 Thread t = new Thread() |
|
187 { |
|
188 public void run() |
|
189 { |
|
190 iForm.append(ViperUnitTest.run1() + "\n"); |
|
191 } |
|
192 }; |
|
193 t.start(); |
|
194 } |
|
195 else if (c == iRunTestCmd2) |
|
196 { |
|
197 iForm.append("Running LocationProvider tests\n"); |
|
198 Thread t = new Thread() |
|
199 { |
|
200 public void run() |
|
201 { |
|
202 iForm.append(ViperUnitTest.run2(iDisplay) + "\n"); |
|
203 } |
|
204 }; |
|
205 t.start(); |
|
206 } |
|
207 else if (c == iGetLastKnownLocationCmd) |
|
208 { |
|
209 iForm.append("Last known location\n"); |
|
210 Location loc = LocationProvider.getLastKnownLocation(); |
|
211 showLocation(loc); |
|
212 } |
|
213 else |
|
214 { |
|
215 |
|
216 if (iLocationProvider == null) |
|
217 { |
|
218 iLocationProvider = LocationProvider.getInstance(null); |
|
219 if (iLocationProvider == null) |
|
220 { |
|
221 iForm.append("No location provider meets criteria\n"); |
|
222 return; |
|
223 } |
|
224 } |
|
225 |
|
226 if (c == iGetStatusCmd) |
|
227 { |
|
228 iForm.append("Get status\n"); |
|
229 showState(iLocationProvider.getState()); |
|
230 } |
|
231 else if (c == iGetLocationCmd) |
|
232 { |
|
233 iForm.append("Get location\n"); |
|
234 Location loc = iLocationProvider.getLocation(iPrefs.iTimeout); |
|
235 showLocation(loc); |
|
236 } |
|
237 else if (c == iStartTrackingCmd) |
|
238 { |
|
239 iForm.append("Start tracking\n"); |
|
240 int interval = iPrefs.iInterval; |
|
241 int timeout = iPrefs.iTimeout; |
|
242 int maxage = iPrefs.iMaxAge; |
|
243 |
|
244 iLastUpdate = System.currentTimeMillis(); |
|
245 iLocationProvider.setLocationListener(this, interval, timeout, |
|
246 maxage); |
|
247 iForm.addCommand(iStopTrackingCmd); |
|
248 iForm.removeCommand(iStartTrackingCmd); |
|
249 } |
|
250 else if (c == iStopTrackingCmd) |
|
251 { |
|
252 iForm.append("Stop tracking\n"); |
|
253 iLocationProvider.setLocationListener(null, 0, 0, 0); |
|
254 iForm.addCommand(iStartTrackingCmd); |
|
255 iForm.removeCommand(iStopTrackingCmd); |
|
256 } |
|
257 } |
|
258 } |
|
259 |
|
260 private void showLocation(Location loc) |
|
261 { |
|
262 if (loc == null) |
|
263 { |
|
264 iForm.append("Location is null\n"); |
|
265 return; |
|
266 } |
|
267 else if (!loc.isValid()) |
|
268 { |
|
269 iForm.append("Location is not valid\n"); |
|
270 return; |
|
271 } |
|
272 |
|
273 QualifiedCoordinates qc = loc.getQualifiedCoordinates(); |
|
274 |
|
275 iForm.append("Lat = " + format(qc.getLatitude()) + "\n"); |
|
276 iForm.append("Lon = " + format(qc.getLongitude()) + "\n"); |
|
277 iForm.append("Altitude = " + qc.getAltitude() + "\n"); |
|
278 iForm.append("Hacc = " + qc.getHorizontalAccuracy() + ", "); |
|
279 iForm.append("Vacc = " + qc.getVerticalAccuracy() + "\n"); |
|
280 |
|
281 iForm.append("Speed = " + loc.getSpeed() + ", "); |
|
282 iForm.append("Course = " + loc.getCourse() + "\n"); |
|
283 iForm.append("Method = " + lm(loc.getLocationMethod()) + "\n"); |
|
284 |
|
285 // DateField dateFld = new DateField("Time of fix", |
|
286 // DateField.DATE_TIME); |
|
287 // dateFld.setDate(new Date(loc.getTimestamp())); |
|
288 // iForm.append(dateFld); |
|
289 iForm.append("Timestamp = " + time(loc.getTimestamp()) + "\n"); |
|
290 |
|
291 AddressInfo addrInfo = loc.getAddressInfo(); |
|
292 if (addrInfo != null) |
|
293 { |
|
294 printAddress(addrInfo); |
|
295 } |
|
296 |
|
297 String extra = loc.getExtraInfo("application/X-jsr179-location-nmea"); |
|
298 if (extra != null) |
|
299 { |
|
300 System.out.println("Extra info:\n" + extra); |
|
301 } |
|
302 } |
|
303 |
|
304 private String format(double aCoord) |
|
305 { |
|
306 // if (iPrefs.iCoordFormat > 0) { |
|
307 // return Coordinates.convert(aCoord, iPrefs.iCoordFormat); |
|
308 // } else { |
|
309 return Double.toString(aCoord); |
|
310 // } |
|
311 } |
|
312 |
|
313 private String lm(int aMethod) |
|
314 { |
|
315 String s = ""; |
|
316 if ((aMethod & Location.MTE_SATELLITE) > 0) |
|
317 { |
|
318 s = "Sat,"; |
|
319 } |
|
320 if ((aMethod & Location.MTY_NETWORKBASED) > 0) |
|
321 { |
|
322 s += "Netw"; |
|
323 } |
|
324 if ((aMethod & Location.MTY_TERMINALBASED) > 0) |
|
325 { |
|
326 s += "Term"; |
|
327 } |
|
328 if ((aMethod & Location.MTA_ASSISTED) > 0) |
|
329 { |
|
330 s += ",assist"; |
|
331 } |
|
332 if ((aMethod & Location.MTA_UNASSISTED) > 0) |
|
333 { |
|
334 s += ",unassist"; |
|
335 } |
|
336 if ((aMethod & ~(Location.MTE_SATELLITE | Location.MTY_NETWORKBASED |
|
337 | Location.MTY_TERMINALBASED | Location.MTA_ASSISTED | Location.MTA_UNASSISTED)) > 0) |
|
338 { |
|
339 s = "Illegal"; |
|
340 } |
|
341 return s; |
|
342 } |
|
343 |
|
344 private String time(long aTimestamp) |
|
345 { |
|
346 Calendar cal = Calendar.getInstance(); |
|
347 cal.setTime(new Date(aTimestamp)); |
|
348 int h = cal.get(Calendar.HOUR_OF_DAY); |
|
349 int m = cal.get(Calendar.MINUTE); |
|
350 int s = cal.get(Calendar.SECOND); |
|
351 int ms = cal.get(Calendar.MILLISECOND); |
|
352 return (h < 10 ? "0" : "") + h + ":" + (m < 10 ? "0" : "") + m + ":" |
|
353 + (s < 10 ? "0" : "") + s + "." + ms; |
|
354 } |
|
355 |
|
356 private void showState(int aState) |
|
357 { |
|
358 switch (aState) |
|
359 { |
|
360 case LocationProvider.AVAILABLE: |
|
361 iForm.append("Available"); |
|
362 break; |
|
363 case LocationProvider.TEMPORARILY_UNAVAILABLE: |
|
364 iForm.append("Temp. unavailable"); |
|
365 break; |
|
366 case LocationProvider.OUT_OF_SERVICE: |
|
367 iForm.append("Out of service"); |
|
368 break; |
|
369 default: |
|
370 iForm.append("Unknown state"); |
|
371 break; |
|
372 } |
|
373 } |
|
374 |
|
375 private void printAddress(AddressInfo a) |
|
376 { |
|
377 System.out.println("--- AddressInfo ---"); |
|
378 System.out.println("Floor: " + a.getField(AddressInfo.BUILDING_FLOOR)); |
|
379 System.out.println("Room: " + a.getField(AddressInfo.BUILDING_ROOM)); |
|
380 System.out.println("B name: " + a.getField(AddressInfo.BUILDING_NAME)); |
|
381 System.out.println("B zone: " + a.getField(AddressInfo.BUILDING_ZONE)); |
|
382 System.out.println("City: " + a.getField(AddressInfo.CITY)); |
|
383 System.out.println("Country: " + a.getField(AddressInfo.COUNTRY)); |
|
384 System.out.println("C code: " + a.getField(AddressInfo.COUNTRY_CODE)); |
|
385 System.out.println("County: " + a.getField(AddressInfo.COUNTY)); |
|
386 System.out.println("Xing1: " + a.getField(AddressInfo.CROSSING1)); |
|
387 System.out.println("Xing2: " + a.getField(AddressInfo.CROSSING2)); |
|
388 System.out.println("District: " + a.getField(AddressInfo.DISTRICT)); |
|
389 System.out.println("Extension: " + a.getField(AddressInfo.EXTENSION)); |
|
390 System.out.println("Phone: " + a.getField(AddressInfo.PHONE_NUMBER)); |
|
391 System.out.println("P Code: " + a.getField(AddressInfo.POSTAL_CODE)); |
|
392 System.out.println("Street: " + a.getField(AddressInfo.STREET)); |
|
393 System.out.println("State: " + a.getField(AddressInfo.STATE)); |
|
394 System.out.println("URL: " + a.getField(AddressInfo.URL)); |
|
395 } |
|
396 |
|
397 private class ProviderSelecter extends Form implements CommandListener |
|
398 { |
|
399 private Command iOkCmd = new Command("OK", Command.OK, 1); |
|
400 |
|
401 private Command iDefaultCmd = new Command("Null Criteria", |
|
402 Command.SCREEN, 1); |
|
403 |
|
404 private Command iNullCmd = new Command("Dereference", Command.SCREEN, 1); |
|
405 |
|
406 private Command iGcCmd = new Command("Garbage collect", Command.SCREEN, |
|
407 1); |
|
408 |
|
409 private TextField iHaccField = new TextField("Horizontal Accuracy", |
|
410 "0", 5, TextField.NUMERIC); |
|
411 |
|
412 private TextField iVaccField = new TextField("Vertical Accuracy", "0", |
|
413 5, TextField.NUMERIC); |
|
414 |
|
415 private TextField iRespTimeField = new TextField("Response time", "0", |
|
416 5, TextField.NUMERIC); |
|
417 |
|
418 private ChoiceGroup iPowerChoice = new ChoiceGroup("Power consumption", |
|
419 Choice.POPUP, new String[] { "No requirement", "Low", "Medium", |
|
420 "High" |
|
421 }, null); |
|
422 |
|
423 private ChoiceGroup iChoices = new ChoiceGroup("Choices", |
|
424 Choice.MULTIPLE, new String[] { "AddressInfo", "Cost allowed", |
|
425 "Altitude", "Speed + Course" |
|
426 }, null); |
|
427 |
|
428 ProviderSelecter() |
|
429 { |
|
430 super("Criteria"); |
|
431 addCommand(iOkCmd); |
|
432 addCommand(iDefaultCmd); |
|
433 addCommand(iNullCmd); |
|
434 addCommand(iGcCmd); |
|
435 setCommandListener(this); |
|
436 append(iHaccField); |
|
437 append(iVaccField); |
|
438 append(iRespTimeField); |
|
439 append(iPowerChoice); |
|
440 append(iChoices); |
|
441 } |
|
442 |
|
443 public void commandAction(Command c, Displayable d) |
|
444 { |
|
445 if (c == iOkCmd || c == iDefaultCmd) |
|
446 { |
|
447 iForm.deleteAll(); |
|
448 |
|
449 try |
|
450 { |
|
451 LocationProvider l; |
|
452 if (c == iOkCmd) |
|
453 { |
|
454 l = LocationProvider.getInstance(getCriteria()); |
|
455 } |
|
456 else |
|
457 { |
|
458 l = LocationProvider.getInstance(null); |
|
459 } |
|
460 if (l == null) |
|
461 { |
|
462 iForm.append("Criteria too restrictive!\n"); |
|
463 } |
|
464 else if (l != iLocationProvider) |
|
465 { |
|
466 iForm.append("New provider selected\n"); |
|
467 iLocationProvider = l; |
|
468 } |
|
469 } |
|
470 catch (Exception e) |
|
471 { |
|
472 iForm.append("Exception: " + e); |
|
473 } |
|
474 } |
|
475 else if (c == iNullCmd) |
|
476 { |
|
477 System.out.println("Setting LocationProvider to null"); |
|
478 iLocationProvider = null; |
|
479 } |
|
480 else if (c == iGcCmd) |
|
481 { |
|
482 System.out.println("Calling System.gc()"); |
|
483 System.gc(); |
|
484 } |
|
485 iDisplay.setCurrent(iForm); |
|
486 } |
|
487 |
|
488 private Criteria getCriteria() |
|
489 { |
|
490 Criteria cr = new Criteria(); |
|
491 int hacc = Integer.parseInt(iHaccField.getString()); |
|
492 cr.setHorizontalAccuracy(hacc); |
|
493 |
|
494 int vacc = Integer.parseInt(iVaccField.getString()); |
|
495 cr.setVerticalAccuracy(vacc); |
|
496 |
|
497 int resp = Integer.parseInt(iRespTimeField.getString()); |
|
498 cr.setPreferredResponseTime(resp); |
|
499 |
|
500 int power = iPowerChoice.getSelectedIndex(); |
|
501 cr.setPreferredPowerConsumption(power); |
|
502 |
|
503 boolean[] flags = new boolean[4]; |
|
504 int numselected = iChoices.getSelectedFlags(flags); |
|
505 cr.setAddressInfoRequired(flags[0]); |
|
506 cr.setCostAllowed(flags[1]); |
|
507 cr.setAltitudeRequired(flags[2]); |
|
508 cr.setSpeedAndCourseRequired(flags[3]); |
|
509 |
|
510 print(cr); |
|
511 return cr; |
|
512 } |
|
513 |
|
514 void print(Criteria cr) |
|
515 { |
|
516 System.out.println("\n-- Criterias --"); |
|
517 int hacc = cr.getHorizontalAccuracy(); |
|
518 if (hacc != Criteria.NO_REQUIREMENT) |
|
519 { |
|
520 System.out.println("Required horizontal accuracy = " + hacc); |
|
521 } |
|
522 int vacc = cr.getVerticalAccuracy(); |
|
523 if (vacc != Criteria.NO_REQUIREMENT) |
|
524 { |
|
525 System.out.println("Required vertical accuracy = " + vacc); |
|
526 } |
|
527 int resp = cr.getPreferredResponseTime(); |
|
528 if (resp != Criteria.NO_REQUIREMENT) |
|
529 { |
|
530 System.out.println("Preferred response time = " + resp); |
|
531 } |
|
532 |
|
533 switch (cr.getPreferredPowerConsumption()) |
|
534 { |
|
535 case Criteria.POWER_USAGE_LOW: |
|
536 System.out.println("Low power usage wanted"); |
|
537 break; |
|
538 case Criteria.POWER_USAGE_MEDIUM: |
|
539 System.out.println("Medium power usage acceptable"); |
|
540 break; |
|
541 case Criteria.POWER_USAGE_HIGH: |
|
542 System.out.println("High power usage acceptable"); |
|
543 break; |
|
544 } |
|
545 |
|
546 if (cr.isAddressInfoRequired()) |
|
547 { |
|
548 System.out.println("AddressInfo required"); |
|
549 } |
|
550 if (!cr.isAllowedToCost()) |
|
551 { |
|
552 System.out.println("Cost is not allowed"); |
|
553 } |
|
554 if (cr.isAltitudeRequired()) |
|
555 { |
|
556 System.out.println("Altitude required"); |
|
557 } |
|
558 if (cr.isSpeedAndCourseRequired()) |
|
559 { |
|
560 System.out.println("Speed and course info required"); |
|
561 } |
|
562 System.out.println("---------------"); |
|
563 } |
|
564 |
|
565 } |
|
566 |
|
567 } |