Export and import markers to file

This commit is contained in:
Vitaliy 2020-11-09 10:31:17 +02:00
parent 4d3ef1fa91
commit 460980a475
3 changed files with 116 additions and 21 deletions

View file

@ -481,7 +481,7 @@ public class MapMarkersDialogFragment extends DialogFragment implements OnGroupS
@Override
public void saveGpx(final String fileName) {
final String gpxPath = mapActivity.getMyApplication().getMapMarkersHelper().generateGpx(fileName);
final String gpxPath = mapActivity.getMyApplication().getMapMarkersHelper().saveMarkersToFile(fileName);
snackbar = Snackbar.make(viewPager, String.format(getString(R.string.shared_string_file_is_saved), fileName) + ".", Snackbar.LENGTH_LONG)
.setAction(R.string.shared_string_show, new View.OnClickListener() {
@Override

View file

@ -5,6 +5,7 @@ import android.os.AsyncTask;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import net.osmand.FileUtils;
import net.osmand.GPXUtilities;
@ -58,6 +59,10 @@ public class MapMarkersHelper {
public static final int BY_DATE_ADDED_ASC = 4;
public static final String GROUP_NAME = "group_name";
public static final String GROUP_TYPE = "group_type";
public static final String MARKER_HISTORY = "marker_history";
private static final Log LOG = PlatformUtil.getLog(MapMarkersHelper.class);
@Retention(RetentionPolicy.SOURCE)
@ -303,7 +308,7 @@ public class MapMarkersHelper {
}
public MapMarkersGroup getMarkersGroup(GPXFile gpx) {
if(gpx == null || gpx.path == null) {
if (gpx == null || gpx.path == null) {
return null;
}
return getMapMarkerGroupById(getMarkerGroupId(new File(gpx.path)), MapMarkersGroup.GPX_TYPE);
@ -347,7 +352,7 @@ public class MapMarkersHelper {
public void enableGroup(@NonNull MapMarkersGroup gr) {
// check if group doesn't exist internally
if(!mapMarkersGroups.contains(gr)) {
if (!mapMarkersGroups.contains(gr)) {
addGroupInternally(gr);
}
if (gr.isDisabled()) {
@ -558,12 +563,12 @@ public class MapMarkersHelper {
public List<MapMarkersGroup> getGroupsForSavedArticlesTravelBook() {
List<MapMarkersGroup> res = new ArrayList<>();
TravelDbHelper travelDbHelper = ctx.getTravelDbHelper();
if(travelDbHelper.getSelectedTravelBook() != null) {
if (travelDbHelper.getSelectedTravelBook() != null) {
List<TravelArticle> savedArticles = travelDbHelper.getLocalDataHelper().getSavedArticles();
for (TravelArticle art : savedArticles) {
String gpxName = travelDbHelper.getGPXName(art);
File path = ctx.getAppPath(IndexConstants.GPX_TRAVEL_DIR + gpxName);
LOG.debug("Article group " + path.getAbsolutePath() + " " + path.exists()) ;
LOG.debug("Article group " + path.getAbsolutePath() + " " + path.exists());
MapMarkersGroup search = getMapMarkerGroupById(getMarkerGroupId(path), MapMarkersGroup.GPX_TYPE);
if (search == null) {
MapMarkersGroup group = createGPXMarkerGroup(path);
@ -1001,7 +1006,8 @@ public class MapMarkersHelper {
});
}
public String generateGpx(String fileName) {
public String saveMarkersToFile(String fileName) {
GPXFile gpxFile = generateGpx();
String dirName = IndexConstants.GPX_INDEX_DIR + IndexConstants.MAP_MARKERS_INDEX_DIR;
File dir = ctx.getAppPath(dirName);
if (!dir.exists()) {
@ -1009,18 +1015,32 @@ public class MapMarkersHelper {
}
String uniqueFileName = FileUtils.createUniqueFileName(ctx, fileName, dirName, IndexConstants.GPX_FILE_EXT);
File fout = new File(dir, uniqueFileName + IndexConstants.GPX_FILE_EXT);
GPXUtilities.writeGpxFile(fout, gpxFile);
GPXFile file = new GPXFile(Version.getFullVersion(ctx));
for (MapMarker marker : mapMarkers) {
return fout.getAbsolutePath();
}
public GPXFile generateGpx() {
return generateGpx(mapMarkers, false);
}
public GPXFile generateGpx(List<MapMarker> markers, boolean completeBackup) {
GPXFile gpxFile = new GPXFile(Version.getFullVersion(ctx));
for (MapMarker marker : markers) {
WptPt wpt = new WptPt();
wpt.lat = marker.getLatitude();
wpt.lon = marker.getLongitude();
wpt.setColor(ctx.getResources().getColor(MapMarker.getColorId(marker.colorIndex)));
wpt.name = marker.getOnlyName();
file.addPoint(wpt);
wpt.setColor(ContextCompat.getColor(ctx, MapMarker.getColorId(marker.colorIndex)));
if (completeBackup) {
wpt.category = marker.groupKey;
wpt.getExtensionsToWrite().put(GROUP_NAME, marker.groupName);
wpt.getExtensionsToWrite().put(GROUP_TYPE, String.valueOf(marker.getType()));
wpt.getExtensionsToWrite().put(MARKER_HISTORY, String.valueOf(marker.history));
}
gpxFile.addPoint(wpt);
}
GPXUtilities.writeGpxFile(fout, file);
return fout.getAbsolutePath();
return gpxFile;
}
// ---------------------------------------------------------------------------------------------

View file

@ -7,28 +7,37 @@ import androidx.annotation.Nullable;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.plus.mapmarkers.MapMarkersHelper;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.mapmarkers.MapMarker;
import net.osmand.plus.mapmarkers.MapMarkersDbHelper;
import net.osmand.plus.mapmarkers.MapMarkersGroup;
import net.osmand.plus.mapmarkers.MapMarkersHelper;
import net.osmand.util.Algorithms;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static net.osmand.IndexConstants.GPX_FILE_EXT;
import static net.osmand.plus.mapmarkers.MapMarkersHelper.GROUP_NAME;
import static net.osmand.plus.mapmarkers.MapMarkersHelper.GROUP_TYPE;
public class MarkersSettingsItem extends CollectionSettingsItem<MapMarkersGroup> {
private MapMarkersHelper markersHelper;
private Map<String, MapMarkersGroup> flatGroups = new LinkedHashMap<>();
public MarkersSettingsItem(@NonNull OsmandApplication app, @NonNull List<MapMarkersGroup> items) {
super(app, null, items);
@ -85,9 +94,20 @@ public class MarkersSettingsItem extends CollectionSettingsItem<MapMarkersGroup>
for (MapMarkersGroup duplicate : duplicateItems) {
if (shouldReplace) {
MapMarkersGroup existingGroup = markersHelper.getMapMarkerGroupById(duplicate.getId(), duplicate.getType());
if (existingGroup != null) {
List<MapMarker> existingMarkers = new ArrayList<>(existingGroup.getMarkers());
for (MapMarker marker : existingMarkers) {
markersHelper.removeMarker(marker);
}
}
}
appliedItems.add(duplicate);
}
for (MapMarkersGroup markersGroup : appliedItems) {
for (MapMarker marker : markersGroup.getMarkers()) {
markersHelper.addMapMarker(marker.point, marker.getOriginalPointDescription());
}
appliedItems.add(shouldReplace ? duplicate : renameItem(duplicate));
}
}
}
@ -96,7 +116,9 @@ public class MarkersSettingsItem extends CollectionSettingsItem<MapMarkersGroup>
public boolean isDuplicate(@NonNull MapMarkersGroup markersGroup) {
String name = markersGroup.getName();
for (MapMarkersGroup group : existingItems) {
if (Algorithms.stringsEqual(group.getName(), name)) {
if (Algorithms.stringsEqual(group.getName(), name)
&& !Algorithms.isEmpty(group.getMarkers())
&& !Algorithms.isEmpty(markersGroup.getMarkers())) {
return true;
}
}
@ -120,18 +142,62 @@ public class MarkersSettingsItem extends CollectionSettingsItem<MapMarkersGroup>
return new SettingsItemReader<MarkersSettingsItem>(this) {
@Override
public void readFromStream(@NonNull InputStream inputStream, File destination) throws IOException, IllegalArgumentException {
public void readFromStream(@NonNull InputStream inputStream, String entryName) throws IllegalArgumentException {
GPXFile gpxFile = GPXUtilities.loadGPXFile(inputStream);
if (gpxFile.error != null) {
warnings.add(app.getString(R.string.settings_item_read_error, String.valueOf(getType())));
SettingsHelper.LOG.error("Failed read gpx file", gpxFile.error);
} else {
Map<String, MapMarkersGroup> flatGroups = new LinkedHashMap<>();
List<Integer> markerColors = getMarkersColors();
for (WptPt point : gpxFile.getPoints()) {
LatLon latLon = new LatLon(point.lat, point.lon);
int colorIndex = markerColors.indexOf(point.getColor());
if (colorIndex == -1) {
colorIndex = 0;
}
PointDescription pointDescription = new PointDescription(PointDescription.POINT_TYPE_LOCATION, point.name);
MapMarker marker = new MapMarker(latLon, pointDescription, colorIndex, false, 0);
marker.nextKey = MapMarkersDbHelper.TAIL_NEXT_VALUE;
MapMarkersGroup group = getOrCreateGroup(point);
group.getMarkers().add(marker);
}
}
}
};
}
private MapMarkersGroup getOrCreateGroup(WptPt point) {
MapMarkersGroup markersGroup = flatGroups.get(point.category);
if (markersGroup != null) {
return markersGroup;
}
Map<String, String> extensions = point.getExtensionsToRead();
String groupName = extensions.get(GROUP_NAME);
String groupType = extensions.get(GROUP_TYPE);
int type = Algorithms.parseIntSilently(groupType, MapMarkersGroup.ANY_TYPE);
if (point.category != null && groupName != null) {
markersGroup = new MapMarkersGroup(point.category, groupName, type);
} else {
markersGroup = new MapMarkersGroup();
}
flatGroups.put(markersGroup.getId(), markersGroup);
items.add(markersGroup);
return markersGroup;
}
private List<Integer> getMarkersColors() {
List<Integer> colors = new ArrayList<>();
for (int color : MapMarker.getColors(app)) {
colors.add(color);
}
return colors;
}
@Nullable
@Override
SettingsItemWriter<MarkersSettingsItem> getWriter() {
@ -139,7 +205,8 @@ public class MarkersSettingsItem extends CollectionSettingsItem<MapMarkersGroup>
@Override
public boolean writeToStream(@NonNull OutputStream outputStream) throws IOException {
GPXFile gpxFile = generateGpx();
List<MapMarker> mapMarkers = getMarkersFromGroups(items);
GPXFile gpxFile = markersHelper.generateGpx(mapMarkers, true);
Exception error = GPXUtilities.writeGpx(new OutputStreamWriter(outputStream, "UTF-8"), gpxFile);
if (error != null) {
warnings.add(app.getString(R.string.settings_item_write_error, String.valueOf(getType())));
@ -150,4 +217,12 @@ public class MarkersSettingsItem extends CollectionSettingsItem<MapMarkersGroup>
}
};
}
private List<MapMarker> getMarkersFromGroups(List<MapMarkersGroup> markersGroups) {
List<MapMarker> mapMarkers = new ArrayList<>();
for (MapMarkersGroup group : markersGroups) {
mapMarkers.addAll(group.getMarkers());
}
return mapMarkers;
}
}