Merge pull request #618 from krabaey/feature-share
Share calculated routes to external apps
This commit is contained in:
commit
a0ca47d5e8
3 changed files with 61 additions and 12 deletions
|
@ -815,6 +815,8 @@ Afghanistan, Albania, Algeria, Andorra, Angola, Anguilla, Antigua and Barbuda, A
|
|||
<string name="gpxup_private">Private</string>
|
||||
<string name="asap">ASAP</string>
|
||||
<string name="save_route_as_gpx">Save route as GPX file</string>
|
||||
<string name="share_route_as_gpx">Share route as GPX file</string>
|
||||
<string name="share_route_subject">Route shared via OsmAnd</string>
|
||||
<string name="route_roundabout">Roundabout: take %1$d exit and go</string>
|
||||
<string name="route_kl">Keep left and go</string>
|
||||
<string name="route_kr">Keep right and go</string>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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, helper).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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue