Skip to content

Commit

Permalink
Merge pull request #1661 from OpenTracksApp/idlePreparation#1187
Browse files Browse the repository at this point in the history
Idle preparation#1187
  • Loading branch information
dennisguse authored Aug 8, 2023
2 parents 8bc781a + c0b5e71 commit 94191bb
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void testRecording_blesensor_only_no_distance() {
// then
new TrackPointAssert().assertEquals(List.of(
new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse(startTime)),
new TrackPoint(TrackPoint.Type.SENSORPOINT, Instant.parse(sensor3))
new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse(sensor3))
.setAltitudeGain(0f)
.setAltitudeLoss(0f)
.setHeartRate(HeartRate.of(7)),
Expand Down Expand Up @@ -867,7 +867,7 @@ public void testRecording_gpsAndSensor_gpsIdleMoving_sensorMoving() {
// then
new TrackPointAssert().assertEquals(List.of(
new TrackPoint(TrackPoint.Type.SEGMENT_START_MANUAL, Instant.parse(startTime)),
new TrackPoint(TrackPoint.Type.SENSORPOINT, Instant.parse(sensor2)) //First moving TrackPoint: store as the time might be interesting.
new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse(sensor2)) //First moving TrackPoint: store as the time might be interesting.
.setSpeed(Speed.of(5))
.setSensorDistance(Distance.of(2)),
new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse(gps1))
Expand All @@ -876,7 +876,7 @@ public void testRecording_gpsAndSensor_gpsIdleMoving_sensorMoving() {
.setHorizontalAccuracy(Distance.of(1))
.setSpeed(Speed.of(5))
.setSensorDistance(Distance.of(0)),
new TrackPoint(TrackPoint.Type.SENSORPOINT, Instant.parse(sensor3))
new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse(sensor3))
.setSpeed(Speed.of(5))
.setSensorDistance(Distance.of(10)),
new TrackPoint(TrackPoint.Type.TRACKPOINT, Instant.parse(gps3))
Expand Down
6 changes: 3 additions & 3 deletions src/androidTest/res/raw/csv_export.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#time,trackpoint_type,latitude,longitude,altitude,accuracy_horizontal,accuracy_vertical,speed,altitude_gain,altitude_loss,sensor_distance,heartrate,cadence,power
"2020-02-02T03:02:02+01:00","SEGMENT_START_MANUAL",,,,,,,,,,,,
"2020-02-02T03:02:03+01:00","TRACKPOINT",3,14,10,10,,54,1,1,,,,
"2020-02-02T03:02:04+01:00","SENSORPOINT",,,,,,54,1,1,10,66,3,50
"2020-02-02T03:02:15+01:00","SENSORPOINT",,,,,,,,,,68,3,50
"2020-02-02T03:02:16+01:00","SENSORPOINT",,,,,,18,,,2,69,3,50
"2020-02-02T03:02:04+01:00","TRACKPOINT",,,,,,54,1,1,10,66,3,50
"2020-02-02T03:02:15+01:00","TRACKPOINT",,,,,,,,,,68,3,50
"2020-02-02T03:02:16+01:00","TRACKPOINT",,,,,,18,,,2,69,3,50
"2020-02-02T03:02:17+01:00","TRACKPOINT",3,14.001,10,10,,18,0,0,2,69,3,50
"2020-02-02T03:02:18+01:00","SEGMENT_END_MANUAL",,,,,,,,,,,,
"2020-02-02T03:03:20+01:00","SEGMENT_START_MANUAL",,,,,,,,,,,,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class CustomSQLiteOpenHelper extends SQLiteOpenHelper {

private static final String TAG = CustomSQLiteOpenHelper.class.getSimpleName();

private static final int DATABASE_VERSION = 34;
private static final int DATABASE_VERSION = 36;

public CustomSQLiteOpenHelper(Context context) {
this(context, ((Startup) context.getApplicationContext()).getDatabaseName());
Expand Down Expand Up @@ -72,6 +72,8 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
case 32 -> upgradeFrom31to32(db);
case 33 -> upgradeFrom32to33(db);
case 34 -> upgradeFrom33to34(db);
case 35 -> upgradeFrom34to35(db);
case 36 -> upgradeFrom35to36(db);
default -> throw new RuntimeException("Not implemented: upgrade to " + toVersion);
}
}
Expand All @@ -93,6 +95,8 @@ public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
case 31 -> downgradeFrom32to31(db);
case 32 -> downgradeFrom33to32(db);
case 33 -> downgradeFrom34to33(db);
case 34 -> downgradeFrom35to34(db);
case 35 -> downgradeFrom36to35(db);
default -> throw new RuntimeException("Not implemented: downgrade to " + toVersion);
}
}
Expand Down Expand Up @@ -493,7 +497,7 @@ private void downgradeFrom34to33(SQLiteDatabase db) {
db.beginTransaction();

db.execSQL("ALTER TABLE trackpoints RENAME TO trackpoints_old");
db.execSQL("CREATE TABLE trackpoints (_id INTEGER PRIMARY KEY AUTOINCREMENT, trackid INTEGER NOT NULL, longitude INTEGER, latitude INTEGER, time INTEGER, elevation FLOAT, accuracy FLOAT, speed FLOAT, bearing FLOAT, sensor_heartrate FLOAT, sensor_cadence FLOAT, sensor_power FLOAT, elevation_gain FLOAT, elevation_loss FLOAT, type TEXT CHECK(type IN (-2, -1, 0, 1)), sensor_distance FLOAT, FOREIGN KEY (trackid) REFERENCES tracks(_id) ON UPDATE CASCADE ON DELETE CASCADE)");
db.execSQL("CREATE TABLE trackpoints (_id INTEGER PRIMARY KEY AUTOINCREMENT, trackid INTEGER NOT NULL, longitude INTEGER, latitude INTEGER, time INTEGER, elevation FLOAT, accuracy FLOAT, speed FLOAT, bearing FLOAT, sensor_heartrate FLOAT, sensor_cadence FLOAT, sensor_power FLOAT, elevation_gain FLOAT, elevation_loss FLOAT, type TEXT CHECK(type IN (-2, -1, 0, 1, 2)), sensor_distance FLOAT, FOREIGN KEY (trackid) REFERENCES tracks(_id) ON UPDATE CASCADE ON DELETE CASCADE)");
db.execSQL("INSERT INTO trackpoints SELECT _id, trackid, longitude, latitude, time, elevation, accuracy, speed, bearing, sensor_heartrate, sensor_cadence, sensor_power, elevation_gain, elevation_gain, type, sensor_distance FROM trackpoints_old");
db.execSQL("DROP TABLE trackpoints_old");

Expand All @@ -502,4 +506,60 @@ private void downgradeFrom34to33(SQLiteDatabase db) {
db.setTransactionSuccessful();
db.endTransaction();
}

private void upgradeFrom34to35(SQLiteDatabase db) {
db.beginTransaction();

db.execSQL("ALTER TABLE trackpoints RENAME TO trackpoints_old");
db.execSQL("CREATE TABLE trackpoints (_id INTEGER PRIMARY KEY AUTOINCREMENT, trackid INTEGER NOT NULL, longitude INTEGER, latitude INTEGER, time INTEGER, elevation FLOAT, accuracy FLOAT, speed FLOAT, bearing FLOAT, sensor_heartrate FLOAT, sensor_cadence FLOAT, sensor_power FLOAT, elevation_gain FLOAT, elevation_loss FLOAT, type TEXT CHECK(type IN (-2, -1, 0, 1, 2, 3)), sensor_distance FLOAT, accuracy_vertical FLOAT, FOREIGN KEY (trackid) REFERENCES tracks(_id) ON UPDATE CASCADE ON DELETE CASCADE)");
db.execSQL("INSERT INTO trackpoints SELECT _id, trackid, longitude, latitude, time, elevation, accuracy, speed, bearing, sensor_heartrate, sensor_cadence, sensor_power, elevation_gain, elevation_gain, type, sensor_distance, accuracy_vertical FROM trackpoints_old");
db.execSQL("DROP TABLE trackpoints_old");

db.execSQL("CREATE INDEX trackpoints_trackid_index ON trackpoints(trackid)");

db.setTransactionSuccessful();
db.endTransaction();
}

private void downgradeFrom35to34(SQLiteDatabase db) {
db.beginTransaction();

db.execSQL("ALTER TABLE trackpoints RENAME TO trackpoints_old");
db.execSQL("CREATE TABLE trackpoints (_id INTEGER PRIMARY KEY AUTOINCREMENT, trackid INTEGER NOT NULL, longitude INTEGER, latitude INTEGER, time INTEGER, elevation FLOAT, accuracy FLOAT, speed FLOAT, bearing FLOAT, sensor_heartrate FLOAT, sensor_cadence FLOAT, sensor_power FLOAT, elevation_gain FLOAT, elevation_loss FLOAT, type TEXT CHECK(type IN (-2, -1, 0, 1, 2)), sensor_distance FLOAT, accuracy_vertical FLOAT, FOREIGN KEY (trackid) REFERENCES tracks(_id) ON UPDATE CASCADE ON DELETE CASCADE)");
db.execSQL("INSERT INTO trackpoints SELECT _id, trackid, longitude, latitude, time, elevation, accuracy, speed, bearing, sensor_heartrate, sensor_cadence, sensor_power, elevation_gain, elevation_gain, type, sensor_distance, accuracy_vertical FROM trackpoints_old");
db.execSQL("DROP TABLE trackpoints_old");

db.execSQL("CREATE INDEX trackpoints_trackid_index ON trackpoints(trackid)");

db.setTransactionSuccessful();
db.endTransaction();
}

private void upgradeFrom35to36(SQLiteDatabase db) {
db.beginTransaction();

db.execSQL("ALTER TABLE trackpoints RENAME TO trackpoints_old");
db.execSQL("CREATE TABLE trackpoints (_id INTEGER PRIMARY KEY AUTOINCREMENT, trackid INTEGER NOT NULL, longitude INTEGER, latitude INTEGER, time INTEGER, elevation FLOAT, accuracy FLOAT, speed FLOAT, bearing FLOAT, sensor_heartrate FLOAT, sensor_cadence FLOAT, sensor_power FLOAT, elevation_gain FLOAT, elevation_loss FLOAT, type TEXT CHECK(type IN (-2, -1, 0, 1, 3)), sensor_distance FLOAT, accuracy_vertical FLOAT, FOREIGN KEY (trackid) REFERENCES tracks(_id) ON UPDATE CASCADE ON DELETE CASCADE)");
db.execSQL("INSERT INTO trackpoints SELECT _id, trackid, longitude, latitude, time, elevation, accuracy, speed, bearing, sensor_heartrate, sensor_cadence, sensor_power, elevation_gain, elevation_gain, type, sensor_distance, accuracy_vertical FROM trackpoints_old");
db.execSQL("DROP TABLE trackpoints_old");

db.execSQL("CREATE INDEX trackpoints_trackid_index ON trackpoints(trackid)");

db.setTransactionSuccessful();
db.endTransaction();
}

private void downgradeFrom36to35(SQLiteDatabase db) {
db.beginTransaction();

db.execSQL("ALTER TABLE trackpoints RENAME TO trackpoints_old");
db.execSQL("CREATE TABLE trackpoints (_id INTEGER PRIMARY KEY AUTOINCREMENT, trackid INTEGER NOT NULL, longitude INTEGER, latitude INTEGER, time INTEGER, elevation FLOAT, accuracy FLOAT, speed FLOAT, bearing FLOAT, sensor_heartrate FLOAT, sensor_cadence FLOAT, sensor_power FLOAT, elevation_gain FLOAT, elevation_loss FLOAT, type TEXT CHECK(type IN (-2, -1, 0, 1, 2)), sensor_distance FLOAT, accuracy_vertical FLOAT, FOREIGN KEY (trackid) REFERENCES tracks(_id) ON UPDATE CASCADE ON DELETE CASCADE)");
db.execSQL("INSERT INTO trackpoints SELECT _id, trackid, longitude, latitude, time, elevation, accuracy, speed, bearing, sensor_heartrate, sensor_cadence, sensor_power, elevation_gain, elevation_gain, type, sensor_distance, accuracy_vertical FROM trackpoints_old");
db.execSQL("DROP TABLE trackpoints_old");

db.execSQL("CREATE INDEX trackpoints_trackid_index ON trackpoints(trackid)");

db.setTransactionSuccessful();
db.endTransaction();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ public enum Type {
SEGMENT_START_MANUAL(-2), //Start of a segment due to user interaction (start, resume)

SEGMENT_START_AUTOMATIC(-1), //Start of a segment due to too much distance from previous TrackPoint
TRACKPOINT(0), //Just GPS data and may contain BLE sensor data
SENSORPOINT(2), //Just BLE sensor data; may have speed and sensorDistance
TRACKPOINT(0), //Was created due to sensor data (may contain GPS or other BLE data)

// Was used to distinguish the source (i.e., GPS vs BLE sensor), but this was too complicated. Everything is now a TRACKPOINT again.
@Deprecated
SENSORPOINT(2),
IDLE(3), //Device became idle

SEGMENT_END_MANUAL(1); //End of a segment

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public interface TrackPointsColumns extends BaseColumns {
+ SENSOR_POWER + " FLOAT, "
+ ALTITUDE_GAIN + " FLOAT, "
+ ALTITUDE_LOSS + " FLOAT, "
+ TYPE + " TEXT CHECK(type IN (-2, -1, 0, 1, 2)), "
+ TYPE + " TEXT CHECK(type IN (-2, -1, 0, 1, 3)), "
+ SENSOR_DISTANCE + " FLOAT, "
+ VERTICAL_ACCURACY + " FLOAT, "
+ "FOREIGN KEY (" + TRACKID + ") REFERENCES " + TracksColumns.TABLE_NAME + "(" + TracksColumns._ID + ") ON UPDATE CASCADE ON DELETE CASCADE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,7 @@ private void writeTrackPoints(List<Column> columns, Track track) throws Interrup
while (trackPointIterator.hasNext()) {
if (Thread.interrupted()) throw new InterruptedException();

TrackPoint trackPoint = trackPointIterator.next();

switch (trackPoint.getType()) {
case SEGMENT_START_MANUAL, SEGMENT_END_MANUAL, SEGMENT_START_AUTOMATIC, SENSORPOINT, TRACKPOINT ->
writeTrackPoint(columns, trackPoint);
default ->
throw new RuntimeException("Exporting this TrackPoint type is not implemented: " + trackPoint.getType());
}
writeTrackPoint(columns, trackPointIterator.next());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,18 @@ private void writeTrackPoints(Track track) throws InterruptedException {
sensorPoints.add(trackPoint);
}
}
case SENSORPOINT -> sensorPoints.add(trackPoint);
case TRACKPOINT -> {
if (!wroteSegment) {
// Might happen for older data (pre v3.15.0)
writeOpenSegment();
wroteSegment = true;
}
trackDistance = trackDistance.plus(writeTrackPoint(track.getZoneOffset(), trackPoint, sensorPoints, trackDistance));
sensorPoints.clear();
if (trackPoint.hasLocation()) {
trackDistance = trackDistance.plus(writeTrackPoint(track.getZoneOffset(), trackPoint, sensorPoints, trackDistance));
sensorPoints.clear();
} else {
sensorPoints.add(trackPoint);
}
}
default ->
throw new RuntimeException("Exporting this TrackPoint type is not implemented: " + trackPoint.getType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private void writeLocations(Track track) throws InterruptedException {
writeCloseSegment();
wroteSegment = false;
}
case SENSORPOINT, TRACKPOINT -> {
case TRACKPOINT -> {
if (!wroteSegment) {
// Might happen for older data (pre v3.15.0)
writeOpenSegment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,8 @@ private void onTrackSegmentEnd() {
Instant time = whenList.get(i);
Location location = locationList.get(i);

TrackPoint trackPoint = new TrackPoint(TrackPoint.Type.SENSORPOINT, time);
TrackPoint trackPoint = new TrackPoint(TrackPoint.Type.TRACKPOINT, time);
if (location != null) {
trackPoint.setType(TrackPoint.Type.TRACKPOINT);
trackPoint.setLocation(location);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public synchronized void onChange(@NonNull Location location) {
* Got a new TrackPoint from Bluetooth only; contains no GPS location.
*/
public synchronized void onChange(@NonNull SensorDataSet unused) {
onNewTrackPoint(new TrackPoint(TrackPoint.Type.SENSORPOINT, createNow()));
onNewTrackPoint(new TrackPoint(TrackPoint.Type.TRACKPOINT, createNow()));
}

@VisibleForTesting
Expand Down

0 comments on commit 94191bb

Please sign in to comment.