Merge pull request #10689 from osmandapp/fix_waypoints_currently_recording

Fix saving waypoints currently recording
This commit is contained in:
Vitaliy 2021-02-01 01:04:13 +02:00 committed by GitHub
commit 6f32c8cf0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -40,7 +40,7 @@ import java.util.Map;
public class SavingTrackHelper extends SQLiteOpenHelper { public class SavingTrackHelper extends SQLiteOpenHelper {
public final static String DATABASE_NAME = "tracks"; //$NON-NLS-1$ public final static String DATABASE_NAME = "tracks"; //$NON-NLS-1$
public final static int DATABASE_VERSION = 6; public final static int DATABASE_VERSION = 7;
public final static String TRACK_NAME = "track"; //$NON-NLS-1$ public final static String TRACK_NAME = "track"; //$NON-NLS-1$
public final static String TRACK_COL_DATE = "date"; //$NON-NLS-1$ public final static String TRACK_COL_DATE = "date"; //$NON-NLS-1$
@ -59,6 +59,8 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
public final static String POINT_COL_CATEGORY = "category"; //$NON-NLS-1$ public final static String POINT_COL_CATEGORY = "category"; //$NON-NLS-1$
public final static String POINT_COL_DESCRIPTION = "description"; //$NON-NLS-1$ public final static String POINT_COL_DESCRIPTION = "description"; //$NON-NLS-1$
public final static String POINT_COL_COLOR = "color"; //$NON-NLS-1$ public final static String POINT_COL_COLOR = "color"; //$NON-NLS-1$
public final static String POINT_COL_ICON = "icon"; //$NON-NLS-1$
public final static String POINT_COL_BACKGROUND = "background"; //$NON-NLS-1$
public final static float NO_HEADING = -1.0f; public final static float NO_HEADING = -1.0f;
@ -77,7 +79,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
private int points; private int points;
private int trkPoints = 0; private int trkPoints = 0;
public SavingTrackHelper(OsmandApplication ctx){ public SavingTrackHelper(OsmandApplication ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION); super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
this.ctx = ctx; this.ctx = ctx;
this.currentTrack = new SelectedGpxFile(); this.currentTrack = new SelectedGpxFile();
@ -89,10 +91,13 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
updateScript = "INSERT INTO " + TRACK_NAME + " (" + TRACK_COL_LAT + ", " + TRACK_COL_LON + ", " updateScript = "INSERT INTO " + TRACK_NAME + " (" + TRACK_COL_LAT + ", " + TRACK_COL_LON + ", "
+ TRACK_COL_ALTITUDE + ", " + TRACK_COL_SPEED + ", " + TRACK_COL_HDOP + ", " + TRACK_COL_ALTITUDE + ", " + TRACK_COL_SPEED + ", " + TRACK_COL_HDOP + ", "
+ TRACK_COL_DATE + ", " + TRACK_COL_HEADING + ")" + TRACK_COL_DATE + ", " + TRACK_COL_HEADING + ")"
+ " VALUES (?, ?, ?, ?, ?, ?, ?)"; //$NON-NLS-1$ //$NON-NLS-2$ + " VALUES (?, ?, ?, ?, ?, ?, ?)"; //$NON-NLS-1$ //$NON-NLS-2$
insertPointsScript = "INSERT INTO " + POINT_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?)"; //$NON-NLS-1$ //$NON-NLS-2$ insertPointsScript = "INSERT INTO " + POINT_NAME + " (" + POINT_COL_LAT + ", " + POINT_COL_LON + ", "
+ POINT_COL_DATE + ", " + POINT_COL_DESCRIPTION + ", " + POINT_COL_NAME + ", "
+ POINT_COL_CATEGORY + ", " + POINT_COL_COLOR + ", " + POINT_COL_ICON + ", "
+ POINT_COL_BACKGROUND + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; //$NON-NLS-1$ //$NON-NLS-2$
} }
@Override @Override
@ -101,18 +106,18 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
createTableForPoints(db); createTableForPoints(db);
} }
private void createTableForTrack(SQLiteDatabase db){ private void createTableForTrack(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TRACK_NAME + " (" + TRACK_COL_LAT + " double, " + TRACK_COL_LON + " double, " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ db.execSQL("CREATE TABLE " + TRACK_NAME + " (" + TRACK_COL_LAT + " double, " + TRACK_COL_LON + " double, " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ TRACK_COL_ALTITUDE + " double, " + TRACK_COL_SPEED + " double, " //$NON-NLS-1$ //$NON-NLS-2$ + TRACK_COL_ALTITUDE + " double, " + TRACK_COL_SPEED + " double, " //$NON-NLS-1$ //$NON-NLS-2$
+ TRACK_COL_HDOP + " double, " + TRACK_COL_DATE + " long, " + TRACK_COL_HDOP + " double, " + TRACK_COL_DATE + " long, "
+ TRACK_COL_HEADING + " float )"); //$NON-NLS-1$ //$NON-NLS-2$ + TRACK_COL_HEADING + " float )"); //$NON-NLS-1$ //$NON-NLS-2$
} }
private void createTableForPoints(SQLiteDatabase db){ private void createTableForPoints(SQLiteDatabase db) {
try { try {
db.execSQL("CREATE TABLE " + POINT_NAME + " (" + POINT_COL_LAT + " double, " + POINT_COL_LON + " double, " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ db.execSQL("CREATE TABLE " + POINT_NAME + " (" + POINT_COL_LAT + " double, " + POINT_COL_LON + " double, " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ POINT_COL_DATE + " long, " + POINT_COL_DESCRIPTION + " text, " + POINT_COL_NAME + " text, " + POINT_COL_DATE + " long, " + POINT_COL_DESCRIPTION + " text, " + POINT_COL_NAME + " text, "
+ POINT_COL_CATEGORY + " text, " + POINT_COL_COLOR + " long" + ")"); //$NON-NLS-1$ //$NON-NLS-2$ + POINT_COL_CATEGORY + " text, " + POINT_COL_COLOR + " long, " + POINT_COL_ICON + " text, " + POINT_COL_BACKGROUND + " text )"); //$NON-NLS-1$ //$NON-NLS-2$
} catch (RuntimeException e) { } catch (RuntimeException e) {
// ignore if already exists // ignore if already exists
} }
@ -120,21 +125,25 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
@Override @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion < 2){ if (oldVersion < 2) {
createTableForPoints(db); createTableForPoints(db);
} }
if(oldVersion < 3){ if (oldVersion < 3) {
db.execSQL("ALTER TABLE " + TRACK_NAME + " ADD " + TRACK_COL_HDOP + " double"); db.execSQL("ALTER TABLE " + TRACK_NAME + " ADD " + TRACK_COL_HDOP + " double");
} }
if(oldVersion < 4){ if (oldVersion < 4) {
db.execSQL("ALTER TABLE " + POINT_NAME + " ADD " + POINT_COL_NAME + " text"); db.execSQL("ALTER TABLE " + POINT_NAME + " ADD " + POINT_COL_NAME + " text");
db.execSQL("ALTER TABLE " + POINT_NAME + " ADD " + POINT_COL_CATEGORY + " text"); db.execSQL("ALTER TABLE " + POINT_NAME + " ADD " + POINT_COL_CATEGORY + " text");
} }
if(oldVersion < 5){ if (oldVersion < 5) {
db.execSQL("ALTER TABLE " + POINT_NAME + " ADD " + POINT_COL_COLOR + " long"); db.execSQL("ALTER TABLE " + POINT_NAME + " ADD " + POINT_COL_COLOR + " long");
} }
if(oldVersion < 6){ if (oldVersion < 6) {
db.execSQL("ALTER TABLE " + TRACK_NAME + " ADD " + TRACK_COL_HEADING + " float"); db.execSQL("ALTER TABLE " + TRACK_NAME + " ADD " + TRACK_COL_HEADING + " float");
}
if (oldVersion < 7) {
db.execSQL("ALTER TABLE " + POINT_NAME + " ADD " + POINT_COL_ICON + " text");
db.execSQL("ALTER TABLE " + POINT_NAME + " ADD " + POINT_COL_BACKGROUND + " text");
} }
} }
@ -146,7 +155,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
if (db != null) { if (db != null) {
try { try {
Cursor query = db.rawQuery("SELECT " + TRACK_COL_DATE + " FROM " + TRACK_NAME + " ORDER BY " + TRACK_COL_DATE + " DESC", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ Cursor query = db.rawQuery("SELECT " + TRACK_COL_DATE + " FROM " + TRACK_NAME + " ORDER BY " + TRACK_COL_DATE + " DESC", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
if(query.moveToFirst()) { if (query.moveToFirst()) {
res = query.getLong(0); res = query.getLong(0);
} }
query.close(); query.close();
@ -154,7 +163,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
db.close(); db.close();
} }
} }
} catch(RuntimeException e) { } catch (RuntimeException e) {
} }
return res; return res;
} }
@ -172,11 +181,11 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
} }
q = db.query(false, POINT_NAME, new String[]{POINT_COL_LAT, POINT_COL_LON}, null, null, null, null, null, null); q = db.query(false, POINT_NAME, new String[]{POINT_COL_LAT, POINT_COL_LON}, null, null, null, null, null, null);
has = q.moveToFirst(); has = q.moveToFirst();
while(has) { while (has) {
if(q.getDouble(0) != 0 || q.getDouble(1) != 0) { if (q.getDouble(0) != 0 || q.getDouble(1) != 0) {
break; break;
} }
if(!q.moveToNext()) { if (!q.moveToNext()) {
has = false; has = false;
break; break;
} }
@ -189,7 +198,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
db.close(); db.close();
} }
} }
} catch(RuntimeException e) { } catch (RuntimeException e) {
return false; return false;
} }
@ -294,7 +303,8 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
private void collectDBPoints(SQLiteDatabase db, Map<String, GPXFile> dataTracks) { private void collectDBPoints(SQLiteDatabase db, Map<String, GPXFile> dataTracks) {
Cursor query = db.rawQuery("SELECT " + POINT_COL_LAT + "," + POINT_COL_LON + "," + POINT_COL_DATE + "," //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ Cursor query = db.rawQuery("SELECT " + POINT_COL_LAT + "," + POINT_COL_LON + "," + POINT_COL_DATE + "," //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ POINT_COL_DESCRIPTION + "," + POINT_COL_NAME + "," + POINT_COL_CATEGORY + "," + POINT_COL_COLOR + " FROM " + POINT_NAME+" ORDER BY " + POINT_COL_DATE +" ASC", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + POINT_COL_DESCRIPTION + "," + POINT_COL_NAME + "," + POINT_COL_CATEGORY + "," + POINT_COL_COLOR + ","
+ POINT_COL_ICON + "," + POINT_COL_BACKGROUND + " FROM " + POINT_NAME + " ORDER BY " + POINT_COL_DATE + " ASC", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
if (query.moveToFirst()) { if (query.moveToFirst()) {
do { do {
WptPt pt = new WptPt(); WptPt pt = new WptPt();
@ -309,9 +319,11 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
if (color != 0) { if (color != 0) {
pt.setColor(color); pt.setColor(color);
} }
pt.setIconName(query.getString(7));
pt.setBackgroundType(query.getString(8));
// check if name is extension (needed for audio/video plugin & josm integration) // check if name is extension (needed for audio/video plugin & josm integration)
if(pt.name != null && pt.name.length() > 4 && pt.name.charAt(pt.name.length() - 4) == '.') { if (pt.name != null && pt.name.length() > 4 && pt.name.charAt(pt.name.length() - 4) == '.') {
pt.link = pt.name; pt.link = pt.name;
} }
@ -320,7 +332,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
if (dataTracks.containsKey(date)) { if (dataTracks.containsKey(date)) {
gpx = dataTracks.get(date); gpx = dataTracks.get(date);
} else { } else {
gpx = new GPXFile(Version.getFullVersion(ctx)); gpx = new GPXFile(Version.getFullVersion(ctx));
dataTracks.put(date, gpx); dataTracks.put(date, gpx);
} }
ctx.getSelectedGpxHelper().addPoint(pt, gpx); ctx.getSelectedGpxHelper().addPoint(pt, gpx);
@ -332,7 +344,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
private void collectDBTracks(SQLiteDatabase db, Map<String, GPXFile> dataTracks) { private void collectDBTracks(SQLiteDatabase db, Map<String, GPXFile> dataTracks) {
Cursor query = db.rawQuery("SELECT " + TRACK_COL_LAT + "," + TRACK_COL_LON + "," + TRACK_COL_ALTITUDE + "," //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ Cursor query = db.rawQuery("SELECT " + TRACK_COL_LAT + "," + TRACK_COL_LON + "," + TRACK_COL_ALTITUDE + "," //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ TRACK_COL_SPEED + "," + TRACK_COL_HDOP + "," + TRACK_COL_DATE + "," + TRACK_COL_HEADING + " FROM " + TRACK_NAME +" ORDER BY " + TRACK_COL_DATE +" ASC", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + TRACK_COL_SPEED + "," + TRACK_COL_HDOP + "," + TRACK_COL_DATE + "," + TRACK_COL_HEADING + " FROM " + TRACK_NAME + " ORDER BY " + TRACK_COL_DATE + " ASC", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
long previousTime = 0; long previousTime = 0;
long previousInterval = 0; long previousInterval = 0;
TrkSegment segment = null; TrkSegment segment = null;
@ -358,7 +370,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
} else if (track != null && (ctx.getSettings().AUTO_SPLIT_RECORDING.get() && currentInterval < 2 * 60 * 60 * 1000)) { } else if (track != null && (ctx.getSettings().AUTO_SPLIT_RECORDING.get() && currentInterval < 2 * 60 * 60 * 1000)) {
// 2 hour - same track // 2 hour - same track
segment = new TrkSegment(); segment = new TrkSegment();
if(!newInterval) { if (!newInterval) {
segment.points.add(pt); segment.points.add(pt);
} }
track.segments.add(segment); track.segments.add(segment);
@ -367,10 +379,10 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
track = new Track(); track = new Track();
segment = new TrkSegment(); segment = new TrkSegment();
track.segments.add(segment); track.segments.add(segment);
if(!newInterval) { if (!newInterval) {
segment.points.add(pt); segment.points.add(pt);
} }
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(new Date(time));; //$NON-NLS-1$ String date = new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(new Date(time)); //$NON-NLS-1$
if (dataTracks.containsKey(date)) { if (dataTracks.containsKey(date)) {
GPXFile gpx = dataTracks.get(date); GPXFile gpx = dataTracks.get(date);
gpx.tracks.add(track); gpx.tracks.add(track);
@ -415,7 +427,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
public void startNewSegment() { public void startNewSegment() {
lastTimeUpdated = 0; lastTimeUpdated = 0;
lastPoint = null; lastPoint = null;
execWithClose(updateScript, new Object[] { 0, 0, 0, 0, 0, System.currentTimeMillis(), NO_HEADING}); execWithClose(updateScript, new Object[]{0, 0, 0, 0, 0, System.currentTimeMillis(), NO_HEADING});
addTrackPoint(null, true, System.currentTimeMillis()); addTrackPoint(null, true, System.currentTimeMillis());
} }
@ -461,10 +473,10 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
} }
public void insertData(double lat, double lon, double alt, double speed, double hdop, long time, float heading, public void insertData(double lat, double lon, double alt, double speed, double hdop, long time, float heading,
OsmandSettings settings) { OsmandSettings settings) {
// * 1000 in next line seems to be wrong with new IntervalChooseDialog // * 1000 in next line seems to be wrong with new IntervalChooseDialog
// if (time - lastTimeUpdated > settings.SAVE_TRACK_INTERVAL.get() * 1000) { // if (time - lastTimeUpdated > settings.SAVE_TRACK_INTERVAL.get() * 1000) {
execWithClose(updateScript, new Object[] { lat, lon, alt, speed, hdop, time, heading }); execWithClose(updateScript, new Object[]{lat, lon, alt, speed, hdop, time, heading});
boolean newSegment = false; boolean newSegment = false;
if (lastPoint == null || (time - lastTimeUpdated) > 180 * 1000) { if (lastPoint == null || (time - lastTimeUpdated) > 180 * 1000) {
lastPoint = new LatLon(lat, lon); lastPoint = new LatLon(lat, lon);
@ -511,12 +523,12 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
} }
public WptPt insertPointData(double lat, double lon, long time, String description, String name, String category, public WptPt insertPointData(double lat, double lon, long time, String description, String name, String category,
int color) { int color) {
return insertPointData(lat, lon, time, description, name, category, color, null, null); return insertPointData(lat, lon, time, description, name, category, color, null, null);
} }
public WptPt insertPointData(double lat, double lon, long time, String description, String name, String category, public WptPt insertPointData(double lat, double lon, long time, String description, String name, String category,
int color, String iconName, String backgroundName) { int color, String iconName, String backgroundName) {
final WptPt pt = new WptPt(lat, lon, time, Double.NaN, 0, Double.NaN); final WptPt pt = new WptPt(lat, lon, time, Double.NaN, 0, Double.NaN);
pt.name = name; pt.name = name;
pt.category = category; pt.category = category;
@ -529,7 +541,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
ctx.getSelectedGpxHelper().addPoint(pt, currentTrack.getModifiableGpxFile()); ctx.getSelectedGpxHelper().addPoint(pt, currentTrack.getModifiableGpxFile());
currentTrack.getModifiableGpxFile().modifiedTime = time; currentTrack.getModifiableGpxFile().modifiedTime = time;
points++; points++;
execWithClose(insertPointsScript, new Object[] { lat, lon, time, description, name, category, color }); execWithClose(insertPointsScript, new Object[]{lat, lon, time, description, name, category, color, iconName, backgroundName});
return pt; return pt;
} }
@ -538,7 +550,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
} }
public void updatePointData(WptPt pt, double lat, double lon, long time, String description, String name, public void updatePointData(WptPt pt, double lat, double lon, long time, String description, String name,
String category, int color, String iconName, String iconBackground) { String category, int color, String iconName, String iconBackground) {
currentTrack.getModifiableGpxFile().modifiedTime = time; currentTrack.getModifiableGpxFile().modifiedTime = time;
List<Object> params = new ArrayList<>(); List<Object> params = new ArrayList<>();
@ -549,6 +561,8 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
params.add(name); params.add(name);
params.add(category); params.add(category);
params.add(color); params.add(color);
params.add(iconName);
params.add(iconBackground);
params.add(pt.getLatitude()); params.add(pt.getLatitude());
params.add(pt.getLongitude()); params.add(pt.getLongitude());
@ -563,7 +577,9 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
+ POINT_COL_DESCRIPTION + "=?, " + POINT_COL_DESCRIPTION + "=?, "
+ POINT_COL_NAME + "=?, " + POINT_COL_NAME + "=?, "
+ POINT_COL_CATEGORY + "=?, " + POINT_COL_CATEGORY + "=?, "
+ POINT_COL_COLOR + "=? " + POINT_COL_COLOR + "=?, "
+ POINT_COL_ICON + "=?, "
+ POINT_COL_BACKGROUND + "=? "
+ "WHERE " + "WHERE "
+ POINT_COL_LAT + "=? AND " + POINT_COL_LAT + "=? AND "
+ POINT_COL_LON + "=? AND " + POINT_COL_LON + "=? AND "
@ -662,10 +678,10 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
} }
} }
public void loadGpxFromDatabase(){ public void loadGpxFromDatabase() {
Map<String, GPXFile> files = collectRecordedData(); Map<String, GPXFile> files = collectRecordedData();
currentTrack.getModifiableGpxFile().tracks.clear(); currentTrack.getModifiableGpxFile().tracks.clear();
for (Map.Entry<String, GPXFile> entry : files.entrySet()){ for (Map.Entry<String, GPXFile> entry : files.entrySet()) {
ctx.getSelectedGpxHelper().addPoints(entry.getValue().getPoints(), currentTrack.getModifiableGpxFile()); ctx.getSelectedGpxHelper().addPoints(entry.getValue().getPoints(), currentTrack.getModifiableGpxFile());
currentTrack.getModifiableGpxFile().tracks.addAll(entry.getValue().tracks); currentTrack.getModifiableGpxFile().tracks.addAll(entry.getValue().tracks);
} }
@ -679,10 +695,10 @@ public class SavingTrackHelper extends SQLiteOpenHelper {
} }
private void prepareCurrentTrackForRecording() { private void prepareCurrentTrackForRecording() {
if(currentTrack.getModifiableGpxFile().tracks.size() == 0) { if (currentTrack.getModifiableGpxFile().tracks.size() == 0) {
currentTrack.getModifiableGpxFile().tracks.add(new Track()); currentTrack.getModifiableGpxFile().tracks.add(new Track());
} }
while(currentTrack.getPointsToDisplay().size() < currentTrack.getModifiableGpxFile().tracks.size()) { while (currentTrack.getPointsToDisplay().size() < currentTrack.getModifiableGpxFile().tracks.size()) {
TrkSegment trkSegment = new TrkSegment(); TrkSegment trkSegment = new TrkSegment();
currentTrack.getModifiablePointsToDisplay().add(trkSegment); currentTrack.getModifiablePointsToDisplay().add(trkSegment);
} }