From 3419175299a0eb4e6fa7e8d08eb0fefc7ae4969b Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Fri, 11 Aug 2017 11:15:14 +0300 Subject: [PATCH] Refactor saving gpx methods --- .../MeasurementToolFragment.java | 123 +++++++----------- 1 file changed, 45 insertions(+), 78 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index a1dd58ef24..9e975f9624 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -496,7 +496,7 @@ public class MeasurementToolFragment extends Fragment { GPXFile gpx = newGpxLine.getGpxFile(); SelectedGpxFile selectedGpxFile = mapActivity.getMyApplication().getSelectedGpxHelper().getSelectedFileByPath(gpx.path); boolean showOnMap = selectedGpxFile != null; - saveGpx(gpx, showOnMap); + saveExistingGpx(gpx, showOnMap); } private void saveAsGpxOnClick(MapActivity mapActivity) { @@ -558,73 +558,22 @@ public class MeasurementToolFragment extends Fragment { fout = new File(dir, fileName); } } - createAndSaveGpx(dir, fileName, showOnMapToggle.isChecked()); + saveNewGpx(dir, fileName, showOnMapToggle.isChecked()); } }) .setNegativeButton(R.string.shared_string_cancel, null) .show(); } - private void saveGpx(final GPXFile gpx, final boolean showOnMap) { - new AsyncTask() { - - private ProgressDialog progressDialog; - private File toSave; - - @Override - protected void onPreExecute() { - MapActivity activity = getMapActivity(); - if (activity != null) { - progressDialog = new ProgressDialog(activity); - progressDialog.setMessage(getString(R.string.saving_gpx_tracks)); - progressDialog.show(); - } - } - - @Override - protected String doInBackground(Void... voids) { - MeasurementToolLayer measurementLayer = getMeasurementLayer(); - toSave = new File(gpx.path); - if (measurementLayer != null) { - List points = measurementLayer.getMeasurementPoints(); - gpx.addTrkSegment(points); - } - MapActivity activity = getMapActivity(); - if (activity != null) { - String res = GPXUtilities.writeGpxFile(toSave, gpx, activity.getMyApplication()); - if (showOnMap) { - SelectedGpxFile sf = activity.getMyApplication().getSelectedGpxHelper().selectGpxFile(gpx, true, false); - if (sf != null) { - sf.processPoints(); - } - } - return res; - } - return null; - } - - @Override - protected void onPostExecute(String warning) { - MapActivity activity = getMapActivity(); - if (activity != null) { - if (warning == null) { - Toast.makeText(activity, - MessageFormat.format(getString(R.string.gpx_saved_sucessfully), toSave.getAbsolutePath()), - Toast.LENGTH_LONG).show(); - saved = true; - } else { - Toast.makeText(activity, warning, Toast.LENGTH_LONG).show(); - } - activity.refreshMap(); - } - if (progressDialog != null && progressDialog.isShowing()) { - progressDialog.dismiss(); - } - } - }.execute(); + private void saveNewGpx(File dir, String fileName, boolean checked) { + saveGpx(dir, fileName, checked, null); } - private void createAndSaveGpx(final File dir, final String fileName, final boolean showOnMap) { + private void saveExistingGpx(GPXFile gpx, boolean showOnMap) { + saveGpx(null, null, showOnMap, gpx); + } + + private void saveGpx(final File dir, final String fileName, final boolean showOnMap, final GPXFile gpx) { new AsyncTask() { private ProgressDialog progressDialog; @@ -642,27 +591,45 @@ public class MeasurementToolFragment extends Fragment { @Override protected String doInBackground(Void... voids) { - toSave = new File(dir, fileName); - GPXFile gpx = new GPXFile(); MeasurementToolLayer measurementLayer = getMeasurementLayer(); - if (measurementLayer != null) { - LinkedList points = measurementLayer.getMeasurementPoints(); - if (points.size() == 1) { - gpx.points.add(points.getFirst()); - } else if (points.size() > 1) { - Route rt = new Route(); - gpx.routes.add(rt); - rt.points.addAll(points); - } - } MapActivity activity = getMapActivity(); - if (activity != null) { - String res = GPXUtilities.writeGpxFile(toSave, gpx, activity.getMyApplication()); - gpx.path = toSave.getAbsolutePath(); - if (showOnMap) { - activity.getMyApplication().getSelectedGpxHelper().selectGpxFile(gpx, true, false); + if (gpx == null) { + toSave = new File(dir, fileName); + GPXFile gpx = new GPXFile(); + if (measurementLayer != null) { + LinkedList points = measurementLayer.getMeasurementPoints(); + if (points.size() == 1) { + gpx.points.add(points.getFirst()); + } else if (points.size() > 1) { + Route rt = new Route(); + gpx.routes.add(rt); + rt.points.addAll(points); + } + } + if (activity != null) { + String res = GPXUtilities.writeGpxFile(toSave, gpx, activity.getMyApplication()); + gpx.path = toSave.getAbsolutePath(); + if (showOnMap) { + activity.getMyApplication().getSelectedGpxHelper().selectGpxFile(gpx, true, false); + } + return res; + } + } else { + toSave = new File(gpx.path); + if (measurementLayer != null) { + List points = measurementLayer.getMeasurementPoints(); + gpx.addTrkSegment(points); + } + if (activity != null) { + String res = GPXUtilities.writeGpxFile(toSave, gpx, activity.getMyApplication()); + if (showOnMap) { + SelectedGpxFile sf = activity.getMyApplication().getSelectedGpxHelper().selectGpxFile(gpx, true, false); + if (sf != null) { + sf.processPoints(); + } + } + return res; } - return res; } return null; }