forked from upstream-mirrors/OpenTracks
Merge heads
This commit is contained in:
@@ -114,12 +114,12 @@ public class MarkerEditActivity extends AbstractMyTracksActivity {
|
||||
if (newMarker) {
|
||||
statisticsSection.setVisibility(View.GONE);
|
||||
waypointSection.setVisibility(View.VISIBLE);
|
||||
int nextMarkerNumber = trackId == -1L ? -1
|
||||
: MyTracksProviderUtils.Factory.get(this).getNextMarkerNumber(trackId, false);
|
||||
if (nextMarkerNumber == -1) {
|
||||
nextMarkerNumber = 0;
|
||||
int nextWaypointNumber = trackId == -1L ? -1
|
||||
: MyTracksProviderUtils.Factory.get(this).getNextWaypointNumber(trackId, false);
|
||||
if (nextWaypointNumber == -1) {
|
||||
nextWaypointNumber = 0;
|
||||
}
|
||||
waypointName.setText(getString(R.string.marker_name_format, nextMarkerNumber));
|
||||
waypointName.setText(getString(R.string.marker_name_format, nextWaypointNumber));
|
||||
waypointName.selectAll();
|
||||
waypointMarkerType.setText("");
|
||||
waypointDescription.setText("");
|
||||
|
||||
@@ -125,7 +125,7 @@ public class TrackEditActivity extends AbstractMyTracksActivity {
|
||||
Button cancel = (Button) findViewById(R.id.track_edit_cancel);
|
||||
if (getIntent().getBooleanExtra(EXTRA_NEW_TRACK, false)) {
|
||||
String trackName = TrackNameUtils.getTrackName(
|
||||
this, -1L, -1L, myTracksProviderUtils.getLastLocation());
|
||||
this, -1L, -1L, myTracksProviderUtils.getLastValidTrackPoint(trackId));
|
||||
if (trackName != null) {
|
||||
name.setText(trackName);
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ public class SearchEngine {
|
||||
queryLikeSelection,
|
||||
queryLikeSelection };
|
||||
|
||||
Cursor tracksCursor = providerUtils.getTracksCursor(
|
||||
Cursor tracksCursor = providerUtils.getTrackCursor(
|
||||
TRACK_SELECTION_QUERY, trackSelectionArgs, TRACK_SELECTION_ORDER);
|
||||
if (tracksCursor != null) {
|
||||
try {
|
||||
@@ -213,21 +213,21 @@ public class SearchEngine {
|
||||
queryLikeSelection2,
|
||||
queryLikeSelection2,
|
||||
queryLikeSelection2 };
|
||||
Cursor waypointsCursor = providerUtils.getWaypointsCursor(
|
||||
Cursor waypointCursor = providerUtils.getWaypointCursor(
|
||||
WAYPOINT_SELECTION_QUERY, waypointSelectionArgs, WAYPOINT_SELECTION_ORDER,
|
||||
MAX_SCORED_WAYPOINTS);
|
||||
if (waypointsCursor != null) {
|
||||
if (waypointCursor != null) {
|
||||
try {
|
||||
waypoints.ensureCapacity(waypointsCursor.getCount());
|
||||
waypoints.ensureCapacity(waypointCursor.getCount());
|
||||
|
||||
while (waypointsCursor.moveToNext()) {
|
||||
Waypoint waypoint = providerUtils.createWaypoint(waypointsCursor);
|
||||
while (waypointCursor.moveToNext()) {
|
||||
Waypoint waypoint = providerUtils.createWaypoint(waypointCursor);
|
||||
if (LocationUtils.isValidLocation(waypoint.getLocation())) {
|
||||
waypoints.add(waypoint);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
waypointsCursor.close();
|
||||
waypointCursor.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -563,7 +563,7 @@ public class TrackDataHub implements DataSourceListener {
|
||||
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = myTracksProviderUtils.getWaypointsCursor(
|
||||
cursor = myTracksProviderUtils.getWaypointCursor(
|
||||
selectedTrackId, 0L, MAX_DISPLAYED_WAYPOINTS_POINTS);
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
do {
|
||||
@@ -614,9 +614,9 @@ public class TrackDataHub implements DataSourceListener {
|
||||
long localLastSeenLocationId = updateSamplingState ? lastSeenLocationId : -1L;
|
||||
long maxPointId = updateSamplingState ? -1L : lastSeenLocationId;
|
||||
|
||||
long lastLocationId = myTracksProviderUtils.getLastTrackLocationId(selectedTrackId);
|
||||
long lastTrackPointId = myTracksProviderUtils.getLastTrackPointId(selectedTrackId);
|
||||
int samplingFrequency = -1;
|
||||
LocationIterator iterator = myTracksProviderUtils.getLocationIterator(
|
||||
LocationIterator iterator = myTracksProviderUtils.getTrackPointLocationIterator(
|
||||
selectedTrackId, localLastSeenLocationId + 1, false, locationFactory);
|
||||
boolean includeNextPoint = false;
|
||||
while (iterator.hasNext()) {
|
||||
@@ -633,7 +633,7 @@ public class TrackDataHub implements DataSourceListener {
|
||||
}
|
||||
|
||||
if (samplingFrequency == -1) {
|
||||
long numTotalPoints = Math.max(0L, lastLocationId - localFirstSeenLocationId);
|
||||
long numTotalPoints = Math.max(0L, lastTrackPointId - localFirstSeenLocationId);
|
||||
samplingFrequency = 1 + (int) (numTotalPoints / targetNumPoints);
|
||||
}
|
||||
|
||||
@@ -646,7 +646,7 @@ public class TrackDataHub implements DataSourceListener {
|
||||
} else {
|
||||
// Also include the last point if the selected track is not recording.
|
||||
if (includeNextPoint || (localNumLoadedPoints % samplingFrequency == 0)
|
||||
|| (locationId == lastLocationId && !isSelectedTrackRecording())) {
|
||||
|| (locationId == lastTrackPointId && !isSelectedTrackRecording())) {
|
||||
includeNextPoint = false;
|
||||
for (TrackDataListener trackDataListener : sampledInListeners) {
|
||||
trackDataListener.onSampledInTrackPoint(location);
|
||||
|
||||
@@ -27,7 +27,7 @@ import com.google.android.apps.mytracks.content.TrackDataListener;
|
||||
import com.google.android.apps.mytracks.content.TrackDataType;
|
||||
import com.google.android.apps.mytracks.content.Waypoint;
|
||||
import com.google.android.apps.mytracks.stats.TripStatistics;
|
||||
import com.google.android.apps.mytracks.stats.TripStatisticsBuilder;
|
||||
import com.google.android.apps.mytracks.stats.TripStatisticsUpdater;
|
||||
import com.google.android.apps.mytracks.util.LocationUtils;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
import com.google.android.apps.mytracks.util.UnitConversions;
|
||||
@@ -61,7 +61,7 @@ public class ChartFragment extends Fragment implements TrackDataListener {
|
||||
private TrackDataHub trackDataHub;
|
||||
|
||||
// Stats gathered from the received data
|
||||
private TripStatisticsBuilder tripStatisticsBuilder;
|
||||
private TripStatisticsUpdater tripStatisticsUpdater;
|
||||
private long startTime;
|
||||
|
||||
private boolean metricUnits = PreferencesUtils.METRIC_UNITS_DEFAULT;
|
||||
@@ -83,7 +83,7 @@ public class ChartFragment extends Fragment implements TrackDataListener {
|
||||
private final Runnable updateChart = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (trackDataHub == null) {
|
||||
if (!isResumed() || trackDataHub == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -178,111 +178,141 @@ public class ChartFragment extends Fragment implements TrackDataListener {
|
||||
|
||||
@Override
|
||||
public void onTrackUpdated(Track track) {
|
||||
if (track == null || track.getTripStatistics() == null) {
|
||||
startTime = -1L;
|
||||
return;
|
||||
if (isResumed()) {
|
||||
if (track == null || track.getTripStatistics() == null) {
|
||||
startTime = -1L;
|
||||
return;
|
||||
}
|
||||
startTime = track.getTripStatistics().getStartTime();
|
||||
}
|
||||
startTime = track.getTripStatistics().getStartTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearTrackPoints() {
|
||||
tripStatisticsBuilder = startTime != -1L ? new TripStatisticsBuilder(startTime) : null;
|
||||
pendingPoints.clear();
|
||||
chartView.reset();
|
||||
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
chartView.resetScroll();
|
||||
}
|
||||
});
|
||||
if (isResumed()) {
|
||||
tripStatisticsUpdater = startTime != -1L ? new TripStatisticsUpdater(startTime) : null;
|
||||
pendingPoints.clear();
|
||||
chartView.reset();
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (isResumed()) {
|
||||
chartView.resetScroll();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSampledInTrackPoint(Location location) {
|
||||
double[] data = new double[ChartView.NUM_SERIES + 1];
|
||||
fillDataPoint(location, data);
|
||||
pendingPoints.add(data);
|
||||
if (isResumed()) {
|
||||
double[] data = new double[ChartView.NUM_SERIES + 1];
|
||||
fillDataPoint(location, data);
|
||||
pendingPoints.add(data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSampledOutTrackPoint(Location location) {
|
||||
fillDataPoint(location, null);
|
||||
if (isResumed()) {
|
||||
fillDataPoint(location, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSegmentSplit(Location location) {
|
||||
fillDataPoint(location, null);
|
||||
if (isResumed()) {
|
||||
fillDataPoint(location, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNewTrackPointsDone() {
|
||||
chartView.addDataPoints(pendingPoints);
|
||||
pendingPoints.clear();
|
||||
getActivity().runOnUiThread(updateChart);
|
||||
if (isResumed()) {
|
||||
chartView.addDataPoints(pendingPoints);
|
||||
pendingPoints.clear();
|
||||
getActivity().runOnUiThread(updateChart);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearWaypoints() {
|
||||
chartView.clearWaypoints();
|
||||
if (isResumed()) {
|
||||
chartView.clearWaypoints();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNewWaypoint(Waypoint waypoint) {
|
||||
if (waypoint != null && LocationUtils.isValidLocation(waypoint.getLocation())) {
|
||||
if (isResumed() && waypoint != null && LocationUtils.isValidLocation(waypoint.getLocation())) {
|
||||
chartView.addWaypoint(waypoint);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNewWaypointsDone() {
|
||||
getActivity().runOnUiThread(updateChart);
|
||||
if (isResumed()) {
|
||||
getActivity().runOnUiThread(updateChart);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMetricUnitsChanged(boolean metric) {
|
||||
if (metricUnits == metric) {
|
||||
return false;
|
||||
}
|
||||
metricUnits = metric;
|
||||
chartView.setMetricUnits(metricUnits);
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
chartView.requestLayout();
|
||||
if (isResumed()) {
|
||||
if (metricUnits == metric) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return true;
|
||||
metricUnits = metric;
|
||||
chartView.setMetricUnits(metricUnits);
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (isResumed()) {
|
||||
chartView.requestLayout();
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onReportSpeedChanged(boolean speed) {
|
||||
if (reportSpeed == speed) {
|
||||
return false;
|
||||
}
|
||||
reportSpeed = speed;
|
||||
chartView.setReportSpeed(reportSpeed);
|
||||
boolean chartShowSpeed = PreferencesUtils.getBoolean(
|
||||
getActivity(), R.string.chart_show_speed_key, PreferencesUtils.CHART_SHOW_SPEED_DEFAULT);
|
||||
setSeriesEnabled(ChartView.SPEED_SERIES, chartShowSpeed && reportSpeed);
|
||||
setSeriesEnabled(ChartView.PACE_SERIES, chartShowSpeed && !reportSpeed);
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
chartView.requestLayout();
|
||||
if (isResumed()) {
|
||||
if (reportSpeed == speed) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
return true;
|
||||
reportSpeed = speed;
|
||||
chartView.setReportSpeed(reportSpeed);
|
||||
boolean chartShowSpeed = PreferencesUtils.getBoolean(
|
||||
getActivity(), R.string.chart_show_speed_key, PreferencesUtils.CHART_SHOW_SPEED_DEFAULT);
|
||||
setSeriesEnabled(ChartView.SPEED_SERIES, chartShowSpeed && reportSpeed);
|
||||
setSeriesEnabled(ChartView.PACE_SERIES, chartShowSpeed && !reportSpeed);
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (isResumed()) {
|
||||
chartView.requestLayout();
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onMinRecordingDistanceChanged(int value) {
|
||||
if (minRecordingDistance == value) {
|
||||
return false;
|
||||
if (isResumed()) {
|
||||
if (minRecordingDistance == value) {
|
||||
return false;
|
||||
}
|
||||
minRecordingDistance = value;
|
||||
return true;
|
||||
}
|
||||
minRecordingDistance = value;
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -424,9 +454,9 @@ public class ChartFragment extends Fragment implements TrackDataListener {
|
||||
double cadence = Double.NaN;
|
||||
double power = Double.NaN;
|
||||
|
||||
if (tripStatisticsBuilder != null) {
|
||||
tripStatisticsBuilder.addLocation(location, minRecordingDistance);
|
||||
TripStatistics tripStatistics = tripStatisticsBuilder.getTripStatistics();
|
||||
if (tripStatisticsUpdater != null) {
|
||||
tripStatisticsUpdater.addLocation(location, minRecordingDistance);
|
||||
TripStatistics tripStatistics = tripStatisticsUpdater.getTripStatistics();
|
||||
if (chartByDistance) {
|
||||
double distance = tripStatistics.getTotalDistance() * UnitConversions.M_TO_KM;
|
||||
if (!metricUnits) {
|
||||
@@ -436,13 +466,13 @@ public class ChartFragment extends Fragment implements TrackDataListener {
|
||||
} else {
|
||||
timeOrDistance = tripStatistics.getTotalTime();
|
||||
}
|
||||
|
||||
elevation = tripStatisticsBuilder.getSmoothedElevation();
|
||||
|
||||
elevation = tripStatisticsUpdater.getSmoothedElevation();
|
||||
if (!metricUnits) {
|
||||
elevation *= UnitConversions.M_TO_FT;
|
||||
}
|
||||
|
||||
speed = tripStatisticsBuilder.getSmoothedSpeed() * UnitConversions.MS_TO_KMH;
|
||||
|
||||
speed = tripStatisticsUpdater.getSmoothedSpeed() * UnitConversions.MS_TO_KMH;
|
||||
if (!metricUnits) {
|
||||
speed *= UnitConversions.KM_TO_MI;
|
||||
}
|
||||
@@ -485,8 +515,8 @@ public class ChartFragment extends Fragment implements TrackDataListener {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void setTripStatisticsBuilder(long time) {
|
||||
tripStatisticsBuilder = new TripStatisticsBuilder(time);
|
||||
void setTripStatisticsUpdater(long time) {
|
||||
tripStatisticsUpdater = new TripStatisticsUpdater(time);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
|
||||
@@ -275,93 +275,104 @@ public class MapFragment extends Fragment
|
||||
|
||||
@Override
|
||||
public void onLocationStateChanged(LocationState state) {
|
||||
final String message;
|
||||
final 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);
|
||||
}
|
||||
}
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (message != null) {
|
||||
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(MapFragment.this);
|
||||
} else {
|
||||
messageTextView.setOnClickListener(null);
|
||||
}
|
||||
} else {
|
||||
messageTextView.setVisibility(View.GONE);
|
||||
if (isResumed()) {
|
||||
final String message;
|
||||
final 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);
|
||||
}
|
||||
}
|
||||
});
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (isResumed()) {
|
||||
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(MapFragment.this);
|
||||
} else {
|
||||
messageTextView.setOnClickListener(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(Location location) {
|
||||
currentLocation = location;
|
||||
updateCurrentLocation();
|
||||
if (isResumed()) {
|
||||
currentLocation = location;
|
||||
updateCurrentLocation();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onHeadingChanged(double heading) {
|
||||
if (mapOverlay.setHeading((float) heading)) {
|
||||
mapView.postInvalidate();
|
||||
if (isResumed()) {
|
||||
if (mapOverlay.setHeading((float) heading)) {
|
||||
mapView.postInvalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSelectedTrackChanged(final Track track) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
boolean hasTrack = track != null;
|
||||
mapOverlay.setTrackDrawingEnabled(hasTrack);
|
||||
if (isResumed()) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (isResumed()) {
|
||||
boolean hasTrack = track != null;
|
||||
mapOverlay.setTrackDrawingEnabled(hasTrack);
|
||||
|
||||
if (hasTrack) {
|
||||
synchronized (this) {
|
||||
/*
|
||||
* Synchronize to prevent race condition in changing markerTrackId
|
||||
* and markerId variables.
|
||||
*/
|
||||
currentSelectedTrackId = track.getId();
|
||||
updateMap(track);
|
||||
if (hasTrack) {
|
||||
synchronized (this) {
|
||||
/*
|
||||
* Synchronize to prevent race condition in changing
|
||||
* markerTrackId and markerId variables.
|
||||
*/
|
||||
currentSelectedTrackId = track.getId();
|
||||
updateMap(track);
|
||||
}
|
||||
mapOverlay.setShowEndMarker(!isSelectedTrackRecording());
|
||||
}
|
||||
mapView.invalidate();
|
||||
}
|
||||
mapOverlay.setShowEndMarker(!isSelectedTrackRecording());
|
||||
}
|
||||
mapView.invalidate();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -371,12 +382,16 @@ public class MapFragment extends Fragment
|
||||
|
||||
@Override
|
||||
public void clearTrackPoints() {
|
||||
mapOverlay.clearPoints();
|
||||
if (isResumed()) {
|
||||
mapOverlay.clearPoints();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSampledInTrackPoint(Location location) {
|
||||
mapOverlay.addLocation(location);
|
||||
if (isResumed()) {
|
||||
mapOverlay.addLocation(location);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -386,22 +401,28 @@ public class MapFragment extends Fragment
|
||||
|
||||
@Override
|
||||
public void onSegmentSplit(Location location) {
|
||||
mapOverlay.addSegmentSplit();
|
||||
if (isResumed()) {
|
||||
mapOverlay.addSegmentSplit();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNewTrackPointsDone() {
|
||||
mapView.postInvalidate();
|
||||
if (isResumed()) {
|
||||
mapView.postInvalidate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearWaypoints() {
|
||||
mapOverlay.clearWaypoints();
|
||||
if (isResumed()) {
|
||||
mapOverlay.clearWaypoints();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNewWaypoint(Waypoint waypoint) {
|
||||
if (waypoint != null && LocationUtils.isValidLocation(waypoint.getLocation())) {
|
||||
if (isResumed() && waypoint != null && LocationUtils.isValidLocation(waypoint.getLocation())) {
|
||||
// TODO: Optimize locking inside addWaypoint
|
||||
mapOverlay.addWaypoint(waypoint);
|
||||
}
|
||||
@@ -409,7 +430,9 @@ public class MapFragment extends Fragment
|
||||
|
||||
@Override
|
||||
public void onNewWaypointsDone() {
|
||||
mapView.postInvalidate();
|
||||
if (isResumed()) {
|
||||
mapView.postInvalidate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -29,6 +29,7 @@ import com.google.android.maps.mytracks.R;
|
||||
import android.location.Location;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
@@ -57,7 +58,8 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
// A runnable to update the total time field.
|
||||
private final Runnable updateTotalTime = new Runnable() {
|
||||
public void run() {
|
||||
if (isSelectedTrackRecording() && !isSelectedTrackPaused() && lastTripStatistics != null) {
|
||||
if (isResumed() && isSelectedTrackRecording() && !isSelectedTrackPaused()
|
||||
&& lastTripStatistics != null) {
|
||||
StatsUtils.setTotalTimeValue(getActivity(), System.currentTimeMillis()
|
||||
- lastTripStatistics.getStopTime() + lastTripStatistics.getTotalTime());
|
||||
}
|
||||
@@ -71,7 +73,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.d(TAG, "UI update thread started");
|
||||
while (true) {
|
||||
while (isResumed() && isSelectedTrackRecording()) {
|
||||
getActivity().runOnUiThread(updateTotalTime);
|
||||
try {
|
||||
Thread.sleep(1000L);
|
||||
@@ -93,7 +95,7 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
updateUi();
|
||||
updateUi(getActivity());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -114,12 +116,14 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
|
||||
@Override
|
||||
public void onLocationStateChanged(LocationState state) {
|
||||
if (isResumed() && (state == LocationState.DISABLED || state == LocationState.NO_FIX)) {
|
||||
if (isResumed() && state != LocationState.GOOD_FIX) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
lastLocation = null;
|
||||
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
|
||||
if (isResumed()) {
|
||||
lastLocation = null;
|
||||
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -127,12 +131,21 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(final Location location) {
|
||||
if (isResumed() && isSelectedTrackRecording()) {
|
||||
if (isResumed()) {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
lastLocation = location;
|
||||
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
|
||||
if (isResumed()) {
|
||||
if (isSelectedTrackRecording() && !isSelectedTrackPaused()) {
|
||||
lastLocation = location;
|
||||
StatsUtils.setLocationValues(getActivity(), location, true);
|
||||
} else {
|
||||
if (lastLocation != null) {
|
||||
lastLocation = null;
|
||||
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -146,14 +159,14 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
@Override
|
||||
public void onSelectedTrackChanged(Track track) {
|
||||
if (isResumed()) {
|
||||
boolean isRecording = isSelectedTrackRecording();
|
||||
if (uiUpdateThread == null && isRecording) {
|
||||
uiUpdateThread = new UiUpdateThread();
|
||||
uiUpdateThread.start();
|
||||
} else if (uiUpdateThread != null && !isRecording) {
|
||||
if (uiUpdateThread != null) {
|
||||
uiUpdateThread.interrupt();
|
||||
uiUpdateThread = null;
|
||||
}
|
||||
if (isSelectedTrackRecording()) {
|
||||
uiUpdateThread = new UiUpdateThread();
|
||||
uiUpdateThread.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,18 +176,10 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (track == null || track.getTripStatistics() == null) {
|
||||
lastLocation = null;
|
||||
lastTripStatistics = null;
|
||||
updateUi();
|
||||
return;
|
||||
if (isResumed()) {
|
||||
lastTripStatistics = track != null ? track.getTripStatistics() : null;
|
||||
updateUi(getActivity());
|
||||
}
|
||||
lastTripStatistics = track.getTripStatistics();
|
||||
|
||||
if (!isSelectedTrackRecording()) {
|
||||
lastLocation = null;
|
||||
}
|
||||
updateUi();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -226,7 +231,9 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateUi();
|
||||
if (isResumed()) {
|
||||
updateUi(getActivity());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -239,7 +246,9 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
getActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateUi();
|
||||
if (isResumed()) {
|
||||
updateUi(getActivity());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -258,11 +267,8 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
*/
|
||||
private synchronized void resumeTrackDataHub() {
|
||||
trackDataHub = ((TrackDetailActivity) getActivity()).getTrackDataHub();
|
||||
trackDataHub.registerTrackDataListener(this, EnumSet.of(
|
||||
TrackDataType.SELECTED_TRACK,
|
||||
TrackDataType.TRACKS_TABLE,
|
||||
TrackDataType.LOCATION,
|
||||
TrackDataType.PREFERENCE));
|
||||
trackDataHub.registerTrackDataListener(this, EnumSet.of(TrackDataType.SELECTED_TRACK,
|
||||
TrackDataType.TRACKS_TABLE, TrackDataType.LOCATION, TrackDataType.PREFERENCE));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -293,8 +299,8 @@ public class StatsFragment extends Fragment implements TrackDataListener {
|
||||
/**
|
||||
* Updates the UI.
|
||||
*/
|
||||
private void updateUi() {
|
||||
StatsUtils.setTripStatisticsValues(getActivity(), lastTripStatistics);
|
||||
StatsUtils.setLocationValues(getActivity(), lastLocation, true);
|
||||
private void updateUi(FragmentActivity activity) {
|
||||
StatsUtils.setTripStatisticsValues(activity, lastTripStatistics);
|
||||
StatsUtils.setLocationValues(activity, lastLocation, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ package com.google.android.apps.mytracks.io.file;
|
||||
import com.google.android.apps.mytracks.Constants;
|
||||
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
|
||||
import com.google.android.apps.mytracks.content.Track;
|
||||
import com.google.android.apps.mytracks.stats.TripStatisticsBuilder;
|
||||
import com.google.android.apps.mytracks.stats.TripStatisticsUpdater;
|
||||
import com.google.android.apps.mytracks.util.LocationUtils;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
import com.google.android.apps.mytracks.util.StringUtils;
|
||||
@@ -106,9 +106,9 @@ public class GpxImporter extends DefaultHandler {
|
||||
private Track track;
|
||||
|
||||
/**
|
||||
* Statistics builder for the current track.
|
||||
* Trip statistics updater for the current track.
|
||||
*/
|
||||
private TripStatisticsBuilder tripStatisticsBuilder;
|
||||
private TripStatisticsUpdater tripStatisticsUpdater;
|
||||
|
||||
/**
|
||||
* Buffer of locations to be bulk-inserted into the database.
|
||||
@@ -301,20 +301,20 @@ public class GpxImporter extends DefaultHandler {
|
||||
flushPoints();
|
||||
|
||||
// Calculate statistics for the imported track
|
||||
tripStatisticsBuilder.updateTime(lastLocation.getTime());
|
||||
tripStatisticsUpdater.updateTime(lastLocation.getTime());
|
||||
track.setStopId(getLastPointId());
|
||||
} else {
|
||||
tripStatisticsBuilder = new TripStatisticsBuilder(0L);
|
||||
tripStatisticsBuilder.updateTime(0L);
|
||||
tripStatisticsUpdater = new TripStatisticsUpdater(0L);
|
||||
tripStatisticsUpdater.updateTime(0L);
|
||||
}
|
||||
track.setTripStatistics(tripStatisticsBuilder.getTripStatistics());
|
||||
track.setTripStatistics(tripStatisticsUpdater.getTripStatistics());
|
||||
track.setNumberOfPoints(numberOfLocations);
|
||||
myTracksProviderUtils.updateTrack(track);
|
||||
tracksIds.add(track.getId());
|
||||
isCurrentTrackRollbackable = false;
|
||||
lastSegmentLocation = null;
|
||||
lastLocation = null;
|
||||
tripStatisticsBuilder = null;
|
||||
tripStatisticsUpdater = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -377,11 +377,11 @@ public class GpxImporter extends DefaultHandler {
|
||||
*/
|
||||
private void onTrackPointElementEnd() throws SAXException {
|
||||
if (LocationUtils.isValidLocation(location)) {
|
||||
if (tripStatisticsBuilder == null) {
|
||||
if (tripStatisticsUpdater == null) {
|
||||
// first point did not have a time, start stats builder without it
|
||||
tripStatisticsBuilder = new TripStatisticsBuilder(0);
|
||||
tripStatisticsUpdater = new TripStatisticsUpdater(0);
|
||||
}
|
||||
tripStatisticsBuilder.addLocation(location, PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT);
|
||||
tripStatisticsUpdater.addLocation(location, PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT);
|
||||
|
||||
// insert in db
|
||||
insertPoint(location);
|
||||
@@ -448,8 +448,8 @@ public class GpxImporter extends DefaultHandler {
|
||||
location.setTime(time);
|
||||
|
||||
// initialize start time with time of first track point
|
||||
if (tripStatisticsBuilder == null) {
|
||||
tripStatisticsBuilder = new TripStatisticsBuilder(time);
|
||||
if (tripStatisticsUpdater == null) {
|
||||
tripStatisticsUpdater = new TripStatisticsUpdater(time);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -506,7 +506,7 @@ public class GpxImporter extends DefaultHandler {
|
||||
if (numBufferedPointInserts <= 0) {
|
||||
return;
|
||||
}
|
||||
myTracksProviderUtils.bulkInsertTrackPoints(
|
||||
myTracksProviderUtils.bulkInsertTrackPoint(
|
||||
bufferedPointInserts, numBufferedPointInserts, track.getId());
|
||||
numBufferedPointInserts = 0;
|
||||
}
|
||||
@@ -528,7 +528,7 @@ public class GpxImporter extends DefaultHandler {
|
||||
*/
|
||||
private long getLastPointId() {
|
||||
flushPoints();
|
||||
return myTracksProviderUtils.getLastTrackLocationId(track.getId());
|
||||
return myTracksProviderUtils.getLastTrackPointId(track.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -168,7 +168,7 @@ public class SaveAsyncTask extends AsyncTask<Void, Integer, Boolean> {
|
||||
private Boolean saveAllTracks() {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = myTracksProviderUtils.getTracksCursor(null, null, TracksColumns._ID);
|
||||
cursor = myTracksProviderUtils.getTrackCursor(null, null, TracksColumns._ID);
|
||||
if (cursor == null) {
|
||||
messageId = R.string.sd_card_save_error_no_track;
|
||||
return false;
|
||||
|
||||
@@ -245,7 +245,7 @@ class TrackWriterImpl implements TrackWriter {
|
||||
// problem because we don't try to load them into objects all at the
|
||||
// same time.
|
||||
Cursor cursor = null;
|
||||
cursor = providerUtils.getWaypointsCursor(trackId, 0,
|
||||
cursor = providerUtils.getWaypointCursor(trackId, 0,
|
||||
Constants.MAX_LOADED_WAYPOINTS_POINTS);
|
||||
boolean hasWaypoints = false;
|
||||
if (cursor != null) {
|
||||
@@ -314,7 +314,7 @@ class TrackWriterImpl implements TrackWriter {
|
||||
};
|
||||
|
||||
TrackWriterLocationFactory locationFactory = new TrackWriterLocationFactory();
|
||||
LocationIterator it = providerUtils.getLocationIterator(track.getId(), 0, false,
|
||||
LocationIterator it = providerUtils.getTrackPointLocationIterator(track.getId(), 0, false,
|
||||
locationFactory);
|
||||
try {
|
||||
if (!it.hasNext()) {
|
||||
|
||||
+19
-19
@@ -10,7 +10,7 @@ import com.google.android.apps.mytracks.content.Track;
|
||||
import com.google.android.apps.mytracks.content.Waypoint;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.AbstractSendAsyncTask;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
|
||||
import com.google.android.apps.mytracks.stats.TripStatisticsBuilder;
|
||||
import com.google.android.apps.mytracks.stats.TripStatisticsUpdater;
|
||||
import com.google.android.apps.mytracks.util.ApiAdapterFactory;
|
||||
import com.google.android.apps.mytracks.util.LocationUtils;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
@@ -208,31 +208,31 @@ public class SendFusionTablesAsyncTask extends AbstractSendAsyncTask {
|
||||
* @return true if success.
|
||||
*/
|
||||
private boolean uploadAllTrackPoints(Track track) {
|
||||
Cursor locationsCursor = null;
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
locationsCursor = myTracksProviderUtils.getLocationsCursor(trackId, 0, -1, false);
|
||||
if (locationsCursor == null) {
|
||||
cursor = myTracksProviderUtils.getTrackPointCursor(trackId, 0, -1, false);
|
||||
if (cursor == null) {
|
||||
Log.d(TAG, "Location cursor is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
int locationsCount = locationsCursor.getCount();
|
||||
int count = cursor.getCount();
|
||||
List<Location> locations = new ArrayList<Location>(MAX_POINTS_PER_UPLOAD);
|
||||
Location lastLocation = null;
|
||||
|
||||
// For chart server, limit the number of elevation readings to 250.
|
||||
int elevationSamplingFrequency = Math.max(1, (int) (locationsCount / 250.0));
|
||||
int elevationSamplingFrequency = Math.max(1, (int) (count / 250.0));
|
||||
Vector<Double> distances = new Vector<Double>();
|
||||
Vector<Double> elevations = new Vector<Double>();
|
||||
TripStatisticsBuilder tripStatisticsBuilder = new TripStatisticsBuilder(
|
||||
TripStatisticsUpdater tripStatisticsUpdater = new TripStatisticsUpdater(
|
||||
track.getTripStatistics().getStartTime());
|
||||
int minRecordingDistance = PreferencesUtils.getInt(context,
|
||||
R.string.min_recording_distance_key, PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT);
|
||||
|
||||
for (int i = 0; i < locationsCount; i++) {
|
||||
locationsCursor.moveToPosition(i);
|
||||
for (int i = 0; i < count; i++) {
|
||||
cursor.moveToPosition(i);
|
||||
|
||||
Location location = myTracksProviderUtils.createLocation(locationsCursor);
|
||||
Location location = myTracksProviderUtils.createTrackPoint(cursor);
|
||||
locations.add(location);
|
||||
|
||||
if (i == 0) {
|
||||
@@ -244,10 +244,10 @@ public class SendFusionTablesAsyncTask extends AbstractSendAsyncTask {
|
||||
}
|
||||
}
|
||||
|
||||
tripStatisticsBuilder.addLocation(location, minRecordingDistance);
|
||||
tripStatisticsUpdater.addLocation(location, minRecordingDistance);
|
||||
if (i % elevationSamplingFrequency == 0) {
|
||||
distances.add(tripStatisticsBuilder.getTripStatistics().getTotalDistance());
|
||||
elevations.add(tripStatisticsBuilder.getSmoothedElevation());
|
||||
distances.add(tripStatisticsUpdater.getTripStatistics().getTotalDistance());
|
||||
elevations.add(tripStatisticsUpdater.getSmoothedElevation());
|
||||
}
|
||||
if (LocationUtils.isValidLocation(location)) {
|
||||
lastLocation = location;
|
||||
@@ -260,7 +260,7 @@ public class SendFusionTablesAsyncTask extends AbstractSendAsyncTask {
|
||||
Log.d(TAG, "Unable to upload points");
|
||||
return false;
|
||||
}
|
||||
updateProgress(readCount, locationsCount);
|
||||
updateProgress(readCount, count);
|
||||
locations.clear();
|
||||
}
|
||||
}
|
||||
@@ -273,8 +273,8 @@ public class SendFusionTablesAsyncTask extends AbstractSendAsyncTask {
|
||||
|
||||
// Create an end marker
|
||||
if (lastLocation != null) {
|
||||
distances.add(tripStatisticsBuilder.getTripStatistics().getTotalDistance());
|
||||
elevations.add(tripStatisticsBuilder.getSmoothedElevation());
|
||||
distances.add(tripStatisticsUpdater.getTripStatistics().getTotalDistance());
|
||||
elevations.add(tripStatisticsUpdater.getSmoothedElevation());
|
||||
DescriptionGenerator descriptionGenerator = new DescriptionGeneratorImpl(context);
|
||||
track.setDescription(
|
||||
descriptionGenerator.generateTrackDescription(track, distances, elevations, true));
|
||||
@@ -287,8 +287,8 @@ public class SendFusionTablesAsyncTask extends AbstractSendAsyncTask {
|
||||
|
||||
return true;
|
||||
} finally {
|
||||
if (locationsCursor != null) {
|
||||
locationsCursor.close();
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -328,7 +328,7 @@ public class SendFusionTablesAsyncTask extends AbstractSendAsyncTask {
|
||||
private boolean uploadWaypoints() {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = myTracksProviderUtils.getWaypointsCursor(
|
||||
cursor = myTracksProviderUtils.getWaypointCursor(
|
||||
trackId, 0, Constants.MAX_LOADED_WAYPOINTS_POINTS);
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
// This will skip the first waypoint (it carries the stats for the
|
||||
|
||||
@@ -28,7 +28,7 @@ import com.google.android.apps.mytracks.io.gdata.maps.MapsGDataConverter;
|
||||
import com.google.android.apps.mytracks.io.gdata.maps.XmlMapsGDataParserFactory;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.AbstractSendAsyncTask;
|
||||
import com.google.android.apps.mytracks.io.sendtogoogle.SendToGoogleUtils;
|
||||
import com.google.android.apps.mytracks.stats.TripStatisticsBuilder;
|
||||
import com.google.android.apps.mytracks.stats.TripStatisticsUpdater;
|
||||
import com.google.android.apps.mytracks.util.LocationUtils;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
import com.google.android.common.gdata.AndroidXmlParserFactory;
|
||||
@@ -251,31 +251,31 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask {
|
||||
*/
|
||||
@VisibleForTesting
|
||||
boolean uploadAllTrackPoints(Track track) {
|
||||
Cursor locationsCursor = null;
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
locationsCursor = myTracksProviderUtils.getLocationsCursor(trackId, 0, -1, false);
|
||||
if (locationsCursor == null) {
|
||||
cursor = myTracksProviderUtils.getTrackPointCursor(trackId, 0, -1, false);
|
||||
if (cursor == null) {
|
||||
Log.d(TAG, "Location cursor is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
int locationsCount = locationsCursor.getCount();
|
||||
int count = cursor.getCount();
|
||||
List<Location> locations = new ArrayList<Location>(MAX_POINTS_PER_UPLOAD);
|
||||
Location lastLocation = null;
|
||||
|
||||
// For chart server, limit the number of elevation readings to 250.
|
||||
int elevationSamplingFrequency = Math.max(1, (int) (locationsCount / 250.0));
|
||||
int elevationSamplingFrequency = Math.max(1, (int) (count / 250.0));
|
||||
Vector<Double> distances = new Vector<Double>();
|
||||
Vector<Double> elevations = new Vector<Double>();
|
||||
TripStatisticsBuilder tripStatisticsBuilder = new TripStatisticsBuilder(
|
||||
TripStatisticsUpdater tripStatisticsUpdater = new TripStatisticsUpdater(
|
||||
track.getTripStatistics().getStartTime());
|
||||
int minRecordingDistance = PreferencesUtils.getInt(context,
|
||||
R.string.min_recording_distance_key, PreferencesUtils.MIN_RECORDING_DISTANCE_DEFAULT);
|
||||
|
||||
for (int i = 0; i < locationsCount; i++) {
|
||||
locationsCursor.moveToPosition(i);
|
||||
for (int i = 0; i < count; i++) {
|
||||
cursor.moveToPosition(i);
|
||||
|
||||
Location location = myTracksProviderUtils.createLocation(locationsCursor);
|
||||
Location location = myTracksProviderUtils.createTrackPoint(cursor);
|
||||
locations.add(location);
|
||||
|
||||
if (i == 0) {
|
||||
@@ -287,10 +287,10 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask {
|
||||
}
|
||||
}
|
||||
|
||||
tripStatisticsBuilder.addLocation(location, minRecordingDistance);
|
||||
tripStatisticsUpdater.addLocation(location, minRecordingDistance);
|
||||
if (i % elevationSamplingFrequency == 0) {
|
||||
distances.add(tripStatisticsBuilder.getTripStatistics().getTotalDistance());
|
||||
elevations.add(tripStatisticsBuilder.getSmoothedElevation());
|
||||
distances.add(tripStatisticsUpdater.getTripStatistics().getTotalDistance());
|
||||
elevations.add(tripStatisticsUpdater.getSmoothedElevation());
|
||||
}
|
||||
if (LocationUtils.isValidLocation(location)) {
|
||||
lastLocation = location;
|
||||
@@ -303,7 +303,7 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask {
|
||||
Log.d(TAG, "Unable to upload points");
|
||||
return false;
|
||||
}
|
||||
updateProgress(readCount, locationsCount);
|
||||
updateProgress(readCount, count);
|
||||
locations.clear();
|
||||
}
|
||||
}
|
||||
@@ -316,8 +316,8 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask {
|
||||
|
||||
// Create an end marker
|
||||
if (lastLocation != null) {
|
||||
distances.add(tripStatisticsBuilder.getTripStatistics().getTotalDistance());
|
||||
elevations.add(tripStatisticsBuilder.getSmoothedElevation());
|
||||
distances.add(tripStatisticsUpdater.getTripStatistics().getTotalDistance());
|
||||
elevations.add(tripStatisticsUpdater.getSmoothedElevation());
|
||||
DescriptionGenerator descriptionGenerator = new DescriptionGeneratorImpl(context);
|
||||
track.setDescription(
|
||||
descriptionGenerator.generateTrackDescription(track, distances, elevations, true));
|
||||
@@ -329,8 +329,8 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask {
|
||||
}
|
||||
return true;
|
||||
} finally {
|
||||
if (locationsCursor != null) {
|
||||
locationsCursor.close();
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -436,7 +436,7 @@ public class SendMapsAsyncTask extends AbstractSendAsyncTask {
|
||||
boolean uploadWaypoints() {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
cursor = myTracksProviderUtils.getWaypointsCursor(
|
||||
cursor = myTracksProviderUtils.getWaypointCursor(
|
||||
trackId, 0, Constants.MAX_LOADED_WAYPOINTS_POINTS);
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
// This will skip the first waypoint (it carries the stats for the
|
||||
|
||||
@@ -36,7 +36,7 @@ import com.google.android.apps.mytracks.services.tasks.AnnouncementPeriodicTaskF
|
||||
import com.google.android.apps.mytracks.services.tasks.PeriodicTaskExecutor;
|
||||
import com.google.android.apps.mytracks.services.tasks.SplitPeriodicTaskFactory;
|
||||
import com.google.android.apps.mytracks.stats.TripStatistics;
|
||||
import com.google.android.apps.mytracks.stats.TripStatisticsBuilder;
|
||||
import com.google.android.apps.mytracks.stats.TripStatisticsUpdater;
|
||||
import com.google.android.apps.mytracks.util.IntentUtils;
|
||||
import com.google.android.apps.mytracks.util.LocationUtils;
|
||||
import com.google.android.apps.mytracks.util.PreferencesUtils;
|
||||
@@ -82,8 +82,8 @@ import java.util.concurrent.Executors;
|
||||
public class TrackRecordingService extends Service {
|
||||
|
||||
private static final String TAG = TrackRecordingService.class.getSimpleName();
|
||||
public static final int PAUSE_LATITUDE = 100;
|
||||
private static final int RESUME_LATITUDE = 200;
|
||||
public static final double PAUSE_LATITUDE = 100.0;
|
||||
private static final double RESUME_LATITUDE = 200.0;
|
||||
|
||||
// One second in milliseconds
|
||||
private static final long ONE_SECOND = 1000;
|
||||
@@ -110,8 +110,8 @@ public class TrackRecordingService extends Service {
|
||||
private long currentRecordingInterval;
|
||||
|
||||
// The following variables are set when recording:
|
||||
private TripStatisticsBuilder trackTripStatisticsBuilder;
|
||||
private TripStatisticsBuilder markerTripStatisticsBuilder;
|
||||
private TripStatisticsUpdater trackTripStatisticsUpdater;
|
||||
private TripStatisticsUpdater markerTripStatisticsUpdater;
|
||||
private WakeLock wakeLock;
|
||||
private SensorManager sensorManager;
|
||||
private Location lastLocation;
|
||||
@@ -397,10 +397,10 @@ public class TrackRecordingService extends Service {
|
||||
* Gets the trip statistics.
|
||||
*/
|
||||
public TripStatistics getTripStatistics() {
|
||||
if (trackTripStatisticsBuilder == null) {
|
||||
if (trackTripStatisticsUpdater == null) {
|
||||
return null;
|
||||
}
|
||||
return trackTripStatisticsBuilder.getTripStatistics();
|
||||
return trackTripStatisticsUpdater.getTripStatistics();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -419,23 +419,23 @@ public class TrackRecordingService extends Service {
|
||||
if (waypointCreationRequest.getName() != null) {
|
||||
name = waypointCreationRequest.getName();
|
||||
} else {
|
||||
int nextMarkerNumber = myTracksProviderUtils.getNextMarkerNumber(
|
||||
int nextWaypointNumber = myTracksProviderUtils.getNextWaypointNumber(
|
||||
recordingTrackId, isStatistics);
|
||||
if (nextMarkerNumber == -1) {
|
||||
nextMarkerNumber = 0;
|
||||
if (nextWaypointNumber == -1) {
|
||||
nextWaypointNumber = 0;
|
||||
}
|
||||
name = getString(
|
||||
isStatistics ? R.string.marker_split_name_format : R.string.marker_name_format,
|
||||
nextMarkerNumber);
|
||||
nextWaypointNumber);
|
||||
}
|
||||
|
||||
TripStatistics tripStatistics;
|
||||
String description;
|
||||
if (isStatistics) {
|
||||
long now = System.currentTimeMillis();
|
||||
markerTripStatisticsBuilder.updateTime(now);
|
||||
tripStatistics = markerTripStatisticsBuilder.getTripStatistics();
|
||||
markerTripStatisticsBuilder = new TripStatisticsBuilder(now);
|
||||
markerTripStatisticsUpdater.updateTime(now);
|
||||
tripStatistics = markerTripStatisticsUpdater.getTripStatistics();
|
||||
markerTripStatisticsUpdater = new TripStatisticsUpdater(now);
|
||||
description = new DescriptionGeneratorImpl(this).generateWaypointDescription(tripStatistics);
|
||||
} else {
|
||||
tripStatistics = null;
|
||||
@@ -452,10 +452,9 @@ public class TrackRecordingService extends Service {
|
||||
int type = isStatistics ? Waypoint.TYPE_STATISTICS : Waypoint.TYPE_WAYPOINT;
|
||||
long duration;
|
||||
double length;
|
||||
Location lastTrackLocation = myTracksProviderUtils.getLastTrackLocation(recordingTrackId);
|
||||
if (currentSegmentHasLocation && LocationUtils.isValidLocation(lastTrackLocation)
|
||||
&& trackTripStatisticsBuilder != null) {
|
||||
TripStatistics stats = trackTripStatisticsBuilder.getTripStatistics();
|
||||
Location location = getLastValidTrackPointInCurrentSegment(recordingTrackId);
|
||||
if (location != null && trackTripStatisticsUpdater != null) {
|
||||
TripStatistics stats = trackTripStatisticsUpdater.getTripStatistics();
|
||||
length = stats.getTotalDistance();
|
||||
duration = stats.getTotalTime();
|
||||
} else {
|
||||
@@ -463,14 +462,14 @@ public class TrackRecordingService extends Service {
|
||||
return -1L;
|
||||
}
|
||||
// For track statistics, make it an impossible location
|
||||
lastTrackLocation = new Location("");
|
||||
lastTrackLocation.setLatitude(100);
|
||||
lastTrackLocation.setLongitude(180);
|
||||
location = new Location("");
|
||||
location.setLatitude(100);
|
||||
location.setLongitude(180);
|
||||
length = 0;
|
||||
duration = 0;
|
||||
}
|
||||
Waypoint waypoint = new Waypoint(name, description, category, icon, recordingTrackId, type,
|
||||
length, duration, -1L, -1L, lastTrackLocation, tripStatistics);
|
||||
length, duration, -1L, -1L, location, tripStatistics);
|
||||
Uri uri = myTracksProviderUtils.insertWaypoint(waypoint);
|
||||
return Long.parseLong(uri.getLastPathSegment());
|
||||
}
|
||||
@@ -557,8 +556,8 @@ public class TrackRecordingService extends Service {
|
||||
return -1L;
|
||||
}
|
||||
long now = System.currentTimeMillis();
|
||||
trackTripStatisticsBuilder = new TripStatisticsBuilder(now);
|
||||
markerTripStatisticsBuilder = new TripStatisticsBuilder(now);
|
||||
trackTripStatisticsUpdater = new TripStatisticsUpdater(now);
|
||||
markerTripStatisticsUpdater = new TripStatisticsUpdater(now);
|
||||
|
||||
// Insert a track
|
||||
Track track = new Track();
|
||||
@@ -574,7 +573,7 @@ public class TrackRecordingService extends Service {
|
||||
track.setName(TrackNameUtils.getTrackName(this, trackId, now, null));
|
||||
track.setCategory(PreferencesUtils.getString(
|
||||
this, R.string.default_activity_key, PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT));
|
||||
track.setTripStatistics(trackTripStatisticsBuilder.getTripStatistics());
|
||||
track.setTripStatistics(trackTripStatisticsUpdater.getTripStatistics());
|
||||
myTracksProviderUtils.updateTrack(track);
|
||||
insertWaypoint(WaypointCreationRequest.DEFAULT_START_TRACK);
|
||||
|
||||
@@ -591,7 +590,7 @@ public class TrackRecordingService extends Service {
|
||||
Log.d(TAG, "Restarting track: " + track.getId());
|
||||
|
||||
TripStatistics tripStatistics = track.getTripStatistics();
|
||||
trackTripStatisticsBuilder = new TripStatisticsBuilder(tripStatistics.getStartTime());
|
||||
trackTripStatisticsUpdater = new TripStatisticsUpdater(tripStatistics.getStartTime());
|
||||
|
||||
long markerStartTime;
|
||||
Waypoint waypoint = myTracksProviderUtils.getLastStatisticsWaypoint(recordingTrackId);
|
||||
@@ -600,22 +599,22 @@ public class TrackRecordingService extends Service {
|
||||
} else {
|
||||
markerStartTime = tripStatistics.getStartTime();
|
||||
}
|
||||
markerTripStatisticsBuilder = new TripStatisticsBuilder(markerStartTime);
|
||||
markerTripStatisticsUpdater = new TripStatisticsUpdater(markerStartTime);
|
||||
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
// TODO: how to handle very long track.
|
||||
cursor = myTracksProviderUtils.getLocationsCursor(
|
||||
cursor = myTracksProviderUtils.getTrackPointCursor(
|
||||
recordingTrackId, -1, Constants.MAX_LOADED_TRACK_POINTS, true);
|
||||
if (cursor == null) {
|
||||
Log.e(TAG, "Cursor is null.");
|
||||
} else {
|
||||
if (cursor.moveToLast()) {
|
||||
do {
|
||||
Location location = myTracksProviderUtils.createLocation(cursor);
|
||||
trackTripStatisticsBuilder.addLocation(location, minRecordingDistance);
|
||||
Location location = myTracksProviderUtils.createTrackPoint(cursor);
|
||||
trackTripStatisticsUpdater.addLocation(location, minRecordingDistance);
|
||||
if (location.getTime() > markerStartTime) {
|
||||
markerTripStatisticsBuilder.addLocation(location, minRecordingDistance);
|
||||
markerTripStatisticsUpdater.addLocation(location, minRecordingDistance);
|
||||
}
|
||||
} while (cursor.moveToPrevious());
|
||||
}
|
||||
@@ -701,14 +700,26 @@ public class TrackRecordingService extends Service {
|
||||
// Update database
|
||||
Track track = myTracksProviderUtils.getTrack(trackId);
|
||||
if (track != null) {
|
||||
insertLocation(track, lastLocation, myTracksProviderUtils.getLastTrackLocation(trackId));
|
||||
updateRecordingTrack(track, myTracksProviderUtils.getLastTrackLocationId(trackId), false);
|
||||
insertLocation(track, lastLocation, getLastValidTrackPointInCurrentSegment(trackId));
|
||||
updateRecordingTrack(track, myTracksProviderUtils.getLastTrackPointId(trackId), false);
|
||||
}
|
||||
|
||||
endRecording(true, trackId);
|
||||
stopSelf();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the last valid track point in the current segment. Returns null if not available.
|
||||
*
|
||||
* @param trackId the track id
|
||||
*/
|
||||
private Location getLastValidTrackPointInCurrentSegment(long trackId) {
|
||||
if (!currentSegmentHasLocation) {
|
||||
return null;
|
||||
}
|
||||
return myTracksProviderUtils.getLastValidTrackPoint(trackId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pauses the current track.
|
||||
*/
|
||||
@@ -725,8 +736,7 @@ public class TrackRecordingService extends Service {
|
||||
// Update database
|
||||
Track track = myTracksProviderUtils.getTrack(recordingTrackId);
|
||||
if (track != null) {
|
||||
insertLocation(
|
||||
track, lastLocation, myTracksProviderUtils.getLastTrackLocation(track.getId()));
|
||||
insertLocation(track, lastLocation, getLastValidTrackPointInCurrentSegment(track.getId()));
|
||||
|
||||
Location pause = new Location(LocationManager.GPS_PROVIDER);
|
||||
pause.setLongitude(0);
|
||||
@@ -809,9 +819,8 @@ public class TrackRecordingService extends Service {
|
||||
return;
|
||||
}
|
||||
|
||||
Location lastTrackLocation = myTracksProviderUtils.getLastTrackLocation(track.getId());
|
||||
long idleTime = currentSegmentHasLocation && LocationUtils.isValidLocation(lastTrackLocation) ?
|
||||
location.getTime() - lastTrackLocation.getTime()
|
||||
Location lastValidTrackPoint = getLastValidTrackPointInCurrentSegment(track.getId());
|
||||
long idleTime = lastValidTrackPoint != null ? location.getTime() - lastValidTrackPoint.getTime()
|
||||
: 0L;
|
||||
locationListenerPolicy.updateIdleTime(idleTime);
|
||||
if (currentRecordingInterval != locationListenerPolicy.getDesiredPollingInterval()) {
|
||||
@@ -825,41 +834,41 @@ public class TrackRecordingService extends Service {
|
||||
|
||||
// Always insert the first segment location
|
||||
if (!currentSegmentHasLocation) {
|
||||
insertLocation(track, location, lastTrackLocation);
|
||||
insertLocation(track, location, null);
|
||||
currentSegmentHasLocation = true;
|
||||
lastLocation = location;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!LocationUtils.isValidLocation(lastTrackLocation)) {
|
||||
if (!LocationUtils.isValidLocation(lastValidTrackPoint)) {
|
||||
/*
|
||||
* Should not happen. The current segment should have a location. Just
|
||||
* insert the current location.
|
||||
*/
|
||||
insertLocation(track, location, lastTrackLocation);
|
||||
insertLocation(track, location, null);
|
||||
lastLocation = location;
|
||||
return;
|
||||
}
|
||||
|
||||
double distanceToLastTrackLocation = location.distanceTo(lastTrackLocation);
|
||||
double distanceToLastTrackLocation = location.distanceTo(lastValidTrackPoint);
|
||||
if (distanceToLastTrackLocation < minRecordingDistance && sensorDataSet == null) {
|
||||
Log.d(TAG, "Not recording location due to min recording distance.");
|
||||
} else if (distanceToLastTrackLocation > maxRecordingDistance) {
|
||||
insertLocation(track, lastLocation, lastTrackLocation);
|
||||
insertLocation(track, lastLocation, lastValidTrackPoint);
|
||||
Location pause = new Location(LocationManager.GPS_PROVIDER);
|
||||
pause.setLongitude(0);
|
||||
pause.setLatitude(PAUSE_LATITUDE);
|
||||
pause.setTime(lastLocation.getTime());
|
||||
insertLocation(track, pause, null);
|
||||
|
||||
insertLocation(track, location, lastTrackLocation);
|
||||
insertLocation(track, location, null);
|
||||
} else {
|
||||
/*
|
||||
* (distanceToLastTrackLocation >= minRecordingDistance ||
|
||||
* hasSensorData) && distanceToLastTrackLocation <= maxRecordingDistance
|
||||
*/
|
||||
insertLocation(track, lastLocation, lastTrackLocation);
|
||||
insertLocation(track, location, lastTrackLocation);
|
||||
insertLocation(track, lastLocation, lastValidTrackPoint);
|
||||
insertLocation(track, location, null);
|
||||
}
|
||||
lastLocation = location;
|
||||
} catch (Error e) {
|
||||
@@ -871,21 +880,29 @@ public class TrackRecordingService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
private void insertLocation(Track track, Location location, Location lastRecordedLocation) {
|
||||
/**
|
||||
* Inserts a location.
|
||||
*
|
||||
* @param track the track
|
||||
* @param location the location
|
||||
* @param lastValidTrackPoint the last valid track point, can be null
|
||||
*/
|
||||
private void insertLocation(Track track, Location location, Location lastValidTrackPoint) {
|
||||
if (location == null) {
|
||||
Log.w(TAG, "Ignore insertLocation. loation is null.");
|
||||
return;
|
||||
}
|
||||
if (location.equals(lastRecordedLocation)) {
|
||||
Log.w(TAG, "Ignore insertLocation. loation same as last recorded location.");
|
||||
// Do not insert if inserted already
|
||||
if (lastValidTrackPoint != null && lastValidTrackPoint.getTime() == location.getTime()) {
|
||||
Log.w(TAG, "Ignore insertLocation. location time same as last valid track point time.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Uri uri = myTracksProviderUtils.insertTrackPoint(location, track.getId());
|
||||
long trackPointId = Long.parseLong(uri.getLastPathSegment());
|
||||
trackTripStatisticsBuilder.addLocation(location, minRecordingDistance);
|
||||
markerTripStatisticsBuilder.addLocation(location, minRecordingDistance);
|
||||
trackTripStatisticsUpdater.addLocation(location, minRecordingDistance);
|
||||
markerTripStatisticsUpdater.addLocation(location, minRecordingDistance);
|
||||
updateRecordingTrack(track, trackPointId, LocationUtils.isValidLocation(location));
|
||||
} catch (SQLiteException e) {
|
||||
/*
|
||||
@@ -911,8 +928,8 @@ public class TrackRecordingService extends Service {
|
||||
track.setNumberOfPoints(track.getNumberOfPoints() + 1);
|
||||
}
|
||||
|
||||
trackTripStatisticsBuilder.updateTime(System.currentTimeMillis());
|
||||
track.setTripStatistics(trackTripStatisticsBuilder.getTripStatistics());
|
||||
trackTripStatisticsUpdater.updateTime(System.currentTimeMillis());
|
||||
track.setTripStatistics(trackTripStatisticsUpdater.getTripStatistics());
|
||||
myTracksProviderUtils.updateTrack(track);
|
||||
}
|
||||
|
||||
|
||||
+8
-4
@@ -27,14 +27,14 @@ import android.location.Location;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* Builder for {@link TripStatistics}. For keeping track statistics as new
|
||||
* Updater for {@link TripStatistics}. For updating track trip statistics as new
|
||||
* locations are added. Note that some of the locations represent pause/resume
|
||||
* separator.
|
||||
*
|
||||
* @author Sandor Dornbush
|
||||
* @author Rodrigo Damazio
|
||||
*/
|
||||
public class TripStatisticsBuilder {
|
||||
public class TripStatisticsUpdater {
|
||||
|
||||
// The track's trip statistics
|
||||
private final TripStatistics tripStatistics;
|
||||
@@ -62,11 +62,11 @@ public class TripStatisticsBuilder {
|
||||
private final DoubleBuffer gradeBuffer = new DoubleBuffer(Constants.GRADE_SMOOTHING_FACTOR);
|
||||
|
||||
/**
|
||||
* Creates a new trip statistics builder.
|
||||
* Creates a new trip statistics updater.
|
||||
*
|
||||
* @param startTime the start time
|
||||
*/
|
||||
public TripStatisticsBuilder(long startTime) {
|
||||
public TripStatisticsUpdater(long startTime) {
|
||||
tripStatistics = init(startTime);
|
||||
currentSegment = init(startTime);
|
||||
}
|
||||
@@ -96,6 +96,10 @@ public class TripStatisticsBuilder {
|
||||
if (!LocationUtils.isValidLocation(location)) {
|
||||
updateTime(location.getTime());
|
||||
if (location.getLatitude() == TrackRecordingService.PAUSE_LATITUDE) {
|
||||
if (lastLocation != null && lastMovingLocation != null
|
||||
&& lastLocation != lastMovingLocation) {
|
||||
currentSegment.addTotalDistance(lastMovingLocation.distanceTo(lastLocation));
|
||||
}
|
||||
tripStatistics.merge(currentSegment);
|
||||
}
|
||||
currentSegment = init(location.getTime());
|
||||
Reference in New Issue
Block a user