Allow following a recorded track by pressing the my current location button

This commit is contained in:
Jimmy Shih
2014-01-24 14:02:09 -08:00
parent 409621a240
commit 0691975a4d
@@ -96,8 +96,6 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
private static final int MAP_VIEW_PADDING = 32; private static final int MAP_VIEW_PADDING = 32;
// States from TrackDetailActivity, set in onResume // States from TrackDetailActivity, set in onResume
private long trackId;
private long markerId;
private TrackDataHub trackDataHub; private TrackDataHub trackDataHub;
// Current location // Current location
@@ -116,6 +114,10 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
* 2. user manually zooms/pans * 2. user manually zooms/pans
*/ */
private boolean keepCurrentLocationVisible; private boolean keepCurrentLocationVisible;
private MyTracksLocationManager myTracksLocationManager;
// LocationListener for periodic location request
private LocationListener locationListener;
private OnLocationChangedListener onLocationChangedListener; private OnLocationChangedListener onLocationChangedListener;
@@ -161,22 +163,43 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
myLocationImageButton.setOnClickListener(new View.OnClickListener() { myLocationImageButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
final MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager(
getActivity(), Looper.myLooper(), true);
if (!myTracksLocationManager.isGpsProviderEnabled()) { if (!myTracksLocationManager.isGpsProviderEnabled()) {
String message = GoogleLocationUtils.getGpsDisabledMyLocationMessage(getActivity()); String message = GoogleLocationUtils.getGpsDisabledMyLocationMessage(getActivity());
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
myTracksLocationManager.close();
} else { } else {
myTracksLocationManager.requestLastLocation(new LocationListener() { keepCurrentLocationVisible = true;
@Override if (locationListener != null) {
public void onLocationChanged(Location location) { myTracksLocationManager.removeLocationUpdates(locationListener);
myTracksLocationManager.close(); locationListener = null;
keepCurrentLocationVisible = true; }
setCurrentLocation(location); if (isSelectedTrackRecording()) {
updateCurrentLocation(true); myTracksLocationManager.requestLastLocation(new LocationListener() {
} @Override
}); public void onLocationChanged(Location location) {
if (isResumed()) {
setCurrentLocation(location);
updateCurrentLocation(true);
}
}
});
} else {
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (isResumed()) {
boolean isFirst = setCurrentLocation(location);
updateCurrentLocation(isFirst);
}
}
};
/*
* Set currentLocation to null to cause the first requested location
* to force zoom to the default level.
*/
currentLocation = null;
myTracksLocationManager.requestLocationUpdates(0, 0f, locationListener);
}
} }
} }
}); });
@@ -265,14 +288,12 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
super.onResume(); super.onResume();
// First obtain the states from TrackDetailActivity // First obtain the states from TrackDetailActivity
trackId = ((TrackDetailActivity) getActivity()).getTrackId(); long trackId = ((TrackDetailActivity) getActivity()).getTrackId();
markerId = ((TrackDetailActivity) getActivity()).getMarkerId(); long markerId = ((TrackDetailActivity) getActivity()).getMarkerId();
resumeTrackDataHub(); resumeTrackDataHub();
MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager( myTracksLocationManager = new MyTracksLocationManager(getActivity(), Looper.myLooper(), true);
getActivity(), Looper.myLooper(), false);
boolean isGpsProviderEnabled = myTracksLocationManager.isGpsProviderEnabled(); boolean isGpsProviderEnabled = myTracksLocationManager.isGpsProviderEnabled();
myTracksLocationManager.close();
if (googleMap != null) { if (googleMap != null) {
@@ -291,6 +312,13 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
if (keepCurrentLocationVisible && currentLocation != null && isSelectedTrackRecording()) { if (keepCurrentLocationVisible && currentLocation != null && isSelectedTrackRecording()) {
updateCurrentLocation(true); updateCurrentLocation(true);
} else { } else {
/*
* Clear the current location in case the current location is no longer
* being updated continuously.
*/
if (onLocationChangedListener != null) {
onLocationChangedListener.onLocationChanged(new Location(""));
}
showTrack(); showTrack();
} }
} }
@@ -315,6 +343,11 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
pauseTrackDataHub(); pauseTrackDataHub();
if (locationListener != null) {
myTracksLocationManager.removeLocationUpdates(locationListener);
locationListener = null;
}
myTracksLocationManager.close();
} }
@Override @Override
@@ -499,12 +532,12 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
* @return true if this is the first location * @return true if this is the first location
*/ */
private boolean setCurrentLocation(Location location) { private boolean setCurrentLocation(Location location) {
boolean firstLocation = false; boolean isFirst = false;
if (currentLocation == null && location != null) { if (currentLocation == null && location != null) {
firstLocation = true; isFirst = true;
} }
currentLocation = location; currentLocation = location;
return firstLocation; return isFirst;
} }
/** /**
@@ -554,6 +587,9 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override @Override
public void onGlobalLayout() { public void onGlobalLayout() {
if (!isResumed()) {
return;
}
ApiAdapterFactory.getApiAdapter() ApiAdapterFactory.getApiAdapter()
.removeGlobalLayoutListener(mapView.getViewTreeObserver(), this); .removeGlobalLayoutListener(mapView.getViewTreeObserver(), this);
getActivity().runOnUiThread(new Runnable() { getActivity().runOnUiThread(new Runnable() {
@@ -622,13 +658,17 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
} }
MyTracksProviderUtils MyTracksProviderUtils = Factory.get(getActivity()); MyTracksProviderUtils MyTracksProviderUtils = Factory.get(getActivity());
Waypoint waypoint = MyTracksProviderUtils.getWaypoint(id); Waypoint waypoint = MyTracksProviderUtils.getWaypoint(id);
if (waypoint != null && waypoint.getLocation() != null) { if (waypoint == null) {
Location location = waypoint.getLocation(); return;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
keepCurrentLocationVisible = false;
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, DEFAULT_ZOOM_LEVEL);
googleMap.moveCamera(cameraUpdate);
} }
Location location = waypoint.getLocation();
if (location == null) {
return;
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
keepCurrentLocationVisible = false;
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, DEFAULT_ZOOM_LEVEL);
googleMap.moveCamera(cameraUpdate);
} }
}); });
} }
@@ -664,32 +704,7 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
* @param isGpsProviderEnabled true if gps provider is enabled * @param isGpsProviderEnabled true if gps provider is enabled
*/ */
private void setWarningMessage(boolean isGpsProviderEnabled) { private void setWarningMessage(boolean isGpsProviderEnabled) {
String message; String message = getWarningMessage(isGpsProviderEnabled);
if (!isSelectedTrackRecording()) {
message = null;
} else {
if (isGpsProviderEnabled) {
boolean hasFix;
boolean hasGoodFix;
if (currentLocation == null) {
hasFix = false;
hasGoodFix = false;
} else {
hasFix = !LocationUtils.isLocationOld(currentLocation);
hasGoodFix = currentLocation.hasAccuracy()
&& currentLocation.getAccuracy() < recordingGpsAccuracy;
}
if (!hasFix) {
message = getString(R.string.gps_wait_for_signal);
} else if (!hasGoodFix) {
message = getString(R.string.gps_wait_for_better_signal);
} else {
message = null;
}
} else {
message = GoogleLocationUtils.getGpsDisabledMessage(getActivity());
}
}
if (message == null) { if (message == null) {
messageTextView.setVisibility(View.GONE); messageTextView.setVisibility(View.GONE);
@@ -707,9 +722,43 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
@Override @Override
public void onClick(View v) { public void onClick(View v) {
startActivity(GoogleLocationUtils.newLocationSettingsIntent(getActivity())); if (isResumed()) {
startActivity(GoogleLocationUtils.newLocationSettingsIntent(getActivity()));
}
} }
}); });
} }
} }
/**
* Gets the warning message.
*
* @param isGpsProviderEnabled true if gps provider is enabled
*/
private String getWarningMessage(boolean isGpsProviderEnabled) {
if (!isSelectedTrackRecording()) {
return null;
}
if (!isGpsProviderEnabled) {
return GoogleLocationUtils.getGpsDisabledMessage(getActivity());
}
boolean hasFix;
boolean hasGoodFix;
if (currentLocation == null) {
hasFix = false;
hasGoodFix = false;
} else {
hasFix = !LocationUtils.isLocationOld(currentLocation);
hasGoodFix = currentLocation.hasAccuracy()
&& currentLocation.getAccuracy() < recordingGpsAccuracy;
}
if (!hasFix) {
return getString(R.string.gps_wait_for_signal);
} else if (!hasGoodFix) {
return getString(R.string.gps_wait_for_better_signal);
} else {
return null;
}
}
} }