[android] Made native providers selection more merely, i.e. only providers satisfying to Criteria.ACCURACY_FINE should be returned. All unworking providers will be filtered.

This commit is contained in:
alexzatsepin 2017-01-17 15:14:27 +03:00
parent da9d301120
commit 9c3686c2e0
3 changed files with 15 additions and 12 deletions

View file

@ -1,6 +1,7 @@
package com.mapswithme.maps.location;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
@ -39,6 +40,7 @@ class AndroidNativeProvider extends BaseLocationProvider
mIsActive = true;
for (String provider : providers)
{
sLogger.d("Request location updates from the provider: " + provider);
LocationListener listener = new BaseLocationListener(getLocationFixChecker());
mLocationManager.requestLocationUpdates(provider, LocationHelper.INSTANCE.getInterval(), 0, listener);
mListeners.add(listener);
@ -107,11 +109,10 @@ class AndroidNativeProvider extends BaseLocationProvider
@NonNull
private static List<String> filterProviders(LocationManager locationManager)
{
final List<String> res = locationManager.getProviders(true /* enabledOnly */);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
final List<String> res = locationManager.getProviders(criteria, true /* enabledOnly */);
res.remove(LocationManager.PASSIVE_PROVIDER);
// On Huawei P9 Lite, when all location services are disabled OS returns "local_database" provider,
// but it doesn't provide any locations actually, so consider it as unreliable.
res.remove("local_database");
return res;
}
}

View file

@ -1,6 +1,7 @@
package com.mapswithme.maps.location;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.mapswithme.util.LocationUtils;
@ -16,13 +17,13 @@ class DefaultLocationFixChecker implements LocationFixChecker
return false;
final Location lastLocation = LocationHelper.INSTANCE.getSavedLocation();
return (lastLocation == null || isLocationBetterThanLast(newLocation, lastLocation));
return lastLocation == null || isLocationBetterThanLast(newLocation, lastLocation);
}
boolean isLocationBetterThanLast(Location newLocation, Location lastLocation)
boolean isLocationBetterThanLast(@NonNull Location newLocation, @NonNull Location lastLocation)
{
double speed = Math.max(DEFAULT_SPEED_MPS, (newLocation.getSpeed() + lastLocation.getSpeed()) / 2.0);
double lastAccuracy = (lastLocation.getAccuracy() + speed * LocationUtils.getDiff(lastLocation, newLocation));
return (newLocation.getAccuracy() < lastAccuracy);
double lastAccuracy = lastLocation.getAccuracy() + speed * LocationUtils.getDiff(lastLocation, newLocation);
return newLocation.getAccuracy() < lastAccuracy;
}
}

View file

@ -1,17 +1,18 @@
package com.mapswithme.maps.location;
import android.location.Location;
import android.support.annotation.NonNull;
public class FusedLocationFixChecker extends DefaultLocationFixChecker
class FusedLocationFixChecker extends DefaultLocationFixChecker
{
private static final String GMS_LOCATION_PROVIDER = "fused";
@Override
boolean isLocationBetterThanLast(Location newLocation, Location lastLocation)
boolean isLocationBetterThanLast(@NonNull Location newLocation, @NonNull Location lastLocation)
{
// We believe that google services always return good locations.
return (isFromFusedProvider(newLocation) ||
(!isFromFusedProvider(lastLocation) && super.isLocationBetterThanLast(newLocation, lastLocation)));
return isFromFusedProvider(newLocation) ||
(!isFromFusedProvider(lastLocation) && super.isLocationBetterThanLast(newLocation, lastLocation));
}
private static boolean isFromFusedProvider(Location location)