Clean up onBackPressed in MapActivity from fragments

This commit is contained in:
Vitaliy 2020-09-14 21:46:11 +03:00
parent e955652a6b
commit c2e110604d
7 changed files with 123 additions and 92 deletions

View file

@ -86,7 +86,6 @@ import net.osmand.plus.base.FailSafeFuntions;
import net.osmand.plus.base.MapViewTrackingUtilities;
import net.osmand.plus.chooseplan.OsmLiveCancelledDialog;
import net.osmand.plus.dashboard.DashboardOnMap;
import net.osmand.plus.dashboard.DashboardOnMap.DashboardType;
import net.osmand.plus.dialogs.CrashBottomSheetDialogFragment;
import net.osmand.plus.dialogs.ImportGpxBottomSheetDialogFragment;
import net.osmand.plus.dialogs.RateUsBottomSheetDialogFragment;
@ -109,11 +108,7 @@ import net.osmand.plus.helpers.ScrollHelper.OnScrollEventListener;
import net.osmand.plus.mapcontextmenu.AdditionalActionsBottomSheetDialogFragment;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.mapcontextmenu.MenuController.MenuState;
import net.osmand.plus.mapcontextmenu.builders.cards.dialogs.ContextMenuCardDialogFragment;
import net.osmand.plus.mapcontextmenu.editors.FavoritePointEditor;
import net.osmand.plus.mapcontextmenu.editors.PointEditorFragmentNew;
import net.osmand.plus.mapcontextmenu.editors.WptPtEditor;
import net.osmand.plus.mapcontextmenu.other.DestinationReachedMenu;
import net.osmand.plus.mapcontextmenu.other.TrackDetailsMenu;
import net.osmand.plus.mapmarkers.PlanRouteFragment;
@ -122,7 +117,6 @@ import net.osmand.plus.measurementtool.GpxData;
import net.osmand.plus.measurementtool.MeasurementEditingContext;
import net.osmand.plus.measurementtool.MeasurementToolFragment;
import net.osmand.plus.measurementtool.SnapTrackWarningFragment;
import net.osmand.plus.quickaction.QuickActionListFragment;
import net.osmand.plus.render.RendererRegistry;
import net.osmand.plus.resources.ResourceManager;
import net.osmand.plus.routepreparationmenu.ChooseRouteFragment;
@ -153,7 +147,6 @@ import net.osmand.plus.views.OsmandMapTileView.OnDrawMapListener;
import net.osmand.plus.views.corenative.NativeCoreContext;
import net.osmand.plus.views.layers.MapControlsLayer;
import net.osmand.plus.views.layers.MapInfoLayer;
import net.osmand.plus.views.layers.MapQuickActionLayer;
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController;
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControllerType;
import net.osmand.render.RenderingRulesStorage;
@ -668,44 +661,16 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
showQuickSearch(ShowQuickSearchMode.CURRENT, false);
return;
}
if (trackDetailsMenu.isVisible()) {
trackDetailsMenu.hide(true);
if (mapContextMenu.isActive() && mapContextMenu.getPointDescription() != null
&& mapContextMenu.getPointDescription().isGpxPoint()) {
mapContextMenu.show();
return;
}
if (prevActivityIntent == null) {
return;
}
}
PointEditorFragmentNew pointEditorFragmentNew = getPointEditorFragmentNew();
if (pointEditorFragmentNew != null) {
pointEditorFragmentNew.showExitDialog();
return;
}
TrackAppearanceFragment trackAppearanceFragment = getTrackAppearanceFragment();
if (trackAppearanceFragment != null) {
trackAppearanceFragment.dismissImmediate();
if (prevActivityIntent == null) {
return;
}
}
if (mapContextMenu.isVisible() && mapContextMenu.isClosable()) {
if (mapContextMenu.getCurrentMenuState() != MenuState.HEADER_ONLY && !isLandscapeLayout()) {
mapContextMenu.openMenuHeaderOnly();
} else {
mapContextMenu.close();
}
return;
}
if (getMapLayers().getContextMenuLayer().isInAddGpxPointMode()) {
quitAddGpxPointMode();
}
if (launchPrevActivityIntent()) {
return;
}
super.onBackPressed();
}
public boolean launchPrevActivityIntent() {
if (prevActivityIntent != null && getSupportFragmentManager().getBackStackEntryCount() == 0) {
prevActivityIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
LatLon loc = getMapLocation();
@ -714,21 +679,11 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
if (mapViewTrackingUtilities.isMapLinkedToLocation()) {
prevActivityIntent.putExtra(SearchActivity.SEARCH_NEARBY, true);
}
this.startActivity(prevActivityIntent);
startActivity(prevActivityIntent);
prevActivityIntent = null;
return;
return true;
}
QuickActionListFragment quickActionListFragment = getQuickActionListFragment();
if (quickActionListFragment != null && quickActionListFragment.isVisible()) {
if (quickActionListFragment.fromDashboard()) {
this.getDashboard().setDashboardVisibility(true, DashboardType.CONFIGURE_SCREEN, null);
} else {
getMapView().getLayerByClass(MapQuickActionLayer.class).onBackPressed();
}
} else if (getMapView().getLayerByClass(MapQuickActionLayer.class).onBackPressed()) {
return;
}
super.onBackPressed();
return false;
}
private void quitAddGpxPointMode() {
@ -2243,14 +2198,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
return getFragment(ChooseRouteFragment.TAG);
}
public QuickActionListFragment getQuickActionListFragment() {
return getFragment(QuickActionListFragment.TAG);
}
public TrackAppearanceFragment getTrackAppearanceFragment() {
return getFragment(TrackAppearanceFragment.TAG);
}
public GpxApproximationFragment getGpxApproximationFragment() {
return getFragment(GpxApproximationFragment.TAG);
}
@ -2259,15 +2206,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
return getFragment(SnapTrackWarningFragment.TAG);
}
public PointEditorFragmentNew getPointEditorFragmentNew() {
PointEditorFragmentNew pointEditorFragmentNew;
pointEditorFragmentNew = getFragment(FavoritePointEditor.TAG);
if (pointEditorFragmentNew == null) {
pointEditorFragmentNew = getFragment(WptPtEditor.TAG);
}
return pointEditorFragmentNew;
}
public void backToConfigureProfileFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
int backStackEntryCount = fragmentManager.getBackStackEntryCount();

