diff --git a/OsmAnd/res/layout/fragment_map_markers_dialog.xml b/OsmAnd/res/layout/fragment_map_markers_dialog.xml
index 08e4bcd12d..eb8ebecc96 100644
--- a/OsmAnd/res/layout/fragment_map_markers_dialog.xml
+++ b/OsmAnd/res/layout/fragment_map_markers_dialog.xml
@@ -30,16 +30,34 @@
app:contentInsetLeft="54dp"
app:contentInsetStart="54dp">
-
+
+
+
+
+
+
+
+
diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml
index e2926e4430..cd52e1afbc 100644
--- a/OsmAnd/res/values/strings.xml
+++ b/OsmAnd/res/values/strings.xml
@@ -9,6 +9,8 @@
3. All your modified/created strings are in the top of the file (to make easier find what\'s translated).
PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
-->
+ You can add all of the track\'s waypoints, or select separate categories.
+ Total
Clear all intermediate points
Group deleted
Whitewater sports
diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java
index d591287c2f..2c9515ce09 100644
--- a/OsmAnd/src/net/osmand/plus/AppInitializer.java
+++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java
@@ -603,7 +603,6 @@ public class AppInitializer implements IProgress {
notifyEvent(InitEvents.LOAD_GPX_TRACKS);
saveGPXTracks();
notifyEvent(InitEvents.SAVE_GPX_TRACKS);
- app.mapMarkersHelper.syncAllGroupsAsync();
// restore backuped favorites to normal file
restoreBackupForFavoritesFiles();
notifyEvent(InitEvents.RESTORE_BACKUPS);
diff --git a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java
index fd8d7c9b74..87a95eb7cc 100644
--- a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java
+++ b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java
@@ -10,7 +10,6 @@ import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
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;
@@ -110,6 +109,20 @@ public class FavouritesDbHelper {
return changed;
}
+ private void runSyncWithMarkers(FavoriteGroup favGroup) {
+ MapMarkersHelper helper = context.getMapMarkersHelper();
+ helper.runSynchronization(helper.getOrCreateGroup(favGroup));
+ }
+
+ private void removeFromMarkers(FavoriteGroup favGroup) {
+ MapMarkersHelper helper = context.getMapMarkersHelper();
+ helper.removeMarkersGroup(helper.getOrCreateGroup(favGroup));
+ }
+
+ private void addToMarkers(FavoriteGroup favGroup) {
+ MapMarkersHelper helper = context.getMapMarkersHelper();
+ helper.syncWithMarkers(helper.getOrCreateGroup(favGroup));
+ }
private File getInternalFile() {
return context.getFileStreamPath(FILE_TO_BACKUP);
@@ -127,7 +140,7 @@ public class FavouritesDbHelper {
cachedFavoritePoints.remove(p);
}
for (FavoriteGroup gr : groupsToSync) {
- context.getMapMarkersHelper().syncGroupAsync(new MarkersSyncGroup(gr.name, gr.name, MarkersSyncGroup.FAVORITES_TYPE));
+ runSyncWithMarkers(gr);
}
}
if (groupsToDelete != null) {
@@ -135,7 +148,7 @@ public class FavouritesDbHelper {
flatGroups.remove(g.name);
favoriteGroups.remove(g);
cachedFavoritePoints.removeAll(g.points);
- context.getMapMarkersHelper().removeMarkersSyncGroup(g.name);
+ removeFromMarkers(g);
}
}
saveCurrentPointsIntoFile();
@@ -150,7 +163,7 @@ public class FavouritesDbHelper {
FavoriteGroup group = flatGroups.get(p.getCategory());
if (group != null) {
group.points.remove(p);
- context.getMapMarkersHelper().syncGroupAsync(new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE));
+ runSyncWithMarkers(group);
}
cachedFavoritePoints.remove(p);
}
@@ -180,7 +193,7 @@ public class FavouritesDbHelper {
sortAll();
saveCurrentPointsIntoFile();
}
- context.getMapMarkersHelper().syncGroupAsync(new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE, group.color));
+ runSyncWithMarkers(group);
return true;
}
@@ -275,7 +288,7 @@ public class FavouritesDbHelper {
}
sortAll();
saveCurrentPointsIntoFile();
- context.getMapMarkersHelper().syncGroupAsync(new MarkersSyncGroup(category, category, MarkersSyncGroup.FAVORITES_TYPE, p.getColor()));
+ runSyncWithMarkers(getOrCreateGroup(p, 0));
return true;
}
@@ -283,7 +296,7 @@ public class FavouritesDbHelper {
p.setLatitude(lat);
p.setLongitude(lon);
saveCurrentPointsIntoFile();
- context.getMapMarkersHelper().syncGroupAsync(new MarkersSyncGroup(p.getCategory(), p.getCategory(), MarkersSyncGroup.FAVORITES_TYPE, p.getColor()));
+ runSyncWithMarkers(getOrCreateGroup(p, 0));
return true;
}
@@ -352,7 +365,7 @@ public class FavouritesDbHelper {
if (remove) {
flatGroups.remove(group.name);
saveCurrentPointsIntoFile();
- context.getMapMarkersHelper().removeMarkersSyncGroup(group.name);
+ removeFromMarkers(group);
return true;
}
return false;
@@ -603,14 +616,13 @@ public class FavouritesDbHelper {
}
public void editFavouriteGroup(FavoriteGroup group, String newName, int color, boolean visible) {
- MapMarkersHelper markersHelper = context.getMapMarkersHelper();
if (color != 0 && group.color != color) {
FavoriteGroup gr = flatGroups.get(group.name);
group.color = color;
for (FavouritePoint p : gr.points) {
p.setColor(color);
}
- markersHelper.syncGroupAsync(new MarkersSyncGroup(gr.name, gr.name, MarkersSyncGroup.FAVORITES_TYPE, color));
+ runSyncWithMarkers(gr);
}
if (group.visible != visible) {
FavoriteGroup gr = flatGroups.get(group.name);
@@ -618,11 +630,11 @@ public class FavouritesDbHelper {
for (FavouritePoint p : gr.points) {
p.setVisible(visible);
}
- markersHelper.syncGroupAsync(new MarkersSyncGroup(gr.name, gr.name, MarkersSyncGroup.FAVORITES_TYPE, group.color));
+ runSyncWithMarkers(gr);
}
if (!group.name.equals(newName)) {
FavoriteGroup gr = flatGroups.remove(group.name);
- markersHelper.removeMarkersSyncGroup(group.name);
+ removeFromMarkers(gr);
gr.name = newName;
FavoriteGroup renamedGroup = flatGroups.get(gr.name);
boolean existing = renamedGroup != null;
@@ -638,9 +650,7 @@ public class FavouritesDbHelper {
renamedGroup.points.add(p);
}
}
- MarkersSyncGroup syncGroup = new MarkersSyncGroup(renamedGroup.name, renamedGroup.name, MarkersSyncGroup.FAVORITES_TYPE, group.color);
- markersHelper.addMarkersSyncGroup(syncGroup);
- markersHelper.syncGroupAsync(syncGroup);
+ addToMarkers(renamedGroup);
}
saveCurrentPointsIntoFile();
}
diff --git a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java
index 0dc42d50a9..2022567095 100644
--- a/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java
+++ b/OsmAnd/src/net/osmand/plus/GpxSelectionHelper.java
@@ -6,7 +6,6 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
-import net.osmand.AndroidUtils;
import net.osmand.IProgress;
import net.osmand.data.LatLon;
import net.osmand.plus.GPXDatabase.GpxDataItem;
@@ -16,8 +15,6 @@ import net.osmand.plus.GPXUtilities.Route;
import net.osmand.plus.GPXUtilities.Track;
import net.osmand.plus.GPXUtilities.TrkSegment;
import net.osmand.plus.GPXUtilities.WptPt;
-import net.osmand.plus.MapMarkersHelper.MarkersSyncGroup;
-import net.osmand.plus.MapMarkersHelper.OnGroupSyncedListener;
import net.osmand.plus.OsmandSettings.MetricsConstants;
import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.helpers.GpxUiHelper;
@@ -502,7 +499,7 @@ public class GpxSelectionHelper {
}
}
if (syncGroup) {
- syncGpx(gpx, true, null);
+ syncGpx(gpx);
}
return sf;
}
@@ -539,25 +536,10 @@ public class GpxSelectionHelper {
}
private void syncGpx(GPXFile gpxFile) {
- syncGpx(gpxFile, false, null);
- }
-
- public void syncGpx(GPXFile gpxFile, boolean createOrDeleteGroup, @Nullable OnGroupSyncedListener callback) {
File gpx = new File(gpxFile.path);
if (gpx.exists()) {
MapMarkersHelper mapMarkersHelper = app.getMapMarkersHelper();
- MarkersSyncGroup syncGroup = new MarkersSyncGroup(gpx.getAbsolutePath(), AndroidUtils.trimExtension(gpx.getName()), MarkersSyncGroup.GPX_TYPE);
- boolean enabled = true;
- if (createOrDeleteGroup) {
- boolean show = getSelectedFileByPath(gpx.getAbsolutePath()) != null;
- enabled = mapMarkersHelper.isGroupSynced(gpx.getAbsolutePath());
- if (show && !enabled) {
- mapMarkersHelper.addMarkersSyncGroup(syncGroup);
- } else if (!show && mapMarkersHelper.isGroupDisabled(gpx.getAbsolutePath())) {
- mapMarkersHelper.removeMarkersSyncGroup(gpx.getAbsolutePath());
- }
- }
- mapMarkersHelper.syncGroupAsync(syncGroup, enabled, callback);
+ mapMarkersHelper.runSynchronization(mapMarkersHelper.getOrCreateGroup(gpx));
}
}
diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java
index 54dea59630..52ab9f268b 100644
--- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java
+++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java
@@ -7,6 +7,7 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
+import net.osmand.AndroidUtils;
import net.osmand.IndexConstants;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
@@ -15,6 +16,7 @@ import net.osmand.data.PointDescription;
import net.osmand.plus.FavouritesDbHelper.FavoriteGroup;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt;
+import net.osmand.plus.GeocodingLookupService.AddressLookupRequest;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.mapmarkers.MapMarkersDbHelper;
import net.osmand.plus.mapmarkers.MarkersPlanRouteContext;
@@ -27,9 +29,11 @@ import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
-import java.util.LinkedHashMap;
+import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -61,11 +65,947 @@ public class MapMarkersHelper {
private List mapMarkersGroups = new ArrayList<>();
private List listeners = new ArrayList<>();
+ private Set syncListeners = new HashSet<>();
private boolean startFromMyLocation;
private MarkersPlanRouteContext planRouteContext;
+ public List getMapMarkers() {
+ return mapMarkers;
+ }
+
+ public List getMapMarkersHistory() {
+ return mapMarkersHistory;
+ }
+
+ public List getMapMarkersGroups() {
+ return mapMarkersGroups;
+ }
+
+ public boolean isStartFromMyLocation() {
+ return startFromMyLocation;
+ }
+
+ public void setStartFromMyLocation(boolean startFromMyLocation) {
+ this.startFromMyLocation = startFromMyLocation;
+ settings.ROUTE_MAP_MARKERS_START_MY_LOC.set(startFromMyLocation);
+ }
+
+ public MarkersPlanRouteContext getPlanRouteContext() {
+ return planRouteContext;
+ }
+
+ public MapMarkersHelper(OsmandApplication ctx) {
+ this.ctx = ctx;
+ settings = ctx.getSettings();
+ markersDbHelper = ctx.getMapMarkersDbHelper();
+ planRouteContext = new MarkersPlanRouteContext(ctx);
+ startFromMyLocation = settings.ROUTE_MAP_MARKERS_START_MY_LOC.get();
+ markersDbHelper.removeDisabledGroups();
+ loadMarkers();
+ loadGroups();
+ }
+
+ private void loadMarkers() {
+ mapMarkers = new ArrayList<>();
+ mapMarkersHistory = new ArrayList<>();
+
+ addToMapMarkersList(markersDbHelper.getActiveMarkers());
+ reorderActiveMarkersIfNeeded();
+
+ List markersHistory = markersDbHelper.getMarkersHistory();
+ sortMarkers(markersHistory, true, BY_DATE_ADDED_DESC);
+ addToMapMarkersHistoryList(markersHistory);
+
+ if (!ctx.isApplicationInitializing()) {
+ lookupAddressAll();
+ }
+ }
+
+ private void loadGroups() {
+ Map groupsMap = markersDbHelper.getAllGroupsMap();
+ List allMarkers = new ArrayList<>(mapMarkers);
+ allMarkers.addAll(mapMarkersHistory);
+
+ MapMarkersGroup noGroup = null;
+
+ for (MapMarker marker : allMarkers) {
+ MapMarkersGroup group = groupsMap.get(marker.groupKey);
+ if (group == null) {
+ if (noGroup == null) {
+ noGroup = new MapMarkersGroup();
+ noGroup.creationDate = Long.MAX_VALUE;
+ }
+ noGroup.getMarkers().add(marker);
+ } else {
+ if (marker.creationDate < group.creationDate) {
+ group.creationDate = marker.creationDate;
+ }
+ group.getMarkers().add(marker);
+ }
+ }
+
+ mapMarkersGroups = new ArrayList<>(groupsMap.values());
+ if (noGroup != null) {
+ sortMarkers(noGroup.getMarkers(), false, BY_DATE_ADDED_DESC);
+ addToGroupsList(noGroup);
+ }
+
+ sortGroups();
+
+ for (MapMarkersGroup group : mapMarkersGroups) {
+ updateGroup(group);
+ }
+
+ syncAllGroupsAsync();
+ }
+
+ private void syncAllGroupsAsync() {
+ for (MapMarkersGroup gr : mapMarkersGroups) {
+ if (gr.getId() != null && gr.getName() != null) {
+ runSynchronization(gr);
+ }
+ }
+ }
+
+ public void lookupAddressAll() {
+ for (MapMarker mapMarker : mapMarkers) {
+ lookupAddress(mapMarker);
+ }
+ for (MapMarker mapMarker : mapMarkersHistory) {
+ lookupAddress(mapMarker);
+ }
+ }
+
+ private void lookupAddress(final MapMarker mapMarker) {
+ if (mapMarker != null && mapMarker.pointDescription.isSearchingAddress(ctx)) {
+ cancelPointAddressRequests(mapMarker.point);
+ AddressLookupRequest lookupRequest = new AddressLookupRequest(mapMarker.point,
+ new GeocodingLookupService.OnAddressLookupResult() {
+ @Override
+ public void geocodingDone(String address) {
+ if (Algorithms.isEmpty(address)) {
+ mapMarker.pointDescription.setName(PointDescription.getAddressNotFoundStr(ctx));
+ } else {
+ mapMarker.pointDescription.setName(address);
+ }
+ markersDbHelper.updateMarker(mapMarker);
+ refreshMarker(mapMarker);
+ }
+ }, null);
+ ctx.getGeocodingLookupService().lookupAddress(lookupRequest);
+ }
+ }
+
+ private void cancelAddressRequests() {
+ List markers = new ArrayList<>(mapMarkers);
+ for (MapMarker m : markers) {
+ cancelPointAddressRequests(m.point);
+ }
+ markers = new ArrayList<>(mapMarkersHistory);
+ for (MapMarker m : markers) {
+ cancelPointAddressRequests(m.point);
+ }
+ }
+
+ private void cancelPointAddressRequests(LatLon latLon) {
+ if (latLon != null) {
+ ctx.getGeocodingLookupService().cancel(latLon);
+ }
+ }
+
+ public void reorderActiveMarkersIfNeeded() {
+ if (!mapMarkers.isEmpty()) {
+ if (mapMarkers.size() > 1) {
+ for (int i = 0; i < mapMarkers.size() - 1; i++) {
+ MapMarker first = mapMarkers.get(i);
+ MapMarker second = mapMarkers.get(i + 1);
+ if (!first.nextKey.equals(second.id)) {
+ markersDbHelper.changeActiveMarkerPosition(first, second);
+ first.nextKey = second.id;
+ }
+ }
+ }
+
+ MapMarker tail = mapMarkers.get(mapMarkers.size() - 1);
+ if (!tail.nextKey.equals(MapMarkersDbHelper.TAIL_NEXT_VALUE)) {
+ markersDbHelper.changeActiveMarkerPosition(tail, null);
+ }
+ }
+ }
+
+ public void sortMarkers(final @MapMarkersSortByDef int sortByMode, LatLon location) {
+ sortMarkers(getMapMarkers(), false, sortByMode, location);
+ reorderActiveMarkersIfNeeded();
+ }
+
+ private void sortMarkers(List markers, final boolean visited, final @MapMarkersSortByDef int sortByMode) {
+ sortMarkers(markers, visited, sortByMode, null);
+ }
+
+ private void sortMarkers(List markers,
+ final boolean visited,
+ final @MapMarkersSortByDef int sortByMode,
+ @Nullable final LatLon location) {
+ Collections.sort(markers, new Comparator() {
+ @Override
+ public int compare(MapMarker mapMarker1, MapMarker mapMarker2) {
+ if (sortByMode == BY_DATE_ADDED_DESC || sortByMode == BY_DATE_ADDED_ASC) {
+ long t1 = visited ? mapMarker1.visitedDate : mapMarker1.creationDate;
+ long t2 = visited ? mapMarker2.visitedDate : mapMarker2.creationDate;
+ if (t1 > t2) {
+ return sortByMode == BY_DATE_ADDED_DESC ? -1 : 1;
+ } else if (t1 == t2) {
+ return 0;
+ } else {
+ return sortByMode == BY_DATE_ADDED_DESC ? 1 : -1;
+ }
+ } else if (location != null && (sortByMode == BY_DISTANCE_DESC || sortByMode == BY_DISTANCE_ASC)) {
+ int d1 = (int) MapUtils.getDistance(location, mapMarker1.getLatitude(), mapMarker1.getLongitude());
+ int d2 = (int) MapUtils.getDistance(location, mapMarker2.getLatitude(), mapMarker2.getLongitude());
+ if (d1 > d2) {
+ return sortByMode == BY_DISTANCE_DESC ? -1 : 1;
+ } else if (d1 == d2) {
+ return 0;
+ } else {
+ return sortByMode == BY_DISTANCE_DESC ? 1 : -1;
+ }
+ } else {
+ String n1 = mapMarker1.getName(ctx);
+ String n2 = mapMarker2.getName(ctx);
+ return n1.compareToIgnoreCase(n2);
+ }
+ }
+ });
+ }
+
+ public boolean isGroupSynced(String id) {
+ return getMapMarkerGroupById(id) != null;
+ }
+
+ public void runSynchronization(@NonNull final MapMarkersGroup group) {
+ ctx.runInUIThread(new Runnable() {
+ @Override
+ public void run() {
+ new SyncGroupTask(group).executeOnExecutor(executorService);
+ }
+ });
+ }
+
+ public boolean isSynced(SelectedGpxFile gpxFile) {
+ List gpxPoints = gpxFile.getGpxFile().getPoints();
+ for (WptPt wptPt : gpxPoints) {
+ if (getMapMarker(wptPt) != null) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public boolean isSynced(FavouritePoint favouritePoint) {
+ return getMapMarker(favouritePoint) != null;
+ }
+
+ public void syncWithMarkers(@NonNull MapMarkersGroup group) {
+ if (!isGroupSynced(group.getId())) {
+ markersDbHelper.addGroup(group);
+ addToGroupsList(group);
+ } else if (group.wptCategories != null) {
+ markersDbHelper.updateGroupCategories(group.getId(), group.getWptCategoriesString());
+ }
+ runSynchronization(group);
+ }
+
+ public void removeMarkersGroup(MapMarkersGroup group) {
+ if (group != null) {
+ markersDbHelper.removeMarkersGroup(group.getId());
+ removeGroupActiveMarkers(group, false);
+ removeFromGroupsList(group);
+ }
+ }
+
+ public void updateGroupDisabled(@NonNull MapMarkersGroup group, boolean disabled) {
+ String id = group.getId();
+ if (id != null) {
+ markersDbHelper.updateGroupDisabled(id, disabled);
+ group.disabled = disabled;
+ }
+ }
+
+ private void removeGroupActiveMarkers(MapMarkersGroup group, boolean updateGroup) {
+ if (group != null) {
+ markersDbHelper.removeActiveMarkersFromGroup(group.getId());
+ removeFromMapMarkersList(group.getActiveMarkers());
+ if (updateGroup) {
+ group.markers = group.getHistoryMarkers();
+ updateGroup(group);
+ }
+ reorderActiveMarkersIfNeeded();
+ refresh();
+ }
+ }
+
+ public void updateGroups() {
+ for (MapMarkersGroup group : mapMarkersGroups) {
+ updateGroup(group);
+ }
+ }
+
+ public void updateGroup(MapMarkersGroup mapMarkersGroup) {
+ if (mapMarkersGroup.getId() == null || mapMarkersGroup.getName() == null) {
+ return;
+ }
+ createHeaderInGroup(mapMarkersGroup);
+ int historyMarkersCount = mapMarkersGroup.getHistoryMarkers().size();
+ ShowHideHistoryButton showHideHistoryButton = mapMarkersGroup.getShowHideHistoryButton();
+ if (showHideHistoryButton != null) {
+ if (historyMarkersCount == 0) {
+ mapMarkersGroup.showHideHistoryButton = null;
+ }
+ } else if (historyMarkersCount > 0) {
+ showHideHistoryButton = new ShowHideHistoryButton();
+ showHideHistoryButton.showHistory = false;
+ mapMarkersGroup.showHideHistoryButton = showHideHistoryButton;
+ }
+ }
+
+ private void addMarkersToGroups(@NonNull List markers) {
+ for (MapMarker marker : markers) {
+ addMarkerToGroup(marker);
+ }
+ }
+
+ private void addMarkerToGroup(MapMarker marker) {
+ if (marker != null) {
+ MapMarkersGroup mapMarkersGroup = getMapMarkerGroupById(marker.groupKey);
+ if (mapMarkersGroup != null) {
+ mapMarkersGroup.getMarkers().add(marker);
+ updateGroup(mapMarkersGroup);
+ if (mapMarkersGroup.getName() == null) {
+ sortMarkers(mapMarkersGroup.getMarkers(), false, BY_DATE_ADDED_DESC);
+ }
+ } else {
+ mapMarkersGroup = new MapMarkersGroup();
+ mapMarkersGroup.id = marker.groupKey;
+ mapMarkersGroup.name = marker.groupName;
+ mapMarkersGroup.creationDate = Long.MAX_VALUE;
+ mapMarkersGroup.getMarkers().add(marker);
+ addToGroupsList(mapMarkersGroup);
+ sortGroups();
+ updateGroup(mapMarkersGroup);
+ }
+ }
+ }
+
+ private void createHeaderInGroup(@NonNull MapMarkersGroup group) {
+ GroupHeader header = new GroupHeader();
+ int type = group.getType();
+ if (type != -1) {
+ header.iconRes = type == MapMarkersGroup.FAVORITES_TYPE
+ ? R.drawable.ic_action_fav_dark : R.drawable.ic_action_polygom_dark;
+ }
+ header.group = group;
+ group.header = header;
+ }
+
+ private void removeMarkerFromGroup(MapMarker marker) {
+ if (marker != null) {
+ MapMarkersGroup mapMarkersGroup = getMapMarkerGroupById(marker.groupKey);
+ if (mapMarkersGroup != null) {
+ mapMarkersGroup.getMarkers().remove(marker);
+ updateGroup(mapMarkersGroup);
+ }
+ }
+ }
+
+ private void sortGroups() {
+ if (mapMarkersGroups.size() > 0) {
+ Collections.sort(mapMarkersGroups, new Comparator() {
+ @Override
+ public int compare(MapMarkersGroup group1, MapMarkersGroup group2) {
+ long t1 = group1.creationDate;
+ long t2 = group2.creationDate;
+ return (t1 > t2) ? -1 : ((t1 == t2) ? 0 : 1);
+ }
+ });
+ }
+ }
+
+ @Nullable
+ public MapMarkersGroup getMapMarkerGroupById(String id) {
+ for (MapMarkersGroup group : mapMarkersGroups) {
+ if ((id == null && group.getId() == null)
+ || (group.getId() != null && group.getId().equals(id))) {
+ return group;
+ }
+ }
+ return null;
+ }
+
+ public MapMarkersGroup getOrCreateGroup(@NonNull FavoriteGroup favGroup) {
+ MapMarkersGroup group = getMapMarkerGroupById(favGroup.name);
+ if (group == null) {
+ group = new MapMarkersGroup(favGroup.name, favGroup.name, MapMarkersGroup.FAVORITES_TYPE);
+ }
+ return group;
+ }
+
+ public MapMarkersGroup getOrCreateGroup(@NonNull File gpx) {
+ MapMarkersGroup group = getMapMarkerGroupById(gpx.getAbsolutePath());
+ if (group == null) {
+ group = new MapMarkersGroup(gpx.getAbsolutePath(),
+ AndroidUtils.trimExtension(gpx.getName()),
+ MapMarkersGroup.GPX_TYPE);
+ }
+ return group;
+ }
+
+ @NonNull
+ public List getGroupsForDisplayedGpx() {
+ List res = new ArrayList<>();
+ List selectedGpxFiles = ctx.getSelectedGpxHelper().getSelectedGPXFiles();
+ for (SelectedGpxFile selected : selectedGpxFiles) {
+ MapMarkersGroup group = getOrCreateGroup(new File(selected.getGpxFile().path));
+ if (!isGroupSynced(group.getId())) {
+ group.disabled = true;
+ createHeaderInGroup(group);
+ res.add(group);
+ }
+ }
+ return res;
+ }
+
+ @Nullable
+ public MapMarker getMapMarker(WptPt wptPt) {
+ for (MapMarker marker : mapMarkers) {
+ if (marker.wptPt == wptPt) {
+ return marker;
+ }
+ }
+ return null;
+ }
+
+ @Nullable
+ public MapMarker getMapMarker(FavouritePoint favouritePoint) {
+ for (MapMarker marker : mapMarkers) {
+ if (marker.favouritePoint == favouritePoint) {
+ return marker;
+ }
+ }
+ return null;
+ }
+
+ @Nullable
+ public MapMarker getMapMarker(@NonNull LatLon latLon) {
+ for (MapMarker marker : mapMarkers) {
+ if (marker.point != null && marker.point.equals(latLon)) {
+ return marker;
+ }
+ }
+ return null;
+ }
+
+ @Nullable
+ public MapMarker getMapMarker(@NonNull String mapObjectName, @NonNull LatLon latLon) {
+ for (MapMarker marker : mapMarkers) {
+ if (marker.mapObjectName != null
+ && marker.point != null
+ && marker.mapObjectName.equals(mapObjectName)
+ && MapUtils.getDistance(latLon, marker.point) < 15) {
+ return marker;
+ }
+ }
+ return null;
+ }
+
+ private void addNewMarkerIfNeeded(@NonNull MapMarkersGroup group,
+ @NonNull List groupMarkers,
+ @NonNull LatLon latLon,
+ @NonNull String name,
+ @Nullable FavouritePoint favouritePoint,
+ @Nullable WptPt wptPt) {
+ boolean exists = false;
+
+ Iterator iterator = groupMarkers.iterator();
+ while (iterator.hasNext()) {
+ MapMarker marker = iterator.next();
+ if (marker.id.equals(group.getId() + name)) {
+ exists = true;
+ marker.favouritePoint = favouritePoint;
+ marker.wptPt = wptPt;
+ if (!marker.history && !marker.point.equals(latLon)) {
+ marker.point = latLon;
+ updateMapMarker(marker, true);
+ }
+ iterator.remove();
+ break;
+ }
+ }
+
+ if (!exists) {
+ // TODO create method add1Marker
+ addMarkers(Collections.singletonList(latLon),
+ Collections.singletonList(new PointDescription(POINT_TYPE_MAP_MARKER, name)),
+ group,
+ Collections.singletonList(favouritePoint),
+ Collections.singletonList(wptPt),
+ null);
+ }
+ }
+
+ private void removeOldMarkersIfPresent(List markers) {
+ if (!markers.isEmpty()) {
+ boolean needRefresh = false;
+ for (MapMarker marker : markers) {
+ if (!marker.history) {
+ removeMarker(marker, false);
+ needRefresh = true;
+ }
+ }
+ if (needRefresh) {
+ reorderActiveMarkersIfNeeded();
+ refresh();
+ }
+ }
+ }
+
+ public void moveMapMarkerToHistory(MapMarker marker) {
+ if (marker != null) {
+ cancelPointAddressRequests(marker.point);
+ markersDbHelper.moveMarkerToHistory(marker);
+ removeFromMapMarkersList(marker);
+ marker.history = true;
+ marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE;
+ addToMapMarkersHistoryList(marker);
+ reorderActiveMarkersIfNeeded();
+ sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC);
+ refresh();
+ }
+ }
+
+ public void addMarkers(List markers) {
+ if (markers != null) {
+ markersDbHelper.addMarkers(markers);
+ addToMapMarkersList(markers);
+ reorderActiveMarkersIfNeeded();
+ addMarkersToGroups(markers);
+ refresh();
+ }
+ }
+
+ public void addMarker(MapMarker marker) {
+ if (marker != null) {
+ markersDbHelper.addMarker(marker);
+ if (marker.history) {
+ addToMapMarkersHistoryList(marker);
+ sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC);
+ } else {
+ addToMapMarkersList(marker);
+ reorderActiveMarkersIfNeeded();
+ }
+ addMarkerToGroup(marker);
+ refresh();
+ }
+ }
+
+ public void restoreMarkerFromHistory(MapMarker marker, int position) {
+ if (marker != null) {
+ markersDbHelper.restoreMapMarkerFromHistory(marker);
+ removeFromMapMarkersHistoryList(marker);
+ marker.history = false;
+ addToMapMarkersList(position, marker);
+ reorderActiveMarkersIfNeeded();
+ sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC);
+ refresh();
+ }
+ }
+
+ public void restoreMarkersFromHistory(List markers) {
+ if (markers != null) {
+ for (MapMarker marker : markers) {
+ markersDbHelper.restoreMapMarkerFromHistory(marker);
+ marker.history = false;
+ }
+ removeFromMapMarkersHistoryList(markers);
+ addToMapMarkersList(markers);
+ reorderActiveMarkersIfNeeded();
+ sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC);
+ updateGroups();
+ refresh();
+ }
+ }
+
+ public void removeMarker(MapMarker marker) {
+ removeMarker(marker, true);
+ }
+
+ private void removeMarker(MapMarker marker, boolean refresh) {
+ if (marker != null) {
+ markersDbHelper.removeMarker(marker);
+ if (marker.history) {
+ removeFromMapMarkersHistoryList(marker);
+ } else {
+ removeFromMapMarkersList(marker);
+ }
+ removeMarkerFromGroup(marker);
+ if (refresh) {
+ refresh();
+ }
+ }
+ }
+
+ @Nullable
+ public MapMarker getFirstMapMarker() {
+ return mapMarkers.size() > 0 ? mapMarkers.get(0) : null;
+ }
+
+ public void deselectAllActiveMarkers() {
+ for (MapMarker m : mapMarkers) {
+ if (m.selected) {
+ m.selected = false;
+ markersDbHelper.updateMarker(m);
+ }
+ }
+ }
+
+ public void selectAllActiveMarkers() {
+ for (MapMarker m : mapMarkers) {
+ if (!m.selected) {
+ m.selected = true;
+ markersDbHelper.updateMarker(m);
+ }
+ }
+ }
+
+ public List getSelectedMarkers() {
+ List list = new ArrayList<>();
+ for (MapMarker m : mapMarkers) {
+ if (m.selected) {
+ list.add(m);
+ }
+ }
+ return list;
+ }
+
+ public int getSelectedMarkersCount() {
+ int res = 0;
+ for (MapMarker m : mapMarkers) {
+ if (m.selected) {
+ res++;
+ }
+ }
+ return res;
+ }
+
+ public void addSelectedMarkersToTop(@NonNull List markers) {
+ List markersToRemove = new ArrayList<>();
+ for (MapMarker m : mapMarkers) {
+ if (m.selected) {
+ if (!markers.contains(m)) {
+ return;
+ }
+ markersToRemove.add(m);
+ }
+ }
+ if (markersToRemove.size() != markers.size()) {
+ return;
+ }
+
+ removeFromMapMarkersList(markersToRemove);
+ addToMapMarkersList(0, markers);
+ reorderActiveMarkersIfNeeded();
+ }
+
+ public List getSelectedMarkersLatLon() {
+ List list = new ArrayList<>();
+ for (MapMarker m : this.mapMarkers) {
+ if (m.selected) {
+ list.add(m.point);
+ }
+ }
+ return list;
+ }
+
+ public void reverseActiveMarkersOrder() {
+ cancelAddressRequests();
+ Collections.reverse(mapMarkers);
+ reorderActiveMarkersIfNeeded();
+ }
+
+ public void moveAllActiveMarkersToHistory() {
+ cancelAddressRequests();
+ long timestamp = System.currentTimeMillis();
+ markersDbHelper.moveAllActiveMarkersToHistory(timestamp);
+ for (MapMarker marker : mapMarkers) {
+ marker.visitedDate = timestamp;
+ marker.history = true;
+ marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE;
+ }
+ addToMapMarkersHistoryList(mapMarkers);
+ mapMarkers = new ArrayList<>();
+ sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC);
+ updateGroups();
+ refresh();
+ }
+
+ public void addMapMarker(@NonNull LatLon point, @Nullable PointDescription historyName) {
+ addMapMarkers(Collections.singletonList(point), Collections.singletonList(historyName), null);
+ }
+
+ public void addMapMarker(@NonNull LatLon point,
+ @Nullable PointDescription historyName,
+ @Nullable String mapObjectName) {
+ addMarkers(Collections.singletonList(point),
+ Collections.singletonList(historyName),
+ null,
+ null,
+ null,
+ Collections.singletonList(mapObjectName));
+ }
+
+ public void addMapMarkers(@NonNull List points,
+ @NonNull List historyNames,
+ @Nullable MapMarkersGroup group) {
+ addMarkers(points, historyNames, group, null, null, null);
+ }
+
+ private void addMarkers(@NonNull List points,
+ @NonNull List historyNames,
+ @Nullable MapMarkersGroup group,
+ @Nullable List favouritePoints,
+ @Nullable List wptPts,
+ @Nullable List mapObjNames) {
+ if (points.size() > 0) {
+ int colorIndex = -1;
+ List addedMarkers = new ArrayList<>();
+ for (int i = 0; i < points.size(); i++) {
+ LatLon point = points.get(i);
+ PointDescription historyName = historyNames.get(i);
+ FavouritePoint favouritePoint = favouritePoints == null ? null : favouritePoints.get(i);
+ WptPt wptPt = wptPts == null ? null : wptPts.get(i);
+ String mapObjName = mapObjNames == null ? null : mapObjNames.get(i);
+ final PointDescription pointDescription;
+ if (historyName == null) {
+ pointDescription = new PointDescription(PointDescription.POINT_TYPE_LOCATION, "");
+ } else {
+ pointDescription = historyName;
+ }
+ if (pointDescription.isLocation() && Algorithms.isEmpty(pointDescription.getName())) {
+ pointDescription.setName(PointDescription.getSearchAddressStr(ctx));
+ }
+ if (colorIndex == -1) {
+ if (mapMarkers.size() > 0) {
+ colorIndex = (mapMarkers.get(0).colorIndex + 1) % MAP_MARKERS_COLORS_COUNT;
+ } else {
+ colorIndex = 0;
+ }
+ } else {
+ colorIndex = (colorIndex + 1) % MAP_MARKERS_COLORS_COUNT;
+ }
+
+ MapMarker marker = new MapMarker(point, pointDescription, colorIndex, false, 0);
+ if (group != null) {
+ marker.id = group.getId() + marker.getName(ctx);
+ // TODO ???????
+ if (markersDbHelper.getMarker(marker.id) != null) {
+ continue;
+ }
+ marker.groupName = group.getName();
+ marker.groupKey = group.getId();
+ }
+ marker.history = false;
+ marker.nextKey = MapMarkersDbHelper.TAIL_NEXT_VALUE;
+ marker.favouritePoint = favouritePoint;
+ marker.wptPt = wptPt;
+ marker.mapObjectName = mapObjName;
+ markersDbHelper.addMarker(marker);
+ addToMapMarkersList(0, marker);
+ addedMarkers.add(marker);
+ reorderActiveMarkersIfNeeded();
+ lookupAddress(marker);
+ }
+ addMarkersToGroups(addedMarkers);
+ }
+ }
+
+ public void updateMapMarker(MapMarker marker, boolean refresh) {
+ if (marker != null) {
+ markersDbHelper.updateMarker(marker);
+ if (refresh) {
+ refresh();
+ }
+ }
+ }
+
+ public void moveMarkerToTop(MapMarker marker) {
+ int i = mapMarkers.indexOf(marker);
+ if (i != -1 && mapMarkers.size() > 1) {
+ removeFromMapMarkersList(marker);
+ addToMapMarkersList(0, marker);
+ reorderActiveMarkersIfNeeded();
+ refresh();
+ }
+ }
+
+ public void moveMapMarker(MapMarker marker, LatLon latLon) {
+ if (marker != null) {
+ LatLon point = new LatLon(latLon.getLatitude(), latLon.getLongitude());
+ int index = mapMarkers.indexOf(marker);
+ if (index != -1) {
+ mapMarkers.get(index).point = point;
+ }
+ marker.point = point;
+ markersDbHelper.updateMarker(marker);
+ reorderActiveMarkersIfNeeded();
+ refresh();
+ lookupAddress(marker);
+ }
+ }
+
+ public void addSyncListener(OnGroupSyncedListener listener) {
+ syncListeners.add(listener);
+ }
+
+ public void removeSyncListener(OnGroupSyncedListener listener) {
+ syncListeners.remove(listener);
+ }
+
+ public void addListener(MapMarkerChangedListener l) {
+ if (!listeners.contains(l)) {
+ listeners.add(l);
+ }
+ }
+
+ public void removeListener(MapMarkerChangedListener l) {
+ listeners.remove(l);
+ }
+
+ private void refreshMarker(final MapMarker marker) {
+ ctx.runInUIThread(new Runnable() {
+ @Override
+ public void run() {
+ for (MapMarkerChangedListener l : listeners) {
+ l.onMapMarkerChanged(marker);
+ }
+ }
+ });
+ }
+
+ private void refresh() {
+ ctx.runInUIThread(new Runnable() {
+ @Override
+ public void run() {
+ for (MapMarkerChangedListener l : listeners) {
+ l.onMapMarkersChanged();
+ }
+ }
+ });
+ }
+
+ public String generateGpx(String fileName) {
+ final File dir = ctx.getAppPath(IndexConstants.GPX_INDEX_DIR + "/map markers");
+ if (!dir.exists()) {
+ dir.mkdirs();
+ }
+ File fout = new File(dir, fileName + ".gpx");
+ int ind = 1;
+ while (fout.exists()) {
+ fout = new File(dir, fileName + "_" + (++ind) + ".gpx");
+ }
+ GPXFile file = new GPXFile();
+ for (MapMarker marker : mapMarkers) {
+ WptPt wpt = new WptPt();
+ wpt.lat = marker.getLatitude();
+ wpt.lon = marker.getLongitude();
+ wpt.setColor(ctx.getResources().getColor(MapMarker.getColorId(marker.colorIndex)));
+ wpt.name = marker.getOnlyName();
+ file.addPoint(wpt);
+ }
+ GPXUtilities.writeGpxFile(fout, file, ctx);
+ return fout.getAbsolutePath();
+ }
+
+ // ---------------------------------------------------------------------------------------------
+
+ // accessors to active markers:
+
+ private void addToMapMarkersList(MapMarker marker) {
+ addToMapMarkersList(mapMarkers.size(), marker);
+ }
+
+ private void addToMapMarkersList(int position, MapMarker marker) {
+ List copyList = new ArrayList<>(mapMarkers);
+ copyList.add(position, marker);
+ mapMarkers = copyList;
+ }
+
+ private void addToMapMarkersList(List markers) {
+ addToMapMarkersList(mapMarkers.size(), markers);
+ }
+
+ private void addToMapMarkersList(int position, List markers) {
+ List copyList = new ArrayList<>(mapMarkers);
+ copyList.addAll(position, markers);
+ mapMarkers = copyList;
+ }
+
+ private void removeFromMapMarkersList(MapMarker marker) {
+ List copyList = new ArrayList<>(mapMarkers);
+ copyList.remove(marker);
+ mapMarkers = copyList;
+ }
+
+ private void removeFromMapMarkersList(List markers) {
+ List copyList = new ArrayList<>(mapMarkers);
+ copyList.removeAll(markers);
+ mapMarkers = copyList;
+ }
+
+ // accessors to history markers:
+
+ private void addToMapMarkersHistoryList(MapMarker marker) {
+ List copyList = new ArrayList<>(mapMarkersHistory);
+ copyList.add(marker);
+ mapMarkersHistory = copyList;
+ }
+
+ private void addToMapMarkersHistoryList(List markers) {
+ List copyList = new ArrayList<>(mapMarkersHistory);
+ copyList.addAll(markers);
+ mapMarkersHistory = copyList;
+ }
+
+ private void removeFromMapMarkersHistoryList(MapMarker marker) {
+ List copyList = new ArrayList<>(mapMarkersHistory);
+ copyList.remove(marker);
+ mapMarkersHistory = copyList;
+ }
+
+ private void removeFromMapMarkersHistoryList(List markers) {
+ List copyList = new ArrayList<>(mapMarkersHistory);
+ copyList.removeAll(markers);
+ mapMarkersHistory = copyList;
+ }
+
+ // accessors to markers groups:
+
+ private void addToGroupsList(MapMarkersGroup group) {
+ List copyList = new ArrayList<>(mapMarkersGroups);
+ copyList.add(group);
+ mapMarkersGroups = copyList;
+ }
+
+ private void removeFromGroupsList(MapMarkersGroup group) {
+ List copyList = new ArrayList<>(mapMarkersGroups);
+ copyList.remove(group);
+ mapMarkersGroups = copyList;
+ }
+
+ // ---------------------------------------------------------------------------------------------
+
+ // classes and interfaces:
+
public interface MapMarkerChangedListener {
void onMapMarkerChanged(MapMarker mapMarker);
@@ -73,9 +1013,223 @@ public class MapMarkersHelper {
}
public interface OnGroupSyncedListener {
+
+ void onSyncStarted();
+
void onSyncDone();
}
+ private class SyncGroupTask extends AsyncTask {
+
+ private MapMarkersGroup group;
+
+ SyncGroupTask(MapMarkersGroup group) {
+ this.group = group;
+ }
+
+ @Override
+ protected void onPreExecute() {
+ if (!syncListeners.isEmpty()) {
+ ctx.runInUIThread(new Runnable() {
+ @Override
+ public void run() {
+ for (OnGroupSyncedListener listener : syncListeners) {
+ listener.onSyncStarted();
+ }
+ }
+ });
+ }
+ }
+
+ @Override
+ protected Void doInBackground(Void... voids) {
+ runGroupSynchronization();
+ return null;
+ }
+
+ // TODO extract method from Asynctask to Helper directly
+ private void runGroupSynchronization() {
+ if (!isGroupSynced(group.getId())) {
+ return;
+ }
+
+ List groupMarkers = new ArrayList<>(group.getMarkers());
+
+ if (group.getType() == MapMarkersGroup.FAVORITES_TYPE) {
+ FavoriteGroup favGroup = ctx.getFavorites().getGroup(group.getName());
+ if (favGroup == null) {
+ return;
+ }
+ group.visible = favGroup.visible;
+ if (!group.isVisible() || group.isDisabled()) {
+ removeGroupActiveMarkers(group, true);
+ return;
+ }
+
+ for (FavouritePoint fp : favGroup.points) {
+ addNewMarkerIfNeeded(group, groupMarkers, new LatLon(fp.getLatitude(), fp.getLongitude()), fp.getName(), fp, null);
+ }
+
+
+ } else if (group.getType() == MapMarkersGroup.GPX_TYPE) {
+ GpxSelectionHelper gpxHelper = ctx.getSelectedGpxHelper();
+ File file = new File(group.getId());
+ if (!file.exists()) {
+ return;
+ }
+
+ SelectedGpxFile selectedGpxFile = gpxHelper.getSelectedFileByPath(group.getId());
+ GPXFile gpx = selectedGpxFile == null ? null : selectedGpxFile.getGpxFile();
+ group.visible = gpx != null;
+ if (!group.isVisible() || group.isDisabled()) {
+ removeGroupActiveMarkers(group, true);
+ return;
+ }
+
+ boolean addAll = group.wptCategories == null || group.wptCategories.isEmpty();
+ List gpxPoints = new ArrayList<>(gpx.getPoints());
+ for (WptPt pt : gpxPoints) {
+ if (addAll || group.wptCategories.contains(pt.category)
+ || (pt.category == null && group.wptCategories.contains(""))) {
+ addNewMarkerIfNeeded(group, groupMarkers, new LatLon(pt.lat, pt.lon), pt.name, null, pt);
+ }
+ }
+ }
+
+ removeOldMarkersIfPresent(groupMarkers);
+ }
+
+ @Override
+ protected void onPostExecute(Void aVoid) {
+ if (!syncListeners.isEmpty()) {
+ ctx.runInUIThread(new Runnable() {
+ @Override
+ public void run() {
+ for (OnGroupSyncedListener listener : syncListeners) {
+ listener.onSyncDone();
+ }
+ }
+ });
+ }
+ }
+ }
+
+ public static class MapMarkersGroup {
+
+ public static final int FAVORITES_TYPE = 0;
+ public static final int GPX_TYPE = 1;
+
+ public static final String MARKERS_SYNC_GROUP_ID = "markers_sync_group_id";
+
+ private String id;
+ private String name;
+ private int type = -1;
+ private Set wptCategories;
+ private long creationDate;
+ private boolean disabled;
+ private boolean visible = true;
+ private List markers = new ArrayList<>();
+ // TODO should be removed from this class:
+ private GroupHeader header;
+ private ShowHideHistoryButton showHideHistoryButton;
+
+ public MapMarkersGroup() {
+
+ }
+
+ public MapMarkersGroup(@NonNull String id, @NonNull String name, int type) {
+ this.id = id;
+ this.name = name;
+ this.type = type;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public int getType() {
+ return type;
+ }
+
+ public void setWptCategories(Set wptCategories) {
+ this.wptCategories = wptCategories;
+ }
+
+ public boolean isDisabled() {
+ return disabled;
+ }
+
+ public void setDisabled(boolean disabled) {
+ this.disabled = disabled;
+ }
+
+ public boolean isVisible() {
+ return visible;
+ }
+
+ public List getMarkers() {
+ return markers;
+ }
+
+ public GroupHeader getGroupHeader() {
+ return header;
+ }
+
+ public ShowHideHistoryButton getShowHideHistoryButton() {
+ return showHideHistoryButton;
+ }
+
+ @Nullable
+ public String getWptCategoriesString() {
+ if (wptCategories != null) {
+ return Algorithms.encodeStringSet(wptCategories);
+ }
+ return null;
+ }
+
+ public List getActiveMarkers() {
+ List markers = new ArrayList<>(this.markers);
+ List activeMarkers = new ArrayList<>(markers.size());
+ for (MapMarker marker : markers) {
+ if (!marker.history) {
+ activeMarkers.add(marker);
+ }
+ }
+ return activeMarkers;
+ }
+
+ public List getHistoryMarkers() {
+ List historyMarkers = new ArrayList<>();
+ for (MapMarker marker : markers) {
+ if (marker.history) {
+ historyMarkers.add(marker);
+ }
+ }
+ return historyMarkers;
+ }
+ }
+
+ public static class ShowHideHistoryButton {
+ public boolean showHistory;
+ }
+
+ public static class GroupHeader {
+ private int iconRes;
+ private MapMarkersGroup group;
+
+ public int getIconRes() {
+ return iconRes;
+ }
+
+ public MapMarkersGroup getGroup() {
+ return group;
+ }
+ }
+
public static class MapMarker implements LocationPoint {
private static int[] colors;
@@ -96,8 +1250,7 @@ public class MapMarkersHelper {
public FavouritePoint favouritePoint;
public String mapObjectName;
- public MapMarker(LatLon point, PointDescription name, int colorIndex,
- boolean selected, int index) {
+ public MapMarker(LatLon point, PointDescription name, int colorIndex, boolean selected, int index) {
this.point = point;
this.pointDescription = name;
this.colorIndex = colorIndex;
@@ -106,8 +1259,7 @@ public class MapMarkersHelper {
}
public PointDescription getPointDescription(Context ctx) {
- return new PointDescription(POINT_TYPE_MAP_MARKER, ctx.getString(R.string.map_marker),
- getOnlyName());
+ return new PointDescription(POINT_TYPE_MAP_MARKER, ctx.getString(R.string.map_marker), getOnlyName());
}
public String getName(Context ctx) {
@@ -158,9 +1310,7 @@ public class MapMarkersHelper {
MapMarker mapMarker = (MapMarker) o;
- if (colorIndex != mapMarker.colorIndex) return false;
- return point.equals(mapMarker.point);
-
+ return colorIndex == mapMarker.colorIndex && point.equals(mapMarker.point);
}
@Override
@@ -195,1336 +1345,4 @@ public class MapMarkersHelper {
return (colorIndex >= 0 && colorIndex < colorsIds.length) ? colorsIds[colorIndex] : colorsIds[0];
}
}
-
- @Nullable
- public MarkersSyncGroup getGroup(String id) {
- return markersDbHelper.getGroup(id);
- }
-
- public static class MarkersSyncGroup {
-
- public static final int FAVORITES_TYPE = 0;
- public static final int GPX_TYPE = 1;
-
- public static final String MARKERS_SYNC_GROUP_ID = "markers_sync_group_id";
-
- private String id;
- private String name;
- private int type;
- private int color;
-
- public MarkersSyncGroup(@NonNull String id, @NonNull String name, int type, int color) {
- this.id = id;
- this.name = name;
- this.type = type;
- this.color = color;
- }
-
- public MarkersSyncGroup(@NonNull String id, @NonNull String name, int type) {
- this.id = id;
- this.name = name;
- this.type = type;
- this.color = -1;
- }
-
- public String getId() {
- return id;
- }
-
- public String getName() {
- return name;
- }
-
- public int getType() {
- return type;
- }
-
- public int getColor() {
- return color;
- }
-
- public void setColor(int color) {
- this.color = color;
- }
- }
-
- public MapMarkersHelper(OsmandApplication ctx) {
- this.ctx = ctx;
- settings = ctx.getSettings();
- markersDbHelper = ctx.getMapMarkersDbHelper();
- planRouteContext = new MarkersPlanRouteContext(ctx);
- startFromMyLocation = settings.ROUTE_MAP_MARKERS_START_MY_LOC.get();
- removeDisabledGroups();
- loadMarkers();
- createMapMarkersGroups();
- }
-
- public MarkersPlanRouteContext getPlanRouteContext() {
- return planRouteContext;
- }
-
- public boolean isStartFromMyLocation() {
- return startFromMyLocation;
- }
-
- public void setStartFromMyLocation(boolean startFromMyLocation) {
- this.startFromMyLocation = startFromMyLocation;
- settings.ROUTE_MAP_MARKERS_START_MY_LOC.set(startFromMyLocation);
- }
-
- public void lookupAddressAll() {
- for (MapMarker mapMarker : mapMarkers) {
- lookupAddress(mapMarker);
- }
- for (MapMarker mapMarker : mapMarkersHistory) {
- lookupAddress(mapMarker);
- }
- }
-
- private void loadMarkers() {
- mapMarkers = new ArrayList<>();
- mapMarkersHistory = new ArrayList<>();
-
- List activeMarkers = markersDbHelper.getActiveMarkers();
- addToMapMarkersList(activeMarkers);
- reorderActiveMarkersIfNeeded();
-
- List markersHistory = markersDbHelper.getMarkersHistory();
- sortMarkers(markersHistory, true, BY_DATE_ADDED_DESC);
- addToMapMarkersHistoryList(markersHistory);
-
- if (!ctx.isApplicationInitializing()) {
- lookupAddressAll();
- }
- }
-
- private void removeFromMapMarkersList(List markers) {
- List copyList = new ArrayList<>(mapMarkers);
- copyList.removeAll(markers);
- mapMarkers = copyList;
- }
-
- private void removeFromMapMarkersList(MapMarker marker) {
- List copyList = new ArrayList<>(mapMarkers);
- copyList.remove(marker);
- mapMarkers = copyList;
- }
-
- private void addToMapMarkersList(MapMarker marker) {
- addToMapMarkersList(mapMarkers.size(), marker);
- }
-
- private void addToMapMarkersList(int position, MapMarker marker) {
- List copyList = new ArrayList<>(mapMarkers);
- copyList.add(position, marker);
- mapMarkers = copyList;
- }
-
- private void addToMapMarkersList(List markers) {
- addToMapMarkersList(mapMarkers.size(), markers);
- }
-
- private void addToMapMarkersList(int position, List markers) {
- List copyList = new ArrayList<>(mapMarkers);
- copyList.addAll(position, markers);
- mapMarkers = copyList;
- }
-
- private void removeFromMapMarkersHistoryList(MapMarker marker) {
- List copyList = new ArrayList<>(mapMarkersHistory);
- copyList.remove(marker);
- mapMarkersHistory = copyList;
- }
-
- private void addToMapMarkersHistoryList(MapMarker marker) {
- addToMapMarkersHistoryList(mapMarkersHistory.size(), marker);
- }
-
- private void addToMapMarkersHistoryList(int position, MapMarker marker) {
- List copyList = new ArrayList<>(mapMarkersHistory);
- copyList.add(position, marker);
- mapMarkersHistory = copyList;
- }
-
- private void addToMapMarkersHistoryList(int position, List markers) {
- List copyList = new ArrayList<>(mapMarkersHistory);
- copyList.addAll(position, markers);
- mapMarkersHistory = copyList;
- }
-
- private void addToMapMarkersHistoryList(List markers) {
- addToMapMarkersHistoryList(mapMarkersHistory.size(), markers);
- }
-
- private void removeFromGroupsList(MapMarkersGroup group) {
- List copyList = new ArrayList<>(mapMarkersGroups);
- copyList.remove(group);
- mapMarkersGroups = copyList;
- }
-
- private void addToGroupsList(int position, MapMarkersGroup group) {
- List copyList = new ArrayList<>(mapMarkersGroups);
- copyList.add(position, group);
- mapMarkersGroups = copyList;
- }
-
- private void addToGroupsList(MapMarkersGroup group) {
- addToGroupsList(mapMarkersGroups.size(), group);
- }
-
- public void reorderActiveMarkersIfNeeded() {
- if (!mapMarkers.isEmpty()) {
- if (mapMarkers.size() > 1) {
- for (int i = 0; i < mapMarkers.size() - 1; i++) {
- MapMarker first = mapMarkers.get(i);
- MapMarker second = mapMarkers.get(i + 1);
- if (!first.nextKey.equals(second.id)) {
- markersDbHelper.changeActiveMarkerPosition(first, second);
- first.nextKey = second.id;
- }
- }
- }
-
- MapMarker tail = mapMarkers.get(mapMarkers.size() - 1);
- if (!tail.nextKey.equals(MapMarkersDbHelper.TAIL_NEXT_VALUE)) {
- markersDbHelper.changeActiveMarkerPosition(tail, null);
- }
- }
- }
-
- private void sortMarkers(List markers, final boolean visited, final @MapMarkersSortByDef int sortByMode) {
- sortMarkers(markers, visited, sortByMode, null);
- }
-
- private void sortMarkers(List markers, final boolean visited,
- final @MapMarkersSortByDef int sortByMode, @Nullable final LatLon location) {
- Collections.sort(markers, new Comparator() {
- @Override
- public int compare(MapMarker mapMarker1, MapMarker mapMarker2) {
- if (sortByMode == BY_DATE_ADDED_DESC || sortByMode == BY_DATE_ADDED_ASC) {
- long t1 = visited ? mapMarker1.visitedDate : mapMarker1.creationDate;
- long t2 = visited ? mapMarker2.visitedDate : mapMarker2.creationDate;
- if (t1 > t2) {
- return sortByMode == BY_DATE_ADDED_DESC ? -1 : 1;
- } else if (t1 == t2) {
- return 0;
- } else {
- return sortByMode == BY_DATE_ADDED_DESC ? 1 : -1;
- }
- } else if (location != null && (sortByMode == BY_DISTANCE_DESC || sortByMode == BY_DISTANCE_ASC)) {
- int d1 = (int) MapUtils.getDistance(location, mapMarker1.getLatitude(), mapMarker1.getLongitude());
- int d2 = (int) MapUtils.getDistance(location, mapMarker2.getLatitude(), mapMarker2.getLongitude());
- if (d1 > d2) {
- return sortByMode == BY_DISTANCE_DESC ? -1 : 1;
- } else if (d1 == d2) {
- return 0;
- } else {
- return sortByMode == BY_DISTANCE_DESC ? 1 : -1;
- }
- } else {
- String n1 = mapMarker1.getName(ctx);
- String n2 = mapMarker2.getName(ctx);
- return n1.compareToIgnoreCase(n2);
- }
- }
- });
- }
-
- public void sortMarkers(final @MapMarkersSortByDef int sortByMode, LatLon location) {
- sortMarkers(getMapMarkers(), false, sortByMode, location);
- reorderActiveMarkersIfNeeded();
- }
-
- private void lookupAddress(final MapMarker mapMarker) {
- if (mapMarker != null && mapMarker.pointDescription.isSearchingAddress(ctx)) {
- cancelPointAddressRequests(mapMarker.point);
- GeocodingLookupService.AddressLookupRequest lookupRequest =
- new GeocodingLookupService.AddressLookupRequest(mapMarker.point, new GeocodingLookupService.OnAddressLookupResult() {
- @Override
- public void geocodingDone(String address) {
- if (Algorithms.isEmpty(address)) {
- mapMarker.pointDescription.setName(PointDescription.getAddressNotFoundStr(ctx));
- } else {
- mapMarker.pointDescription.setName(address);
- }
- markersDbHelper.updateMarker(mapMarker);
- refreshMarker(mapMarker);
- }
- }, null);
- ctx.getGeocodingLookupService().lookupAddress(lookupRequest);
- }
- }
-
- public boolean isGroupSynced(String id) {
- return markersDbHelper.getGroup(id) != null;
- }
-
- public boolean isGroupDisabled(String id) {
- return markersDbHelper.isGroupDisabled(id);
- }
-
- public void syncAllGroupsAsync() {
- List groups = markersDbHelper.getAllGroups();
- for (MarkersSyncGroup gr : groups) {
- syncGroupAsync(gr);
- }
- }
-
- public void syncGroupAsync(@NonNull MarkersSyncGroup group) {
- syncGroupAsync(group, true, null);
- }
-
- public void syncGroupAsync(@NonNull MarkersSyncGroup group, boolean enabled) {
- syncGroupAsync(group, enabled, null);
- }
-
- public void syncGroupAsync(@NonNull MarkersSyncGroup group, @Nullable OnGroupSyncedListener groupSyncedListener) {
- syncGroupAsync(group, true, groupSyncedListener);
- }
-
- public void syncGroupAsync(@NonNull final MarkersSyncGroup group, final boolean enabled, @Nullable final OnGroupSyncedListener groupSyncedListener) {
- ctx.runInUIThread(new Runnable() {
- @Override
- public void run() {
- SyncGroupTask syncGroupTask = new SyncGroupTask(group, enabled, groupSyncedListener);
- syncGroupTask.executeOnExecutor(executorService);
- }
- });
- }
-
- private class SyncGroupTask extends AsyncTask {
-
- private MarkersSyncGroup group;
- private boolean enabled;
- private OnGroupSyncedListener listener;
-
- SyncGroupTask(MarkersSyncGroup group, boolean enabled, OnGroupSyncedListener listener) {
- this.group = group;
- this.enabled = enabled;
- this.listener = listener;
- }
-
- @Override
- protected Void doInBackground(Void... voids) {
- runGroupSynchronization();
- return null;
- }
-
- private void runGroupSynchronization() {
- if (!isGroupSynced(group.getId())) {
- return;
- }
-
- List dbMarkers = markersDbHelper.getMarkersFromGroup(group);
-
- if (group.getType() == MarkersSyncGroup.FAVORITES_TYPE) {
- FavoriteGroup favGroup = ctx.getFavorites().getGroup(group.getName());
- if (favGroup == null) {
- return;
- }
- if (!favGroup.visible) {
- removeActiveMarkersFromSyncGroup(group.getId());
- removeActiveMarkersFromGroup(group.getId());
- return;
- }
- if (group.getColor() == -1) {
- group.setColor(favGroup.color);
- }
-
- for (FavouritePoint fp : favGroup.points) {
- addNewMarkerIfNeeded(group, dbMarkers, new LatLon(fp.getLatitude(), fp.getLongitude()), fp.getName(), enabled, fp, null);
- }
-
- removeOldMarkersIfNeeded(dbMarkers);
- } else if (group.getType() == MarkersSyncGroup.GPX_TYPE) {
- GpxSelectionHelper gpxHelper = ctx.getSelectedGpxHelper();
- File file = new File(group.getId());
- if (!file.exists()) {
- return;
- }
-
- SelectedGpxFile selectedGpxFile = gpxHelper.getSelectedFileByPath(group.getId());
- GPXFile gpx = selectedGpxFile == null ? null : selectedGpxFile.getGpxFile();
- if (gpx == null) {
- removeActiveMarkersFromSyncGroup(group.getId());
- removeActiveMarkersFromGroup(group.getId());
- return;
- }
-
- List gpxPoints = new ArrayList<>(gpx.getPoints());
- int defColor = ContextCompat.getColor(ctx, R.color.marker_red);
- for (WptPt pt : gpxPoints) {
- group.setColor(pt.getColor(defColor));
- addNewMarkerIfNeeded(group, dbMarkers, new LatLon(pt.lat, pt.lon), pt.name, enabled, null, pt);
- }
-
- removeOldMarkersIfNeeded(dbMarkers);
- }
- }
-
- @Override
- protected void onPostExecute(Void aVoid) {
- if (listener != null) {
- ctx.runInUIThread(new Runnable() {
- @Override
- public void run() {
- listener.onSyncDone();
- }
- });
- }
- }
- }
-
- public boolean isSynced(SelectedGpxFile gpxFile) {
- GPXFile gpx = gpxFile.getGpxFile();
- List gpxPoints = gpx.getPoints();
- for (WptPt wptPt : gpxPoints) {
- MapMarker mapMarker = getMapMarker(wptPt);
- if (mapMarker != null) {
- return true;
- }
- }
- return false;
- }
-
- public boolean isSynced(FavouritePoint favouritePoint) {
- MapMarker mapMarker = getMapMarker(favouritePoint);
- return mapMarker != null;
- }
-
- public MapMarker getMapMarker(WptPt wptPt) {
- for (MapMarker marker : mapMarkers) {
- if (marker.wptPt == wptPt) {
- return marker;
- }
- }
- return null;
- }
-
- public MapMarker getMapMarker(FavouritePoint favouritePoint) {
- for (MapMarker marker : mapMarkers) {
- if (marker.favouritePoint == favouritePoint) {
- return marker;
- }
- }
- return null;
- }
-
- @Nullable
- public MapMarker getMapMarker(@NonNull LatLon latLon) {
- for (MapMarker marker : mapMarkers) {
- if (marker.point != null && marker.point.equals(latLon)) {
- return marker;
- }
- }
- return null;
- }
-
- @Nullable
- public MapMarker getMapMarker(@NonNull String mapObjectName, @NonNull LatLon latLon) {
- for (MapMarker marker : mapMarkers) {
- if (marker.mapObjectName != null && marker.mapObjectName.equals(mapObjectName) && marker.point != null) {
- if (MapUtils.getDistance(latLon, marker.point) < 15) {
- return marker;
- }
- }
- }
- return null;
- }
-
- private void addNewMarkerIfNeeded(@NonNull MarkersSyncGroup group,
- @NonNull List markers,
- @NonNull LatLon latLon,
- @NonNull String name,
- boolean enabled,
- @Nullable FavouritePoint favouritePoint,
- @Nullable WptPt wptPt) {
- boolean exists = false;
-
- for (MapMarker marker : markers) {
- if (marker.id.equals(group.getId() + name)) {
- exists = true;
- for (MapMarker m : mapMarkers) {
- if (m.id.equals(marker.id)) {
- m.favouritePoint = favouritePoint;
- m.wptPt = wptPt;
- if (!marker.history && !marker.point.equals(latLon)) {
- m.point = latLon;
- updateMapMarker(m, true);
- }
- break;
- }
- }
- markers.remove(marker);
- break;
- }
- }
-
- if (!exists) {
- addMarkers(Collections.singletonList(latLon),
- Collections.singletonList(new PointDescription(POINT_TYPE_MAP_MARKER, name)),
- group, enabled, Collections.singletonList(favouritePoint), Collections.singletonList(wptPt), null);
- }
- }
-
- private void removeOldMarkersIfNeeded(List markers) {
- if (!markers.isEmpty()) {
- boolean needRefresh = false;
- for (MapMarker marker : markers) {
- if (!marker.history) {
- markersDbHelper.removeMarker(marker, false);
- removeFromMapMarkersList(marker);
- removeMarkerFromGroup(marker);
- needRefresh = true;
- }
- }
- if (needRefresh) {
- reorderActiveMarkersIfNeeded();
- refresh();
- }
- }
- }
-
- public void moveMapMarkerToHistory(MapMarker marker) {
- if (marker != null) {
- cancelPointAddressRequests(marker.point);
- markersDbHelper.moveMarkerToHistory(marker);
- removeFromMapMarkersList(marker);
- marker.history = true;
- marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE;
- addToMapMarkersHistoryList(marker);
- reorderActiveMarkersIfNeeded();
- sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC);
- refresh();
- }
- }
-
- public void addMarkers(List markers) {
- if (markers != null) {
- markersDbHelper.addMarkers(markers);
- addToMapMarkersList(markers);
- reorderActiveMarkersIfNeeded();
- addMarkersToGroups(markers, true);
- refresh();
- }
- }
-
- public void addMarker(MapMarker marker) {
- if (marker != null) {
- markersDbHelper.addMarker(marker);
- if (marker.history) {
- addToMapMarkersHistoryList(marker);
- sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC);
- } else {
- addToMapMarkersList(marker);
- reorderActiveMarkersIfNeeded();
- }
- addMarkerToGroup(marker);
- refresh();
- }
- }
-
- public void restoreMarkerFromHistory(MapMarker marker, int position) {
- if (marker != null) {
- markersDbHelper.restoreMapMarkerFromHistory(marker);
- removeFromMapMarkersHistoryList(marker);
- marker.history = false;
- addToMapMarkersList(position, marker);
- reorderActiveMarkersIfNeeded();
- sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC);
- refresh();
- }
- }
-
- public void restoreMarkersFromHistory(List markers) {
- if (markers != null) {
- for (MapMarker marker : markers) {
- markersDbHelper.restoreMapMarkerFromHistory(marker);
- removeFromMapMarkersHistoryList(marker);
- marker.history = false;
- addToMapMarkersList(marker);
- }
- reorderActiveMarkersIfNeeded();
- sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC);
- updateGroups();
- refresh();
- }
- }
-
- public void removeMarker(MapMarker marker) {
- if (marker != null) {
- boolean history = marker.history;
- markersDbHelper.removeMarker(marker, history);
- if (history) {
- removeFromMapMarkersHistoryList(marker);
- } else {
- removeFromMapMarkersList(marker);
- }
- removeMarkerFromGroup(marker);
- refresh();
- }
- }
-
- public List getMapMarkers() {
- return mapMarkers;
- }
-
- public MapMarker getFirstMapMarker() {
- if (mapMarkers.size() > 0) {
- return mapMarkers.get(0);
- } else {
- return null;
- }
- }
-
- public List getMapMarkersHistory() {
- return mapMarkersHistory;
- }
-
- public void deselectAllActiveMarkers() {
- for (MapMarker m : mapMarkers) {
- if (m.selected) {
- m.selected = false;
- markersDbHelper.updateMarker(m);
- }
- }
- }
-
- public void selectAllActiveMarkers() {
- for (MapMarker m : mapMarkers) {
- if (!m.selected) {
- m.selected = true;
- markersDbHelper.updateMarker(m);
- }
- }
- }
-
- public List getSelectedMarkers() {
- List list = new ArrayList<>();
- for (MapMarker m : this.mapMarkers) {
- if (m.selected) {
- list.add(m);
- }
- }
- return list;
- }
-
- public int getSelectedMarkersCount() {
- int res = 0;
- for (MapMarker m : this.mapMarkers) {
- if (m.selected) {
- res++;
- }
- }
- return res;
- }
-
- public void addSelectedMarkersToTop(@NonNull List markers) {
- List markersToRemove = new ArrayList<>();
- for (MapMarker m : mapMarkers) {
- if (m.selected) {
- if (!markers.contains(m)) {
- return;
- }
- markersToRemove.add(m);
- }
- }
- if (markersToRemove.size() != markers.size()) {
- return;
- }
-
- removeFromMapMarkersList(markersToRemove);
- addToMapMarkersList(0, markers);
- reorderActiveMarkersIfNeeded();
- }
-
- public List getActiveMarkersLatLon() {
- List list = new ArrayList<>();
- for (MapMarker m : this.mapMarkers) {
- list.add(m.point);
- }
- return list;
- }
-
- public List getSelectedMarkersLatLon() {
- List list = new ArrayList<>();
- for (MapMarker m : this.mapMarkers) {
- if (m.selected) {
- list.add(m.point);
- }
- }
- return list;
- }
-
- public List getMarkersHistoryLatLon() {
- List list = new ArrayList<>();
- for (MapMarker m : this.mapMarkersHistory) {
- list.add(m.point);
- }
- return list;
- }
-
- public void reverseActiveMarkersOrder() {
- cancelAddressRequests();
- Collections.reverse(mapMarkers);
- reorderActiveMarkersIfNeeded();
- }
-
- public void moveAllActiveMarkersToHistory() {
- cancelAddressRequests();
- long timestamp = System.currentTimeMillis();
- markersDbHelper.moveAllActiveMarkersToHistory(timestamp);
- for (MapMarker marker : mapMarkers) {
- marker.visitedDate = timestamp;
- marker.history = true;
- marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE;
- }
- addToMapMarkersHistoryList(mapMarkers);
- mapMarkers = new ArrayList<>();
- sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC);
- updateGroups();
- refresh();
- }
-
- public void removeMarkersHistory() {
- cancelAddressRequests();
- markersDbHelper.clearAllMarkersHistory();
- mapMarkersHistory = new ArrayList<>();
- refresh();
- removeHistoryMarkersFromGroups();
- }
-
- 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);
- removeActiveMarkersFromSyncGroup(id);
- MapMarkersGroup group = getMapMarkerGroupByKey(id);
- if (group != null) {
- removeFromGroupsList(group);
- }
- }
- }
-
- public void removeDisabledGroups() {
- markersDbHelper.removeDisabledGroups();
- }
-
- public void updateGroupDisabled(@NonNull MapMarkersGroup group, boolean disabled) {
- String id = group.getGroupKey();
- if (id != null) {
- markersDbHelper.updateSyncGroupDisabled(id, disabled);
- updateSyncGroupDisabled(group, disabled);
- }
- }
-
- private void updateSyncGroupDisabled(@NonNull MapMarkersGroup group, boolean disabled) {
- List groupMarkers = new ArrayList<>(group.getMarkers());
- for (MapMarker marker : groupMarkers) {
- if (marker.history) {
- if (disabled) {
- removeFromMapMarkersHistoryList(marker);
- } else {
- addToMapMarkersHistoryList(marker);
- }
- } else {
- if (disabled) {
- removeFromMapMarkersList(marker);
- } else {
- addToMapMarkersList(marker);
- }
- }
- }
- reorderActiveMarkersIfNeeded();
- sortMarkers(mapMarkersHistory, true, BY_DATE_ADDED_DESC);
- refresh();
- }
-
- public void removeActiveMarkersFromSyncGroup(String syncGroupId) {
- if (syncGroupId != null) {
- markersDbHelper.removeActiveMarkersFromSyncGroup(syncGroupId);
- List copyList = new ArrayList<>(mapMarkers);
- for (int i = 0; i < copyList.size(); i++) {
- MapMarker marker = copyList.get(i);
- String groupKey = marker.groupKey;
- if (groupKey != null && groupKey.equals(syncGroupId)) {
- removeFromMapMarkersList(marker);
- }
- }
- reorderActiveMarkersIfNeeded();
- refresh();
- }
- }
-
- public void addMapMarker(@NonNull LatLon point, @Nullable PointDescription historyName) {
- addMarkers(Collections.singletonList(point), Collections.singletonList(historyName), null, true);
- }
-
- public void addMapMarker(@NonNull LatLon point, @Nullable PointDescription historyName, @Nullable String mapObjectName) {
- addMarkers(Collections.singletonList(point), Collections.singletonList(historyName), null,
- true, null, null, Collections.singletonList(mapObjectName));
- }
-
- public void addMapMarkers(@NonNull List points, @NonNull List historyNames, @Nullable MarkersSyncGroup group) {
- addMarkers(points, historyNames, group, true);
- }
-
- private void addMarkers(@NonNull List points, @NonNull List historyNames, @Nullable MarkersSyncGroup group, boolean enabled) {
- addMarkers(points, historyNames, group, enabled, null, null, null);
- }
-
- private void addMarkers(@NonNull List points, @NonNull List historyNames, @Nullable MarkersSyncGroup group,
- boolean enabled, @Nullable List favouritePoints, @Nullable List wptPts, @Nullable List mapObjNames) {
- if (points.size() > 0) {
- int colorIndex = -1;
- List addedMarkers = new ArrayList<>();
- for (int i = 0; i < points.size(); i++) {
- LatLon point = points.get(i);
- PointDescription historyName = historyNames.get(i);
- FavouritePoint favouritePoint = favouritePoints == null ? null : favouritePoints.get(i);
- WptPt wptPt = wptPts == null ? null : wptPts.get(i);
- String mapObjName = mapObjNames == null ? null : mapObjNames.get(i);
- final PointDescription pointDescription;
- if (historyName == null) {
- pointDescription = new PointDescription(PointDescription.POINT_TYPE_LOCATION, "");
- } else {
- pointDescription = historyName;
- }
- if (pointDescription.isLocation() && Algorithms.isEmpty(pointDescription.getName())) {
- pointDescription.setName(PointDescription.getSearchAddressStr(ctx));
- }
- if (colorIndex == -1) {
- if (mapMarkers.size() > 0) {
- colorIndex = (mapMarkers.get(0).colorIndex + 1) % MAP_MARKERS_COLORS_COUNT;
- } else {
- colorIndex = 0;
- }
- } else {
- colorIndex = (colorIndex + 1) % MAP_MARKERS_COLORS_COUNT;
- }
-
- MapMarker marker = new MapMarker(point, pointDescription, colorIndex, false, 0);
- if (group != null) {
- marker.id = group.getId() + marker.getName(ctx);
- if (markersDbHelper.getMarker(marker.id) != null) {
- continue;
- }
- marker.groupName = group.getName();
- marker.groupKey = group.getId();
- }
- marker.history = false;
- marker.nextKey = MapMarkersDbHelper.TAIL_NEXT_VALUE;
- marker.favouritePoint = favouritePoint;
- marker.wptPt = wptPt;
- marker.mapObjectName = mapObjName;
- markersDbHelper.addMarker(marker);
- if (enabled) {
- addToMapMarkersList(0, marker);
- }
- addedMarkers.add(marker);
- reorderActiveMarkersIfNeeded();
- lookupAddress(marker);
- }
- addMarkersToGroups(addedMarkers, enabled);
- }
- }
-
- public void updateMapMarker(MapMarker marker, boolean refresh) {
- if (marker != null) {
- markersDbHelper.updateMarker(marker);
- if (refresh) {
- refresh();
- }
- }
- }
-
- public void moveMarkerToTop(MapMarker marker) {
- int i = mapMarkers.indexOf(marker);
- if (i != -1 && mapMarkers.size() > 1) {
- removeFromMapMarkersList(marker);
- addToMapMarkersList(0, marker);
- reorderActiveMarkersIfNeeded();
- refresh();
- }
- }
-
- public void moveMapMarker(MapMarker marker, LatLon latLon) {
- if (marker != null) {
- LatLon point = new LatLon(latLon.getLatitude(), latLon.getLongitude());
- int index = mapMarkers.indexOf(marker);
- if (index != -1) {
- mapMarkers.get(index).point = point;
- }
- marker.point = point;
- markersDbHelper.updateMarker(marker);
- reorderActiveMarkersIfNeeded();
- refresh();
- lookupAddress(marker);
- }
- }
-
- public void addListener(MapMarkerChangedListener l) {
- if (!listeners.contains(l)) {
- listeners.add(l);
- }
- }
-
- public void removeListener(MapMarkerChangedListener l) {
- listeners.remove(l);
- }
-
- private void refreshMarker(final MapMarker marker) {
- ctx.runInUIThread(new Runnable() {
- @Override
- public void run() {
- for (MapMarkerChangedListener l : listeners) {
- l.onMapMarkerChanged(marker);
- }
- }
- });
- }
-
- private void refreshMarkers() {
- ctx.runInUIThread(new Runnable() {
- @Override
- public void run() {
- for (MapMarkerChangedListener l : listeners) {
- l.onMapMarkersChanged();
- }
- }
- });
- }
-
- public void refresh() {
- refreshMarkers();
- }
-
- private void cancelAddressRequests() {
- List list = getActiveMarkersLatLon();
- for (LatLon latLon : list) {
- cancelPointAddressRequests(latLon);
- }
- list = getMarkersHistoryLatLon();
- for (LatLon latLon : list) {
- cancelPointAddressRequests(latLon);
- }
- }
-
- private void cancelPointAddressRequests(LatLon latLon) {
- if (latLon != null) {
- ctx.getGeocodingLookupService().cancel(latLon);
- }
- }
-
- public String generateGpx(String fileName) {
- final File dir = ctx.getAppPath(IndexConstants.GPX_INDEX_DIR + "/map markers");
- if (!dir.exists()) {
- dir.mkdirs();
- }
- File fout = new File(dir, fileName + ".gpx");
- int ind = 1;
- while (fout.exists()) {
- fout = new File(dir, fileName + "_" + (++ind) + ".gpx");
- }
- GPXFile file = new GPXFile();
- for (MapMarker marker : mapMarkers) {
- WptPt wpt = new WptPt();
- wpt.lat = marker.getLatitude();
- wpt.lon = marker.getLongitude();
- wpt.setColor(ctx.getResources().getColor(MapMarker.getColorId(marker.colorIndex)));
- wpt.name = marker.getOnlyName();
- file.addPoint(wpt);
- }
- GPXUtilities.writeGpxFile(fout, file, ctx);
- return fout.getAbsolutePath();
- }
-
- private void removeHistoryMarkersFromGroups() {
- for (MapMarkersGroup markersGroup : mapMarkersGroups) {
- List activeMarkers = new ArrayList<>();
- for (MapMarker marker : markersGroup.getMarkers()) {
- if (!marker.history) {
- activeMarkers.add(marker);
- }
- }
- markersGroup.setMarkers(activeMarkers);
- updateGroup(markersGroup);
- }
- }
-
- private void removeActiveMarkersFromGroup(String groupId) {
- MapMarkersGroup group = getMapMarkerGroupByKey(groupId);
- if (group != null) {
- List markers = group.getMarkers();
- List historyMarkers = new ArrayList<>();
- for (MapMarker marker : markers) {
- if (marker.history) {
- historyMarkers.add(marker);
- }
- }
- group.setMarkers(historyMarkers);
- updateGroup(group);
- }
- }
-
- public void updateGroups() {
- for (MapMarkersGroup group : mapMarkersGroups) {
- updateGroup(group);
- }
- }
-
- public void updateGroup(MapMarkersGroup mapMarkersGroup) {
- if (mapMarkersGroup.getMarkers().size() == 0) {
- removeFromGroupsList(mapMarkersGroup);
- return;
- }
- int historyMarkersCount = mapMarkersGroup.getHistoryMarkers().size();
- ShowHideHistoryButton showHideHistoryButton = mapMarkersGroup.getShowHideHistoryButton();
- if (showHideHistoryButton != null) {
- if (historyMarkersCount == 0) {
- mapMarkersGroup.setShowHideHistoryButton(null);
- }
- } else if (historyMarkersCount > 0) {
- showHideHistoryButton = new ShowHideHistoryButton();
- showHideHistoryButton.setShowHistory(false);
- showHideHistoryButton.setMarkerGroup(mapMarkersGroup);
- mapMarkersGroup.setShowHideHistoryButton(showHideHistoryButton);
- }
- }
-
- private void addMarkersToGroups(@NonNull List markers, boolean enabled) {
- List groups = new ArrayList<>();
- for (int i = 0; i < markers.size(); i++) {
- MapMarkersGroup group = addMarkerToGroup(markers.get(i));
- if (group != null && !groups.contains(group)) {
- groups.add(group);
- }
- }
- if (!enabled) {
- for (MapMarkersGroup mapMarkersGroup : groups) {
- mapMarkersGroup.setDisabled(true);
- updateGroupDisabled(mapMarkersGroup, true);
- }
- }
- }
-
- private MapMarkersGroup addMarkerToGroup(MapMarker marker) {
- if (marker != null) {
- MapMarkersGroup mapMarkersGroup = getMapMarkerGroupByName(marker.groupName);
- if (mapMarkersGroup != null) {
- mapMarkersGroup.getMarkers().add(marker);
- updateGroup(mapMarkersGroup);
- if (mapMarkersGroup.getName() == null) {
- sortMarkers(mapMarkersGroup.getMarkers(), false, BY_DATE_ADDED_DESC);
- }
- } else {
- mapMarkersGroup = createMapMarkerGroup(marker);
- mapMarkersGroup.getMarkers().add(marker);
- createHeaderAndHistoryButtonInGroup(mapMarkersGroup);
- }
- return mapMarkersGroup;
- }
- return null;
- }
-
- private MapMarkersGroup createMapMarkerGroup(@NonNull MapMarker marker) {
- MapMarkersGroup group = new MapMarkersGroup();
- if (marker.groupName != null) {
- group.setName(marker.groupName);
- group.setGroupKey(marker.groupKey);
- MapMarkersHelper.MarkersSyncGroup syncGroup = getGroup(marker.groupKey);
- if (syncGroup != null) {
- group.setType(syncGroup.getType());
- } else {
- group.setType(MarkersSyncGroup.FAVORITES_TYPE);
- }
- group.setColor(MapMarker.getColorId(marker.colorIndex));
- }
- group.setCreationDate(marker.creationDate);
- addToGroupsList(group);
- sortGroups();
- return group;
- }
-
- private void createHeaderAndHistoryButtonInGroup(@NonNull MapMarkersGroup group) {
- if (group.getName() != null) {
- GroupHeader header = new GroupHeader();
- int type = group.getType();
- if (type != -1) {
- header.setIconRes(type == MapMarkersHelper.MarkersSyncGroup.FAVORITES_TYPE ? R.drawable.ic_action_fav_dark : R.drawable.ic_action_polygom_dark);
- }
- header.setGroup(group);
- group.setGroupHeader(header);
- updateGroup(group);
- }
- }
-
- private void removeMarkerFromGroup(MapMarker marker) {
- if (marker != null) {
- MapMarkersGroup mapMarkersGroup = getMapMarkerGroupByName(marker.groupName);
- if (mapMarkersGroup != null) {
- mapMarkersGroup.getMarkers().remove(marker);
- updateGroup(mapMarkersGroup);
- }
- }
- }
-
- public List getMapMarkersGroups() {
- return mapMarkersGroups;
- }
-
- private void createMapMarkersGroups() {
- List markers = new ArrayList<>();
- markers.addAll(mapMarkers);
- markers.addAll(mapMarkersHistory);
-
- Map groupsMap = new LinkedHashMap<>();
- MapMarkersGroup noGroup = null;
- for (MapMarker marker : markers) {
- String groupName = marker.groupName;
- if (groupName == null) {
- if (noGroup == null) {
- noGroup = new MapMarkersGroup();
- noGroup.setCreationDate(marker.creationDate);
- }
- noGroup.getMarkers().add(marker);
- } else {
- MapMarkersGroup group = groupsMap.get(groupName);
- if (group == null) {
- group = new MapMarkersGroup();
- group.setName(marker.groupName);
- group.setGroupKey(marker.groupKey);
- MapMarkersHelper.MarkersSyncGroup syncGroup = getGroup(marker.groupKey);
- if (syncGroup != null) {
- group.setType(syncGroup.getType());
- } else {
- group.setType(MarkersSyncGroup.FAVORITES_TYPE);
- }
- group.setColor(MapMarker.getColorId(marker.colorIndex));
- group.setCreationDate(marker.creationDate);
- groupsMap.put(groupName, group);
- } else {
- long markerCreationDate = marker.creationDate;
- if (markerCreationDate < group.getCreationDate()) {
- group.setCreationDate(markerCreationDate);
- }
- }
- group.getMarkers().add(marker);
- }
- }
- mapMarkersGroups = new ArrayList<>(groupsMap.values());
- if (noGroup != null) {
- addToGroupsList(noGroup);
- }
- sortGroups();
-
- for (MapMarkersGroup group : mapMarkersGroups) {
- createHeaderAndHistoryButtonInGroup(group);
- }
- }
-
- private void sortGroups() {
- if (mapMarkersGroups.size() > 0) {
- MapMarkersGroup noGroup = null;
- for (int i = 0; i < mapMarkersGroups.size(); i++) {
- MapMarkersGroup group = mapMarkersGroups.get(i);
- if (group.getName() == null) {
- sortMarkers(group.getMarkers(), false, BY_DATE_ADDED_DESC);
- removeFromGroupsList(group);
- noGroup = group;
- }
- }
- Collections.sort(mapMarkersGroups, new Comparator() {
- @Override
- public int compare(MapMarkersGroup group1, MapMarkersGroup group2) {
- long t1 = group1.getCreationDate();
- long t2 = group2.getCreationDate();
- if (t1 > t2) {
- return -1;
- } else if (t1 == t2) {
- return 0;
- } else {
- return 1;
- }
- }
- });
- if (noGroup != null) {
- addToGroupsList(0, noGroup);
- }
- }
- }
-
- public MapMarkersGroup getMapMarkerGroupByName(String name) {
- for (MapMarkersGroup group : mapMarkersGroups) {
- if ((name == null && group.getName() == null)
- || (group.getName() != null && group.getName().equals(name))) {
- return group;
- }
- }
- return null;
- }
-
- public MapMarkersGroup getMapMarkerGroupByKey(String key) {
- for (MapMarkersGroup group : mapMarkersGroups) {
- if ((key == null && group.getGroupKey() == null)
- || (group.getGroupKey() != null && group.getGroupKey().equals(key))) {
- return group;
- }
- }
- return null;
- }
-
- public static class MapMarkersGroup {
- private String name;
- private String groupKey;
- private GroupHeader header;
- private int type = -1;
- private List markers = new ArrayList<>();
- private long creationDate;
- private ShowHideHistoryButton showHideHistoryButton;
- private int color;
- private boolean disabled;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getGroupKey() {
- return groupKey;
- }
-
- public void setGroupKey(String groupKey) {
- this.groupKey = groupKey;
- }
-
- public GroupHeader getGroupHeader() {
- return header;
- }
-
- public void setGroupHeader(GroupHeader header) {
- this.header = header;
- }
-
- public int getType() {
- return type;
- }
-
- public void setType(int type) {
- this.type = type;
- }
-
- public List getActiveMarkers() {
- List markers = new ArrayList<>(this.markers);
- List activeMarkers = new ArrayList<>(markers.size());
- for (MapMarker marker : markers) {
- if (!marker.history) {
- activeMarkers.add(marker);
- }
- }
- return activeMarkers;
- }
-
- public List getHistoryMarkers() {
- List historyMarkers = new ArrayList<>();
- for (MapMarker marker : markers) {
- if (marker.history) {
- historyMarkers.add(marker);
- }
- }
- return historyMarkers;
- }
-
- public List getMarkers() {
- return markers;
- }
-
- public void setMarkers(List markers) {
- this.markers = markers;
- }
-
- public long getCreationDate() {
- return creationDate;
- }
-
- public void setCreationDate(long creationDate) {
- this.creationDate = creationDate;
- }
-
- public ShowHideHistoryButton getShowHideHistoryButton() {
- return showHideHistoryButton;
- }
-
- public void setShowHideHistoryButton(ShowHideHistoryButton showHideHistoryButton) {
- this.showHideHistoryButton = showHideHistoryButton;
- }
-
- public int getColor() {
- return color;
- }
-
- public void setColor(int color) {
- this.color = color;
- }
-
- public boolean isDisabled() {
- return disabled;
- }
-
- public void setDisabled(boolean disabled) {
- this.disabled = disabled;
- }
- }
-
- public static class ShowHideHistoryButton {
- private boolean showHistory;
- private MapMarkersGroup group;
-
- public boolean isShowHistory() {
- return showHistory;
- }
-
- public void setShowHistory(boolean showHistory) {
- this.showHistory = showHistory;
- }
-
- public MapMarkersGroup getMapMarkerGroup() {
- return group;
- }
-
- public void setMarkerGroup(MapMarkersGroup group) {
- this.group = group;
- }
- }
-
- public static class GroupHeader {
- private int iconRes;
- private MapMarkersGroup group;
-
- public int getIconRes() {
- return iconRes;
- }
-
- public void setIconRes(int iconRes) {
- this.iconRes = iconRes;
- }
-
- public MapMarkersGroup getGroup() {
- return group;
- }
-
- public void setGroup(MapMarkersGroup group) {
- this.group = group;
- }
- }
}
diff --git a/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java b/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java
index 435d360211..bf55a108d1 100644
--- a/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java
+++ b/OsmAnd/src/net/osmand/plus/activities/EditFavoriteGroupDialogFragment.java
@@ -23,7 +23,7 @@ import net.osmand.AndroidUtils;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.FavouritesDbHelper.FavoriteGroup;
import net.osmand.plus.MapMarkersHelper;
-import net.osmand.plus.MapMarkersHelper.MarkersSyncGroup;
+import net.osmand.plus.MapMarkersHelper.MapMarkersGroup;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
@@ -156,49 +156,30 @@ public class EditFavoriteGroupDialogFragment extends MenuBottomSheetDialogFragme
if (group.points.size() > 0) {
items.add(new DividerHalfItem(getContext()));
- }
- final MapMarkersHelper markersHelper = app.getMapMarkersHelper();
- final MarkersSyncGroup syncGroup =
- new MarkersSyncGroup(group.name, group.name, MarkersSyncGroup.FAVORITES_TYPE, group.color);
- boolean groupSyncedWithMarkers = markersHelper.isGroupSynced(syncGroup.getId());
+ final MapMarkersHelper markersHelper = app.getMapMarkersHelper();
+ final MapMarkersGroup markersGr = markersHelper.getOrCreateGroup(this.group);
+ final boolean synced = markersHelper.isGroupSynced(markersGr.getId());
- if (app.getSettings().USE_MAP_MARKERS.get() && group.points.size() > 0 && !groupSyncedWithMarkers) {
- BaseBottomSheetItem addToMarkersItem = new SimpleBottomSheetItem.Builder()
- .setIcon(getContentIcon(R.drawable.ic_action_flag_dark))
- .setTitle(getString(R.string.shared_string_add_to_map_markers))
- .setLayoutId(R.layout.bottom_sheet_item_simple)
- .setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- markersHelper.addMarkersSyncGroup(syncGroup);
- markersHelper.syncGroupAsync(syncGroup);
- dismiss();
- MapActivity.launchMapActivityMoveToTop(getActivity());
- }
- })
- .create();
- items.add(addToMarkersItem);
- }
-
- if (app.getSettings().USE_MAP_MARKERS.get() && groupSyncedWithMarkers) {
- BaseBottomSheetItem removeFromMarkersItem = new SimpleBottomSheetItem.Builder()
- .setIcon(getContentIcon(R.drawable.ic_action_delete_dark))
- .setTitle(getString(R.string.remove_from_map_markers))
+ BaseBottomSheetItem markersGroupItem = new SimpleBottomSheetItem.Builder()
+ .setIcon(getContentIcon(synced ? R.drawable.ic_action_delete_dark : R.drawable.ic_action_flag_dark))
+ .setTitle(getString(synced ? R.string.remove_from_map_markers : R.string.shared_string_add_to_map_markers))
.setLayoutId(R.layout.bottom_sheet_item_simple)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
- markersHelper.removeMarkersSyncGroup(syncGroup.getId());
+ if (synced) {
+ markersHelper.removeMarkersGroup(markersGr);
+ } else {
+ markersHelper.syncWithMarkers(markersGr);
+ }
dismiss();
MapActivity.launchMapActivityMoveToTop(getActivity());
}
})
.create();
- items.add(removeFromMarkersItem);
- }
+ items.add(markersGroupItem);
- if (group.points.size() > 0) {
BaseBottomSheetItem shareItem = new SimpleBottomSheetItem.Builder()
.setIcon(getContentIcon(R.drawable.ic_action_gshare_dark))
.setTitle(getString(R.string.shared_string_share))
@@ -208,7 +189,7 @@ public class EditFavoriteGroupDialogFragment extends MenuBottomSheetDialogFragme
public void onClick(View view) {
FavoritesTreeFragment fragment = getFavoritesTreeFragment();
if (fragment != null) {
- fragment.shareFavorites(group);
+ fragment.shareFavorites(EditFavoriteGroupDialogFragment.this.group);
}
dismiss();
}
diff --git a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java
index b54979ac83..1f0d11b19d 100644
--- a/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java
+++ b/OsmAnd/src/net/osmand/plus/activities/FavoritesTreeFragment.java
@@ -36,12 +36,11 @@ import net.osmand.data.PointDescription;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.FavouritesDbHelper.FavoriteGroup;
import net.osmand.plus.MapMarkersHelper;
-import net.osmand.plus.MapMarkersHelper.MarkersSyncGroup;
+import net.osmand.plus.MapMarkersHelper.MapMarkersGroup;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
-import net.osmand.plus.TargetPointsHelper;
import net.osmand.plus.base.FavoriteImageDrawable;
import net.osmand.plus.base.OsmandExpandableListFragment;
import net.osmand.plus.helpers.AndroidUiHelper;
@@ -58,7 +57,6 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -347,13 +345,8 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment {
R.drawable.ic_action_plus, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
createMenuItem(menu, SHARE_ID, R.string.shared_string_share, R.drawable.ic_action_gshare_dark,
R.drawable.ic_action_gshare_dark, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
- if (getSettings().USE_MAP_MARKERS.get()) {
- createMenuItem(menu, SELECT_MAP_MARKERS_ID, R.string.select_map_markers, R.drawable.ic_action_flag_dark,
- R.drawable.ic_action_flag_dark, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
- } else {
- createMenuItem(menu, SELECT_MAP_MARKERS_ID, R.string.select_destination_and_intermediate_points, R.drawable.ic_action_intermediate,
- R.drawable.ic_action_intermediate, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
- }
+ createMenuItem(menu, SELECT_MAP_MARKERS_ID, R.string.select_map_markers, R.drawable.ic_action_flag_dark,
+ R.drawable.ic_action_flag_dark, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
createMenuItem(menu, DELETE_ID, R.string.shared_string_delete, R.drawable.ic_action_delete_dark,
R.drawable.ic_action_delete_dark, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
// createMenuItem(menu, EXPORT_ID, R.string.shared_string_export, R.drawable.ic_action_gsave_light,
@@ -383,15 +376,9 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
enableSelectionMode(true);
- if (getSettings().USE_MAP_MARKERS.get()) {
- createMenuItem(menu, SELECT_MAP_MARKERS_ACTION_MODE_ID, R.string.select_map_markers,
- R.drawable.ic_action_flag_dark, R.drawable.ic_action_flag_dark,
- MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
- } else {
- createMenuItem(menu, SELECT_MAP_MARKERS_ACTION_MODE_ID, R.string.select_destination_and_intermediate_points,
- R.drawable.ic_action_intermediate, R.drawable.ic_action_intermediate,
- MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
- }
+ createMenuItem(menu, SELECT_MAP_MARKERS_ACTION_MODE_ID, R.string.select_map_markers,
+ R.drawable.ic_action_flag_dark, R.drawable.ic_action_flag_dark,
+ MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
favoritesSelected.clear();
groupsToDelete.clear();
favouritesAdapter.notifyDataSetInvalidated();
@@ -424,40 +411,25 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment {
private void selectMapMarkersImpl() {
if (getSelectedFavoritesCount() > 0) {
- if (getSettings().USE_MAP_MARKERS.get()) {
- MapMarkersHelper markersHelper = getMyApplication().getMapMarkersHelper();
- List points = new LinkedList<>();
- List names = new LinkedList<>();
- for (Map.Entry> entry : favoritesSelected.entrySet()) {
- FavoriteGroup favGr = helper.getGroup(entry.getKey());
- MarkersSyncGroup syncGr =
- new MarkersSyncGroup(favGr.name, favGr.name, MarkersSyncGroup.FAVORITES_TYPE, favGr.color);
- if (entry.getValue().size() == favGr.points.size()) {
- markersHelper.addMarkersSyncGroup(syncGr);
- markersHelper.syncGroupAsync(syncGr);
- } else {
- for (FavouritePoint fp : entry.getValue()) {
- points.add(new LatLon(fp.getLatitude(), fp.getLongitude()));
- names.add(new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, fp.getName()));
- }
- markersHelper.addMapMarkers(points, names, syncGr);
- points.clear();
- names.clear();
+ MapMarkersHelper markersHelper = getMyApplication().getMapMarkersHelper();
+ List points = new ArrayList<>();
+ List names = new ArrayList<>();
+ for (Map.Entry> entry : favoritesSelected.entrySet()) {
+ FavoriteGroup favGr = helper.getGroup(entry.getKey());
+ MapMarkersGroup markersGr = markersHelper.getOrCreateGroup(favGr);
+ if (entry.getValue().size() == favGr.points.size()) {
+ markersHelper.syncWithMarkers(markersGr);
+ } else {
+ for (FavouritePoint fp : entry.getValue()) {
+ points.add(new LatLon(fp.getLatitude(), fp.getLongitude()));
+ names.add(new PointDescription(PointDescription.POINT_TYPE_MAP_MARKER, fp.getName()));
}
+ markersHelper.addMapMarkers(points, names, markersGr);
+ points.clear();
+ names.clear();
}
- MapActivity.launchMapActivityMoveToTop(getActivity());
- } else {
- final TargetPointsHelper targetPointsHelper = getMyApplication().getTargetPointsHelper();
- for (FavouritePoint fp : getSelectedFavorites()) {
- targetPointsHelper.navigateToPoint(new LatLon(fp.getLatitude(), fp.getLongitude()), false,
- targetPointsHelper.getIntermediatePoints().size() + 1,
- new PointDescription(PointDescription.POINT_TYPE_FAVORITE, fp.getName()));
- }
- if (getMyApplication().getRoutingHelper().isRouteCalculated()) {
- targetPointsHelper.updateRouteAndRefresh(true);
- }
- IntermediatePointsDialog.openIntermediatePointsDialog(getActivity(), getMyApplication(), true);
}
+ MapActivity.launchMapActivityMoveToTop(getActivity());
}
}
diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java
index 8bad7a1d68..5f4dbdd064 100644
--- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java
+++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java
@@ -719,7 +719,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
if (intent.hasExtra(MapMarkersDialogFragment.OPEN_MAP_MARKERS_GROUPS)) {
Bundle openMapMarkersGroupsExtra = intent.getBundleExtra(MapMarkersDialogFragment.OPEN_MAP_MARKERS_GROUPS);
if (openMapMarkersGroupsExtra != null) {
- MapMarkersDialogFragment.showInstance(this, openMapMarkersGroupsExtra.getString(MapMarkersHelper.MarkersSyncGroup.MARKERS_SYNC_GROUP_ID));
+ MapMarkersDialogFragment.showInstance(this, openMapMarkersGroupsExtra.getString(MapMarkersHelper.MapMarkersGroup.MARKERS_SYNC_GROUP_ID));
}
setIntent(null);
}
diff --git a/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BaseBottomSheetItem.java b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BaseBottomSheetItem.java
index ace5f0dfd2..8917d22975 100644
--- a/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BaseBottomSheetItem.java
+++ b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BaseBottomSheetItem.java
@@ -26,6 +26,10 @@ public class BaseBottomSheetItem {
return view;
}
+ public Object getTag() {
+ return tag;
+ }
+
public BaseBottomSheetItem(View view,
@LayoutRes int layoutId,
Object tag,
diff --git a/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BottomSheetItemWithCompoundButton.java b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BottomSheetItemWithCompoundButton.java
index ab4835b8bf..07bcb93895 100644
--- a/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BottomSheetItemWithCompoundButton.java
+++ b/OsmAnd/src/net/osmand/plus/base/bottomsheetmenu/BottomSheetItemWithCompoundButton.java
@@ -8,6 +8,7 @@ import android.support.v4.widget.CompoundButtonCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
+import android.widget.CompoundButton.OnCheckedChangeListener;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@@ -16,9 +17,14 @@ public class BottomSheetItemWithCompoundButton extends BottomSheetItemWithDescri
private boolean checked;
private ColorStateList buttonTintList;
+ private OnCheckedChangeListener onCheckedChangeListener;
private CompoundButton compoundButton;
+ public boolean isChecked() {
+ return checked;
+ }
+
public BottomSheetItemWithCompoundButton(View customView,
@LayoutRes int layoutId,
Object tag,
@@ -31,7 +37,8 @@ public class BottomSheetItemWithCompoundButton extends BottomSheetItemWithDescri
CharSequence description,
@ColorRes int descriptionColorId,
boolean checked,
- ColorStateList buttonTintList) {
+ ColorStateList buttonTintList,
+ OnCheckedChangeListener onCheckedChangeListener) {
super(customView,
layoutId,
tag,
@@ -45,6 +52,7 @@ public class BottomSheetItemWithCompoundButton extends BottomSheetItemWithDescri
descriptionColorId);
this.checked = checked;
this.buttonTintList = buttonTintList;
+ this.onCheckedChangeListener = onCheckedChangeListener;
}
public void setChecked(boolean checked) {
@@ -58,12 +66,14 @@ public class BottomSheetItemWithCompoundButton extends BottomSheetItemWithDescri
compoundButton = (CompoundButton) view.findViewById(R.id.compound_button);
compoundButton.setChecked(checked);
CompoundButtonCompat.setButtonTintList(compoundButton, buttonTintList);
+ compoundButton.setOnCheckedChangeListener(onCheckedChangeListener);
}
public static class Builder extends BottomSheetItemWithDescription.Builder {
protected boolean checked;
protected ColorStateList buttonTintList;
+ protected OnCheckedChangeListener onCheckedChangeListener;
public Builder setChecked(boolean checked) {
this.checked = checked;
@@ -75,6 +85,11 @@ public class BottomSheetItemWithCompoundButton extends BottomSheetItemWithDescri
return this;
}
+ public Builder setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
+ this.onCheckedChangeListener = onCheckedChangeListener;
+ return this;
+ }
+
public BottomSheetItemWithCompoundButton create() {
return new BottomSheetItemWithCompoundButton(customView,
layoutId,
@@ -88,7 +103,8 @@ public class BottomSheetItemWithCompoundButton extends BottomSheetItemWithDescri
description,
descriptionColorId,
checked,
- buttonTintList);
+ buttonTintList,
+ onCheckedChangeListener);
}
}
}
diff --git a/OsmAnd/src/net/osmand/plus/helpers/MapMarkerDialogHelper.java b/OsmAnd/src/net/osmand/plus/helpers/MapMarkerDialogHelper.java
index 4d7360b6a1..2d7efeef18 100644
--- a/OsmAnd/src/net/osmand/plus/helpers/MapMarkerDialogHelper.java
+++ b/OsmAnd/src/net/osmand/plus/helpers/MapMarkerDialogHelper.java
@@ -1,473 +1,35 @@
package net.osmand.plus.helpers;
import android.content.Context;
-import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
-import android.graphics.drawable.ShapeDrawable;
-import android.graphics.drawable.shapes.Shape;
-import android.support.v7.app.AlertDialog;
-import android.support.v7.widget.PopupMenu;
-import android.text.format.DateFormat;
-import android.view.MenuItem;
import android.view.View;
-import android.view.ViewGroup;
-import android.widget.AdapterView;
-import android.widget.ArrayAdapter;
-import android.widget.Button;
import android.widget.CheckBox;
-import android.widget.ImageButton;
import android.widget.ImageView;
-import android.widget.ListView;
import android.widget.TextView;
import net.osmand.AndroidUtils;
-import net.osmand.IndexConstants;
import net.osmand.Location;
import net.osmand.data.LatLon;
-import net.osmand.data.PointDescription;
-import net.osmand.plus.ApplicationMode;
-import net.osmand.plus.GPXUtilities;
-import net.osmand.plus.GPXUtilities.GPXFile;
-import net.osmand.plus.GPXUtilities.WptPt;
-import net.osmand.plus.IconsCache;
-import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
-import net.osmand.plus.activities.MapActivity;
-import net.osmand.plus.dashboard.DashLocationFragment;
-import net.osmand.plus.dashboard.DashboardOnMap;
-import net.osmand.plus.dialogs.DirectionsDialogs;
import net.osmand.plus.views.DirectionDrawable;
-import net.osmand.plus.views.controls.DynamicListView;
-import net.osmand.plus.views.controls.ListDividerShape;
-import net.osmand.plus.views.controls.StableArrayAdapter;
-import net.osmand.util.MapUtils;
-import java.io.File;
import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
import java.util.Date;
-import java.util.List;
import java.util.Locale;
public class MapMarkerDialogHelper {
- public static final int ACTIVE_MARKERS = 0;
- public static final int MY_LOCATION = 10;
- public static final int MARKERS_HISTORY = 100;
- private MapActivity mapActivity;
- private OsmandApplication app;
- private MapMarkersHelper markersHelper;
- private MapMarkersDialogHelperCallbacks helperCallbacks;
- private boolean sorted;
- private boolean nightMode;
- private boolean selectionMode;
-
- private boolean useCenter;
- private LatLon myLoc;
- private LatLon loc;
- private Float heading;
- private int screenOrientation;
- private boolean reloading;
- private long lastUpdateTime;
- private boolean allSelected;
-
- public interface MapMarkersDialogHelperCallbacks {
- void reloadAdapter();
-
- void deleteMapMarker(int position);
-
- void showMarkersRouteOnMap();
- }
-
- public MapMarkerDialogHelper(MapActivity mapActivity) {
- this.mapActivity = mapActivity;
- app = mapActivity.getMyApplication();
- markersHelper = app.getMapMarkersHelper();
- }
-
- public void setHelperCallbacks(MapMarkersDialogHelperCallbacks helperCallbacks) {
- this.helperCallbacks = helperCallbacks;
- }
-
- public boolean isInSelectionMode() {
- return selectionMode;
- }
-
- public boolean hasActiveMarkers() {
- return markersHelper.getMapMarkers().size() > 0;
- }
-
- public void setSelectionMode(boolean selectionMode) {
- this.selectionMode = selectionMode;
- }
-
- public boolean isNightMode() {
- return nightMode;
- }
-
- public void setNightMode(boolean nightMode) {
- this.nightMode = nightMode;
- }
-
- public boolean isSorted() {
- return sorted;
- }
-
- public void setSorted(boolean sorted) {
- this.sorted = sorted;
- }
-
- public AdapterView.OnItemClickListener getItemClickListener(final ArrayAdapter