diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index 83ef171fda..0cb1ae2f33 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -1,6 +1,5 @@ package net.osmand.plus.measurementtool; -import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -61,6 +60,7 @@ import net.osmand.plus.measurementtool.OptionsBottomSheetDialogFragment.OptionsF import net.osmand.plus.measurementtool.RouteBetweenPointsBottomSheetDialogFragment.RouteBetweenPointsDialogMode; import net.osmand.plus.measurementtool.RouteBetweenPointsBottomSheetDialogFragment.RouteBetweenPointsDialogType; import net.osmand.plus.measurementtool.RouteBetweenPointsBottomSheetDialogFragment.RouteBetweenPointsFragmentListener; +import net.osmand.plus.measurementtool.SaveGpxRouteAsyncTask.SaveGpxRouteListener; import net.osmand.plus.measurementtool.SelectedPointBottomSheetDialogFragment.SelectedPointFragmentListener; import net.osmand.plus.measurementtool.adapter.MeasurementToolAdapter; import net.osmand.plus.measurementtool.adapter.MeasurementToolAdapter.MeasurementAdapterListener; @@ -83,6 +83,8 @@ import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarView; import net.osmand.router.RoutePlannerFrontEnd.GpxRouteApproximation; import java.io.File; +import java.lang.ref.WeakReference; +import java.text.MessageFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; @@ -145,9 +147,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route private MeasurementEditingContext editingCtx = new MeasurementEditingContext(); private LatLon initialPoint; - private OsmandApplication app; - private MapActivity mapActivity; - private MeasurementToolLayer measurementToolLayer; enum SaveType { ROUTE_POINT, @@ -160,6 +159,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route SHOW_IS_SAVED_FRAGMENT } + protected MeasurementEditingContext getEditingCtx() { + return editingCtx; + } + private void setEditingCtx(MeasurementEditingContext editingCtx) { this.editingCtx = editingCtx; } @@ -168,7 +171,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route this.initialPoint = initialPoint; } - void setMode(int mode, boolean on) { + private void setMode(int mode, boolean on) { int modes = this.modes; if (on) { modes |= mode; @@ -207,7 +210,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, - @Nullable Bundle savedInstanceState) { + @Nullable Bundle savedInstanceState) { final MapActivity mapActivity = (MapActivity) getActivity(); if (mapActivity == null) { return null; @@ -1059,7 +1062,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route } @Nullable - GpxData setupGpxData(@Nullable GPXFile gpxFile) { + private GpxData setupGpxData(@Nullable GPXFile gpxFile) { GpxData gpxData = null; if (gpxFile != null) { QuadRect rect = gpxFile.getRect(); @@ -1524,25 +1527,118 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route return displayedName; } - private void saveNewGpx(File dir, String fileName, boolean showOnMap, SaveType saveType, FinalSaveAction finalSaveAction) { - saveGpx(dir, fileName, showOnMap, null, null, saveType, finalSaveAction); + private void saveNewGpx(@NonNull File dir, @NonNull String fileName, boolean showOnMap, SaveType saveType, FinalSaveAction finalSaveAction) { + saveGpx(new File(dir, fileName), null, null, saveType, finalSaveAction, showOnMap); } - private void saveExistingGpx(GPXFile gpx, boolean showOnMap, ActionType actionType, SaveType saveType, - FinalSaveAction finalSaveAction) { - saveGpx(null, null, showOnMap, gpx, actionType, saveType, finalSaveAction); + private void saveExistingGpx(@NonNull GPXFile gpx, boolean showOnMap, ActionType actionType, + SaveType saveType, FinalSaveAction finalSaveAction) { + saveGpx(new File(gpx.path), gpx, actionType, saveType, finalSaveAction, showOnMap); } - @SuppressLint("StaticFieldLeak") - private void saveGpx(final File dir, - final String fileName, - final boolean showOnMap, - final GPXFile gpxFile, - final ActionType actionType, - final SaveType saveType, - final FinalSaveAction finalSaveAction) { + private void saveGpx(@NonNull final File outFile, @Nullable GPXFile gpxFile, final ActionType actionType, + SaveType saveType, final FinalSaveAction finalSaveAction, final boolean showOnMap) { + SaveGpxRouteListener saveGpxRouteListener = new SaveGpxRouteListener() { + @Override + public void gpxSavingFinished(Exception warning, GPXFile savedGpxFile, File backupFile) { + onGpxSaved(warning, savedGpxFile, outFile, backupFile, actionType, finalSaveAction, showOnMap); + } + }; - new SaveGPX(this, gpxFile, dir, fileName, saveType, showOnMap, actionType, finalSaveAction, app, mapActivity, measurementToolLayer, nightMode).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + SaveGpxRouteAsyncTask saveTask = new SaveGpxRouteAsyncTask(this, outFile, gpxFile, actionType, saveType, showOnMap, saveGpxRouteListener); + saveTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + + private void onGpxSaved(Exception warning, GPXFile savedGpxFile, final File outFile, final File backupFile, + final ActionType actionType, FinalSaveAction finalSaveAction, final boolean showOnMap) { + MapActivity mapActivity = getMapActivity(); + if (mapActivity == null) { + return; + } + mapActivity.refreshMap(); + if (warning == null) { + if (editingCtx.isNewData() && savedGpxFile != null) { + QuadRect rect = savedGpxFile.getRect(); + TrkSegment segment = savedGpxFile.getNonEmptyTrkSegment(); + GpxData gpxData = new GpxData(savedGpxFile, rect, ActionType.EDIT_SEGMENT, segment); + editingCtx.setGpxData(gpxData); + updateToolbar(); + } + if (isInEditMode()) { + editingCtx.setChangesSaved(); + dismiss(mapActivity); + } else { + switch (finalSaveAction) { + case SHOW_SNACK_BAR_AND_CLOSE: + final WeakReference mapActivityRef = new WeakReference<>(mapActivity); + snackbar = Snackbar.make(mapActivity.getLayout(), + MessageFormat.format(getString(R.string.gpx_saved_sucessfully), outFile.getName()), + Snackbar.LENGTH_LONG) + .setAction(R.string.shared_string_undo, new OnClickListener() { + @Override + public void onClick(View view) { + MapActivity mapActivity = mapActivityRef.get(); + if (mapActivity != null) { + OsmandApplication app = mapActivity.getMyApplication(); + FileUtils.removeGpxFile(app, outFile); + if (backupFile != null) { + FileUtils.renameGpxFile(app, backupFile, outFile); + GPXFile gpx = GPXUtilities.loadGPXFile(outFile); + setupGpxData(gpx); + if (showOnMap) { + showGpxOnMap(app, gpx, actionType, false); + } + } else { + setupGpxData(null); + } + setMode(UNDO_MODE, true); + MeasurementToolFragment.showInstance(mapActivity.getSupportFragmentManager(), + editingCtx, modes); + } + } + }) + .addCallback(new Snackbar.Callback() { + @Override + public void onDismissed(Snackbar transientBottomBar, int event) { + if (event != DISMISS_EVENT_ACTION) { + editingCtx.setChangesSaved(); + } + super.onDismissed(transientBottomBar, event); + } + }); + snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_action) + .setAllCaps(false); + UiUtilities.setupSnackbar(snackbar, nightMode); + snackbar.show(); + dismiss(mapActivity, false); + break; + case SHOW_IS_SAVED_FRAGMENT: + editingCtx.setChangesSaved(); + SavedTrackBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(), + outFile.getAbsolutePath()); + dismiss(mapActivity); + break; + case SHOW_TOAST: + editingCtx.setChangesSaved(); + if (savedGpxFile != null && !savedGpxFile.showCurrentTrack) { + Toast.makeText(mapActivity, + MessageFormat.format(getString(R.string.gpx_saved_sucessfully), outFile.getAbsolutePath()), + Toast.LENGTH_LONG).show(); + } + } + } + } else { + Toast.makeText(mapActivity, warning.getMessage(), Toast.LENGTH_LONG).show(); + } + } + + protected static void showGpxOnMap(OsmandApplication app, GPXFile gpx, ActionType actionType, boolean isNewGpx) { + SelectedGpxFile sf = app.getSelectedGpxHelper().selectGpxFile(gpx, true, false); + if (sf != null && !isNewGpx) { + if (actionType == ActionType.ADD_SEGMENT || actionType == ActionType.EDIT_SEGMENT) { + sf.processPoints(app); + } + } } private void updateUndoRedoButton(boolean enable, View view) { @@ -1575,7 +1671,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route updateToolbar(); } - void updateToolbar() { + private void updateToolbar() { MapActivity mapActivity = getMapActivity(); if (mapActivity == null) { return; @@ -1706,7 +1802,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route } } - void dismiss(@NonNull MapActivity mapActivity) { + private void dismiss(@NonNull MapActivity mapActivity) { dismiss(mapActivity, true); } @@ -1928,5 +2024,4 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route public boolean isNightModeForMapControls() { return nightMode; } - -} +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/SaveGPX.java b/OsmAnd/src/net/osmand/plus/measurementtool/SaveGPX.java deleted file mode 100644 index de47191ab5..0000000000 --- a/OsmAnd/src/net/osmand/plus/measurementtool/SaveGPX.java +++ /dev/null @@ -1,297 +0,0 @@ -package net.osmand.plus.measurementtool; - -import android.annotation.SuppressLint; -import android.app.ProgressDialog; -import android.os.AsyncTask; -import android.view.View; -import android.widget.TextView; -import android.widget.Toast; - -import com.google.android.material.snackbar.Snackbar; - -import net.osmand.FileUtils; -import net.osmand.GPXUtilities; -import net.osmand.data.QuadRect; -import net.osmand.plus.GpxSelectionHelper; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.R; -import net.osmand.plus.UiUtilities; -import net.osmand.plus.Version; -import net.osmand.plus.activities.MapActivity; -import net.osmand.util.Algorithms; - -import java.io.File; -import java.lang.ref.WeakReference; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.List; - -import static net.osmand.IndexConstants.GPX_FILE_EXT; - -class SaveGPX extends AsyncTask { - - private final MeasurementToolFragment measurementToolFragment; - private final GPXUtilities.GPXFile gpxFile; - private final File dir; - private final String fileName; - private final MeasurementToolFragment.SaveType saveType; - private final boolean showOnMap; - private final GpxData.ActionType actionType; - private final MeasurementToolFragment.FinalSaveAction finalSaveAction; - private final OsmandApplication app; - @SuppressLint("StaticFieldLeak") - private final MapActivity mapActivity; - private MeasurementToolLayer measurementToolLayer; - private boolean nightMode; - private ProgressDialog progressDialog; - private File backupFile; - private File outFile; - private GPXUtilities.GPXFile savedGpxFile; - public MeasurementEditingContext editingCtx = new MeasurementEditingContext(); - private static final int UNDO_MODE = 0x8; - - public SaveGPX(MeasurementToolFragment measurementToolFragment, GPXUtilities.GPXFile gpxFile, File dir, String fileName, MeasurementToolFragment.SaveType saveType, boolean showOnMap, GpxData.ActionType actionType, MeasurementToolFragment.FinalSaveAction finalSaveAction, OsmandApplication app, MapActivity mapActivity, MeasurementToolLayer measurementToolLayer, boolean nightMode) { - this.measurementToolFragment = measurementToolFragment; - this.gpxFile = gpxFile; - this.dir = dir; - this.fileName = fileName; - this.saveType = saveType; - this.showOnMap = showOnMap; - this.actionType = actionType; - this.finalSaveAction = finalSaveAction; - this.app = app; - this.mapActivity = mapActivity; - this.measurementToolLayer = measurementToolLayer; - this.nightMode = nightMode; - } - - @Override - protected void onPreExecute() { - measurementToolFragment.cancelModes(); - if (mapActivity != null) { - progressDialog = new ProgressDialog(mapActivity); - progressDialog.setMessage(measurementToolFragment.getString(R.string.saving_gpx_tracks)); - progressDialog.show(); - } - } - - @Override - protected Exception doInBackground(Void... voids) { - if (app == null) { - return null; - } - List points = editingCtx.getPoints(); - GPXUtilities.TrkSegment before = editingCtx.getBeforeTrkSegmentLine(); - GPXUtilities.TrkSegment after = editingCtx.getAfterTrkSegmentLine(); - if (gpxFile == null) { - outFile = new File(dir, fileName); - String trackName = fileName.substring(0, fileName.length() - GPX_FILE_EXT.length()); - GPXUtilities.GPXFile gpx = new GPXUtilities.GPXFile(Version.getFullVersion(app)); - if (measurementToolLayer != null) { - if (saveType == MeasurementToolFragment.SaveType.LINE) { - GPXUtilities.TrkSegment segment = new GPXUtilities.TrkSegment(); - if (editingCtx.hasRoute()) { - segment.points.addAll(editingCtx.getRoutePoints()); - } else { - segment.points.addAll(before.points); - segment.points.addAll(after.points); - } - GPXUtilities.Track track = new GPXUtilities.Track(); - track.name = trackName; - track.segments.add(segment); - gpx.tracks.add(track); - } else if (saveType == MeasurementToolFragment.SaveType.ROUTE_POINT) { - if (editingCtx.hasRoute()) { - GPXUtilities.GPXFile newGpx = editingCtx.exportRouteAsGpx(trackName); - if (newGpx != null) { - gpx = newGpx; - } - } - gpx.addRoutePoints(points); - } - } - Exception res = GPXUtilities.writeGpxFile(outFile, gpx); - gpx.path = outFile.getAbsolutePath(); - savedGpxFile = gpx; - if (showOnMap) { - showGpxOnMap(app, gpx, true); - } - return res; - } else { - GPXUtilities.GPXFile gpx = gpxFile; - outFile = new File(gpx.path); - backupFile = FileUtils.backupFile(app, outFile); - String trackName = Algorithms.getFileNameWithoutExtension(outFile); - if (measurementToolLayer != null) { - if (measurementToolFragment.isPlanRouteMode()) { - if (saveType == MeasurementToolFragment.SaveType.LINE) { - GPXUtilities.TrkSegment segment = new GPXUtilities.TrkSegment(); - if (editingCtx.hasRoute()) { - segment.points.addAll(editingCtx.getRoutePoints()); - } else { - segment.points.addAll(before.points); - segment.points.addAll(after.points); - } - GPXUtilities.Track track = new GPXUtilities.Track(); - track.name = trackName; - track.segments.add(segment); - gpx.tracks.add(track); - } else if (saveType == MeasurementToolFragment.SaveType.ROUTE_POINT) { - if (editingCtx.hasRoute()) { - GPXUtilities.GPXFile newGpx = editingCtx.exportRouteAsGpx(trackName); - if (newGpx != null) { - gpx = newGpx; - } - } - gpx.addRoutePoints(points); - } - } else if (actionType != null) { - GpxData gpxData = editingCtx.getGpxData(); - switch (actionType) { - case ADD_SEGMENT: { - List snappedPoints = new ArrayList<>(); - snappedPoints.addAll(before.points); - snappedPoints.addAll(after.points); - gpx.addTrkSegment(snappedPoints); - break; - } - case ADD_ROUTE_POINTS: { - gpx.replaceRoutePoints(points); - break; - } - case EDIT_SEGMENT: { - if (gpxData != null) { - GPXUtilities.TrkSegment segment = new GPXUtilities.TrkSegment(); - segment.points.addAll(points); - gpx.replaceSegment(gpxData.getTrkSegment(), segment); - } - break; - } - case OVERWRITE_SEGMENT: { - if (gpxData != null) { - List snappedPoints = new ArrayList<>(); - snappedPoints.addAll(before.points); - snappedPoints.addAll(after.points); - GPXUtilities.TrkSegment segment = new GPXUtilities.TrkSegment(); - segment.points.addAll(snappedPoints); - gpx.replaceSegment(gpxData.getTrkSegment(), segment); - } - break; - } - } - } else { - gpx.addRoutePoints(points); - } - } - Exception res = null; - if (!gpx.showCurrentTrack) { - res = GPXUtilities.writeGpxFile(outFile, gpx); - } - savedGpxFile = gpx; - if (showOnMap) { - showGpxOnMap(app, gpx, false); - } - return res; - } - } - - private void showGpxOnMap(OsmandApplication app, GPXUtilities.GPXFile gpx, boolean isNewGpx) { - GpxSelectionHelper.SelectedGpxFile sf = app.getSelectedGpxHelper().selectGpxFile(gpx, true, false); - if (sf != null && !isNewGpx) { - if (actionType == GpxData.ActionType.ADD_SEGMENT || actionType == GpxData.ActionType.EDIT_SEGMENT) { - sf.processPoints(app); - } - } - } - - @Override - protected void onPostExecute(Exception warning) { - onGpxSaved(warning); - } - - private void onGpxSaved(Exception warning) { - if (mapActivity == null) { - return; - } - if (progressDialog != null && progressDialog.isShowing()) { - progressDialog.dismiss(); - } - mapActivity.refreshMap(); - if (warning == null) { - if (editingCtx.isNewData() && savedGpxFile != null) { - QuadRect rect = savedGpxFile.getRect(); - GPXUtilities.TrkSegment segment = savedGpxFile.getNonEmptyTrkSegment(); - GpxData gpxData = new GpxData(savedGpxFile, rect, GpxData.ActionType.EDIT_SEGMENT, segment); - editingCtx.setGpxData(gpxData); - measurementToolFragment.updateToolbar(); - } - if (measurementToolFragment.isInEditMode()) { - editingCtx.setChangesSaved(); - measurementToolFragment.dismiss(mapActivity); - } else { - switch (finalSaveAction) { - case SHOW_SNACK_BAR_AND_CLOSE: - final WeakReference mapActivityRef = new WeakReference<>(mapActivity); - Snackbar snackbar = Snackbar.make(mapActivity.getLayout(), - MessageFormat.format(measurementToolFragment.getString(R.string.gpx_saved_sucessfully), outFile.getName()), - Snackbar.LENGTH_LONG) - .setAction(R.string.shared_string_undo, new View.OnClickListener() { - @Override - public void onClick(View view) { - MapActivity mapActivity = mapActivityRef.get(); - if (mapActivity != null) { - if (outFile != null) { - OsmandApplication app = mapActivity.getMyApplication(); - FileUtils.removeGpxFile(app, outFile); - if (backupFile != null) { - FileUtils.renameGpxFile(app, backupFile, outFile); - GPXUtilities.GPXFile gpx = GPXUtilities.loadGPXFile(outFile); - measurementToolFragment.setupGpxData(gpx); - if (showOnMap) { - showGpxOnMap(app, gpx, false); - } - } else { - measurementToolFragment.setupGpxData(null); - } - } - measurementToolFragment.setMode(UNDO_MODE, true); - MeasurementToolFragment.showInstance(mapActivity.getSupportFragmentManager() - ); - } - } - }) - .addCallback(new Snackbar.Callback() { - @Override - public void onDismissed(Snackbar transientBottomBar, int event) { - if (event != DISMISS_EVENT_ACTION) { - editingCtx.setChangesSaved(); - } - super.onDismissed(transientBottomBar, event); - } - }); - snackbar.getView().findViewById(com.google.android.material.R.id.snackbar_action) - .setAllCaps(false); - UiUtilities.setupSnackbar(snackbar, nightMode); - snackbar.show(); - measurementToolFragment.dismiss(mapActivity); - break; - case SHOW_IS_SAVED_FRAGMENT: - editingCtx.setChangesSaved(); - SavedTrackBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(), - outFile.getAbsolutePath()); - measurementToolFragment.dismiss(mapActivity); - break; - case SHOW_TOAST: - editingCtx.setChangesSaved(); - if (!savedGpxFile.showCurrentTrack) { - Toast.makeText(mapActivity, - MessageFormat.format(measurementToolFragment.getString(R.string.gpx_saved_sucessfully), outFile.getAbsolutePath()), - Toast.LENGTH_LONG).show(); - } - } - } - } else { - Toast.makeText(mapActivity, warning.getMessage(), Toast.LENGTH_LONG).show(); - } - } -} diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/SaveGpxRouteAsyncTask.java b/OsmAnd/src/net/osmand/plus/measurementtool/SaveGpxRouteAsyncTask.java new file mode 100644 index 0000000000..ddae8ed26d --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/measurementtool/SaveGpxRouteAsyncTask.java @@ -0,0 +1,210 @@ +package net.osmand.plus.measurementtool; + +import android.app.ProgressDialog; +import android.content.Context; +import android.os.AsyncTask; + +import net.osmand.AndroidUtils; +import net.osmand.FileUtils; +import net.osmand.GPXUtilities; +import net.osmand.GPXUtilities.GPXFile; +import net.osmand.GPXUtilities.Track; +import net.osmand.GPXUtilities.TrkSegment; +import net.osmand.GPXUtilities.WptPt; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.Version; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.measurementtool.GpxData.ActionType; +import net.osmand.plus.measurementtool.MeasurementToolFragment.SaveType; +import net.osmand.util.Algorithms; + +import java.io.File; +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.List; + +import static net.osmand.IndexConstants.GPX_FILE_EXT; + +class SaveGpxRouteAsyncTask extends AsyncTask { + + private WeakReference fragmentRef; + private ProgressDialog progressDialog; + + private SaveType saveType; + private ActionType actionType; + + private File outFile; + private File backupFile; + private GPXFile gpxFile; + private GPXFile savedGpxFile; + private boolean showOnMap; + + private SaveGpxRouteListener saveGpxRouteListener; + + + public SaveGpxRouteAsyncTask(MeasurementToolFragment fragment, File outFile, GPXFile gpxFile, + ActionType actionType, SaveType saveType, boolean showOnMap, SaveGpxRouteListener saveGpxRouteListener) { + fragmentRef = new WeakReference<>(fragment); + this.outFile = outFile; + this.showOnMap = showOnMap; + this.gpxFile = gpxFile; + this.actionType = actionType; + this.saveType = saveType; + this.saveGpxRouteListener = saveGpxRouteListener; + } + + @Override + protected void onPreExecute() { + MeasurementToolFragment fragment = fragmentRef.get(); + if (fragment != null && fragment.getContext() != null) { + fragment.cancelModes(); + + Context ctx = fragment.getContext(); + progressDialog = new ProgressDialog(ctx); + progressDialog.setMessage(ctx.getString(R.string.saving_gpx_tracks)); + progressDialog.show(); + } + } + + @Override + protected Exception doInBackground(Void... voids) { + MeasurementToolFragment fragment = fragmentRef.get(); + if (fragment == null || fragment.getActivity() == null) { + return null; + } + MapActivity mapActivity = (MapActivity) fragment.getActivity(); + OsmandApplication app = mapActivity.getMyApplication(); + MeasurementToolLayer measurementLayer = mapActivity.getMapLayers().getMeasurementToolLayer(); + MeasurementEditingContext editingCtx = fragment.getEditingCtx(); + + List points = editingCtx.getPoints(); + TrkSegment before = editingCtx.getBeforeTrkSegmentLine(); + TrkSegment after = editingCtx.getAfterTrkSegmentLine(); + if (gpxFile == null) { + String fileName = outFile.getName(); + String trackName = fileName.substring(0, fileName.length() - GPX_FILE_EXT.length()); + GPXFile gpx = new GPXFile(Version.getFullVersion(app)); + if (measurementLayer != null) { + if (saveType == MeasurementToolFragment.SaveType.LINE) { + TrkSegment segment = new TrkSegment(); + if (editingCtx.hasRoute()) { + segment.points.addAll(editingCtx.getRoutePoints()); + } else { + segment.points.addAll(before.points); + segment.points.addAll(after.points); + } + Track track = new Track(); + track.name = trackName; + track.segments.add(segment); + gpx.tracks.add(track); + } else if (saveType == MeasurementToolFragment.SaveType.ROUTE_POINT) { + if (editingCtx.hasRoute()) { + GPXFile newGpx = editingCtx.exportRouteAsGpx(trackName); + if (newGpx != null) { + gpx = newGpx; + } + } + gpx.addRoutePoints(points); + } + } + Exception res = GPXUtilities.writeGpxFile(outFile, gpx); + gpx.path = outFile.getAbsolutePath(); + savedGpxFile = gpx; + if (showOnMap) { + MeasurementToolFragment.showGpxOnMap(app, gpx, actionType, true); + } + return res; + } else { + GPXFile gpx = gpxFile; + backupFile = FileUtils.backupFile(app, outFile); + String trackName = Algorithms.getFileNameWithoutExtension(outFile); + if (measurementLayer != null) { + if (fragment.isPlanRouteMode()) { + if (saveType == MeasurementToolFragment.SaveType.LINE) { + TrkSegment segment = new TrkSegment(); + if (editingCtx.hasRoute()) { + segment.points.addAll(editingCtx.getRoutePoints()); + } else { + segment.points.addAll(before.points); + segment.points.addAll(after.points); + } + Track track = new Track(); + track.name = trackName; + track.segments.add(segment); + gpx.tracks.add(track); + } else if (saveType == MeasurementToolFragment.SaveType.ROUTE_POINT) { + if (editingCtx.hasRoute()) { + GPXFile newGpx = editingCtx.exportRouteAsGpx(trackName); + if (newGpx != null) { + gpx = newGpx; + } + } + gpx.addRoutePoints(points); + } + } else if (actionType != null) { + GpxData gpxData = editingCtx.getGpxData(); + switch (actionType) { + case ADD_SEGMENT: { + List snappedPoints = new ArrayList<>(); + snappedPoints.addAll(before.points); + snappedPoints.addAll(after.points); + gpx.addTrkSegment(snappedPoints); + break; + } + case ADD_ROUTE_POINTS: { + gpx.replaceRoutePoints(points); + break; + } + case EDIT_SEGMENT: { + if (gpxData != null) { + TrkSegment segment = new TrkSegment(); + segment.points.addAll(points); + gpx.replaceSegment(gpxData.getTrkSegment(), segment); + } + break; + } + case OVERWRITE_SEGMENT: { + if (gpxData != null) { + List snappedPoints = new ArrayList<>(); + snappedPoints.addAll(before.points); + snappedPoints.addAll(after.points); + TrkSegment segment = new TrkSegment(); + segment.points.addAll(snappedPoints); + gpx.replaceSegment(gpxData.getTrkSegment(), segment); + } + break; + } + } + } else { + gpx.addRoutePoints(points); + } + } + Exception res = null; + if (!gpx.showCurrentTrack) { + res = GPXUtilities.writeGpxFile(outFile, gpx); + } + savedGpxFile = gpx; + if (showOnMap) { + MeasurementToolFragment.showGpxOnMap(app, gpx, actionType, false); + } + return res; + } + } + + @Override + protected void onPostExecute(Exception warning) { + MeasurementToolFragment fragment = fragmentRef.get(); + if (fragment != null && progressDialog != null && AndroidUtils.isActivityNotDestroyed(fragment.getActivity())) { + progressDialog.dismiss(); + } + if (saveGpxRouteListener != null) { + saveGpxRouteListener.gpxSavingFinished(warning, savedGpxFile, backupFile); + } + } + + public interface SaveGpxRouteListener { + + void gpxSavingFinished(Exception warning, GPXFile savedGpxFile, File backupFile); + } +} \ No newline at end of file