/[zanavi_public1]/navit/navit/android/src/com/zoffcc/applications/zanavi/NavitVehicle.java
ZANavi

Contents of /navit/navit/android/src/com/zoffcc/applications/zanavi/NavitVehicle.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 28 - (show annotations) (download)
Sun Jun 17 08:12:47 2012 UTC (11 years, 10 months ago) by zoff99
File size: 18149 byte(s)
lots of new stuff and fixes
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011 Zoff <zoff@zoff.cc>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * Navit, a modular navigation system.
22 * Copyright (C) 2005-2008 Navit Team
23 *
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * version 2 as published by the Free Software Foundation.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the
35 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
36 * Boston, MA 02110-1301, USA.
37 */
38
39 package com.zoffcc.applications.zanavi;
40
41 import java.util.Iterator;
42
43 import android.content.Context;
44 import android.location.Criteria;
45 import android.location.GpsSatellite;
46 import android.location.GpsStatus;
47 import android.location.Location;
48 import android.location.LocationListener;
49 import android.location.LocationManager;
50 import android.location.LocationProvider;
51 import android.os.Bundle;
52 import android.os.Handler;
53 import android.os.Message;
54 import android.util.Log;
55
56 public class NavitVehicle
57 {
58 private LocationManager locationManager = null;
59 private static LocationManager locationManager_s = null;
60 private String preciseProvider = null;
61 private String fastProvider = null;
62 private static LocationListener fastLocationListener_s = null;
63 private static LocationListener preciseLocationListener_s = null;
64 private static GpsStatus.Listener gps_status_listener_s = null;
65 private static float compass_heading;
66 private static float current_accuracy = 99999999F;
67
68 public static Handler vehicle_handler_ = null;
69 public static long lastcompass_update_timestamp = 0L;
70
71 public static final float GPS_SPEED_ABOVE_USE_FOR_HEADING = (float) (9 / 3.6f); // (9 km/h) / (3.6) ~= m/s
72
73 private static String preciseProvider_s = null;
74 private static String fastProvider_s = null;
75
76 public static long last_p_fix = 0;
77 public static long last_f_fix = 0;
78 public Bundle gps_extras = null;
79 public static GpsStatus gps_status = null;
80 public static Boolean update_location_in_progress = false;
81
82 private static Location last_location = null;
83
84 public static native void VehicleCallback(Location location);
85
86 // private static SatStatusThread st = null;
87
88 private class SatStatusThread extends Thread
89 {
90 // get new gpsstatus --------
91 int sats1 = 0;
92 int satsInFix1 = 0;
93 Boolean running = true;
94
95 public void run()
96 {
97 this.running = true;
98
99 while (this.running)
100 {
101 try
102 {
103 if (!Navit.DemoVehicle)
104 {
105 GpsStatus stat = locationManager.getGpsStatus(gps_status);
106 gps_status = stat;
107 Iterator<GpsSatellite> localIterator = stat.getSatellites().iterator();
108 while (localIterator.hasNext())
109 {
110 GpsSatellite localGpsSatellite = (GpsSatellite) localIterator.next();
111 sats1++;
112 if (localGpsSatellite.usedInFix())
113 {
114 satsInFix1++;
115 }
116 }
117 }
118 }
119 catch (Exception e)
120 {
121 e.printStackTrace();
122 }
123 // System.out.println("Statellites (Thread): " + satsInFix1 + "/" + sats1);
124 // Navit.set_debug_messages3_wrapper("sat: " + satsInFix1 + "/" + sats1);
125 // get new gpsstatus --------
126
127 try
128 {
129 Thread.sleep(1000);
130 }
131 catch (InterruptedException e)
132 {
133 }
134 }
135 }
136
137 public void stop_me()
138 {
139 this.running = false;
140 }
141 }
142
143 NavitVehicle(Context context, int callbackid)
144 {
145 vehicle_handler_ = vehicle_handler;
146
147 locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
148 locationManager_s = locationManager;
149
150 LocationListener fastLocationListener = new LocationListener()
151 {
152 public void onLocationChanged(Location location)
153 {
154 last_f_fix = location.getTime();
155 // if last gps fix was longer than 4 secs. ago, use this fix
156 // and we dont have a GPS lock
157 if ((last_p_fix + 4000 < last_f_fix) && (Navit.satsInFix < 3))
158 {
159 if (Navit.PREF_follow_gps)
160 {
161 if (Navit.PREF_use_compass_heading_base)
162 {
163 if ((Navit.PREF_use_compass_heading_always) || (location.getSpeed() < GPS_SPEED_ABOVE_USE_FOR_HEADING))
164 {
165 // use compass heading
166 location.setBearing(compass_heading);
167 }
168 }
169
170 //System.out.println("send values 1");
171 //Log.e("NavitVehicle", "LocationChanged provider=fast Latitude " + location.getLatitude() + " Longitude " + location.getLongitude());
172 last_location = location;
173 if (!Navit.DemoVehicle)
174 {
175 //Log.e("NavitVehicle", "call VehicleCallback 001");
176 VehicleCallback(location);
177 }
178 }
179 }
180 }
181
182 public void onProviderDisabled(String provider)
183 {
184 //Log.e("NavitVehicle", "onProviderDisabled -> provider=" + provider);
185 }
186
187 public void onProviderEnabled(String provider)
188 {
189 //Log.e("NavitVehicle", "onProviderEnabled -> provider=" + provider);
190 }
191
192 public void onStatusChanged(String provider, int status, Bundle extras)
193 {
194 //Log.e("NavitVehicle", "onStatusChanged -> provider=" + provider + " status=" + status);
195 try
196 {
197 switch (status)
198 {
199 case LocationProvider.OUT_OF_SERVICE:
200 System.out.println("*** No Service ***");
201 break;
202 case LocationProvider.TEMPORARILY_UNAVAILABLE:
203 System.out.println("*** No Fix ***");
204 break;
205 case LocationProvider.AVAILABLE:
206 System.out.println("@@@ Fix @@@");
207 break;
208 }
209 }
210 catch (Exception e)
211 {
212
213 }
214 }
215 };
216 fastLocationListener_s = fastLocationListener;
217
218 LocationListener preciseLocationListener = new LocationListener()
219 {
220 public void onLocationChanged(Location location)
221 {
222 last_p_fix = location.getTime();
223 current_accuracy = location.getAccuracy();
224
225 if (Navit.PREF_follow_gps)
226 {
227 if (Navit.PREF_use_compass_heading_base)
228 {
229 if ((Navit.PREF_use_compass_heading_always) || (location.getSpeed() < GPS_SPEED_ABOVE_USE_FOR_HEADING))
230 {
231 // use compass heading
232 location.setBearing(compass_heading);
233 }
234 }
235 //System.out.println("send values 2");
236 //Log.e("NavitVehicle", "LocationChanged provider=precise Latitude " + location.getLatitude() + " Longitude " + location.getLongitude());
237 last_location = location;
238 //Log.e("NavitVehicle", "call VehicleCallback 002");
239 VehicleCallback(location);
240 }
241 }
242
243 public void onProviderDisabled(String provider)
244 {
245 //Log.e("NavitVehicle", "onProviderDisabled -> provider=" + provider);
246 }
247
248 public void onProviderEnabled(String provider)
249 {
250 //Log.e("NavitVehicle", "onProviderEnabled -> provider=" + provider);
251 }
252
253 public void onStatusChanged(String provider, int status, Bundle extras)
254 {
255 //Log.e("NavitVehicle", "onStatusChanged -> provider=" + provider + " status=" + status);
256
257 try
258 {
259 if (status == GpsStatus.GPS_EVENT_FIRST_FIX)
260 {
261 System.out.println("*** GPS first fix ***");
262 }
263 switch (status)
264 {
265 case LocationProvider.OUT_OF_SERVICE:
266 System.out.println("*** No Service ***");
267 break;
268 case LocationProvider.TEMPORARILY_UNAVAILABLE:
269 System.out.println("*** No Fix ***");
270 break;
271 case LocationProvider.AVAILABLE:
272 System.out.println("@@@ Fix @@@");
273 break;
274 }
275 }
276 catch (Exception e)
277 {
278
279 }
280 }
281 };
282 preciseLocationListener_s = preciseLocationListener;
283
284 /*
285 * Use 2 LocationProviders, one precise (usually GPS), and one
286 * not so precise, but possible faster.
287 */
288 Criteria highCriteria = null;
289 Criteria lowCriteria = null;
290 try
291 {
292 // Selection criterias for the precise provider
293 highCriteria = new Criteria();
294 highCriteria.setAccuracy(Criteria.ACCURACY_FINE);
295 highCriteria.setAltitudeRequired(false);
296 highCriteria.setBearingRequired(true);
297 //highCriteria.setCostAllowed(true);
298 //highCriteria.setPowerRequirement(Criteria.POWER_HIGH);
299
300 // Selection criterias for the fast provider
301 lowCriteria = new Criteria();
302 lowCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
303 lowCriteria.setAltitudeRequired(false);
304 lowCriteria.setBearingRequired(false);
305 //lowCriteria.setCostAllowed(true);
306 //lowCriteria.setPowerRequirement(Criteria.POWER_HIGH);
307 }
308 catch (Exception e)
309 {
310 e.printStackTrace();
311 }
312
313 try
314 {
315 //Log.e("NavitVehicle", "Providers " + locationManager.getAllProviders());
316
317 preciseProvider = locationManager.getBestProvider(highCriteria, false);
318 preciseProvider_s = preciseProvider;
319 //Log.e("NavitVehicle", "Precise Provider " + preciseProvider);
320 fastProvider = locationManager.getBestProvider(lowCriteria, false);
321 fastProvider_s = fastProvider;
322 //Log.e("NavitVehicle", "Fast Provider " + fastProvider);
323 }
324 catch (Exception e)
325 {
326 e.printStackTrace();
327 }
328
329 try
330 {
331 locationManager.requestLocationUpdates(preciseProvider, 0, 0, preciseLocationListener);
332 }
333 catch (Exception e)
334 {
335 e.printStackTrace();
336 }
337
338 try
339 {
340 // If the 2 providers is the same, only activate one listener
341 if (fastProvider == null || preciseProvider.compareTo(fastProvider) == 0)
342 {
343 fastProvider = null;
344 }
345 else
346 {
347 if (Navit.PREF_use_fast_provider)
348 {
349 locationManager.requestLocationUpdates(fastProvider, 0, 0, fastLocationListener);
350 }
351 }
352 }
353 catch (Exception e)
354 {
355 e.printStackTrace();
356 }
357
358 gps_status_listener_s = new GpsStatus.Listener()
359 {
360 public void onGpsStatusChanged(int event)
361 {
362 if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS)
363 {
364 // get new gpsstatus --------
365 GpsStatus stat = locationManager.getGpsStatus(null);
366 Navit.sats = 0;
367 Navit.satsInFix = 0;
368
369 try
370 {
371 Iterator<GpsSatellite> localIterator = stat.getSatellites().iterator();
372 while (localIterator.hasNext())
373 {
374 GpsSatellite localGpsSatellite = (GpsSatellite) localIterator.next();
375 Navit.sats++;
376 if (localGpsSatellite.usedInFix())
377 {
378 Navit.satsInFix++;
379 }
380 }
381 // redraw NavitOSDJava
382 Message msg = NavitOSDJava.progress_handler_.obtainMessage();
383 Bundle b = new Bundle();
384 msg.what = 1;
385 msg.setData(b);
386 NavitOSDJava.progress_handler_.sendMessage(msg);
387 }
388 catch (Exception e)
389 {
390 e.printStackTrace();
391 }
392 // Navit.set_debug_messages3_wrapper("sat: " + Navit.satsInFix + "/" + Navit.sats);
393 // System.out.println("Statellites: " + Navit.satsInFix + "/" + Navit.sats);
394 // get new gpsstatus --------
395 }
396 }
397 };
398 }
399
400 public static void set_mock_location__fast(Location mock_location)
401 {
402 try
403 {
404 //locationManager_s.setTestProviderLocation("ZANavi_mock", mock_location);
405 // mock_location;
406 // System.out.println("llllllll" + mock_location.getLatitude() + " " + mock_location.getLongitude());
407 if (mock_location != null)
408 {
409 if (mock_location.getSpeed() == 0.0f)
410 {
411 float save_speed = last_location.getSpeed();
412 mock_location.setSpeed(0.2f);
413 //Log.e("NavitVehicle", "call VehicleCallback 003");
414 VehicleCallback(mock_location);
415 mock_location.setSpeed(save_speed);
416 }
417 else
418 {
419 //Log.e("NavitVehicle", "call VehicleCallback 004");
420 VehicleCallback(mock_location);
421 }
422 }
423 }
424 catch (Exception e)
425 {
426 e.printStackTrace();
427 }
428 }
429
430 public static void set_last_known_pos_precise_provider()
431 {
432 try
433 {
434 Location l = locationManager_s.getLastKnownLocation(preciseProvider_s);
435 if (l != null)
436 {
437 if (l.getAccuracy() > 0)
438 {
439 if ((l.getLatitude() != 0) && (l.getLongitude() != 0))
440 {
441 if (Navit.PREF_follow_gps)
442 {
443 Log.e("NavitVehicle", "getLastKnownLocation precise (2) l=" + l.toString());
444 last_location = l;
445 //Log.e("NavitVehicle", "call VehicleCallback 005");
446 VehicleCallback(l);
447 }
448 }
449 }
450 }
451 }
452 catch (Exception e)
453 {
454 e.printStackTrace();
455 }
456 }
457
458 public static void set_last_known_pos_fast_provider()
459 {
460 try
461 {
462 // If the 2 providers is the same, only activate one listener
463 if (fastProvider_s != null)
464 {
465 if (Navit.PREF_use_fast_provider)
466 {
467 if (!Navit.DemoVehicle)
468 {
469 Location l = locationManager_s.getLastKnownLocation(fastProvider_s);
470 if (l != null)
471 {
472 if (l.getAccuracy() > 0)
473 {
474 if ((l.getLatitude() != 0) && (l.getLongitude() != 0))
475 {
476 if (Navit.PREF_follow_gps)
477 {
478 Log.e("NavitVehicle", "getLastKnownLocation fast (3) l=" + l.toString());
479 last_location = l;
480 //Log.e("NavitVehicle", "call VehicleCallback 006");
481 VehicleCallback(l);
482 }
483 }
484 }
485 }
486 }
487 }
488 }
489 }
490 catch (Exception e)
491 {
492 e.printStackTrace();
493 }
494
495 }
496
497 public static void turn_on_fast_provider()
498 {
499 try
500 {
501 // If the 2 providers is the same, only activate one listener
502 if (fastProvider_s != null)
503 {
504 if (Navit.PREF_use_fast_provider)
505 {
506 if (!Navit.DemoVehicle)
507 {
508 locationManager_s.requestLocationUpdates(fastProvider_s, 0, 0, fastLocationListener_s);
509 }
510 }
511 }
512 }
513 catch (Exception e)
514 {
515 e.printStackTrace();
516 }
517
518 }
519
520 public static void turn_on_precise_provider()
521 {
522 try
523 {
524 locationManager_s.requestLocationUpdates(preciseProvider_s, 0, 0, preciseLocationListener_s);
525 }
526 catch (Exception e)
527 {
528 e.printStackTrace();
529 }
530
531 turn_on_sat_status();
532
533 try
534 {
535 // try to download aGPS data!!
536 if (Navit.PREF_use_agps)
537 {
538 Navit.downloadGPSXtra(Navit.getBaseContext_);
539 }
540 }
541 catch (Exception e)
542 {
543 e.printStackTrace();
544 }
545 }
546
547 public static void turn_off_precise_provider()
548 {
549 try
550 {
551 if (preciseProvider_s != null)
552 {
553 locationManager_s.removeUpdates(preciseLocationListener_s);
554 }
555 }
556 catch (Exception e)
557 {
558 e.printStackTrace();
559 }
560
561 turn_off_sat_status();
562 }
563
564 public static void turn_on_sat_status()
565 {
566 try
567 {
568 Navit.sats = 0;
569 Navit.satsInFix = 0;
570 if (preciseProvider_s != null)
571 {
572 try
573 {
574 locationManager_s.removeGpsStatusListener(gps_status_listener_s);
575 }
576 catch (Exception e3)
577 {
578 e3.printStackTrace();
579 }
580 locationManager_s.addGpsStatusListener(gps_status_listener_s);
581 }
582 }
583 catch (Exception e)
584 {
585 e.printStackTrace();
586 }
587 System.out.println("turn_ON_sat_status");
588 }
589
590 public static void turn_off_sat_status()
591 {
592 try
593 {
594 Navit.sats = 0;
595 Navit.satsInFix = 0;
596 if (preciseProvider_s != null)
597 {
598 locationManager_s.removeGpsStatusListener(gps_status_listener_s);
599 }
600 }
601 catch (Exception e)
602 {
603 e.printStackTrace();
604 }
605 System.out.println("turn_off_sat_status");
606 }
607
608 public static void turn_off_fast_provider()
609 {
610 try
611 {
612 if (fastProvider_s != null)
613 {
614 locationManager_s.removeUpdates(fastLocationListener_s);
615 }
616 }
617 catch (Exception e)
618 {
619 e.printStackTrace();
620 }
621 }
622
623 public static void turn_off_all_providers()
624 {
625 turn_off_precise_provider();
626 turn_off_fast_provider();
627 }
628
629 public Handler vehicle_handler = new Handler()
630 {
631 public void handleMessage(Message msg)
632 {
633 switch (msg.what)
634 {
635 case 1:
636 // dismissDialog(msg.getData().getInt("dialog_num"));
637 // removeDialog(msg.getData().getInt("dialog_num"));
638 Location l = new Location("Network");
639 l.setLatitude(msg.getData().getFloat("lat"));
640 l.setLongitude(msg.getData().getFloat("lng"));
641 l.setBearing(msg.getData().getFloat("b"));
642 l.setSpeed(0.8f);
643 NavitVehicle.set_mock_location__fast(l);
644 break;
645 case 2:
646 if (update_location_in_progress)
647 {
648 }
649 else
650 {
651 update_location_in_progress = true;
652 VehicleCallback(last_location);
653 update_location_in_progress = false;
654 }
655 break;
656 }
657 }
658 };
659
660 public static void update_compass_heading(float heading)
661 {
662 compass_heading = heading;
663 // use compass heading
664 try
665 {
666 if (Navit.PREF_use_compass_heading_base)
667 {
668 if ((Navit.PREF_use_compass_heading_always) || (last_location.getSpeed() < GPS_SPEED_ABOVE_USE_FOR_HEADING))
669 {
670 if ((lastcompass_update_timestamp + 400) > System.currentTimeMillis())
671 {
672 //Log.e("NavitVehicle", "compass update to fast!");
673 return;
674 }
675 lastcompass_update_timestamp = System.currentTimeMillis();
676
677 last_location.setBearing(compass_heading);
678 // !! ugly hack to make map redraw !!
679 // !! ugly hack to make map redraw !!
680 // !! ugly hack to make map redraw !!
681 if (last_location.getSpeed() == 0.0f)
682 {
683 float save_speed = last_location.getSpeed();
684 last_location.setSpeed(0.2f);
685 if (!Navit.DemoVehicle)
686 {
687 //Log.e("NavitVehicle", "call VehicleCallback 007:start");
688 VehicleCallback(last_location);
689 /*
690 Message m2 = new Message();
691 m2.what = 2;
692 vehicle_handler_.handleMessage(m2);
693 */
694 //Log.e("NavitVehicle", "call VehicleCallback 007:end");
695 }
696 last_location.setSpeed(save_speed);
697 }
698 else
699 {
700 if (!Navit.DemoVehicle)
701 {
702 //Log.e("NavitVehicle", "call VehicleCallback 008");
703 VehicleCallback(last_location);
704 }
705 }
706 // !! ugly hack to make map redraw !!
707 // !! ugly hack to make map redraw !!
708 // !! ugly hack to make map redraw !!
709 }
710 }
711 }
712 catch (Exception e)
713 {
714 //e.printStackTrace();
715 }
716 }
717 }

   
Visit the ZANavi Wiki