diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesHelper.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesHelper.java index 03d67f37644..dbcfbe6f9de 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesHelper.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/TrackSortModesHelper.java @@ -20,7 +20,7 @@ public class TrackSortModesHelper { - private static final String ROOT_FOLDER = IndexConstants.GPX_INDEX_DIR; + private static final String ROOT_FOLDER_ID = ""; private static final String SEPARATOR = ",,"; private final Map cachedSortModes = new ConcurrentHashMap<>(); @@ -34,27 +34,29 @@ public TrackSortModesHelper(@NonNull OsmandApplication app) { @NonNull public TracksSortMode getRootFolderSortMode() { - return requireSortMode(""); + return requireSortMode(ROOT_FOLDER_ID); } @NonNull public TracksSortMode requireSortMode(@Nullable String id) { - TracksSortMode sortMode = getSortMode(id); + TracksSortMode sortMode = id != null ? getSortMode(id) : null; return sortMode != null ? sortMode : TracksSortMode.getDefaultSortMode(); } @Nullable - public TracksSortMode getSortMode(@Nullable String id) { + public TracksSortMode getSortMode(@NonNull String id) { id = removeExtraFileSeparator(id); return cachedSortModes.get(id); } - public void setSortMode(@NonNull String id, @NonNull TracksSortMode sortMode) { + public void setSortMode(@NonNull String id, @Nullable TracksSortMode sortMode) { id = removeExtraFileSeparator(id); cachedSortModes.put(id, sortMode); } public void setSortModes(@NonNull Map sortModes) { + // The extra file separator is not checked here to avoid redundant validations. + // It should be handled in the methods that call this one. cachedSortModes.clear(); cachedSortModes.putAll(sortModes); } @@ -69,7 +71,7 @@ public void updateAfterMoveTrackFolder(@NonNull TrackFolder trackFolder, @NonNul } public void updateAfterDeleteTrackFolder(@NonNull TrackFolder trackFolder) { - cachedSortModes.remove(trackFolder.getId()); + setSortMode(trackFolder.getId(), null); syncSettings(); } @@ -83,7 +85,8 @@ private void loadFromPreference(@NonNull OsmandApplication app) { for (String token : tokens) { String[] tokenParts = token.split(SEPARATOR); if (tokenParts.length == 2) { - cachedSortModes.put(tokenParts[0], TracksSortMode.getByValue(tokenParts[1])); + String id = removeExtraFileSeparator(tokenParts[0]); + cachedSortModes.put(id, TracksSortMode.getByValue(tokenParts[1])); } } UpgradeTrackSortModeKeysAlgorithm.execute(app, this); @@ -93,25 +96,33 @@ private void loadFromPreference(@NonNull OsmandApplication app) { private void saveToPreference() { List tokens = new ArrayList<>(); for (Entry entry : cachedSortModes.entrySet()) { - tokens.add(entry.getKey() + SEPARATOR + entry.getValue().name()); + TracksSortMode value = entry.getValue(); + if (value != null) { + tokens.add(entry.getKey() + SEPARATOR + value.name()); + } } preference.setStringsList(tokens); } @NonNull public static String getFolderId(@NonNull String absolutePath) { - int index = absolutePath.indexOf(ROOT_FOLDER); + String basePath = IndexConstants.GPX_INDEX_DIR; + int index = absolutePath.indexOf(basePath); if (index > 0) { - index += ROOT_FOLDER.length(); + index += basePath.length(); + String relativePath = absolutePath.substring(index); + return removeExtraFileSeparator(relativePath); + } else if (absolutePath.endsWith(removeExtraFileSeparator(basePath))) { + return ROOT_FOLDER_ID; } - return index > 0 ? absolutePath.substring(index) : absolutePath; + return absolutePath; } - @Nullable - private static String removeExtraFileSeparator(@Nullable String id) { + @NonNull + private static String removeExtraFileSeparator(@NonNull String id) { // Ensure consistency by removing trailing File.separator from relative paths // before querying or saving to settings to avoid key mismatches. - if (id != null && id.endsWith(File.separator)) { + if (id.endsWith(File.separator)) { return id.substring(0, id.length() - 1); } return id; diff --git a/OsmAnd/src/net/osmand/plus/configmap/tracks/UpgradeTrackSortModeKeysAlgorithm.java b/OsmAnd/src/net/osmand/plus/configmap/tracks/UpgradeTrackSortModeKeysAlgorithm.java index c12527091fa..16788df5797 100644 --- a/OsmAnd/src/net/osmand/plus/configmap/tracks/UpgradeTrackSortModeKeysAlgorithm.java +++ b/OsmAnd/src/net/osmand/plus/configmap/tracks/UpgradeTrackSortModeKeysAlgorithm.java @@ -57,7 +57,7 @@ private void executeImpl() { putUpgradedKey(upgradedCache, TrackTabType.ON_MAP.name()); putUpgradedKey(upgradedCache, TrackTabType.ALL.name()); putUpgradedKey(upgradedCache, TrackTabType.FOLDERS.name()); - for (String id : getDirIds()) { + for (String id : collectStandardDirectoriesIds()) { putUpgradedKey(upgradedCache, id); } for (SmartFolder folder : smartFolderHelper.getSmartFolders()) { @@ -68,20 +68,20 @@ private void executeImpl() { } @NonNull - private Set getDirIds() { - Set ids = new HashSet<>(); + private Set collectStandardDirectoriesIds() { + Set result = new HashSet<>(); + // add root directory + result.add(""); + // collect all subdirectories which contains at least one gpx track for (GpxDataItem item : gpxDbHelper.getItems()) { KFile file = item.getFile(); - KFile dir = file.getParentFile(); - if (dir != null) { - String path = dir.absolutePath(); - if (!path.endsWith(File.separator)) { - path += File.separator; - } - ids.add(getFolderIdV2(path)); + KFile directory = file.getParentFile(); + if (directory != null) { + String path = directory.absolutePath(); + result.add(getFolderIdV2(path)); } } - return ids; + return result; } private void putUpgradedKey(@NonNull Map map, @NonNull String id) { @@ -91,6 +91,7 @@ private void putUpgradedKey(@NonNull Map map, @NonNull S } } + @Nullable private TracksSortMode getSortMode(@NonNull String id) { TracksSortMode sortMode = sortModesHelper.getSortMode(id); if (sortMode == null) {