Close #9746 Request: Replace saving dialog in Route Details > Save

This commit is contained in:
Dima-1 2020-09-13 13:54:31 +03:00
parent a594262762
commit 2ca2e5a554
5 changed files with 114 additions and 49 deletions

View file

@ -19,8 +19,14 @@ 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("[?:\"*|/<>]");
@ -165,6 +171,22 @@ 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);
}
}
return displayedName;
}
public interface RenameCallback {
void renamedTo(File file);
}

View file

@ -293,7 +293,8 @@ public class MapActivityActions implements DialogProvider {
dlg.findViewById(R.id.DuplicateFileName).setVisibility(View.VISIBLE);
} else {
dlg.dismiss();
new SaveDirectionsAsyncTask(app).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, toSave);
new SaveDirectionsAsyncTask(app, false)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, toSave);
}
}
});
@ -309,31 +310,41 @@ public class MapActivityActions implements DialogProvider {
return dlg;
}
private static class SaveDirectionsAsyncTask extends AsyncTask<File, Void, String> {
public static class SaveDirectionsAsyncTask extends AsyncTask<File, Void, GPXFile> {
private final OsmandApplication app;
boolean showOnMap;
public SaveDirectionsAsyncTask(OsmandApplication app) {
public SaveDirectionsAsyncTask(OsmandApplication app, boolean showOnMap) {
this.app = app;
this.showOnMap = showOnMap;
}
@Override
protected String doInBackground(File... params) {
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()));
GPXUtilities.writeGpxFile(file, gpx);
return app.getString(R.string.route_successfully_saved_at, file.getName());
GPXFile gpx = app.getRoutingHelper().generateGPXFileWithRoute(fileName.substring(0, fileName.length() - GPX_FILE_EXT.length()));
gpx.error = GPXUtilities.writeGpxFile(file, gpx);
return gpx;
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (result != null) {
Toast.makeText(app, result, Toast.LENGTH_LONG).show();
protected void onPostExecute(GPXFile gpxFile) {
if (gpxFile.error != null) {
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();
}
}

View file

@ -34,6 +34,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.Track;
@ -85,11 +86,8 @@ 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;
@ -1487,18 +1485,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
GpxData gpxData = editingCtx.getGpxData();
String displayedName;
if (gpxData == null) {
final String suggestedName = new SimpleDateFormat("EEE dd MMM yyyy", Locale.US).format(new Date());
displayedName = suggestedName;
OsmandApplication app = getMyApplication();
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);
}
}
displayedName = FileUtils.createName(getMyApplication());
} else {
displayedName = AndroidUtils.trimExtension(new File(gpxData.getGpxFile().path).getName());
}

View file

