Skip to content

Commit

Permalink
Merge pull request #21973 from osmandapp/fix_#19505
Browse files Browse the repository at this point in the history
Fix #19505
  • Loading branch information
Chumva authored Feb 26, 2025
2 parents d0c9cee + f6acf76 commit 12020b4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
3 changes: 3 additions & 0 deletions OsmAnd-java/src/main/java/net/osmand/gpx/GPXUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,9 @@ private static String getFilename(String path) {
if (i > 0) {
path = path.substring(0, i);
}
if (path.contains(XML_COLON)) {
path = path.replaceAll(XML_COLON, ":");
}
}
return path;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,9 @@ object GpxUtilities {
if (i > 0) {
newPath = newPath.substring(0, i)
}
if (path.contains(XML_COLON)) {
newPath = newPath.replace(XML_COLON, ":");
}
}
return newPath
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
import static net.osmand.IndexConstants.FAVORITES_INDEX_DIR;
import static net.osmand.IndexConstants.GPX_FILE_EXT;
import static net.osmand.IndexConstants.ZIP_EXT;
import static net.osmand.shared.gpx.GpxFile.XML_COLON;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import net.osmand.CallbackWithObject;
import net.osmand.PlatformUtil;
import net.osmand.plus.shared.SharedUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.Version;
import net.osmand.plus.myplaces.favorites.SaveFavoritesTask.SaveFavoritesListener;
import net.osmand.plus.shared.SharedUtil;
import net.osmand.plus.track.helpers.GpxFileLoaderTask;
import net.osmand.plus.utils.OsmAndFormatter;
import net.osmand.shared.gpx.GpxFile;
Expand All @@ -24,14 +25,7 @@

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -69,12 +63,12 @@ public File getLegacyExternalFile() {
return new File(app.getAppPath(null), LEGACY_FAV_FILE_PREFIX + GPX_FILE_EXT);
}

public File getExternalFile(FavoriteGroup group) {
@NonNull
public File getExternalFile(@NonNull FavoriteGroup group) {
File favDir = getExternalDir();
String fileName = (group.getName().isEmpty()
? FAV_FILE_PREFIX
: FAV_FILE_PREFIX + FAV_GROUP_NAME_SEPARATOR + getGroupFileName(group.getName())) + GPX_FILE_EXT;
return new File(favDir, fileName);
String fileName = group.getName().isEmpty() ? FAV_FILE_PREFIX
: FAV_FILE_PREFIX + FAV_GROUP_NAME_SEPARATOR + getGroupFileName(group.getName());
return new File(favDir, fileName + GPX_FILE_EXT);
}

@NonNull
Expand Down Expand Up @@ -190,6 +184,7 @@ public Exception saveFile(@NonNull List<FavoriteGroup> favoriteGroups, @NonNull
return SharedUtil.writeGpxFile(file, gpx);
}

@NonNull
private File getBackupsFolder() {
File folder = new File(app.getAppPath(null), BACKUP_INDEX_DIR);
if (!folder.exists()) {
Expand Down Expand Up @@ -218,6 +213,7 @@ private List<File> getBackupFilesForToday() {
return result;
}

@NonNull
public List<File> getBackupFiles() {
List<File> backupFiles = new ArrayList<>();
File[] files = getBackupsFolder().listFiles();
Expand All @@ -235,7 +231,7 @@ protected void clearOldBackups() {
clearOldBackups(getBackupFiles(), BACKUP_MAX_COUNT);
}

private void clearOldBackups(List<File> files, int maxCount) {
private void clearOldBackups(@NonNull List<File> files, int maxCount) {
if (files.size() >= maxCount) {
// sort in order from oldest to newest
Collections.sort(files, (f1, f2) -> {
Expand All @@ -248,28 +244,38 @@ private void clearOldBackups(List<File> files, int maxCount) {
}
}

@NonNull
private static String formatTime(long time) {
SimpleDateFormat format = getTimeFormatter();
return format.format(new Date(time));
}

@NonNull
private static SimpleDateFormat getTimeFormatter() {
SimpleDateFormat format = new SimpleDateFormat(TIME_PATTERN, Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
return format;
}

@NonNull
public static String getGroupFileName(@NonNull String groupName) {
if (groupName.contains("/")) {
return groupName.replaceAll("/", SUBFOLDER_PLACEHOLDER);
}
if (groupName.contains(":")) {
return groupName.replaceAll(":", XML_COLON);
}
return groupName;
}

@NonNull
public static String getGroupName(@NonNull String fileName) {
if (fileName.contains(SUBFOLDER_PLACEHOLDER)) {
return fileName.replaceAll(SUBFOLDER_PLACEHOLDER, "/");
}
if (fileName.contains(XML_COLON)) {
return fileName.replaceAll(XML_COLON, ":");
}
return fileName;
}
}

0 comments on commit 12020b4

Please sign in to comment.