Merge pull request #10168 from osmandapp/markers_backup

Markers backup fixes
This commit is contained in:
alex-osm 2020-11-12 17:58:52 +03:00 committed by GitHub
commit 35b6c9f313
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 9 deletions

View file

@ -55,7 +55,7 @@ public class GPXUtilities {
private static final String GAP_PROFILE_TYPE = "gap"; private static final String GAP_PROFILE_TYPE = "gap";
private static final String TRKPT_INDEX_EXTENSION = "trkpt_idx"; private static final String TRKPT_INDEX_EXTENSION = "trkpt_idx";
private final static String GPX_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; //$NON-NLS-1$ public final static String GPX_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; //$NON-NLS-1$
private final static String GPX_TIME_FORMAT_MILLIS = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; //$NON-NLS-1$ private final static String GPX_TIME_FORMAT_MILLIS = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; //$NON-NLS-1$
private final static NumberFormat latLonFormat = new DecimalFormat("0.00#####", new DecimalFormatSymbols( private final static NumberFormat latLonFormat = new DecimalFormat("0.00#####", new DecimalFormatSymbols(

View file

@ -35,17 +35,23 @@ import org.apache.commons.logging.Log;
import java.io.File; import java.io.File;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import static net.osmand.GPXUtilities.GPX_TIME_FORMAT;
import static net.osmand.data.PointDescription.POINT_TYPE_MAP_MARKER; import static net.osmand.data.PointDescription.POINT_TYPE_MAP_MARKER;
public class MapMarkersHelper { public class MapMarkersHelper {
@ -1005,6 +1011,20 @@ public class MapMarkersHelper {
}); });
} }
public List<MapMarker> getMapMarkersFromDefaultGroups(boolean history) {
List<MapMarker> mapMarkers = new ArrayList<>();
for (MapMarkersGroup group : mapMarkersGroups) {
if (group.getType() == MapMarkersGroup.ANY_TYPE) {
for (MapMarker marker : group.getMarkers()) {
if (history && marker.history || !history && !marker.history) {
mapMarkers.add(marker);
}
}
}
}
return mapMarkers;
}
public String saveMarkersToFile(String fileName) { public String saveMarkersToFile(String fileName) {
GPXFile gpxFile = generateGpx(); GPXFile gpxFile = generateGpx();
String dirName = IndexConstants.GPX_INDEX_DIR + IndexConstants.MAP_MARKERS_INDEX_DIR; String dirName = IndexConstants.GPX_INDEX_DIR + IndexConstants.MAP_MARKERS_INDEX_DIR;
@ -1024,6 +1044,9 @@ public class MapMarkersHelper {
} }
public GPXFile generateGpx(List<MapMarker> markers, boolean completeBackup) { public GPXFile generateGpx(List<MapMarker> markers, boolean completeBackup) {
SimpleDateFormat format = new SimpleDateFormat(GPX_TIME_FORMAT, Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
GPXFile gpxFile = new GPXFile(Version.getFullVersion(ctx)); GPXFile gpxFile = new GPXFile(Version.getFullVersion(ctx));
for (MapMarker marker : markers) { for (MapMarker marker : markers) {
WptPt wpt = new WptPt(); WptPt wpt = new WptPt();
@ -1033,10 +1056,10 @@ public class MapMarkersHelper {
wpt.setColor(ContextCompat.getColor(ctx, MapMarker.getColorId(marker.colorIndex))); wpt.setColor(ContextCompat.getColor(ctx, MapMarker.getColorId(marker.colorIndex)));
if (completeBackup) { if (completeBackup) {
if (marker.creationDate != 0) { if (marker.creationDate != 0) {
wpt.getExtensionsToWrite().put(CREATION_DATE, String.valueOf(marker.creationDate)); wpt.getExtensionsToWrite().put(CREATION_DATE, format.format(new Date(marker.creationDate)));
} }
if (marker.visitedDate != 0) { if (marker.visitedDate != 0) {
wpt.getExtensionsToWrite().put(VISITED_DATE, String.valueOf(marker.visitedDate)); wpt.getExtensionsToWrite().put(VISITED_DATE, format.format(new Date(marker.visitedDate)));
} }
} }
gpxFile.addPoint(wpt); gpxFile.addPoint(wpt);
@ -1045,6 +1068,9 @@ public class MapMarkersHelper {
} }
public List<MapMarker> readMarkersFromGpx(GPXFile gpxFile, boolean history) { public List<MapMarker> readMarkersFromGpx(GPXFile gpxFile, boolean history) {
SimpleDateFormat format = new SimpleDateFormat(GPX_TIME_FORMAT, Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
List<MapMarker> mapMarkers = new ArrayList<>(); List<MapMarker> mapMarkers = new ArrayList<>();
for (WptPt point : gpxFile.getPoints()) { for (WptPt point : gpxFile.getPoints()) {
LatLon latLon = new LatLon(point.lat, point.lon); LatLon latLon = new LatLon(point.lat, point.lon);
@ -1055,8 +1081,8 @@ public class MapMarkersHelper {
String visitedDateStr = point.getExtensionsToRead().get(VISITED_DATE); String visitedDateStr = point.getExtensionsToRead().get(VISITED_DATE);
String creationDateStr = point.getExtensionsToRead().get(CREATION_DATE); String creationDateStr = point.getExtensionsToRead().get(CREATION_DATE);
marker.visitedDate = Algorithms.parseLongSilently(visitedDateStr, 0); marker.visitedDate = parseTime(visitedDateStr, format);
marker.creationDate = Algorithms.parseLongSilently(creationDateStr, 0); marker.creationDate = parseTime(creationDateStr, format);
marker.nextKey = history ? MapMarkersDbHelper.HISTORY_NEXT_VALUE : MapMarkersDbHelper.TAIL_NEXT_VALUE; marker.nextKey = history ? MapMarkersDbHelper.HISTORY_NEXT_VALUE : MapMarkersDbHelper.TAIL_NEXT_VALUE;
mapMarkers.add(marker); mapMarkers.add(marker);
@ -1064,6 +1090,18 @@ public class MapMarkersHelper {
return mapMarkers; return mapMarkers;
} }
private static long parseTime(String text, SimpleDateFormat format) {
long time = 0;
if (text != null) {
try {
time = format.parse(text).getTime();
} catch (ParseException e) {
LOG.error(e);
}
}
return time;
}
// --------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------
// accessors to active markers: // accessors to active markers:

View file

@ -46,7 +46,7 @@ public class HistoryMarkersSettingsItem extends CollectionSettingsItem<MapMarker
protected void init() { protected void init() {
super.init(); super.init();
markersHelper = app.getMapMarkersHelper(); markersHelper = app.getMapMarkersHelper();
existingItems = new ArrayList<>(markersHelper.getMapMarkersHistory()); existingItems = new ArrayList<>(markersHelper.getMapMarkersFromDefaultGroups(true));
} }
@NonNull @NonNull

View file

@ -46,7 +46,7 @@ public class MarkersSettingsItem extends CollectionSettingsItem<MapMarker> {
protected void init() { protected void init() {
super.init(); super.init();
markersHelper = app.getMapMarkersHelper(); markersHelper = app.getMapMarkersHelper();
existingItems = new ArrayList<>(markersHelper.getMapMarkers()); existingItems = new ArrayList<>(markersHelper.getMapMarkersFromDefaultGroups(false));
} }
@NonNull @NonNull

View file

@ -597,7 +597,7 @@ public class SettingsHelper {
if (!files.isEmpty()) { if (!files.isEmpty()) {
dataList.put(ExportSettingsType.VOICE, files); dataList.put(ExportSettingsType.VOICE, files);
} }
List<MapMarker> mapMarkers = app.getMapMarkersHelper().getMapMarkers(); List<MapMarker> mapMarkers = app.getMapMarkersHelper().getMapMarkersFromDefaultGroups(false);
if (!mapMarkers.isEmpty()) { if (!mapMarkers.isEmpty()) {
String name = app.getString(R.string.map_markers); String name = app.getString(R.string.map_markers);
String groupId = ExportSettingsType.ACTIVE_MARKERS.name(); String groupId = ExportSettingsType.ACTIVE_MARKERS.name();
@ -605,7 +605,7 @@ public class SettingsHelper {
markersGroup.setMarkers(mapMarkers); markersGroup.setMarkers(mapMarkers);
dataList.put(ExportSettingsType.ACTIVE_MARKERS, Collections.singletonList(markersGroup)); dataList.put(ExportSettingsType.ACTIVE_MARKERS, Collections.singletonList(markersGroup));
} }
List<MapMarker> markersHistory = app.getMapMarkersHelper().getMapMarkersHistory(); List<MapMarker> markersHistory = app.getMapMarkersHelper().getMapMarkersFromDefaultGroups(true);
if (!markersHistory.isEmpty()) { if (!markersHistory.isEmpty()) {
String name = app.getString(R.string.shared_string_history); String name = app.getString(R.string.shared_string_history);
String groupId = ExportSettingsType.HISTORY_MARKERS.name(); String groupId = ExportSettingsType.HISTORY_MARKERS.name();