View file

@ -476,6 +476,7 @@ public class MapActivityActions implements DialogProvider {
} else if (standardId == R.string.avoid_road) {
getMyApplication().getAvoidSpecificRoads().addImpassableRoad(mapActivity, new LatLon(latitude, longitude), true, false, null);
} else if (standardId == R.string.shared_string_add || standardId == R.string.favourites_context_menu_edit) {
mapActivity.getContextMenu().hide();
mapActivity.getContextMenu().buttonFavoritePressed();
} else if (standardId == R.string.shared_string_marker || standardId == R.string.shared_string_edit) {
mapActivity.getContextMenu().buttonWaypointPressed();

View file

@ -37,6 +37,7 @@ import android.widget.OverScroller;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
@ -108,11 +109,8 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
private InterceptorLinearLayout mainView;
private View toolbarContainer;
private View toolbarView;
private ImageView toolbarBackButton;
private TextView toolbarTextView;
private View topButtonContainer;
private LockableScrollView menuScrollView;
private LinearLayout mainRouteBadgeContainer;
private LinearLayout nearbyRoutesLayout;
@ -123,8 +121,6 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
private TextView localRoutesMoreTv;
private View zoomButtonsView;
private ImageButton zoomInButtonView;
private ImageButton zoomOutButtonView;
private MapContextMenu menu;
private OnLayoutChangeListener containerLayoutListener;
@ -149,7 +145,6 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
private int topScreenPosY;
private int bottomToolbarPosY;
private int minHalfY;
private int shadowHeight;
private int zoomPaddingTop;
private OsmandMapTileView map;
@ -164,13 +159,28 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
private boolean wasDrawerDisabled;
private boolean zoomIn;
private int screenOrientation;
private boolean created;
private boolean transportBadgesCreated;
private UpdateLocationViewCache updateLocationViewCache;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requireMyActivity().getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
public void handleOnBackPressed() {
if (menu.isVisible() && menu.isClosable()) {
if (menu.getCurrentMenuState() != MenuState.HEADER_ONLY && !menu.isLandscapeLayout()) {
menu.openMenuHeaderOnly();
} else {
menu.close();
}
}
}
});
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
@ -188,7 +198,7 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
markerPaddingPx = dpToPx(MARKER_PADDING_DP);
markerPaddingXPx = dpToPx(MARKER_PADDING_X_DP);
shadowHeight = dpToPx(SHADOW_HEIGHT_TOP_DP);
int shadowHeight = dpToPx(SHADOW_HEIGHT_TOP_DP);
topScreenPosY = addStatusBarHeightIfNeeded(-shadowHeight);
bottomToolbarPosY = addStatusBarHeightIfNeeded(getResources().getDimensionPixelSize(R.dimen.dashboard_map_toolbar));
minHalfY = viewHeight - (int) (viewHeight * menu.getHalfScreenMaxHeightKoef());
@ -204,8 +214,7 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
mainView = view.findViewById(R.id.context_menu_main);
toolbarContainer = view.findViewById(R.id.context_menu_toolbar_container);
toolbarView = view.findViewById(R.id.context_menu_toolbar);
toolbarBackButton = view.findViewById(R.id.context_menu_toolbar_back);
ImageView toolbarBackButton = view.findViewById(R.id.context_menu_toolbar_back);
toolbarTextView = (TextView) view.findViewById(R.id.context_menu_toolbar_text);
updateVisibility(toolbarContainer, 0);
toolbarBackButton.setOnClickListener(new View.OnClickListener() {
@ -510,9 +519,9 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
// Zoom buttons
zoomButtonsView = view.findViewById(R.id.context_menu_zoom_buttons);
zoomInButtonView = (ImageButton) view.findViewById(R.id.context_menu_zoom_in_button);
zoomOutButtonView = (ImageButton) view.findViewById(R.id.context_menu_zoom_out_button);
if (menu.zoomButtonsVisible()) {
ImageButton zoomInButtonView = view.findViewById(R.id.context_menu_zoom_in_button);
ImageButton zoomOutButtonView = view.findViewById(R.id.context_menu_zoom_out_button);
AndroidUtils.updateImageButton(app, zoomInButtonView, R.drawable.ic_zoom_in, R.drawable.ic_zoom_in,
R.drawable.btn_circle_trans, R.drawable.btn_circle_night, nightMode);
AndroidUtils.updateImageButton(app, zoomOutButtonView, R.drawable.ic_zoom_out, R.drawable.ic_zoom_out,
@ -2303,4 +2312,3 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
runLayoutListener();
}
}

View file

@ -25,6 +25,7 @@ import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
@ -49,6 +50,7 @@ import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.helpers.ColorDialogs;
import net.osmand.plus.mapcontextmenu.MapContextMenu;
import net.osmand.plus.mapcontextmenu.other.HorizontalSelectionAdapter;
import net.osmand.plus.widgets.FlowLayout;
import net.osmand.util.Algorithms;
@ -99,6 +101,19 @@ public abstract class PointEditorFragmentNew extends BaseOsmAndFragment {
private EditText addressEdit;
private int layoutHeightPrevious = 0;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requireMyActivity().getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
public void handleOnBackPressed() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
showExitDialog();
}
}
});
}
@SuppressLint("ClickableViewAccessibility")
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
@ -867,14 +882,28 @@ public abstract class PointEditorFragmentNew extends BaseOsmAndFragment {
dismissDialog.setPositiveButton(R.string.shared_string_exit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
cancelled = true;
dismiss();
exitEditing();
}
});
dismissDialog.show();
} else {
exitEditing();
}
}
private void exitEditing() {
cancelled = true;
dismiss();
showContextMenu();
}
private void showContextMenu() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
MapContextMenu mapContextMenu = mapActivity.getContextMenu();
if (!mapContextMenu.isVisible() && mapContextMenu.isActive()) {
mapContextMenu.show();
}
}
}

