From 332658157612b05e89f9962fb7a41264c5eaeb11 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Thu, 10 Sep 2015 19:14:21 +0300 Subject: [PATCH] Fix context menu crash while changing device orientation --- .../res/drawable/bg_map_context_menu_dark.xml | 2 +- .../src/net/osmand/data/PointDescription.java | 5 ++++- .../plus/mapcontextmenu/MapContextMenu.java | 19 +++++++++++++++++++ .../MapContextMenuFragment.java | 11 ++++++++++- .../sections/AmenityInfoMenuBuilder.java | 6 ++---- 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/OsmAnd/res/drawable/bg_map_context_menu_dark.xml b/OsmAnd/res/drawable/bg_map_context_menu_dark.xml index f67482f175..c584b75e83 100644 --- a/OsmAnd/res/drawable/bg_map_context_menu_dark.xml +++ b/OsmAnd/res/drawable/bg_map_context_menu_dark.xml @@ -1,7 +1,7 @@ - + diff --git a/OsmAnd/src/net/osmand/data/PointDescription.java b/OsmAnd/src/net/osmand/data/PointDescription.java index 77d5e1b4fd..8b77835709 100644 --- a/OsmAnd/src/net/osmand/data/PointDescription.java +++ b/OsmAnd/src/net/osmand/data/PointDescription.java @@ -1,5 +1,6 @@ package net.osmand.data; +import java.io.Serializable; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.Locale; @@ -15,7 +16,7 @@ import net.osmand.util.Algorithms; import android.content.Context; import android.support.annotation.NonNull; -public class PointDescription { +public class PointDescription implements Serializable { private String type = ""; private String name = ""; private String typeName; @@ -23,6 +24,8 @@ public class PointDescription { private double lat = 0; private double lon = 0; + private static final long serialVersionUID = 4078409090417168638L; + public static final String POINT_TYPE_FAVORITE = "favorite"; public static final String POINT_TYPE_WPT = "wpt"; public static final String POINT_TYPE_POI = "poi"; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java index 8e6281dad3..78a3937a09 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java @@ -1,5 +1,6 @@ package net.osmand.plus.mapcontextmenu; +import android.os.Bundle; import android.support.v4.app.Fragment; import net.osmand.binary.RouteDataObject; @@ -29,6 +30,9 @@ public class MapContextMenu { private String foundStreetName; + private static final String KEY_CTX_MENU_OBJECT = "key_ctx_menu_object"; + private static final String KEY_CTX_MENU_POINT_DESC = "key_ctx_menu_point_desc"; + public boolean isMenuVisible(MapActivity mapActivity) { return mapActivity.getSupportFragmentManager().findFragmentByTag("MapContextMenuFragment") != null; } @@ -168,4 +172,19 @@ public class MapContextMenu { mapActivity.getMapActions().contextMenuPoint(pointDescription.getLat(), pointDescription.getLon(), menuAdapter, object); } + + public void saveMenuState(Bundle bundle) { + if (object != null) { + if (object instanceof Amenity) + bundle.putSerializable(KEY_CTX_MENU_OBJECT, (Amenity)object); + } + bundle.putSerializable(KEY_CTX_MENU_POINT_DESC, pointDescription); + } + + public void restoreMenuState(Bundle bundle) { + object = bundle.getSerializable(KEY_CTX_MENU_OBJECT); + Object pDescObj = bundle.getSerializable(KEY_CTX_MENU_POINT_DESC); + if (pDescObj != null) + pointDescription = (PointDescription)pDescObj; + } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java index 3e1682094a..2a2c213754 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java @@ -85,11 +85,20 @@ public class MapContextMenuFragment extends Fragment { */ } + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + getCtxMenu().saveMenuState(outState); + } + @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + if (savedInstanceState != null) + getCtxMenu().restoreMenuState(savedInstanceState); + view = inflater.inflate(R.layout.map_context_menu_fragment, container, false); ViewTreeObserver vto = view.getViewTreeObserver(); @@ -425,7 +434,7 @@ public class MapContextMenuFragment extends Fragment { } private MapContextMenu getCtxMenu() { - return ((MapActivity)getActivity()).getContextMenu(); + return getMapActivity().getContextMenu(); } private MapActivity getMapActivity() { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/sections/AmenityInfoMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/sections/AmenityInfoMenuBuilder.java index 1ee0b9044e..cf5d90571f 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/sections/AmenityInfoMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/sections/AmenityInfoMenuBuilder.java @@ -1,8 +1,6 @@ package net.osmand.plus.mapcontextmenu.sections; import android.content.res.Resources; -import android.text.SpannableString; -import android.text.method.LinkMovementMethod; import android.text.util.Linkify; import android.util.TypedValue; import android.view.Gravity; @@ -26,7 +24,7 @@ import static android.util.TypedValue.COMPLEX_UNIT_DIP; public class AmenityInfoMenuBuilder extends MenuBuilder { - private static float SHADOW_HEIGHT = 6f; // in dp + private static final float SHADOW_HEIGHT = 6f; // in dp private final Amenity amenity; @@ -53,7 +51,7 @@ public class AmenityInfoMenuBuilder extends MenuBuilder { ImageView icon = new ImageView(view.getContext()); LinearLayout.LayoutParams llIconParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) ; - llIconParams.setMargins(dpToPx(16f), firstRow ? dpToPx(12f) - dpToPx(SHADOW_HEIGHT) : dpToPx(12f), dpToPx(32f), dpToPx(12f)); + llIconParams.setMargins(dpToPx(16f), firstRow ? dpToPx(12f) - dpToPx(SHADOW_HEIGHT / 2f) : dpToPx(12f), dpToPx(32f), dpToPx(12f)); llIconParams.gravity = Gravity.CENTER_VERTICAL; icon.setLayoutParams(llIconParams); icon.setScaleType(ImageView.ScaleType.CENTER);