Cleanup: Marker stores URI to photo (instead of String).

This commit is contained in:
Dennis Guse
2025-02-21 22:50:24 +01:00
parent b9ed5e28e0
commit 1b33184835
17 changed files with 45 additions and 78 deletions
@@ -418,7 +418,7 @@ public class ContentProviderUtils {
marker.setIcon(cursor.getString(iconIndex));
}
if (!cursor.isNull(photoUrlIndex)) {
marker.setPhotoUrl(cursor.getString(photoUrlIndex));
marker.setPhotoUrl(Uri.parse(cursor.getString(photoUrlIndex)));
}
return marker;
}
@@ -496,7 +496,7 @@ public class ContentProviderUtils {
private void deleteMarkerPhoto(Context context, Marker marker) {
if (marker != null && marker.hasPhoto()) {
Uri uri = marker.getPhotoURI();
Uri uri = marker.getPhotoUrl();
File file = MarkerUtils.buildInternalPhotoFile(context, marker.getTrackId(), uri);
if (file.exists()) {
File parent = file.getParentFile();
@@ -545,7 +545,9 @@ public class ContentProviderUtils {
values.put(MarkerColumns.BEARING, marker.getBearing());
}
values.put(MarkerColumns.PHOTOURL, marker.getPhotoUrl());
if (marker.hasPhoto()) {
values.put(MarkerColumns.PHOTOURL, marker.getPhotoUrl().toString());
}
return values;
}
@@ -24,7 +24,6 @@ import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.time.Duration;
import java.time.Instant;
/**
@@ -51,8 +50,7 @@ public final class Marker {
private Altitude altitude;
private Float bearing;
@Deprecated //TODO Make an URI instead of String
private String photoUrl = "";
private Uri photoUrl = null;
public Marker(@Nullable Track.Id trackId, Instant time) {
this.trackId = trackId;
@@ -71,7 +69,7 @@ public final class Marker {
}
@Deprecated
public Marker(String name, String description, String category, String icon, @NonNull Track.Id trackId, @NonNull TrackPoint trackPoint, String photoUrl) {
public Marker(String name, String description, String category, String icon, @NonNull Track.Id trackId, @NonNull TrackPoint trackPoint, Uri photoUrl) {
this(trackId, trackPoint);
this.name = name;
this.description = description;
@@ -222,20 +220,16 @@ public final class Marker {
this.bearing = bearing;
}
public String getPhotoUrl() {
public Uri getPhotoUrl() {
return photoUrl;
}
public void setPhotoUrl(String photoUrl) {
public void setPhotoUrl(Uri photoUrl) {
this.photoUrl = photoUrl;
}
public Uri getPhotoURI() {
return Uri.parse(photoUrl);
}
public boolean hasPhoto() {
return photoUrl != null && !photoUrl.isEmpty();
return photoUrl != null;
}
public record Id(long id) implements Parcelable {
@@ -270,7 +270,7 @@ public class KMLTrackExporter implements TrackExporter {
}
private void writeMarker(Marker marker, ZoneOffset zoneOffset) {
boolean existsPhoto = MarkerUtils.buildInternalPhotoFile(context, marker.getTrackId(), marker.getPhotoURI()) != null;
boolean existsPhoto = MarkerUtils.buildInternalPhotoFile(context, marker.getTrackId(), marker.getPhotoUrl()) != null;
if (marker.hasPhoto() && exportPhotos && existsPhoto) {
float heading = getHeading(marker.getTrackId(), marker.getLocation());
writePhotoOverlay(marker, heading, zoneOffset);
@@ -97,7 +97,7 @@ public class KmzTrackExporter implements TrackExporter {
}
Marker marker = contentProviderUtils.createMarker(cursor);
if (marker.hasPhoto()) {
Uri uriPhoto = marker.getPhotoURI();
Uri uriPhoto = marker.getPhotoUrl();
boolean existsPhoto = MarkerUtils.buildInternalPhotoFile(context, track.getId(), uriPhoto) != null;
if (existsPhoto) {
addImage(context, zipOutputStream, uriPhoto, marker);
@@ -17,6 +17,7 @@
package de.dennisguse.opentracks.io.file.importer;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import org.xml.sax.Attributes;
@@ -109,7 +110,7 @@ public class GpxTrackImporter extends DefaultHandler implements XMLImporter.Trac
private String cadence;
private String power;
private String markerType;
private String photoUrl;
private Uri photoUrl;
private String uuid;
private String gain;
private String loss;
@@ -18,6 +18,7 @@ package de.dennisguse.opentracks.io.file.importer;
import android.content.Context;
import android.location.Location;
import android.net.Uri;
import android.util.Log;
import org.xml.sax.Attributes;
@@ -121,7 +122,7 @@ public class KmlTrackImporter extends DefaultHandler implements XMLImporter.Trac
private String longitude;
private String altitude;
private String markerType;
private String photoUrl;
private Uri photoUrl;
private String uuid;
private final TrackImporter trackImporter;
@@ -223,7 +224,7 @@ public class KmlTrackImporter extends DefaultHandler implements XMLImporter.Trac
}
case TAG_HREF -> {
if (content != null) {
photoUrl = content.trim();
photoUrl = Uri.parse(content.trim());
}
}
}
@@ -191,7 +191,7 @@ public class KmzTrackImporter {
List<String> photosName = new ArrayList<>();
for (Marker marker : markers) {
if (marker.hasPhoto()) {
String photoUrl = Uri.decode(marker.getPhotoUrl());
String photoUrl = Uri.decode(marker.getPhotoUrl().toString()); //TODO Why Uri.decode()?
photosName.add(photoUrl.substring(photoUrl.lastIndexOf(File.separatorChar) + 1));
}
}
@@ -233,12 +233,11 @@ public class TrackImporter {
*
* @param externalPhotoUrl the file name
*/
private String getInternalPhotoUrl(@NonNull Track.Id trackId, @NonNull String externalPhotoUrl) {
String importFileName = KmzTrackImporter.importNameForFilename(externalPhotoUrl);
private Uri getInternalPhotoUrl(@NonNull Track.Id trackId, @NonNull Uri externalPhotoUrl) {
String importFileName = KmzTrackImporter.importNameForFilename(externalPhotoUrl.toString());
File file = MarkerUtils.buildInternalPhotoFile(context, trackId, Uri.parse(importFileName));
if (file != null) {
Uri photoUri = FileUtils.getUriForFile(context, file);
return "" + photoUri;
return FileUtils.getUriForFile(context, file);
}
return null;
@@ -93,14 +93,14 @@ public class ShareUtils {
Log.e(TAG, "MarkerId " + markerId.id() + " could not be resolved.");
continue;
}
if (marker.getPhotoURI() == null) {
if (marker.getPhotoUrl() == null) {
Log.e(TAG, "MarkerId " + markerId.id() + " has no picture.");
continue;
}
mime = context.getContentResolver().getType(marker.getPhotoURI());
mime = context.getContentResolver().getType(marker.getPhotoUrl());
uris.add(marker.getPhotoURI());
uris.add(marker.getPhotoUrl());
}
if (uris.isEmpty()) {
@@ -235,7 +235,7 @@ public class MarkerDetailFragment extends Fragment {
boolean hasPhoto = marker.hasPhoto();
if (hasPhoto) {
handler.removeCallbacks(hideText);
viewBinding.markerDetailMarkerPhoto.setImageURI(marker.getPhotoURI());
viewBinding.markerDetailMarkerPhoto.setImageURI(marker.getPhotoUrl());
handler.postDelayed(hideText, HIDE_TEXT_DELAY.toMillis());
} else {
viewBinding.markerDetailMarkerPhoto.setImageResource(MarkerUtils.ICON_ID);
@@ -183,7 +183,7 @@ public class MarkerEditActivity extends AbstractActivity {
viewBinding.markerEditMarkerType.setText(marker.getCategory());
viewBinding.markerEditDescription.setText(marker.getDescription());
if (marker.hasPhoto()) {
setMarkerImageView(marker.getPhotoURI());
setMarkerImageView(marker.getPhotoUrl());
} else {
viewBinding.markerEditPhoto.setImageDrawable(null);
}
@@ -41,7 +41,7 @@ public class MarkerEditViewModel extends AndroidViewModel {
Marker marker = new ContentProviderUtils(getApplication()).getMarker(markerId);
if (marker.hasPhoto()) {
photoOriginalUri = marker.getPhotoURI();
photoOriginalUri = marker.getPhotoUrl();
}
markerData.postValue(marker);
@@ -71,15 +71,15 @@ public class MarkerEditViewModel extends AndroidViewModel {
private void deletePhoto(Marker marker) {
if (marker.hasPhoto()) {
deletePhoto(marker.getPhotoURI());
deletePhoto(marker.getPhotoUrl());
}
}
public void onPhotoDelete(String name, String category, String description) {
Marker marker = getMarker();
if (marker.hasPhoto()) {
if (!marker.getPhotoURI().equals(photoOriginalUri)) {
deletePhoto(marker.getPhotoURI());
if (!marker.getPhotoUrl().equals(photoOriginalUri)) {
deletePhoto(marker.getPhotoUrl());
}
marker.setPhotoUrl(null);
marker.setName(name);
@@ -91,7 +91,7 @@ public class MarkerEditViewModel extends AndroidViewModel {
public void onNewCameraPhoto(@NonNull Uri photoUri, String name, String category, String description) {
Marker marker = getMarker();
marker.setPhotoUrl(photoUri.toString());
marker.setPhotoUrl(photoUri);
marker.setName(name);
marker.setCategory(category);
marker.setDescription(description);
@@ -107,7 +107,7 @@ public class MarkerEditViewModel extends AndroidViewModel {
FileUtils.copy(srcFd, dstFile);
Uri photoUri = FileUtils.getUriForFile(getApplication(), dstFile);
marker.setPhotoUrl(photoUri.toString());
marker.setPhotoUrl(photoUri);
marker.setName(name);
marker.setCategory(category);
marker.setDescription(description);
@@ -130,7 +130,7 @@ public class MarkerEditViewModel extends AndroidViewModel {
new ContentProviderUtils(getApplication()).updateMarker(getApplication(), marker);
}
if (photoOriginalUri != null && (!marker.hasPhoto() || !photoOriginalUri.equals(marker.getPhotoURI()))) {
if (photoOriginalUri != null && (!marker.hasPhoto() || !photoOriginalUri.equals(marker.getPhotoUrl()))) {
deletePhoto(photoOriginalUri);
}
}
@@ -143,7 +143,7 @@ public class MarkerEditViewModel extends AndroidViewModel {
String name = getApplication().getString(R.string.marker_name_format, nextMarkerNumber + 1);
String icon = getApplication().getString(R.string.marker_icon_url);
Marker marker = new Marker(name, "", "", icon, trackId, trackPoint, "");
Marker marker = new Marker(name, "", "", icon, trackId, trackPoint, null);
if (markerData == null) {
markerData = new MutableLiveData<>();
@@ -158,7 +158,7 @@ public class MarkerEditViewModel extends AndroidViewModel {
// it's new marker -> clean all photos.
deletePhoto(marker);
deletePhoto(photoOriginalUri);
} else if (photoOriginalUri == null || (marker.hasPhoto() && !marker.getPhotoURI().equals(photoOriginalUri))) {
} else if (photoOriginalUri == null || (marker.hasPhoto() && !marker.getPhotoUrl().equals(photoOriginalUri))) {
// it's an edit marker -> delete photo if it was empty or it was changed (leaving the original in that case).
deletePhoto(marker);
}
@@ -190,7 +190,7 @@ public class MarkerListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHol
viewBinding.markerListItemPhoto.setLayoutParams(params);
viewBinding.markerListItemTextGradient.setLayoutParams(params);
asyncLoadPhoto(viewBinding.markerListItemPhoto, marker.getPhotoURI(), marker.getId());
asyncLoadPhoto(viewBinding.markerListItemPhoto, marker.getPhotoUrl(), marker.getId());
}
viewBinding.markerListItemName.setText(marker.getName());
@@ -109,6 +109,10 @@ public class FileUtils {
return getExtension(file.getName());
}
public static String getExtension(Uri uri) {
return getExtension(uri.toString());
}
/**
* Builds a filename with the given base and the given extension, possibly adding a suffix to ensure the file doesn't exist.
*