diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index c67aaab288..4c65c2ff3d 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -97,6 +97,7 @@ import net.osmand.plus.mapcontextmenu.other.DestinationReachedMenu; import net.osmand.plus.mapcontextmenu.other.MapRouteInfoMenu; import net.osmand.plus.mapcontextmenu.other.MapRouteInfoMenuFragment; import net.osmand.plus.mapcontextmenu.other.TrackDetailsMenu; +import net.osmand.plus.measurementtool.MeasurementToolFragment; import net.osmand.plus.render.RendererRegistry; import net.osmand.plus.resources.ResourceManager; import net.osmand.plus.routing.RoutingHelper; @@ -500,6 +501,10 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven return; } } + if (getMeasurementToolFragment() != null) { + getMeasurementToolFragment().showQuitDialog(); + return; + } if (prevActivityIntent != null && getSupportFragmentManager().getBackStackEntryCount() == 0) { prevActivityIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); LatLon loc = getMapLocation(); @@ -1675,6 +1680,11 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven return fragment!= null && !fragment.isDetached() && !fragment.isRemoving() ? (QuickSearchDialogFragment) fragment : null; } + public MeasurementToolFragment getMeasurementToolFragment() { + Fragment fragment = getSupportFragmentManager().findFragmentByTag(MeasurementToolFragment.TAG); + return fragment != null && !fragment.isDetached() && !fragment.isRemoving() ? (MeasurementToolFragment) fragment : null; + } + public boolean isTopToolbarActive() { MapInfoLayer mapInfoLayer = getMapLayers().getMapInfoLayer(); return mapInfoLayer.hasTopToolbar(); diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index 4f419af00e..52b63cc52b 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -7,6 +7,7 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.widget.SwitchCompat; @@ -71,7 +72,7 @@ public class MeasurementToolFragment extends Fragment { nightMode ? R.color.ctx_menu_info_view_bg_dark : R.color.ctx_menu_info_view_bg_light); boolean portrait = AndroidUiHelper.isOrientationPortrait(mapActivity); - pointsSt = mapActivity.getString(R.string.points).toLowerCase(); + pointsSt = getString(R.string.points).toLowerCase(); View view = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_measurement_tool, null); @@ -145,11 +146,11 @@ public class MeasurementToolFragment extends Fragment { if (portrait) { toolBarController = new MeasurementToolBarController(); - toolBarController.setTitle(mapActivity.getString(R.string.measurement_tool_action_bar)); + toolBarController.setTitle(getString(R.string.measurement_tool_action_bar)); toolBarController.setOnBackButtonClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - mapActivity.onBackPressed(); + showQuitDialog(); } }); toolBarController.setOnCloseButtonClickListener(new View.OnClickListener() { @@ -169,7 +170,7 @@ public class MeasurementToolFragment extends Fragment { if (measurementLayer.getPointsCount() > 0) { saveAsGpxOnClick(mapActivity); } else { - Toast.makeText(mapActivity, mapActivity.getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); + Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); } return true; case R.id.action_clear_all: @@ -279,7 +280,7 @@ public class MeasurementToolFragment extends Fragment { MapActivity activity = getMapActivity(); if (activity != null) { progressDialog = new ProgressDialog(activity); - progressDialog.setMessage(activity.getString(R.string.saving_gpx_tracks)); + progressDialog.setMessage(getString(R.string.saving_gpx_tracks)); progressDialog.show(); } } @@ -301,7 +302,6 @@ public class MeasurementToolFragment extends Fragment { } MapActivity activity = getMapActivity(); if (activity != null) { - // todo String res = GPXUtilities.writeGpxFile(toSave, gpx, activity.getMyApplication()); gpx.path = toSave.getAbsolutePath(); if (showOnMap) { @@ -318,7 +318,7 @@ public class MeasurementToolFragment extends Fragment { if (activity != null) { if (warning == null) { Toast.makeText(activity, - MessageFormat.format(activity.getString(R.string.gpx_saved_sucessfully), toSave.getAbsolutePath()), + MessageFormat.format(getString(R.string.gpx_saved_sucessfully), toSave.getAbsolutePath()), Toast.LENGTH_LONG).show(); } else { Toast.makeText(activity, warning, Toast.LENGTH_LONG).show(); @@ -430,6 +430,36 @@ public class MeasurementToolFragment extends Fragment { } } + public void showQuitDialog() { + final MapActivity mapActivity = getMapActivity(); + MeasurementToolLayer measurementLayer = getMeasurementLayer(); + if (mapActivity != null && measurementLayer != null) { + if (measurementLayer.getPointsCount() < 1) { + dismiss(mapActivity); + return; + } + new AlertDialog.Builder(mapActivity) + .setTitle(getString(R.string.are_you_sure)) + .setMessage(getString(R.string.unsaved_changes_will_be_lost)) + .setPositiveButton(R.string.shared_string_ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dismiss(mapActivity); + } + }) + .setNegativeButton(R.string.shared_string_cancel, null) + .show(); + } + } + + private void dismiss(MapActivity mapActivity) { + try { + mapActivity.getSupportFragmentManager().popBackStackImmediate(TAG, FragmentManager.POP_BACK_STACK_INCLUSIVE); + } catch (Exception e) { + // ignore + } + } + private static class MeasurementToolBarController extends TopToolbarController { MeasurementToolBarController() { diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolLayer.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolLayer.java index d9217340a2..1ea5208cc6 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolLayer.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolLayer.java @@ -160,8 +160,10 @@ public class MeasurementToolLayer extends OsmandMapLayer implements ContextMenuL boolean undoPointOnClick() { measurementPoints.remove(measurementPoints.size() - 1); - WptPt pt = measurementPoints.get(measurementPoints.size() - 1); - view.getAnimatedDraggingThread().startMoving(pt.getLatitude(), pt.getLongitude(), view.getZoom(), true); + if (measurementPoints.size() > 0) { + WptPt pt = measurementPoints.get(measurementPoints.size() - 1); + view.getAnimatedDraggingThread().startMoving(pt.getLatitude(), pt.getLongitude(), view.getZoom(), true); + } return measurementPoints.size() > 0; }