Refactor createUniqueFileName

This commit is contained in:
Vitaliy 2020-09-15 01:57:21 +03:00
parent e9f890b9ff
commit 454ab23eed
8 changed files with 56 additions and 72 deletions

View file

@ -119,7 +119,7 @@ public class Algorithms {
} }
public static String getFileNameWithoutExtension(String name) { public static String getFileNameWithoutExtension(String name) {
int i = name.indexOf('.'); int i = name.lastIndexOf('.');
if (i >= 0) { if (i >= 0) {
name = name.substring(0, i); name = name.substring(0, i);
} }

View file

@ -8,6 +8,7 @@ import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import net.osmand.plus.GpxSelectionHelper; import net.osmand.plus.GpxSelectionHelper;
@ -19,14 +20,8 @@ import net.osmand.util.Algorithms;
import java.io.File; import java.io.File;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.regex.Pattern; 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 class FileUtils {
public static final Pattern ILLEGAL_FILE_NAME_CHARACTERS = Pattern.compile("[?:\"*|/<>]"); public static final Pattern ILLEGAL_FILE_NAME_CHARACTERS = Pattern.compile("[?:\"*|/<>]");
@ -171,20 +166,16 @@ public class FileUtils {
return dest; return dest;
} }
public static String createName(OsmandApplication app) { public static String createUniqueFileName(@NonNull OsmandApplication app, String name, String dirName, String extension) {
String displayedName; String uniqueFileName = name;
final String suggestedName = new SimpleDateFormat("EEE dd MMM yyyy", Locale.US).format(new Date()); File dir = app.getAppPath(dirName);
displayedName = suggestedName; File fout = new File(dir, name + extension);
if (app != null) { int ind = 0;
File dir = app.getAppPath(GPX_INDEX_DIR); while (fout.exists()) {
File fout = new File(dir, suggestedName + GPX_FILE_EXT); uniqueFileName = name + "_" + (++ind);
int ind = 0; fout = new File(dir, uniqueFileName + extension);
while (fout.exists()) {
displayedName = suggestedName + "_" + (++ind);
fout = new File(dir, displayedName + GPX_FILE_EXT);
}
} }
return displayedName; return uniqueFileName;
} }
public interface RenameCallback { public interface RenameCallback {

View file

@ -9,6 +9,7 @@ import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.FileUtils;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt; import net.osmand.GPXUtilities.WptPt;
@ -1005,15 +1006,14 @@ public class MapMarkersHelper {
} }
public String generateGpx(String fileName) { 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()) { if (!dir.exists()) {
dir.mkdirs(); dir.mkdirs();
} }
File fout = new File(dir, fileName + IndexConstants.GPX_FILE_EXT); String uniqueFileName = FileUtils.createUniqueFileName(ctx, fileName, dirName, IndexConstants.GPX_FILE_EXT);
int ind = 1; File fout = new File(dir, uniqueFileName + IndexConstants.GPX_FILE_EXT);
while (fout.exists()) {
fout = new File(dir, fileName + "_" + (++ind) + IndexConstants.GPX_FILE_EXT);
}
GPXFile file = new GPXFile(Version.getFullVersion(ctx)); GPXFile file = new GPXFile(Version.getFullVersion(ctx));
for (MapMarker marker : mapMarkers) { for (MapMarker marker : mapMarkers) {
WptPt wpt = new WptPt(); WptPt wpt = new WptPt();

View file

@ -294,8 +294,7 @@ public class MapActivityActions implements DialogProvider {
dlg.findViewById(R.id.DuplicateFileName).setVisibility(View.VISIBLE); dlg.findViewById(R.id.DuplicateFileName).setVisibility(View.VISIBLE);
} else { } else {
dlg.dismiss(); dlg.dismiss();
new SaveDirectionsAsyncTask(app, false) new SaveDirectionsAsyncTask(app, false).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, toSave);
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, toSave);
} }
} }
}); });
@ -325,8 +324,8 @@ public class MapActivityActions implements DialogProvider {
protected GPXFile doInBackground(File... params) { protected GPXFile doInBackground(File... params) {
if (params.length > 0) { if (params.length > 0) {
File file = params[0]; File file = params[0];
String fileName = file.getName(); String fileName = Algorithms.getFileNameWithoutExtension(file);
GPXFile gpx = app.getRoutingHelper().generateGPXFileWithRoute(fileName.substring(0, fileName.length() - GPX_FILE_EXT.length())); GPXFile gpx = app.getRoutingHelper().generateGPXFileWithRoute(fileName);
gpx.error = GPXUtilities.writeGpxFile(file, gpx); gpx.error = GPXUtilities.writeGpxFile(file, gpx);
return gpx; return gpx;
} }
@ -335,19 +334,18 @@ public class MapActivityActions implements DialogProvider {
@Override @Override
protected void onPostExecute(GPXFile gpxFile) { 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(); String errorMessage = gpxFile.error.getMessage();
if (errorMessage == null) { if (errorMessage == null) {
errorMessage = app.getString(R.string.error_occurred_saving_gpx); errorMessage = app.getString(R.string.error_occurred_saving_gpx);
} }
Toast.makeText(app, errorMessage, Toast.LENGTH_LONG).show(); 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, public void addActionsToAdapter(final double latitude,

View file

@ -43,7 +43,6 @@ import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes; import androidx.annotation.IdRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.content.res.AppCompatResources; import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.PopupMenu; import androidx.appcompat.widget.PopupMenu;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
@ -57,6 +56,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.FileUtils;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt; 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.OsmAndCompassListener;
import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities;
import net.osmand.plus.Version; 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.DMS;
import net.osmand.plus.mapmarkers.CoordinateInputFormats.Format; import net.osmand.plus.mapmarkers.CoordinateInputFormats.Format;
import net.osmand.plus.mapmarkers.adapters.CoordinateInputAdapter; import net.osmand.plus.mapmarkers.adapters.CoordinateInputAdapter;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.widgets.EditTextEx; import net.osmand.plus.widgets.EditTextEx;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import net.osmand.util.LocationParser; import net.osmand.util.LocationParser;
@ -1498,15 +1498,13 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
if (Algorithms.isEmpty(gpx.path)) { if (Algorithms.isEmpty(gpx.path)) {
if (!Algorithms.isEmpty(fileName)) { 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()) { if (!dir.exists()) {
dir.mkdirs(); dir.mkdirs();
} }
File fout = new File(dir, fileName + IndexConstants.GPX_FILE_EXT); String uniqueFileName = FileUtils.createUniqueFileName(app, fileName, dirName, IndexConstants.GPX_FILE_EXT);
int ind = 1; File fout = new File(dir, uniqueFileName + IndexConstants.GPX_FILE_EXT);
while (fout.exists()) {
fout = new File(dir, fileName + "_" + (++ind) + IndexConstants.GPX_FILE_EXT);
}
GPXUtilities.writeGpxFile(fout, gpx); GPXUtilities.writeGpxFile(fout, gpx);
} }
} else { } else {

View file

@ -21,6 +21,7 @@ import androidx.core.content.ContextCompat;
import com.google.android.material.textfield.TextInputLayout; import com.google.android.material.textfield.TextInputLayout;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.FileUtils;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; 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.helpers.AndroidUiHelper;
import net.osmand.plus.widgets.OsmandTextFieldBoxes; import net.osmand.plus.widgets.OsmandTextFieldBoxes;
import java.io.File;
import java.util.Date; import java.util.Date;
import static net.osmand.plus.mapmarkers.CoordinateInputDialogFragment.ADDED_POINTS_NUMBER_KEY; 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(); Date date = new Date();
final String suggestedName = app.getString(R.string.markers) + "_" + DateFormat.format("yyyy-MM-dd", date).toString(); String dirName = IndexConstants.GPX_INDEX_DIR + IndexConstants.MAP_MARKERS_INDEX_DIR;
String displayedName = suggestedName; String suggestedName = app.getString(R.string.markers) + "_" + DateFormat.format("yyyy-MM-dd", date).toString();
File fout = new File(dir, suggestedName + IndexConstants.GPX_FILE_EXT); String uniqueFileName = FileUtils.createUniqueFileName(app, suggestedName, dirName, IndexConstants.GPX_FILE_EXT);
int ind = 1;
while (fout.exists()) { final EditText nameEditText = mainView.findViewById(R.id.name_edit_text);
displayedName = suggestedName + "_" + (++ind); nameEditText.setText(uniqueFileName);
fout = new File(dir, displayedName + IndexConstants.GPX_FILE_EXT);
}
final EditText nameEditText = (EditText) mainView.findViewById(R.id.name_edit_text);
nameEditText.setText(displayedName);
nameEditText.setTextColor(ContextCompat.getColor(getContext(), textPrimaryColor)); nameEditText.setTextColor(ContextCompat.getColor(getContext(), textPrimaryColor));
mainView.findViewById(R.id.save_button).setOnClickListener(new View.OnClickListener() { mainView.findViewById(R.id.save_button).setOnClickListener(new View.OnClickListener() {

View file

@ -86,8 +86,11 @@ import net.osmand.util.Algorithms;
import java.io.File; import java.io.File;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
import static net.osmand.IndexConstants.GPX_FILE_EXT; import static net.osmand.IndexConstants.GPX_FILE_EXT;
import static net.osmand.IndexConstants.GPX_INDEX_DIR; import static net.osmand.IndexConstants.GPX_INDEX_DIR;
@ -1443,7 +1446,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
GpxData gpxData = editingCtx.getGpxData(); GpxData gpxData = editingCtx.getGpxData();
String displayedName; String displayedName;
if (gpxData == null) { 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 { } else {
displayedName = AndroidUtils.trimExtension(new File(gpxData.getGpxFile().path).getName()); displayedName = AndroidUtils.trimExtension(new File(gpxData.getGpxFile().path).getName());
} }

View file

@ -56,12 +56,12 @@ import net.osmand.plus.routepreparationmenu.RouteDetailsFragment.CumulativeInfo;
import net.osmand.plus.routepreparationmenu.RouteDetailsFragment.RouteDetailsFragmentListener; import net.osmand.plus.routepreparationmenu.RouteDetailsFragment.RouteDetailsFragmentListener;
import net.osmand.plus.routepreparationmenu.cards.PublicTransportCard; import net.osmand.plus.routepreparationmenu.cards.PublicTransportCard;
import net.osmand.plus.routing.RouteDirectionInfo; 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.RoutingHelper;
import net.osmand.plus.routing.TransportRoutingHelper; import net.osmand.plus.routing.TransportRoutingHelper;
import net.osmand.plus.settings.backend.OsmandSettings; 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.OsmandMapTileView;
import net.osmand.plus.views.layers.MapControlsLayer;
import net.osmand.router.TransportRouteResult; import net.osmand.router.TransportRouteResult;
import net.osmand.util.Algorithms; 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.BACK_TO_LOC_HUD_ID;
import static net.osmand.aidlapi.OsmAndCustomizationConstants.ZOOM_IN_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.aidlapi.OsmAndCustomizationConstants.ZOOM_OUT_HUD_ID;
import static net.osmand.plus.activities.MapActivityActions.*; import static net.osmand.plus.activities.MapActivityActions.SaveDirectionsAsyncTask;
import static net.osmand.plus.measurementtool.SaveAsNewTrackBottomSheetDialogFragment.*; import static net.osmand.plus.measurementtool.SaveAsNewTrackBottomSheetDialogFragment.SaveAsNewTrackFragmentListener;
public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMenuFragmentListener, public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMenuFragmentListener,
RouteDetailsFragmentListener, SaveAsNewTrackFragmentListener { RouteDetailsFragmentListener, SaveAsNewTrackFragmentListener {
@ -467,15 +467,16 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
@Override @Override
public void onClick(View v) { public void onClick(View v) {
MapActivity mapActivity = getMapActivity(); MapActivity mapActivity = getMapActivity();
OsmandApplication app = getMyApplication(); if (mapActivity != null) {
if (mapActivity != null && app != null) { OsmandApplication app = mapActivity.getMyApplication();
RoutingHelper routingHelper = app.getRoutingHelper(); GPXRouteParamsBuilder paramsBuilder = app.getRoutingHelper().getCurrentGPXRoute();
final RouteProvider.GPXRouteParamsBuilder rp = routingHelper.getCurrentGPXRoute();
final String fileName; String fileName;
if (rp == null || rp.getFile() == null || rp.getFile().path == null) { if (paramsBuilder == null || paramsBuilder.getFile() == null || paramsBuilder.getFile().path == null) {
fileName = FileUtils.createName(app); 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 { } else {
fileName = new File(rp.getFile().path).getName(); fileName = new File(paramsBuilder.getFile().path).getName();
} }
SaveAsNewTrackBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(), SaveAsNewTrackBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(),
ChooseRouteFragment.this, null, fileName, ChooseRouteFragment.this, null, fileName,