Save gpx file for api navigation
This commit is contained in:
parent
b956b63ba1
commit
7417df6f6f
2 changed files with 77 additions and 57 deletions
|
@ -671,50 +671,35 @@ public class OsmandAidlApi {
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
MapActivity mapActivity = mapActivityRef.get();
|
MapActivity mapActivity = mapActivityRef.get();
|
||||||
if (mapActivity != null) {
|
if (mapActivity != null) {
|
||||||
boolean force = intent.getBooleanExtra(AIDL_FORCE, false);
|
GPXFile gpx = loadGpxFileFromIntent(mapActivity, intent);
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gpx != null) {
|
if (gpx != null) {
|
||||||
final RoutingHelper routingHelper = app.getRoutingHelper();
|
boolean force = intent.getBooleanExtra(AIDL_FORCE, false);
|
||||||
if (routingHelper.isFollowingMode() && !force) {
|
ExternalApiHelper.saveAndNavigateGpx(mapActivity, gpx, force);
|
||||||
final GPXFile gpxFile = gpx;
|
}
|
||||||
AlertDialog dlg = mapActivity.getMapActions().stopNavigationActionConfirm();
|
}
|
||||||
dlg.setOnDismissListener(new DialogInterface.OnDismissListener() {
|
}
|
||||||
|
|
||||||
@Override
|
private GPXFile loadGpxFileFromIntent(@NonNull MapActivity mapActivity, @NonNull Intent intent) {
|
||||||
public void onDismiss(DialogInterface dialog) {
|
GPXFile gpx = null;
|
||||||
MapActivity mapActivity = mapActivityRef.get();
|
String gpxStr = intent.getStringExtra(AIDL_DATA);
|
||||||
if (mapActivity != null && !routingHelper.isFollowingMode()) {
|
if (!Algorithms.isEmpty(gpxStr)) {
|
||||||
ExternalApiHelper.startNavigation(mapActivity, gpxFile);
|
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) {
|
||||||
} else {
|
ParcelFileDescriptor gpxParcelDescriptor = null;
|
||||||
ExternalApiHelper.startNavigation(mapActivity, gpx);
|
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);
|
registerReceiver(navigateGpxReceiver, mapActivity, AIDL_NAVIGATE_GPX);
|
||||||
|
|
|
@ -5,6 +5,7 @@ import android.app.ProgressDialog;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.ParcelFileDescriptor;
|
import android.os.ParcelFileDescriptor;
|
||||||
|
|
||||||
|
@ -15,6 +16,7 @@ import androidx.appcompat.app.AlertDialog;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
|
import net.osmand.AndroidUtils;
|
||||||
import net.osmand.GPXUtilities;
|
import net.osmand.GPXUtilities;
|
||||||
import net.osmand.GPXUtilities.GPXFile;
|
import net.osmand.GPXUtilities.GPXFile;
|
||||||
import net.osmand.IndexConstants;
|
import net.osmand.IndexConstants;
|
||||||
|
@ -26,7 +28,6 @@ import net.osmand.aidl.search.SearchParams;
|
||||||
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;
|
||||||
import net.osmand.plus.settings.backend.ApplicationMode;
|
|
||||||
import net.osmand.plus.FavouritesDbHelper;
|
import net.osmand.plus.FavouritesDbHelper;
|
||||||
import net.osmand.plus.MapMarkersHelper;
|
import net.osmand.plus.MapMarkersHelper;
|
||||||
import net.osmand.plus.MapMarkersHelper.MapMarker;
|
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.RouteDirectionInfo;
|
||||||
import net.osmand.plus.routing.RoutingHelper;
|
import net.osmand.plus.routing.RoutingHelper;
|
||||||
import net.osmand.plus.search.listitems.QuickSearchListItem;
|
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.router.TurnType;
|
||||||
import net.osmand.search.SearchUICore;
|
import net.osmand.search.SearchUICore;
|
||||||
import net.osmand.search.core.ObjectType;
|
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)) {
|
if (API_CMD_SHOW_GPX.equals(cmd) || API_CMD_NAVIGATE_GPX.equals(cmd)) {
|
||||||
boolean navigate = API_CMD_NAVIGATE_GPX.equals(cmd);
|
boolean navigate = API_CMD_NAVIGATE_GPX.equals(cmd);
|
||||||
String path = uri.getQueryParameter(PARAM_PATH);
|
String path = uri.getQueryParameter(PARAM_PATH);
|
||||||
boolean force = uri.getBooleanQueryParameter(PARAM_FORCE, false);
|
|
||||||
|
|
||||||
GPXFile gpx = null;
|
GPXFile gpx = null;
|
||||||
if (path != null) {
|
if (path != null) {
|
||||||
|
@ -240,22 +243,8 @@ public class ExternalApiHelper {
|
||||||
|
|
||||||
if (gpx != null) {
|
if (gpx != null) {
|
||||||
if (navigate) {
|
if (navigate) {
|
||||||
final RoutingHelper routingHelper = app.getRoutingHelper();
|
boolean force = uri.getBooleanQueryParameter(PARAM_FORCE, false);
|
||||||
if (routingHelper.isFollowingMode() && !force) {
|
saveAndNavigateGpx(mapActivity, gpx, 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);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
app.getSelectedGpxHelper().setGpxFileToDisplay(gpx);
|
app.getSelectedGpxHelper().setGpxFileToDisplay(gpx);
|
||||||
}
|
}
|
||||||
|
@ -651,6 +640,52 @@ public class ExternalApiHelper {
|
||||||
return result;
|
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) {
|
private void updateTurnInfo(String prefix, Intent result, NextDirectionInfo ni) {
|
||||||
result.putExtra(prefix + PARAM_NT_DISTANCE, ni.distanceTo);
|
result.putExtra(prefix + PARAM_NT_DISTANCE, ni.distanceTo);
|
||||||
result.putExtra(prefix + PARAM_NT_IMMINENT, ni.imminent);
|
result.putExtra(prefix + PARAM_NT_IMMINENT, ni.imminent);
|
||||||
|
|
Loading…
Reference in a new issue