diff --git a/OsmAnd/src/net/osmand/plus/base/ContextMenuScrollFragment.java b/OsmAnd/src/net/osmand/plus/base/ContextMenuScrollFragment.java index 5884316361..0526885739 100644 --- a/OsmAnd/src/net/osmand/plus/base/ContextMenuScrollFragment.java +++ b/OsmAnd/src/net/osmand/plus/base/ContextMenuScrollFragment.java @@ -140,13 +140,13 @@ public abstract class ContextMenuScrollFragment extends ContextMenuFragment impl return mapControlsContainer != null ? mapControlsContainer.getHeight() : 0; } - public int getMapControlsVisibleMenuState() { - return MenuState.HEADER_ONLY; + public boolean shouldShowMapControls(int menuState) { + return menuState == MenuState.HEADER_ONLY; } private void updateMapControlsVisibility(int menuState) { if (mapBottomHudButtons != null) { - if ((menuState & getMapControlsVisibleMenuState()) != 0) { + if (shouldShowMapControls(menuState)) { if (mapBottomHudButtons.getVisibility() != View.VISIBLE) { mapBottomHudButtons.setVisibility(View.VISIBLE); } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/GpxApproximationFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/GpxApproximationFragment.java index 176bb48b88..020ee63b8e 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/GpxApproximationFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/GpxApproximationFragment.java @@ -10,38 +10,33 @@ import android.widget.LinearLayout; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; import net.osmand.AndroidUtils; import net.osmand.PlatformUtil; -import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.UiUtilities; import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.base.ContextMenuFragment; import net.osmand.plus.base.ContextMenuScrollFragment; import net.osmand.plus.helpers.AndroidUiHelper; -import net.osmand.plus.mapcontextmenu.MenuController; import net.osmand.plus.settings.backend.ApplicationMode; import org.apache.commons.logging.Log; -public class GpxApproximationFragment extends ContextMenuScrollFragment { +import static net.osmand.plus.measurementtool.ProfileCard.*; +import static net.osmand.plus.measurementtool.SliderCard.*; + +public class GpxApproximationFragment extends ContextMenuScrollFragment implements SliderCardListener, ProfileCardListener { private static final Log LOG = PlatformUtil.getLog(GpxApproximationFragment.class); public static final String TAG = GpxApproximationFragment.class.getSimpleName(); - private GpxApproximationFragmentListener listener; private int menuTitleHeight; private ApplicationMode snapToRoadAppMode; private int distanceThreshold; - public void setListener(GpxApproximationFragmentListener listener) { - this.listener = listener; - } - - @Override public int getMainLayoutId() { return R.layout.fragment_gpx_approximation_bottom_sheet_dialog; @@ -106,11 +101,12 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment { @Override public void onDestroyView() { - if (listener != null) { - listener.onDestroyView(); - } super.onDestroyView(); exitGpxApproximationMode(); + Fragment fragment = getTargetFragment(); + if (fragment instanceof GpxApproximationFragmentListener) { + ((GpxApproximationFragmentListener) fragment).cancelButtonOnClick(); + } } private void updateCardsLayout() { @@ -137,11 +133,14 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment { private void updateButtons(View view) { View buttonsContainer = view.findViewById(R.id.buttons_container); buttonsContainer.setBackgroundColor(AndroidUtils.getColorFromAttr(view.getContext(), R.attr.route_info_bg)); - View saveButton = view.findViewById(R.id.right_bottom_button); - saveButton.setOnClickListener(new View.OnClickListener() { + View applyButton = view.findViewById(R.id.right_bottom_button); + applyButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { -// saveTrackInfo(); + Fragment fragment = getTargetFragment(); + if (fragment instanceof GpxApproximationFragmentListener) { + ((GpxApproximationFragmentListener) fragment).applyButtonOnClick(snapToRoadAppMode, distanceThreshold); + } dismiss(); } }); @@ -150,7 +149,6 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment { cancelButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { -// discardSplitChanges(); FragmentActivity activity = getActivity(); if (activity != null) { activity.onBackPressed(); @@ -159,9 +157,9 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment { }); UiUtilities.setupDialogButton(isNightMode(), cancelButton, UiUtilities.DialogButtonType.SECONDARY, R.string.shared_string_cancel); - UiUtilities.setupDialogButton(isNightMode(), saveButton, UiUtilities.DialogButtonType.PRIMARY, R.string.shared_string_apply); + UiUtilities.setupDialogButton(isNightMode(), applyButton, UiUtilities.DialogButtonType.PRIMARY, R.string.shared_string_apply); - AndroidUiHelper.updateVisibility(saveButton, true); + AndroidUiHelper.updateVisibility(applyButton, true); AndroidUiHelper.updateVisibility(view.findViewById(R.id.buttons_divider), true); } @@ -172,9 +170,12 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment { cardsContainer.removeAllViews(); SliderCard sliderCard = new SliderCard(mapActivity); + sliderCard.setListener(this); cardsContainer.addView(sliderCard.build(mapActivity)); + ProfileCard profileCard = new ProfileCard(mapActivity); + profileCard.setListener(this); cardsContainer.addView(profileCard.build(mapActivity)); } } @@ -202,21 +203,15 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment { } @Override - public int getMapControlsVisibleMenuState() { - return MenuState.HEADER_ONLY | MenuState.HALF_SCREEN; + public boolean shouldShowMapControls(int menuState) { + return (menuState & (MenuState.HEADER_ONLY | MenuState.HALF_SCREEN)) != 0; } - // public static void showInstance(FragmentManager fm, RouteBetweenPointsFragmentListener listener, -// CalculationType calculationType, ApplicationMode applicationMode) { - public static void showInstance(FragmentManager fm) { + public static void showInstance(FragmentManager fm, Fragment targetFragment) { try { if (!fm.isStateSaved()) { GpxApproximationFragment fragment = new GpxApproximationFragment(); -// fragment.setListener(listener); - fragment.setRetainInstance(true); - Bundle args = new Bundle(); - args.putInt(ContextMenuFragment.MENU_STATE_KEY, MenuController.MenuState.HALF_SCREEN); - fragment.setArguments(args); + fragment.setTargetFragment(targetFragment, 0); fm.beginTransaction() .replace(R.id.fragmentContainer, fragment, TAG) .addToBackStack(TAG) @@ -238,10 +233,30 @@ public class GpxApproximationFragment extends ContextMenuScrollFragment { } } + @Override + public void onSliderChange(int sliderValue) { + Fragment fragment = getTargetFragment(); + if (fragment instanceof GpxApproximationFragmentListener) { + ((GpxApproximationFragmentListener) fragment).onParametersChanged(snapToRoadAppMode, sliderValue); + distanceThreshold = sliderValue; + } + } + + @Override + public void onProfileSelect(ApplicationMode applicationMode) { + Fragment fragment = getTargetFragment(); + if (fragment instanceof GpxApproximationFragmentListener) { + ((GpxApproximationFragmentListener) fragment).onParametersChanged(applicationMode, distanceThreshold); + snapToRoadAppMode = applicationMode; + } + } + public interface GpxApproximationFragmentListener { - void onDestroyView(); + void onParametersChanged(ApplicationMode mode, int distanceThreshold); void applyButtonOnClick(ApplicationMode mode, int distanceThreshold); + + void cancelButtonOnClick(); } } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java index 297c01d102..78bbb88549 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/MeasurementToolFragment.java @@ -90,6 +90,7 @@ import java.util.List; import java.util.Locale; import static net.osmand.IndexConstants.GPX_FILE_EXT; +import static net.osmand.plus.measurementtool.GpxApproximationFragment.*; import static net.osmand.plus.measurementtool.MeasurementEditingContext.CalculationType; import static net.osmand.plus.measurementtool.MeasurementEditingContext.ExportAsGpxListener; import static net.osmand.plus.measurementtool.MeasurementEditingContext.SnapToRoadProgressListener; @@ -99,7 +100,9 @@ import static net.osmand.plus.measurementtool.SelectFileBottomSheet.SelectFileLi import static net.osmand.plus.measurementtool.SnapTrackWarningBottomSheet.SnapTrackWarningListener; import static net.osmand.plus.measurementtool.StartPlanRouteBottomSheet.StartPlanRouteListener; -public class MeasurementToolFragment extends BaseOsmAndFragment { +public class MeasurementToolFragment extends BaseOsmAndFragment + implements RouteBetweenPointsFragmentListener, GpxApproximationFragmentListener, OptionsFragmentListener, + SnapTrackWarningListener { public static final String TAG = MeasurementToolFragment.class.getSimpleName(); @@ -126,7 +129,6 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { private boolean progressBarVisible; private boolean pointsListOpened; private boolean planRouteMode = false; - private boolean firstShow = true; private Boolean saved; private boolean portrait; private boolean nightMode; @@ -156,7 +158,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { @Nullable @Override - public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { final MapActivity mapActivity = (MapActivity) getActivity(); final MeasurementToolLayer measurementLayer = mapActivity.getMapLayers().getMeasurementToolLayer(); @@ -211,7 +214,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { downIcon = getContentIcon(R.drawable.ic_action_arrow_down); pointsSt = getString(R.string.shared_string_gpx_points).toLowerCase(); - View view = UiUtilities.getInflater(getContext(), nightMode).inflate(R.layout.fragment_measurement_tool, null); + View view = UiUtilities.getInflater(getContext(), nightMode) + .inflate(R.layout.fragment_measurement_tool, container, false); mainView = view.findViewById(R.id.main_view); AndroidUtils.setBackground(mapActivity, mainView, nightMode, R.drawable.bg_bottom_menu_light, R.drawable.bg_bottom_menu_dark); @@ -284,9 +288,12 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { @Override public void onClick(View view) { OptionsBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(), + MeasurementToolFragment.this, editingCtx.isSnapToRoadTrack() || editingCtx.isNewData(), editingCtx.isInSnapToRoadMode(), - editingCtx.getSnapToRoadAppMode(), createOptionsFragmentListener() + editingCtx.getSnapToRoadAppMode() != null + ? editingCtx.getSnapToRoadAppMode().getStringKey() + : null ); } }); @@ -462,10 +469,9 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { initMeasurementMode(newGpxData); - if (planRouteMode && firstShow) { + if (planRouteMode && savedInstanceState == null) { StartPlanRouteBottomSheet.showInstance(mapActivity.getSupportFragmentManager(), createStartPlanRouteListener()); - firstShow = false; } return view; } @@ -473,9 +479,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { private void initMeasurementMode(NewGpxData newGpxData) { editingCtx.getCommandManager().resetMeasurementLayer(getMapActivity().getMapLayers().getMeasurementToolLayer()); enterMeasurementMode(); - showSnapToRoadControls(); - if (newGpxData != null && !gpxPointsAdded) { List points = newGpxData.getGpxFile().getRoutePoints(); if (!points.isEmpty()) { @@ -494,9 +498,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { gpxPointsAdded = true; } } - if (saved == null) { - saved = newGpxData != null && (newGpxData.getActionType() == ActionType.ADD_ROUTE_POINTS || newGpxData.getActionType() == ActionType.EDIT_SEGMENT); + saved = newGpxData != null + && (newGpxData.getActionType() == ActionType.ADD_ROUTE_POINTS + || newGpxData.getActionType() == ActionType.EDIT_SEGMENT); } } @@ -539,6 +544,21 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { return R.color.status_bar_transparent_gradient; } + @Override + public void onParametersChanged(ApplicationMode mode, int distanceThreshold) { + + } + + @Override + public void applyButtonOnClick(ApplicationMode mode, int distanceThreshold) { + + } + + @Override + public void cancelButtonOnClick() { + + } + @Nullable private MapActivity getMapActivity() { Activity activity = getActivity(); @@ -596,116 +616,110 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { toolBarController.setTitle(getString(R.string.route_between_points)); mapActivity.refreshMap(); RouteBetweenPointsBottomSheetDialogFragment.showInstance(mapActivity.getSupportFragmentManager(), - createRouteBetweenPointsFragmentListener(), editingCtx.getCalculationType(), + this, editingCtx.getCalculationType(), editingCtx.getSnapToRoadAppMode()); } } - private SnapTrackWarningListener createSnapTrackWarningListener() { - return new SnapTrackWarningListener() { - @Override - public void continueButtonOnClick() { - MapActivity mapActivity = getMapActivity(); - if (mapActivity != null) { + @Override + public void continueButtonOnClick() { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { - GpxApproximationFragment.showInstance(mapActivity.getSupportFragmentManager()); - } - } - - @Override - public void dismissButtonOnClick() { - toolBarController.setSaveViewVisible(true); - updateToolbar(); - } - }; + GpxApproximationFragment.showInstance(mapActivity.getSupportFragmentManager(), + MeasurementToolFragment.this); + } } - private OptionsFragmentListener createOptionsFragmentListener() { - return new OptionsFragmentListener() { + @Override + public void dismissButtonOnClick() { + toolBarController.setSaveViewVisible(true); + updateToolbar(); + } - final MapActivity mapActivity = getMapActivity(); - final MeasurementToolLayer measurementLayer = getMeasurementLayer(); + @Override + public void snapToRoadOnCLick() { + showRouteBetweenPointsMenu(true); + } - @Override - public void snapToRoadOnCLick() { - showRouteBetweenPointsMenu(true); - } - - @Override - public void directionsOnClick() { - if (mapActivity != null) { - MapControlsLayer mapControlsLayer = mapActivity.getMapLayers().getMapControlsLayer(); - if (mapControlsLayer != null) { - if (editingCtx.getPointsCount() > 0) { - mapControlsLayer.doRoute(false); - }else{ - Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); - } - } + @Override + public void directionsOnClick() { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + MapControlsLayer mapControlsLayer = mapActivity.getMapLayers().getMapControlsLayer(); + if (mapControlsLayer != null) { + if (editingCtx.getPointsCount() > 0) { + mapControlsLayer.doRoute(false); + } else { + Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); } } + } + } - @Override - public void addToGpxOnClick() { - if (mapActivity != null && measurementLayer != null) { - if (editingCtx.getPointsCount() > 0) { - if (editingCtx.isInSnapToRoadMode()) { - 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(); - } + @Override + public void addToGpxOnClick() { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + if (editingCtx.getPointsCount() > 0) { + if (editingCtx.isInSnapToRoadMode()) { + editingCtx.getPoints().clear(); + editingCtx.getPoints().addAll(editingCtx.getBeforePoints()); + editingCtx.getBeforePoints().clear(); + editingCtx.getBeforePoints().addAll(editingCtx.getBeforeTrkSegmentLine().points); } - } - - @Override - public void saveAsNewTrackOnClick() { - if (mapActivity != null && measurementLayer != null) { - if (editingCtx.getPointsCount() > 0) { - openSaveAsNewTrackMenu(mapActivity); - } else { - Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); - } + if (editingCtx.isNewData()) { + saveAsGpx(SaveType.ROUTE_POINT); + } else { + addToGpx(mapActivity); } + } else { + Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); } + } + } - @Override - public void addToTheTrackOnClick() { - if (mapActivity != null && measurementLayer != null) { - if (editingCtx.getPointsCount() > 0) { - showAddToTrackDialog(mapActivity); - } else { - Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); - } - } + @Override + public void saveAsNewTrackOnClick() { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + if (editingCtx.getPointsCount() > 0) { + openSaveAsNewTrackMenu(mapActivity); + } else { + Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); } + } + } - @Override - public void clearAllOnClick() { - editingCtx.getCommandManager().execute(new ClearPointsCommand(measurementLayer)); - editingCtx.cancelSnapToRoad(); - if (pointsListOpened) { - hidePointsList(); - } - updateUndoRedoButton(false, redoBtn); - disable(upDownBtn); - updateDistancePointsText(); - saved = false; + @Override + public void addToTheTrackOnClick() { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + if (editingCtx.getPointsCount() > 0) { + showAddToTrackDialog(mapActivity); + } else { + Toast.makeText(mapActivity, getString(R.string.none_point_error), Toast.LENGTH_SHORT).show(); } + } + } - @Override - public void reverseRouteOnClick() { + @Override + public void clearAllOnClick() { + MeasurementToolLayer measurementLayer = getMeasurementLayer(); + editingCtx.getCommandManager().execute(new ClearPointsCommand(measurementLayer)); + editingCtx.cancelSnapToRoad(); + if (pointsListOpened) { + hidePointsList(); + } + updateUndoRedoButton(false, redoBtn); + disable(upDownBtn); + updateDistancePointsText(); + saved = false; + } + + @Override + public void reverseRouteOnClick() { - } - }; } private SelectedPointFragmentListener createSelectedPointFragmentListener() { @@ -765,35 +779,31 @@ public class MeasurementToolFragment extends BaseOsmAndFragment { }; } - private RouteBetweenPointsFragmentListener createRouteBetweenPointsFragmentListener() { - return new RouteBetweenPointsFragmentListener() { - @Override - public void onDestroyView(boolean snapToRoadEnabled) { - if (!snapToRoadEnabled && !editingCtx.isInSnapToRoadMode()) { - toolBarController.setTitle(previousToolBarTitle); - MapActivity mapActivity = getMapActivity(); - if (mapActivity != null) { - mapActivity.refreshMap(); - } - } + @Override + public void onDestroyView(boolean snapToRoadEnabled) { + if (!snapToRoadEnabled && !editingCtx.isInSnapToRoadMode()) { + toolBarController.setTitle(previousToolBarTitle); + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null) { + mapActivity.refreshMap(); } + } + } - @Override - public void onApplicationModeItemClick(ApplicationMode mode) { - if (mode == null) { - disableSnapToRoadMode(); - editingCtx.setSnapToRoadAppMode(null); - showSnapToRoadControls(); - } else { - enableSnapToRoadMode(mode); - } - } + @Override + public void onApplicationModeItemClick(ApplicationMode mode) { + if (mode == null) { + disableSnapToRoadMode(); + editingCtx.setSnapToRoadAppMode(null); + showSnapToRoadControls(); + } else { + enableSnapToRoadMode(mode); + } + } - @Override - public void onCalculationTypeChanges(CalculationType calculationType) { - editingCtx.setCalculationType(calculationType); - } - }; + @Override + public void onCalculationTypeChanges(CalculationType calculationType) { + editingCtx.setCalculationType(calculationType); } private StartPlanRouteListener createStartPlanRouteListener() { diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/OptionsBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/OptionsBottomSheetDialogFragment.java index 1559a97b86..ad28e59754 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/OptionsBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/OptionsBottomSheetDialogFragment.java @@ -8,6 +8,7 @@ import android.widget.ImageView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import net.osmand.PlatformUtil; @@ -28,18 +29,10 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm public static final String SNAP_TO_ROAD_ENABLED_KEY = "snap_to_road_enabled"; public static final String TRACK_SNAPPED_TO_ROAD_KEY = "track_snapped_to_road"; + public static final String SNAP_TO_ROAD_APP_MODE_KEY = "snap_to_road_app_mode"; - private OptionsFragmentListener listener; private ApplicationMode routeAppMode; - public void setListener(OptionsFragmentListener listener) { - this.listener = listener; - } - - private void setRouteAppMode(ApplicationMode routeAppMode) { - this.routeAppMode = routeAppMode; - } - @Override public void createMenuItems(Bundle savedInstanceState) { Bundle args = getArguments(); @@ -48,6 +41,7 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm if (args != null) { snapToRoadEnabled = args.getBoolean(SNAP_TO_ROAD_ENABLED_KEY); trackSnappedToRoad = args.getBoolean(TRACK_SNAPPED_TO_ROAD_KEY); + routeAppMode = ApplicationMode.valueOfStringKey(args.getString(SNAP_TO_ROAD_APP_MODE_KEY), null); } items.add(new TitleItem(getString(R.string.shared_string_options))); @@ -75,8 +69,9 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (listener != null) { - listener.snapToRoadOnCLick(); + Fragment fragment = getTargetFragment(); + if (fragment instanceof OptionsFragmentListener) { + ((OptionsFragmentListener) fragment).snapToRoadOnCLick(); } dismiss(); } @@ -94,8 +89,9 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (listener != null) { - listener.addToGpxOnClick(); + Fragment fragment = getTargetFragment(); + if (fragment instanceof OptionsFragmentListener) { + ((OptionsFragmentListener) fragment).addToGpxOnClick(); } dismiss(); } @@ -112,8 +108,9 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (listener != null) { - listener.addToTheTrackOnClick(); + Fragment fragment = getTargetFragment(); + if (fragment instanceof OptionsFragmentListener) { + ((OptionsFragmentListener) fragment).addToTheTrackOnClick(); } dismiss(); } @@ -131,8 +128,9 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (listener != null) { - listener.clearAllOnClick(); + Fragment fragment = getTargetFragment(); + if (fragment instanceof OptionsFragmentListener) { + ((OptionsFragmentListener) fragment).clearAllOnClick(); } dismiss(); } @@ -149,8 +147,9 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm .setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (listener != null) { - listener.saveAsNewTrackOnClick(); + Fragment fragment = getTargetFragment(); + if (fragment instanceof OptionsFragmentListener) { + ((OptionsFragmentListener) fragment).saveAsNewTrackOnClick(); } dismiss(); } @@ -166,18 +165,17 @@ public class OptionsBottomSheetDialogFragment extends MenuBottomSheetDialogFragm params.rightMargin = view.getContext().getResources().getDimensionPixelSize(R.dimen.bottom_sheet_icon_margin_large); } - public static void showInstance(@NonNull FragmentManager fm, boolean trackSnappedToRoad, boolean snapToRoad, - ApplicationMode routeAppMode, OptionsFragmentListener optionsFragmentListener) { + public static void showInstance(@NonNull FragmentManager fm, Fragment targetFragment, boolean trackSnappedToRoad, + boolean snapToRoad, String routeAppModeStringKey) { try { if (!fm.isStateSaved()) { + OptionsBottomSheetDialogFragment fragment = new OptionsBottomSheetDialogFragment(); Bundle args = new Bundle(); args.putBoolean(TRACK_SNAPPED_TO_ROAD_KEY, trackSnappedToRoad); args.putBoolean(SNAP_TO_ROAD_ENABLED_KEY, snapToRoad); - OptionsBottomSheetDialogFragment fragment = new OptionsBottomSheetDialogFragment(); + args.putString(SNAP_TO_ROAD_APP_MODE_KEY, routeAppModeStringKey); fragment.setArguments(args); - fragment.setRouteAppMode(routeAppMode); - fragment.setUsedOnMap(true); - fragment.setListener(optionsFragmentListener); + fragment.setTargetFragment(targetFragment,0); fragment.show(fm, TAG); } } catch (RuntimeException e) { diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/ProfileCard.java b/OsmAnd/src/net/osmand/plus/measurementtool/ProfileCard.java index 9dd78c0f0e..fecb2c38d7 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/ProfileCard.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/ProfileCard.java @@ -19,17 +19,13 @@ import java.util.List; public class ProfileCard extends BaseCard { - public static final int INIT_MODE = 0; - ApplicationMode selectedMode; + private ApplicationMode selectedMode; + private ProfileCardListener listener; public ProfileCard(MapActivity mapActivity) { super(mapActivity); } - public ApplicationMode getSelectedMode() { - return selectedMode; - } - @Override public int getCardLayoutId() { return R.layout.navigation_profiles_card; @@ -52,6 +48,9 @@ public class ProfileCard extends BaseCard { selectedMode = modes.get((Integer) v.getTag()); clearChecked(); selectedProfile.setChecked(true); + if (listener != null) { + listener.onProfileSelect(selectedMode); + } } private void clearChecked() { @@ -63,12 +62,12 @@ public class ProfileCard extends BaseCard { }; addProfileView(container, onClickListener, i, icon, title); } - initSelected(modes); + resetSelected(modes); } - private void initSelected(List modes) { - selectedMode = modes.get(INIT_MODE); - ((RadioButton) view.findViewWithTag(INIT_MODE).findViewById(R.id.compound_button)).setChecked(true); + private void resetSelected(List modes) { + selectedMode = modes.get(0); + ((RadioButton) view.findViewWithTag(0).findViewById(R.id.compound_button)).setChecked(true); } private void addProfileView(LinearLayout container, View.OnClickListener onClickListener, Object tag, @@ -84,4 +83,14 @@ public class ProfileCard extends BaseCard { row.setTag(tag); container.addView(row); } + + public void setListener(ProfileCardListener listener) { + this.listener = listener; + } + + interface ProfileCardListener { + + void onProfileSelect(ApplicationMode applicationMode); + + } } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/RouteBetweenPointsBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/measurementtool/RouteBetweenPointsBottomSheetDialogFragment.java index 3c30b7dc0e..fcb7602785 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/RouteBetweenPointsBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/RouteBetweenPointsBottomSheetDialogFragment.java @@ -19,6 +19,7 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentManager; @@ -45,8 +46,9 @@ public class RouteBetweenPointsBottomSheetDialogFragment extends BottomSheetDial private static final Log LOG = PlatformUtil.getLog(RouteBetweenPointsBottomSheetDialogFragment.class); public static final String TAG = RouteBetweenPointsBottomSheetDialogFragment.class.getSimpleName(); public static final int STRAIGHT_LINE_TAG = -1; + public static final String CALCULATION_TYPE_KEY = "calculation_type"; + public static final String ROUTE_APP_MODE_KEY = "route_app_mode"; - private RouteBetweenPointsFragmentListener listener; private boolean nightMode; private boolean portrait; private boolean snapToRoadEnabled; @@ -58,13 +60,14 @@ public class RouteBetweenPointsBottomSheetDialogFragment extends BottomSheetDial private CalculationType calculationType = WHOLE_TRACK; private ApplicationMode snapToRoadAppMode; - public void setListener(RouteBetweenPointsFragmentListener listener) { - this.listener = listener; - } - @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + Bundle args = getArguments(); + if (args != null) { + snapToRoadAppMode = ApplicationMode.valueOfStringKey(args.getString(ROUTE_APP_MODE_KEY), null); + calculationType = (CalculationType) args.get(CALCULATION_TYPE_KEY); + } OsmandApplication app = requiredMyApplication(); nightMode = app.getDaynightHelper().isNightModeForMapControls(); FragmentActivity activity = requireActivity(); @@ -90,7 +93,7 @@ public class RouteBetweenPointsBottomSheetDialogFragment extends BottomSheetDial wholeTrackBtn = mainView.findViewById(R.id.whole_track_btn); btnDescription = mainView.findViewById(R.id.button_description); - LinearLayout navigationType = (LinearLayout) mainView.findViewById(R.id.navigation_types_container); + LinearLayout navigationType = mainView.findViewById(R.id.navigation_types_container); final List modes = new ArrayList<>(ApplicationMode.values(app)); modes.remove(ApplicationMode.DEFAULT); @@ -98,13 +101,13 @@ public class RouteBetweenPointsBottomSheetDialogFragment extends BottomSheetDial @Override public void onClick(View view) { snapToRoadEnabled = false; - if (listener != null) { - ApplicationMode mode = null; - if ((int) view.getTag() != STRAIGHT_LINE_TAG) { - mode = modes.get((int) view.getTag()); - snapToRoadEnabled = true; + if ((int) view.getTag() != STRAIGHT_LINE_TAG) { + ApplicationMode mode = modes.get((int) view.getTag()); + snapToRoadEnabled = true; + Fragment fragment = getTargetFragment(); + if (fragment instanceof RouteBetweenPointsFragmentListener) { + ((RouteBetweenPointsFragmentListener) fragment).onApplicationModeItemClick(mode); } - listener.onApplicationModeItemClick(mode); } dismiss(); } @@ -176,8 +179,9 @@ public class RouteBetweenPointsBottomSheetDialogFragment extends BottomSheetDial btnDescription.setText(R.string.rourte_between_points_whole_track_button_desc); } setCalculationType(calculationType); - if (listener != null) { - listener.onCalculationTypeChanges(calculationType); + Fragment fragment = getTargetFragment(); + if (fragment instanceof RouteBetweenPointsFragmentListener) { + ((RouteBetweenPointsFragmentListener) fragment).onCalculationTypeChanges(calculationType); } } @@ -212,29 +216,27 @@ public class RouteBetweenPointsBottomSheetDialogFragment extends BottomSheetDial @Override public void onDestroyView() { - if (listener != null) { - listener.onDestroyView(snapToRoadEnabled); + Fragment fragment = getTargetFragment(); + if (fragment instanceof RouteBetweenPointsFragmentListener) { + ((RouteBetweenPointsFragmentListener) fragment).onDestroyView(snapToRoadEnabled); } super.onDestroyView(); } - - public void setRouteMode(ApplicationMode snapToRoadAppMode) { - this.snapToRoadAppMode = snapToRoadAppMode; - } - public void setCalculationType(CalculationType calculationType) { this.calculationType = calculationType; } - public static void showInstance(FragmentManager fm, RouteBetweenPointsFragmentListener listener, - CalculationType calculationType, ApplicationMode applicationMode) { + public static void showInstance(FragmentManager fm, Fragment targetFragment, CalculationType calculationType, + ApplicationMode applicationMode) { try { if (!fm.isStateSaved()) { RouteBetweenPointsBottomSheetDialogFragment fragment = new RouteBetweenPointsBottomSheetDialogFragment(); - fragment.setListener(listener); - fragment.setCalculationType(calculationType); - fragment.setRouteMode(applicationMode); + Bundle args = new Bundle(); + args.putString(ROUTE_APP_MODE_KEY, applicationMode != null ? applicationMode.getStringKey() : null); + args.putSerializable(CALCULATION_TYPE_KEY, calculationType); + fragment.setArguments(args); + fragment.setTargetFragment(targetFragment, 0); fragment.show(fm, TAG); } } catch (RuntimeException e) { diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/SliderCard.java b/OsmAnd/src/net/osmand/plus/measurementtool/SliderCard.java index fa0dd4d7fa..c3237ba912 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/SliderCard.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/SliderCard.java @@ -16,6 +16,8 @@ public class SliderCard extends BaseCard { public static final int DEFAULT_VALUE = 30; + private SliderCardListener listener; + public SliderCard(MapActivity mapActivity) { super(mapActivity); } @@ -44,13 +46,26 @@ public class SliderCard extends BaseCard { if (fromUser) { String valueStr = getStringValueWithMetric((int) value); thresholdDistanceValue.setText(valueStr); + if (listener != null) { + listener.onSliderChange((int) value); + } } } }); } + public void setListener(SliderCardListener listener) { + this.listener = listener; + } + private String getStringValueWithMetric(int value) { return String.format(view.getContext().getString(R.string.ltr_or_rtl_combine_via_space), String.valueOf(value), view.getContext().getString(R.string.m)); } + + interface SliderCardListener { + + void onSliderChange(int sliderValue); + + } } diff --git a/OsmAnd/src/net/osmand/plus/measurementtool/SnapTrackWarningBottomSheet.java b/OsmAnd/src/net/osmand/plus/measurementtool/SnapTrackWarningBottomSheet.java index 048e5bbe4a..9839474c6d 100644 --- a/OsmAnd/src/net/osmand/plus/measurementtool/SnapTrackWarningBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/measurementtool/SnapTrackWarningBottomSheet.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.os.Bundle; import android.view.View; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import net.osmand.PlatformUtil; @@ -26,11 +27,6 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment { protected View mainView; protected GpxTrackAdapter adapter; - private SnapTrackWarningListener listener; - - public void setListener(SnapTrackWarningListener listener) { - this.listener = listener; - } @Override public void createMenuItems(Bundle savedInstanceState) { @@ -49,22 +45,6 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment { items.add(new DividerSpaceItem(app, app.getResources().getDimensionPixelSize(R.dimen.content_padding_half))); } - public static void showInstance(FragmentManager fm, SnapTrackWarningListener listener) { - try { - if (!fm.isStateSaved()) { - SnapTrackWarningBottomSheet fragment = new SnapTrackWarningBottomSheet(); - fragment.setUsedOnMap(true); - fragment.setRetainInstance(true); - fragment.setListener(listener); - fm.beginTransaction() - .add(R.id.bottomFragmentContainer, fragment, TAG) - .commitAllowingStateLoss(); - } - } catch (RuntimeException e) { - LOG.error("showInstance", e); - } - } - @Override protected int getRightBottomButtonTextId() { return R.string.shared_string_continue; @@ -72,8 +52,9 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment { @Override protected void onRightBottomButtonClick() { - if (listener != null) { - listener.continueButtonOnClick(); + Fragment fragment = getTargetFragment(); + if (fragment instanceof SnapTrackWarningListener) { + ((SnapTrackWarningListener) fragment).continueButtonOnClick(); } dismiss(); } @@ -83,7 +64,6 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment { return R.string.shared_string_cancel; } - @Override public void onDestroyView() { super.onDestroyView(); @@ -91,8 +71,23 @@ public class SnapTrackWarningBottomSheet extends MenuBottomSheetDialogFragment { if (activity instanceof MapActivity) { activity.findViewById(R.id.snap_to_road_image_button).setVisibility(View.VISIBLE); } - if (listener != null) { - listener.dismissButtonOnClick(); + Fragment fragment = getTargetFragment(); + if (fragment instanceof SnapTrackWarningListener) { + ((SnapTrackWarningListener) fragment).dismissButtonOnClick(); + } + } + + public static void showInstance(FragmentManager fm, Fragment targetFragment) { + try { + if (!fm.isStateSaved()) { + SnapTrackWarningBottomSheet fragment = new SnapTrackWarningBottomSheet(); + fragment.setTargetFragment(targetFragment, 0); + fm.beginTransaction() + .add(R.id.bottomFragmentContainer, fragment, TAG) + .commitAllowingStateLoss(); + } + } catch (RuntimeException e) { + LOG.error("showInstance", e); } }