diff --git a/OsmAnd/src/net/osmand/FileUtils.java b/OsmAnd/src/net/osmand/FileUtils.java index 580450df60..13a5b18f4b 100644 --- a/OsmAnd/src/net/osmand/FileUtils.java +++ b/OsmAnd/src/net/osmand/FileUtils.java @@ -1,5 +1,6 @@ package net.osmand; +import android.os.AsyncTask; import android.widget.Toast; import androidx.annotation.NonNull; @@ -8,6 +9,8 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; +import net.osmand.GPXUtilities.GPXFile; +import net.osmand.GPXUtilities.Metadata; import net.osmand.plus.GpxSelectionHelper; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.OsmandApplication; @@ -109,6 +112,8 @@ public class FileUtils { selected.getGpxFile().path = dest.getAbsolutePath(); helper.updateSelectedGpxFile(selected); } + RenameGpxAsyncTask renameGpxAsyncTask = new RenameGpxAsyncTask(app, dest); + renameGpxAsyncTask.execute(); return dest; } return null; @@ -196,4 +201,34 @@ public class FileUtils { public interface RenameCallback { void renamedTo(File file); } + + private static class RenameGpxAsyncTask extends AsyncTask { + + private OsmandApplication app; + private File file; + + private RenameGpxAsyncTask(@NonNull OsmandApplication app, @NonNull File file) { + this.app = app; + this.file = file; + } + + @Override + protected Exception doInBackground(Void... voids) { + GpxSelectionHelper helper = app.getSelectedGpxHelper(); + SelectedGpxFile selected = helper.getSelectedFileByPath(file.getAbsolutePath()); + + GPXFile gpxFile; + if (selected != null && selected.getGpxFile() != null) { + gpxFile = selected.getGpxFile(); + } else { + gpxFile = GPXUtilities.loadGPXFile(file); + } + if (gpxFile.metadata == null) { + gpxFile.metadata = new Metadata(); + } + gpxFile.metadata.name = Algorithms.getFileNameWithoutExtension(file.getName()); + + return GPXUtilities.writeGpxFile(file, gpxFile); + } + } }