Fix Sharing a route from the Route Info screen shares useless file #2925

This commit is contained in:
Victor Shcherb 2016-08-12 01:38:46 +02:00
parent 23ae52d6f9
commit 07a73c38bd
3 changed files with 154 additions and 86 deletions

View file

@ -15,6 +15,8 @@ import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.support.v7.view.ActionMode; import android.support.v7.view.ActionMode;
import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -30,7 +32,6 @@ import android.widget.ImageView;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import net.osmand.data.FavouritePoint; import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.PointDescription; import net.osmand.data.PointDescription;
@ -43,11 +44,13 @@ import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.TargetPointsHelper; import net.osmand.plus.TargetPointsHelper;
import net.osmand.plus.activities.ShowRouteInfoActivity.RouteInfoAdapter;
import net.osmand.plus.base.FavoriteImageDrawable; import net.osmand.plus.base.FavoriteImageDrawable;
import net.osmand.plus.base.OsmandExpandableListFragment; import net.osmand.plus.base.OsmandExpandableListFragment;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.helpers.ColorDialogs; import net.osmand.plus.helpers.ColorDialogs;
import net.osmand.plus.myplaces.FavoritesActivity; import net.osmand.plus.myplaces.FavoritesActivity;
import net.osmand.plus.routing.RouteDirectionInfo;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils; import net.osmand.util.MapUtils;
@ -473,6 +476,26 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment {
b.show(); b.show();
} }
} }
private StringBuilder generateHtmlPrint() {
StringBuilder html = new StringBuilder();
html.append("<h1>My Favorites</h1>");
List<FavoriteGroup> groups = getMyApplication().getFavorites().getFavoriteGroups();
for(FavoriteGroup group : groups) {
html.append("<h3>"+group.name+"</h3>");
for(FavouritePoint fp : group.points) {
String url = "geo:"+((float)fp.getLatitude())+","+((float)fp.getLongitude())+"?m="+fp.getName();
html.append("<p>" + fp.getName() + " - " + "<a href=\"" + url + "\">geo:"
+ ((float) fp.getLatitude()) + "," + ((float) fp.getLongitude()) + "</a><br>");
if(!Algorithms.isEmpty(fp.getDescription())) {
html.append(": " + fp.getDescription());
}
html.append("</p>");
}
}
return html;
}
private void shareFavourites() { private void shareFavourites() {
@ -503,7 +526,7 @@ public class FavoritesTreeFragment extends OsmandExpandableListFragment {
Algorithms.fileCopy(src, dst); Algorithms.fileCopy(src, dst);
final Intent sendIntent = new Intent(); final Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND); sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Content of the attached file Favourites.gpx:\n\n\n" + GPXUtilities.asString(gpxFile, getMyApplication())); sendIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(generateHtmlPrint().toString()));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_fav_subject)); sendIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_fav_subject));
sendIntent.putExtra(Intent.EXTRA_STREAM, sendIntent.putExtra(Intent.EXTRA_STREAM,
FileProvider.getUriForFile(getActivity(), FileProvider.getUriForFile(getActivity(),

View file

@ -8,6 +8,7 @@ package net.osmand.plus.activities;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -19,16 +20,18 @@ import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.mapcontextmenu.other.MapRouteInfoMenu;
import net.osmand.plus.routing.RouteDirectionInfo; import net.osmand.plus.routing.RouteDirectionInfo;
import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.RoutingHelper;
import net.osmand.plus.views.TurnPathHelper; import net.osmand.plus.views.TurnPathHelper;
import net.osmand.plus.mapcontextmenu.other.MapRouteInfoMenu;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.content.FileProvider;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.text.Html;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -89,22 +92,41 @@ public class ShowRouteInfoActivity extends OsmandListActivity {
MapActivityActions.createSaveDirections(ShowRouteInfoActivity.this, helper).show(); MapActivityActions.createSaveDirections(ShowRouteInfoActivity.this, helper).show();
return true; return true;
} }
if (item.getItemId() == SHARE) { if (item.getItemId() == SHARE) {
final GPXFile gpx = helper.generateGPXFileWithRoute(); final GPXFile gpx = helper.generateGPXFileWithRoute();
final Uri fileUri = Uri.fromFile(new File(gpx.path));
final Intent sendIntent = new Intent(); File dir = new File(ShowRouteInfoActivity.this.getCacheDir(), "share");
sendIntent.setAction(Intent.ACTION_SEND); if (!dir.exists()) {
sendIntent.putExtra(Intent.EXTRA_TEXT, GPXUtilities.asString(gpx, getMyApplication())); dir.mkdir();
sendIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_route_subject)); }
sendIntent.setType("application/gpx+xml"); File dst = new File(dir, "route.gpx");
startActivity(sendIntent); try {
return true; FileWriter fw = new FileWriter(dst);
} GPXUtilities.writeGpx(fw, gpx, getMyApplication());
if (item.getItemId() == PRINT) { fw.close();
print(); final Intent sendIntent = new Intent();
return true; sendIntent.setAction(Intent.ACTION_SEND);
} sendIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(generateHtml(((RouteInfoAdapter)getListAdapter()),
helper.getGeneralRouteInformation()).toString()));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.share_route_subject));
sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
sendIntent.putExtra(
Intent.EXTRA_STREAM,
FileProvider.getUriForFile(ShowRouteInfoActivity.this,
ShowRouteInfoActivity.this.getPackageName() + ".fileprovider", dst));
sendIntent.setType("text/plain");
startActivity(sendIntent);
} catch (IOException e) {
// Toast.makeText(getActivity(), "Error sharing favorites: " + e.getMessage(),
// Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return true;
}
if (item.getItemId() == PRINT) {
print();
return true;
}
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@ -254,74 +276,9 @@ public class ShowRouteInfoActivity extends OsmandListActivity {
} }
final String FILE_NAME = "route_info.html"; final String FILE_NAME = "route_info.html";
StringBuilder html = new StringBuilder(); StringBuilder html = generateHtmlPrint(routeInfo, title);
html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">");
html.append("<head>");
html.append("<title>Route info</title>");
html.append("<meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />");
html.append("<style>");
html.append("table, th, td {");
html.append("border: 1px solid black;");
html.append("border-collapse: collapse;}");
html.append("th, td {");
html.append("padding: 5px;}");
html.append("</style>");
html.append("</head>");
html.append("<body>");
FileOutputStream fos = null; FileOutputStream fos = null;
try { try {
if (!TextUtils.isEmpty(title)) {
html.append("<h1>");
html.append(title);
html.append("</h1>");
}
html.append("<table style=\"width:100%\">");
final String NBSP = "&nbsp;";
final String BR = "<br>";
for (int i = 0; i < routeInfo.getCount(); i++) {
RouteDirectionInfo routeDirectionInfo = (RouteDirectionInfo) routeInfo
.getItem(i);
html.append("<tr>");
StringBuilder sb = new StringBuilder();
sb.append(OsmAndFormatter.getFormattedDistance(
routeDirectionInfo.distance, getMyApplication()));
sb.append(", ");
sb.append(getTimeDescription(routeDirectionInfo));
String distance = sb.toString().replaceAll("\\s", NBSP);
html.append("<td>");
html.append(distance);
html.append("</td>");
String description = routeDirectionInfo
.getDescriptionRoutePart();
html.append("<td>");
html.append(String.valueOf(i+1) + ". " + description);
html.append("</td>");
RouteInfoAdapter.CumulativeInfo cumulativeInfo = routeInfo
.getRouteDirectionCumulativeInfo(i);
html.append("<td>");
sb = new StringBuilder();
sb.append(OsmAndFormatter.getFormattedDistance(
cumulativeInfo.distance, getMyApplication()));
sb.append(" - ");
sb.append(OsmAndFormatter.getFormattedDistance(
cumulativeInfo.distance + routeDirectionInfo.distance,
getMyApplication()));
sb.append(BR);
sb.append(Algorithms.formatDuration(cumulativeInfo.time, getMyApplication().accessibilityEnabled()));
sb.append(" - ");
sb.append(Algorithms.formatDuration(cumulativeInfo.time
+ routeDirectionInfo.getExpectedTime(), getMyApplication().accessibilityEnabled()));
String cumulativeTimeAndDistance = sb.toString().replaceAll("\\s", NBSP);
html.append(cumulativeTimeAndDistance);
html.append("</td>");
html.append("</tr>");
}
html.append("</table>");
html.append("</body>");
html.append("</html>");
file = ((OsmandApplication) getApplication()).getAppPath(FILE_NAME); file = ((OsmandApplication) getApplication()).getAppPath(FILE_NAME);
fos = new FileOutputStream(file); fos = new FileOutputStream(file);
fos.write(html.toString().getBytes("UTF-8")); fos.write(html.toString().getBytes("UTF-8"));
@ -342,5 +299,92 @@ public class ShowRouteInfoActivity extends OsmandListActivity {
return file; return file;
} }
private StringBuilder generateHtml(RouteInfoAdapter routeInfo, String title) {
StringBuilder html = new StringBuilder();
if (!TextUtils.isEmpty(title)) {
html.append("<h1>");
html.append(title);
html.append("</h1>");
}
final String NBSP = "&nbsp;";
final String BR = "<br>";
for (int i = 0; i < routeInfo.getCount(); i++) {
RouteDirectionInfo routeDirectionInfo = (RouteDirectionInfo) routeInfo.getItem(i);
StringBuilder sb = new StringBuilder();
sb.append(OsmAndFormatter.getFormattedDistance(routeDirectionInfo.distance, getMyApplication()));
sb.append(", ").append(NBSP);
sb.append(getTimeDescription(routeDirectionInfo));
String distance = sb.toString().replaceAll("\\s", NBSP);
String description = routeDirectionInfo.getDescriptionRoutePart();
html.append(BR);
html.append("<p>" + String.valueOf(i + 1) + ". " + NBSP + description + NBSP + "(" + distance + ")</p>");
}
return html;
}
private StringBuilder generateHtmlPrint(RouteInfoAdapter routeInfo, String title) {
StringBuilder html = new StringBuilder();
html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">");
html.append("<head>");
html.append("<title>Route info</title>");
html.append("<meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />");
html.append("<style>");
html.append("table, th, td {");
html.append("border: 1px solid black;");
html.append("border-collapse: collapse;}");
html.append("th, td {");
html.append("padding: 5px;}");
html.append("</style>");
html.append("</head>");
html.append("<body>");
if (!TextUtils.isEmpty(title)) {
html.append("<h1>");
html.append(title);
html.append("</h1>");
}
html.append("<table style=\"width:100%\">");
final String NBSP = "&nbsp;";
final String BR = "<br>";
for (int i = 0; i < routeInfo.getCount(); i++) {
RouteDirectionInfo routeDirectionInfo = (RouteDirectionInfo) routeInfo.getItem(i);
html.append("<tr>");
StringBuilder sb = new StringBuilder();
sb.append(OsmAndFormatter.getFormattedDistance(routeDirectionInfo.distance, getMyApplication()));
sb.append(", ");
sb.append(getTimeDescription(routeDirectionInfo));
String distance = sb.toString().replaceAll("\\s", NBSP);
html.append("<td>");
html.append(distance);
html.append("</td>");
String description = routeDirectionInfo.getDescriptionRoutePart();
html.append("<td>");
html.append(String.valueOf(i + 1) + ". " + description);
html.append("</td>");
RouteInfoAdapter.CumulativeInfo cumulativeInfo = routeInfo.getRouteDirectionCumulativeInfo(i);
html.append("<td>");
sb = new StringBuilder();
sb.append(OsmAndFormatter.getFormattedDistance(cumulativeInfo.distance, getMyApplication()));
sb.append(" - ");
sb.append(OsmAndFormatter.getFormattedDistance(cumulativeInfo.distance + routeDirectionInfo.distance,
getMyApplication()));
sb.append(BR);
sb.append(Algorithms.formatDuration(cumulativeInfo.time, getMyApplication().accessibilityEnabled()));
sb.append(" - ");
sb.append(Algorithms.formatDuration(cumulativeInfo.time + routeDirectionInfo.getExpectedTime(),
getMyApplication().accessibilityEnabled()));
String cumulativeTimeAndDistance = sb.toString().replaceAll("\\s", NBSP);
html.append(cumulativeTimeAndDistance);
html.append("</td>");
html.append("</tr>");
}
html.append("</table>");
html.append("</body>");
html.append("</html>");
return html;
}
} }

View file

@ -820,7 +820,8 @@ public class RouteCalculationResult {
// if(p == null || !i.getTurnType().isSkipToSpeak() || // if(p == null || !i.getTurnType().isSkipToSpeak() ||
// (!Algorithms.objectEquals(p.getRef(), i.getRef()) && // (!Algorithms.objectEquals(p.getRef(), i.getRef()) &&
// !Algorithms.objectEquals(p.getStreetName(), i.getStreetName()))) { // !Algorithms.objectEquals(p.getStreetName(), i.getStreetName()))) {
if(i.getTurnType() != null && !i.getTurnType().isSkipToSpeak()) { if(p == null ||
(i.getTurnType() != null && !i.getTurnType().isSkipToSpeak())) {
p = new RouteDirectionInfo(i.getAverageSpeed(), i.getTurnType()); p = new RouteDirectionInfo(i.getAverageSpeed(), i.getTurnType());
p.routePointOffset = i.routePointOffset; p.routePointOffset = i.routePointOffset;
p.routeEndPointOffset = i.routeEndPointOffset; p.routeEndPointOffset = i.routeEndPointOffset;