From 961389a5c9c9872375f0e87538f9f43ae6ff19dc Mon Sep 17 00:00:00 2001 From: Koen Rabaey Date: Tue, 29 Apr 2014 21:42:48 +0200 Subject: [PATCH] Share calculated routes --- OsmAnd/res/values/strings.xml | 2 + OsmAnd/src/net/osmand/plus/GPXUtilities.java | 52 ++++++++++++++----- .../activities/ShowRouteInfoActivity.java | 19 +++++++ 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 85c498b469..a984a43468 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -815,6 +815,8 @@ Afghanistan, Albania, Algeria, Andorra, Angola, Anguilla, Antigua and Barbuda, A Private ASAP Save route as GPX file + Share route as GPX file + Route shared via OsmAnd Roundabout: take %1$d exit and go Keep left and go Keep right and go diff --git a/OsmAnd/src/net/osmand/plus/GPXUtilities.java b/OsmAnd/src/net/osmand/plus/GPXUtilities.java index c4ea6d0bb7..03eb594513 100644 --- a/OsmAnd/src/net/osmand/plus/GPXUtilities.java +++ b/OsmAnd/src/net/osmand/plus/GPXUtilities.java @@ -8,7 +8,10 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.OutputStreamWriter; import java.io.Reader; +import java.io.StringWriter; +import java.io.Writer; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.text.NumberFormat; @@ -175,14 +178,47 @@ public class GPXUtilities { } - public static String writeGpxFile(File fout, GPXFile file, OsmandApplication ctx) { - FileOutputStream output = null; + public static String asString(GPXFile file, OsmandApplication ctx) { + final Writer writer = new StringWriter(); + GPXUtilities.writeGpx(writer, file, ctx); + return writer.toString(); + } + + public static String writeGpxFile(File fout, GPXFile file, OsmandApplication ctx) + { + Writer output = null; + try + { + output = new OutputStreamWriter(new FileOutputStream(fout), "UTF-8"); //$NON-NLS-1$ + return writeGpx(output, file, ctx); + } + catch (IOException e) + { + log.error("Error saving gpx", e); //$NON-NLS-1$ + return ctx.getString(R.string.error_occurred_saving_gpx); + } + finally + { + if (output != null) + { + try + { + output.close(); + } + catch (IOException ignore) + { + // ignore + } + } + } + } + + public static String writeGpx(Writer output, GPXFile file, OsmandApplication ctx) { try { SimpleDateFormat format = new SimpleDateFormat(GPX_TIME_FORMAT); format.setTimeZone(TimeZone.getTimeZone("UTC")); - output = new FileOutputStream(fout); XmlSerializer serializer = PlatformUtil.newSerializer(); - serializer.setOutput(output, "UTF-8"); //$NON-NLS-1$ + serializer.setOutput(output); serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); //$NON-NLS-1$ serializer.startDocument("UTF-8", true); //$NON-NLS-1$ serializer.startTag(null, "gpx"); //$NON-NLS-1$ @@ -243,14 +279,6 @@ public class GPXUtilities { } catch (IOException e) { log.error("Error saving gpx", e); //$NON-NLS-1$ return ctx.getString(R.string.error_occurred_saving_gpx); - } finally { - if (output != null) { - try { - output.close(); - } catch (IOException ignore) { - // ignore - } - } } return null; } diff --git a/OsmAnd/src/net/osmand/plus/activities/ShowRouteInfoActivity.java b/OsmAnd/src/net/osmand/plus/activities/ShowRouteInfoActivity.java index deee5540d4..ff22514b7e 100644 --- a/OsmAnd/src/net/osmand/plus/activities/ShowRouteInfoActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/ShowRouteInfoActivity.java @@ -8,7 +8,10 @@ package net.osmand.plus.activities; import java.util.List; +import android.content.Intent; import net.osmand.Location; +import net.osmand.plus.GPXUtilities; +import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; @@ -37,6 +40,7 @@ public class ShowRouteInfoActivity extends OsmandListActivity { private static final int SAVE = 0; + private static final int SHARE = 1; private RoutingHelper helper; private TextView header; private DisplayMetrics dm; @@ -61,6 +65,18 @@ public class ShowRouteInfoActivity extends OsmandListActivity { MapActivityActions.createSaveDirections(ShowRouteInfoActivity.this).show(); return true; } + if (item.getItemId() == SHARE) { + final GPXFile gpx = getMyApplication().getRoutingHelper().generateGPXFileWithRoute(); + + final Intent sendIntent = new Intent(); + sendIntent.setAction(Intent.ACTION_SEND); + sendIntent.putExtra(Intent.EXTRA_TEXT, GPXUtilities.asString(gpx, getMyApplication())); + sendIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_route_subject)); + sendIntent.setType("application/gpx+xml"); + startActivity(sendIntent); + return true; + } + return super.onOptionsItemSelected(item); } @@ -99,6 +115,9 @@ public class ShowRouteInfoActivity extends OsmandListActivity { createMenuItem(menu, SAVE, R.string.save_route_as_gpx, R.drawable.ic_action_gsave_light, R.drawable.ic_action_gsave_dark, MenuItem.SHOW_AS_ACTION_ALWAYS); + createMenuItem(menu, SHARE, R.string.share_route_as_gpx, + R.drawable.ic_action_gshare_light, R.drawable.ic_action_gshare_dark, + MenuItem.SHOW_AS_ACTION_ALWAYS); return super.onCreateOptionsMenu(menu); }