diff --git a/OsmAnd-java/src/net/osmand/util/Algorithms.java b/OsmAnd-java/src/net/osmand/util/Algorithms.java index 32a01eb452..8f1bae7cbf 100644 --- a/OsmAnd-java/src/net/osmand/util/Algorithms.java +++ b/OsmAnd-java/src/net/osmand/util/Algorithms.java @@ -2,7 +2,6 @@ package net.osmand.util; import net.osmand.IProgress; import net.osmand.PlatformUtil; -import net.osmand.binary.BinaryMapIndexReader; import org.apache.commons.logging.Log; @@ -21,10 +20,12 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; /** @@ -177,6 +178,24 @@ public class Algorithms { return ""; } + public static Set decodeStringSet(String s) { + if (isEmpty(s)) { + return Collections.emptySet(); + } + return new HashSet<>(Arrays.asList(s.split(CHAR_TOSPLIT + ""))); + } + + public static String encodeStringSet(Set set) { + if (set != null) { + StringBuilder sb = new StringBuilder(); + for (String s : set) { + sb.append(s).append(CHAR_TOSPLIT); + } + return sb.toString(); + } + return ""; + } + public static int findFirstNumberEndIndex(String value) { int i = 0; boolean valid = false; diff --git a/OsmAnd/src/net/osmand/plus/GPXDatabase.java b/OsmAnd/src/net/osmand/plus/GPXDatabase.java index a037e31a65..25f9350007 100644 --- a/OsmAnd/src/net/osmand/plus/GPXDatabase.java +++ b/OsmAnd/src/net/osmand/plus/GPXDatabase.java @@ -15,7 +15,7 @@ import java.util.List; public class GPXDatabase { private static final String DB_NAME = "gpx_database"; - private static final int DB_VERSION = 6; + private static final int DB_VERSION = 7; private static final String GPX_TABLE_NAME = "gpxTable"; private static final String GPX_COL_NAME = "fileName"; private static final String GPX_COL_DIR = "fileDir"; @@ -47,6 +47,8 @@ public class GPXDatabase { private static final String GPX_COL_API_IMPORTED = "apiImported"; + private static final String GPX_COL_WPT_CATEGORY_NAMES = "wptCategoryNames"; + public static final int GPX_SPLIT_TYPE_NO_SPLIT = -1; public static final int GPX_SPLIT_TYPE_DISTANCE = 1; public static final int GPX_SPLIT_TYPE_TIME = 2; @@ -77,7 +79,8 @@ public class GPXDatabase { GPX_COL_FILE_LAST_MODIFIED_TIME + " long, " + GPX_COL_SPLIT_TYPE + " int, " + GPX_COL_SPLIT_INTERVAL + " double, " + - GPX_COL_API_IMPORTED + " int);"; // 1 = true, 0 = false + GPX_COL_API_IMPORTED + " int, " + // 1 = true, 0 = false + GPX_COL_WPT_CATEGORY_NAMES + " TEXT);"; private static final String GPX_TABLE_SELECT = "SELECT " + GPX_COL_NAME + ", " + @@ -102,7 +105,8 @@ public class GPXDatabase { GPX_COL_FILE_LAST_MODIFIED_TIME + ", " + GPX_COL_SPLIT_TYPE + ", " + GPX_COL_SPLIT_INTERVAL + ", " + - GPX_COL_API_IMPORTED + + GPX_COL_API_IMPORTED + ", " + + GPX_COL_WPT_CATEGORY_NAMES + " FROM " + GPX_TABLE_NAME; private OsmandApplication context; @@ -244,6 +248,10 @@ public class GPXDatabase { " SET " + GPX_COL_API_IMPORTED + " = ? " + "WHERE " + GPX_COL_API_IMPORTED + " IS NULL", new Object[]{0}); } + + if (oldVersion < 7) { + db.execSQL("ALTER TABLE " + GPX_TABLE_NAME + " ADD " + GPX_COL_WPT_CATEGORY_NAMES + " TEXT"); + } } private boolean updateLastModifiedTime(GpxDataItem item) { @@ -382,11 +390,12 @@ public class GPXDatabase { } if (a != null) { db.execSQL( - "INSERT INTO " + GPX_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "INSERT INTO " + GPX_TABLE_NAME + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[]{ fileName, fileDir, a.totalDistance, a.totalTracks, a.startTime, a.endTime, a.timeSpan, a.timeMoving, a.totalDistanceMoving, a.diffElevationUp, a.diffElevationDown, a.avgElevation, a.minElevation, a.maxElevation, a.maxSpeed, a.avgSpeed, a.points, a.wptPoints, - color, item.file.lastModified(), item.splitType, item.splitInterval, item.apiImported ? 1 : 0}); + color, item.file.lastModified(), item.splitType, item.splitInterval, item.apiImported ? 1 : 0, + Algorithms.encodeStringSet(item.analysis.wptCategoryNames)}); } else { db.execSQL("INSERT INTO " + GPX_TABLE_NAME + "(" + GPX_COL_NAME + ", " + @@ -424,13 +433,14 @@ public class GPXDatabase { GPX_COL_AVG_SPEED + " = ?, " + GPX_COL_POINTS + " = ?, " + GPX_COL_WPT_POINTS + " = ?, " + - GPX_COL_FILE_LAST_MODIFIED_TIME + " = ? " + + GPX_COL_FILE_LAST_MODIFIED_TIME + " = ?, " + + GPX_COL_WPT_CATEGORY_NAMES + " = ? " + " WHERE " + GPX_COL_NAME + " = ? AND " + GPX_COL_DIR + " = ?", new Object[]{ a.totalDistance, a.totalTracks, a.startTime, a.endTime, a.timeSpan, a.timeMoving, a.totalDistanceMoving, a.diffElevationUp, a.diffElevationDown, a.avgElevation, a.minElevation, a.maxElevation, a.maxSpeed, a.avgSpeed, a.points, a.wptPoints, item.file.lastModified(), - fileName, fileDir }); + Algorithms.encodeStringSet(a.wptCategoryNames), fileName, fileDir }); } finally { db.close(); } @@ -463,6 +473,7 @@ public class GPXDatabase { int splitType = (int)query.getInt(20); double splitInterval = query.getDouble(21); boolean apiImported = query.getInt(22) == 1; + String wptCategoryNames = query.getString(23); GPXTrackAnalysis a = new GPXTrackAnalysis(); a.totalDistance = totalDistance; @@ -482,6 +493,9 @@ public class GPXDatabase { a.avgSpeed = avgSpeed; a.points = points; a.wptPoints = wptPoints; + if (wptCategoryNames != null) { + a.wptCategoryNames = Algorithms.decodeStringSet(wptCategoryNames); + } File dir; if (!Algorithms.isEmpty(fileDir)) { diff --git a/OsmAnd/src/net/osmand/plus/GPXUtilities.java b/OsmAnd/src/net/osmand/plus/GPXUtilities.java index 307de185fc..977c15c434 100644 --- a/OsmAnd/src/net/osmand/plus/GPXUtilities.java +++ b/OsmAnd/src/net/osmand/plus/GPXUtilities.java @@ -42,10 +42,13 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import java.util.Stack; import java.util.TimeZone; @@ -287,6 +290,8 @@ public class GPXUtilities { public int points; public int wptPoints = 0; + public Set wptCategoryNames; + public double metricEnd; public double secondaryMetricEnd; public WptPt locationStart; @@ -787,6 +792,22 @@ public class GPXUtilities { return Collections.unmodifiableList(points); } + public Map> getPointsByCategories() { + Map> res = new HashMap<>(); + for (WptPt pt : points) { + String category = pt.category == null ? "" : pt.category; + List list = res.get(category); + if (list != null) { + list.add(pt); + } else { + list = new ArrayList<>(); + list.add(pt); + res.put(category, list); + } + } + return res; + } + public boolean isPointsEmpty() { return points.isEmpty(); } @@ -868,6 +889,7 @@ public class GPXUtilities { public GPXTrackAnalysis getAnalysis(long fileTimestamp) { GPXTrackAnalysis g = new GPXTrackAnalysis(); g.wptPoints = points.size(); + g.wptCategoryNames = getWaypointCategories(true); List splitSegments = new ArrayList(); for (int i = 0; i < tracks.size(); i++) { Track subtrack = tracks.get(i); @@ -1167,14 +1189,12 @@ public class GPXUtilities { return count; } - public List getWaypointCategories() { - List categories = new ArrayList<>(); + public Set getWaypointCategories(boolean withDefaultCategory) { + Set categories = new HashSet<>(); for (WptPt pt : points) { - String category = pt.category; - if (!TextUtils.isEmpty(category)) { - if (!categories.contains(category)) { - categories.add(category); - } + String category = pt.category == null ? "" : pt.category; + if (withDefaultCategory || !TextUtils.isEmpty(category)) { + categories.add(category); } } return categories; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/SelectCategoryDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/SelectCategoryDialogFragment.java index 87ed37f512..fd0bfa8233 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/SelectCategoryDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/SelectCategoryDialogFragment.java @@ -20,11 +20,9 @@ import net.osmand.plus.IconsCache; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.quickaction.QuickAction; -import net.osmand.plus.quickaction.QuickActionFactory; -import net.osmand.plus.quickaction.QuickActionRegistry; import java.util.List; +import java.util.Set; import static android.util.TypedValue.COMPLEX_UNIT_DIP; @@ -69,7 +67,7 @@ public class SelectCategoryDialogFragment extends DialogFragment { final FavouritesDbHelper helper = ((OsmandApplication) getActivity().getApplication()).getFavorites(); if (gpxFile != null) { - List categories = gpxFile.getWaypointCategories(); + Set categories = gpxFile.getWaypointCategories(false); for (final String category : categories) { addCategory(ll, category, 0); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/AddTracksGroupBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/AddTracksGroupBottomSheetDialogFragment.java index d8feb5906e..639f243c10 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/AddTracksGroupBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/AddTracksGroupBottomSheetDialogFragment.java @@ -124,7 +124,9 @@ public class AddTracksGroupBottomSheetDialogFragment extends AddGroupBottomSheet processGPXFolder(gpxFile, sub); } else if (gpxFile.isFile() && gpxFile.getName().toLowerCase().endsWith(".gpx")) { GpxDataItem item = processedDataFiles.get(gpxFile); - if (item == null || item.getFileLastModifiedTime() != gpxFile.lastModified()) { + if (item == null + || item.getFileLastModifiedTime() != gpxFile.lastModified() + || item.getAnalysis().wptCategoryNames == null) { GPXFile f = GPXUtilities.loadGPXFile(app, gpxFile); GPXTrackAnalysis analysis = f.getAnalysis(gpxFile.lastModified()); if (item == null) {