diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/ExportSettingsType.java b/OsmAnd/src/net/osmand/plus/settings/backend/ExportSettingsType.java index 91d5493217..a530ef5dcf 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/ExportSettingsType.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/ExportSettingsType.java @@ -16,5 +16,6 @@ public enum ExportSettingsType { OFFLINE_MAPS, FAVORITES, TTS_VOICE, - VOICE + VOICE, + MARKERS } diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/MarkersSettingsItem.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/MarkersSettingsItem.java new file mode 100644 index 0000000000..0d1f9d6bb8 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/MarkersSettingsItem.java @@ -0,0 +1,152 @@ +package net.osmand.plus.settings.backend.backup; + +import android.content.Context; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import net.osmand.GPXUtilities; +import net.osmand.GPXUtilities.GPXFile; +import net.osmand.plus.MapMarkersHelper; +import net.osmand.plus.MapMarkersHelper.MapMarkersGroup; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; + +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.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import static net.osmand.IndexConstants.GPX_FILE_EXT; + +public class MarkersSettingsItem extends CollectionSettingsItem { + + private MapMarkersHelper markersHelper; + + public MarkersSettingsItem(@NonNull OsmandApplication app, @NonNull List items) { + super(app, null, items); + } + + public MarkersSettingsItem(@NonNull OsmandApplication app, @Nullable MarkersSettingsItem baseItem, @NonNull List items) { + super(app, baseItem, items); + } + + MarkersSettingsItem(@NonNull OsmandApplication app, @NonNull JSONObject json) throws JSONException { + super(app, json); + } + + @Override + protected void init() { + super.init(); + markersHelper = app.getMapMarkersHelper(); + existingItems = new ArrayList<>(markersHelper.getMapMarkersGroups()); + } + + @NonNull + @Override + public SettingsItemType getType() { + return SettingsItemType.MARKERS; + } + + @NonNull + @Override + public String getName() { + return "markers"; + } + + @NonNull + @Override + public String getPublicName(@NonNull Context ctx) { + return ctx.getString(R.string.map_markers); + } + + @NonNull + public String getDefaultFileName() { + return getName() + getDefaultFileExtension(); + } + + @NonNull + public String getDefaultFileExtension() { + return GPX_FILE_EXT; + } + + @Override + public void apply() { + List newItems = getNewItems(); + if (!newItems.isEmpty() || !duplicateItems.isEmpty()) { + appliedItems = new ArrayList<>(newItems); + + for (MapMarkersGroup duplicate : duplicateItems) { + if (shouldReplace) { + + } + appliedItems.add(shouldReplace ? duplicate : renameItem(duplicate)); + } + } + } + + @Override + public boolean isDuplicate(@NonNull MapMarkersGroup markersGroup) { + String name = markersGroup.getName(); + for (MapMarkersGroup group : existingItems) { + if (group.getName().equals(name)) { + return true; + } + } + return false; + } + + @Override + public boolean shouldReadOnCollecting() { + return true; + } + + @NonNull + @Override + public MapMarkersGroup renameItem(@NonNull MapMarkersGroup item) { + return item; + } + + @Nullable + @Override + SettingsItemReader getReader() { + return new SettingsItemReader(this) { + + @Override + public void readFromStream(@NonNull InputStream inputStream, File destination) throws IOException, 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 flatGroups = new LinkedHashMap<>(); + } + } + }; + } + + @Nullable + @Override + SettingsItemWriter getWriter() { + return new SettingsItemWriter(this) { + + @Override + public boolean writeToStream(@NonNull OutputStream outputStream) throws IOException { +// 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()))); +// SettingsHelper.LOG.error("Failed write to gpx file", error); +// return false; +// } + return true; + } + }; + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java index f3821505a2..43e8fe22c2 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsHelper.java @@ -14,6 +14,7 @@ import net.osmand.map.ITileSource; import net.osmand.map.TileSourceManager; import net.osmand.map.TileSourceManager.TileSourceTemplate; import net.osmand.plus.FavouritesDbHelper.FavoriteGroup; +import net.osmand.plus.MapMarkersHelper.MapMarkersGroup; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.SQLiteTileSource; @@ -552,6 +553,10 @@ public class SettingsHelper { if (!files.isEmpty()) { dataList.put(ExportSettingsType.VOICE, files); } + List markersGroups = app.getMapMarkersHelper().getMapMarkersGroups(); + if (!markersGroups.isEmpty()) { + dataList.put(ExportSettingsType.MARKERS, markersGroups); + } return dataList; } @@ -591,6 +596,7 @@ public class SettingsHelper { List favoriteGroups = new ArrayList<>(); List osmNotesPointList = new ArrayList<>(); List osmEditsPointList = new ArrayList<>(); + List markersGroups = new ArrayList<>(); for (Object object : data) { if (object instanceof QuickAction) { @@ -615,6 +621,8 @@ public class SettingsHelper { osmEditsPointList.add((OpenstreetmapPoint) object); } else if (object instanceof FavoriteGroup) { favoriteGroups.add((FavoriteGroup) object); + } else if (object instanceof MapMarkersGroup) { + markersGroups.add((MapMarkersGroup) object); } } if (!quickActions.isEmpty()) { @@ -646,6 +654,9 @@ public class SettingsHelper { if (!favoriteGroups.isEmpty()) { settingsItems.add(new FavoritesSettingsItem(app, favoriteGroups)); } + if (!markersGroups.isEmpty()) { + settingsItems.add(new MarkersSettingsItem(app, markersGroups)); + } return settingsItems; } @@ -667,6 +678,7 @@ public class SettingsHelper { List notesPointList = new ArrayList<>(); List editsPointList = new ArrayList<>(); List favoriteGroups = new ArrayList<>(); + List markersGroups = new ArrayList<>(); for (SettingsItem item : settingsItems) { switch (item.getType()) { @@ -746,6 +758,10 @@ public class SettingsHelper { FavoritesSettingsItem favoritesSettingsItem = (FavoritesSettingsItem) item; favoriteGroups.addAll(favoritesSettingsItem.getItems()); break; + case MARKERS: + MarkersSettingsItem markersSettingsItem = (MarkersSettingsItem) item; + markersGroups.addAll(markersSettingsItem.getItems()); + break; default: break; } @@ -799,6 +815,9 @@ public class SettingsHelper { if (!voiceFilesList.isEmpty()) { settingsToOperate.put(ExportSettingsType.VOICE, voiceFilesList); } + if (!markersGroups.isEmpty()) { + settingsToOperate.put(ExportSettingsType.MARKERS, markersGroups); + } return settingsToOperate; } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsItemType.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsItemType.java index bb6b68b3ce..bc254e28ca 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsItemType.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsItemType.java @@ -15,5 +15,6 @@ public enum SettingsItemType { DOWNLOADS, OSM_NOTES, OSM_EDITS, - FAVOURITES + FAVOURITES, + MARKERS } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsItemsFactory.java b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsItemsFactory.java index 9669099003..3012cc3dd8 100644 --- a/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsItemsFactory.java +++ b/OsmAnd/src/net/osmand/plus/settings/backend/backup/SettingsItemsFactory.java @@ -134,6 +134,9 @@ class SettingsItemsFactory { case FAVOURITES: item = new FavoritesSettingsItem(app, json); break; + case MARKERS: + item = new MarkersSettingsItem(app, json); + break; } return item; } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/DuplicatesSettingsAdapter.java b/OsmAnd/src/net/osmand/plus/settings/fragments/DuplicatesSettingsAdapter.java index d90f4b7429..58af347031 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/DuplicatesSettingsAdapter.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/DuplicatesSettingsAdapter.java @@ -14,19 +14,20 @@ import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.map.ITileSource; import net.osmand.plus.FavouritesDbHelper.FavoriteGroup; -import net.osmand.plus.audionotes.AudioVideoNotesPlugin; -import net.osmand.plus.helpers.GpxUiHelper; -import net.osmand.plus.settings.backend.ApplicationMode; -import net.osmand.plus.settings.backend.ApplicationMode.ApplicationModeBean; +import net.osmand.plus.MapMarkersHelper.MapMarkersGroup; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; +import net.osmand.plus.audionotes.AudioVideoNotesPlugin; import net.osmand.plus.helpers.AvoidSpecificRoads.AvoidRoadInfo; +import net.osmand.plus.helpers.GpxUiHelper; import net.osmand.plus.poi.PoiUIFilter; import net.osmand.plus.profiles.ProfileIconColors; import net.osmand.plus.profiles.RoutingProfileDataObject.RoutingProfilesResources; import net.osmand.plus.quickaction.QuickAction; import net.osmand.plus.render.RenderingIcons; +import net.osmand.plus.settings.backend.ApplicationMode; +import net.osmand.plus.settings.backend.ApplicationMode.ApplicationModeBean; import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; @@ -134,7 +135,7 @@ public class DuplicatesSettingsAdapter extends RecyclerView.Adapter