View file

@ -12,6 +12,7 @@ import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
@ -49,11 +50,33 @@ public class TrackDetailsMenuFragment extends BaseOsmAndFragment implements OsmA
return (MapActivity) requireMyActivity();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final MapActivity mapActivity = requireMapActivity();
menu = mapActivity.getTrackDetailsMenu();
mapActivity.getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
public void handleOnBackPressed() {
if (menu.isVisible()) {
menu.hide(true);
MapContextMenu contextMenu = mapActivity.getContextMenu();
if (contextMenu.isActive() && contextMenu.getPointDescription() != null
&& contextMenu.getPointDescription().isGpxPoint()) {
contextMenu.show();
} else {
mapActivity.launchPrevActivityIntent();
}
}
}
});
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MapActivity mapActivity = requireMapActivity();
menu = mapActivity.getTrackDetailsMenu();
nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls();
View view = UiUtilities.getInflater(mapActivity, nightMode).inflate(R.layout.track_details, container, false);
if (!AndroidUiHelper.isOrientationPortrait(mapActivity)) {

View file

@ -18,6 +18,7 @@ import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
@ -38,11 +39,12 @@ import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.dashboard.DashboardOnMap;
import net.osmand.plus.settings.backend.ApplicationMode;
import net.osmand.plus.views.layers.MapQuickActionLayer;
import net.osmand.plus.views.controls.ReorderItemTouchHelperCallback;
import net.osmand.plus.views.controls.ReorderItemTouchHelperCallback.OnItemMoveCallback;
import net.osmand.plus.views.controls.ReorderItemTouchHelperCallback.UnmovableItem;
import net.osmand.plus.views.layers.MapQuickActionLayer;
import java.util.ArrayList;
import java.util.Collections;
@ -100,6 +102,26 @@ public class QuickActionListFragment extends BaseOsmAndFragment
}
screenType = savedInstanceState.getInt(SCREEN_TYPE_KEY, SCREEN_TYPE_REORDER);
}
requireMyActivity().getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
public void handleOnBackPressed() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (isVisible()) {
if (fromDashboard()) {
mapActivity.getDashboard().setDashboardVisibility(true, DashboardOnMap.DashboardType.CONFIGURE_SCREEN, null);
} else {
mapActivity.getMapView().getLayerByClass(MapQuickActionLayer.class).onBackPressed();
}
} else if (mapActivity.getMapView().getLayerByClass(MapQuickActionLayer.class).onBackPressed()) {
return;
}
FragmentManager fragmentManager = mapActivity.getSupportFragmentManager();
if (!fragmentManager.isStateSaved()) {
fragmentManager.popBackStackImmediate();
}
}
}
});
}
@Nullable

View file

@ -14,6 +14,7 @@ import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -147,6 +148,15 @@ public class TrackAppearanceFragment extends ContextMenuScrollFragment implement
}
updateTrackColor();
}
requireMyActivity().getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
public void handleOnBackPressed() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
dismissImmediate();
mapActivity.launchPrevActivityIntent();
}
}
});
}
private void updateTrackColor() {