NearestFavoriteStationWidgetService: Use 'fused' location provider from Google Play Services, if available.

This commit is contained in:
Andreas Schildbach 2019-01-09 23:16:32 +01:00
parent 681398598b
commit 56e13aa8cf

View file

@ -60,6 +60,7 @@ import android.location.Criteria;
import android.location.Location; import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
import android.location.LocationManager; import android.location.LocationManager;
import android.location.LocationProvider;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
@ -121,14 +122,22 @@ public class NearestFavoriteStationWidgetService extends JobIntentService {
return; return;
} }
// we want to use as little power as possible log.info("Available location providers: {}", locationManager.getAllProviders());
final Criteria criteria = new Criteria(); final String provider;
criteria.setPowerRequirement(Criteria.POWER_LOW); final LocationProvider fused = locationManager.getProvider("fused");
final String provider = locationManager.getBestProvider(criteria, true); if (fused != null) {
if (provider == null || LocationManager.PASSIVE_PROVIDER.equals(provider)) { // prefer fused provider from Google Play Services
widgetsMessage(appWidgetIds, getString(R.string.acquire_location_no_provider)); provider = fused.getName();
log.info("No location provider found"); } else {
return; // otherwise, we want to use as little power as possible
final Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);
if (provider == null || LocationManager.PASSIVE_PROVIDER.equals(provider)) {
widgetsMessage(appWidgetIds, getString(R.string.acquire_location_no_provider));
log.info("No location provider found");
return;
}
} }
widgetsHeader(appWidgetIds, getString(R.string.acquire_location_start, provider)); widgetsHeader(appWidgetIds, getString(R.string.acquire_location_start, provider));