forked from upstream-mirrors/OpenTracks
Change MyTracksMapFragment to not listen to location changes.
This commit is contained in:
@@ -97,6 +97,8 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
|
|
||||||
// Current location
|
// Current location
|
||||||
private Location currentLocation;
|
private Location currentLocation;
|
||||||
|
private Location lastTrackPoint;
|
||||||
|
private int minRequiredAccuracy = PreferencesUtils.MIN_REQUIRED_ACCURACY_DEFAULT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* True to continue keeping the current location visible on the screen.
|
* True to continue keeping the current location visible on the screen.
|
||||||
@@ -285,7 +287,12 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
public void onSaveInstanceState(Bundle outState) {
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
super.onSaveInstanceState(outState);
|
super.onSaveInstanceState(outState);
|
||||||
if (currentLocation != null) {
|
if (currentLocation != null) {
|
||||||
outState.putParcelable(CURRENT_LOCATION_KEY, currentLocation);
|
/*
|
||||||
|
* currentLocation is a MyTracksLocation object, which cannot be
|
||||||
|
* unmarshalled. Thus creating a Location object before placing it in the
|
||||||
|
* bundle.
|
||||||
|
*/
|
||||||
|
outState.putParcelable(CURRENT_LOCATION_KEY, new Location(currentLocation));
|
||||||
}
|
}
|
||||||
outState.putBoolean(KEEP_CURRENT_LOCATION_VISIBLE_KEY, keepCurrentLocationVisible);
|
outState.putBoolean(KEEP_CURRENT_LOCATION_VISIBLE_KEY, keepCurrentLocationVisible);
|
||||||
outState.putBoolean(ZOOM_TO_CURRENT_LOCATION_KEY, zoomToCurrentLocation);
|
outState.putBoolean(ZOOM_TO_CURRENT_LOCATION_KEY, zoomToCurrentLocation);
|
||||||
@@ -381,86 +388,13 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLocationStateChanged(final LocationState state) {
|
public void onLocationStateChanged(LocationState locationState) {
|
||||||
if (isResumed()) {
|
// We don't care.
|
||||||
getActivity().runOnUiThread(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (!isResumed() || googleMap == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
boolean myLocationEnabled = true;
|
|
||||||
if (state == LocationState.DISABLED) {
|
|
||||||
setCurrentLocation(null);
|
|
||||||
myLocationEnabled = false;
|
|
||||||
}
|
|
||||||
googleMap.setMyLocationEnabled(myLocationEnabled);
|
|
||||||
|
|
||||||
String message;
|
|
||||||
boolean isGpsDisabled;
|
|
||||||
if (!isSelectedTrackRecording()) {
|
|
||||||
message = null;
|
|
||||||
isGpsDisabled = false;
|
|
||||||
} else {
|
|
||||||
switch (state) {
|
|
||||||
case DISABLED:
|
|
||||||
String setting = getString(GoogleLocationUtils.isAvailable(getActivity())
|
|
||||||
? R.string.gps_google_location_settings
|
|
||||||
: R.string.gps_location_access);
|
|
||||||
message = getString(R.string.gps_disabled, setting);
|
|
||||||
isGpsDisabled = true;
|
|
||||||
break;
|
|
||||||
case NO_FIX:
|
|
||||||
message = getString(R.string.gps_wait_for_signal);
|
|
||||||
isGpsDisabled = false;
|
|
||||||
break;
|
|
||||||
case BAD_FIX:
|
|
||||||
message = getString(R.string.gps_wait_for_better_signal);
|
|
||||||
isGpsDisabled = false;
|
|
||||||
break;
|
|
||||||
case GOOD_FIX:
|
|
||||||
message = null;
|
|
||||||
isGpsDisabled = false;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("Unexpected state: " + state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (message == null) {
|
|
||||||
messageTextView.setVisibility(View.GONE);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
messageTextView.setText(message);
|
|
||||||
messageTextView.setVisibility(View.VISIBLE);
|
|
||||||
if (isGpsDisabled) {
|
|
||||||
Toast.makeText(getActivity(), R.string.gps_not_found, Toast.LENGTH_LONG).show();
|
|
||||||
|
|
||||||
// Click to show the location source settings
|
|
||||||
messageTextView.setOnClickListener(new OnClickListener() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
Intent intent = GoogleLocationUtils.isAvailable(getActivity()) ? new Intent(
|
|
||||||
GoogleLocationUtils.ACTION_GOOGLE_LOCATION_SETTINGS)
|
|
||||||
: new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
|
||||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
||||||
startActivity(intent);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
messageTextView.setOnClickListener(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(final Location location) {
|
public void onLocationChanged(Location location) {
|
||||||
if (isResumed()) {
|
// We don't care.
|
||||||
setCurrentLocation(location);
|
|
||||||
updateCurrentLocation();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -497,6 +431,7 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearTrackPoints() {
|
public void clearTrackPoints() {
|
||||||
|
lastTrackPoint = null;
|
||||||
if (isResumed()) {
|
if (isResumed()) {
|
||||||
mapOverlay.clearPoints();
|
mapOverlay.clearPoints();
|
||||||
reloadPaths = true;
|
reloadPaths = true;
|
||||||
@@ -505,6 +440,7 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSampledInTrackPoint(final Location location) {
|
public void onSampledInTrackPoint(final Location location) {
|
||||||
|
lastTrackPoint = location;
|
||||||
if (isResumed()) {
|
if (isResumed()) {
|
||||||
mapOverlay.addLocation(location);
|
mapOverlay.addLocation(location);
|
||||||
}
|
}
|
||||||
@@ -512,7 +448,7 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSampledOutTrackPoint(Location location) {
|
public void onSampledOutTrackPoint(Location location) {
|
||||||
// We don't care.
|
lastTrackPoint = location;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -530,6 +466,15 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
if (isResumed() && googleMap != null) {
|
if (isResumed() && googleMap != null) {
|
||||||
mapOverlay.update(googleMap, paths, reloadPaths);
|
mapOverlay.update(googleMap, paths, reloadPaths);
|
||||||
reloadPaths = false;
|
reloadPaths = false;
|
||||||
|
|
||||||
|
if (!isSelectedTrackRecording() || isSelectedTrackPaused()) {
|
||||||
|
lastTrackPoint = null;
|
||||||
|
}
|
||||||
|
if (lastTrackPoint != null) {
|
||||||
|
setCurrentLocation(lastTrackPoint);
|
||||||
|
updateCurrentLocation();
|
||||||
|
setWarningMessage(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -576,8 +521,8 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onMinRequiredAccuracy(int minRequiredAccuracy) {
|
public boolean onMinRequiredAccuracy(int newValue) {
|
||||||
// We don't care.
|
minRequiredAccuracy = newValue;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -595,7 +540,17 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
trackDataHub = ((TrackDetailActivity) getActivity()).getTrackDataHub();
|
trackDataHub = ((TrackDetailActivity) getActivity()).getTrackDataHub();
|
||||||
trackDataHub.registerTrackDataListener(this, EnumSet.of(TrackDataType.SELECTED_TRACK,
|
trackDataHub.registerTrackDataListener(this, EnumSet.of(TrackDataType.SELECTED_TRACK,
|
||||||
TrackDataType.WAYPOINTS_TABLE, TrackDataType.SAMPLED_IN_TRACK_POINTS_TABLE,
|
TrackDataType.WAYPOINTS_TABLE, TrackDataType.SAMPLED_IN_TRACK_POINTS_TABLE,
|
||||||
TrackDataType.LOCATION));
|
TrackDataType.SAMPLED_OUT_TRACK_POINTS_TABLE, TrackDataType.PREFERENCE));
|
||||||
|
|
||||||
|
MyTracksLocationManager myTracksLocationManager = new MyTracksLocationManager(
|
||||||
|
getActivity(), Looper.myLooper());
|
||||||
|
boolean isGpsProviderEnabled = myTracksLocationManager.isGpsProviderEnabled();
|
||||||
|
|
||||||
|
if (googleMap != null) {
|
||||||
|
googleMap.setMyLocationEnabled(isGpsProviderEnabled);
|
||||||
|
}
|
||||||
|
setWarningMessage(isGpsProviderEnabled);
|
||||||
|
myTracksLocationManager.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -617,6 +572,14 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
return trackDataHub != null && trackDataHub.isSelectedTrackRecording();
|
return trackDataHub != null && trackDataHub.isSelectedTrackRecording();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the selected track is paused. Needs to be synchronized
|
||||||
|
* because trackDataHub can be accessed by multiple threads.
|
||||||
|
*/
|
||||||
|
private synchronized boolean isSelectedTrackPaused() {
|
||||||
|
return trackDataHub != null && trackDataHub.isSelectedTrackPaused();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the current location.
|
* Sets the current location.
|
||||||
*
|
*
|
||||||
@@ -750,4 +713,65 @@ public class MyTracksMapFragment extends SupportMapFragment implements TrackData
|
|||||||
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
|
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
|
||||||
return googleMap.getProjection().getVisibleRegion().latLngBounds.contains(latLng);
|
return googleMap.getProjection().getVisibleRegion().latLngBounds.contains(latLng);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the warning message.
|
||||||
|
*
|
||||||
|
* @param isGpsProviderEnabled true if gps provider is enabled
|
||||||
|
*/
|
||||||
|
private void setWarningMessage(boolean isGpsProviderEnabled) {
|
||||||
|
String message;
|
||||||
|
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.getAccuracy() <= minRequiredAccuracy;
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
String setting = getString(
|
||||||
|
GoogleLocationUtils.isAvailable(getActivity()) ? R.string.gps_google_location_settings
|
||||||
|
: R.string.gps_location_access);
|
||||||
|
message = getString(R.string.gps_disabled, setting);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message == null) {
|
||||||
|
messageTextView.setVisibility(View.GONE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
messageTextView.setText(message);
|
||||||
|
messageTextView.setVisibility(View.VISIBLE);
|
||||||
|
if (isGpsProviderEnabled) {
|
||||||
|
messageTextView.setOnClickListener(null);
|
||||||
|
} else {
|
||||||
|
Toast.makeText(getActivity(), R.string.gps_not_found, Toast.LENGTH_LONG).show();
|
||||||
|
|
||||||
|
// Click to show the location source settings
|
||||||
|
messageTextView.setOnClickListener(new OnClickListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
Intent intent = GoogleLocationUtils.isAvailable(getActivity()) ? new Intent(
|
||||||
|
GoogleLocationUtils.ACTION_GOOGLE_LOCATION_SETTINGS)
|
||||||
|
: new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
|
||||||
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
startActivity(intent);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -49,7 +49,6 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
|
|
||||||
public static final String STATS_FRAGMENT_TAG = "statsFragment";
|
public static final String STATS_FRAGMENT_TAG = "statsFragment";
|
||||||
|
|
||||||
private static final long MAX_LOCATION_AGE_MS = 60 * 1000; // 1 minute
|
|
||||||
private static final int ONE_SECOND = 1000;
|
private static final int ONE_SECOND = 1000;
|
||||||
|
|
||||||
private TrackDataHub trackDataHub;
|
private TrackDataHub trackDataHub;
|
||||||
@@ -165,7 +164,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (lastLocation != null) {
|
if (lastLocation != null) {
|
||||||
boolean hasFix = !isLocationOld(lastLocation);
|
boolean hasFix = !LocationUtils.isLocationOld(lastLocation);
|
||||||
boolean hasGoodFix = lastLocation.getAccuracy() <= minRequiredAccuracy;
|
boolean hasGoodFix = lastLocation.getAccuracy() <= minRequiredAccuracy;
|
||||||
|
|
||||||
if (!hasFix || !hasGoodFix) {
|
if (!hasFix || !hasGoodFix) {
|
||||||
@@ -272,16 +271,6 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
|||||||
return trackDataHub != null && trackDataHub.isSelectedTrackPaused();
|
return trackDataHub != null && trackDataHub.isSelectedTrackPaused();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true if a location is old.
|
|
||||||
*
|
|
||||||
* @param location the location
|
|
||||||
*/
|
|
||||||
private boolean isLocationOld(Location location) {
|
|
||||||
return !LocationUtils.isValidLocation(location)
|
|
||||||
|| (System.currentTimeMillis() - location.getTime() > MAX_LOCATION_AGE_MS);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the UI.
|
* Updates the UI.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ import java.util.Stack;
|
|||||||
*/
|
*/
|
||||||
public class LocationUtils {
|
public class LocationUtils {
|
||||||
|
|
||||||
|
private static final long MAX_LOCATION_AGE_MS = 60 * 1000; // 1 minute
|
||||||
|
|
||||||
private LocationUtils() {}
|
private LocationUtils() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -159,4 +161,14 @@ public class LocationUtils {
|
|||||||
return location != null && Math.abs(location.getLatitude()) <= 90
|
return location != null && Math.abs(location.getLatitude()) <= 90
|
||||||
&& Math.abs(location.getLongitude()) <= 180;
|
&& Math.abs(location.getLongitude()) <= 180;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if a location is old.
|
||||||
|
*
|
||||||
|
* @param location the location
|
||||||
|
*/
|
||||||
|
public static boolean isLocationOld(Location location) {
|
||||||
|
return !LocationUtils.isValidLocation(location)
|
||||||
|
|| (System.currentTimeMillis() - location.getTime() > MAX_LOCATION_AGE_MS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user