Fix / refactor save gpx (plan route)

This commit is contained in:
max-klaus 2020-08-23 20:07:30 +03:00
parent d0a274d877
commit 7907e72781
16 changed files with 261 additions and 196 deletions

View file

@ -461,7 +461,21 @@ public class GPXUtilities {
public String pointTypes;
public String names;
public StringBundle getStringBundle() {
public static RouteSegment fromStringBundle(StringBundle bundle) {
RouteSegment s = new RouteSegment();
s.id = bundle.getString("id", null);
s.length = bundle.getString("length", null);
s.segmentTime = bundle.getString("segmentTime", null);
s.speed = bundle.getString("speed", null);
s.turnType = bundle.getString("turnType", null);
s.turnAngle = bundle.getString("turnAngle", null);
s.types = bundle.getString("types", null);
s.pointTypes = bundle.getString("pointTypes", null);
s.names = bundle.getString("names", null);
return s;
}
public StringBundle toStringBundle() {
StringBundle bundle = new StringBundle();
bundle.putString("id", id);
bundle.putString("length", length);
@ -480,7 +494,14 @@ public class GPXUtilities {
public String tag;
public String value;
public StringBundle getStringBundle() {
public static RouteType fromStringBundle(StringBundle bundle) {
RouteType t = new RouteType();
t.tag = bundle.getString("t", null);
t.value = bundle.getString("v", null);
return t;
}
public StringBundle toStringBundle() {
StringBundle bundle = new StringBundle();
bundle.putString("t", tag);
bundle.putString("v", value);
@ -1834,12 +1855,12 @@ public class GPXUtilities {
StringBundle bundle = new StringBundle();
List<StringBundle> segmentsBundle = new ArrayList<>();
for (RouteSegment segment : gpxFile.routeSegments) {
segmentsBundle.add(segment.getStringBundle());
segmentsBundle.add(segment.toStringBundle());
}
bundle.putBundleList("route", "segment", segmentsBundle);
List<StringBundle> typesBundle = new ArrayList<>();
for (RouteType routeType : gpxFile.routeTypes) {
typesBundle.add(routeType.getStringBundle());
typesBundle.add(routeType.toStringBundle());
}
bundle.putBundleList("types", "type", typesBundle);
StringBundleWriter bundleWriter = new StringBundleXmlWriter(bundle, serializer);

View file

@ -1,7 +1,10 @@
package net.osmand.router;
import net.osmand.GPXUtilities;
import net.osmand.GPXUtilities.GPXExtensionsWriter;
import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.RouteSegment;
import net.osmand.GPXUtilities.RouteType;
import net.osmand.GPXUtilities.Track;
import net.osmand.GPXUtilities.TrkSegment;
import net.osmand.GPXUtilities.WptPt;
@ -37,8 +40,7 @@ public class RouteExporter {
public GPXFile exportRoute() {
RouteDataResources resources = new RouteDataResources(locations);
final RouteDataBundle bundle = new RouteDataBundle(resources);
List<StringBundle> routeItems = new ArrayList<>();
if (!Algorithms.isEmpty(route)) {
for (RouteSegmentResult sr : route) {
sr.collectTypes(resources);
@ -47,15 +49,12 @@ public class RouteExporter {
sr.collectNames(resources);
}
List<StringBundle> routeItems = new ArrayList<>();
for (RouteSegmentResult sr : route) {
RouteDataBundle itemBundle = new RouteDataBundle(resources);
sr.writeToBundle(itemBundle);
routeItems.add(itemBundle);
}
bundle.putBundleList("route", "segment", routeItems);
}
List<StringBundle> typeList = new ArrayList<>();
Map<RouteTypeRule, Integer> rules = resources.getRules();
for (RouteTypeRule rule : rules.keySet()) {
@ -63,7 +62,6 @@ public class RouteExporter {
rule.writeToBundle(typeBundle);
typeList.add(typeBundle);
}
bundle.putBundleList("types", "type", typeList);
GPXFile gpx = new GPXFile(OSMAND_ROUTER_V2);
Track track = new Track();
@ -75,7 +73,6 @@ public class RouteExporter {
if (locations == null || locations.isEmpty()) {
return gpx;
}
for (int i = 0; i < locations.size(); i++) {
Location loc = locations.get(i);
WptPt pt = new WptPt();
@ -92,21 +89,22 @@ public class RouteExporter {
}
trkSegment.points.add(pt);
}
if (points != null) {
for (WptPt pt : points) {
gpx.addPoint(pt);
}
}
GPXExtensionsWriter extensionsWriter = new GPXExtensionsWriter() {
@Override
public void writeExtensions(XmlSerializer serializer) {
StringBundleWriter bundleWriter = new StringBundleXmlWriter(bundle, serializer);
bundleWriter.writeBundle();
}
};
gpx.setExtensionsWriter(extensionsWriter);
List<RouteSegment> routeSegments = new ArrayList<>();
for (StringBundle item : routeItems) {
routeSegments.add(RouteSegment.fromStringBundle(item));
}
gpx.routeSegments = routeSegments;
List<RouteType> routeTypes = new ArrayList<>();
for (StringBundle item : typeList) {
routeTypes.add(RouteType.fromStringBundle(item));
}
gpx.routeTypes = routeTypes;
return gpx;
}

View file

@ -98,7 +98,7 @@ public class RouteImporter {
for (RouteSegment segment : gpxFile.routeSegments) {
RouteDataObject object = new RouteDataObject(region);
RouteSegmentResult segmentResult = new RouteSegmentResult(object);
segmentResult.readFromBundle(new RouteDataBundle(resources, segment.getStringBundle()));
segmentResult.readFromBundle(new RouteDataBundle(resources, segment.toStringBundle()));
route.add(segmentResult);
}
}
@ -106,7 +106,7 @@ public class RouteImporter {
private void collectTypes() {
int i = 0;
for (RouteType routeType : gpxFile.routeTypes) {
StringBundle bundle = routeType.getStringBundle();
StringBundle bundle = routeType.toStringBundle();
String t = bundle.getString("t", null);
String v = bundle.getString("v", null);
region.initRouteEncodingRule(i++, t, v);

View file

@ -327,6 +327,9 @@ public class RouteSegmentResult implements StringExternalizable<RouteDataBundle>
Location prevLocation = null;
for (int i = 0; i < length; i++) {
Location location = resources.getLocation(index);
if (location == null) {
break;
}
double dist = 0;
if (prevLocation != null) {
dist = MapUtils.getDistance(prevLocation.getLatitude(), prevLocation.getLongitude(), location.getLatitude(), location.getLongitude());

View file

@ -120,7 +120,7 @@ import net.osmand.plus.mapmarkers.PlanRouteFragment;
import net.osmand.plus.measurementtool.GpxApproximationFragment;
import net.osmand.plus.measurementtool.MeasurementEditingContext;
import net.osmand.plus.measurementtool.MeasurementToolFragment;
import net.osmand.plus.measurementtool.NewGpxData;
import net.osmand.plus.measurementtool.GpxData;
import net.osmand.plus.measurementtool.SnapTrackWarningBottomSheet;
import net.osmand.plus.quickaction.QuickActionListFragment;
import net.osmand.plus.render.RendererRegistry;
@ -1290,12 +1290,12 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
QuadRect qr = newGpxPoint.getRect();
mapView.fitRectToMap(qr.left, qr.right, qr.top, qr.bottom, (int) qr.width(), (int) qr.height(), 0);
getMapLayers().getContextMenuLayer().enterAddGpxPointMode(newGpxPoint);
} else if (toShow instanceof NewGpxData) {
NewGpxData newGpxData = (NewGpxData) toShow;
QuadRect qr = newGpxData.getRect();
} else if (toShow instanceof GpxData) {
GpxData gpxData = (GpxData) toShow;
QuadRect qr = gpxData.getRect();
mapView.fitRectToMap(qr.left, qr.right, qr.top, qr.bottom, (int) qr.width(), (int) qr.height(), 0);
MeasurementEditingContext editingContext = new MeasurementEditingContext();
editingContext.setNewGpxData(newGpxData);
editingContext.setGpxData(gpxData);
MeasurementToolFragment.showInstance(getSupportFragmentManager(), editingContext);
} else {
mapContextMenu.show(latLonToShow, mapLabelToShow, toShow);

View file

@ -37,7 +37,7 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.mapmarkers.CoordinateInputDialogFragment;
import net.osmand.plus.measurementtool.NewGpxData;
import net.osmand.plus.measurementtool.GpxData;
import net.osmand.plus.myplaces.FavoritesActivity;
import net.osmand.plus.myplaces.SplitSegmentDialogFragment;
import net.osmand.plus.myplaces.TrackBitmapDrawer;
@ -136,14 +136,14 @@ public class TrackActivity extends TabActivity {
}
}
public void addNewGpxData(NewGpxData.ActionType actionType) {
public void addNewGpxData(GpxData.ActionType actionType) {
addNewGpxData(actionType, null);
}
public void addNewGpxData(NewGpxData.ActionType actionType, TrkSegment segment) {
public void addNewGpxData(GpxData.ActionType actionType, TrkSegment segment) {
GPXFile gpxFile = getGpx();
QuadRect rect = getRect();
NewGpxData newGpxData = new NewGpxData(gpxFile, rect, actionType, segment);
GpxData gpxData = new GpxData(gpxFile, rect, actionType, segment);
WptPt pointToShow = gpxFile != null ? gpxFile.findPointToShow() : null;
if (pointToShow != null) {
LatLon location = new LatLon(pointToShow.getLatitude(), pointToShow.getLongitude());
@ -152,7 +152,7 @@ public class TrackActivity extends TabActivity {
settings.getLastKnownMapZoom(),
new PointDescription(PointDescription.POINT_TYPE_WPT, getString(R.string.add_line)),
false,
newGpxData
gpxData
);
MapActivity.launchMapActivityMoveToTop(this);

View file

@ -4,7 +4,7 @@ import net.osmand.GPXUtilities.GPXFile;
import net.osmand.GPXUtilities.TrkSegment;
import net.osmand.data.QuadRect;
public class NewGpxData {
public class GpxData {
public enum ActionType {
ADD_SEGMENT,
@ -18,7 +18,7 @@ public class NewGpxData {
private QuadRect rect;
private ActionType actionType;
public NewGpxData(GPXFile gpxFile, QuadRect rect, ActionType actionType, TrkSegment trkSegment) {
public GpxData(GPXFile gpxFile, QuadRect rect, ActionType actionType, TrkSegment trkSegment) {
this.gpxFile = gpxFile;
this.rect = rect;
this.actionType = actionType;

View file

@ -39,7 +39,6 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static net.osmand.plus.measurementtool.MeasurementEditingContext.CalculationMode.NEXT_SEGMENT;
import static net.osmand.plus.measurementtool.MeasurementEditingContext.CalculationMode.WHOLE_TRACK;
public class MeasurementEditingContext {
@ -54,7 +53,7 @@ public class MeasurementEditingContext {
private final TrkSegment after = new TrkSegment();
private TrkSegment afterCacheForSnap;
private NewGpxData newGpxData;
private GpxData gpxData;
private int selectedPointPosition = -1;
private WptPt originalPointToMove;
@ -139,6 +138,14 @@ public class MeasurementEditingContext {
return commandManager;
}
public boolean hasChanges() {
return commandManager.hasChanges();
}
public void setChangesSaved() {
commandManager.resetChangesCounter();
}
boolean isInAddPointMode() {
return inAddPointMode;
}
@ -167,20 +174,21 @@ public class MeasurementEditingContext {
this.inAddPointMode = inAddPointMode;
}
NewGpxData getNewGpxData() {
return newGpxData;
@Nullable
GpxData getGpxData() {
return gpxData;
}
public boolean isNewData() {
return newGpxData == null;
return gpxData == null;
}
public void setNewGpxData(NewGpxData newGpxData) {
this.newGpxData = newGpxData;
public void setGpxData(GpxData gpxData) {
this.gpxData = gpxData;
}
public boolean hasRoutePoints() {
return newGpxData != null && newGpxData.getGpxFile() != null && newGpxData.getGpxFile().hasRtePt();
return gpxData != null && gpxData.getGpxFile() != null && gpxData.getGpxFile().hasRtePt();
}
public CalculationMode getCalculationMode() {
@ -390,15 +398,15 @@ public class MeasurementEditingContext {
}
void addPoints() {
NewGpxData newGpxData = getNewGpxData();
if (newGpxData == null || newGpxData.getTrkSegment() == null || Algorithms.isEmpty(newGpxData.getTrkSegment().points)) {
GpxData gpxData = getGpxData();
if (gpxData == null || gpxData.getTrkSegment() == null || Algorithms.isEmpty(gpxData.getTrkSegment().points)) {
return;
}
List<WptPt> points = newGpxData.getTrkSegment().points;
List<WptPt> points = gpxData.getTrkSegment().points;
if (isTrackSnappedToRoad()) {
RouteImporter routeImporter = new RouteImporter(newGpxData.getGpxFile());
RouteImporter routeImporter = new RouteImporter(gpxData.getGpxFile());
List<RouteSegmentResult> segments = routeImporter.importRoute();
List<WptPt> routePoints = newGpxData.getGpxFile().getRoutePoints();
List<WptPt> routePoints = gpxData.getGpxFile().getRoutePoints();
int prevPointIndex = 0;
for (int i = 0; i < routePoints.size() - 1; i++) {
Pair<WptPt, WptPt> pair = new Pair<>(routePoints.get(i), routePoints.get(i + 1));
@ -504,10 +512,10 @@ public class MeasurementEditingContext {
}
boolean isTrackSnappedToRoad() {
NewGpxData newGpxData = getNewGpxData();
return newGpxData != null && newGpxData.getTrkSegment() != null
&& !newGpxData.getTrkSegment().points.isEmpty()
&& newGpxData.getGpxFile().hasRoute();
GpxData gpxData = getGpxData();
return gpxData != null && gpxData.getTrkSegment() != null
&& !gpxData.getTrkSegment().points.isEmpty()
&& gpxData.getGpxFile().hasRoute();
}
private void updateCacheForSnap(boolean both) {

View file

@ -61,7 +61,7 @@ import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.measurementtool.GpxApproximationFragment.GpxApproximationFragmentListener;
import net.osmand.plus.measurementtool.NewGpxData.ActionType;
import net.osmand.plus.measurementtool.GpxData.ActionType;
import net.osmand.plus.measurementtool.OptionsBottomSheetDialogFragment.OptionsFragmentListener;
import net.osmand.plus.measurementtool.RouteBetweenPointsBottomSheetDialogFragment.RouteBetweenPointsFragmentListener;
import net.osmand.plus.measurementtool.SaveAsNewTrackBottomSheetDialogFragment.SaveAsNewTrackFragmentListener;
@ -83,6 +83,7 @@ import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControll
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControllerType;
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarView;
import net.osmand.router.RoutePlannerFrontEnd.GpxRouteApproximation;
import net.osmand.util.Algorithms;
import java.io.File;
import java.lang.ref.WeakReference;
@ -131,11 +132,9 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
private boolean progressBarVisible;
private boolean pointsListOpened;
private boolean planRouteMode = false;
private Boolean saved;
private boolean portrait;
private boolean nightMode;
private int cachedMapPosition;
private boolean gpxPointsAdded;
private MeasurementEditingContext editingCtx = new MeasurementEditingContext();
@ -154,7 +153,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
this.initialPoint = initialPoint;
}
public void setPlanRouteMode(boolean planRouteMode) {
private void setPlanRouteMode(boolean planRouteMode) {
this.planRouteMode = planRouteMode;
}
@ -386,19 +385,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
} else {
toolBarController.setBackBtnIconIds(R.drawable.ic_action_remove_dark, R.drawable.ic_action_remove_dark);
}
final NewGpxData newGpxData = editingCtx.getNewGpxData();
if (newGpxData != null) {
ActionType actionType = newGpxData.getActionType();
if (actionType == ActionType.ADD_ROUTE_POINTS) {
toolBarController.setTitle(getString(R.string.add_route_points));
} else if (actionType == ActionType.ADD_SEGMENT) {
toolBarController.setTitle(getString(R.string.add_line));
} else if (actionType == ActionType.EDIT_SEGMENT) {
toolBarController.setTitle(getString(R.string.edit_line));
}
} else {
toolBarController.setTitle(getString(R.string.plan_route));
}
toolBarController.setOnBackButtonClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
@ -408,27 +394,14 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
toolBarController.setOnSaveViewClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (editingCtx.getPointsCount() > 0) {
if (newGpxData != null && newGpxData.getActionType() == ActionType.EDIT_SEGMENT) {
openSaveAsNewTrackMenu(mapActivity);
} else {
if (newGpxData == null) {
final File dir = mapActivity.getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR);
String fileName = getSuggestedName(dir) + GPX_FILE_EXT;
saveNewGpx(dir, fileName, true, SaveType.ROUTE_POINT, true);
} else {
addToGpx(mapActivity);
}
}
} else {
Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show();
}
saveChanges(true);
}
});
mapActivity.showTopToolbar(toolBarController);
updateToolbar();
final GpxData gpxData = editingCtx.getGpxData();
adapter = new MeasurementToolAdapter(getMapActivity(), editingCtx.getPoints(),
newGpxData != null ? newGpxData.getActionType() : null);
gpxData != null ? gpxData.getActionType() : null);
if (portrait) {
pointsRv = mainView.findViewById(R.id.measure_points_recycler_view);
} else {
@ -450,7 +423,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
});
snapToRoadBtn.setVisibility(View.VISIBLE);
initMeasurementMode(newGpxData);
initMeasurementMode(gpxData);
if (savedInstanceState == null) {
if (editingCtx.isNewData() && planRouteMode) {
@ -464,6 +437,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
return view;
}
public boolean isInEditMode() {
return !planRouteMode && !editingCtx.isNewData();
}
private void updateUndoRedoCommonStuff() {
hidePointsListIfNoPoints();
if (editingCtx.getPointsCount() > 0) {
@ -474,34 +451,34 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
updateSnapToRoadControls();
}
private void initMeasurementMode(NewGpxData newGpxData) {
private void initMeasurementMode(GpxData gpxData) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
editingCtx.getCommandManager().setMeasurementLayer(mapActivity.getMapLayers().getMeasurementToolLayer());
enterMeasurementMode();
updateSnapToRoadControls();
if (newGpxData != null && !gpxPointsAdded) {
List<WptPt> points = newGpxData.getGpxFile().getRoutePoints();
if (gpxData != null) {
List<WptPt> points = gpxData.getGpxFile().getRoutePoints();
if (!points.isEmpty()) {
ApplicationMode snapToRoadAppMode = ApplicationMode.valueOfStringKey(points.get(points.size() - 1).getProfileType(), null);
if (snapToRoadAppMode != null) {
setAppMode(snapToRoadAppMode);
}
}
ActionType actionType = newGpxData.getActionType();
ActionType actionType = gpxData.getActionType();
if (actionType == ActionType.ADD_ROUTE_POINTS) {
displayRoutePoints();
gpxPointsAdded = true;
} else if (actionType == ActionType.EDIT_SEGMENT) {
displaySegmentPoints();
gpxPointsAdded = true;
}
}
/*
if (saved == null) {
saved = newGpxData != null
&& (newGpxData.getActionType() == ActionType.ADD_ROUTE_POINTS
|| newGpxData.getActionType() == ActionType.EDIT_SEGMENT);
saved = gpxData != null
&& (gpxData.getActionType() == ActionType.ADD_ROUTE_POINTS
|| gpxData.getActionType() == ActionType.EDIT_SEGMENT);
}
*/
}
}
@ -579,9 +556,9 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
}
private void updateMainIcon() {
NewGpxData newGpxData = editingCtx.getNewGpxData();
if (newGpxData != null) {
ActionType actionType = newGpxData.getActionType();
GpxData gpxData = editingCtx.getGpxData();
if (gpxData != null) {
ActionType actionType = gpxData.getActionType();
if (actionType == ActionType.ADD_SEGMENT || actionType == ActionType.EDIT_SEGMENT) {
mainIcon.setImageDrawable(getActiveIcon(R.drawable.ic_action_polygom_dark));
} else {
@ -611,6 +588,24 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
}
}
public void saveChanges(boolean close) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (editingCtx.getPointsCount() > 0) {
GpxData gpxData = editingCtx.getGpxData();
if (editingCtx.isNewData()) {
saveAsGpx(SaveType.ROUTE_POINT);
} else if (isInEditMode() && gpxData.getActionType() == ActionType.EDIT_SEGMENT) {
openSaveAsNewTrackMenu(mapActivity);
} else {
addToGpx(mapActivity, close);
}
} else {
Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
@ -663,23 +658,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
}
@Override
public void addToGpxOnClick() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (editingCtx.getPointsCount() > 0) {
editingCtx.getPoints().clear();
editingCtx.getPoints().addAll(editingCtx.getBeforePoints());
editingCtx.getBeforePoints().clear();
editingCtx.getBeforePoints().addAll(editingCtx.getBeforeTrkSegmentLine().points);
if (editingCtx.isNewData()) {
saveAsGpx(SaveType.ROUTE_POINT);
} else {
addToGpx(mapActivity);
}
} else {
Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show();
}
}
public void saveChangesOnClick() {
saveChanges(false);
}
@Override
@ -710,7 +690,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
updateUndoRedoButton(false, redoBtn);
disable(upDownBtn);
updateDistancePointsText();
saved = false;
}
@Override
@ -783,7 +762,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
updateUndoRedoButton(false, redoBtn);
updateUndoRedoButton(true, undoBtn);
updateDistancePointsText();
saved = false;
}
@Override
@ -841,7 +819,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
@Override
public void openLastEditTrackOnClick(String gpxFileName) {
addNewGpxData(getGpxFile(gpxFileName));
saved = true;
}
@Override
@ -856,7 +833,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
@Override
public void selectFileOnCLick(String gpxFileName) {
addNewGpxData(getGpxFile(gpxFileName));
saved = true;
}
@Override
@ -896,7 +872,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
SelectedGpxFile selectedGpxFile = mapActivity.getMyApplication().getSelectedGpxHelper()
.getSelectedFileByPath(gpxFile.path);
boolean showOnMap = selectedGpxFile != null;
saveExistingGpx(gpxFile, showOnMap, ActionType.ADD_SEGMENT, false);
saveExistingGpx(gpxFile, showOnMap, ActionType.ADD_SEGMENT, editingCtx.hasRoute() ? SaveType.ROUTE_POINT : SaveType.LINE, false);
}
}
@ -910,11 +886,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
QuadRect rect = gpxFile.getRect();
TrkSegment segment = gpxFile.getNonEmptyTrkSegment();
ActionType actionType = segment == null ? ActionType.ADD_ROUTE_POINTS : ActionType.EDIT_SEGMENT;
NewGpxData newGpxData = new NewGpxData(gpxFile, rect, actionType, segment);
editingCtx.setNewGpxData(newGpxData);
initMeasurementMode(newGpxData);
QuadRect qr = newGpxData.getRect();
GpxData gpxData = new GpxData(gpxFile, rect, actionType, segment);
editingCtx.setGpxData(gpxData);
initMeasurementMode(gpxData);
QuadRect qr = gpxData.getRect();
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
mapActivity.getMapView().fitRectToMap(qr.left, qr.right, qr.top, qr.bottom,
@ -929,7 +904,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
updateUndoRedoButton(true, undoBtn);
updateUndoRedoButton(false, redoBtn);
updateDistancePointsText();
saved = false;
hidePointsListIfNoPoints();
}
}
@ -991,7 +965,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
updateUndoRedoButton(false, redoBtn);
updateDistancePointsText();
mapActivity.refreshMap();
saved = false;
}
}
}
@ -1042,12 +1015,15 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
private void displayRoutePoints() {
MeasurementToolLayer measurementLayer = getMeasurementLayer();
GPXFile gpx = editingCtx.getNewGpxData().getGpxFile();
List<WptPt> points = gpx.getRoutePoints();
if (measurementLayer != null) {
editingCtx.addPoints(points);
adapter.notifyDataSetChanged();
updateDistancePointsText();
GpxData gpxData = editingCtx.getGpxData();
GPXFile gpx = gpxData != null ? gpxData.getGpxFile() : null;
if (gpx != null) {
List<WptPt> points = gpx.getRoutePoints();
if (measurementLayer != null) {
editingCtx.addPoints(points);
adapter.notifyDataSetChanged();
updateDistancePointsText();
}
}
}
@ -1254,7 +1230,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
updateUndoRedoButton(false, redoBtn);
updateDistancePointsText();
adapter.notifyDataSetChanged();
saved = false;
}
private void showPointsList() {
@ -1340,12 +1315,15 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
}
}
private void addToGpx(MapActivity mapActivity) {
GPXFile gpx = editingCtx.getNewGpxData().getGpxFile();
SelectedGpxFile selectedGpxFile = mapActivity.getMyApplication().getSelectedGpxHelper().getSelectedFileByPath(gpx.path);
boolean showOnMap = selectedGpxFile != null;
ActionType actionType = editingCtx.getNewGpxData().getActionType();
saveExistingGpx(gpx, showOnMap, actionType, true);
private void addToGpx(MapActivity mapActivity, boolean close) {
GpxData gpxData = editingCtx.getGpxData();
GPXFile gpx = gpxData != null ? gpxData.getGpxFile() : null;
if (gpx != null) {
SelectedGpxFile selectedGpxFile =
mapActivity.getMyApplication().getSelectedGpxHelper().getSelectedFileByPath(gpx.path);
boolean showOnMap = selectedGpxFile != null;
saveExistingGpx(gpx, showOnMap, gpxData.getActionType(), editingCtx.hasRoute() ? SaveType.ROUTE_POINT : SaveType.LINE, close);
}
}
private void saveAsGpx(final SaveType saveType) {
@ -1366,7 +1344,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
});
showOnMapToggle.setChecked(true);
String displayedName = getSuggestedName(dir);
String displayedName = getSuggestedFileName(dir);
nameEt.setText(displayedName);
nameEt.setSelection(displayedName.length());
final boolean[] textChanged = new boolean[1];
@ -1425,10 +1403,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
}
}
private String getSuggestedName(File dir) {
NewGpxData newGpxData = editingCtx.getNewGpxData();
private String getSuggestedFileName(File dir) {
GpxData gpxData = editingCtx.getGpxData();
String displayedName;
if (newGpxData == null) {
if (gpxData == null) {
final String suggestedName = new SimpleDateFormat("EEE dd MMM yyyy", Locale.US).format(new Date());
displayedName = suggestedName;
File fout = new File(dir, suggestedName + GPX_FILE_EXT);
@ -1438,25 +1416,24 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
fout = new File(dir, displayedName + GPX_FILE_EXT);
}
} else {
displayedName = AndroidUtils.trimExtension(new File(newGpxData.getGpxFile().path).getName());
displayedName = AndroidUtils.trimExtension(new File(gpxData.getGpxFile().path).getName());
}
return displayedName;
}
private void saveNewGpx(File dir, String fileName, boolean showOnMap, SaveType saveType, boolean close) {
saveGpx(dir, fileName, showOnMap, null, false, null, saveType, close);
saveGpx(dir, fileName, showOnMap, null, null, saveType, close);
}
private void saveExistingGpx(GPXFile gpx, boolean showOnMap, ActionType actionType, boolean openTrackActivity) {
saveGpx(null, null, showOnMap, gpx, openTrackActivity, actionType, null, false);
private void saveExistingGpx(GPXFile gpx, boolean showOnMap, ActionType actionType, SaveType saveType, boolean close) {
saveGpx(null, null, showOnMap, gpx, actionType, saveType, close);
}
@SuppressLint("StaticFieldLeak")
private void saveGpx(final File dir,
final String fileName,
final boolean showOnMap,
final GPXFile gpx,
final boolean openTrackActivity,
final GPXFile gpxFile,
final ActionType actionType,
final SaveType saveType,
final boolean close) {
@ -1487,7 +1464,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
List<WptPt> points = editingCtx.getPoints();
TrkSegment before = editingCtx.getBeforeTrkSegmentLine();
TrkSegment after = editingCtx.getAfterTrkSegmentLine();
if (gpx == null) {
if (gpxFile == null) {
toSave = new File(dir, fileName);
String trackName = fileName.substring(0, fileName.length() - GPX_FILE_EXT.length());
GPXFile gpx = new GPXFile(Version.getFullVersion(app));
@ -1517,9 +1494,30 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
}
return res;
} else {
GPXFile gpx = gpxFile;
toSave = new File(gpx.path);
String trackName = Algorithms.getFileNameWithoutExtension(toSave);
if (measurementLayer != null) {
if (actionType != null) {
if (planRouteMode) {
if (saveType == SaveType.LINE) {
TrkSegment segment = new TrkSegment();
segment.points.addAll(before.points);
segment.points.addAll(after.points);
Track track = new Track();
track.name = trackName;
track.segments.add(segment);
gpx.tracks.add(track);
} else if (saveType == SaveType.ROUTE_POINT) {
if (editingCtx.hasRoute()) {
GPXFile newGpx = editingCtx.exportRouteAsGpx(trackName);
if (newGpx != null) {
gpx = newGpx;
}
}
gpx.addRoutePoints(points);
}
} else if (actionType != null) {
GpxData gpxData = editingCtx.getGpxData();
switch (actionType) {
case ADD_SEGMENT: {
List<WptPt> snappedPoints = new ArrayList<>();
@ -1533,18 +1531,22 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
break;
}
case EDIT_SEGMENT: {
TrkSegment segment = new TrkSegment();
segment.points.addAll(points);
gpx.replaceSegment(editingCtx.getNewGpxData().getTrkSegment(), segment);
if (gpxData != null) {
TrkSegment segment = new TrkSegment();
segment.points.addAll(points);
gpx.replaceSegment(gpxData.getTrkSegment(), segment);
}
break;
}
case OVERWRITE_SEGMENT: {
List<WptPt> snappedPoints = new ArrayList<>();
snappedPoints.addAll(before.points);
snappedPoints.addAll(after.points);
TrkSegment segment = new TrkSegment();
segment.points.addAll(snappedPoints);
gpx.replaceSegment(editingCtx.getNewGpxData().getTrkSegment(), segment);
if (gpxData != null) {
List<WptPt> snappedPoints = new ArrayList<>();
snappedPoints.addAll(before.points);
snappedPoints.addAll(after.points);
TrkSegment segment = new TrkSegment();
segment.points.addAll(snappedPoints);
gpx.replaceSegment(gpxData.getTrkSegment(), segment);
}
break;
}
}
@ -1580,8 +1582,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
}
mapActivity.refreshMap();
if (warning == null) {
saved = true;
if (openTrackActivity) {
editingCtx.setChangesSaved();
if (isInEditMode()) {
dismiss(mapActivity);
} else {
if (close) {
@ -1651,12 +1653,30 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
if (mapActivity == null) {
return;
}
if (editingCtx.getPointsCount() > 1) {
final File dir = mapActivity.getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR);
toolBarController.setTitle(getSuggestedName(dir));
toolBarController.setDescription(getString(R.string.plan_route));
final GpxData gpxData = editingCtx.getGpxData();
String fileName = getSuggestedFileName(mapActivity.getMyApplication().getAppPath(IndexConstants.GPX_INDEX_DIR));
String actionStr = getString(R.string.plan_route);
boolean editMode = isInEditMode();
if (editMode) {
ActionType actionType = gpxData.getActionType();
switch (actionType) {
case ADD_ROUTE_POINTS:
actionStr = getString(R.string.add_route_points);
break;
case ADD_SEGMENT:
actionStr = getString(R.string.add_line);
break;
case EDIT_SEGMENT:
case OVERWRITE_SEGMENT:
actionStr = getString(R.string.edit_line);
break;
}
}
if (!editMode && editingCtx.getPointsCount() > 1) {
toolBarController.setTitle(fileName);
toolBarController.setDescription(actionStr);
} else {
toolBarController.setTitle(getString(R.string.plan_route));
toolBarController.setTitle(actionStr);
toolBarController.setDescription(null);
}
mapActivity.showTopToolbar(toolBarController);
@ -1743,7 +1763,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
hidePointsList();
return;
}
if (editingCtx.getPointsCount() == 0 || saved) {
if (!editingCtx.hasChanges()) {
dismiss(mapActivity);
return;
}
@ -1759,13 +1779,16 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
}
resetAppMode();
hideSnapToRoadIcon();
if (!editingCtx.isNewData() && !planRouteMode) {
GPXFile gpx = editingCtx.getNewGpxData().getGpxFile();
Intent newIntent = new Intent(mapActivity, mapActivity.getMyApplication().getAppCustomization().getTrackActivity());
newIntent.putExtra(TrackActivity.TRACK_FILE_NAME, gpx.path);
newIntent.putExtra(TrackActivity.OPEN_TRACKS_LIST, true);
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(newIntent);
if (isInEditMode()) {
GpxData gpxData = editingCtx.getGpxData();
GPXFile gpx = gpxData != null ? gpxData.getGpxFile() : null;
if (gpx != null) {
Intent newIntent = new Intent(mapActivity, mapActivity.getMyApplication().getAppCustomization().getTrackActivity());
newIntent.putExtra(TrackActivity.TRACK_FILE_NAME, gpx.path);
newIntent.putExtra(TrackActivity.OPEN_TRACKS_LIST, true);
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(newIntent);
}
}
mapActivity.getSupportFragmentManager().beginTransaction().remove(this).commitAllowingStateLoss();
} catch (Exception e) {

View file

@ -89,7 +89,7 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
public void onClick(View v) {
Fragment fragment = getTargetFragment();
if (fragment instanceof OptionsFragmentListener) {
((OptionsFragmentListener) fragment).addToGpxOnClick();
((OptionsFragmentListener) fragment).saveChangesOnClick();
}
dismiss();
}
@ -189,7 +189,7 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
void snapToRoadOnCLick();
void addToGpxOnClick();
void saveChangesOnClick();
void saveAsNewTrackOnClick();

View file

@ -25,7 +25,7 @@ import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription;
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleDividerItem;
import net.osmand.plus.helpers.FontCache;
import net.osmand.plus.measurementtool.NewGpxData.ActionType;
import net.osmand.plus.measurementtool.GpxData.ActionType;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.util.MapUtils;
@ -224,8 +224,8 @@ public class SelectedPointBottomSheetDialogFragment extends MenuBottomSheetDialo
if (!TextUtils.isEmpty(pointName)) {
return pointName;
}
NewGpxData newGpxData = editingCtx.getNewGpxData();
if (newGpxData != null && newGpxData.getActionType() == ActionType.ADD_ROUTE_POINTS) {
GpxData gpxData = editingCtx.getGpxData();
if (gpxData != null && gpxData.getActionType() == ActionType.ADD_ROUTE_POINTS) {
return getString(R.string.route_point) + " - " + (pos + 1);
}
return getString(R.string.plugin_distance_point) + " - " + (pos + 1);
@ -265,8 +265,8 @@ public class SelectedPointBottomSheetDialogFragment extends MenuBottomSheetDialo
}
description.append(OsmAndFormatter.getFormattedDistance(dist, mapActivity.getMyApplication()));
}
NewGpxData newGpxData = editingCtx.getNewGpxData();
if (newGpxData != null && newGpxData.getActionType() == ActionType.EDIT_SEGMENT) {
GpxData gpxData = editingCtx.getGpxData();
if (gpxData != null && gpxData.getActionType() == ActionType.EDIT_SEGMENT) {
double elevation = pt.ele;
if (!Double.isNaN(elevation)) {
description.append(" ").append((getString(R.string.altitude)).substring(0, 1)).append(": ");

View file

@ -19,7 +19,7 @@ import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.measurementtool.NewGpxData.ActionType;
import net.osmand.plus.measurementtool.GpxData.ActionType;
import net.osmand.plus.views.controls.ReorderItemTouchHelperCallback;
import java.util.Collections;

View file

@ -13,6 +13,16 @@ public class MeasurementCommandManager {
private final Deque<MeasurementModeCommand> undoCommands = new LinkedList<>();
private final Deque<MeasurementModeCommand> redoCommands = new LinkedList<>();
private int changesCounter = 0;
public boolean hasChanges() {
return changesCounter != 0;
}
public void resetChangesCounter() {
changesCounter = 0;
}
public boolean canUndo() {
return undoCommands.size() > 0;
}
@ -25,6 +35,7 @@ public class MeasurementCommandManager {
if (command.execute()) {
undoCommands.push(command);
redoCommands.clear();
changesCounter++;
return true;
}
return false;
@ -41,6 +52,7 @@ public class MeasurementCommandManager {
MeasurementModeCommand command = undoCommands.pop();
redoCommands.push(command);
command.undo();
changesCounter--;
return command.getType();
}
return null;
@ -52,6 +64,7 @@ public class MeasurementCommandManager {
MeasurementModeCommand command = redoCommands.pop();
undoCommands.push(command);
command.redo();
changesCounter++;
return command.getType();
}
return null;

View file

@ -54,7 +54,7 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.dialogs.GpxAppearanceAdapter;
import net.osmand.plus.measurementtool.NewGpxData;
import net.osmand.plus.measurementtool.GpxData;
import net.osmand.plus.myplaces.TrackBitmapDrawer.TrackBitmapDrawerListener;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.settings.backend.OsmandSettings.CommonPreference;
@ -918,14 +918,14 @@ public class TrackActivityFragmentAdapter implements TrackBitmapDrawerListener {
}
}
public void addNewGpxData(NewGpxData.ActionType actionType) {
public void addNewGpxData(GpxData.ActionType actionType) {
TrackActivity activity = getTrackActivity();
if (activity != null) {
activity.addNewGpxData(actionType);
}
}
public void addNewGpxData(NewGpxData.ActionType actionType, GPXUtilities.TrkSegment segment) {
public void addNewGpxData(GpxData.ActionType actionType, GPXUtilities.TrkSegment segment) {
TrackActivity activity = getTrackActivity();
if (activity != null) {
activity.addNewGpxData(actionType, segment);
@ -952,9 +952,9 @@ public class TrackActivityFragmentAdapter implements TrackBitmapDrawerListener {
new PointDescription(PointDescription.POINT_TYPE_WPT, app.getString(R.string.add_waypoint));
addPoint(pointWptDescription);
} else if (i == R.id.route_text_layout || i == R.id.route_fab) {
addNewGpxData(NewGpxData.ActionType.ADD_ROUTE_POINTS);
addNewGpxData(GpxData.ActionType.ADD_ROUTE_POINTS);
} else if (i == R.id.line_text_layout || i == R.id.line_fab) {
addNewGpxData(NewGpxData.ActionType.ADD_SEGMENT);
addNewGpxData(GpxData.ActionType.ADD_SEGMENT);
}
}
};

View file

@ -65,7 +65,7 @@ import net.osmand.plus.helpers.GpxUiHelper;
import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetAxisType;
import net.osmand.plus.helpers.GpxUiHelper.GPXDataSetType;
import net.osmand.plus.helpers.GpxUiHelper.OrderedLineDataSet;
import net.osmand.plus.measurementtool.NewGpxData;
import net.osmand.plus.measurementtool.GpxData;
import net.osmand.plus.track.SaveGpxAsyncTask;
import net.osmand.plus.track.SaveGpxAsyncTask.SaveGpxListener;
import net.osmand.plus.myplaces.TrackBitmapDrawer.TrackBitmapDrawerListener;
@ -1026,7 +1026,7 @@ public class TrackSegmentFragment extends OsmAndListFragment implements TrackBit
private void editSegment() {
TrkSegment segment = getTrkSegment();
if (segment != null && fragmentAdapter != null) {
fragmentAdapter.addNewGpxData(NewGpxData.ActionType.EDIT_SEGMENT, segment);
fragmentAdapter.addNewGpxData(GpxData.ActionType.EDIT_SEGMENT, segment);
}
}

View file

@ -42,8 +42,8 @@ import net.osmand.plus.helpers.ImportHelper;
import net.osmand.plus.helpers.ImportHelper.OnGpxImportCompleteListener;
import net.osmand.plus.measurementtool.MeasurementEditingContext;
import net.osmand.plus.measurementtool.MeasurementToolFragment;
import net.osmand.plus.measurementtool.NewGpxData;
import net.osmand.plus.measurementtool.NewGpxData.ActionType;
import net.osmand.plus.measurementtool.GpxData;
import net.osmand.plus.measurementtool.GpxData.ActionType;
import net.osmand.plus.routepreparationmenu.RoutingOptionsHelper.LocalRoutingParameter;
import net.osmand.plus.routepreparationmenu.RoutingOptionsHelper.OtherLocalRoutingParameter;
import net.osmand.plus.routepreparationmenu.cards.AttachTrackToRoadsCard;
@ -480,10 +480,9 @@ public class FollowTrackFragment extends ContextMenuScrollFragment implements Ca
QuadRect rect = gpxFile.getRect();
TrkSegment segment = gpxFile.getNonEmptyTrkSegment();
ActionType actionType = segment == null ? ActionType.ADD_ROUTE_POINTS : ActionType.EDIT_SEGMENT;
NewGpxData newGpxData = new NewGpxData(gpxFile, rect, actionType, segment);
GpxData gpxData = new GpxData(gpxFile, rect, actionType, segment);
MeasurementEditingContext editingContext = new MeasurementEditingContext();
editingContext.setNewGpxData(newGpxData);
editingContext.setGpxData(gpxData);
if (useAppMode) {
editingContext.setAppMode(app.getRoutingHelper().getAppMode());
}