@ -8,6 +8,7 @@ import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
@ -59,6 +60,7 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
private String folderName;
private boolean rightButtonEnabled = true;
private boolean showSimplifiedButton = true;
private TextInputLayout nameTextBox;
@Override
public void createMenuItems(Bundle savedInstanceState) {
@ -80,7 +82,7 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
View editNameView = View.inflate(UiUtilities.getThemedContext(app, nightMode),
R.layout.track_name_edit_text, null);
final TextInputLayout nameTextBox = editNameView.findViewById(R.id.name_text_box);
nameTextBox = editNameView.findViewById(R.id.name_text_box);
nameTextBox.setBoxBackgroundColorResource(R.color.material_text_input_layout_bg);
nameTextBox.setHint(app.getString(R.string.ltr_or_rtl_combine_via_colon,
app.getString(R.string.shared_string_file_name), "").trim());
@ -100,7 +102,7 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
@Override
public void afterTextChanged(Editable s) {
checkEmptyName(s, nameTextBox);
checkEmptyName(s.toString());
}
});
BaseBottomSheetItem editFileName = new BaseBottomSheetItem.Builder()
@ -206,6 +208,10 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
@Override
public void onItemSelected(String item) {
folderName = item;
EditText editText = nameTextBox.getEditText();
if (editText != null) {
checkEmptyName(editText.getText().toString());
}
}
};
}
@ -260,17 +266,8 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
private void renameFile() {
OsmandApplication app = getMyApplication();
if (app != null) {
File dir = getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR);
File source = dir;
if (sourceFolderName != null) {
source = new File(dir, sourceFolderName);
}
source = new File(source, sourceFileName + IndexConstants.GPX_FILE_EXT);
File dest = dir;
if (folderName != null) {
dest = new File(dir, folderName);
}
dest = new File(dest, fileName + IndexConstants.GPX_FILE_EXT);
File source = getFile(app, sourceFolderName, sourceFileName);
File dest = getFile(app, folderName, fileName);
if (!source.equals(dest)) {
if (dest.exists()) {
Toast.makeText(app, R.string.file_with_name_already_exists, Toast.LENGTH_LONG).show();
@ -290,25 +287,46 @@ public class SaveAsNewTrackBottomSheetDialogFragment extends MenuBottomSheetDial
}
}
private File getFile(OsmandApplication app, String folderName, String fileName) {
File dir = app.getAppPath(IndexConstants.GPX_INDEX_DIR);
File source = dir;
if (folderName != null) {
source = new File(dir, folderName);
}
source = new File(source, fileName + IndexConstants.GPX_FILE_EXT);
return source;
}
@Override
protected boolean isRightBottomButtonEnabled() {
return rightButtonEnabled;
}
private void checkEmptyName(Editable name, TextInputLayout nameCaption) {
String text = name.toString().trim();
private void checkEmptyName(String name) {
rightButtonEnabled = false;
String text = name.trim();
if (text.isEmpty()) {
nameCaption.setError(getString(R.string.empty_filename));
rightButtonEnabled = false;
nameTextBox.setError(getString(R.string.empty_filename));
} else if (isFileExist(name)) {
nameTextBox.setError(getString(R.string.file_with_name_already_exist));
} else {
nameCaption.setError(null);
nameTextBox.setError(null);
fileName = text;
rightButtonEnabled = true;
}
updateBottomButtons();
}
interface SaveAsNewTrackFragmentListener {
private boolean isFileExist(String name) {
OsmandApplication app = getMyApplication();
if (app != null) {
File file = getFile(app, folderName, name);
return file.exists();
}
return false;
}
public interface SaveAsNewTrackFragmentListener {
void onSaveAsNewTrack(String folderName, String fileName, boolean showOnMap, boolean simplifiedTrack);

View file

@ -6,6 +6,7 @@ import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
@ -33,7 +34,9 @@ import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import net.osmand.AndroidUtils;
import net.osmand.FileUtils;
import net.osmand.GPXUtilities;
import net.osmand.IndexConstants;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.plus.GpxSelectionHelper.GpxDisplayItem;
@ -42,17 +45,18 @@ import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.MapActivityActions;
import net.osmand.plus.activities.PrintDialogActivity;
import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.base.ContextMenuFragment;
import net.osmand.plus.base.ContextMenuFragment.ContextMenuFragmentListener;
import net.osmand.plus.base.ContextMenuFragment.MenuState;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.measurementtool.SaveAsNewTrackBottomSheetDialogFragment;
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.RoutingHelper;
import net.osmand.plus.routing.TransportRoutingHelper;
import net.osmand.plus.settings.backend.OsmandSettings;
@ -73,12 +77,15 @@ import java.util.Date;
import java.util.List;
import java.util.Locale;
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.*;
public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMenuFragmentListener,
RouteDetailsFragmentListener {
RouteDetailsFragmentListener, SaveAsNewTrackFragmentListener {
public static final String TAG = "ChooseRouteFragment";
public static final String ROUTE_INDEX_KEY = "route_index_key";
@ -460,9 +467,19 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
@Override
public void onClick(View v) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
RoutingHelper routingHelper = mapActivity.getMyApplication().getRoutingHelper();
MapActivityActions.createSaveDirections(mapActivity, routingHelper).show();
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);
} else {
fileName = new File(rp.getFile().path).getName();
}
SaveAsNewTrackBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(),
ChooseRouteFragment.this, null, fileName,
false, true);
}
}
};
@ -861,6 +878,16 @@ public class ChooseRouteFragment extends BaseOsmAndFragment implements ContextMe
}
}
@Override
public void onSaveAsNewTrack(String folderName, String fileName, boolean showOnMap, boolean simplifiedTrack) {
OsmandApplication app = getMyApplication();
if (app != null) {
File fileDir = new File(app.getAppPath(IndexConstants.GPX_INDEX_DIR), folderName == null ? "" : folderName);
File toSave = new File(fileDir, fileName + GPX_FILE_EXT);
new SaveDirectionsAsyncTask(app, showOnMap).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, toSave);
}
}
public class RoutesPagerAdapter extends FragmentPagerAdapter {
private int routesCount;