Save gpx file for api navigation

This commit is contained in:
Vitaliy 2020-07-23 18:18:49 +03:00
parent b956b63ba1
commit 7417df6f6f
2 changed files with 77 additions and 57 deletions

View file

@ -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);

View file

@ -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<MapActivity> 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);