forked from upstream-mirrors/OpenTracks
Providing network or null locations to activities, in the appropriate situations, and addressing minor style suggestions.
This commit is contained in:
@@ -255,17 +255,18 @@ public class MapActivity extends com.google.android.maps.MapActivity
|
|||||||
* the current location is outside the visible area.
|
* the current location is outside the visible area.
|
||||||
*/
|
*/
|
||||||
private void showCurrentLocation() {
|
private void showCurrentLocation() {
|
||||||
if (currentLocation == null || mapOverlay == null || mapView == null) {
|
if (mapOverlay == null || mapView == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
mapOverlay.setMyLocation(currentLocation);
|
mapOverlay.setMyLocation(currentLocation);
|
||||||
mapView.postInvalidate();
|
mapView.postInvalidate();
|
||||||
if (keepMyLocationVisible && !locationIsVisible(currentLocation)) {
|
|
||||||
|
if (currentLocation != null && keepMyLocationVisible && !locationIsVisible(currentLocation)) {
|
||||||
GeoPoint geoPoint = LocationUtils.getGeoPoint(currentLocation);
|
GeoPoint geoPoint = LocationUtils.getGeoPoint(currentLocation);
|
||||||
MapController controller = mapView.getController();
|
MapController controller = mapView.getController();
|
||||||
controller.animateTo(geoPoint);
|
controller.animateTo(geoPoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -507,10 +508,6 @@ public class MapActivity extends com.google.android.maps.MapActivity
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCurrentLocationChanged(Location location) {
|
public void onCurrentLocationChanged(Location location) {
|
||||||
if (!location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentLocation = location;
|
currentLocation = location;
|
||||||
showCurrentLocation();
|
showCurrentLocation();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,16 +19,15 @@ import static com.google.android.apps.mytracks.Constants.TAG;
|
|||||||
|
|
||||||
import com.google.android.apps.mytracks.content.Track;
|
import com.google.android.apps.mytracks.content.Track;
|
||||||
import com.google.android.apps.mytracks.content.TrackDataHub;
|
import com.google.android.apps.mytracks.content.TrackDataHub;
|
||||||
|
import com.google.android.apps.mytracks.content.TrackDataHub.ListenerDataType;
|
||||||
import com.google.android.apps.mytracks.content.TrackDataListener;
|
import com.google.android.apps.mytracks.content.TrackDataListener;
|
||||||
import com.google.android.apps.mytracks.content.Waypoint;
|
import com.google.android.apps.mytracks.content.Waypoint;
|
||||||
import com.google.android.apps.mytracks.content.TrackDataHub.ListenerDataType;
|
|
||||||
import com.google.android.apps.mytracks.services.tasks.StatusAnnouncerFactory;
|
import com.google.android.apps.mytracks.services.tasks.StatusAnnouncerFactory;
|
||||||
import com.google.android.apps.mytracks.util.ApiFeatures;
|
import com.google.android.apps.mytracks.util.ApiFeatures;
|
||||||
import com.google.android.maps.mytracks.R;
|
import com.google.android.maps.mytracks.R;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.location.LocationManager;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -261,15 +260,15 @@ public class StatsActivity extends Activity implements TrackDataListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCurrentLocationChanged(final Location loc) {
|
public void onCurrentLocationChanged(final Location loc) {
|
||||||
if (!loc.getProvider().equals(LocationManager.GPS_PROVIDER)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dataHub.isRecordingSelected()) {
|
if (dataHub.isRecordingSelected()) {
|
||||||
runOnUiThread(new Runnable() {
|
runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
showLocation(loc);
|
if (loc != null) {
|
||||||
|
showLocation(loc);
|
||||||
|
} else {
|
||||||
|
showUnknownLocation();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
package com.google.android.apps.mytracks.content;
|
package com.google.android.apps.mytracks.content;
|
||||||
|
|
||||||
import static com.google.android.apps.mytracks.Constants.MAX_LOCATION_AGE_MS;
|
import static com.google.android.apps.mytracks.Constants.MAX_LOCATION_AGE_MS;
|
||||||
|
import static com.google.android.apps.mytracks.Constants.MAX_NETWORK_AGE_MS;
|
||||||
import static com.google.android.apps.mytracks.Constants.TAG;
|
import static com.google.android.apps.mytracks.Constants.TAG;
|
||||||
|
|
||||||
import com.google.android.apps.mytracks.Constants;
|
import com.google.android.apps.mytracks.Constants;
|
||||||
@@ -346,7 +347,7 @@ public class TrackDataHub {
|
|||||||
loadSharedPreferences();
|
loadSharedPreferences();
|
||||||
}
|
}
|
||||||
long recordingTrackId = preferences.getLong(RECORDING_TRACK_KEY, -1);
|
long recordingTrackId = preferences.getLong(RECORDING_TRACK_KEY, -1);
|
||||||
return recordingTrackId > 0 && recordingTrackId == selectedTrackId;
|
return recordingTrackId > 0 && recordingTrackId == selectedTrackId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -463,11 +464,20 @@ public class TrackDataHub {
|
|||||||
boolean interestedInSampledOutPoints =
|
boolean interestedInSampledOutPoints =
|
||||||
registration.isInterestedIn(ListenerDataType.SAMPLED_OUT_POINT_UPDATES);
|
registration.isInterestedIn(ListenerDataType.SAMPLED_OUT_POINT_UPDATES);
|
||||||
if (interestedInPoints || interestedInSampledOutPoints) {
|
if (interestedInPoints || interestedInSampledOutPoints) {
|
||||||
if (reloadAll) notifyPointsCleared(listenerSet);
|
long minPointId = 0;
|
||||||
|
int previousNumPoints = 0;
|
||||||
|
if (reloadAll) {
|
||||||
|
// Clear existing points and send them all again
|
||||||
|
notifyPointsCleared(listenerSet);
|
||||||
|
} else {
|
||||||
|
// Send only new points
|
||||||
|
minPointId = registration.lastPointId + 1;
|
||||||
|
previousNumPoints = registration.numLoadedPoints;
|
||||||
|
}
|
||||||
|
|
||||||
notifyPointsUpdated(false,
|
notifyPointsUpdated(false,
|
||||||
reloadAll ? 0 : registration.lastPointId + 1,
|
minPointId,
|
||||||
reloadAll ? 0 : registration.numLoadedPoints,
|
previousNumPoints,
|
||||||
listenerSet,
|
listenerSet,
|
||||||
interestedInSampledOutPoints ? listenerSet : Collections.EMPTY_SET);
|
interestedInSampledOutPoints ? listenerSet : Collections.EMPTY_SET);
|
||||||
}
|
}
|
||||||
@@ -650,7 +660,7 @@ public class TrackDataHub {
|
|||||||
* @param forceUpdate whether to force the notifications to happen
|
* @param forceUpdate whether to force the notifications to happen
|
||||||
* @param listeners the listeners to notify
|
* @param listeners the listeners to notify
|
||||||
*/
|
*/
|
||||||
private void notifyLocationChanged(final Location location, boolean forceUpdate,
|
private void notifyLocationChanged(Location location, boolean forceUpdate,
|
||||||
final Set<TrackDataListener> listeners) {
|
final Set<TrackDataListener> listeners) {
|
||||||
if (location == null) return;
|
if (location == null) return;
|
||||||
if (listeners.isEmpty()) return;
|
if (listeners.isEmpty()) return;
|
||||||
@@ -660,26 +670,56 @@ public class TrackDataHub {
|
|||||||
boolean oldHasFix = hasFix;
|
boolean oldHasFix = hasFix;
|
||||||
boolean oldHasGoodFix = hasGoodFix;
|
boolean oldHasGoodFix = hasGoodFix;
|
||||||
|
|
||||||
// We consider a good fix to be a recent one with reasonable accuracy.
|
long now = System.currentTimeMillis();
|
||||||
if (isGpsLocation) {
|
if (isGpsLocation) {
|
||||||
lastSeenLocation = location;
|
// We consider a good fix to be a recent one with reasonable accuracy.
|
||||||
hasFix = (location != null && System.currentTimeMillis() - location.getTime() <= MAX_LOCATION_AGE_MS);
|
hasFix = !isLocationOld(location, now, MAX_LOCATION_AGE_MS);
|
||||||
hasGoodFix = (location != null && location.getAccuracy() <= minRequiredAccuracy);
|
hasGoodFix = (location != null && location.getAccuracy() <= minRequiredAccuracy);
|
||||||
if (hasFix != oldHasFix || hasGoodFix != oldHasGoodFix || forceUpdate) {
|
} else {
|
||||||
notifyFixType();
|
if (!isLocationOld(lastSeenLocation, now, MAX_LOCATION_AGE_MS)) {
|
||||||
|
// This is a network location, but we have a recent/valid GPS location, just ignore this.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// We haven't gotten a GPS location in a while (or at all), assume we have no fix anymore.
|
||||||
|
hasFix = false;
|
||||||
|
hasGoodFix = false;
|
||||||
|
|
||||||
|
// If the network location is recent, we'll use that.
|
||||||
|
if (isLocationOld(location, now, MAX_NETWORK_AGE_MS)) {
|
||||||
|
// Alas, we have no clue where we are.
|
||||||
|
location = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hasFix != oldHasFix || hasGoodFix != oldHasGoodFix || forceUpdate) {
|
||||||
|
notifyFixType();
|
||||||
|
}
|
||||||
|
|
||||||
|
lastSeenLocation = location;
|
||||||
|
final Location finalLoc = location;
|
||||||
runInListenerThread(new Runnable() {
|
runInListenerThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
for (TrackDataListener listener : listeners) {
|
for (TrackDataListener listener : listeners) {
|
||||||
listener.onCurrentLocationChanged(location);
|
listener.onCurrentLocationChanged(finalLoc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the given location is either invalid or too old.
|
||||||
|
*
|
||||||
|
* @param location the location to test
|
||||||
|
* @param now the current timestamp in milliseconds
|
||||||
|
* @param maxAge the maximum age in milliseconds
|
||||||
|
* @return true if it's invalid or too old, false otherwise
|
||||||
|
*/
|
||||||
|
private static boolean isLocationOld(Location location, long now, long maxAge) {
|
||||||
|
return !LocationUtils.isValidLocation(location) || now - location.getTime() > maxAge;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Notifies that the current heading has changed.
|
* Notifies that the current heading has changed.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user