Ticket #670: fastfix.patch

File fastfix.patch, 4.4 KB (added by tgc, 11 years ago)

The fastfix patch

  • navit/android/src/org/navitproject/navit/NavitVehicle.java

     
    3131public class NavitVehicle {
    3232        private LocationManager locationManager;
    3333        private int vehicle_callbackid;
     34        private String preciseProvider;
     35        private String fastProvider;
    3436        public native void VehicleCallback(int id, Location location);
    3537
    3638        NavitVehicle (Context context, int callbackid) {
    3739                locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
    3840
    39                 Criteria criteria = new Criteria();
    40                 criteria.setAccuracy(Criteria.ACCURACY_FINE);
    41                 criteria.setAltitudeRequired(true);
    42                 criteria.setBearingRequired(true);
    43                 criteria.setCostAllowed(true);
    44                 criteria.setPowerRequirement(Criteria.POWER_HIGH);
     41                /* Use 2 LocationProviders, one precise (usually GPS), and one
     42                   not so precise, but possible faster. The fast provider is
     43                   disabled when the precise provider gets its first fix. */
     44               
     45                // Selection criterias for the precise provider
     46                Criteria highCriteria = new Criteria();
     47                highCriteria.setAccuracy(Criteria.ACCURACY_FINE);
     48                highCriteria.setAltitudeRequired(true);
     49                highCriteria.setBearingRequired(true);
     50                highCriteria.setCostAllowed(true);
     51                highCriteria.setPowerRequirement(Criteria.POWER_HIGH);
    4552
     53                // Selection criterias for the fast provider
     54                Criteria lowCriteria = new Criteria();
     55                lowCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
     56                lowCriteria.setAltitudeRequired(false);
     57                lowCriteria.setBearingRequired(false);
     58                lowCriteria.setCostAllowed(true);
     59                lowCriteria.setPowerRequirement(Criteria.POWER_HIGH);
    4660               
    4761                Log.e("NavitVehicle", "Providers "+locationManager.getAllProviders());
    48                 String provider = locationManager.getBestProvider(criteria, false);
    49                 Log.e("NavitVehicle", "Provider "+provider);
    50                 Location location = locationManager.getLastKnownLocation(provider);
     62               
     63                preciseProvider = locationManager.getBestProvider(highCriteria, false);
     64                Log.e("NavitVehicle", "Precise Provider " + preciseProvider);
     65                fastProvider = locationManager.getBestProvider(lowCriteria, false);
     66                Log.e("NavitVehicle", "Fast Provider " + fastProvider);
    5167                vehicle_callbackid=callbackid;
    52                 locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
     68               
     69                locationManager.requestLocationUpdates(preciseProvider, 0, 0, preciseLocationListener);
     70               
     71                // If the 2 providers is the same, only activate one listener
     72                if (fastProvider == null || preciseProvider.compareTo(fastProvider) == 0) {
     73                        fastProvider = null;
     74                } else {
     75                        locationManager.requestLocationUpdates(fastProvider, 0, 0, fastLocationListener);
     76                }
    5377        }
    54         private final LocationListener locationListener = new LocationListener() {
     78        private final LocationListener preciseLocationListener = new LocationListener() {
    5579                public void onLocationChanged(Location location) {
    5680                       
     81                        // Disable the fast provider if still active
     82                        if (fastProvider != null) {
     83                                locationManager.removeUpdates(fastLocationListener);
     84                                fastProvider = null;
     85                        }
     86                       
    5787                        // Log.e("NavitVehicle", "LocationChanged Accuracy " + location.getAccuracy() + " Altitude " + location.getAltitude() + " Bearing " + location.getBearing() + " Speed " + location.getSpeed() + " Latitude " + location.getLatitude() + " Longitude " + location.getLongitude());
    58                         VehicleCallback(vehicle_callbackid, location); 
     88                        VehicleCallback(vehicle_callbackid, location);
    5989                }
    6090                public void onProviderDisabled(String provider){}
    6191                public void onProviderEnabled(String provider) {}
    6292                public void onStatusChanged(String provider, int status, Bundle extras) {}
    6393        };
     94
     95        private final LocationListener fastLocationListener = new LocationListener() {
     96                public void onLocationChanged(Location location) {
     97                       
     98                        // Log.e("NavitVehicle", "LocationChanged Accuracy " + location.getAccuracy() + " Altitude " + location.getAltitude() + " Bearing " + location.getBearing() + " Speed " + location.getSpeed() + " Latitude " + location.getLatitude() + " Longitude " + location.getLongitude());
     99                        VehicleCallback(vehicle_callbackid, location);
     100                }
     101                public void onProviderDisabled(String provider){}
     102                public void onProviderEnabled(String provider) {}
     103                public void onStatusChanged(String provider, int status, Bundle extras) {}
     104        };
     105
    64106}