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

   
Visit the ZANavi Wiki