This commit is contained in:
Alexander Sytnyk 2017-09-13 19:42:05 +03:00
parent 14b70827f1
commit d4acaad187
7 changed files with 221 additions and 82 deletions

View file

@ -501,6 +501,7 @@ public class AppInitializer implements IProgress {
startBgTime = System.currentTimeMillis(); startBgTime = System.currentTimeMillis();
app.favorites.loadFavorites(); app.favorites.loadFavorites();
notifyEvent(InitEvents.FAVORITES_INITIALIZED); notifyEvent(InitEvents.FAVORITES_INITIALIZED);
app.mapMarkersHelper.syncAllGroups();
// init poi types before indexes and before POI // init poi types before indexes and before POI
initPoiTypes(); initPoiTypes();
notifyEvent(InitEvents.POI_TYPES_INITIALIZED); notifyEvent(InitEvents.POI_TYPES_INITIALIZED);

View file

@ -7,6 +7,7 @@ import net.osmand.PlatformUtil;
import net.osmand.data.FavouritePoint; import net.osmand.data.FavouritePoint;
import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt; 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.SQLiteConnection;
import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; import net.osmand.plus.api.SQLiteAPI.SQLiteCursor;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
@ -20,6 +21,7 @@ import java.text.Collator;
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.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -63,9 +65,9 @@ public class FavouritesDbHelper {
favoriteGroups.clear(); favoriteGroups.clear();
File internalFile = getInternalFile(); File internalFile = getInternalFile();
if(!internalFile.exists()) { if (!internalFile.exists()) {
File dbPath = context.getDatabasePath(FAVOURITE_DB_NAME); File dbPath = context.getDatabasePath(FAVOURITE_DB_NAME);
if(dbPath.exists()) { if (dbPath.exists()) {
loadAndCheckDatabasePoints(); loadAndCheckDatabasePoints();
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
} }
@ -77,27 +79,27 @@ public class FavouritesDbHelper {
loadGPXFile(getExternalFile(), extPoints); loadGPXFile(getExternalFile(), extPoints);
boolean changed = merge(extPoints, points); boolean changed = merge(extPoints, points);
for(FavouritePoint pns : points.values()) { for (FavouritePoint pns : points.values()) {
FavoriteGroup group = getOrCreateGroup(pns, 0); FavoriteGroup group = getOrCreateGroup(pns, 0);
group.points.add(pns); group.points.add(pns);
} }
sortAll(); sortAll();
recalculateCachedFavPoints(); recalculateCachedFavPoints();
if(changed) { if (changed) {
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
} }
favouritesUpdated(); favouritesUpdated();
} }
private void favouritesUpdated(){ private void favouritesUpdated() {
} }
private boolean merge(Map<String, FavouritePoint> source, Map<String, FavouritePoint> destination) { private boolean merge(Map<String, FavouritePoint> source, Map<String, FavouritePoint> destination) {
boolean changed = false; boolean changed = false;
for(String ks : source.keySet()) { for (String ks : source.keySet()) {
if(!destination.containsKey(ks)) { if (!destination.containsKey(ks)) {
changed = true; changed = true;
destination.put(ks, source.get(ks)); destination.put(ks, source.get(ks));
} }
@ -106,26 +108,31 @@ public class FavouritesDbHelper {
} }
private File getInternalFile() { private File getInternalFile() {
return context.getFileStreamPath(FILE_TO_BACKUP); return context.getFileStreamPath(FILE_TO_BACKUP);
} }
public void delete(Set<FavoriteGroup> groupsToDelete, Set<FavouritePoint> favoritesSelected) { public void delete(Set<FavoriteGroup> groupsToDelete, Set<FavouritePoint> favoritesSelected) {
if (favoritesSelected != null) { if (favoritesSelected != null) {
Set<FavoriteGroup> groupsToSync = new HashSet<>();
for (FavouritePoint p : favoritesSelected) { for (FavouritePoint p : favoritesSelected) {
FavoriteGroup group = flatGroups.get(p.getCategory()); FavoriteGroup group = flatGroups.get(p.getCategory());
if (group != null) { if (group != null) {
group.points.remove(p); group.points.remove(p);
groupsToSync.add(group);
} }
cachedFavoritePoints.remove(p); cachedFavoritePoints.remove(p);
} }
for (FavoriteGroup gr : groupsToSync) {
context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(gr.name, gr.name, MarkersSyncGroup.FAVORITES_TYPE));
}
} }
if (groupsToDelete != null) { if (groupsToDelete != null) {
for (FavoriteGroup g : groupsToDelete) { for (FavoriteGroup g : groupsToDelete) {
flatGroups.remove(g.name); flatGroups.remove(g.name);
favoriteGroups.remove(g); favoriteGroups.remove(g);
cachedFavoritePoints.removeAll(g.points); cachedFavoritePoints.removeAll(g.points);
context.getMapMarkersHelper().removeMarkersSyncGroup(g.name);
} }
} }
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
@ -140,6 +147,7 @@ public class FavouritesDbHelper {
FavoriteGroup group = flatGroups.get(p.getCategory()); FavoriteGroup group = flatGroups.get(p.getCategory());
if (group != null) { if (group != null) {
group.points.remove(p); group.points.remove(p);
context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE));
} }
cachedFavoritePoints.remove(p); cachedFavoritePoints.remove(p);
} }
@ -169,6 +177,7 @@ public class FavouritesDbHelper {
sortAll(); sortAll();
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
} }
context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE));
return true; 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); AlertDialog.Builder builder = new AlertDialog.Builder(uiContext);
builder.setTitle(R.string.fav_point_dublicate); builder.setTitle(R.string.fav_point_dublicate);
if (emoticons) { if (emoticons) {
@ -215,7 +224,7 @@ public class FavouritesDbHelper {
return null; return null;
} }
public static String checkEmoticons(String name){ public static String checkEmoticons(String name) {
char[] chars = name.toCharArray(); char[] chars = name.toCharArray();
int index; int index;
char ch1; char ch1;
@ -225,16 +234,15 @@ public class FavouritesDbHelper {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
while (index < chars.length) { while (index < chars.length) {
ch1 = chars[index]; ch1 = chars[index];
if ((int)ch1 == 0xD83C) { if ((int) ch1 == 0xD83C) {
ch2 = chars[index+1]; ch2 = chars[index + 1];
if ((int)ch2 >= 0xDF00 && (int)ch2 <= 0xDFFF) { if ((int) ch2 >= 0xDF00 && (int) ch2 <= 0xDFFF) {
index += 2; index += 2;
continue; continue;
} }
} } else if ((int) ch1 == 0xD83D) {
else if ((int)ch1 == 0xD83D) { ch2 = chars[index + 1];
ch2 = chars[index+1]; if ((int) ch2 >= 0xDC00 && (int) ch2 <= 0xDDFF) {
if ((int)ch2 >= 0xDC00 && (int)ch2 <= 0xDDFF) {
index += 2; index += 2;
continue; continue;
} }
@ -264,14 +272,15 @@ public class FavouritesDbHelper {
} }
sortAll(); sortAll();
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(category, category, MarkersSyncGroup.FAVORITES_TYPE));
return true; return true;
} }
public boolean editFavourite(FavouritePoint p, double lat, double lon) { public boolean editFavourite(FavouritePoint p, double lat, double lon) {
p.setLatitude(lat); p.setLatitude(lat);
p.setLongitude(lon); p.setLongitude(lon);
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
context.getMapMarkersHelper().syncGroup(new MarkersSyncGroup(p.getCategory(), p.getCategory(), MarkersSyncGroup.FAVORITES_TYPE));
return true; return true;
} }
@ -279,7 +288,7 @@ public class FavouritesDbHelper {
try { try {
Map<String, FavouritePoint> deletedInMemory = new LinkedHashMap<String, FavouritePoint>(); Map<String, FavouritePoint> deletedInMemory = new LinkedHashMap<String, FavouritePoint>();
loadGPXFile(getInternalFile(), deletedInMemory); loadGPXFile(getInternalFile(), deletedInMemory);
for(FavouritePoint fp : cachedFavoritePoints) { for (FavouritePoint fp : cachedFavoritePoints) {
deletedInMemory.remove(getKey(fp)); deletedInMemory.remove(getKey(fp));
} }
saveFile(cachedFavoritePoints, getInternalFile()); saveFile(cachedFavoritePoints, getInternalFile());
@ -312,18 +321,17 @@ public class FavouritesDbHelper {
} }
private String saveExternalFile(Set<String> deleted) { private String saveExternalFile(Set<String> deleted) {
Map<String, FavouritePoint> all = new LinkedHashMap<String, FavouritePoint>(); Map<String, FavouritePoint> all = new LinkedHashMap<String, FavouritePoint>();
loadGPXFile(getExternalFile(), all); loadGPXFile(getExternalFile(), all);
List<FavouritePoint> favoritePoints = new ArrayList<FavouritePoint>(cachedFavoritePoints); List<FavouritePoint> favoritePoints = new ArrayList<FavouritePoint>(cachedFavoritePoints);
if(deleted != null) { if (deleted != null) {
for(String key : deleted) { for (String key : deleted) {
all.remove(key); all.remove(key);
} }
} }
// remove already existing in memory // remove already existing in memory
for(FavouritePoint p : favoritePoints) { for (FavouritePoint p : favoritePoints) {
all.remove(getKey(p)); all.remove(getKey(p));
} }
// save favoritePoints from memory in order to update existing // save favoritePoints from memory in order to update existing
@ -332,18 +340,16 @@ public class FavouritesDbHelper {
} }
private String getKey(FavouritePoint p) { private String getKey(FavouritePoint p) {
return p.getName() + DELIMETER + p.getCategory(); return p.getName() + DELIMETER + p.getCategory();
} }
public boolean deleteGroup(FavoriteGroup group) { public boolean deleteGroup(FavoriteGroup group) {
boolean remove = favoriteGroups.remove(group); boolean remove = favoriteGroups.remove(group);
if (remove) { if (remove) {
flatGroups.remove(group.name); flatGroups.remove(group.name);
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
context.getMapMarkersHelper().removeMarkersSyncGroup(group.name);
return true; return true;
} }
return false; return false;
@ -355,26 +361,26 @@ public class FavouritesDbHelper {
public File getBackupFile() { public File getBackupFile() {
File fld = new File(context.getAppPath(null), BACKUP_FOLDER); File fld = new File(context.getAppPath(null), BACKUP_FOLDER);
if(!fld.exists()) { if (!fld.exists()) {
fld.mkdirs(); fld.mkdirs();
} }
int back = 1; int back = 1;
String backPrefix = "" + back; String backPrefix = "" + back;
File firstModified = null; File firstModified = null;
long firstModifiedMin = System.currentTimeMillis(); long firstModifiedMin = System.currentTimeMillis();
while(back <= BACKUP_CNT) { while (back <= BACKUP_CNT) {
backPrefix = "" + back; backPrefix = "" + back;
if(back < 10) { if (back < 10) {
backPrefix = "0"+backPrefix; 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()) { if (!bak.exists()) {
return bak; return bak;
} else if (bak.lastModified() < firstModifiedMin) { } else if (bak.lastModified() < firstModifiedMin) {
firstModified = bak; firstModified = bak;
firstModifiedMin = bak.lastModified(); firstModifiedMin = bak.lastModified();
} }
back ++; back++;
} }
return firstModified; return firstModified;
} }
@ -395,10 +401,10 @@ public class FavouritesDbHelper {
WptPt pt = new WptPt(); WptPt pt = new WptPt();
pt.lat = p.getLatitude(); pt.lat = p.getLatitude();
pt.lon = p.getLongitude(); pt.lon = p.getLongitude();
if(!p.isVisible()) { if (!p.isVisible()) {
pt.getExtensionsToWrite().put(HIDDEN, "true"); pt.getExtensionsToWrite().put(HIDDEN, "true");
} }
if(p.getColor() != 0) { if (p.getColor() != 0) {
pt.setColor(p.getColor()); pt.setColor(p.getColor());
} }
pt.name = p.getName(); pt.name = p.getName();
@ -437,8 +443,8 @@ public class FavouritesDbHelper {
public List<FavouritePoint> getVisibleFavouritePoints() { public List<FavouritePoint> getVisibleFavouritePoints() {
List<FavouritePoint> fp = new ArrayList<>(); List<FavouritePoint> fp = new ArrayList<>();
for(FavouritePoint p : cachedFavoritePoints) { for (FavouritePoint p : cachedFavoritePoints) {
if(p.isVisible()) { if (p.isVisible()) {
fp.add(p); 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)) { if (flatGroups.containsKey(category)) {
FavoriteGroup fg = flatGroups.get(category); FavoriteGroup fg = flatGroups.get(category);
for (FavouritePoint fv : fg.points) { for (FavouritePoint fv : fg.points) {
@ -489,10 +495,9 @@ public class FavouritesDbHelper {
} }
public void recalculateCachedFavPoints() {
public void recalculateCachedFavPoints(){
ArrayList<FavouritePoint> temp = new ArrayList<FavouritePoint>(); ArrayList<FavouritePoint> temp = new ArrayList<FavouritePoint>();
for(FavoriteGroup f : favoriteGroups){ for (FavoriteGroup f : favoriteGroups) {
temp.addAll(f.points); temp.addAll(f.points);
} }
cachedFavoritePoints = temp; cachedFavoritePoints = temp;
@ -553,7 +558,7 @@ public class FavouritesDbHelper {
private boolean loadGPXFile(File file, Map<String, FavouritePoint> points) { private boolean loadGPXFile(File file, Map<String, FavouritePoint> points) {
if(!file.exists()) { if (!file.exists()) {
return false; return false;
} }
GPXFile res = GPXUtilities.loadGPXFile(context, file); GPXFile res = GPXUtilities.loadGPXFile(context, file);
@ -584,18 +589,19 @@ public class FavouritesDbHelper {
return true; return true;
} }
//todo
public void editFavouriteGroup(FavoriteGroup group, String newName, int color, boolean visible) { 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); FavoriteGroup gr = flatGroups.get(group.name);
group.color = color; group.color = color;
for(FavouritePoint p : gr.points) { for (FavouritePoint p : gr.points) {
p.setColor(color); p.setColor(color);
} }
} }
if(group.visible != visible) { if (group.visible != visible) {
FavoriteGroup gr = flatGroups.get(group.name); FavoriteGroup gr = flatGroups.get(group.name);
group.visible = visible; group.visible = visible;
for(FavouritePoint p : gr.points) { for (FavouritePoint p : gr.points) {
p.setVisible(visible); p.setVisible(visible);
} }
} }
@ -604,15 +610,15 @@ public class FavouritesDbHelper {
gr.name = newName; gr.name = newName;
FavoriteGroup renamedGroup = flatGroups.get(gr.name); FavoriteGroup renamedGroup = flatGroups.get(gr.name);
boolean existing = renamedGroup != null; boolean existing = renamedGroup != null;
if(renamedGroup == null) { if (renamedGroup == null) {
renamedGroup = gr; renamedGroup = gr;
flatGroups.put(gr.name, gr); flatGroups.put(gr.name, gr);
} else { } else {
favoriteGroups.remove(gr); favoriteGroups.remove(gr);
} }
for(FavouritePoint p : gr.points) { for (FavouritePoint p : gr.points) {
p.setCategory(newName); p.setCategory(newName);
if(existing) { if (existing) {
renamedGroup.points.add(p); renamedGroup.points.add(p);
} }
} }
@ -680,13 +686,13 @@ public class FavouritesDbHelper {
} }
public void onUpgrade(SQLiteConnection db, int oldVersion, int newVersion) { public void onUpgrade(SQLiteConnection db, int oldVersion, int newVersion) {
if(oldVersion == 1){ if (oldVersion == 1) {
db.execSQL("ALTER TABLE " + FAVOURITE_TABLE_NAME + " ADD " + FAVOURITE_COL_CATEGORY + " text"); 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$ 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) { if (favoriteGroups == null) {
SQLiteConnection db = openConnection(true); SQLiteConnection db = openConnection(true);
if (db != null) { if (db != null) {
@ -727,17 +733,17 @@ public class FavouritesDbHelper {
if (db != null) { if (db != null) {
try { try {
db.execSQL( 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()); FavouritePoint fp = findFavoriteByAllProperties(p.getCategory(), p.getName(), p.getLatitude(), p.getLongitude());
if (fp != null) { if (fp != null) {
FavoriteGroup group = flatGroups.get(p.getCategory()); FavoriteGroup group = flatGroups.get(p.getCategory());
if(group != null) { if (group != null) {
group.points.remove(fp); group.points.remove(fp);
} }
cachedFavoritePoints.remove(fp); cachedFavoritePoints.remove(fp);
} }
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
} finally{ } finally {
db.close(); db.close();
} }
return true; return true;
@ -747,7 +753,7 @@ public class FavouritesDbHelper {
public boolean addFavouriteDB(FavouritePoint p) { public boolean addFavouriteDB(FavouritePoint p) {
if(p.getName().equals("") && flatGroups.containsKey(p.getCategory())){ if (p.getName().equals("") && flatGroups.containsKey(p.getCategory())) {
return true; return true;
} }
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
@ -755,8 +761,8 @@ public class FavouritesDbHelper {
try { try {
db.execSQL( db.execSQL(
"INSERT INTO " + FAVOURITE_TABLE_NAME + " (" + FAVOURITE_COL_NAME + ", " + FAVOURITE_COL_CATEGORY + ", " "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$ + 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); FavoriteGroup group = getOrCreateGroup(p, 0);
if (!p.getName().equals("")) { if (!p.getName().equals("")) {
p.setVisible(group.visible); p.setVisible(group.visible);
p.setColor(group.color); p.setColor(group.color);
@ -773,14 +779,13 @@ public class FavouritesDbHelper {
} }
public boolean editFavouriteNameDB(FavouritePoint p, String newName, String category) { public boolean editFavouriteNameDB(FavouritePoint p, String newName, String category) {
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
if (db != null) { if (db != null) {
try { try {
String oldCategory = p.getCategory(); String oldCategory = p.getCategory();
db.execSQL( 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.setName(newName);
p.setCategory(category); p.setCategory(category);
if (!oldCategory.equals(category)) { if (!oldCategory.equals(category)) {
@ -808,7 +813,7 @@ public class FavouritesDbHelper {
if (db != null) { if (db != null) {
try { try {
db.execSQL( 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.setLatitude(lat);
p.setLongitude(lon); p.setLongitude(lon);
saveCurrentPointsIntoFile(); saveCurrentPointsIntoFile();
@ -826,6 +831,4 @@ public class FavouritesDbHelper {
} }
} }

View file

@ -6,6 +6,7 @@ import android.support.annotation.Nullable;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.LocationPoint; import net.osmand.data.LocationPoint;
import net.osmand.data.PointDescription; import net.osmand.data.PointDescription;
@ -22,6 +23,8 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import static net.osmand.data.PointDescription.POINT_TYPE_MAP_MARKER;
public class MapMarkersHelper { public class MapMarkersHelper {
public static final int MAP_MARKERS_COLORS_COUNT = 7; public static final int MAP_MARKERS_COLORS_COUNT = 7;
@ -64,7 +67,7 @@ public class MapMarkersHelper {
} }
public PointDescription getPointDescription(Context ctx) { 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()); getOnlyName());
} }
@ -283,8 +286,52 @@ public class MapMarkersHelper {
} }
} }
public void syncGroup(MarkersSyncGroup group) { public void syncAllGroups() {
List<MarkersSyncGroup> groups = markersDbHelper.getAllGroups();
for (MarkersSyncGroup gr : groups) {
syncGroup(gr);
}
}
public void syncGroup(MarkersSyncGroup group) {
if (markersDbHelper.getGroup(group.getId()) == null) {
return;
}
List<MapMarker> dbMarkers = markersDbHelper.getMarkersFromGroup(group);
if (group.getType() == MarkersSyncGroup.FAVORITES_TYPE) {
List<FavouritePoint> 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) { public void moveMapMarkerToHistory(MapMarker marker) {
@ -329,7 +376,7 @@ public class MapMarkersHelper {
public void removeMarkerFromHistory(MapMarker marker) { public void removeMarkerFromHistory(MapMarker marker) {
if (marker != null) { if (marker != null) {
markersDbHelper.removeMarkerFromHistory(marker); markersDbHelper.removeMarker(marker, true);
mapMarkersHistory.remove(marker); mapMarkersHistory.remove(marker);
refresh(); refresh();
} }
@ -395,8 +442,10 @@ public class MapMarkersHelper {
public void moveAllActiveMarkersToHistory() { public void moveAllActiveMarkersToHistory() {
cancelAddressRequests(); cancelAddressRequests();
markersDbHelper.moveAllActiveMarkersToHistory(); long timestamp = System.currentTimeMillis();
markersDbHelper.moveAllActiveMarkersToHistory(timestamp);
for (MapMarker marker : mapMarkers) { for (MapMarker marker : mapMarkers) {
marker.visitedDate = timestamp;
marker.history = true; marker.history = true;
marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE; marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE;
} }
@ -413,6 +462,20 @@ public class MapMarkersHelper {
refresh(); 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) { public void addMapMarker(LatLon point, PointDescription historyName) {
addMarkers(Collections.singletonList(point), Collections.singletonList(historyName), null); addMarkers(Collections.singletonList(point), Collections.singletonList(historyName), null);
} }
@ -421,10 +484,6 @@ public class MapMarkersHelper {
addMarkers(points, historyNames, group); addMarkers(points, historyNames, group);
} }
public void addMapMarkers(List<LatLon> points, List<PointDescription> historyNames) {
addMarkers(points, historyNames, null);
}
private void addMarkers(List<LatLon> points, List<PointDescription> historyNames, @Nullable MarkersSyncGroup group) { private void addMarkers(List<LatLon> points, List<PointDescription> historyNames, @Nullable MarkersSyncGroup group) {
if (points.size() > 0) { if (points.size() > 0) {
int colorIndex = -1; int colorIndex = -1;
@ -452,11 +511,12 @@ public class MapMarkersHelper {
MapMarker marker = new MapMarker(point, pointDescription, colorIndex, false, 0); MapMarker marker = new MapMarker(point, pointDescription, colorIndex, false, 0);
if (group != null) { if (group != null) {
if (markersDbHelper.getGroup(group.getId()) == null) { marker.id = group.getId() + marker.getName(ctx) + marker.getLatitude() + marker.getLongitude();
markersDbHelper.addGroup(group.getId(), group.getName(), group.getType()); if (markersDbHelper.getMarker(marker.id) != null) {
continue;
} }
marker.id = group.getId() + marker.getName(ctx);
marker.groupName = group.getName(); marker.groupName = group.getName();
marker.groupKey = group.getId();
} }
marker.history = false; marker.history = false;
marker.nextKey = MapMarkersDbHelper.TAIL_NEXT_VALUE; marker.nextKey = MapMarkersDbHelper.TAIL_NEXT_VALUE;

View file

@ -189,7 +189,9 @@ public class EditFavoriteGroupDialogFragment extends BottomSheetDialogFragment {
points.add(new LatLon(fp.getLatitude(), fp.getLongitude())); points.add(new LatLon(fp.getLatitude(), fp.getLongitude()));
names.add(new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, fp.getName())); 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(); dismiss();
MapActivity.launchMapActivityMoveToTop(getActivity()); MapActivity.launchMapActivityMoveToTop(getActivity());
} }

View file

@ -406,10 +406,10 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment {
List<LatLon> points = new LinkedList<>(); List<LatLon> points = new LinkedList<>();
List<PointDescription> names = new LinkedList<>(); List<PointDescription> names = new LinkedList<>();
for (Map.Entry<String, Set<FavouritePoint>> entry : favoritesSelected.entrySet()) { for (Map.Entry<String, Set<FavouritePoint>> entry : favoritesSelected.entrySet()) {
MarkersSyncGroup syncGr = null;
FavoriteGroup favGr = helper.getGroup(entry.getKey()); FavoriteGroup favGr = helper.getGroup(entry.getKey());
MarkersSyncGroup syncGr = new MarkersSyncGroup(favGr.name, favGr.name, MarkersSyncGroup.FAVORITES_TYPE);
if (entry.getValue().size() == favGr.points.size()) { 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()) { for (FavouritePoint fp : entry.getValue()) {
points.add(new LatLon(fp.getLatitude(), fp.getLongitude())); points.add(new LatLon(fp.getLatitude(), fp.getLongitude()));

View file

@ -164,6 +164,25 @@ public class MapMarkersDbHelper {
} }
} }
public List<MarkersSyncGroup> getAllGroups() {
List<MarkersSyncGroup> 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 @Nullable
public MarkersSyncGroup getGroup(String id) { public MarkersSyncGroup getGroup(String id) {
MarkersSyncGroup res = null; MarkersSyncGroup res = null;
@ -190,6 +209,22 @@ public class MapMarkersDbHelper {
return new MarkersSyncGroup(id, name, type); 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) { public void addMarker(MapMarker marker) {
addMarker(marker, false); addMarker(marker, false);
} }
@ -239,6 +274,44 @@ public class MapMarkersDbHelper {
marker.history ? HISTORY_NEXT_VALUE : TAIL_NEXT_VALUE}); marker.history ? HISTORY_NEXT_VALUE : TAIL_NEXT_VALUE});
} }
public List<MapMarker> getMarkersFromGroup(MarkersSyncGroup group) {
List<MapMarker> 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<MapMarker> getActiveMarkers() { public List<MapMarker> getActiveMarkers() {
List<MapMarker> res = new LinkedList<>(); List<MapMarker> res = new LinkedList<>();
HashMap<String, MapMarker> markers = new LinkedHashMap<>(); HashMap<String, MapMarker> markers = new LinkedHashMap<>();
@ -351,16 +424,15 @@ public class MapMarkersDbHelper {
} }
} }
public void moveAllActiveMarkersToHistory() { public void moveAllActiveMarkersToHistory(long timestamp) {
SQLiteConnection db = openConnection(false); SQLiteConnection db = openConnection(false);
if (db != null) { if (db != null) {
try { try {
long visitedDate = System.currentTimeMillis();
db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " + db.execSQL("UPDATE " + MARKERS_TABLE_NAME + " SET " +
MARKERS_COL_ACTIVE + " = ?, " + MARKERS_COL_ACTIVE + " = ?, " +
MARKERS_COL_VISITED + " = ?, " + MARKERS_COL_VISITED + " = ?, " +
MARKERS_COL_NEXT_KEY + " = ? " + 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 { } finally {
db.close(); db.close();
} }
@ -402,14 +474,14 @@ public class MapMarkersDbHelper {
return markers; return markers;
} }
public void removeMarkerFromHistory(MapMarker marker) { public void removeMarker(MapMarker marker, boolean history) {
SQLiteConnection db = openConnection(true); SQLiteConnection db = openConnection(true);
if (db != null) { if (db != null) {
try { try {
db.execSQL("DELETE FROM " + MARKERS_TABLE_NAME + db.execSQL("DELETE FROM " + MARKERS_TABLE_NAME +
" WHERE " + MARKERS_COL_ID + " = ?" + " WHERE " + MARKERS_COL_ID + " = ?" +
" AND " + MARKERS_COL_ACTIVE + " = ?", " AND " + MARKERS_COL_ACTIVE + " = ?",
new Object[]{marker.id, 0}); new Object[]{marker.id, history ? 0 : 1});
} finally { } finally {
db.close(); db.close();
} }

View file

@ -610,6 +610,7 @@ public class TrackPointFragment extends OsmandExpandableListFragment {
} }
File gpx = getGpxDataItem().getFile(); File gpx = getGpxDataItem().getFile();
MarkersSyncGroup syncGroup = new MarkersSyncGroup(gpx.getAbsolutePath(), trimExtension(gpx.getName()), MarkersSyncGroup.GPX_TYPE); MarkersSyncGroup syncGroup = new MarkersSyncGroup(gpx.getAbsolutePath(), trimExtension(gpx.getName()), MarkersSyncGroup.GPX_TYPE);
markersHelper.addMarkersSyncGroup(syncGroup);
markersHelper.addMapMarkers(points, names, syncGroup); markersHelper.addMapMarkers(points, names, syncGroup);
MapActivity.launchMapActivityMoveToTop(getActivity()); MapActivity.launchMapActivityMoveToTop(getActivity());
} else { } else {