From 6399c38bd9918686d1263f4c9fd3c96d5a17726a Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Fri, 15 Sep 2017 18:49:49 +0300 Subject: [PATCH] Add sync for GPXFile --- .../src/net/osmand/plus/AppInitializer.java | 2 +- .../src/net/osmand/plus/MapMarkersHelper.java | 117 ++++++++++++------ .../EditFavoriteGroupDialogFragment.java | 2 +- 3 files changed, 80 insertions(+), 41 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index ec14837769..901123e9f0 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -501,7 +501,7 @@ public class AppInitializer implements IProgress { startBgTime = System.currentTimeMillis(); app.favorites.loadFavorites(); notifyEvent(InitEvents.FAVORITES_INITIALIZED); - app.mapMarkersHelper.syncAllGroups(); + app.mapMarkersHelper.syncAllGroups(true); // init poi types before indexes and before POI initPoiTypes(); notifyEvent(InitEvents.POI_TYPES_INITIALIZED); diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 602e331c9c..3baadd86fd 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -10,6 +10,8 @@ import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.data.LocationPoint; import net.osmand.data.PointDescription; +import net.osmand.plus.GPXUtilities.GPXFile; +import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.mapmarkers.MapMarkersDbHelper; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; @@ -314,19 +316,23 @@ public class MapMarkersHelper { } } - public boolean isGroupSynced(MarkersSyncGroup group) { - return markersDbHelper.getGroup(group.getId()) != null; + public boolean isGroupSynced(String id) { + return markersDbHelper.getGroup(id) != null; } - public void syncAllGroups() { + public void syncAllGroups(boolean startupSync) { List groups = markersDbHelper.getAllGroups(); for (MarkersSyncGroup gr : groups) { - syncGroup(gr); + syncGroup(gr, startupSync); } } public void syncGroup(MarkersSyncGroup group) { - if (markersDbHelper.getGroup(group.getId()) == null) { + syncGroup(group, false); + } + + private void syncGroup(MarkersSyncGroup group, boolean startupSync) { + if (!isGroupSynced(group.getId())) { return; } List dbMarkers = markersDbHelper.getMarkersFromGroup(group); @@ -340,46 +346,79 @@ public class MapMarkersHelper { removeActiveMarkersFromSyncGroup(group.id); return; } - List favPoints = favGroup.points; - for (FavouritePoint fp : favPoints) { - LatLon fpLatLon = new LatLon(fp.getLatitude(), fp.getLongitude()); - boolean exists = false; - for (MapMarker marker : dbMarkers) { - if (marker.id.equals(group.getId() + fp.getName(ctx))) { - exists = true; - if (!marker.history && !marker.point.equals(fpLatLon)) { - for (MapMarker m : mapMarkers) { - if (m.id.equals(marker.id)) { - m.point = fpLatLon; - updateMapMarker(m, true); - break; - } - } + for (FavouritePoint fp : favGroup.points) { + addNewMarkerIfNeeded(group, dbMarkers, new LatLon(fp.getLatitude(), fp.getLongitude()), fp.getName()); + } + + removeOldMarkersIfNeeded(dbMarkers); + } else if (group.getType() == MarkersSyncGroup.GPX_TYPE) { + GpxSelectionHelper gpxHelper = ctx.getSelectedGpxHelper(); + File file = new File(group.getId()); + if (!file.exists()) { + return; + } + + GPXFile gpx; + if (startupSync) { + gpx = GPXUtilities.loadGPXFile(ctx, file); + gpxHelper.selectGpxFile(gpx, true, false); + } else { + gpx = gpxHelper.getSelectedFileByPath(group.getId()).getGpxFile(); + } + if (gpx == null) { + return; + } + + List gpxPoints = new LinkedList<>(gpx.getPoints()); + for (WptPt pt : gpxPoints) { + addNewMarkerIfNeeded(group, dbMarkers, new LatLon(pt.lat, pt.lon), pt.name); + } + + removeOldMarkersIfNeeded(dbMarkers); + } + } + + private void addNewMarkerIfNeeded(MarkersSyncGroup group, List markers, LatLon latLon, String name) { + boolean exists = false; + + for (MapMarker marker : markers) { + if (marker.id.equals(group.getId() + name)) { + exists = true; + if (!marker.history && !marker.point.equals(latLon)) { + for (MapMarker m : mapMarkers) { + if (m.id.equals(marker.id)) { + m.point = latLon; + updateMapMarker(m, true); + break; } - dbMarkers.remove(marker); - break; } } + markers.remove(marker); + break; + } + } - if (!exists) { - addMarkers(Collections.singletonList(fpLatLon), - Collections.singletonList(new PointDescription(POINT_TYPE_MAP_MARKER, fp.getName())), group); + if (!exists) { + addMarkers(Collections.singletonList(latLon), + Collections.singletonList(new PointDescription(POINT_TYPE_MAP_MARKER, name)), group); + } + } + + private void removeOldMarkersIfNeeded(List markers) { + if (!markers.isEmpty()) { + boolean needRefresh = false; + for (MapMarker marker : markers) { + if (!marker.history) { + markersDbHelper.removeMarker(marker, false); + mapMarkers.remove(marker); + needRefresh = true; } } - - if (!dbMarkers.isEmpty()) { - for (MapMarker marker : dbMarkers) { - if (!marker.history) { - markersDbHelper.removeMarker(marker, false); - mapMarkers.remove(marker); - checkAndFixActiveMarkersOrderIfNeeded(); - refresh(); - } - } + if (needRefresh) { + checkAndFixActiveMarkersOrderIfNeeded(); + refresh(); } - } else { - } } @@ -732,9 +771,9 @@ public class MapMarkersHelper { while (fout.exists()) { fout = new File(dir, fileName + "_" + (++ind) + ".gpx"); } - GPXUtilities.GPXFile file = new GPXUtilities.GPXFile(); + GPXFile file = new GPXFile(); for (MapMarker marker : mapMarkers) { - GPXUtilities.WptPt wpt = new GPXUtilities.WptPt(); + WptPt wpt = new WptPt(); wpt.lat = marker.getLatitude(); wpt.lon = marker.getLongitude(); wpt.setColor(ctx.getResources().getColor(MapMarker.getColorId(marker.colorIndex))); diff --git a/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java b/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java index 0e583a13fc..3baa1d9318 100644 --- a/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java @@ -177,7 +177,7 @@ public class EditFavoriteGroupDialogFragment extends BottomSheetDialogFragment { final MapMarkersHelper markersHelper = getMyApplication().getMapMarkersHelper(); final MarkersSyncGroup syncGroup = new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE); - boolean groupSyncedWithMarkers = markersHelper.isGroupSynced(syncGroup); + boolean groupSyncedWithMarkers = markersHelper.isGroupSynced(syncGroup.getId()); View addToMarkersView = view.findViewById(R.id.add_to_markers_view); if (app.getSettings().USE_MAP_MARKERS.get() && group.points.size() > 0 && !groupSyncedWithMarkers) {