Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions app/src/main/java/net/osmtracker/activity/DisplayTrackMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.osmtracker.R;
import net.osmtracker.db.TrackContentProvider;
import net.osmtracker.overlay.WayPointsOverlay;
import net.osmtracker.overlay.Polylines;

import org.osmdroid.api.IMapController;
import org.osmdroid.config.Configuration;
Expand All @@ -33,7 +34,6 @@
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.CustomZoomButtonsController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Polyline;
import org.osmdroid.views.overlay.ScaleBarOverlay;
import org.osmdroid.views.overlay.mylocation.SimpleLocationOverlay;

Expand Down Expand Up @@ -118,7 +118,7 @@ public class DisplayTrackMap extends Activity {
/**
* OSM view overlay that displays current path
*/
private Polyline polyline;
private Polylines polylines;

/**
* OSM view overlay that displays waypoints
Expand Down Expand Up @@ -158,6 +158,11 @@ public class DisplayTrackMap extends Activity {
*/
private Integer lastTrackPointIdProcessed = null;

/**
* The id of the last segment
*/
private int prevSegmentId=-1;

/**
* Observes changes on track points
*/
Expand Down Expand Up @@ -303,6 +308,7 @@ private void resumeActivity() {
// This ensures that all waypoints for the track will be reloaded
// from the database to populate the path layout
lastTrackPointIdProcessed = null;
prevSegmentId = -1;

// Reload path
pathChanged();
Expand All @@ -321,7 +327,7 @@ protected void onPause() {
getContentResolver().unregisterContentObserver(trackpointContentObserver);

// Clear the points list.
polyline.setPoints(new ArrayList<>());
polylines.clear();

super.onPause();
}
Expand Down Expand Up @@ -387,12 +393,8 @@ private void createOverlays() {
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);

// set with to hopefully DPI independent 0.5mm
polyline = new Polyline();
Paint paint = polyline.getOutlinePaint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth((float) (metrics.densityDpi / 25.4 / 2));
osmView.getOverlayManager().add(polyline);

polylines = new Polylines(Color.BLUE, (float)(metrics.densityDpi / 25.4 / 2), osmView);

myLocationOverlay = new SimpleLocationOverlay(this);
osmView.getOverlays().add(myLocationOverlay);

Expand Down Expand Up @@ -439,7 +441,7 @@ private void pathChanged() {

// Projection: The columns to retrieve. Here, we want the latitude,
// longitude and primary key only
String[] projection = {TrackContentProvider.Schema.COL_LATITUDE, TrackContentProvider.Schema.COL_LONGITUDE, TrackContentProvider.Schema.COL_ID};
String[] projection = {TrackContentProvider.Schema.COL_LATITUDE, TrackContentProvider.Schema.COL_LONGITUDE, TrackContentProvider.Schema.COL_ID, TrackContentProvider.Schema.COL_SEG_ID };
// Selection: The where clause to use
String selection = null;
// SelectionArgs: The parameter replacements to use for the '?' in the selection
Expand Down Expand Up @@ -470,13 +472,20 @@ private void pathChanged() {
int primaryKeyColumnIndex = c.getColumnIndex(TrackContentProvider.Schema.COL_ID);
int latitudeColumnIndex = c.getColumnIndex(TrackContentProvider.Schema.COL_LATITUDE);
int longitudeColumnIndex = c.getColumnIndex(TrackContentProvider.Schema.COL_LONGITUDE);
int segmentIdColumnIndex = c.getColumnIndex(TrackContentProvider.Schema.COL_SEG_ID);

// Add each new point to the track
while (!c.isAfterLast()) {
lastLat = c.getDouble(latitudeColumnIndex);
lastLon = c.getDouble(longitudeColumnIndex);
lastTrackPointIdProcessed = c.getInt(primaryKeyColumnIndex);
polyline.addPoint(new GeoPoint(lastLat, lastLon));
int segmentId = c.getInt(segmentIdColumnIndex);
if(segmentId != prevSegmentId) {
polylines.nextSegment();
}
prevSegmentId = segmentId;

polylines.addPoint(new GeoPoint(lastLat, lastLon));
if (doInitialBoundsCalc) {
if (lastLat < minLat) minLat = lastLat;
if (lastLon < minLon) minLon = lastLon;
Expand Down
26 changes: 25 additions & 1 deletion app/src/main/java/net/osmtracker/db/DataHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import net.osmtracker.db.model.TrackPoint;
import net.osmtracker.db.model.WayPoint;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -145,8 +147,10 @@ public DataHelper(Context c) {
* ignored if azimuth is invalid.
* @param pressure
* atmospheric pressure
* @param segId
* Id of the segment
*/
public void track(long trackId, Location location, float azimuth, int accuracy, float pressure) {
public void track(long trackId, Location location, float azimuth, int accuracy, float pressure, long segId) {
Log.v(TAG, "Tracking (trackId=" + trackId + ") location: " + location + " azimuth: " + azimuth + ", accuracy: " + accuracy);
ContentValues values = new ContentValues();
values.put(TrackContentProvider.Schema.COL_TRACK_ID, trackId);
Expand Down Expand Up @@ -180,6 +184,8 @@ public void track(long trackId, Location location, float azimuth, int accuracy,
values.put(TrackContentProvider.Schema.COL_ATMOSPHERIC_PRESSURE, pressure);
}

values.put(TrackContentProvider.Schema.COL_SEG_ID, segId);

Uri trackUri = ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK, trackId);
contentResolver.insert(Uri.withAppendedPath(trackUri, TrackContentProvider.Schema.TBL_TRACKPOINT + "s"), values);
}
Expand Down Expand Up @@ -404,6 +410,24 @@ public static long getActiveTrackId(ContentResolver cr) {
return currentTrackId;
}

/**
* Find the segment ID for a track
* @param trackId Id of the track
* @param cr {@link ContentResolver} for query
* @return the segment ID for the track, or 0 if not found
*/
public static long getSegmentIdFor(long trackId, @NotNull ContentResolver cr) {
Cursor ca = cr.query(ContentUris.withAppendedId(TrackContentProvider.CONTENT_URI_TRACK,
trackId),null, null, null, null);

if (! ca.moveToFirst()) {
Log.v(TAG, "Track " + trackId + " not found");
return 0; // <--- Early return ---
}

return Track.build(trackId, ca, cr, true).getMaxSegId();
}

/**
* Change the name of this track.
* @param trackId Id of the track
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/net/osmtracker/db/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public class DatabaseHelper extends SQLiteOpenHelper {
+ TrackContentProvider.Schema.COL_TIMESTAMP + " long not null,"
+ TrackContentProvider.Schema.COL_COMPASS + " double null,"
+ TrackContentProvider.Schema.COL_COMPASS_ACCURACY + " integer null,"
+ TrackContentProvider.Schema.COL_ATMOSPHERIC_PRESSURE + " double null" + ")";
+ TrackContentProvider.Schema.COL_ATMOSPHERIC_PRESSURE + " double null,"
+ TrackContentProvider.Schema.COL_SEG_ID + " integer not null default 0"
+ ")";

/**
* SQL for creating index TRACKPOINT_idx (track id)
Expand Down Expand Up @@ -140,9 +142,10 @@ public class DatabaseHelper extends SQLiteOpenHelper {
* TBL_WAYPOINT.COL_COMPASS and TBL_WAYPOINT.COL_COMPASS_ACCURACY
* v17: add TBL_TRACKPOINT.COL_ATMOSPHERIC_PRESSURE and TBL_WAYPOINT.COL_ATMOSPHERIC_PRESSURE
* v18: add TBL_NOTE
* v19: add TBL_TRACKPOINT.COL_SEG_ID for track segments support
*</pre>
*/
private static final int DB_VERSION = 18;
private static final int DB_VERSION = 19;

private Context context;

Expand Down Expand Up @@ -202,6 +205,8 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("alter table " + TrackContentProvider.Schema.TBL_WAYPOINT + " add column " + TrackContentProvider.Schema.COL_ATMOSPHERIC_PRESSURE + " double null");
case 17:
db.execSQL(SQL_CREATE_TABLE_NOTE);
case 18:
db.execSQL("alter table "+TrackContentProvider.Schema.TBL_TRACKPOINT + " add column " + TrackContentProvider.Schema.COL_SEG_ID + " integer default 0");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public class TrackContentProvider extends ContentProvider {
"WHERE " + Schema.TBL_NOTE + "." + Schema.COL_TRACK_ID +" " +
"= " + Schema.TBL_TRACK + "." + Schema.COL_ID +") " +
"as " + Schema.COL_NOTE_COUNT,
"(SELECT max("+Schema.TBL_TRACKPOINT+"."+Schema.COL_SEG_ID+") " +
"FROM "+Schema.TBL_TRACKPOINT+" " +
"WHERE "+Schema.TBL_TRACKPOINT+"."+Schema.COL_TRACK_ID+" " +
"= " + Schema.TBL_TRACK + "." + Schema.COL_ID + ") " +
"as " + Schema.COL_SEG_ID_MAX
};

/**
Expand Down Expand Up @@ -593,11 +598,13 @@ public static final class Schema {
public static final String COL_COMPASS = "compass_heading";
public static final String COL_COMPASS_ACCURACY = "compass_accuracy";
public static final String COL_ATMOSPHERIC_PRESSURE = "atmospheric_pressure";

public static final String COL_SEG_ID = "segment_id";

// virtual colums that are used in some sqls but dont exist in database
public static final String COL_TRACKPOINT_COUNT = "tp_count";
public static final String COL_WAYPOINT_COUNT = "wp_count";
public static final String COL_NOTE_COUNT = "note_count";
public static final String COL_SEG_ID_MAX = "segment_id_max";

// Codes for UriMatcher
public static final int URI_CODE_TRACK = 3;
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/net/osmtracker/db/model/Track.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static OSMVisibility fromPosition(int position) {
private int tpCount, wpCount, noteCount;
private long trackDate;
private long trackId;
private long maxSegId;

private Long startDate=null, endDate=null;
private Float startLat=null, startLong=null, endLat=null, endLong=null;
Expand Down Expand Up @@ -95,6 +96,8 @@ public static Track build(final long trackId, Cursor tc, ContentResolver cr, boo

out.noteCount = tc.getInt(tc.getColumnIndex(TrackContentProvider.Schema.COL_NOTE_COUNT));

out.maxSegId = tc.getLong(tc.getColumnIndex(TrackContentProvider.Schema.COL_SEG_ID_MAX));

if(withExtraInformation){
out.readExtraInformation();
}
Expand Down Expand Up @@ -193,6 +196,10 @@ public Integer getWpCount() {
return wpCount;
}

public long getMaxSegId() {
return maxSegId;
}

public Integer getTpCount() {
return tpCount;
}
Expand Down
15 changes: 12 additions & 3 deletions app/src/main/java/net/osmtracker/gpx/ExportTrackTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ protected void exportTrackAsGpx(long trackId) throws ExportTrackException {
* @param target Target GPX file
* @throws IOException
*/
private void writeGpxFile(String trackName, String tags, String track_description, Cursor cTrackPoints, Cursor cWayPoints, File target) throws IOException {
protected void writeGpxFile(String trackName, String tags, String track_description, Cursor cTrackPoints, Cursor cWayPoints, File target) throws IOException {

String accuracyOutput = PreferenceManager.getDefaultSharedPreferences(context).getString(
OSMTracker.Preferences.KEY_OUTPUT_ACCURACY,
Expand Down Expand Up @@ -329,7 +329,7 @@ private void writeGpxFile(String trackName, String tags, String track_descriptio
* @param compass Indicates if and how to write compass heading to the GPX ('none', 'comment', 'extension')
* @throws IOException
*/
private void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fillHDOP, String compass) throws IOException {
protected void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fillHDOP, String compass) throws IOException {
// Update dialog every 1%
int dialogUpdateThreshold = c.getCount() / 100;
if (dialogUpdateThreshold == 0) {
Expand All @@ -348,8 +348,17 @@ private void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fil
fw.write("\t\t" + "<trkseg>" + "\n");

int i=0;
int prevSegId=-1;
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext(),i++) {
StringBuffer out = new StringBuffer();

int segId = c.getInt(c.getColumnIndex(TrackContentProvider.Schema.COL_SEG_ID));
if(prevSegId != -1 && segId != prevSegId) {
fw.write("\t\t" + "</trkseg>" + "\n");
fw.write("\t\t" + "<trkseg>" + "\n");
}
prevSegId = segId;

out.append("\t\t\t" + "<trkpt lat=\""
+ c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_LATITUDE)) + "\" "
+ "lon=\"" + c.getDouble(c.getColumnIndex(TrackContentProvider.Schema.COL_LONGITUDE)) + "\">" + "\n");
Expand Down Expand Up @@ -411,7 +420,7 @@ private void writeTrackPoints(String trackName, Writer fw, Cursor c, boolean fil
* @param compass Indicates if and how to write compass heading to the GPX ('none', 'comment', 'extension')
* @throws IOException
*/
private void writeWayPoints(Writer fw, Cursor c, String accuracyInfo, boolean fillHDOP, String compass) throws IOException {
protected void writeWayPoints(Writer fw, Cursor c, String accuracyInfo, boolean fillHDOP, String compass) throws IOException {

// Update dialog every 1%
int dialogUpdateThreshold = c.getCount() / 100;
Expand Down
61 changes: 61 additions & 0 deletions app/src/main/java/net/osmtracker/overlay/Polylines.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.osmtracker.overlay;

import java.util.ArrayList;
import java.util.List;

import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Polyline;

import android.graphics.Paint;

/**
* Collection of Polylines, useful to draw interrupted paths
*/
public class Polylines {
private int color;
private float width;
private MapView osmView;
private boolean havePoint;

private int curIdx=0;

private List<Polyline> polylines = new ArrayList<Polyline>();

private void addPolyline() {
Polyline polyline = new Polyline();
Paint paint = polyline.getOutlinePaint();
paint.setColor(color);
paint.setStrokeWidth(width);

polylines.add(polyline);
osmView.getOverlayManager().add(polyline);
}

public void clear() {
for(Polyline polyline : polylines)
polyline.setPoints(new ArrayList<>());
curIdx=0;
}

public Polylines(int color, float width, MapView osmView) {
this.color=color;
this.width=width;
this.osmView = osmView;
addPolyline();
havePoint=false;
}

public void addPoint(GeoPoint gp) {
if(curIdx >= polylines.size())
addPolyline();
polylines.get(curIdx).addPoint(gp);
havePoint=true;
}

public void nextSegment() {
if(havePoint)
curIdx++;
havePoint=false;
}
}
12 changes: 9 additions & 3 deletions app/src/main/java/net/osmtracker/service/gps/GPSLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public class GPSLogger extends Service implements LocationListener {
*/
private long currentTrackId = -1;

/**
* Current Segment ID
*/
private long currentSegmentId = -1;

/**
* the timestamp of the last GPS fix we used
*/
Expand Down Expand Up @@ -130,7 +135,7 @@ public void onReceive(Context context, Intent intent) {
dataHelper.wayPoint(trackId, lastLocation, name, link, uuid, sensorListener.getAzimuth(), sensorListener.getAccuracy(), pressureListener.getPressure());

// If there is a waypoint in the track, there should also be a trackpoint
dataHelper.track(currentTrackId, lastLocation, sensorListener.getAzimuth(), sensorListener.getAccuracy(), pressureListener.getPressure());
dataHelper.track(currentTrackId, lastLocation, sensorListener.getAzimuth(), sensorListener.getAccuracy(), pressureListener.getPressure(), currentSegmentId);
}
}
}
Expand Down Expand Up @@ -318,7 +323,8 @@ public void onDestroy() {
*/
private void startTracking(long trackId) {
currentTrackId = trackId;
Log.v(TAG, "Starting track logging for track #" + trackId);
currentSegmentId = DataHelper.getSegmentIdFor(trackId, getContentResolver()) + 1;
Log.v(TAG, "Starting track logging for track #" + trackId + " segment #" +"/" + currentSegmentId);
// Refresh notification with correct Track ID
NotificationManager nmgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nmgr.notify(NOTIFICATION_ID, getNotification());
Expand Down Expand Up @@ -347,7 +353,7 @@ public void onLocationChanged(Location location) {
lastLocation = location;

if (isTracking) {
dataHelper.track(currentTrackId, location, sensorListener.getAzimuth(), sensorListener.getAccuracy(), pressureListener.getPressure());
dataHelper.track(currentTrackId, location, sensorListener.getAzimuth(), sensorListener.getAccuracy(), pressureListener.getPressure(), currentSegmentId);
}
}
}
Expand Down
Loading