providerEntry.criteria = criteria; providersEnabled.put(provider, providerEntry); List<LocationListener> locationUpdateListeners = new ArrayList<>(getRequestLocationUpdateListeners()); for (LocationListener locationUpdateListener : locationUpdateListeners) { if (isEnabled) { getContext().sendBroadcast(intent); Set<PendingIntent> requestLocationUdpatePendingIntentSet = requestLocationUdpateCriteriaPendingIntents .keySet();
@Test public void shouldNotSetBestEnabledProviderIfProviderIsDisabled() throws Exception { shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); assertTrue(shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true)); }
public void simulateLocation(Location location) { cleanupRemovedLocationListeners(); setLastKnownLocation(location.getProvider(), location); List<ListenerRegistration> providerListeners = locationListeners.get( location.getProvider()); if (providerListeners == null) return; for (ListenerRegistration listenerReg : providerListeners) { if(listenerReg.lastSeenLocation != null && location != null) { float distanceChange = distanceBetween(location, listenerReg.lastSeenLocation); boolean withinMinDistance = distanceChange < listenerReg.minDistance; boolean exceededMinTime = location.getTime() - listenerReg.lastSeenTime > listenerReg.minTime; if (withinMinDistance || !exceededMinTime) continue; } listenerReg.lastSeenLocation = copyOf(location); listenerReg.lastSeenTime = location == null ? 0 : location.getTime(); listenerReg.listener.onLocationChanged(copyOf(location)); } cleanupRemovedLocationListeners(); }
private void addLocationListener(String provider, LocationListener listener, long minTime, float minDistance) { List<ListenerRegistration> providerListeners = locationListeners.get(provider); if (providerListeners == null) { providerListeners = new ArrayList<>(); locationListeners.put(provider, providerListeners); } removeDuplicates(listener, providerListeners); providerListeners.add(new ListenerRegistration(provider, minTime, minDistance, copyOf(getLastKnownLocation(provider)), listener)); }
/** * Sets the value to return from {@link #isProviderEnabled(String)} for the given {@code provider} * * @param provider * name of the provider whose status to set * @param isEnabled * whether that provider should appear enabled */ public void setProviderEnabled(String provider, boolean isEnabled) { setProviderEnabled(provider, isEnabled, null); }
@Test public void shouldRegisterLocationUpdatesWhenProviderGiven() throws Exception { shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true); Intent someIntent = new Intent("some_action"); PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); locationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, someLocationListenerPendingIntent); assertThat(shadowLocationManager.getRequestLocationUdpateProviderPendingIntents().get(someLocationListenerPendingIntent)).isEqualTo(GPS_PROVIDER); }
@Test public void shouldRegisterLocationUpdatesWhenCriteriaGiven() throws Exception { shadowLocationManager.setProviderEnabled(NETWORK_PROVIDER, true); shadowLocationManager.setBestProvider(LocationManager.NETWORK_PROVIDER, true); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); Intent someIntent = new Intent("some_action"); PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); Criteria someCriteria = new Criteria(); someCriteria.setAccuracy(Criteria.ACCURACY_COARSE); locationManager.requestLocationUpdates(0, 0, someCriteria, someLocationListenerPendingIntent); assertThat(shadowLocationManager.getRequestLocationUdpateCriteriaPendingIntents().get(someLocationListenerPendingIntent)).isEqualTo(someCriteria); }
@Implementation protected void requestLocationUpdates( String provider, long minTime, float minDistance, LocationListener listener, Looper looper) { addLocationListener(provider, listener, minTime, minDistance); }
@Implementation protected List<String> getProviders(boolean enabledOnly) { ArrayList<String> enabledProviders = new ArrayList<>(); for (String provider : getAllProviders()) { if (!enabledOnly || providersEnabled.get(provider) != null) { enabledProviders.add(provider); } } return enabledProviders; }
public Collection<String> getProvidersForListener(LocationListener listener) { cleanupRemovedLocationListeners(); Set<String> providers = new HashSet<>(); for (List<ListenerRegistration> listenerRegistrations : locationListeners.values()) { for (ListenerRegistration listenerRegistration : listenerRegistrations) { if (listenerRegistration.listener == listener) { providers.add(listenerRegistration.provider); } } } return providers; }
public boolean setBestProvider(String provider, boolean enabled) throws Exception { return setBestProvider(provider, enabled, null); }
@Test public void shouldStoreRequestLocationUpdateListeners() throws Exception { TestLocationListener listener = new TestLocationListener(); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 2.0f, listener); assertSame(shadowLocationManager.getRequestLocationUpdateListeners().get(0), listener); }
@Test public void shouldNotThrowExceptionIfLocationListenerRemovedInsideOnLocationChanged() throws Exception { TestLocationListenerSelfRemoval listener = new TestLocationListenerSelfRemoval(locationManager); shadowLocationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, listener); Location location = new Location(GPS_PROVIDER); location.setLatitude(0); location.setLongitude(0); shadowLocationManager.simulateLocation(location); assertThat(shadowLocationManager.getRequestLocationUpdateListeners().size()).isEqualTo(0); }
@Implementation protected void requestLocationUpdates( long minTime, float minDistance, Criteria criteria, PendingIntent pendingIntent) { if (pendingIntent == null) { throw new IllegalStateException("Intent must not be null"); } if (getBestProvider(criteria, true) == null) { throw new IllegalArgumentException("no providers found for criteria"); } requestLocationUdpateCriteriaPendingIntents.put(pendingIntent, criteria); }
private void addLocationListener(String provider, LocationListener listener, long minTime, float minDistance) { List<ListenerRegistration> providerListeners = locationListeners.get(provider); if (providerListeners == null) { providerListeners = new ArrayList<>(); locationListeners.put(provider, providerListeners); } providerListeners.add(new ListenerRegistration(provider, minTime, minDistance, copyOf(getLastKnownLocation(provider)), listener)); }
@Test public void shouldReturnLastKnownLocationForAProvider() throws Exception { assertNull(locationManager.getLastKnownLocation(NETWORK_PROVIDER)); Location networkLocation = new Location(NETWORK_PROVIDER); Location gpsLocation = new Location(GPS_PROVIDER); shadowLocationManager.setLastKnownLocation(NETWORK_PROVIDER, networkLocation); shadowLocationManager.setLastKnownLocation(GPS_PROVIDER, gpsLocation); assertSame(locationManager.getLastKnownLocation(NETWORK_PROVIDER), networkLocation); assertSame(locationManager.getLastKnownLocation(GPS_PROVIDER), gpsLocation); }
private void addLocationListener(String provider, LocationListener listener, long minTime, float minDistance) { List<ListenerRegistration> providerListeners = locationListeners.get(provider); if (providerListeners == null) { providerListeners = new ArrayList<>(); locationListeners.put(provider, providerListeners); } removeDuplicates(listener, providerListeners); providerListeners.add(new ListenerRegistration(provider, minTime, minDistance, copyOf(getLastKnownLocation(provider)), listener)); }
@Test public void shouldDisableProvider() { // No provider is enabled by default, so it must be manually enabled shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); shadowLocationManager.setProviderEnabled(GPS_PROVIDER, false); assertFalse(locationManager.isProviderEnabled(GPS_PROVIDER)); }
@Test public void shouldRemovePendingIntentsWhenRequestingLocationUpdatesUsingLocationListeners() throws Exception { Intent someIntent = new Intent("some_action"); PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); Intent someOtherIntent = new Intent("some_other_action"); PendingIntent someOtherLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someOtherIntent, PendingIntent.FLAG_UPDATE_CURRENT); shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true); shadowLocationManager.setProviderEnabled(NETWORK_PROVIDER, true); locationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, someLocationListenerPendingIntent); locationManager.requestLocationUpdates(NETWORK_PROVIDER, 0, 0, someOtherLocationListenerPendingIntent); locationManager.removeUpdates(someLocationListenerPendingIntent); Map<PendingIntent, String> expectedProviders = new HashMap<>(); expectedProviders.put(someOtherLocationListenerPendingIntent, NETWORK_PROVIDER); assertThat(shadowLocationManager.getRequestLocationUdpateProviderPendingIntents()).isEqualTo(expectedProviders); }
@Test public void shouldRemovePendingIntentsWhenRequestingLocationUpdatesUsingCriteria() throws Exception { Intent someIntent = new Intent("some_action"); PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); Intent someOtherIntent = new Intent("some_other_action"); PendingIntent someOtherLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someOtherIntent, PendingIntent.FLAG_UPDATE_CURRENT); shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); locationManager.requestLocationUpdates(0, 0, criteria, someLocationListenerPendingIntent); locationManager.requestLocationUpdates(0, 0, criteria, someOtherLocationListenerPendingIntent); locationManager.removeUpdates(someLocationListenerPendingIntent); Map<PendingIntent, Criteria> expectedCriteria = new HashMap<>(); expectedCriteria.put(someOtherLocationListenerPendingIntent, criteria); assertThat(shadowLocationManager.getRequestLocationUdpateCriteriaPendingIntents()).isEqualTo(expectedCriteria); }