Refactor createUniqueFileName
This commit is contained in:
parent
e9f890b9ff
commit
454ab23eed
8 changed files with 56 additions and 72 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
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()) {
|
||||
displayedName = suggestedName + "_" + (++ind);
|
||||
fout = new File(dir, displayedName + GPX_FILE_EXT);
|
||||
uniqueFileName = name + "_" + (++ind);
|
||||
fout = new File(dir, uniqueFileName + extension);
|
||||
}
|
||||
}
|
||||
return displayedName;
|
||||
return uniqueFileName;
|
||||
}
|
||||
|
||||
public interface RenameCallback {
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue