From 7417df6f6fdfb5b5817eabeba82318c14fc02138 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Thu, 23 Jul 2020 18:18:49 +0300 Subject: [PATCH] Save gpx file for api navigation --- OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java | 63 +++++++--------- .../plus/helpers/ExternalApiHelper.java | 71 ++++++++++++++----- 2 files changed, 77 insertions(+), 57 deletions(-) diff --git a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java index 8f17f06190..d860838c4f 100644 --- a/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java +++ b/OsmAnd/src/net/osmand/aidl/OsmandAidlApi.java @@ -671,50 +671,35 @@ public class OsmandAidlApi { public void onReceive(Context context, Intent intent) { MapActivity mapActivity = mapActivityRef.get(); if (mapActivity != null) { - boolean force = intent.getBooleanExtra(AIDL_FORCE, false); - GPXFile gpx = null; - if (intent.getStringExtra(AIDL_DATA) != null) { - String gpxStr = intent.getStringExtra(AIDL_DATA); - if (!Algorithms.isEmpty(gpxStr)) { - gpx = GPXUtilities.loadGPXFile(new ByteArrayInputStream(gpxStr.getBytes())); - } - } else if (intent.getParcelableExtra(AIDL_URI) != null) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - Uri gpxUri = intent.getParcelableExtra(AIDL_URI); - - ParcelFileDescriptor gpxParcelDescriptor = null; - try { - gpxParcelDescriptor = mapActivity.getContentResolver().openFileDescriptor(gpxUri, "r"); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - if (gpxParcelDescriptor != null) { - FileDescriptor fileDescriptor = gpxParcelDescriptor.getFileDescriptor(); - gpx = GPXUtilities.loadGPXFile(new FileInputStream(fileDescriptor)); - } - } - } - + GPXFile gpx = loadGpxFileFromIntent(mapActivity, intent); if (gpx != null) { - final RoutingHelper routingHelper = app.getRoutingHelper(); - if (routingHelper.isFollowingMode() && !force) { - final GPXFile gpxFile = gpx; - AlertDialog dlg = mapActivity.getMapActions().stopNavigationActionConfirm(); - dlg.setOnDismissListener(new DialogInterface.OnDismissListener() { + boolean force = intent.getBooleanExtra(AIDL_FORCE, false); + ExternalApiHelper.saveAndNavigateGpx(mapActivity, gpx, force); + } + } + } - @Override - public void onDismiss(DialogInterface dialog) { - MapActivity mapActivity = mapActivityRef.get(); - if (mapActivity != null && !routingHelper.isFollowingMode()) { - ExternalApiHelper.startNavigation(mapActivity, gpxFile); - } - } - }); - } else { - ExternalApiHelper.startNavigation(mapActivity, gpx); + private GPXFile loadGpxFileFromIntent(@NonNull MapActivity mapActivity, @NonNull Intent intent) { + GPXFile gpx = null; + String gpxStr = intent.getStringExtra(AIDL_DATA); + if (!Algorithms.isEmpty(gpxStr)) { + gpx = GPXUtilities.loadGPXFile(new ByteArrayInputStream(gpxStr.getBytes())); + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + Uri gpxUri = intent.getParcelableExtra(AIDL_URI); + if (gpxUri != null) { + ParcelFileDescriptor gpxParcelDescriptor = null; + try { + gpxParcelDescriptor = mapActivity.getContentResolver().openFileDescriptor(gpxUri, "r"); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + if (gpxParcelDescriptor != null) { + FileDescriptor fileDescriptor = gpxParcelDescriptor.getFileDescriptor(); + gpx = GPXUtilities.loadGPXFile(new FileInputStream(fileDescriptor)); } } } + return gpx; } }; registerReceiver(navigateGpxReceiver, mapActivity, AIDL_NAVIGATE_GPX); diff --git a/OsmAnd/src/net/osmand/plus/helpers/ExternalApiHelper.java b/OsmAnd/src/net/osmand/plus/helpers/ExternalApiHelper.java index 4cb07d6543..1d56eb7711 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/ExternalApiHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/ExternalApiHelper.java @@ -5,6 +5,7 @@ import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; +import android.os.AsyncTask; import android.os.Build; import android.os.ParcelFileDescriptor; @@ -15,6 +16,7 @@ import androidx.appcompat.app.AlertDialog; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import net.osmand.AndroidUtils; import net.osmand.GPXUtilities; import net.osmand.GPXUtilities.GPXFile; import net.osmand.IndexConstants; @@ -26,7 +28,6 @@ import net.osmand.aidl.search.SearchParams; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; -import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper.MapMarker; @@ -45,6 +46,9 @@ import net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo; import net.osmand.plus.routing.RouteDirectionInfo; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.search.listitems.QuickSearchListItem; +import net.osmand.plus.settings.backend.ApplicationMode; +import net.osmand.plus.track.SaveGpxAsyncTask; +import net.osmand.plus.track.SaveGpxAsyncTask.SaveGpxListener; import net.osmand.router.TurnType; import net.osmand.search.SearchUICore; import net.osmand.search.core.ObjectType; @@ -202,7 +206,6 @@ public class ExternalApiHelper { if (API_CMD_SHOW_GPX.equals(cmd) || API_CMD_NAVIGATE_GPX.equals(cmd)) { boolean navigate = API_CMD_NAVIGATE_GPX.equals(cmd); String path = uri.getQueryParameter(PARAM_PATH); - boolean force = uri.getBooleanQueryParameter(PARAM_FORCE, false); GPXFile gpx = null; if (path != null) { @@ -240,22 +243,8 @@ public class ExternalApiHelper { if (gpx != null) { if (navigate) { - final RoutingHelper routingHelper = app.getRoutingHelper(); - if (routingHelper.isFollowingMode() && !force) { - final GPXFile gpxFile = gpx; - AlertDialog dlg = mapActivity.getMapActions().stopNavigationActionConfirm(); - dlg.setOnDismissListener(new DialogInterface.OnDismissListener() { - - @Override - public void onDismiss(DialogInterface dialog) { - if (!routingHelper.isFollowingMode()) { - startNavigation(mapActivity, gpxFile); - } - } - }); - } else { - startNavigation(mapActivity, gpx); - } + boolean force = uri.getBooleanQueryParameter(PARAM_FORCE, false); + saveAndNavigateGpx(mapActivity, gpx, force); } else { app.getSelectedGpxHelper().setGpxFileToDisplay(gpx); } @@ -651,6 +640,52 @@ public class ExternalApiHelper { return result; } + public static void saveAndNavigateGpx(MapActivity mapActivity, final GPXFile gpxFile, final boolean force) { + final WeakReference mapActivityRef = new WeakReference<>(mapActivity); + + if (Algorithms.isEmpty(gpxFile.path)) { + OsmandApplication app = mapActivity.getMyApplication(); + String destFileName = "route" + IndexConstants.GPX_FILE_EXT; + File destDir = app.getAppPath(IndexConstants.GPX_IMPORT_DIR); + File destFile = app.getAppPath(IndexConstants.GPX_IMPORT_DIR + destFileName); + while (destFile.exists()) { + destFileName = AndroidUtils.createNewFileName(destFileName); + destFile = new File(destDir, destFileName); + } + gpxFile.path = destFile.getAbsolutePath(); + } + + new SaveGpxAsyncTask(gpxFile, new SaveGpxListener() { + @Override + public void gpxSavingStarted() { + + } + + @Override + public void gpxSavingFinished(Exception errorMessage) { + MapActivity mapActivity = mapActivityRef.get(); + if (errorMessage == null && mapActivity != null && AndroidUtils.isActivityNotDestroyed(mapActivity)) { + final RoutingHelper routingHelper = mapActivity.getMyApplication().getRoutingHelper(); + if (routingHelper.isFollowingMode() && !force) { + AlertDialog dlg = mapActivity.getMapActions().stopNavigationActionConfirm(); + dlg.setOnDismissListener(new DialogInterface.OnDismissListener() { + + @Override + public void onDismiss(DialogInterface dialog) { + MapActivity mapActivity = mapActivityRef.get(); + if (mapActivity != null && !routingHelper.isFollowingMode()) { + ExternalApiHelper.startNavigation(mapActivity, gpxFile); + } + } + }); + } else { + startNavigation(mapActivity, gpxFile); + } + } + } + }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + private void updateTurnInfo(String prefix, Intent result, NextDirectionInfo ni) { result.putExtra(prefix + PARAM_NT_DISTANCE, ni.distanceTo); result.putExtra(prefix + PARAM_NT_IMMINENT, ni.imminent);