From d4acaad1872f920ba6eec5aa2bd4036bf1f52a0a Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Wed, 13 Sep 2017 19:42:05 +0300 Subject: [PATCH] Add sync --- .../src/net/osmand/plus/AppInitializer.java | 1 + .../net/osmand/plus/FavouritesDbHelper.java | 129 +++++++++--------- .../src/net/osmand/plus/MapMarkersHelper.java | 82 +++++++++-- .../EditFavoriteGroupDialogFragment.java | 4 +- .../activities/FavoritesTreeFragment.java | 4 +- .../plus/mapmarkers/MapMarkersDbHelper.java | 82 ++++++++++- .../plus/myplaces/TrackPointFragment.java | 1 + 7 files changed, 221 insertions(+), 82 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index e4d3563ac4..ec14837769 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -501,6 +501,7 @@ public class AppInitializer implements IProgress { startBgTime = System.currentTimeMillis(); app.favorites.loadFavorites(); notifyEvent(InitEvents.FAVORITES_INITIALIZED); + app.mapMarkersHelper.syncAllGroups(); // init poi types before indexes and before POI initPoiTypes(); notifyEvent(InitEvents.POI_TYPES_INITIALIZED); diff --git a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java index f250942298..4213b31847 100644 --- a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java @@ -7,6 +7,7 @@ import net.osmand.PlatformUtil; import net.osmand.data.FavouritePoint; import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.WptPt; +import net.osmand.plus.MapMarkersHelper.MarkersSyncGroup; import net.osmand.plus.api.SQLiteAPI.SQLiteConnection; import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; import net.osmand.util.Algorithms; @@ -20,6 +21,7 @@ import java.text.Collator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -63,9 +65,9 @@ public class FavouritesDbHelper { favoriteGroups.clear(); File internalFile = getInternalFile(); - if(!internalFile.exists()) { + if (!internalFile.exists()) { File dbPath = context.getDatabasePath(FAVOURITE_DB_NAME); - if(dbPath.exists()) { + if (dbPath.exists()) { loadAndCheckDatabasePoints(); saveCurrentPointsIntoFile(); } @@ -77,27 +79,27 @@ public class FavouritesDbHelper { loadGPXFile(getExternalFile(), extPoints); boolean changed = merge(extPoints, points); - for(FavouritePoint pns : points.values()) { + for (FavouritePoint pns : points.values()) { FavoriteGroup group = getOrCreateGroup(pns, 0); group.points.add(pns); } sortAll(); recalculateCachedFavPoints(); - if(changed) { + if (changed) { saveCurrentPointsIntoFile(); } favouritesUpdated(); } - private void favouritesUpdated(){ + private void favouritesUpdated() { } private boolean merge(Map source, Map destination) { boolean changed = false; - for(String ks : source.keySet()) { - if(!destination.containsKey(ks)) { + for (String ks : source.keySet()) { + if (!destination.containsKey(ks)) { changed = true; destination.put(ks, source.get(ks)); } @@ -106,26 +108,31 @@ public class FavouritesDbHelper { } - private File getInternalFile() { return context.getFileStreamPath(FILE_TO_BACKUP); } public void delete(Set groupsToDelete, Set favoritesSelected) { if (favoritesSelected != null) { + Set groupsToSync = new HashSet<>(); for (FavouritePoint p : favoritesSelected) { FavoriteGroup group = flatGroups.get(p.getCategory()); if (group != null) { group.points.remove(p); + groupsToSync.add(group); } cachedFavoritePoints.remove(p); } + for (FavoriteGroup gr : groupsToSync) { + context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(gr.name, gr.name, MarkersSyncGroup.FAVORITES_TYPE)); + } } if (groupsToDelete != null) { for (FavoriteGroup g : groupsToDelete) { flatGroups.remove(g.name); favoriteGroups.remove(g); cachedFavoritePoints.removeAll(g.points); + context.getMapMarkersHelper().removeMarkersSyncGroup(g.name); } } saveCurrentPointsIntoFile(); @@ -140,6 +147,7 @@ public class FavouritesDbHelper { FavoriteGroup group = flatGroups.get(p.getCategory()); if (group != null) { group.points.remove(p); + context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE)); } cachedFavoritePoints.remove(p); } @@ -169,6 +177,7 @@ public class FavouritesDbHelper { sortAll(); saveCurrentPointsIntoFile(); } + context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE)); return true; } @@ -201,7 +210,7 @@ public class FavouritesDbHelper { } } } - if ((index.length() > 0 || emoticons) ) { + if ((index.length() > 0 || emoticons)) { AlertDialog.Builder builder = new AlertDialog.Builder(uiContext); builder.setTitle(R.string.fav_point_dublicate); if (emoticons) { @@ -215,7 +224,7 @@ public class FavouritesDbHelper { return null; } - public static String checkEmoticons(String name){ + public static String checkEmoticons(String name) { char[] chars = name.toCharArray(); int index; char ch1; @@ -225,16 +234,15 @@ public class FavouritesDbHelper { StringBuilder builder = new StringBuilder(); while (index < chars.length) { ch1 = chars[index]; - if ((int)ch1 == 0xD83C) { - ch2 = chars[index+1]; - if ((int)ch2 >= 0xDF00 && (int)ch2 <= 0xDFFF) { + if ((int) ch1 == 0xD83C) { + ch2 = chars[index + 1]; + if ((int) ch2 >= 0xDF00 && (int) ch2 <= 0xDFFF) { index += 2; continue; } - } - else if ((int)ch1 == 0xD83D) { - ch2 = chars[index+1]; - if ((int)ch2 >= 0xDC00 && (int)ch2 <= 0xDDFF) { + } else if ((int) ch1 == 0xD83D) { + ch2 = chars[index + 1]; + if ((int) ch2 >= 0xDC00 && (int) ch2 <= 0xDDFF) { index += 2; continue; } @@ -264,14 +272,15 @@ public class FavouritesDbHelper { } sortAll(); saveCurrentPointsIntoFile(); + context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(category, category, MarkersSyncGroup.FAVORITES_TYPE)); return true; } - public boolean editFavourite(FavouritePoint p, double lat, double lon) { p.setLatitude(lat); p.setLongitude(lon); saveCurrentPointsIntoFile(); + context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(p.getCategory(), p.getCategory(), MarkersSyncGroup.FAVORITES_TYPE)); return true; } @@ -279,7 +288,7 @@ public class FavouritesDbHelper { try { Map deletedInMemory = new LinkedHashMap(); loadGPXFile(getInternalFile(), deletedInMemory); - for(FavouritePoint fp : cachedFavoritePoints) { + for (FavouritePoint fp : cachedFavoritePoints) { deletedInMemory.remove(getKey(fp)); } saveFile(cachedFavoritePoints, getInternalFile()); @@ -312,18 +321,17 @@ public class FavouritesDbHelper { } - private String saveExternalFile(Set deleted) { Map all = new LinkedHashMap(); loadGPXFile(getExternalFile(), all); List favoritePoints = new ArrayList(cachedFavoritePoints); - if(deleted != null) { - for(String key : deleted) { + if (deleted != null) { + for (String key : deleted) { all.remove(key); } } // remove already existing in memory - for(FavouritePoint p : favoritePoints) { + for (FavouritePoint p : favoritePoints) { all.remove(getKey(p)); } // save favoritePoints from memory in order to update existing @@ -332,18 +340,16 @@ public class FavouritesDbHelper { } - private String getKey(FavouritePoint p) { return p.getName() + DELIMETER + p.getCategory(); } - - public boolean deleteGroup(FavoriteGroup group) { boolean remove = favoriteGroups.remove(group); if (remove) { flatGroups.remove(group.name); saveCurrentPointsIntoFile(); + context.getMapMarkersHelper().removeMarkersSyncGroup(group.name); return true; } return false; @@ -355,26 +361,26 @@ public class FavouritesDbHelper { public File getBackupFile() { File fld = new File(context.getAppPath(null), BACKUP_FOLDER); - if(!fld.exists()) { + if (!fld.exists()) { fld.mkdirs(); } int back = 1; String backPrefix = "" + back; File firstModified = null; long firstModifiedMin = System.currentTimeMillis(); - while(back <= BACKUP_CNT) { + while (back <= BACKUP_CNT) { backPrefix = "" + back; - if(back < 10) { - backPrefix = "0"+backPrefix; + if (back < 10) { + backPrefix = "0" + backPrefix; } - File bak = new File(fld, "favourites_bak_" + backPrefix +".gpx.bz2"); + File bak = new File(fld, "favourites_bak_" + backPrefix + ".gpx.bz2"); if (!bak.exists()) { return bak; } else if (bak.lastModified() < firstModifiedMin) { firstModified = bak; firstModifiedMin = bak.lastModified(); } - back ++; + back++; } return firstModified; } @@ -395,10 +401,10 @@ public class FavouritesDbHelper { WptPt pt = new WptPt(); pt.lat = p.getLatitude(); pt.lon = p.getLongitude(); - if(!p.isVisible()) { + if (!p.isVisible()) { pt.getExtensionsToWrite().put(HIDDEN, "true"); } - if(p.getColor() != 0) { + if (p.getColor() != 0) { pt.setColor(p.getColor()); } pt.name = p.getName(); @@ -437,8 +443,8 @@ public class FavouritesDbHelper { public List getVisibleFavouritePoints() { List fp = new ArrayList<>(); - for(FavouritePoint p : cachedFavoritePoints) { - if(p.isVisible()) { + for (FavouritePoint p : cachedFavoritePoints) { + if (p.isVisible()) { fp.add(p); } } @@ -476,7 +482,7 @@ public class FavouritesDbHelper { } } - private FavouritePoint findFavoriteByAllProperties(String category, String name, double lat, double lon){ + private FavouritePoint findFavoriteByAllProperties(String category, String name, double lat, double lon) { if (flatGroups.containsKey(category)) { FavoriteGroup fg = flatGroups.get(category); for (FavouritePoint fv : fg.points) { @@ -489,10 +495,9 @@ public class FavouritesDbHelper { } - - public void recalculateCachedFavPoints(){ + public void recalculateCachedFavPoints() { ArrayList temp = new ArrayList(); - for(FavoriteGroup f : favoriteGroups){ + for (FavoriteGroup f : favoriteGroups) { temp.addAll(f.points); } cachedFavoritePoints = temp; @@ -553,7 +558,7 @@ public class FavouritesDbHelper { private boolean loadGPXFile(File file, Map points) { - if(!file.exists()) { + if (!file.exists()) { return false; } GPXFile res = GPXUtilities.loadGPXFile(context, file); @@ -584,18 +589,19 @@ public class FavouritesDbHelper { return true; } + //todo public void editFavouriteGroup(FavoriteGroup group, String newName, int color, boolean visible) { - if(color != 0 && group.color != color) { + if (color != 0 && group.color != color) { FavoriteGroup gr = flatGroups.get(group.name); group.color = color; - for(FavouritePoint p : gr.points) { + for (FavouritePoint p : gr.points) { p.setColor(color); } } - if(group.visible != visible) { + if (group.visible != visible) { FavoriteGroup gr = flatGroups.get(group.name); group.visible = visible; - for(FavouritePoint p : gr.points) { + for (FavouritePoint p : gr.points) { p.setVisible(visible); } } @@ -604,15 +610,15 @@ public class FavouritesDbHelper { gr.name = newName; FavoriteGroup renamedGroup = flatGroups.get(gr.name); boolean existing = renamedGroup != null; - if(renamedGroup == null) { + if (renamedGroup == null) { renamedGroup = gr; flatGroups.put(gr.name, gr); } else { favoriteGroups.remove(gr); } - for(FavouritePoint p : gr.points) { + for (FavouritePoint p : gr.points) { p.setCategory(newName); - if(existing) { + if (existing) { renamedGroup.points.add(p); } } @@ -680,13 +686,13 @@ public class FavouritesDbHelper { } public void onUpgrade(SQLiteConnection db, int oldVersion, int newVersion) { - if(oldVersion == 1){ - db.execSQL("ALTER TABLE " + FAVOURITE_TABLE_NAME + " ADD " + FAVOURITE_COL_CATEGORY + " text"); - db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET category = ?", new Object[] { "" }); //$NON-NLS-1$ //$NON-NLS-2$ + if (oldVersion == 1) { + db.execSQL("ALTER TABLE " + FAVOURITE_TABLE_NAME + " ADD " + FAVOURITE_COL_CATEGORY + " text"); + db.execSQL("UPDATE " + FAVOURITE_TABLE_NAME + " SET category = ?", new Object[]{""}); //$NON-NLS-1$ //$NON-NLS-2$ } } - private void loadAndCheckDatabasePoints(){ + private void loadAndCheckDatabasePoints() { if (favoriteGroups == null) { SQLiteConnection db = openConnection(true); if (db != null) { @@ -727,17 +733,17 @@ public class FavouritesDbHelper { if (db != null) { try { db.execSQL( - "DELETE FROM " + FAVOURITE_TABLE_NAME + " WHERE category = ? AND " + whereNameLatLon(), new Object[] { p.getCategory(), p.getName(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$ + "DELETE FROM " + FAVOURITE_TABLE_NAME + " WHERE category = ? AND " + whereNameLatLon(), new Object[]{p.getCategory(), p.getName(), p.getLatitude(), p.getLongitude()}); //$NON-NLS-1$ //$NON-NLS-2$ FavouritePoint fp = findFavoriteByAllProperties(p.getCategory(), p.getName(), p.getLatitude(), p.getLongitude()); if (fp != null) { FavoriteGroup group = flatGroups.get(p.getCategory()); - if(group != null) { + if (group != null) { group.points.remove(fp); } cachedFavoritePoints.remove(fp); } saveCurrentPointsIntoFile(); - } finally{ + } finally { db.close(); } return true; @@ -747,7 +753,7 @@ public class FavouritesDbHelper { public boolean addFavouriteDB(FavouritePoint p) { - if(p.getName().equals("") && flatGroups.containsKey(p.getCategory())){ + if (p.getName().equals("") && flatGroups.containsKey(p.getCategory())) { return true; } SQLiteConnection db = openConnection(false); @@ -755,8 +761,8 @@ public class FavouritesDbHelper { try { db.execSQL( "INSERT INTO " + FAVOURITE_TABLE_NAME + " (" + FAVOURITE_COL_NAME + ", " + FAVOURITE_COL_CATEGORY + ", " - + FAVOURITE_COL_LAT + ", " + FAVOURITE_COL_LON + ")" + " VALUES (?, ?, ?, ?)", new Object[] { p.getName(), p.getCategory(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$ - FavoriteGroup group = getOrCreateGroup(p, 0); + + FAVOURITE_COL_LAT + ", " + FAVOURITE_COL_LON + ")" + " VALUES (?, ?, ?, ?)", new Object[]{p.getName(), p.getCategory(), p.getLatitude(), p.getLongitude()}); //$NON-NLS-1$ //$NON-NLS-2$ + FavoriteGroup group = getOrCreateGroup(p, 0); if (!p.getName().equals("")) { p.setVisible(group.visible); p.setColor(group.color); @@ -773,14 +779,13 @@ public class FavouritesDbHelper { } - public boolean editFavouriteNameDB(FavouritePoint p, String newName, String category) { SQLiteConnection db = openConnection(false); if (db != null) { try { String oldCategory = p.getCategory(); db.execSQL( - "UPDATE " + FAVOURITE_TABLE_NAME + " SET " + FAVOURITE_COL_NAME + " = ?, " + FAVOURITE_COL_CATEGORY + "= ? WHERE " + whereNameLatLon(), new Object[] { newName, category, p.getName(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$ + "UPDATE " + FAVOURITE_TABLE_NAME + " SET " + FAVOURITE_COL_NAME + " = ?, " + FAVOURITE_COL_CATEGORY + "= ? WHERE " + whereNameLatLon(), new Object[]{newName, category, p.getName(), p.getLatitude(), p.getLongitude()}); //$NON-NLS-1$ //$NON-NLS-2$ p.setName(newName); p.setCategory(category); if (!oldCategory.equals(category)) { @@ -808,7 +813,7 @@ public class FavouritesDbHelper { if (db != null) { try { db.execSQL( - "UPDATE " + FAVOURITE_TABLE_NAME + " SET latitude = ?, longitude = ? WHERE " + whereNameLatLon(), new Object[] { lat, lon, p.getName(), p.getLatitude(), p.getLongitude() }); //$NON-NLS-1$ //$NON-NLS-2$ + "UPDATE " + FAVOURITE_TABLE_NAME + " SET latitude = ?, longitude = ? WHERE " + whereNameLatLon(), new Object[]{lat, lon, p.getName(), p.getLatitude(), p.getLongitude()}); //$NON-NLS-1$ //$NON-NLS-2$ p.setLatitude(lat); p.setLongitude(lon); saveCurrentPointsIntoFile(); @@ -826,6 +831,4 @@ public class FavouritesDbHelper { } - - } diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 74b56dc966..037030610a 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -6,6 +6,7 @@ import android.support.annotation.Nullable; import android.text.format.DateFormat; import net.osmand.IndexConstants; +import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.data.LocationPoint; import net.osmand.data.PointDescription; @@ -22,6 +23,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Locale; +import static net.osmand.data.PointDescription.POINT_TYPE_MAP_MARKER; + public class MapMarkersHelper { public static final int MAP_MARKERS_COLORS_COUNT = 7; @@ -64,7 +67,7 @@ public class MapMarkersHelper { } public PointDescription getPointDescription(Context ctx) { - return new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, ctx.getString(R.string.map_marker), + return new PointDescription(POINT_TYPE_MAP_MARKER, ctx.getString(R.string.map_marker), getOnlyName()); } @@ -283,8 +286,52 @@ public class MapMarkersHelper { } } - public void syncGroup(MarkersSyncGroup group) { + public void syncAllGroups() { + List groups = markersDbHelper.getAllGroups(); + for (MarkersSyncGroup gr : groups) { + syncGroup(gr); + } + } + public void syncGroup(MarkersSyncGroup group) { + if (markersDbHelper.getGroup(group.getId()) == null) { + return; + } + List dbMarkers = markersDbHelper.getMarkersFromGroup(group); + + if (group.getType() == MarkersSyncGroup.FAVORITES_TYPE) { + List favPoints = ctx.getFavorites().getGroup(group.name).points; + for (FavouritePoint fp : favPoints) { + LatLon fpLatLon = new LatLon(fp.getLatitude(), fp.getLongitude()); + boolean exists = false; + + for (MapMarker marker : dbMarkers) { + if (marker.point.equals(fpLatLon) && marker.getName(ctx).equals(fp.getName(ctx))) { + exists = true; + dbMarkers.remove(marker); + break; + } + } + + if (!exists) { + addMarkers(Collections.singletonList(fpLatLon), + Collections.singletonList(new PointDescription(POINT_TYPE_MAP_MARKER, fp.getName())), group); + } + } + + if (!dbMarkers.isEmpty()) { + for (MapMarker marker : dbMarkers) { + if (!marker.history) { + markersDbHelper.removeMarker(marker, false); + mapMarkers.remove(marker); + checkAndFixActiveMarkersOrderIfNeeded(); + refresh(); + } + } + } + } else { + + } } public void moveMapMarkerToHistory(MapMarker marker) { @@ -329,7 +376,7 @@ public class MapMarkersHelper { public void removeMarkerFromHistory(MapMarker marker) { if (marker != null) { - markersDbHelper.removeMarkerFromHistory(marker); + markersDbHelper.removeMarker(marker, true); mapMarkersHistory.remove(marker); refresh(); } @@ -395,8 +442,10 @@ public class MapMarkersHelper { public void moveAllActiveMarkersToHistory() { cancelAddressRequests(); - markersDbHelper.moveAllActiveMarkersToHistory(); + long timestamp = System.currentTimeMillis(); + markersDbHelper.moveAllActiveMarkersToHistory(timestamp); for (MapMarker marker : mapMarkers) { + marker.visitedDate = timestamp; marker.history = true; marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE; } @@ -413,6 +462,20 @@ public class MapMarkersHelper { refresh(); } + public void addMarkersSyncGroup(MarkersSyncGroup group) { + if (group != null) { + if (markersDbHelper.getGroup(group.getId()) == null) { + markersDbHelper.addGroup(group.getId(), group.getName(), group.getType()); + } + } + } + + public void removeMarkersSyncGroup(String id) { + if (id != null) { + markersDbHelper.removeMarkersSyncGroup(id); + } + } + public void addMapMarker(LatLon point, PointDescription historyName) { addMarkers(Collections.singletonList(point), Collections.singletonList(historyName), null); } @@ -421,10 +484,6 @@ public class MapMarkersHelper { addMarkers(points, historyNames, group); } - public void addMapMarkers(List points, List historyNames) { - addMarkers(points, historyNames, null); - } - private void addMarkers(List points, List historyNames, @Nullable MarkersSyncGroup group) { if (points.size() > 0) { int colorIndex = -1; @@ -452,11 +511,12 @@ public class MapMarkersHelper { MapMarker marker = new MapMarker(point, pointDescription, colorIndex, false, 0); if (group != null) { - if (markersDbHelper.getGroup(group.getId()) == null) { - markersDbHelper.addGroup(group.getId(), group.getName(), group.getType()); + marker.id = group.getId() + marker.getName(ctx) + marker.getLatitude() + marker.getLongitude(); + if (markersDbHelper.getMarker(marker.id) != null) { + continue; } - marker.id = group.getId() + marker.getName(ctx); marker.groupName = group.getName(); + marker.groupKey = group.getId(); } marker.history = false; marker.nextKey = MapMarkersDbHelper.TAIL_NEXT_VALUE; diff --git a/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java b/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java index ce4bc97295..722c0bf019 100644 --- a/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java @@ -189,7 +189,9 @@ public class EditFavoriteGroupDialogFragment extends BottomSheetDialogFragment { points.add(new LatLon(fp.getLatitude(), fp.getLongitude())); names.add(new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, fp.getName())); } - markersHelper.addMapMarkers(points, names, new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE)); + MarkersSyncGroup syncGroup = new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE); + markersHelper.addMarkersSyncGroup(syncGroup); + markersHelper.addMapMarkers(points, names, syncGroup); dismiss(); MapActivity.launchMapActivityMoveToTop(getActivity()); } diff --git a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java index 1ee398d73d..e187024fd6 100644 --- a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java +++ b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java @@ -406,10 +406,10 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment { List points = new LinkedList<>(); List names = new LinkedList<>(); for (Map.Entry> entry : favoritesSelected.entrySet()) { - MarkersSyncGroup syncGr = null; FavoriteGroup favGr = helper.getGroup(entry.getKey()); + MarkersSyncGroup syncGr = new MarkersSyncGroup(favGr.name, favGr.name, MarkersSyncGroup.FAVORITES_TYPE); if (entry.getValue().size() == favGr.points.size()) { - syncGr = new MarkersSyncGroup(favGr.name, favGr.name, MarkersSyncGroup.FAVORITES_TYPE); + markersHelper.addMarkersSyncGroup(syncGr); } for (FavouritePoint fp : entry.getValue()) { points.add(new LatLon(fp.getLatitude(), fp.getLongitude())); diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java index f6be7c00ec..9a6c779fdd 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDbHelper.java @@ -164,6 +164,25 @@ public class MapMarkersDbHelper { } } + public List getAllGroups() { + List res = new LinkedList<>(); + SQLiteConnection db = openConnection(true); + if (db != null) { + try { + SQLiteCursor query = db.rawQuery(GROUPS_TABLE_SELECT, null); + if (query.moveToFirst()) { + do { + res.add(readSyncGroup(query)); + } while (query.moveToNext()); + } + query.close(); + } finally { + db.close(); + } + } + return res; + } + @Nullable public MarkersSyncGroup getGroup(String id) { MarkersSyncGroup res = null; @@ -190,6 +209,22 @@ public class MapMarkersDbHelper { return new MarkersSyncGroup(id, name, type); } + public void removeMarkersSyncGroup(String id) { + SQLiteConnection db = openConnection(true); + if (db != null) { + try { + db.execSQL("DELETE FROM " + MARKERS_TABLE_NAME + + " WHERE " + MARKERS_COL_GROUP_KEY + " = ?" + + " AND " + MARKERS_COL_ACTIVE + " = ?", + new Object[]{id, 1}); + + db.execSQL("DELETE FROM " + GROUPS_TABLE_NAME + " WHERE " + GROUPS_COL_ID + " = ?", new Object[]{id}); + } finally { + db.close(); + } + } + } + public void addMarker(MapMarker marker) { addMarker(marker, false); } @@ -239,6 +274,44 @@ public class MapMarkersDbHelper { marker.history ? HISTORY_NEXT_VALUE : TAIL_NEXT_VALUE}); } + public List getMarkersFromGroup(MarkersSyncGroup group) { + List res = new LinkedList<>(); + SQLiteConnection db = openConnection(true); + if (db != null) { + try { + SQLiteCursor query = db.rawQuery(MARKERS_TABLE_SELECT + " WHERE " + MARKERS_COL_GROUP_KEY + " = ?", + new String[]{group.getId()}); + if (query.moveToFirst()) { + do { + res.add(readItem(query)); + } while (query.moveToNext()); + } + query.close(); + } finally { + db.close(); + } + } + return res; + } + + @Nullable + public MapMarker getMarker(String id) { + MapMarker res = null; + SQLiteConnection db = openConnection(true); + if (db != null) { + try { + SQLiteCursor query = db.rawQuery(MARKERS_TABLE_SELECT + " WHERE " + MARKERS_COL_ID + " = ?", new String[]{id}); + if (query.moveToFirst()) { + res = readItem(query); + } + query.close(); + } finally { + db.close(); + } + } + return res; + } + public List getActiveMarkers() { List res = new LinkedList<>(); HashMap markers = new LinkedHashMap<>(); @@ -351,16 +424,15 @@ public class MapMarkersDbHelper { } } - public void moveAllActiveMarkersToHistory() { + public void moveAllActiveMarkersToHistory(long timestamp) { SQLiteConnection db = openConnection(false); if (db != null) { try { - long visitedDate = System.currentTimeMillis(); db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + MARKERS_COL_ACTIVE + " = ?, " + MARKERS_COL_VISITED + " = ?, " + MARKERS_COL_NEXT_KEY + " = ? " + - "WHERE " + MARKERS_COL_ACTIVE + " = ?", new Object[]{0, visitedDate, HISTORY_NEXT_VALUE, 1}); + "WHERE " + MARKERS_COL_ACTIVE + " = ?", new Object[]{0, timestamp, HISTORY_NEXT_VALUE, 1}); } finally { db.close(); } @@ -402,14 +474,14 @@ public class MapMarkersDbHelper { return markers; } - public void removeMarkerFromHistory(MapMarker marker) { + public void removeMarker(MapMarker marker, boolean history) { SQLiteConnection db = openConnection(true); if (db != null) { try { db.execSQL("DELETE FROM " + MARKERS_TABLE_NAME + " WHERE " + MARKERS_COL_ID + " = ?" + " AND " + MARKERS_COL_ACTIVE + " = ?", - new Object[]{marker.id, 0}); + new Object[]{marker.id, history ? 0 : 1}); } finally { db.close(); } diff --git a/OsmAnd/src/net/osmand/plus/myplaces/TrackPointFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/TrackPointFragment.java index 07315990ce..9e135db20b 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/TrackPointFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/TrackPointFragment.java @@ -610,6 +610,7 @@ public class TrackPointFragment extends OsmandExpandableListFragment { } File gpx = getGpxDataItem().getFile(); MarkersSyncGroup syncGroup = new MarkersSyncGroup(gpx.getAbsolutePath(), trimExtension(gpx.getName()), MarkersSyncGroup.GPX_TYPE); + markersHelper.addMarkersSyncGroup(syncGroup); markersHelper.addMapMarkers(points, names, syncGroup); MapActivity.launchMapActivityMoveToTop(getActivity()); } else {