Share gpx file from context menu

This commit is contained in:
Vitaliy 2020-07-23 16:08:34 +03:00
parent 2181731c7b
commit bbb557b67d
5 changed files with 141 additions and 113 deletions

View file

@ -1,15 +1,19 @@
package net.osmand.plus.mapcontextmenu.controllers;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
import net.osmand.AndroidUtils;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
@ -20,7 +24,10 @@ import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.helpers.GpxUiHelper;
import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.mapcontextmenu.builders.SelectedGpxMenuBuilder;
import net.osmand.plus.myplaces.SaveCurrentTrackTask;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.track.SaveGpxAsyncTask.SaveGpxListener;
import net.osmand.util.Algorithms;
import java.io.File;
import java.lang.ref.WeakReference;
@ -191,6 +198,48 @@ public class SelectedGpxMenuController extends MenuController {
return getIcon(R.drawable.ic_action_polygom_dark, color);
}
@Override
public void share(LatLon latLon, String title, String address) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null && selectedGpxPoint != null) {
final GPXFile gpxFile = selectedGpxPoint.getSelectedGpxFile().getGpxFile();
if (gpxFile != null) {
if (Algorithms.isEmpty(gpxFile.path)) {
SaveGpxListener saveGpxListener = new SaveGpxListener() {
@Override
public void gpxSavingStarted() {
}
@Override
public void gpxSavingFinished(Exception errorMessage) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
shareGpx(mapActivity, gpxFile.path);
}
}
};
new SaveCurrentTrackTask(mapActivity.getMyApplication(), gpxFile, saveGpxListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
shareGpx(mapActivity, gpxFile.path);
}
}
} else {
super.share(latLon, title, "");
}
}
private void shareGpx(@NonNull Context context, @NonNull String path) {
final Uri fileUri = AndroidUtils.getUriForFile(context, new File(path));
final Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
sendIntent.setType("application/gpx+xml");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (AndroidUtils.isIntentSafe(context, sendIntent)) {
context.startActivity(sendIntent);
}
}
public static class SelectedGpxPoint {
private final WptPt selectedPoint;

View file

@ -0,0 +1,70 @@
package net.osmand.plus.myplaces;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.IndexConstants;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.track.SaveGpxAsyncTask.SaveGpxListener;
import java.io.File;
import java.util.Map;
public class SaveCurrentTrackTask extends AsyncTask<Void, Void, Boolean> {
private final OsmandApplication app;
private final GPXFile gpx;
private final SaveGpxListener saveGpxListener;
public SaveCurrentTrackTask(@NonNull OsmandApplication app, @NonNull GPXFile gpx, @NonNull SaveGpxListener listener) {
this.app = app;
this.gpx = gpx;
saveGpxListener = listener;
}
@Override
protected void onPreExecute() {
if (saveGpxListener != null) {
saveGpxListener.gpxSavingStarted();
}
}
@Override
protected Boolean doInBackground(Void... params) {
SavingTrackHelper savingTrackHelper = app.getSavingTrackHelper();
Map<String, GPXUtilities.GPXFile> files = savingTrackHelper.collectRecordedData();
File dir;
boolean shouldClearPath = false;
if (gpx.path.isEmpty()) {
dir = app.getCacheDir();
shouldClearPath = true;
} else {
dir = app.getAppCustomization().getTracksDir();
}
if (!dir.exists()) {
dir.mkdir();
}
for (final String f : files.keySet()) {
File fout = new File(dir, f + IndexConstants.GPX_FILE_EXT);
GPXUtilities.writeGpxFile(fout, gpx);
}
return shouldClearPath;
}
@Override
protected void onPostExecute(Boolean shouldClearPath) {
if (gpx != null) {
if (saveGpxListener != null) {
saveGpxListener.gpxSavingFinished(null);
}
if (shouldClearPath) {
gpx.path = "";
}
}
}
}

View file

@ -1,49 +0,0 @@
package net.osmand.plus.myplaces;
import android.os.AsyncTask;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import java.io.File;
public class SaveGpxAsyncTask extends AsyncTask<Void, Void, Exception> {
private final GPXFile gpx;
private final SaveGpxListener saveGpxListener;
public SaveGpxAsyncTask(@NonNull GPXFile gpx,
@Nullable SaveGpxListener saveGpxListener) {
this.gpx = gpx;
this.saveGpxListener = saveGpxListener;
}
@Override
protected void onPreExecute() {
if (saveGpxListener != null) {
saveGpxListener.gpxSavingStarted();
}
}
@Override
protected Exception doInBackground(Void... params) {
return GPXUtilities.writeGpxFile(new File(gpx.path), gpx);
}
@Override
protected void onPostExecute(Exception errorMessage) {
if (saveGpxListener != null) {
saveGpxListener.gpxSavingFinished(errorMessage);
}
}
public interface SaveGpxListener {
void gpxSavingStarted();
void gpxSavingFinished(Exception errorMessage);
}
}

View file

@ -46,7 +46,6 @@ import net.osmand.Collator;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.WptPt;
import net.osmand.IndexConstants;
import net.osmand.OsmAndCollator;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
@ -59,7 +58,6 @@ import net.osmand.plus.GpxSelectionHelper.GpxDisplayItemType;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarkersGroup;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity;
@ -67,10 +65,12 @@ import net.osmand.plus.activities.OsmandActionBarActivity;
import net.osmand.plus.activities.OsmandBaseExpandableListAdapter;
import net.osmand.plus.activities.SavingTrackHelper;
import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.base.PointImageDrawable;
import net.osmand.plus.base.OsmandExpandableListFragment;
import net.osmand.plus.base.PointImageDrawable;
import net.osmand.plus.mapmarkers.CoordinateInputDialogFragment;
import net.osmand.plus.myplaces.TrackBitmapDrawer.TrackBitmapDrawerListener;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.track.SaveGpxAsyncTask.SaveGpxListener;
import net.osmand.plus.widgets.TextViewEx;
import net.osmand.util.Algorithms;
@ -303,10 +303,24 @@ public class TrackPointFragment extends OsmandExpandableListFragment implements
}
private void shareItems() {
GPXFile gpxFile = getGpx();
if (gpxFile != null) {
if (gpxFile.path.isEmpty() && getTrackActivity() != null) {
new SaveAndShareTask(this, gpxFile).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
final GPXFile gpxFile = getGpx();
if (gpxFile != null && getTrackActivity() != null) {
if (Algorithms.isEmpty(gpxFile.path)) {
SaveGpxListener saveGpxListener = new SaveGpxListener() {
@Override
public void gpxSavingStarted() {
showProgressBar();
}
@Override
public void gpxSavingFinished(Exception errorMessage) {
if (isResumed()) {
hideProgressBar();
shareGpx(gpxFile.path);
}
}
};
new SaveCurrentTrackTask(app, gpxFile, saveGpxListener).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
shareGpx(gpxFile.path);
}
@ -1235,62 +1249,6 @@ public class TrackPointFragment extends OsmandExpandableListFragment implements
}
}
public static class SaveAndShareTask extends AsyncTask<Void, Void, Boolean> {
private final GPXFile gpx;
private final OsmandApplication app;
private final WeakReference<TrackPointFragment> fragmentRef;
SaveAndShareTask(@NonNull TrackPointFragment fragment, @NonNull GPXFile gpx) {
this.gpx = gpx;
fragmentRef = new WeakReference<>(fragment);
app = fragment.getMyApplication();
}
@Override
protected void onPreExecute() {
TrackPointFragment fragment = fragmentRef.get();
if (fragment != null) {
fragment.showProgressBar();
}
}
@Override
protected Boolean doInBackground(Void... params) {
SavingTrackHelper savingTrackHelper = app.getSavingTrackHelper();
Map<String, GPXFile> files = savingTrackHelper.collectRecordedData();
File dir;
boolean shouldClearPath = false;
if (gpx.path.isEmpty()) {
dir = app.getCacheDir();
shouldClearPath = true;
} else {
dir = app.getAppCustomization().getTracksDir();
}
if (!dir.exists()) {
dir.mkdir();
}
for (final String f : files.keySet()) {
File fout = new File(dir, f + IndexConstants.GPX_FILE_EXT);
GPXUtilities.writeGpxFile(fout, gpx);
}
return shouldClearPath;
}
@Override
protected void onPostExecute(Boolean shouldClearPath) {
TrackPointFragment fragment = fragmentRef.get();
if (gpx != null) {
if (fragment != null && fragment.isResumed()) {
fragment.hideProgressBar();
fragment.shareGpx(gpx.path);
}
if (shouldClearPath) {
gpx.path = "";
}
}
}
}
private static class DeletePointsTask extends AsyncTask<Void, Void, Void> {
private OsmandApplication app;

View file

@ -453,7 +453,7 @@ public class MapControlsLayer extends OsmandMapLayer {
RoutingHelper routingHelper = mapActivity.getMyApplication().getRoutingHelper();
Object object = menu.getObject();
if (object instanceof SelectedGpxPoint) {
if (object instanceof SelectedGpxPoint && !((SelectedGpxPoint) object).getSelectedGpxFile().isShowCurrentTrack()) {
GPXFile gpxFile = ((SelectedGpxPoint) object).getSelectedGpxFile().getGpxFile();
mapActivity.getMapActions().enterRoutePlanningModeGivenGpx(gpxFile, null, null, true, true, MenuState.HEADER_ONLY);
routingHelper.recalculateRouteDueToSettingsChange();