Context menu - fix do not search street for places + favorites in progress

This commit is contained in:
Alexey Kulish 2015-09-24 16:06:35 +03:00
parent e5e67aa20c
commit 0550318811
7 changed files with 268 additions and 55 deletions

View file

@ -1,6 +1,7 @@
package net.osmand.plus.mapcontextmenu;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
@ -14,11 +15,14 @@ import net.osmand.data.PointDescription;
import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiType;
import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.IconsCache;
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.base.FavoriteImageDrawable;
import net.osmand.plus.mapcontextmenu.sections.AmenityInfoMenuController;
import net.osmand.plus.mapcontextmenu.sections.FavouritePointMenuController;
import net.osmand.plus.mapcontextmenu.sections.MenuController;
import net.osmand.plus.render.RenderingIcons;
import net.osmand.plus.routing.RoutingHelper;
@ -35,8 +39,10 @@ public class MapContextMenu {
private Object object;
private int leftIconId;
private Drawable leftIcon;
private String nameStr;
private String typeStr;
private Drawable secondLineIcon;
private String streetStr;
private static final String KEY_CTX_MENU_OBJECT = "key_ctx_menu_object";
@ -79,9 +85,9 @@ public class MapContextMenu {
typeStr = null;
streetStr = null;
acquireLeftIcon();
acquireIcons();
acquireNameAndType();
if (object != null || Algorithms.isEmpty(pointDescription.getName())) {
if (needStreetName()) {
acquireStreetName(new LatLon(pointDescription.getLat(), pointDescription.getLon()));
}
return true;
@ -105,6 +111,26 @@ public class MapContextMenu {
fragment.dismissMenu();
}
private boolean needStreetName() {
boolean res = object != null || Algorithms.isEmpty(pointDescription.getName());
if (res) {
if (object != null) {
if (object instanceof Amenity) {
Amenity a = (Amenity) object;
if (a.getSubType() != null && a.getType() != null) {
PoiType pt = a.getType().getPoiTypeByKeyName(a.getSubType());
if (pt != null && pt.getOsmTag().equals("place")) {
res = false;
}
}
} else if (object instanceof FavouritePoint) {
res = false;
}
}
}
return res;
}
public void refreshMenuTitle() {
MapContextMenuFragment fragment = findMenuFragment();
if (fragment != null)
@ -123,19 +149,35 @@ public class MapContextMenu {
return leftIconId;
}
public Drawable getLeftIcon() {
return leftIcon;
}
public Drawable getSecondLineIcon() {
return secondLineIcon;
}
public String getTitleStr() {
return nameStr;
}
public String getLocationStr() {
if (Algorithms.isEmpty(streetStr))
if (object != null && object instanceof FavouritePoint) {
return typeStr;
} else {
if (Algorithms.isEmpty(streetStr)) {
return pointDescription.getLocationName(mapActivity, true).replaceAll("\n", "");
else
} else {
return streetStr;
}
}
}
private void acquireLeftIcon() {
private void acquireIcons() {
leftIconId = 0;
leftIcon = null;
secondLineIcon = null;
if (object != null) {
if (object instanceof Amenity) {
String id = null;
@ -151,10 +193,21 @@ public class MapContextMenu {
if (id != null) {
leftIconId = RenderingIcons.getBigIconResourceId(id);
}
} else if (object instanceof FavouritePoint) {
FavouritePoint fav = (FavouritePoint)object;
leftIcon = FavoriteImageDrawable.getOrCreate(mapActivity, fav.getColor(), mapActivity.getMapView().getCurrentRotatedTileBox().getDensity());
secondLineIcon = getIcon(R.drawable.ic_small_group);
}
}
}
private Drawable getIcon(int iconId) {
IconsCache iconsCache = app.getIconsCache();
boolean light = app.getSettings().isLightContent();
return iconsCache.getIcon(iconId,
light ? R.color.icon_color : R.color.icon_color_light);
}
private void acquireNameAndType() {
if (object != null) {
if (object instanceof Amenity) {
@ -169,6 +222,10 @@ public class MapContextMenu {
typeStr = Algorithms.capitalizeFirstLetterAndLowercase(typeStr.replace('_', ' '));
}
nameStr = amenity.getName(settings.MAP_PREFERRED_LOCALE.get());
} else if (object instanceof FavouritePoint) {
FavouritePoint fav = (FavouritePoint)object;
nameStr = fav.getName();
typeStr = fav.getCategory();
}
}
@ -230,19 +287,21 @@ public class MapContextMenu {
}
public MenuController getMenuController(Activity activity) {
MenuController menuController = null;
if (object != null) {
if (object instanceof Amenity) {
MenuController menuController = new AmenityInfoMenuController(app, activity, (Amenity)object);
menuController = new AmenityInfoMenuController(app, activity, (Amenity)object);
if (!Algorithms.isEmpty(typeStr)) {
menuController.addPlainMenuItem(R.drawable.ic_action_info_dark, typeStr);
}
menuController.addPlainMenuItem(R.drawable.map_my_location, pointDescription.getLocationName(activity, true).replaceAll("\n", ""));
return menuController;
} else if (object instanceof FavouritePoint) {
menuController = new FavouritePointMenuController(app, activity, (FavouritePoint)object);
menuController.addPlainMenuItem(R.drawable.map_my_location, pointDescription.getLocationName(activity, true).replaceAll("\n", ""));
}
}
return null;
return menuController;
}
public void buttonNavigatePressed() {
@ -300,6 +359,6 @@ public class MapContextMenu {
if (streetStrObj != null) {
streetStr = streetStrObj.toString();
}
acquireLeftIcon();
acquireIcons();
}
}

View file

@ -5,6 +5,7 @@ import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
@ -318,17 +319,18 @@ public class MapContextMenuFragment extends Fragment {
IconsCache iconsCache = getMyApplication().getIconsCache();
boolean light = getMyApplication().getSettings().isLightContent();
int iconId = getCtxMenu().getLeftIconId();
final View iconLayout = view.findViewById(R.id.context_menu_icon_layout);
final ImageView iconView = (ImageView)view.findViewById(R.id.context_menu_icon_view);
if (iconId == 0) {
iconLayout.setVisibility(View.GONE);
} else {
final ImageView iconView = (ImageView) view.findViewById(R.id.context_menu_icon_view);
Drawable icon = getCtxMenu().getLeftIcon();
int iconId = getCtxMenu().getLeftIconId();
if (icon != null) {
iconView.setImageDrawable(icon);
} else if (iconId != 0) {
iconView.setImageDrawable(iconsCache.getIcon(iconId,
light ? R.color.osmand_orange : R.color.osmand_orange_dark, 0.75f));
} else {
iconLayout.setVisibility(View.GONE);
}
setAddressLocation();
// Close button
@ -434,6 +436,11 @@ public class MapContextMenuFragment extends Fragment {
// Text line 2
TextView line2 = (TextView) view.findViewById(R.id.context_menu_line2);
line2.setText(getCtxMenu().getLocationStr());
Drawable icon = getCtxMenu().getSecondLineIcon();
if (icon != null) {
line2.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
line2.setCompoundDrawablePadding(dpToPx(5f));
}
}
private int getPosY() {

View file

@ -29,7 +29,6 @@ import static android.util.TypedValue.COMPLEX_UNIT_DIP;
public class AmenityInfoMenuBuilder extends MenuBuilder {
boolean firstRow;
private final Amenity amenity;
public AmenityInfoMenuBuilder(OsmandApplication app, final Amenity amenity) {
@ -53,13 +52,13 @@ public class AmenityInfoMenuBuilder extends MenuBuilder {
// Icon
LinearLayout llIcon = new LinearLayout(view.getContext());
llIcon.setOrientation(LinearLayout.HORIZONTAL);
llIcon.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(72f), firstRow ? dpToPx(48f) - dpToPx(SHADOW_HEIGHT_BOTTOM_DP) : dpToPx(48f)));
llIcon.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(72f), isFirstRow() ? dpToPx(48f) - dpToPx(SHADOW_HEIGHT_BOTTOM_DP) : dpToPx(48f)));
llIcon.setGravity(Gravity.CENTER_VERTICAL);
ll.addView(llIcon);
ImageView iconView = 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_BOTTOM_DP / 2f) : dpToPx(12f), dpToPx(32f), dpToPx(12f));
llIconParams.setMargins(dpToPx(16f), isFirstRow() ? dpToPx(12f) - dpToPx(SHADOW_HEIGHT_BOTTOM_DP / 2f) : dpToPx(12f), dpToPx(32f), dpToPx(12f));
llIconParams.gravity = Gravity.CENTER_VERTICAL;
iconView.setLayoutParams(llIconParams);
iconView.setScaleType(ImageView.ScaleType.CENTER);
@ -73,7 +72,7 @@ public class AmenityInfoMenuBuilder extends MenuBuilder {
TextView textView = new TextView(view.getContext());
LinearLayout.LayoutParams llTextParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
llTextParams.setMargins(0, firstRow ? dpToPx(8f) - dpToPx(SHADOW_HEIGHT_BOTTOM_DP) : dpToPx(8f), 0, dpToPx(8f));
llTextParams.setMargins(0, isFirstRow() ? dpToPx(8f) - dpToPx(SHADOW_HEIGHT_BOTTOM_DP) : dpToPx(8f), 0, dpToPx(8f));
textView.setLayoutParams(llTextParams);
textView.setTextSize(16); // todo: create constant
textView.setTextColor(app.getResources().getColor(light ? R.color.ctx_menu_info_text_light : R.color.ctx_menu_info_text_dark));
@ -114,7 +113,7 @@ public class AmenityInfoMenuBuilder extends MenuBuilder {
((LinearLayout) view).addView(horizontalLine);
firstRow = false;
rowBuilt();
}
public int dpToPx(float dp) {
@ -128,8 +127,8 @@ public class AmenityInfoMenuBuilder extends MenuBuilder {
@Override
public void build(View view) {
super.build(view);
firstRow = true;
boolean hasWiki = false;
for (PlainMenuItem item : plainMenuItems) {

View file

@ -0,0 +1,132 @@
package net.osmand.plus.mapcontextmenu.sections;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.util.Linkify;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.osmand.data.FavouritePoint;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.util.Algorithms;
import static android.util.TypedValue.COMPLEX_UNIT_DIP;
public class FavouritePointMenuBuilder extends MenuBuilder {
private final FavouritePoint fav;
public FavouritePointMenuBuilder(OsmandApplication app, final FavouritePoint fav) {
super(app);
this.fav = fav;
}
private void buildRow(View view, int iconId, String text, int textColor, boolean isDescription) {
buildRow(view, getRowIcon(iconId), text, textColor, isDescription);
}
private void buildRow(final View view, Drawable icon, String text, int textColor, final boolean isDescription) {
boolean light = app.getSettings().isLightContent();
LinearLayout ll = new LinearLayout(view.getContext());
ll.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//llParams.setMargins(0, dpToPx(14f), 0, dpToPx(14f));
ll.setLayoutParams(llParams);
// Icon
LinearLayout llIcon = new LinearLayout(view.getContext());
llIcon.setOrientation(LinearLayout.HORIZONTAL);
llIcon.setLayoutParams(new LinearLayout.LayoutParams(dpToPx(72f), isFirstRow() ? dpToPx(48f) - dpToPx(SHADOW_HEIGHT_BOTTOM_DP) : dpToPx(48f)));
llIcon.setGravity(Gravity.CENTER_VERTICAL);
ll.addView(llIcon);
ImageView iconView = new ImageView(view.getContext());
LinearLayout.LayoutParams llIconParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
llIconParams.setMargins(dpToPx(16f), isFirstRow() ? dpToPx(12f) - dpToPx(SHADOW_HEIGHT_BOTTOM_DP / 2f) : dpToPx(12f), dpToPx(32f), dpToPx(12f));
llIconParams.gravity = Gravity.CENTER_VERTICAL;
iconView.setLayoutParams(llIconParams);
iconView.setScaleType(ImageView.ScaleType.CENTER);
iconView.setImageDrawable(icon);
llIcon.addView(iconView);
// Text
LinearLayout llText = new LinearLayout(view.getContext());
llText.setOrientation(LinearLayout.VERTICAL);
ll.addView(llText);
TextView textView = new TextView(view.getContext());
LinearLayout.LayoutParams llTextParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
llTextParams.setMargins(0, isFirstRow() ? dpToPx(8f) - dpToPx(SHADOW_HEIGHT_BOTTOM_DP) : dpToPx(8f), 0, dpToPx(8f));
textView.setLayoutParams(llTextParams);
textView.setTextSize(16); // todo: create constant
textView.setTextColor(app.getResources().getColor(light ? R.color.ctx_menu_info_text_light : R.color.ctx_menu_info_text_dark));
textView.setAutoLinkMask(Linkify.ALL);
textView.setLinksClickable(true);
if (isDescription) {
textView.setMinLines(1);
textView.setMaxLines(5);
}
textView.setText(text);
if (textColor > 0) {
textView.setTextColor(view.getResources().getColor(textColor));
}
if (isDescription) {
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//todo: implement edit fav description dialog
//POIMapLayer.showDescriptionDialog(view.getContext(), app, fav);
}
});
}
LinearLayout.LayoutParams llTextViewParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
llTextViewParams.setMargins(0, 0, dpToPx(10f), 0);
llTextViewParams.gravity = Gravity.CENTER_VERTICAL;
llText.setLayoutParams(llTextViewParams);
llText.addView(textView);
((LinearLayout) view).addView(ll);
View horizontalLine = new View(view.getContext());
LinearLayout.LayoutParams llHorLineParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPx(1f));
llHorLineParams.gravity = Gravity.BOTTOM;
horizontalLine.setLayoutParams(llHorLineParams);
horizontalLine.setBackgroundColor(app.getResources().getColor(light ? R.color.ctx_menu_info_divider_light : R.color.ctx_menu_info_divider_dark));
((LinearLayout) view).addView(horizontalLine);
rowBuilt();
}
public int dpToPx(float dp) {
Resources r = app.getResources();
return (int) TypedValue.applyDimension(
COMPLEX_UNIT_DIP,
dp,
r.getDisplayMetrics()
);
}
@Override
public void build(View view) {
super.build(view);
if (!Algorithms.isEmpty(fav.getDescription())) {
buildRow(view, R.drawable.ic_action_note_dark, fav.getDescription(), 0, true);
}
for (PlainMenuItem item : plainMenuItems) {
buildRow(view, item.getIconId(), item.getText(), 0, false);
}
}
}

View file

@ -0,0 +1,23 @@
package net.osmand.plus.mapcontextmenu.sections;
import android.app.Activity;
import net.osmand.data.FavouritePoint;
import net.osmand.plus.OsmandApplication;
public class FavouritePointMenuController extends MenuController {
public FavouritePointMenuController(OsmandApplication app, Activity activity, final FavouritePoint fav) {
super(new FavouritePointMenuBuilder(app, fav), activity);
}
@Override
protected int getInitialMenuStatePortrait() {
return MenuState.HEADER_ONLY;
}
@Override
protected int getSupportedMenuStatesPortrait() {
return MenuState.HEADER_ONLY | MenuState.HALF_SCREEN | MenuState.FULL_SCREEN;
}
}

View file

@ -38,13 +38,24 @@ public abstract class MenuBuilder {
protected OsmandApplication app;
protected LinkedList<PlainMenuItem> plainMenuItems;
private boolean firstRow;
public MenuBuilder(OsmandApplication app) {
this.app = app;
plainMenuItems = new LinkedList<>();
}
public abstract void build(View view);
public void build(View view) {
firstRow = true;
}
protected boolean isFirstRow() {
return firstRow;
}
protected void rowBuilt() {
firstRow = false;
}
public void addPlainMenuItem(int iconId, String text) {
plainMenuItems.add(new PlainMenuItem(iconId, text));

View file

@ -1,10 +1,14 @@
package net.osmand.plus.views;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.widget.ArrayAdapter;
import net.osmand.access.AccessibleToast;
import net.osmand.data.Amenity;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.data.LocationPoint;
@ -14,22 +18,14 @@ import net.osmand.data.RotatedTileBox;
import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.FavoritesTreeFragment;
import net.osmand.plus.base.FavoriteImageDrawable;
import net.osmand.plus.views.MapTextLayer.MapTextProvider;
import net.osmand.util.Algorithms;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class FavoritesLayer extends OsmandMapLayer implements ContextMenuLayer.IContextMenuProvider, MapTextProvider<LocationPoint> {
@ -203,8 +199,6 @@ public class FavoritesLayer extends OsmandMapLayer implements ContextMenuLayer.
public boolean onContextMenuClick(ArrayAdapter<?> adapter, int itemId, int pos, boolean isChecked) {
if (itemId == R.string.favourites_context_menu_edit) {
FavoritesTreeFragment.editPoint(view.getContext(), a, null);
} else if (itemId == R.string.shared_string_show_description) {
showDescriptionDialog(a);
} else if (itemId == R.string.favourites_context_menu_delete) {
final Resources resources = view.getContext().getResources();
Builder builder = new AlertDialog.Builder(view.getContext());
@ -222,10 +216,6 @@ public class FavoritesLayer extends OsmandMapLayer implements ContextMenuLayer.
return true;
}
};
if (!Algorithms.isEmpty(a.getDescription())) {
adapter.item(R.string.shared_string_show_description).iconColor(R.drawable.ic_action_note_dark)
.listen(listener).reg();
}
adapter.item(R.string.favourites_context_menu_edit).iconColor(R.drawable.ic_action_edit_dark)
.listen(listener).reg();
adapter.item(R.string.favourites_context_menu_delete)
@ -234,14 +224,6 @@ public class FavoritesLayer extends OsmandMapLayer implements ContextMenuLayer.
}
}
private void showDescriptionDialog(FavouritePoint a) {
Builder bs = new AlertDialog.Builder(view.getContext());
bs.setTitle(a.getName(view.getContext()));
bs.setMessage(a.getDescription());
bs.setPositiveButton(R.string.shared_string_ok, null);
bs.show();
}
@Override
public LatLon getTextLocation(LocationPoint o) {
return new LatLon(o.getLatitude(), o.getLongitude());