Add sync
This commit is contained in:
parent
14b70827f1
commit
d4acaad187
7 changed files with 221 additions and 82 deletions
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
@ -106,26 +108,31 @@ public class FavouritesDbHelper {
|
|||
}
|
||||
|
||||
|
||||
|
||||
private File getInternalFile() {
|
||||
return context.getFileStreamPath(FILE_TO_BACKUP);
|
||||
}
|
||||
|
||||
public void delete(Set<FavoriteGroup> groupsToDelete, Set<FavouritePoint> favoritesSelected) {
|
||||
if (favoritesSelected != null) {
|
||||
Set<FavoriteGroup> 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;
|
||||
}
|
||||
|
@ -231,8 +240,7 @@ public class FavouritesDbHelper {
|
|||
index += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else if ((int)ch1 == 0xD83D) {
|
||||
} else if ((int) ch1 == 0xD83D) {
|
||||
ch2 = chars[index + 1];
|
||||
if ((int) ch2 >= 0xDC00 && (int) ch2 <= 0xDDFF) {
|
||||
index += 2;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -312,7 +321,6 @@ public class FavouritesDbHelper {
|
|||
}
|
||||
|
||||
|
||||
|
||||
private String saveExternalFile(Set<String> deleted) {
|
||||
Map<String, FavouritePoint> all = new LinkedHashMap<String, FavouritePoint>();
|
||||
loadGPXFile(getExternalFile(), all);
|
||||
|
@ -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;
|
||||
|
@ -489,7 +495,6 @@ public class FavouritesDbHelper {
|
|||
}
|
||||
|
||||
|
||||
|
||||
public void recalculateCachedFavPoints() {
|
||||
ArrayList<FavouritePoint> temp = new ArrayList<FavouritePoint>();
|
||||
for (FavoriteGroup f : favoriteGroups) {
|
||||
|
@ -584,6 +589,7 @@ public class FavouritesDbHelper {
|
|||
return true;
|
||||
}
|
||||
|
||||
//todo
|
||||
public void editFavouriteGroup(FavoriteGroup group, String newName, int color, boolean visible) {
|
||||
if (color != 0 && group.color != color) {
|
||||
FavoriteGroup gr = flatGroups.get(group.name);
|
||||
|
@ -773,7 +779,6 @@ public class FavouritesDbHelper {
|
|||
}
|
||||
|
||||
|
||||
|
||||
public boolean editFavouriteNameDB(FavouritePoint p, String newName, String category) {
|
||||
SQLiteConnection db = openConnection(false);
|
||||
if (db != null) {
|
||||
|
@ -826,6 +831,4 @@ public class FavouritesDbHelper {
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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<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) {
|
||||
|
@ -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<LatLon> points, List<PointDescription> historyNames) {
|
||||
addMarkers(points, historyNames, null);
|
||||
}
|
||||
|
||||
private void addMarkers(List<LatLon> points, List<PointDescription> 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;
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -406,10 +406,10 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment {
|
|||
List<LatLon> points = new LinkedList<>();
|
||||
List<PointDescription> names = new LinkedList<>();
|
||||
for (Map.Entry<String, Set<FavouritePoint>> 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()));
|
||||
|
|
|
@ -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
|
||||
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<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() {
|
||||
List<MapMarker> res = new LinkedList<>();
|
||||
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);
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue