Implemented directions menu item (plan route)

This commit is contained in:
max-klaus 2020-08-29 21:48:27 +03:00
parent 1196ff2b2c
commit 52d9961914
4 changed files with 137 additions and 10 deletions

View file

@ -529,11 +529,16 @@ public class MapActivityActions implements DialogProvider {
public void enterRoutePlanningModeGivenGpx(GPXFile gpxFile, LatLon from, PointDescription fromName,
boolean useIntermediatePointsByDefault, boolean showMenu, int menuState) {
enterRoutePlanningModeGivenGpx(gpxFile, null, from, fromName, useIntermediatePointsByDefault, showMenu, menuState);
}
public void enterRoutePlanningModeGivenGpx(GPXFile gpxFile, ApplicationMode appMode, LatLon from, PointDescription fromName,
boolean useIntermediatePointsByDefault, boolean showMenu, int menuState) {
settings.USE_INTERMEDIATE_POINTS_NAVIGATION.set(useIntermediatePointsByDefault);
OsmandApplication app = mapActivity.getMyApplication();
TargetPointsHelper targets = app.getTargetPointsHelper();
ApplicationMode mode = getRouteMode(from);
ApplicationMode mode = appMode != null ? appMode : getRouteMode(from);
//app.getSettings().APPLICATION_MODE.set(mode);
app.getRoutingHelper().setAppMode(mode);
app.initVoiceCommandPlayer(mapActivity, mode, true, null, false, false, showMenu);
@ -1102,6 +1107,10 @@ public class MapActivityActions implements DialogProvider {
}
public AlertDialog stopNavigationActionConfirm() {
return stopNavigationActionConfirm(null);
}
public AlertDialog stopNavigationActionConfirm(final Runnable onStopAction) {
AlertDialog.Builder builder = new AlertDialog.Builder(mapActivity);
// Stop the navigation
builder.setTitle(getString(R.string.cancel_route));
@ -1110,6 +1119,9 @@ public class MapActivityActions implements DialogProvider {
@Override
public void onClick(DialogInterface dialog, int which) {
stopNavigationWithoutConfirm();
if (onStopAction != null) {
onStopAction.run();
}
}
});
builder.setNegativeButton(R.string.shared_string_no, null);

View file

@ -54,11 +54,14 @@ import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.TargetPointsHelper;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.Version;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.MapActivityActions;
import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.base.ContextMenuFragment.MenuState;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.measurementtool.GpxApproximationFragment.GpxApproximationFragmentListener;
import net.osmand.plus.measurementtool.GpxData.ActionType;
@ -80,7 +83,6 @@ import net.osmand.plus.measurementtool.command.ReorderPointCommand;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.views.controls.ReorderItemTouchHelperCallback;
import net.osmand.plus.views.layers.MapControlsLayer;
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController;
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControllerType;
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarView;
@ -99,7 +101,7 @@ import java.util.Locale;
import static net.osmand.IndexConstants.GPX_FILE_EXT;
import static net.osmand.plus.measurementtool.MeasurementEditingContext.CalculationMode;
import static net.osmand.plus.measurementtool.MeasurementEditingContext.SnapToRoadProgressListener;
import static net.osmand.plus.measurementtool.SaveAsNewTrackBottomSheetDialogFragment.*;
import static net.osmand.plus.measurementtool.SaveAsNewTrackBottomSheetDialogFragment.SaveAsNewTrackFragmentListener;
import static net.osmand.plus.measurementtool.SelectFileBottomSheet.Mode.ADD_TO_TRACK;
import static net.osmand.plus.measurementtool.SelectFileBottomSheet.Mode.OPEN_TRACK;
import static net.osmand.plus.measurementtool.SelectFileBottomSheet.SelectFileListener;
@ -139,6 +141,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
private boolean progressBarVisible;
private boolean pointsListOpened;
private boolean planRouteMode = false;
private boolean directionMode = false;
private boolean portrait;
private boolean nightMode;
private int cachedMapPosition;
@ -448,7 +451,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
}
public boolean isInEditMode() {
return !planRouteMode && !editingCtx.isNewData();
return !planRouteMode && !editingCtx.isNewData() && !directionMode;
}
public void setFileName(String fileName) {
@ -624,6 +627,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
switch (resultCode) {
case SnapTrackWarningBottomSheet.CANCEL_RESULT_CODE:
toolBarController.setSaveViewVisible(true);
directionMode = false;
updateToolbar();
break;
case SnapTrackWarningBottomSheet.CONTINUE_RESULT_CODE:
@ -656,13 +660,65 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
public void directionsOnClick() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
MapControlsLayer mapControlsLayer = mapActivity.getMapLayers().getMapControlsLayer();
if (mapControlsLayer != null) {
if (editingCtx.getPointsCount() > 0) {
mapControlsLayer.doRoute(false);
OsmandApplication app = mapActivity.getMyApplication();
MapActivityActions mapActions = mapActivity.getMapActions();
TargetPointsHelper targetPointsHelper = app.getTargetPointsHelper();
ApplicationMode appMode = editingCtx.getAppMode();
if (appMode == ApplicationMode.DEFAULT) {
appMode = null;
}
List<WptPt> points = editingCtx.getPoints();
if (points.size() > 0) {
if (points.size() == 1) {
targetPointsHelper.clearAllPoints(false);
targetPointsHelper.navigateToPoint(new LatLon(points.get(0).getLatitude(), points.get(0).getLongitude()), false, -1);
dismiss(mapActivity);
mapActions.enterRoutePlanningModeGivenGpx(null, appMode, null, null, true, true, MenuState.HEADER_ONLY);
} else {
Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show();
String trackName = getSuggestedFileName();
if (editingCtx.hasRoute()) {
GPXFile gpx = editingCtx.exportRouteAsGpx(trackName);
if (gpx != null) {
dismiss(mapActivity);
runNavigation(gpx, appMode);
} else {
Toast.makeText(mapActivity, getString(R.string.error_occurred_saving_gpx), Toast.LENGTH_SHORT).show();
}
} else {
if (editingCtx.isNewData() || editingCtx.hasRoutePoints()) {
GPXFile gpx = new GPXFile(Version.getFullVersion(requireMyApplication()));
gpx.addRoutePoints(points);
dismiss(mapActivity);
targetPointsHelper.clearAllPoints(false);
mapActions.enterRoutePlanningModeGivenGpx(gpx, appMode, null, null, true, true, MenuState.HEADER_ONLY);
} else {
directionMode = true;
SnapTrackWarningBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), this);
}
}
}
} else {
Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show();
}
}
}
private void runNavigation(final GPXFile gpx, final ApplicationMode appMode) {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (mapActivity.getMyApplication().getRoutingHelper().isFollowingMode()) {
mapActivity.getMapActions().stopNavigationActionConfirm(new Runnable() {
@Override
public void run() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
mapActivity.getMapActions().enterRoutePlanningModeGivenGpx(gpx, appMode, null, null, true, true, MenuState.HEADER_ONLY);
}
}
});
} else {
mapActivity.getMapActions().stopNavigationWithoutConfirm();
mapActivity.getMapActions().enterRoutePlanningModeGivenGpx(gpx, appMode, null, null, true, true, MenuState.HEADER_ONLY);
}
}
}
@ -1986,11 +2042,32 @@ public class MeasurementToolFragment extends BaseOsmAndFragment implements Route
@Override
public void onApplyGpxApproximation() {
doAddOrMovePointCommonStuff();
if (directionMode) {
directionMode = false;
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (editingCtx.hasRoute()) {
String trackName = getSuggestedFileName();
GPXFile gpx = editingCtx.exportRouteAsGpx(trackName);
if (gpx != null) {
ApplicationMode appMode = editingCtx.getAppMode();
dismiss(mapActivity);
runNavigation(gpx, appMode);
} else {
Toast.makeText(mapActivity, getString(R.string.error_occurred_saving_gpx), Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(mapActivity, getString(R.string.error_occurred_saving_gpx), Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onCancelGpxApproximation() {
editingCtx.getCommandManager().undo();
directionMode = false;
updateSnapToRoadControls();
updateToolbar();
}
}

View file

@ -118,6 +118,42 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm
items.add(new OptionsDividerItem(getContext()));
BaseBottomSheetItem directions = new SimpleBottomSheetItem.Builder()
.setIcon(getContentIcon(R.drawable.ic_action_gdirections_dark))
.setTitle(getString(R.string.get_directions))
.setLayoutId(R.layout.bottom_sheet_item_simple)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = getTargetFragment();
if (fragment instanceof OptionsFragmentListener) {
((OptionsFragmentListener) fragment).directionsOnClick();
}
dismiss();
}
})
.create();
items.add(directions);
/*BaseBottomSheetItem reverse = new SimpleBottomSheetItem.Builder()
.setIcon(getContentIcon(R.drawable.ic_action_reverse_direction))
.setTitle(getString(R.string.reverse_route))
.setLayoutId(R.layout.bottom_sheet_item_simple)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = getTargetFragment();
if (fragment instanceof OptionsFragmentListener) {
((OptionsFragmentListener) fragment).reverseRouteOnClick();
}
dismiss();
}
})
.create();
items.add(reverse);*/
items.add(new OptionsDividerItem(getContext()));
BaseBottomSheetItem clearAllItem = new SimpleBottomSheetItem.Builder()
.setIcon(getIcon(R.drawable.ic_action_reset_to_default_dark,
nightMode ? R.color.color_osm_edit_delete : R.color.color_osm_edit_delete))

View file

@ -29,6 +29,7 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment {
protected View mainView;
protected GpxTrackAdapter adapter;
private boolean continued = false;
@Override
public void createMenuItems(Bundle savedInstanceState) {
@ -54,6 +55,7 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment {
protected void onRightBottomButtonClick() {
Fragment fragment = getTargetFragment();
if (fragment != null) {
continued = true;
fragment.onActivityResult(REQUEST_CODE, CONTINUE_RESULT_CODE, null);
}
dismiss();
@ -72,7 +74,7 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment {
activity.findViewById(R.id.snap_to_road_image_button).setVisibility(View.VISIBLE);
}
Fragment fragment = getTargetFragment();
if (fragment != null) {
if (fragment != null && !continued) {
fragment.onActivityResult(REQUEST_CODE, CANCEL_RESULT_CODE, null);
}
}