ListenerRegistration(String provider, long minTime, float minDistance, Location locationAtCreation, LocationListener listener) { this.provider = provider; this.minTime = minTime; this.minDistance = minDistance; this.lastSeenTime = locationAtCreation == null ? 0 : locationAtCreation.getTime(); this.lastSeenLocation = locationAtCreation; this.listener = listener; } }
/** * @return the last know best location */ private Location getLastBestLocation() { Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); long GPSLocationTime = 0; if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); } long NetLocationTime = 0; if (null != locationNet) { NetLocationTime = locationNet.getTime(); } if ( 0 < GPSLocationTime - NetLocationTime ) { return locationGPS; } else { return locationNet; } }
private Location getLastLocation(String provider) { Location lastLocation = locationManager.getLastKnownLocation(provider); if (lastLocation != null) { long lastLocationTs = lastLocation.getTime(); long locationAgeMs = System.currentTimeMillis() - lastLocationTs; if (locationAgeMs < params.getLastLocationMaxAgeMs()) { return lastLocation; } } return null; }
public LocationAssert hasTime(long time) { isNotNull(); long actualTime = actual.getTime(); assertThat(actualTime) // .overridingErrorMessage("Expected time <%s> but was <%s>.", time, actualTime) // .isEqualTo(time); return this; }
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(); }
long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
@Test public void simulateLocation_shouldNotNotifyListenerIfLessThanFastestInterval() throws Exception { TestLocationListener listener = new TestLocationListener(); shadowLocationManager.requestLocationUpdates(GPS_PROVIDER, 2000, 0, listener); long time = System.currentTimeMillis(); Location location1 = new Location(GPS_PROVIDER); location1.setTime(time); Location location2 = new Location(GPS_PROVIDER); location2.setTime(time + 1000); shadowLocationManager.simulateLocation(location1); shadowLocationManager.simulateLocation(location2); assertThat(listener.location.getTime()).isEqualTo(location1.getTime()); }
private Location copyOf(Location location) { if (location == null) return null; Location copy = new Location(location); copy.setAccuracy(location.getAccuracy()); copy.setAltitude(location.getAltitude()); copy.setBearing(location.getBearing()); copy.setExtras(location.getExtras()); copy.setLatitude(location.getLatitude()); copy.setLongitude(location.getLongitude()); copy.setProvider(location.getProvider()); copy.setSpeed(location.getSpeed()); copy.setTime(location.getTime()); return copy; }
return; if (l.getTime() <= lastprovidertimestamp && !force) { Log.d(TAG, "Timestamp not never than last"); return;
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); /*try to see if the location stored on android is close enough for you,and avoid rerequesting the location*/ Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location == null || location.getTime() - Calendar.getInstance().getTimeInMillis() > MAX_LAST_LOCATION_TIME) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, TIME_INTERVAL, LOCATION_INTERVAL, yourLocationListener); } else { //do your handling if the last known location stored on android is enouh for you }
private void updateLocation(Location location) { if (location != null) { this.location = location; latitude.setText(String.valueOf(location.getLatitude())); longitude.setText(String.valueOf(location.getLongitude())); accuracy.setText(String.valueOf(location.getAccuracy()) + "m"); final Date date = new Date(location.getTime()); final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); time.setText(sdf.format(date)); provider.setText(location.getProvider()); } else { latitude.setText(R.string.dd_debug_drawer_location_empty); longitude.setText(R.string.dd_debug_drawer_location_empty); accuracy.setText(R.string.dd_debug_drawer_location_empty); time.setText(R.string.dd_debug_drawer_location_empty); provider.setText(R.string.dd_debug_drawer_location_no_provider); } }
boolean isLocationSufficient(Location location, long acceptableTimePeriod, float acceptableAccuracy) { if (location == null) return false; float givenAccuracy = location.getAccuracy(); long givenTime = location.getTime(); long minAcceptableTime = new Date().getTime() - acceptableTimePeriod; return minAcceptableTime <= givenTime && acceptableAccuracy >= givenAccuracy; } }
@Subscribe(sticky = true) public void onEvent(Location l) { this.locationUpdated = TimeUnit.MILLISECONDS.toSeconds(l.getTime()); notifyPropertyChanged(BR.locationUpdated); }
public void onLocationChanged(@Nullable Location location) { if(location == null) { Timber.e("no location provided"); return; } Timber.v("location update received: tst:%s, acc:%s, lat:%s, lon:%s",location.getTime(), location.getAccuracy(), location.getLatitude(), location.getLongitude()); if (location.getTime() > locationRepo.getCurrentLocationTime()) { locationProcessor.onLocationChanged(location); } }