Context menu - POI type and Coorsd in detais view

This commit is contained in:
Alexey Kulish 2015-09-21 16:56:02 +03:00
parent 30bd298e93
commit 4aaf677632
5 changed files with 149 additions and 73 deletions

View file

@ -1,10 +1,8 @@
package net.osmand.plus.mapcontextmenu; package net.osmand.plus.mapcontextmenu;
import android.app.Activity; import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.widget.Toast;
import net.osmand.Location; import net.osmand.Location;
import net.osmand.ResultMatcher; import net.osmand.ResultMatcher;
@ -13,9 +11,9 @@ import net.osmand.data.Amenity;
import net.osmand.data.FavouritePoint; import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon; import net.osmand.data.LatLon;
import net.osmand.data.PointDescription; import net.osmand.data.PointDescription;
import net.osmand.osm.PoiCategory;
import net.osmand.osm.PoiType; import net.osmand.osm.PoiType;
import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuAdapter;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
@ -35,7 +33,10 @@ public class MapContextMenu {
private PointDescription pointDescription; private PointDescription pointDescription;
private Object object; private Object object;
private String foundStreetName; private int leftIconId;
private String nameStr;
private String typeStr;
private String streetStr;
private static final String KEY_CTX_MENU_OBJECT = "key_ctx_menu_object"; 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"; private static final String KEY_CTX_MENU_POINT_DESC = "key_ctx_menu_point_desc";
@ -64,8 +65,13 @@ public class MapContextMenu {
this.pointDescription = pointDescription; this.pointDescription = pointDescription;
this.object = object; this.object = object;
foundStreetName = null; leftIconId = 0;
nameStr = null;
typeStr = null;
streetStr = null;
acquireLeftIconId();
acquireNameAndType();
acquireStreetName(mapActivity, new LatLon(pointDescription.getLat(), pointDescription.getLon())); acquireStreetName(mapActivity, new LatLon(pointDescription.getLat(), pointDescription.getLon()));
MapContextMenuFragment.showInstance(mapActivity); MapContextMenuFragment.showInstance(mapActivity);
@ -93,6 +99,22 @@ public class MapContextMenu {
} }
public int getLeftIconId() { public int getLeftIconId() {
return leftIconId;
}
public String getTitleStr() {
return nameStr;
}
public String getLocationStr(MapActivity mapActivity) {
if (Algorithms.isEmpty(streetStr))
return pointDescription.getLocationName(mapActivity, true).replaceAll("\n", "");
else
return streetStr;
}
private void acquireLeftIconId() {
leftIconId = 0;
if (object != null) { if (object != null) {
if (object instanceof Amenity) { if (object instanceof Amenity) {
String id = null; String id = null;
@ -108,42 +130,45 @@ public class MapContextMenu {
if (id != null) { if (id != null) {
Integer resId = RenderingIcons.getResId(id); Integer resId = RenderingIcons.getResId(id);
if (resId != null) { if (resId != null) {
return resId; leftIconId = resId;
} }
} }
} }
} }
return 0;
} }
public String getAddressStr() { private void acquireNameAndType() {
String res = null;
if (object != null) { if (object != null) {
if (object instanceof Amenity) { if (object instanceof Amenity) {
Amenity amenity = (Amenity) object; Amenity amenity = (Amenity) object;
res = OsmAndFormatter.getPoiStringWithoutType(amenity, settings.MAP_PREFERRED_LOCALE.get());
PoiCategory pc = amenity.getType();
PoiType pt = pc.getPoiTypeByKeyName(amenity.getSubType());
typeStr = amenity.getSubType();
if (pt != null) {
typeStr = pt.getTranslation();
} else if(typeStr != null){
typeStr = Algorithms.capitalizeFirstLetterAndLowercase(typeStr.replace('_', ' '));
}
nameStr = amenity.getName(settings.MAP_PREFERRED_LOCALE.get());
} }
} }
if (Algorithms.isEmpty(res)) { if (Algorithms.isEmpty(nameStr)) {
String typeName = pointDescription.getTypeName(); nameStr = pointDescription.getName();
String name = pointDescription.getName(); }
if (Algorithms.isEmpty(typeStr)) {
if (!Algorithms.isEmpty(name)) typeStr = pointDescription.getTypeName();
res = name;
else if (!Algorithms.isEmpty(typeName))
res = typeName;
} }
return Algorithms.isEmpty(res) ? "Address is unknown yet" : res; // todo: text constant if (Algorithms.isEmpty(nameStr)) {
if (!Algorithms.isEmpty(typeStr)) {
nameStr = typeStr;
typeStr = null;
} else {
nameStr = app.getResources().getString(R.string.address_unknown);
}
} }
public String getLocationStr(MapActivity mapActivity) {
if (foundStreetName == null)
return pointDescription.getLocationName(mapActivity, true).replaceAll("\n", "");
else
return foundStreetName;
} }
private void acquireStreetName(final MapActivity activity, final LatLon loc) { private void acquireStreetName(final MapActivity activity, final LatLon loc) {
@ -154,14 +179,14 @@ public class MapContextMenu {
@Override @Override
public boolean publish(RouteDataObject object) { public boolean publish(RouteDataObject object) {
if(object != null) { if (object != null) {
foundStreetName = RoutingHelper.formatStreetName(object.getName(settings.MAP_PREFERRED_LOCALE.get()), streetStr = RoutingHelper.formatStreetName(object.getName(settings.MAP_PREFERRED_LOCALE.get()),
object.getRef(), object.getDestinationName(settings.MAP_PREFERRED_LOCALE.get())); object.getRef(), object.getDestinationName(settings.MAP_PREFERRED_LOCALE.get()));
if (foundStreetName != null && foundStreetName.trim().length() == 0) { if (streetStr != null && streetStr.trim().length() == 0) {
foundStreetName = null; streetStr = null;
} }
if (foundStreetName != null) { if (streetStr != null) {
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
public void run() { public void run() {
refreshMenuTitle(activity); refreshMenuTitle(activity);
@ -169,7 +194,7 @@ public class MapContextMenu {
}); });
} }
} else { } else {
foundStreetName = null; streetStr = null;
} }
return true; return true;
} }
@ -186,7 +211,12 @@ public class MapContextMenu {
if (object != null) { if (object != null) {
if (object instanceof Amenity) { if (object instanceof Amenity) {
return new AmenityInfoMenuController(app, activity, (Amenity)object); MenuController 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;
} }
} }

View file

@ -213,6 +213,8 @@ public class MapContextMenuFragment extends Fragment {
private float velocityY; private float velocityY;
private float maxVelocityY; private float maxVelocityY;
private boolean hasMoved;
@Override @Override
public boolean onTouch(View v, MotionEvent event) { public boolean onTouch(View v, MotionEvent event) {
@ -220,6 +222,10 @@ public class MapContextMenuFragment extends Fragment {
OsmandMapTileView mapView = getMapActivity().getMapView(); OsmandMapTileView mapView = getMapActivity().getMapView();
mapView.getAnimatedDraggingThread().startMoving(getCtxMenu().getPointDescription().getLat(), getCtxMenu().getPointDescription().getLon(), mapView.getAnimatedDraggingThread().startMoving(getCtxMenu().getPointDescription().getLat(), getCtxMenu().getPointDescription().getLon(),
mapView.getZoom(), true); mapView.getZoom(), true);
if (hasMoved) {
applyPosY(getViewY());
}
return true; return true;
} }
@ -229,6 +235,7 @@ public class MapContextMenuFragment extends Fragment {
switch (event.getAction()) { switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
hasMoved = false;
dy = event.getY(); dy = event.getY();
dyMain = getViewY(); dyMain = getViewY();
velocity = VelocityTracker.obtain(); velocity = VelocityTracker.obtain();
@ -238,6 +245,7 @@ public class MapContextMenuFragment extends Fragment {
break; break;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
hasMoved = true;
float y = event.getY(); float y = event.getY();
float newY = getViewY() + (y - dy); float newY = getViewY() + (y - dy);
setViewY((int) newY); setViewY((int) newY);
@ -275,11 +283,18 @@ public class MapContextMenuFragment extends Fragment {
} }
} }
applyPosY(currentY);
break;
}
return true;
}
private void applyPosY(int currentY) {
final int posY = getPosY(); final int posY = getPosY();
if (currentY != posY) { if (currentY != posY) {
if (posY < currentY) {
if (posY < getViewY()) {
updateMainViewLayout(posY); updateMainViewLayout(posY);
} }
@ -304,10 +319,6 @@ public class MapContextMenuFragment extends Fragment {
updateMainViewLayout(posY); updateMainViewLayout(posY);
} }
} }
break;
}
return true;
} }
}; };
@ -417,7 +428,7 @@ public class MapContextMenuFragment extends Fragment {
private void setAddressLocation() { private void setAddressLocation() {
// Text line 1 // Text line 1
TextView line1 = (TextView) view.findViewById(R.id.context_menu_line1); TextView line1 = (TextView) view.findViewById(R.id.context_menu_line1);
line1.setText(getCtxMenu().getAddressStr()); line1.setText(getCtxMenu().getTitleStr());
// Text line 2 // Text line 2
TextView line2 = (TextView) view.findViewById(R.id.context_menu_line2); TextView line2 = (TextView) view.findViewById(R.id.context_menu_line2);

View file

@ -25,6 +25,7 @@ import static android.util.TypedValue.COMPLEX_UNIT_DIP;
public class AmenityInfoMenuBuilder extends MenuBuilder { public class AmenityInfoMenuBuilder extends MenuBuilder {
boolean firstRow;
private final Amenity amenity; private final Amenity amenity;
public AmenityInfoMenuBuilder(OsmandApplication app, final Amenity amenity) { public AmenityInfoMenuBuilder(OsmandApplication app, final Amenity amenity) {
@ -32,11 +33,11 @@ public class AmenityInfoMenuBuilder extends MenuBuilder {
this.amenity = amenity; this.amenity = amenity;
} }
private void buildRow(View view, int iconId, String text, boolean firstRow) { private void buildRow(View view, int iconId, String text) {
buildRow(view, getRowIcon(iconId), text, firstRow); buildRow(view, getRowIcon(iconId), text);
} }
private void buildRow(View view, Drawable icon, String text, boolean firstRow) { private void buildRow(View view, Drawable icon, String text) {
boolean light = app.getSettings().isLightContent(); boolean light = app.getSettings().isLightContent();
LinearLayout ll = new LinearLayout(view.getContext()); LinearLayout ll = new LinearLayout(view.getContext());
@ -94,6 +95,8 @@ public class AmenityInfoMenuBuilder extends MenuBuilder {
horizontalLine.setBackgroundColor(app.getResources().getColor(light ? R.color.ctx_menu_info_divider_light : R.color.ctx_menu_info_divider_dark)); horizontalLine.setBackgroundColor(app.getResources().getColor(light ? R.color.ctx_menu_info_divider_light : R.color.ctx_menu_info_divider_dark));
((LinearLayout) view).addView(horizontalLine); ((LinearLayout) view).addView(horizontalLine);
firstRow = false;
} }
public int dpToPx(float dp) { public int dpToPx(float dp) {
@ -108,7 +111,11 @@ public class AmenityInfoMenuBuilder extends MenuBuilder {
@Override @Override
public void build(View view) { public void build(View view) {
boolean firstRow = true; firstRow = true;
for (PlainMenuItem item : plainMenuItems) {
buildRow(view, item.getIconId(), item.getText());
}
MapPoiTypes poiTypes = app.getPoiTypes(); MapPoiTypes poiTypes = app.getPoiTypes();
for (Map.Entry<String, String> e : amenity.getAdditionalInfo().entrySet()) { for (Map.Entry<String, String> e : amenity.getAdditionalInfo().entrySet()) {
@ -149,12 +156,10 @@ public class AmenityInfoMenuBuilder extends MenuBuilder {
} }
if (icon != null) { if (icon != null) {
buildRow(view, icon, vl, firstRow); buildRow(view, icon, vl);
} else { } else {
buildRow(view, iconId, vl, firstRow); buildRow(view, iconId, vl);
} }
firstRow = false;
} }
} }
} }

View file

@ -11,19 +11,45 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.render.RenderingIcons; import net.osmand.plus.render.RenderingIcons;
import java.util.LinkedList;
public abstract class MenuBuilder { public abstract class MenuBuilder {
public class PlainMenuItem {
private int iconId;
private String text;
public PlainMenuItem(int iconId, String text) {
this.iconId = iconId;
this.text = text;
}
public int getIconId() {
return iconId;
}
public String getText() {
return text;
}
}
public static final float SHADOW_HEIGHT_TOP_DP = 16f; public static final float SHADOW_HEIGHT_TOP_DP = 16f;
public static final float SHADOW_HEIGHT_BOTTOM_DP = 6f; public static final float SHADOW_HEIGHT_BOTTOM_DP = 6f;
protected OsmandApplication app; protected OsmandApplication app;
protected LinkedList<PlainMenuItem> plainMenuItems;
public MenuBuilder(OsmandApplication app) { public MenuBuilder(OsmandApplication app) {
this.app = app; this.app = app;
plainMenuItems = new LinkedList<>();
} }
public abstract void build(View view); public abstract void build(View view);
public void addPlainMenuItem(int iconId, String text) {
plainMenuItems.add(new PlainMenuItem(iconId, text));
}
public Drawable getRowIcon(int iconId) { public Drawable getRowIcon(int iconId) {
IconsCache iconsCache = app.getIconsCache(); IconsCache iconsCache = app.getIconsCache();
boolean light = app.getSettings().isLightContent(); boolean light = app.getSettings().isLightContent();

View file

@ -30,6 +30,10 @@ public abstract class MenuController {
builder.build(rootView); builder.build(rootView);
} }
public void addPlainMenuItem(int iconId, String text) {
builder.addPlainMenuItem(iconId, text);
}
public int getInitialMenuState() { public int getInitialMenuState() {
if (isLandscapeLayout()) { if (isLandscapeLayout()) {
return MenuState.FULL_SCREEN; return MenuState.FULL_SCREEN;