From b42f00554aec4212e5bb1901dba48e7731ad8a97 Mon Sep 17 00:00:00 2001 From: Chumva Date: Fri, 15 Jun 2018 18:15:49 +0300 Subject: [PATCH] add save as track function to coordinate input --- ...rker_save_as_track_bottom_sheet_dialog.xml | 1 + OsmAnd/res/values/strings.xml | 2 + .../src/net/osmand/plus/MapMarkersHelper.java | 23 +++++++++ ...rdinateInputBottomSheetDialogFragment.java | 22 +++++++++ .../CoordinateInputDialogFragment.java | 49 +++++++++++++++++++ .../SaveAsTrackBottomSheetDialogFragment.java | 13 +++++ 6 files changed, 110 insertions(+) diff --git a/OsmAnd/res/layout/fragment_marker_save_as_track_bottom_sheet_dialog.xml b/OsmAnd/res/layout/fragment_marker_save_as_track_bottom_sheet_dialog.xml index 6cdf00ddf6..7015872187 100644 --- a/OsmAnd/res/layout/fragment_marker_save_as_track_bottom_sheet_dialog.xml +++ b/OsmAnd/res/layout/fragment_marker_save_as_track_bottom_sheet_dialog.xml @@ -32,6 +32,7 @@ osmand:typeface="@string/font_roboto_medium"/> + Save as track + You added %1$s points. Enter the name of the file and click save. Please send screenshoot of this notification to support@osmand.net Edit actions Get OsmAnd Live to unlock all features: Daily map updates with unlimited downloads, all paid and free plugins, Wikipedia, Wikivoyage and much more. diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 6258455773..f76351fecd 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -1016,6 +1016,29 @@ public class MapMarkersHelper { GPXUtilities.writeGpxFile(fout, file, ctx); return fout.getAbsolutePath(); } + + public String generateGpxFromList(String fileName, List mapMarkers) { + final File dir = ctx.getAppPath(IndexConstants.GPX_INDEX_DIR + "/map markers"); + if (!dir.exists()) { + dir.mkdirs(); + } + File fout = new File(dir, fileName + ".gpx"); + int ind = 1; + while (fout.exists()) { + fout = new File(dir, fileName + "_" + (++ind) + ".gpx"); + } + GPXFile file = new GPXFile(); + for (MapMarker marker : mapMarkers) { + WptPt wpt = new WptPt(); + wpt.lat = marker.getLatitude(); + wpt.lon = marker.getLongitude(); + wpt.setColor(ctx.getResources().getColor(MapMarker.getColorId(marker.colorIndex))); + wpt.name = marker.getOnlyName(); + file.addPoint(wpt); + } + GPXUtilities.writeGpxFile(fout, file, ctx); + return fout.getAbsolutePath(); + } // --------------------------------------------------------------------------------------------- diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputBottomSheetDialogFragment.java index 0b6ea87416..a1b1d669ea 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputBottomSheetDialogFragment.java @@ -12,6 +12,8 @@ import net.osmand.plus.base.MenuBottomSheetDialogFragment; import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithCompoundButton; import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription; +import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem; +import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerHalfItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.SubtitleDividerItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.SubtitleItem; import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem; @@ -34,6 +36,24 @@ public class CoordinateInputBottomSheetDialogFragment extends MenuBottomSheetDia final OsmandSettings settings = getMyApplication().getSettings(); items.add(new TitleItem(getString(R.string.shared_string_options))); + BaseBottomSheetItem editItem = new SimpleBottomSheetItem.Builder() + .setIcon(getContentIcon(R.drawable.ic_action_save_to_file)) + .setTitle(getString(R.string.coord_input_save_as_track)) + .setLayoutId(R.layout.bottom_sheet_item_simple) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (listener != null) { + listener.saveAsTrack(); + } + dismiss(); + } + }) + .create(); + items.add(editItem); + + items.add(new DividerHalfItem(context)); + boolean useOsmandKeyboard = settings.COORDS_INPUT_USE_OSMAND_KEYBOARD.get(); BaseBottomSheetItem useSystemKeyboardItem = new BottomSheetItemWithCompoundButton.Builder() @@ -148,5 +168,7 @@ public class CoordinateInputBottomSheetDialogFragment extends MenuBottomSheetDia void onHandChanged(); void onInputSettingsChanged(); + + void saveAsTrack(); } } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java index aaf36d68e6..ef21dae289 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/CoordinateInputDialogFragment.java @@ -4,6 +4,7 @@ import android.app.Dialog; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; +import android.content.Intent; import android.content.res.ColorStateList; import android.graphics.Rect; import android.graphics.drawable.Drawable; @@ -16,6 +17,7 @@ import android.support.annotation.DrawableRes; import android.support.annotation.IdRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.design.widget.Snackbar; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; @@ -59,6 +61,7 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.activities.TrackActivity; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.mapmarkers.CoordinateInputBottomSheetDialogFragment.CoordinateInputFormatChangeListener; import net.osmand.plus.mapmarkers.CoordinateInputFormats.DDM; @@ -82,6 +85,7 @@ import static net.osmand.plus.MapMarkersHelper.MAP_MARKERS_COLORS_COUNT; public class CoordinateInputDialogFragment extends DialogFragment implements OsmAndCompassListener, OsmAndLocationListener { public static final String TAG = "CoordinateInputDialogFragment"; + public static final String ADDED_MARKERS_NUMBER_KEY = "added_markers_number_key"; private final List mapMarkers = new ArrayList<>(); private MapMarker selectedMarker; @@ -91,6 +95,7 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm private final List editTexts = new ArrayList<>(); private CoordinateInputAdapter adapter; private ImageView showHideKeyboardIcon; + private Snackbar snackbar; private boolean lightTheme; private boolean orientationPortrait; @@ -900,6 +905,50 @@ public class CoordinateInputDialogFragment extends DialogFragment implements Osm dismissEditingMode(); registerInputs(); } + + @Override + public void saveAsTrack() { + OsmandApplication app = getMyApplication(); + if (app != null) { + if (mapMarkers.isEmpty()) { + Toast.makeText(app, getString(R.string.plan_route_no_markers_toast), Toast.LENGTH_SHORT).show(); + } else { + SaveAsTrackBottomSheetDialogFragment fragment = new SaveAsTrackBottomSheetDialogFragment(); + Bundle args = new Bundle(); + args.putInt(ADDED_MARKERS_NUMBER_KEY, mapMarkers.size()); + fragment.setArguments(args); + fragment.setListener(createSaveAsTrackFragmentListener()); + fragment.show(getChildFragmentManager(), SaveAsTrackBottomSheetDialogFragment.TAG); + } + } + + } + }; + } + + private SaveAsTrackBottomSheetDialogFragment.MarkerSaveAsTrackFragmentListener createSaveAsTrackFragmentListener() { + return new SaveAsTrackBottomSheetDialogFragment.MarkerSaveAsTrackFragmentListener() { + + final OsmandApplication app = getMyApplication(); + + @Override + public void saveGpx(final String fileName) { + final String gpxPath = app.getMapMarkersHelper().generateGpxFromList(fileName, mapMarkers); + + snackbar = Snackbar.make(mainView, fileName + " " + getString(R.string.is_saved) + ".", Snackbar.LENGTH_LONG) + .setAction(R.string.shared_string_show, new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(app, getMyApplication().getAppCustomization().getTrackActivity()); + intent.putExtra(TrackActivity.TRACK_FILE_NAME, gpxPath); + intent.putExtra(TrackActivity.OPEN_POINTS_TAB, true); + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(intent); + } + }); + AndroidUtils.setSnackbarTextColor(snackbar, R.color.color_dialog_buttons_dark); + snackbar.show(); + } }; } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/SaveAsTrackBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/SaveAsTrackBottomSheetDialogFragment.java index c2ca92ee08..1c8a143c82 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/SaveAsTrackBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/SaveAsTrackBottomSheetDialogFragment.java @@ -17,6 +17,7 @@ import android.view.Window; import android.view.WindowManager; import android.widget.EditText; import android.widget.LinearLayout; +import android.widget.TextView; import net.osmand.AndroidUtils; import net.osmand.IndexConstants; @@ -30,6 +31,7 @@ import java.io.File; import java.util.Date; import static net.osmand.plus.helpers.ImportHelper.GPX_SUFFIX; +import static net.osmand.plus.mapmarkers.CoordinateInputDialogFragment.ADDED_MARKERS_NUMBER_KEY; public class SaveAsTrackBottomSheetDialogFragment extends BottomSheetDialogFragment { @@ -45,6 +47,13 @@ public class SaveAsTrackBottomSheetDialogFragment extends BottomSheetDialogFragm @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + boolean isCoordInput = false; + int number = 0; + if (getArguments() != null) { + number = getArguments().getInt(ADDED_MARKERS_NUMBER_KEY); + if (number != 0) + isCoordInput = true; + } MapActivity mapActivity = (MapActivity) getActivity(); portrait = AndroidUiHelper.isOrientationPortrait(getActivity()); final boolean nightMode = !getMyApplication().getSettings().isLightContent(); @@ -52,6 +61,10 @@ public class SaveAsTrackBottomSheetDialogFragment extends BottomSheetDialogFragm final View mainView = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_marker_save_as_track_bottom_sheet_dialog, container); LinearLayout contentLayout = (LinearLayout) mainView.findViewById(R.id.content_linear_layout); + TextView titleTv = (TextView) mainView.findViewById(R.id.save_as_track_title); + titleTv.setText(isCoordInput ? R.string.coord_input_save_as_track : R.string.marker_save_as_track); + TextView descriptionTv = (TextView) mainView.findViewById(R.id.save_as_track_description); + descriptionTv.setText(isCoordInput ? getString(R.string.coord_input_save_as_track_descr, number) : getString(R.string.marker_save_as_track_descr)); int layoutRes; if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH) { layoutRes = R.layout.markers_track_name_text_field_box;