use of android.location.Criteria in project glasquare by davidvavra.
the class LocationUtils method getLastLocation.
public static Location getLastLocation() {
LocationManager manager = (LocationManager) App.get().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
List<String> providers = manager.getProviders(criteria, true);
List<Location> locations = new ArrayList<Location>();
for (String provider : providers) {
Location location = manager.getLastKnownLocation(provider);
if (location != null && location.getAccuracy() != 0.0f) {
locations.add(location);
}
}
Collections.sort(locations, new Comparator<Location>() {
@Override
public int compare(Location location, Location location2) {
return (int) (location.getAccuracy() - location2.getAccuracy());
}
});
if (locations.size() > 0) {
return locations.get(0);
}
return null;
}
use of android.location.Criteria in project ignition by mttkay.
the class AbstractIgnitedLocationManagerTest method shouldNotRequestUpdatesFromGpsIfBatteryLow.
@Test
public void shouldNotRequestUpdatesFromGpsIfBatteryLow() {
sendBatteryLevelChangedBroadcast(10);
resume();
Map<PendingIntent, Criteria> locationPendingIntents = shadowLocationManager.getRequestLocationUdpateCriteriaPendingIntents();
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
assertThat("Updates from " + LocationManager.GPS_PROVIDER + " provider shouldn't be requested when battery power is low!", !locationPendingIntents.containsValue(criteria));
}
use of android.location.Criteria in project ignition by mttkay.
the class AbstractIgnitedLocationManagerTest method shouldSwitchToNetworkProviderIfBatteryLow.
@Test
public void shouldSwitchToNetworkProviderIfBatteryLow() {
resume();
Map<PendingIntent, Criteria> locationPendingIntents = shadowLocationManager.getRequestLocationUdpateCriteriaPendingIntents();
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
sendBatteryLevelChangedBroadcast(10);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_BATTERY_LOW);
shadowApp.sendBroadcast(intent);
sendBatteryLevelChangedBroadcast(100);
assertThat("Updates from " + LocationManager.GPS_PROVIDER + " provider shouldn't be requested when battery power is low!", !locationPendingIntents.containsValue(criteria));
}
use of android.location.Criteria in project ignition by mttkay.
the class IgnitedLocationManagerGingerbreadTest method shouldRequestUpdatesFromGpsIfBatteryOkay.
@Test
public void shouldRequestUpdatesFromGpsIfBatteryOkay() {
sendBatteryLevelChangedBroadcast(10);
resume();
Map<PendingIntent, Criteria> locationPendingIntents = shadowLocationManager.getRequestLocationUdpateCriteriaPendingIntents();
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
sendBatteryLevelChangedBroadcast(100);
Intent intent = new Intent(Intent.ACTION_BATTERY_OKAY);
shadowApp.sendBroadcast(intent);
assertThat("Updates from " + LocationManager.GPS_PROVIDER + " provider should be requested when battery power is okay!", locationPendingIntents.containsValue(criteria));
}
use of android.location.Criteria in project robolectric by robolectric.
the class ShadowLocationManager method getBestProviderWithCriteria.
private String getBestProviderWithCriteria(Criteria criteria, boolean enabled) {
List<String> providers = getProviders(enabled);
int powerRequirement = criteria.getPowerRequirement();
int accuracy = criteria.getAccuracy();
for (String provider : providers) {
LocationProviderEntry locationProviderEntry = providersEnabled.get(provider);
if (locationProviderEntry == null) {
continue;
}
List<Criteria> criteriaList = locationProviderEntry.getValue();
if (criteriaList == null) {
continue;
}
for (Criteria criteriaListItem : criteriaList) {
if (criteria.equals(criteriaListItem)) {
return provider;
} else if (criteriaListItem.getAccuracy() == accuracy) {
return provider;
} else if (criteriaListItem.getPowerRequirement() == powerRequirement) {
return provider;
}
}
}
// TODO: these conditions are incomplete
for (String provider : providers) {
if (provider.equals(LocationManager.NETWORK_PROVIDER) && (accuracy == Criteria.ACCURACY_COARSE || powerRequirement == Criteria.POWER_LOW)) {
return provider;
} else if (provider.equals(LocationManager.GPS_PROVIDER) && accuracy == Criteria.ACCURACY_FINE && powerRequirement != Criteria.POWER_LOW) {
return provider;
}
}
// No enabled provider found with the desired criteria, then return the the first registered provider(?)
return providers.isEmpty() ? null : providers.get(0);
}
Aggregations