Search in sources :

Example 1 with Criteria

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;
}
Also used : LocationManager(android.location.LocationManager) ArrayList(java.util.ArrayList) Criteria(android.location.Criteria) Location(android.location.Location)

Example 2 with Criteria

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));
}
Also used : PendingIntent(android.app.PendingIntent) Criteria(android.location.Criteria) Test(org.junit.Test)

Example 3 with 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));
}
Also used : Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Criteria(android.location.Criteria) Test(org.junit.Test)

Example 4 with 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));
}
Also used : Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Criteria(android.location.Criteria) Test(org.junit.Test)

Example 5 with 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);
}
Also used : Criteria(android.location.Criteria)

Aggregations

Criteria (android.location.Criteria)51 Location (android.location.Location)19 Test (org.junit.Test)12 LocationManager (android.location.LocationManager)8 ArrayList (java.util.ArrayList)8 PendingIntent (android.app.PendingIntent)6 Intent (android.content.Intent)6 Bundle (android.os.Bundle)6 Handler (android.os.Handler)6 LocationListener (android.location.LocationListener)5 SuppressLint (android.annotation.SuppressLint)2 FacebookException (com.facebook.FacebookException)2 HashMap (java.util.HashMap)2 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)2 PackageManager (android.content.pm.PackageManager)1 ResolveInfo (android.content.pm.ResolveInfo)1 EditTextPreference (android.preference.EditTextPreference)1 ListPreference (android.preference.ListPreference)1 Preference (android.preference.Preference)1 PreferenceCategory (android.preference.PreferenceCategory)1