Add ability to save GPX

This commit is contained in:
Alexander Sytnyk 2017-08-03 14:55:28 +03:00
parent f0a1e92b13
commit f88ba653c4
2 changed files with 64 additions and 9 deletions

View file

@ -7,6 +7,7 @@ import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SwitchCompat;
import android.text.Editable; import android.text.Editable;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
@ -23,6 +24,9 @@ import android.widget.Toast;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.GPXUtilities.Route;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.IconsCache; import net.osmand.plus.IconsCache;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
@ -32,10 +36,14 @@ import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControll
import net.osmand.plus.widgets.IconPopupMenu; import net.osmand.plus.widgets.IconPopupMenu;
import java.io.File; import java.io.File;
import java.text.MessageFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.LinkedList;
import java.util.Locale; import java.util.Locale;
import static net.osmand.plus.GPXUtilities.GPXFile;
public class MeasurementToolFragment extends Fragment { public class MeasurementToolFragment extends Fragment {
public static final String TAG = "MeasurementToolFragment"; public static final String TAG = "MeasurementToolFragment";
@ -148,7 +156,12 @@ public class MeasurementToolFragment extends Fragment {
public boolean onMenuItemClick(MenuItem menuItem) { public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) { switch (menuItem.getItemId()) {
case R.id.action_save_as_gpx: case R.id.action_save_as_gpx:
if (measurementLayer.getPointsCount() > 0) {
saveAsGpxOnClick(mapActivity); saveAsGpxOnClick(mapActivity);
} else {
//todo
Toast.makeText(mapActivity, "There must be at least one point", Toast.LENGTH_SHORT).show();
}
return true; return true;
case R.id.action_clear_all: case R.id.action_clear_all:
measurementLayer.clearPoints(); measurementLayer.clearPoints();
@ -172,10 +185,11 @@ public class MeasurementToolFragment extends Fragment {
final File dir = mapActivity.getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR); final File dir = mapActivity.getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR);
final LayoutInflater inflater = getLayoutInflater(); final LayoutInflater inflater = getLayoutInflater();
final View view = inflater.inflate(R.layout.save_gpx_dialog, null); final View view = inflater.inflate(R.layout.save_gpx_dialog, null);
final EditText nameEt = view.findViewById(R.id.gpx_name_et); final EditText nameEt = (EditText) view.findViewById(R.id.gpx_name_et);
final TextView fileExistsTv = view.findViewById(R.id.file_exists_text_view); final TextView fileExistsTv = (TextView) view.findViewById(R.id.file_exists_text_view);
final SwitchCompat showOnMapToggle = (SwitchCompat) view.findViewById(R.id.toggle_show_on_map);
final String suggestedName = new SimpleDateFormat("dd-M-yyyy hh:mm", Locale.US).format(new Date()); final String suggestedName = new SimpleDateFormat("yyyy-M-dd hh-mm E", Locale.US).format(new Date());
String displayedName = String.copyValueOf(suggestedName.toCharArray()); String displayedName = String.copyValueOf(suggestedName.toCharArray());
File fout = new File(dir, suggestedName + EXT); File fout = new File(dir, suggestedName + EXT);
int ind = 1; int ind = 1;
@ -225,14 +239,14 @@ public class MeasurementToolFragment extends Fragment {
fout = new File(dir, fileName); fout = new File(dir, fileName);
} }
} }
saveGpx(fileName); saveGpx(dir, fileName, showOnMapToggle.isChecked());
} }
}) })
.setNegativeButton(R.string.shared_string_cancel, null) .setNegativeButton(R.string.shared_string_cancel, null)
.show(); .show();
} }
private void saveGpx(final String fileName) { private void saveGpx(final File dir, final String fileName, final boolean showOnMap) {
new AsyncTask<Void, Void, String>() { new AsyncTask<Void, Void, String>() {
private ProgressDialog progressDialog; private ProgressDialog progressDialog;
@ -240,17 +254,54 @@ public class MeasurementToolFragment extends Fragment {
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
super.onPreExecute(); MapActivity activity = getMapActivity();
if (activity != null) {
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage(activity.getString(R.string.saving_gpx_tracks));
progressDialog.show();
}
} }
@Override @Override
protected String doInBackground(Void... voids) { protected String doInBackground(Void... voids) {
toSave = new File(dir, fileName);
GPXFile gpx = new GPXFile();
MeasurementToolLayer measurementLayer = getMeasurementLayer();
if (measurementLayer != null) {
LinkedList<WptPt> 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) {
// todo
String res = GPXUtilities.writeGpxFile(toSave, gpx, activity.getMyApplication());
activity.getMyApplication().getSelectedGpxHelper().selectGpxFile(gpx, showOnMap, false);
return res;
}
return null; return null;
} }
@Override @Override
protected void onPostExecute(String s) { protected void onPostExecute(String warning) {
super.onPostExecute(s); MapActivity activity = getMapActivity();
if (activity != null) {
if (warning == null) {
Toast.makeText(activity,
MessageFormat.format(activity.getString(R.string.gpx_saved_sucessfully), toSave.getAbsolutePath()),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(activity, warning, Toast.LENGTH_LONG).show();
}
}
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
} }
}.execute(); }.execute();
} }

View file

@ -66,6 +66,10 @@ public class MeasurementToolLayer extends OsmandMapLayer {
return measurementPoints.size(); return measurementPoints.size();
} }
public LinkedList<WptPt> getMeasurementPoints() {
return measurementPoints;
}
String getDistanceSt() { String getDistanceSt() {
float dist = 0; float dist = 0;
if (measurementPoints.size() > 0) { if (measurementPoints.size() > 0) {