From 454ab23eed444d1b7dd099109fba18332a6ed59e Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 15 Sep 2020 01:57:21 +0300 Subject: [PATCH] Refactor createUniqueFileName --- .../main/java/net/osmand/util/Algorithms.java | 2 +- OsmAnd/src/net/osmand/FileUtils.java | 29 +++++++------------ .../src/net/osmand/plus/MapMarkersHelper.java | 12 ++++---- .../plus/activities/MapActivityActions.java | 18 +++++------- .../CoordinateInputDialogFragment.java | 14 ++++----- .../SaveAsTrackBottomSheetDialogFragment.java | 22 +++++--------- .../MeasurementToolFragment.java | 6 +++- .../ChooseRouteFragment.java | 25 ++++++++-------- 8 files changed, 56 insertions(+), 72 deletions(-) diff --git a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java index 21e7fbd58c..ea2869aaa3 100644 --- a/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java +++ b/OsmAnd-java/src/main/java/net/osmand/util/Algorithms.java @@ -119,7 +119,7 @@ public class Algorithms { } public static String getFileNameWithoutExtension(String name) { - int i = name.indexOf('.'); + int i = name.lastIndexOf('.'); if (i >= 0) { name = name.substring(0, i); } diff --git a/OsmAnd/src/net/osmand/FileUtils.java b/OsmAnd/src/net/osmand/FileUtils.java index 222b706e3c..59fb36c88d 100644 --- a/OsmAnd/src/net/osmand/FileUtils.java +++ b/OsmAnd/src/net/osmand/FileUtils.java @@ -8,6 +8,7 @@ import android.view.View; import android.widget.EditText; import android.widget.Toast; +import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import net.osmand.plus.GpxSelectionHelper; @@ -19,14 +20,8 @@ import net.osmand.util.Algorithms; import java.io.File; import java.lang.ref.WeakReference; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; import java.util.regex.Pattern; -import static net.osmand.IndexConstants.GPX_FILE_EXT; -import static net.osmand.IndexConstants.GPX_INDEX_DIR; - public class FileUtils { public static final Pattern ILLEGAL_FILE_NAME_CHARACTERS = Pattern.compile("[?:\"*|/<>]"); @@ -171,20 +166,16 @@ public class FileUtils { return dest; } - public static String createName(OsmandApplication app) { - String displayedName; - final String suggestedName = new SimpleDateFormat("EEE dd MMM yyyy", Locale.US).format(new Date()); - displayedName = suggestedName; - if (app != null) { - File dir = app.getAppPath(GPX_INDEX_DIR); - File fout = new File(dir, suggestedName + GPX_FILE_EXT); - int ind = 0; - while (fout.exists()) { - displayedName = suggestedName + "_" + (++ind); - fout = new File(dir, displayedName + GPX_FILE_EXT); - } + public static String createUniqueFileName(@NonNull OsmandApplication app, String name, String dirName, String extension) { + String uniqueFileName = name; + File dir = app.getAppPath(dirName); + File fout = new File(dir, name + extension); + int ind = 0; + while (fout.exists()) { + uniqueFileName = name + "_" + (++ind); + fout = new File(dir, uniqueFileName + extension); } - return displayedName; + return uniqueFileName; } public interface RenameCallback { diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 322c19e8e4..636fcddb72 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -9,6 +9,7 @@ import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; import net.osmand.AndroidUtils; +import net.osmand.FileUtils; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.WptPt; @@ -1005,15 +1006,14 @@ public class MapMarkersHelper { } public String generateGpx(String fileName) { - final File dir = ctx.getAppPath(IndexConstants.GPX_INDEX_DIR + IndexConstants.MAP_MARKERS_INDEX_DIR); + String dirName = IndexConstants.GPX_INDEX_DIR + IndexConstants.MAP_MARKERS_INDEX_DIR; + File dir = ctx.getAppPath(dirName); if (!dir.exists()) { dir.mkdirs(); } - File fout = new File(dir, fileName + IndexConstants.GPX_FILE_EXT); - int ind = 1; - while (fout.exists()) { - fout = new File(dir, fileName + "_" + (++ind) + IndexConstants.GPX_FILE_EXT); - } + String uniqueFileName = FileUtils.createUniqueFileName(ctx, fileName, dirName, IndexConstants.GPX_FILE_EXT); + File fout = new File(dir, uniqueFileName + IndexConstants.GPX_FILE_EXT); + GPXFile file = new GPXFile(Version.getFullVersion(ctx)); for (MapMarker marker : mapMarkers) { WptPt wpt = new WptPt(); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 9ba64ab50e..19d273c9ca 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -294,8 +294,7 @@ public class MapActivityActions implements DialogProvider { dlg.findViewById(R.id.DuplicateFileName).setVisibility(View.VISIBLE); } else { dlg.dismiss(); - new SaveDirectionsAsyncTask(app, false) - .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, toSave); + new SaveDirectionsAsyncTask(app, false).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, toSave); } } }); @@ -325,8 +324,8 @@ public class MapActivityActions implements DialogProvider { protected GPXFile doInBackground(File... params) { if (params.length > 0) { File file = params[0]; - String fileName = file.getName(); - GPXFile gpx = app.getRoutingHelper().generateGPXFileWithRoute(fileName.substring(0, fileName.length() - GPX_FILE_EXT.length())); + String fileName = Algorithms.getFileNameWithoutExtension(file); + GPXFile gpx = app.getRoutingHelper().generateGPXFileWithRoute(fileName); gpx.error = GPXUtilities.writeGpxFile(file, gpx); return gpx; } @@ -335,19 +334,18 @@ public class MapActivityActions implements DialogProvider { @Override protected void onPostExecute(GPXFile gpxFile) { - if (gpxFile.error != null) { + if (gpxFile.error == null) { + app.getSelectedGpxHelper().selectGpxFile(gpxFile, showOnMap, false); + String result = app.getString(R.string.route_successfully_saved_at, gpxFile.tracks.get(0).name); + Toast.makeText(app, result, Toast.LENGTH_LONG).show(); + } else { String errorMessage = gpxFile.error.getMessage(); if (errorMessage == null) { errorMessage = app.getString(R.string.error_occurred_saving_gpx); } Toast.makeText(app, errorMessage, Toast.LENGTH_LONG).show(); - return; } - app.getSelectedGpxHelper().selectGpxFile(gpxFile, showOnMap, false); - String result = app.getString(R.string.route_successfully_saved_at, gpxFile.tracks.get(0).name); - Toast.makeText(app, result, Toast.LENGTH_LONG).show(); } - } public void addActionsToAdapter(final double latitude, diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java index 8c36930451..a68bd4c714 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java @@ -43,7 +43,6 @@ import androidx.annotation.DrawableRes; import androidx.annotation.IdRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.content.res.AppCompatResources; import androidx.appcompat.widget.PopupMenu; import androidx.core.content.ContextCompat; @@ -57,6 +56,7 @@ import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.snackbar.Snackbar; import net.osmand.AndroidUtils; +import net.osmand.FileUtils; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.WptPt; @@ -67,7 +67,6 @@ import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.OsmAndLocationProvider.OsmAndCompassListener; import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; import net.osmand.plus.OsmandApplication; -import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.Version; @@ -79,6 +78,7 @@ import net.osmand.plus.mapmarkers.CoordinateInputFormats.DDM; import net.osmand.plus.mapmarkers.CoordinateInputFormats.DMS; import net.osmand.plus.mapmarkers.CoordinateInputFormats.Format; import net.osmand.plus.mapmarkers.adapters.CoordinateInputAdapter; +import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.widgets.EditTextEx; import net.osmand.util.Algorithms; import net.osmand.util.LocationParser; @@ -1498,15 +1498,13 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm protected Void doInBackground(Void... params) { if (Algorithms.isEmpty(gpx.path)) { if (!Algorithms.isEmpty(fileName)) { - final File dir = app.getAppPath(IndexConstants.GPX_INDEX_DIR + IndexConstants.MAP_MARKERS_INDEX_DIR); + String dirName = IndexConstants.GPX_INDEX_DIR + IndexConstants.MAP_MARKERS_INDEX_DIR; + File dir = app.getAppPath(dirName); if (!dir.exists()) { dir.mkdirs(); } - File fout = new File(dir, fileName + IndexConstants.GPX_FILE_EXT); - int ind = 1; - while (fout.exists()) { - fout = new File(dir, fileName + "_" + (++ind) + IndexConstants.GPX_FILE_EXT); - } + String uniqueFileName = FileUtils.createUniqueFileName(app, fileName, dirName, IndexConstants.GPX_FILE_EXT); + File fout = new File(dir, uniqueFileName + IndexConstants.GPX_FILE_EXT); GPXUtilities.writeGpxFile(fout, gpx); } } else { diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/SaveAsTrackBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/SaveAsTrackBottomSheetDialogFragment.java index f176437ebd..a2a72d3f64 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/SaveAsTrackBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/SaveAsTrackBottomSheetDialogFragment.java @@ -21,6 +21,7 @@ import androidx.core.content.ContextCompat; import com.google.android.material.textfield.TextInputLayout; import net.osmand.AndroidUtils; +import net.osmand.FileUtils; import net.osmand.IndexConstants; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; @@ -28,7 +29,6 @@ import net.osmand.plus.base.BottomSheetDialogFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.widgets.OsmandTextFieldBoxes; -import java.io.File; import java.util.Date; import static net.osmand.plus.mapmarkers.CoordinateInputDialogFragment.ADDED_POINTS_NUMBER_KEY; @@ -85,21 +85,13 @@ public class SaveAsTrackBottomSheetDialogFragment extends BottomSheetDialogFragm } } - final File dir = app.getAppPath(IndexConstants.GPX_INDEX_DIR + "/map markers"); - if (!dir.exists()) { - dir.mkdirs(); - } Date date = new Date(); - final String suggestedName = app.getString(R.string.markers) + "_" + DateFormat.format("yyyy-MM-dd", date).toString(); - String displayedName = suggestedName; - File fout = new File(dir, suggestedName + IndexConstants.GPX_FILE_EXT); - int ind = 1; - while (fout.exists()) { - displayedName = suggestedName + "_" + (++ind); - fout = new File(dir, displayedName + IndexConstants.GPX_FILE_EXT); - } - final EditText nameEditText = (EditText) mainView.findViewById(R.id.name_edit_text); - nameEditText.setText(displayedName); + String dirName = IndexConstants.GPX_INDEX_DIR + IndexConstants.MAP_MARKERS_INDEX_DIR; + String suggestedName = app.getString(R.string.markers) + "_" + DateFormat.format("yyyy-MM-dd", date).toString(); + String uniqueFileName = FileUtils.createUniqueFileName(app, suggestedName, dirName, IndexConstants.GPX_FILE_EXT); + + final EditText nameEditText = mainView.findViewById(R.id.name_edit_text); + nameEditText.setText(uniqueFileName); nameEditText.setTextColor(ContextCompat.getColor(getContext(), textPrimaryColor)); mainView.findViewById(R.id.save_button).setOnClickListener(new View.OnClickListener() { diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index 5e2f1befdf..3c87f6a7f3 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -86,8 +86,11 @@ import net.osmand.util.Algorithms; import java.io.File; import java.lang.ref.WeakReference; import java.text.MessageFormat; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; +import java.util.Locale; import static net.osmand.IndexConstants.GPX_FILE_EXT; import static net.osmand.IndexConstants.GPX_INDEX_DIR; @@ -1443,7 +1446,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route GpxData gpxData = editingCtx.getGpxData(); String displayedName; if (gpxData == null) { - displayedName = FileUtils.createName(getMyApplication()); + String suggestedName = new SimpleDateFormat("EEE dd MMM yyyy", Locale.US).format(new Date()); + displayedName = FileUtils.createUniqueFileName(requireMyApplication(), suggestedName, GPX_INDEX_DIR, GPX_FILE_EXT); } else { displayedName = AndroidUtils.trimExtension(new File(gpxData.getGpxFile().path).getName()); } diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/ChooseRouteFragment.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/ChooseRouteFragment.java index b3e773c881..3f690c1d81 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/ChooseRouteFragment.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/ChooseRouteFragment.java @@ -56,12 +56,12 @@ import net.osmand.plus.routepreparationmenu.RouteDetailsFragment.CumulativeInfo; import net.osmand.plus.routepreparationmenu.RouteDetailsFragment.RouteDetailsFragmentListener; import net.osmand.plus.routepreparationmenu.cards.PublicTransportCard; import net.osmand.plus.routing.RouteDirectionInfo; -import net.osmand.plus.routing.RouteProvider; +import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.TransportRoutingHelper; import net.osmand.plus.settings.backend.OsmandSettings; -import net.osmand.plus.views.layers.MapControlsLayer; import net.osmand.plus.views.OsmandMapTileView; +import net.osmand.plus.views.layers.MapControlsLayer; import net.osmand.router.TransportRouteResult; import net.osmand.util.Algorithms; @@ -81,8 +81,8 @@ import static net.osmand.IndexConstants.GPX_FILE_EXT; import static net.osmand.aidlapi.OsmAndCustomizationConstants.BACK_TO_LOC_HUD_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.ZOOM_IN_HUD_ID; import static net.osmand.aidlapi.OsmAndCustomizationConstants.ZOOM_OUT_HUD_ID; -import static net.osmand.plus.activities.MapActivityActions.*; -import static net.osmand.plus.measurementtool.SaveAsNewTrackBottomSheetDialogFragment.*; +import static net.osmand.plus.activities.MapActivityActions.SaveDirectionsAsyncTask; +import static net.osmand.plus.measurementtool.SaveAsNewTrackBottomSheetDialogFragment.SaveAsNewTrackFragmentListener; public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMenuFragmentListener, RouteDetailsFragmentListener, SaveAsNewTrackFragmentListener { @@ -467,15 +467,16 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe @Override public void onClick(View v) { MapActivity mapActivity = getMapActivity(); - OsmandApplication app = getMyApplication(); - if (mapActivity != null && app != null) { - RoutingHelper routingHelper = app.getRoutingHelper(); - final RouteProvider.GPXRouteParamsBuilder rp = routingHelper.getCurrentGPXRoute(); - final String fileName; - if (rp == null || rp.getFile() == null || rp.getFile().path == null) { - fileName = FileUtils.createName(app); + if (mapActivity != null) { + OsmandApplication app = mapActivity.getMyApplication(); + GPXRouteParamsBuilder paramsBuilder = app.getRoutingHelper().getCurrentGPXRoute(); + + String fileName; + if (paramsBuilder == null || paramsBuilder.getFile() == null || paramsBuilder.getFile().path == null) { + String suggestedName = new SimpleDateFormat("EEE dd MMM yyyy", Locale.US).format(new Date()); + fileName = FileUtils.createUniqueFileName(app, suggestedName, IndexConstants.GPX_INDEX_DIR, GPX_FILE_EXT); } else { - fileName = new File(rp.getFile().path).getName(); + fileName = new File(paramsBuilder.getFile().path).getName(); } SaveAsNewTrackBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(), ChooseRouteFragment.this, null, fileName,