forked from upstream-mirrors/OpenTracks
Marker is now a record.
This commit is contained in:
@@ -50,7 +50,6 @@ import de.dennisguse.opentracks.data.models.Power;
|
||||
import de.dennisguse.opentracks.data.models.Speed;
|
||||
import de.dennisguse.opentracks.data.models.Statistics;
|
||||
import de.dennisguse.opentracks.data.models.Track;
|
||||
import de.dennisguse.opentracks.data.models.TrackBuilder;
|
||||
import de.dennisguse.opentracks.data.models.TrackPoint;
|
||||
import de.dennisguse.opentracks.data.tables.MarkerColumns;
|
||||
import de.dennisguse.opentracks.data.tables.TrackPointsColumns;
|
||||
@@ -319,7 +318,7 @@ public class ContentProviderUtils {
|
||||
int latitudeIndex = cursor.getColumnIndexOrThrow(MarkerColumns.LATITUDE);
|
||||
int timeIndex = cursor.getColumnIndexOrThrow(MarkerColumns.TIME);
|
||||
int altitudeIndex = cursor.getColumnIndexOrThrow(MarkerColumns.ALTITUDE);
|
||||
int accuracyIndex = cursor.getColumnIndexOrThrow(MarkerColumns.ACCURACY);
|
||||
int accuracyIndex = cursor.getColumnIndexOrThrow(MarkerColumns.HORIZONTAL_ACCURACY);
|
||||
int bearingIndex = cursor.getColumnIndexOrThrow(MarkerColumns.BEARING);
|
||||
int photoUrlIndex = cursor.getColumnIndexOrThrow(MarkerColumns.PHOTOURL);
|
||||
|
||||
@@ -353,37 +352,27 @@ public class ContentProviderUtils {
|
||||
bearing,
|
||||
null);
|
||||
|
||||
Track.Id trackId = new Track.Id(cursor.getLong(trackIdIndex));
|
||||
Marker marker = new Marker(trackId, position);
|
||||
|
||||
if (!cursor.isNull(idIndex)) {
|
||||
marker.setId(new Marker.Id(cursor.getLong(idIndex)));
|
||||
}
|
||||
if (!cursor.isNull(nameIndex)) {
|
||||
marker.setName(cursor.getString(nameIndex));
|
||||
}
|
||||
if (!cursor.isNull(descriptionIndex)) {
|
||||
marker.setDescription(cursor.getString(descriptionIndex));
|
||||
}
|
||||
if (!cursor.isNull(categoryIndex)) {
|
||||
marker.setCategory(cursor.getString(categoryIndex));
|
||||
}
|
||||
if (!cursor.isNull(iconIndex)) {
|
||||
marker.setIcon(cursor.getString(iconIndex));
|
||||
}
|
||||
Uri photoUrl = null;
|
||||
if (!cursor.isNull(photoUrlIndex)) {
|
||||
String photoUrl = cursor.getString(photoUrlIndex);
|
||||
if (photoUrl.isEmpty()) {
|
||||
if (cursor.getString(photoUrlIndex).isEmpty()) {
|
||||
// Before v4.18.0: a marker without a picture as URL ""
|
||||
// TODO Data should be migrated.
|
||||
marker.setPhotoUrl(null);
|
||||
photoUrl = null;
|
||||
} else {
|
||||
marker.setPhotoUrl(Uri.parse(cursor.getString(photoUrlIndex)));
|
||||
photoUrl = Uri.parse(cursor.getString(photoUrlIndex));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return marker;
|
||||
return new Marker(
|
||||
new Marker.Id(cursor.getLong(idIndex)),
|
||||
new Track.Id(cursor.getLong(trackIdIndex)),
|
||||
!cursor.isNull(nameIndex) ? cursor.getString(nameIndex) : "",
|
||||
!cursor.isNull(descriptionIndex) ? cursor.getString(descriptionIndex) : "",
|
||||
!cursor.isNull(categoryIndex) ? cursor.getString(categoryIndex) : "",
|
||||
!cursor.isNull(iconIndex) ? cursor.getString(iconIndex) : "",
|
||||
position,
|
||||
photoUrl
|
||||
);
|
||||
}
|
||||
|
||||
public void deleteMarker(Context context, Marker.Id markerId) {
|
||||
@@ -445,24 +434,26 @@ public class ContentProviderUtils {
|
||||
* @return true if successful.
|
||||
*/
|
||||
public boolean updateMarker(Context context, Marker updateMarker) {
|
||||
Marker savedMarker = getMarker(updateMarker.getId());
|
||||
if (!updateMarker.hasPhoto()) {
|
||||
Marker savedMarker = getMarker(updateMarker.id());
|
||||
if (updateMarker.photoUrl() == null) {
|
||||
deleteMarkerPhoto(context, savedMarker);
|
||||
}
|
||||
int rows = contentResolver.update(MarkerColumns.CONTENT_URI, createContentValues(updateMarker), MarkerColumns._ID + "=?", new String[]{Long.toString(updateMarker.getId().id())});
|
||||
int rows = contentResolver.update(MarkerColumns.CONTENT_URI, createContentValues(updateMarker, updateMarker.trackId()), MarkerColumns._ID + "=?", new String[]{Long.toString(updateMarker.id().id())});
|
||||
return rows == 1;
|
||||
}
|
||||
|
||||
public Marker.Id insertMarker(@NonNull Marker marker) {
|
||||
marker.setId(null);
|
||||
Uri uri = contentResolver.insert(MarkerColumns.CONTENT_URI, createContentValues(marker));
|
||||
assert marker.id() == null;
|
||||
assert marker.trackId() != null;
|
||||
|
||||
Uri uri = contentResolver.insert(MarkerColumns.CONTENT_URI, createContentValues(marker, marker.trackId()));
|
||||
return new Marker.Id(ContentUris.parseId(uri));
|
||||
}
|
||||
|
||||
private void deleteMarkerPhoto(Context context, Marker marker) {
|
||||
if (marker != null && marker.hasPhoto()) {
|
||||
Uri uri = marker.getPhotoUrl();
|
||||
File file = MarkerUtils.buildInternalPhotoFile(context, marker.getTrackId(), uri);
|
||||
if (marker != null && marker.photoUrl() != null) {
|
||||
Uri uri = marker.photoUrl();
|
||||
File file = MarkerUtils.buildInternalPhotoFile(context, marker.trackId(), uri);
|
||||
if (file.exists()) {
|
||||
File parent = file.getParentFile();
|
||||
file.delete();
|
||||
@@ -473,33 +464,33 @@ public class ContentProviderUtils {
|
||||
}
|
||||
}
|
||||
|
||||
ContentValues createContentValues(@NonNull Marker marker) {
|
||||
ContentValues createContentValues(@NonNull Marker marker, @NonNull Track.Id trackId) {
|
||||
ContentValues values = new ContentValues();
|
||||
|
||||
if (marker.getId() != null) {
|
||||
values.put(MarkerColumns._ID, marker.getId().id());
|
||||
if (marker.id() != null) {
|
||||
values.put(MarkerColumns._ID, marker.id().id());
|
||||
}
|
||||
values.put(MarkerColumns.NAME, marker.getName());
|
||||
values.put(MarkerColumns.DESCRIPTION, marker.getDescription());
|
||||
values.put(MarkerColumns.CATEGORY, marker.getCategory());
|
||||
values.put(MarkerColumns.ICON, marker.getIcon());
|
||||
values.put(MarkerColumns.TRACKID, marker.getTrackId().id());
|
||||
values.put(MarkerColumns.LONGITUDE, (int) (marker.getPosition().longitude() * 1E6));
|
||||
values.put(MarkerColumns.LATITUDE, (int) (marker.getPosition().latitude() * 1E6));
|
||||
values.put(MarkerColumns.TIME, marker.getTime().toEpochMilli());
|
||||
if (marker.hasAltitude()) {
|
||||
values.put(MarkerColumns.ALTITUDE, marker.getAltitude().toM());
|
||||
values.put(MarkerColumns.TRACKID, trackId.id());
|
||||
|
||||
values.put(MarkerColumns.NAME, marker.name());
|
||||
values.put(MarkerColumns.DESCRIPTION, marker.description());
|
||||
values.put(MarkerColumns.CATEGORY, marker.category());
|
||||
values.put(MarkerColumns.ICON, marker.icon());
|
||||
|
||||
values.put(MarkerColumns.LONGITUDE, (int) (marker.position().longitude() * 1E6));
|
||||
values.put(MarkerColumns.LATITUDE, (int) (marker.position().latitude() * 1E6));
|
||||
values.put(MarkerColumns.TIME, marker.position().time().toEpochMilli());
|
||||
if (marker.position().hasAltitude()) {
|
||||
values.put(MarkerColumns.ALTITUDE, marker.position().altitude().toM());
|
||||
}
|
||||
if (marker.hasAccuracy()) {
|
||||
values.put(MarkerColumns.ACCURACY, marker.getAccuracy().toM());
|
||||
if (marker.position().hasHorizontalAccuracy()) {
|
||||
values.put(MarkerColumns.HORIZONTAL_ACCURACY, marker.position().horizontalAccuracy().toM());
|
||||
}
|
||||
if (marker.hasBearing()) {
|
||||
values.put(MarkerColumns.BEARING, marker.getBearing());
|
||||
if (marker.position().hasBearing()) {
|
||||
values.put(MarkerColumns.BEARING, marker.position().bearing());
|
||||
}
|
||||
|
||||
if (marker.hasPhoto()) {
|
||||
values.put(MarkerColumns.PHOTOURL, marker.getPhotoUrl().toString());
|
||||
}
|
||||
values.put(MarkerColumns.PHOTOURL, marker.photoUrl() != null ? marker.photoUrl().toString() : null);
|
||||
return values;
|
||||
}
|
||||
|
||||
@@ -603,7 +594,7 @@ public class ContentProviderUtils {
|
||||
public int bulkInsertMarkers(List<Marker> markers, Track.Id trackId) {
|
||||
ContentValues[] values = new ContentValues[markers.size()];
|
||||
for (int i = 0; i < markers.size(); i++) {
|
||||
values[i] = createContentValues(markers.get(i));
|
||||
values[i] = createContentValues(markers.get(i), trackId);
|
||||
}
|
||||
return contentResolver.bulkInsert(MarkerColumns.CONTENT_URI, values);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,3 @@
|
||||
/*
|
||||
* Copyright 2009 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package de.dennisguse.opentracks.data.models;
|
||||
|
||||
import android.net.Uri;
|
||||
@@ -23,151 +7,32 @@ import android.os.Parcelable;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* NOTE: A marker is indirectly (via it's {@link Position}) assigned to one {@link TrackPoint} via position.time.
|
||||
*
|
||||
* @author Leif Hendrik Wilden
|
||||
* @author Rodrigo Damazio
|
||||
* * NOTE: A marker is indirectly (via it's {@link Position}) assigned to one {@link TrackPoint} via position.time.
|
||||
*/
|
||||
//TODO All data should be final; no default values.
|
||||
public final class Marker {
|
||||
public record Marker(
|
||||
|
||||
private Id id;
|
||||
private String name = "";
|
||||
private String description = "";
|
||||
private String category = "";
|
||||
private String icon = "";
|
||||
private Track.Id trackId;
|
||||
@NonNull
|
||||
Id id,
|
||||
|
||||
//Some data might not be used.
|
||||
private final Position position;
|
||||
@NonNull
|
||||
Track.Id trackId,
|
||||
|
||||
private Uri photoUrl = null;
|
||||
@NonNull
|
||||
String name,
|
||||
@NonNull
|
||||
String description,
|
||||
@NonNull
|
||||
String category,
|
||||
@NonNull
|
||||
String icon,
|
||||
|
||||
public Marker(@Nullable Track.Id trackId, @NonNull TrackPoint trackPoint) {
|
||||
this.trackId = trackId;
|
||||
|
||||
if (!trackPoint.hasLocation())
|
||||
throw new RuntimeException("Marker requires a trackpoint with a location.");
|
||||
|
||||
this.position = trackPoint.getPosition();
|
||||
}
|
||||
|
||||
public Marker(@Nullable Track.Id trackId, @NonNull Position position) {
|
||||
this.trackId = trackId;
|
||||
Objects.requireNonNull(position);
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public Marker(@Nullable Track.Id trackId, @NonNull TrackPoint trackPoint, String name, String description, String category, String icon, Uri photoUrl) {
|
||||
this(trackId, trackPoint);
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.category = category;
|
||||
this.icon = icon;
|
||||
this.photoUrl = photoUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* May be null if the Marker was not loaded from the database.
|
||||
*/
|
||||
@Nullable
|
||||
public Id getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Id id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Instant getTime() {
|
||||
return position.time();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public String getIcon() {
|
||||
return icon;
|
||||
}
|
||||
|
||||
public void setIcon(String icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
public Track.Id getTrackId() {
|
||||
return trackId;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void setTrackId(@NonNull Track.Id trackId) {
|
||||
this.trackId = trackId;
|
||||
}
|
||||
|
||||
public Position getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public boolean hasAccuracy() {
|
||||
return position.hasHorizontalAccuracy();
|
||||
}
|
||||
|
||||
public Distance getAccuracy() {
|
||||
return position.horizontalAccuracy();
|
||||
}
|
||||
|
||||
public boolean hasAltitude() {
|
||||
return position.hasAltitude();
|
||||
}
|
||||
|
||||
public Altitude getAltitude() {
|
||||
return position.altitude();
|
||||
}
|
||||
|
||||
public boolean hasBearing() {
|
||||
return position.hasBearing();
|
||||
}
|
||||
|
||||
public Float getBearing() {
|
||||
return position.bearing();
|
||||
}
|
||||
|
||||
public Uri getPhotoUrl() {
|
||||
return photoUrl;
|
||||
}
|
||||
|
||||
public void setPhotoUrl(Uri photoUrl) {
|
||||
this.photoUrl = photoUrl;
|
||||
}
|
||||
|
||||
public boolean hasPhoto() {
|
||||
return photoUrl != null;
|
||||
}
|
||||
//Some data might not be used.
|
||||
Position position,
|
||||
|
||||
@Nullable
|
||||
Uri photoUrl
|
||||
) {
|
||||
public record Id(long id) implements Parcelable {
|
||||
|
||||
@NonNull
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* Copyright 2009 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
|
||||
package de.dennisguse.opentracks.data.models;
|
||||
|
||||
import android.net.Uri;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* NOTE: A marker is indirectly (via it's {@link Position}) assigned to one {@link TrackPoint} via position.time.
|
||||
*
|
||||
* @author Leif Hendrik Wilden
|
||||
* @author Rodrigo Damazio
|
||||
*/
|
||||
//TODO All data should be final; no default values.
|
||||
public final class MarkerBuilder {
|
||||
|
||||
private Marker.Id id;
|
||||
private String name;
|
||||
private String description;
|
||||
private String category;
|
||||
private String icon;
|
||||
private final Track.Id trackId;
|
||||
|
||||
//Some data might not be used.
|
||||
private final Position position;
|
||||
|
||||
private Uri photoUrl = null;
|
||||
|
||||
@VisibleForTesting
|
||||
public MarkerBuilder(@Nullable Track.Id trackId, @NonNull TrackPoint trackPoint) {
|
||||
this.id = null;
|
||||
this.trackId = trackId;
|
||||
this.name = "";
|
||||
this.description = "";
|
||||
this.category = "";
|
||||
this.icon = "";
|
||||
this.photoUrl = null;
|
||||
|
||||
if (!trackPoint.hasLocation())
|
||||
throw new RuntimeException("Marker requires a trackpoint with a location.");
|
||||
|
||||
this.position = trackPoint.getPosition();
|
||||
}
|
||||
|
||||
public MarkerBuilder(@Nullable Track.Id trackId, @NonNull TrackPoint trackPoint, String name, String description, String category, String icon, Uri photoUrl) {
|
||||
this(trackId, trackPoint);
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.category = category;
|
||||
this.icon = icon;
|
||||
this.photoUrl = photoUrl;
|
||||
}
|
||||
|
||||
public MarkerBuilder(Marker marker) {
|
||||
this.id = marker.id();
|
||||
this.trackId = marker.trackId();
|
||||
this.name = marker.name();
|
||||
this.description = marker.description();
|
||||
this.category = marker.category();
|
||||
this.icon = marker.icon();
|
||||
this.position = marker.position();
|
||||
this.photoUrl = marker.photoUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* May be null if the Marker was not loaded from the database.
|
||||
*/
|
||||
@Nullable
|
||||
public Marker.Id getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
//TODO Remove
|
||||
@VisibleForTesting
|
||||
@Deprecated
|
||||
public void setId(Marker.Id id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public void setCategory(String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
public void setIcon(String icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
public Track.Id getTrackId() {
|
||||
return trackId;
|
||||
}
|
||||
|
||||
public Uri getPhotoUrl() {
|
||||
return photoUrl;
|
||||
}
|
||||
|
||||
public void setPhotoUrl(Uri photoUrl) {
|
||||
this.photoUrl = photoUrl;
|
||||
}
|
||||
|
||||
public boolean hasPhoto() {
|
||||
return photoUrl != null;
|
||||
}
|
||||
|
||||
public Marker getMarker() {
|
||||
return new Marker(
|
||||
id,
|
||||
trackId,
|
||||
name,
|
||||
description,
|
||||
category,
|
||||
icon,
|
||||
position,
|
||||
photoUrl
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -39,13 +39,14 @@ public interface MarkerColumns extends BaseColumns {
|
||||
String NAME = "name"; // marker name
|
||||
String DESCRIPTION = "description"; // marker description
|
||||
String CATEGORY = "category"; // marker category
|
||||
@Deprecated //TODO Remove: it is always: position
|
||||
String ICON = "icon"; // marker icon
|
||||
String TRACKID = "trackid"; // track id
|
||||
String LONGITUDE = "longitude"; // longitude
|
||||
String LATITUDE = "latitude"; // latitude
|
||||
String TIME = "time"; // time
|
||||
String ALTITUDE = "elevation"; // altitude //TODO RENAME column
|
||||
String ACCURACY = "accuracy"; // accuracy
|
||||
String HORIZONTAL_ACCURACY = "accuracy"; // accuracy
|
||||
String BEARING = "bearing"; // bearing
|
||||
|
||||
String PHOTOURL = "photoUrl"; // url for the photo
|
||||
@@ -61,7 +62,7 @@ public interface MarkerColumns extends BaseColumns {
|
||||
+ LATITUDE + " INTEGER, "
|
||||
+ TIME + " INTEGER, "
|
||||
+ ALTITUDE + " FLOAT, "
|
||||
+ ACCURACY + " FLOAT, "
|
||||
+ HORIZONTAL_ACCURACY + " FLOAT, "
|
||||
+ BEARING + " FLOAT, "
|
||||
+ PHOTOURL + " TEXT, "
|
||||
+ "FOREIGN KEY (" + TRACKID + ") REFERENCES " + TracksColumns.TABLE_NAME + "(" + TracksColumns._ID + ") ON UPDATE CASCADE ON DELETE CASCADE"
|
||||
|
||||
Reference in New Issue
Block a user