This commit is contained in:
Dennis Guse
2020-04-13 19:15:13 +02:00
parent 6a1fada22f
commit 1a86db0a8b
6 changed files with 86 additions and 134 deletions
@@ -18,7 +18,6 @@ package de.dennisguse.opentracks;
import android.location.Location; import android.location.Location;
import de.dennisguse.opentracks.content.data.Track;
import de.dennisguse.opentracks.content.data.TrackPoint; import de.dennisguse.opentracks.content.data.TrackPoint;
import de.dennisguse.opentracks.content.sensor.SensorDataSet; import de.dennisguse.opentracks.content.sensor.SensorDataSet;
@@ -40,21 +39,6 @@ public class TrackStubUtils {
// Used to change the value of latitude, longitude, and altitude. // Used to change the value of latitude, longitude, and altitude.
private static final double DIFFERENCE = 0.01; private static final double DIFFERENCE = 0.01;
/**
* Gets a a {@link Track} stub with specified number of locations.
*
* @param numberOfLocations the number of locations for the track
* @return a track stub.
*/
public static Track createTrack(int numberOfLocations) {
Track track = new Track();
for (int i = 0; i < numberOfLocations; i++) {
track.addTrackPoint(createDefaultTrackPoint(INITIAL_LATITUDE + i * DIFFERENCE, INITIAL_LONGITUDE + i * DIFFERENCE, INITIAL_ALTITUDE + i * DIFFERENCE));
}
return track;
}
/** /**
* Create a MyTracks location with default values. * Create a MyTracks location with default values.
* *
@@ -15,6 +15,7 @@ public class TestDataUtil {
* @param numPoints the location number in the track * @param numPoints the location number in the track
* @return the simulated track * @return the simulated track
*/ */
@Deprecated //TODO Does not store the data in the db.
public static Track getTrack(long id, int numPoints) { public static Track getTrack(long id, int numPoints) {
Track track = new Track(); Track track = new Track();
track.setId(id); track.setId(id);
@@ -196,8 +196,9 @@ public class CustomContentProviderUtilsTest {
long trackId = System.currentTimeMillis(); long trackId = System.currentTimeMillis();
Track track = TestDataUtil.getTrack(trackId, 10); Track track = TestDataUtil.getTrack(trackId, 10);
insertTrackWithLocations(track); insertTrackWithLocations(track);
Waypoint waypoint = new Waypoint(); Waypoint waypoint = new Waypoint(contentProviderUtils.getLastValidTrackPoint(trackId));
contentProviderUtils.insertWaypoint(waypoint); contentProviderUtils.insertWaypoint(waypoint);
ContentResolver contentResolver = context.getContentResolver(); ContentResolver contentResolver = context.getContentResolver();
Cursor tracksCursor = contentResolver.query(TracksColumns.CONTENT_URI, null, null, null, TracksColumns._ID); Cursor tracksCursor = contentResolver.query(TracksColumns.CONTENT_URI, null, null, null, TracksColumns._ID);
Assert.assertEquals(1, tracksCursor.getCount()); Assert.assertEquals(1, tracksCursor.getCount());
@@ -224,12 +225,12 @@ public class CustomContentProviderUtilsTest {
// Insert three tracks, points of two tracks and way point of one track. // Insert three tracks, points of two tracks and way point of one track.
long trackId = System.currentTimeMillis(); long trackId = System.currentTimeMillis();
Track track = TestDataUtil.getTrack(trackId, 10); Track track = TestDataUtil.getTrack(trackId, 10);
contentProviderUtils.insertTrack(track); contentProviderUtils.insertTrack(track);
insertTrackWithLocations(TestDataUtil.getTrack(trackId + 1, 10)); insertTrackWithLocations(TestDataUtil.getTrack(trackId + 1, 10));
insertTrackWithLocations(TestDataUtil.getTrack(trackId + 2, 10)); insertTrackWithLocations(TestDataUtil.getTrack(trackId + 2, 10));
Waypoint waypoint = new Waypoint(); Waypoint waypoint = new Waypoint(contentProviderUtils.getLastValidTrackPoint(trackId + 1));
waypoint.setTrackId(trackId); waypoint.setTrackId(trackId);
contentProviderUtils.insertWaypoint(waypoint); contentProviderUtils.insertWaypoint(waypoint);
@@ -389,10 +390,10 @@ public class CustomContentProviderUtilsTest {
public void testDeleteWaypoint_onlyOneWayPoint() { public void testDeleteWaypoint_onlyOneWayPoint() {
long trackId = System.currentTimeMillis(); long trackId = System.currentTimeMillis();
Track track = TestDataUtil.getTrack(trackId, 10); Track track = TestDataUtil.getTrack(trackId, 10);
contentProviderUtils.insertTrack(track); insertTrackWithLocations(track);
// Insert at first. // Insert at first.
Waypoint waypoint1 = new Waypoint(); Waypoint waypoint1 = new Waypoint(contentProviderUtils.getLastValidTrackPoint(trackId));
waypoint1.setDescription(TEST_DESC); waypoint1.setDescription(TEST_DESC);
waypoint1.setTrackId(trackId); waypoint1.setTrackId(trackId);
contentProviderUtils.insertWaypoint(waypoint1); contentProviderUtils.insertWaypoint(waypoint1);
@@ -425,16 +426,15 @@ public class CustomContentProviderUtilsTest {
statistics.setMinElevation(1200.0); statistics.setMinElevation(1200.0);
track.setTrackStatistics(statistics); track.setTrackStatistics(statistics);
contentProviderUtils.insertTrack(track); insertTrackWithLocations(track);
// Insert at first. // Insert at first.
Waypoint waypoint1 = new Waypoint(); Waypoint waypoint1 = new Waypoint(contentProviderUtils.getLastValidTrackPoint(trackId));
waypoint1.setDescription(MOCK_DESC); waypoint1.setDescription(MOCK_DESC);
waypoint1.setTrackId(trackId); waypoint1.setTrackId(trackId);
long waypoint1Id = ContentUris.parseId(contentProviderUtils.insertWaypoint(waypoint1)); long waypoint1Id = ContentUris.parseId(contentProviderUtils.insertWaypoint(waypoint1));
Waypoint waypoint2 = new Waypoint(); Waypoint waypoint2 = new Waypoint(contentProviderUtils.getLastValidTrackPoint(trackId));
waypoint2.setDescription(MOCK_DESC); waypoint2.setDescription(MOCK_DESC);
waypoint2.setTrackId(trackId); waypoint2.setTrackId(trackId);
long waypoint2Id = ContentUris.parseId(contentProviderUtils.insertWaypoint(waypoint2)); long waypoint2Id = ContentUris.parseId(contentProviderUtils.insertWaypoint(waypoint2));
@@ -454,15 +454,15 @@ public class CustomContentProviderUtilsTest {
public void testGetNextWaypointNumber() { public void testGetNextWaypointNumber() {
long trackId = System.currentTimeMillis(); long trackId = System.currentTimeMillis();
Track track = TestDataUtil.getTrack(trackId, 10); Track track = TestDataUtil.getTrack(trackId, 10);
contentProviderUtils.insertTrack(track); insertTrackWithLocations(track);
Waypoint waypoint1 = new Waypoint(); Waypoint waypoint1 = new Waypoint(contentProviderUtils.getLastValidTrackPoint(trackId));
waypoint1.setTrackId(trackId); waypoint1.setTrackId(trackId);
Waypoint waypoint2 = new Waypoint(); Waypoint waypoint2 = new Waypoint(contentProviderUtils.getLastValidTrackPoint(trackId));
waypoint2.setTrackId(trackId); waypoint2.setTrackId(trackId);
Waypoint waypoint3 = new Waypoint(); Waypoint waypoint3 = new Waypoint(contentProviderUtils.getLastValidTrackPoint(trackId));
waypoint3.setTrackId(trackId); waypoint3.setTrackId(trackId);
Waypoint waypoint4 = new Waypoint(); Waypoint waypoint4 = new Waypoint(contentProviderUtils.getLastValidTrackPoint(trackId));
waypoint4.setTrackId(trackId); waypoint4.setTrackId(trackId);
contentProviderUtils.insertWaypoint(waypoint1); contentProviderUtils.insertWaypoint(waypoint1);
contentProviderUtils.insertWaypoint(waypoint2); contentProviderUtils.insertWaypoint(waypoint2);
@@ -480,9 +480,9 @@ public class CustomContentProviderUtilsTest {
public void testInsertAndGetWaypoint() { public void testInsertAndGetWaypoint() {
long trackId = System.currentTimeMillis(); long trackId = System.currentTimeMillis();
Track track = TestDataUtil.getTrack(trackId, 10); Track track = TestDataUtil.getTrack(trackId, 10);
contentProviderUtils.insertTrack(track); insertTrackWithLocations(track);
Waypoint waypoint = new Waypoint(); Waypoint waypoint = new Waypoint(contentProviderUtils.getLastValidTrackPoint(trackId));
waypoint.setDescription(TEST_DESC); waypoint.setDescription(TEST_DESC);
waypoint.setTrackId(trackId); waypoint.setTrackId(trackId);
long waypointId = ContentUris.parseId(contentProviderUtils.insertWaypoint(waypoint)); long waypointId = ContentUris.parseId(contentProviderUtils.insertWaypoint(waypoint));
@@ -497,9 +497,10 @@ public class CustomContentProviderUtilsTest {
public void testUpdateWaypoint() { public void testUpdateWaypoint() {
long trackId = System.currentTimeMillis(); long trackId = System.currentTimeMillis();
Track track = TestDataUtil.getTrack(trackId, 10); Track track = TestDataUtil.getTrack(trackId, 10);
contentProviderUtils.insertTrack(track); insertTrackWithLocations(track);
// Insert at first. // Insert at first.
Waypoint waypoint = new Waypoint(); Waypoint waypoint = new Waypoint(contentProviderUtils.getLastValidTrackPoint(trackId));
waypoint.setDescription(TEST_DESC); waypoint.setDescription(TEST_DESC);
waypoint.setTrackId(trackId); waypoint.setTrackId(trackId);
long waypointId = ContentUris.parseId(contentProviderUtils.insertWaypoint(waypoint)); long waypointId = ContentUris.parseId(contentProviderUtils.insertWaypoint(waypoint));
@@ -41,6 +41,7 @@ public class Track {
private TrackStatistics trackStatistics = new TrackStatistics(); private TrackStatistics trackStatistics = new TrackStatistics();
// Location points (which may not have been loaded) // Location points (which may not have been loaded)
@Deprecated //TODO Is only used by tests
private List<TrackPoint> trackPoints = new ArrayList<>(); private List<TrackPoint> trackPoints = new ArrayList<>();
public Track() { public Track() {
@@ -94,15 +95,19 @@ public class Track {
this.trackStatistics = trackStatistics; this.trackStatistics = trackStatistics;
} }
@Deprecated
@VisibleForTesting @VisibleForTesting
public void addTrackPoint(TrackPoint location) { public void addTrackPoint(TrackPoint location) {
trackPoints.add(location); trackPoints.add(location);
} }
@VisibleForTesting
@Deprecated //TODO Only used for testing; can be removed?
public List<TrackPoint> getTrackPoints() { public List<TrackPoint> getTrackPoints() {
return trackPoints; return trackPoints;
} }
@Deprecated //TODO Remove
public void setTrackPoints(ArrayList<TrackPoint> trackPoints) { public void setTrackPoints(ArrayList<TrackPoint> trackPoints) {
this.trackPoints = trackPoints; this.trackPoints = trackPoints;
} }
@@ -38,14 +38,15 @@ public final class Waypoint {
private long trackId = -1L; private long trackId = -1L;
private double length = 0.0; private double length = 0.0;
private long duration = 0; private long duration = 0;
private Location location = null; private Location location;
private String photoUrl = ""; private String photoUrl = "";
@VisibleForTesting @VisibleForTesting
public Waypoint() { public Waypoint(@NonNull TrackPoint trackPoint) {
this.location = trackPoint.getLocation();
} }
public Waypoint(Location location) { public Waypoint(@NonNull Location location) {
this.location = location; this.location = location;
} }
@@ -55,19 +55,13 @@ public class ContentProviderUtils {
private static final String TAG = ContentProviderUtils.class.getSimpleName(); private static final String TAG = ContentProviderUtils.class.getSimpleName();
private static final int MAX_LATITUDE = 90000000; private static final int MAX_LATITUDE = 90000000;
/** // The authority (the first part of the URI) for the app's content provider.
* The authority (the first part of the URI) for the app's content provider.
*/
static final String AUTHORITY_PACKAGE = BuildConfig.APPLICATION_ID + ".content"; static final String AUTHORITY_PACKAGE = BuildConfig.APPLICATION_ID + ".content";
/** // The base URI for the app's content provider.
* The base URI for the app's content provider.
*/
public static final String CONTENT_BASE_URI = "content://" + AUTHORITY_PACKAGE; public static final String CONTENT_BASE_URI = "content://" + AUTHORITY_PACKAGE;
/** // Maximum number of waypoints that will be loaded at one time.
* Maximum number of waypoints that will be loaded at one time.
*/
public static final int MAX_LOADED_WAYPOINTS_POINTS = 10000; public static final int MAX_LOADED_WAYPOINTS_POINTS = 10000;
private static final String ID_SEPARATOR = ","; private static final String ID_SEPARATOR = ",";
@@ -87,7 +81,7 @@ public class ContentProviderUtils {
} }
/** /**
* Clears a track: removes waypoints and trackpoints. * Clears a track: removes waypoints and trackPoints.
* Only keeps the track id. * Only keeps the track id.
* *
* @param trackId the track id * @param trackId the track id
@@ -168,7 +162,7 @@ public class ContentProviderUtils {
} }
/** /**
* Deletes all tracks (including waypoints and track points). * Deletes all tracks (including waypoints and trackPoints).
*/ */
public void deleteAllTracks(Context context) { public void deleteAllTracks(Context context) {
contentResolver.delete(TrackPointsColumns.CONTENT_URI_BY_ID, null, null); contentResolver.delete(TrackPointsColumns.CONTENT_URI_BY_ID, null, null);
@@ -188,13 +182,12 @@ public class ContentProviderUtils {
public void deleteTrack(Context context, long trackId) { public void deleteTrack(Context context, long trackId) {
deleteTrackPointsAndWaypoints(context, trackId); deleteTrackPointsAndWaypoints(context, trackId);
// Delete track last since it triggers a database vaccum call // Delete track last since it triggers a database vacuum call
contentResolver.delete(TracksColumns.CONTENT_URI, TracksColumns._ID + "=?", contentResolver.delete(TracksColumns.CONTENT_URI, TracksColumns._ID + "=?", new String[]{Long.toString(trackId)});
new String[]{Long.toString(trackId)});
} }
/** /**
* Deletes track points and waypoints of a track. * Deletes trackPoints and waypoints of a track.
* *
* @param trackId the track id * @param trackId the track id
*/ */
@@ -203,8 +196,7 @@ public class ContentProviderUtils {
String[] selectionArgs = new String[]{Long.toString(trackId)}; String[] selectionArgs = new String[]{Long.toString(trackId)};
contentResolver.delete(TrackPointsColumns.CONTENT_URI_BY_ID, where, selectionArgs); contentResolver.delete(TrackPointsColumns.CONTENT_URI_BY_ID, where, selectionArgs);
contentResolver.delete(WaypointsColumns.CONTENT_URI, WaypointsColumns.TRACKID + "=?", contentResolver.delete(WaypointsColumns.CONTENT_URI, WaypointsColumns.TRACKID + "=?", new String[]{Long.toString(trackId)});
new String[]{Long.toString(trackId)});
deleteDirectoryRecurse(FileUtils.getPhotoDir(context, trackId)); deleteDirectoryRecurse(FileUtils.getPhotoDir(context, trackId));
} }
@@ -224,8 +216,8 @@ public class ContentProviderUtils {
/** /**
* Gets all the tracks. * Gets all the tracks.
* If no track exists, an empty list is returned. *
* NOTE: the returned tracks do not have any track points attached. * @return the tracks do not have any trackPoints attached.
*/ */
@VisibleForTesting @VisibleForTesting
public List<Track> getAllTracks() { public List<Track> getAllTracks() {
@@ -241,9 +233,6 @@ public class ContentProviderUtils {
return tracks; return tracks;
} }
/**
* Gets the last track or null.
*/
public Track getLastTrack() { public Track getLastTrack() {
try (Cursor cursor = getTrackCursor(null, null, TracksColumns.STARTTIME + " DESC")) { try (Cursor cursor = getTrackCursor(null, null, TracksColumns.STARTTIME + " DESC")) {
// Using the same order as shown in the track list // Using the same order as shown in the track list
@@ -255,10 +244,8 @@ public class ContentProviderUtils {
} }
/** /**
* Gets a track by a track id or null
* Note that the returned track doesn't have any track points attached.
*
* @param trackId the track id. * @param trackId the track id.
* @return the track doesn't have any trackPoints attached
*/ */
public Track getTrack(long trackId) { public Track getTrack(long trackId) {
if (trackId < 0) { if (trackId < 0) {
@@ -286,7 +273,7 @@ public class ContentProviderUtils {
/** /**
* Inserts a track. * Inserts a track.
* NOTE: This doesn't insert any track points. * NOTE: This doesn't insert any trackPoints.
* *
* @param track the track * @param track the track
* @return the content provider URI of the inserted track. * @return the content provider URI of the inserted track.
@@ -297,13 +284,12 @@ public class ContentProviderUtils {
/** /**
* Updates a track. * Updates a track.
* NOTE: This doesn't update any track points. * NOTE: This doesn't update any trackPoints.
* *
* @param track the track * @param track the track
*/ */
public void updateTrack(Track track) { public void updateTrack(Track track) {
contentResolver.update(TracksColumns.CONTENT_URI, createContentValues(track), contentResolver.update(TracksColumns.CONTENT_URI, createContentValues(track), TracksColumns._ID + "=?", new String[]{Long.toString(track.getId())});
TracksColumns._ID + "=?", new String[]{Long.toString(track.getId())});
} }
private ContentValues createContentValues(Track track) { private ContentValues createContentValues(Track track) {
@@ -407,14 +393,6 @@ public class ContentProviderUtils {
return waypoint; return waypoint;
} }
/**
* Deletes a waypoint.
* If deleting a statistics waypoint, this will also correct the next statistics waypoint after the deleted one to reflect the deletion.
* The generator is used to update the next statistics waypoint.
*
* @param waypointId the waypoint id
*/
public void deleteWaypoint(long waypointId) { public void deleteWaypoint(long waypointId) {
final Waypoint waypoint = getWaypoint(waypointId); final Waypoint waypoint = getWaypoint(waypointId);
if (waypoint != null && waypoint.hasPhoto()) { if (waypoint != null && waypoint.hasPhoto()) {
@@ -432,10 +410,10 @@ public class ContentProviderUtils {
} }
/** /**
* Gets the next waypoint number for a type. * Gets the next waypoint number.
* Returns -1 if not able to get the next waypoint number.
* *
* @param trackId the track id * @param trackId the track id
* @return -1 if not able to get the next waypoint number.
*/ */
public int getNextWaypointNumber(long trackId) { public int getNextWaypointNumber(long trackId) {
if (trackId < 0) { if (trackId < 0) {
@@ -452,12 +430,6 @@ public class ContentProviderUtils {
return -1; return -1;
} }
/**
* Gets a waypoint from a waypoint id.
* Returns null if not found.
*
* @param waypointId the waypoint id
*/
public Waypoint getWaypoint(long waypointId) { public Waypoint getWaypoint(long waypointId) {
if (waypointId < 0) { if (waypointId < 0) {
return null; return null;
@@ -478,8 +450,7 @@ public class ContentProviderUtils {
* @param selection the selection. Can be null * @param selection the selection. Can be null
* @param selectionArgs the selection arguments. Can be null * @param selectionArgs the selection arguments. Can be null
* @param sortOrder the sort order. Can be null * @param sortOrder the sort order. Can be null
* @param maxWaypoints the maximum number of waypoints to return. -1 for no * @param maxWaypoints the maximum number of waypoints to return. -1 for no limit
* limit
*/ */
public Cursor getWaypointCursor(String selection, String[] selectionArgs, String sortOrder, int maxWaypoints) { public Cursor getWaypointCursor(String selection, String[] selectionArgs, String sortOrder, int maxWaypoints) {
return getWaypointCursor(null, selection, selectionArgs, sortOrder, maxWaypoints); return getWaypointCursor(null, selection, selectionArgs, sortOrder, maxWaypoints);
@@ -536,12 +507,13 @@ public class ContentProviderUtils {
String[] projection = new String[]{"count(*) AS count"}; String[] projection = new String[]{"count(*) AS count"};
String selection = WaypointsColumns.TRACKID + "=?"; String selection = WaypointsColumns.TRACKID + "=?";
String[] selectionArgs = new String[]{Long.toString(trackId)}; String[] selectionArgs = new String[]{Long.toString(trackId)};
Cursor cursor = contentResolver.query(WaypointsColumns.CONTENT_URI, projection, selection, selectionArgs, WaypointsColumns._ID); try (Cursor cursor = contentResolver.query(WaypointsColumns.CONTENT_URI, projection, selection, selectionArgs, WaypointsColumns._ID)) {
if (cursor == null) {
cursor.moveToFirst(); return 0;
int count = cursor.getInt(0); }
cursor.close(); cursor.moveToFirst();
return count; return cursor.getInt(0);
}
} }
/** /**
@@ -582,19 +554,17 @@ public class ContentProviderUtils {
values.put(WaypointsColumns.DURATION, waypoint.getDuration()); values.put(WaypointsColumns.DURATION, waypoint.getDuration());
Location location = waypoint.getLocation(); Location location = waypoint.getLocation();
if (location != null) { values.put(WaypointsColumns.LONGITUDE, (int) (location.getLongitude() * 1E6));
values.put(WaypointsColumns.LONGITUDE, (int) (location.getLongitude() * 1E6)); values.put(WaypointsColumns.LATITUDE, (int) (location.getLatitude() * 1E6));
values.put(WaypointsColumns.LATITUDE, (int) (location.getLatitude() * 1E6)); values.put(WaypointsColumns.TIME, location.getTime());
values.put(WaypointsColumns.TIME, location.getTime()); if (location.hasAltitude()) {
if (location.hasAltitude()) { values.put(WaypointsColumns.ALTITUDE, location.getAltitude());
values.put(WaypointsColumns.ALTITUDE, location.getAltitude()); }
} if (location.hasAccuracy()) {
if (location.hasAccuracy()) { values.put(WaypointsColumns.ACCURACY, location.getAccuracy());
values.put(WaypointsColumns.ACCURACY, location.getAccuracy()); }
} if (location.hasBearing()) {
if (location.hasBearing()) { values.put(WaypointsColumns.BEARING, location.getBearing());
values.put(WaypointsColumns.BEARING, location.getBearing());
}
} }
values.put(WaypointsColumns.PHOTOURL, waypoint.getPhotoUrl()); values.put(WaypointsColumns.PHOTOURL, waypoint.getPhotoUrl());
@@ -623,8 +593,8 @@ public class ContentProviderUtils {
/** /**
* Fills a {@link TrackPoint} from a cursor. * Fills a {@link TrackPoint} from a cursor.
* *
* @param cursor the cursor pointing to a trackPoint. * @param cursor the cursor pointing to a trackPoint.
* @param indexes the cached track points indexes * @param indexes the cached trackPoints indexes
*/ */
static TrackPoint fillTrackPoint(Cursor cursor, CachedTrackPointsIndexes indexes) { static TrackPoint fillTrackPoint(Cursor cursor, CachedTrackPointsIndexes indexes) {
TrackPoint trackPoint = new TrackPoint(); TrackPoint trackPoint = new TrackPoint();
@@ -661,13 +631,12 @@ public class ContentProviderUtils {
} }
/** /**
* Inserts multiple trackPoints points. * Inserts multiple trackPoints.
* *
* @param trackPoints an array of trackPoints * @param trackPoints an array of trackPoints
* @param length the number of trackPoints (from the beginning of the array) to * @param length the number of trackPoints (from the beginning of the array) to insert, or -1 for all of them
* insert, or -1 for all of them * @param trackId the trackPoints id
* @param trackId the trackPoints id * @return the number of trackPoints inserted
* @return the number of points inserted
*/ */
public int bulkInsertTrackPoint(TrackPoint[] trackPoints, int length, long trackId) { public int bulkInsertTrackPoint(TrackPoint[] trackPoints, int length, long trackId) {
if (length == -1) { if (length == -1) {
@@ -691,9 +660,7 @@ public class ContentProviderUtils {
if (trackId < 0) { if (trackId < 0) {
return -1L; return -1L;
} }
String selection = TrackPointsColumns._ID + "=(select min(" + TrackPointsColumns._ID String selection = TrackPointsColumns._ID + "=(SELECT MIN(" + TrackPointsColumns._ID + ") FROM " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID + "=?)";
+ ") from " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID
+ "=?)";
String[] selectionArgs = new String[]{Long.toString(trackId)}; String[] selectionArgs = new String[]{Long.toString(trackId)};
try (Cursor cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection, selectionArgs, TrackPointsColumns._ID)) { try (Cursor cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection, selectionArgs, TrackPointsColumns._ID)) {
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
@@ -714,9 +681,7 @@ public class ContentProviderUtils {
if (trackId < 0) { if (trackId < 0) {
return -1L; return -1L;
} }
String selection = TrackPointsColumns._ID + "=(select max(" + TrackPointsColumns._ID String selection = TrackPointsColumns._ID + "=(SELECT MAX(" + TrackPointsColumns._ID + ") from " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID + "=?)";
+ ") from " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID
+ "=?)";
String[] selectionArgs = new String[]{Long.toString(trackId)}; String[] selectionArgs = new String[]{Long.toString(trackId)};
try (Cursor cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection, selectionArgs, TrackPointsColumns._ID)) { try (Cursor cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection, selectionArgs, TrackPointsColumns._ID)) {
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
@@ -727,19 +692,17 @@ public class ContentProviderUtils {
} }
/** /**
* Gets the track point id of a location. * Gets the trackPoint id for a location.
* *
* @param trackId the track id * @param trackId the track id
* @param location the location * @param location the location
* @return track point id if the location is in the track. -1L otherwise. * @return trackPoint id if the location is in the track. -1L otherwise.
*/ */
public long getTrackPointId(long trackId, Location location) { public long getTrackPointId(long trackId, Location location) {
if (trackId < 0) { if (trackId < 0) {
return -1L; return -1L;
} }
String selection = TrackPointsColumns._ID + "=(select max(" + TrackPointsColumns._ID String selection = TrackPointsColumns._ID + "=(SELECT MAX(" + TrackPointsColumns._ID + ") FROM " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID + "=? AND " + TrackPointsColumns.TIME + "=?)";
+ ") from " + TrackPointsColumns.TABLE_NAME
+ " WHERE " + TrackPointsColumns.TRACKID + "=? AND " + TrackPointsColumns.TIME + "=?)";
String[] selectionArgs = new String[]{Long.toString(trackId), Long.toString(location.getTime())}; String[] selectionArgs = new String[]{Long.toString(trackId), Long.toString(location.getTime())};
try (Cursor cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection, selectionArgs, TrackPointsColumns._ID)) { try (Cursor cursor = getTrackPointCursor(new String[]{TrackPointsColumns._ID}, selection, selectionArgs, TrackPointsColumns._ID)) {
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
@@ -762,7 +725,7 @@ public class ContentProviderUtils {
* Creates a location cursor. The caller owns the returned cursor and is responsible for closing it. * Creates a location cursor. The caller owns the returned cursor and is responsible for closing it.
* *
* @param trackId the track id * @param trackId the track id
* @param startTrackPointId the starting track point id. -1L to ignore * @param startTrackPointId the starting trackPoint id. -1L to ignore
* @param maxLocations maximum number of locations to return. -1 for no limit * @param maxLocations maximum number of locations to return. -1 for no limit
* @param descending true to sort the result in descending order (latest location first) * @param descending true to sort the result in descending order (latest location first)
*/ */
@@ -775,8 +738,7 @@ public class ContentProviderUtils {
String[] selectionArgs; String[] selectionArgs;
if (startTrackPointId >= 0) { if (startTrackPointId >= 0) {
String comparison = descending ? "<=" : ">="; String comparison = descending ? "<=" : ">=";
selection = TrackPointsColumns.TRACKID + "=? AND " + TrackPointsColumns._ID + comparison selection = TrackPointsColumns.TRACKID + "=? AND " + TrackPointsColumns._ID + comparison + "?";
+ "?";
selectionArgs = new String[]{Long.toString(trackId), Long.toString(startTrackPointId)}; selectionArgs = new String[]{Long.toString(trackId), Long.toString(startTrackPointId)};
} else { } else {
selection = TrackPointsColumns.TRACKID + "=?"; selection = TrackPointsColumns.TRACKID + "=?";
@@ -804,19 +766,17 @@ public class ContentProviderUtils {
if (trackId < 0) { if (trackId < 0) {
return null; return null;
} }
String selection = TrackPointsColumns._ID + "=(select max(" + TrackPointsColumns._ID + ") from " String selection = TrackPointsColumns._ID + "=(SELECT MAX(" + TrackPointsColumns._ID + ") FROM " + TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID + "=? AND " + TrackPointsColumns.LATITUDE + "<=" + MAX_LATITUDE + ")";
+ TrackPointsColumns.TABLE_NAME + " WHERE " + TrackPointsColumns.TRACKID + "=? AND "
+ TrackPointsColumns.LATITUDE + "<=" + MAX_LATITUDE + ")";
String[] selectionArgs = new String[]{Long.toString(trackId)}; String[] selectionArgs = new String[]{Long.toString(trackId)};
return findTrackPointBy(selection, selectionArgs); return findTrackPointBy(selection, selectionArgs);
} }
/** /**
* Inserts a track point. * Inserts a trackPoint.
* *
* @param trackPoint the trackPoint * @param trackPoint the trackPoint
* @param trackId the track id * @param trackId the track id
* @return the content provider URI of the inserted track point * @return the content provider URI of the inserted trackPoint
*/ */
public Uri insertTrackPoint(TrackPoint trackPoint, long trackId) { public Uri insertTrackPoint(TrackPoint trackPoint, long trackId) {
return contentResolver.insert(TrackPointsColumns.CONTENT_URI_BY_ID, createContentValues(trackPoint, trackId)); return contentResolver.insert(TrackPointsColumns.CONTENT_URI_BY_ID, createContentValues(trackPoint, trackId));
@@ -826,7 +786,7 @@ public class ContentProviderUtils {
* Creates the {@link ContentValues} for a {@link TrackPoint}. * Creates the {@link ContentValues} for a {@link TrackPoint}.
* *
* @param trackPoint the trackPoint * @param trackPoint the trackPoint
* @param trackId the track id * @param trackId the track id
*/ */
private ContentValues createContentValues(TrackPoint trackPoint, long trackId) { private ContentValues createContentValues(TrackPoint trackPoint, long trackId) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
@@ -870,7 +830,7 @@ public class ContentProviderUtils {
* When done with iteration, {@link TrackPointIterator#close()} must be called. * When done with iteration, {@link TrackPointIterator#close()} must be called.
* *
* @param trackId the track id * @param trackId the track id
* @param startTrackPointId the starting track point id. -1L to ignore * @param startTrackPointId the starting trackPoint id. -1L to ignore
* @param descending true to sort the result in descending order (latest location first) * @param descending true to sort the result in descending order (latest location first)
*/ */
public TrackPointIterator getTrackPointLocationIterator(final long trackId, final long startTrackPointId, final boolean descending) { public TrackPointIterator getTrackPointLocationIterator(final long trackId, final long startTrackPointId, final boolean descending) {
@@ -887,7 +847,7 @@ public class ContentProviderUtils {
} }
/** /**
* Gets a track point cursor. * Gets a trackPoint cursor.
* *
* @param projection the projection * @param projection the projection
* @param selection the selection * @param selection the selection
@@ -915,7 +875,7 @@ public class ContentProviderUtils {
/** /**
* Formats an array of IDs as comma separated string value * Formats an array of IDs as comma separated string value
* *
* @param ids array with IDs * @param ids array with IDs
* @return comma separated list of ids * @return comma separated list of ids
*/ */
public static String formatIdListForUri(long[] ids) { public static String formatIdListForUri(long[] ids) {