From bbb557b67defe904fd72ab03d8bed7c80615040f Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 23 Jul 2020 16:08:34 +0300 Subject: [PATCH] Share gpx file from context menu --- .../SelectedGpxMenuController.java | 49 +++++++++++ .../plus/myplaces/SaveCurrentTrackTask.java | 70 ++++++++++++++++ .../plus/myplaces/SaveGpxAsyncTask.java | 49 ----------- .../plus/myplaces/TrackPointFragment.java | 84 +++++-------------- .../osmand/plus/views/MapControlsLayer.java | 2 +- 5 files changed, 141 insertions(+), 113 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/myplaces/SaveCurrentTrackTask.java delete mode 100644 OsmAnd/src/net/osmand/plus/myplaces/SaveGpxAsyncTask.java diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/SelectedGpxMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/SelectedGpxMenuController.java index b31f24be46..7cb90af938 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/SelectedGpxMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/SelectedGpxMenuController.java @@ -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; diff --git a/OsmAnd/src/net/osmand/plus/myplaces/SaveCurrentTrackTask.java b/OsmAnd/src/net/osmand/plus/myplaces/SaveCurrentTrackTask.java new file mode 100644 index 0000000000..84959d17f7 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/myplaces/SaveCurrentTrackTask.java @@ -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 { + + 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 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 = ""; + } + } + } +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/myplaces/SaveGpxAsyncTask.java b/OsmAnd/src/net/osmand/plus/myplaces/SaveGpxAsyncTask.java deleted file mode 100644 index a3e2ab051d..0000000000 --- a/OsmAnd/src/net/osmand/plus/myplaces/SaveGpxAsyncTask.java +++ /dev/null @@ -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 { - - 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); - } -} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/myplaces/TrackPointFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/TrackPointFragment.java index 5f206edabb..5cbd84e9b3 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/TrackPointFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/TrackPointFragment.java @@ -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 { - private final GPXFile gpx; - private final OsmandApplication app; - private final WeakReference 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 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 { private OsmandApplication app; diff --git a/OsmAnd/src/net/osmand/plus/views/MapControlsLayer.java b/OsmAnd/src/net/osmand/plus/views/MapControlsLayer.java index e685bec671..7ba473c4ee 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapControlsLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapControlsLayer.java @@ -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();