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

   
Visit the ZANavi Wiki