From 1f553c1c1420804a2a975c107475b133612fc9cc Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Thu, 29 Oct 2015 17:25:23 +0300 Subject: [PATCH] Context menu: OsMo menu ready --- .../plus/mapcontextmenu/MapContextMenu.java | 30 +++++++++++++++++-- .../details/OsMoMenuController.java | 17 +++++++++-- .../details/TargetPointMenuController.java | 6 +++- .../editors/FavoritePointEditorFragment.java | 3 +- .../osmand/plus/osmo/OsMoPositionLayer.java | 14 +++++---- 5 files changed, 56 insertions(+), 14 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java index 35c6e4fe74..a2fda07fa9 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java @@ -93,8 +93,8 @@ public class MapContextMenu extends MenuTitleController { return init(latLon, pointDescription, object, false); } - public boolean init(LatLon latLon, PointDescription pointDescription, Object object, boolean reload) { - if (!reload && isVisible()) { + public boolean init(LatLon latLon, PointDescription pointDescription, Object object, boolean update) { + if (!update && isVisible()) { if (this.object == null || !this.object.equals(object)) { hide(); } else { @@ -105,6 +105,7 @@ public class MapContextMenu extends MenuTitleController { if (this.object != null) { clearSelectedObject(this.object); } + setSelectedObject(object); if (pointDescription == null) { this.pointDescription = new PointDescription(latLon.getLatitude(), latLon.getLongitude()); @@ -145,7 +146,7 @@ public class MapContextMenu extends MenuTitleController { } } - public void refreshMenu(LatLon latLon, PointDescription pointDescription, Object object) { + public void update(LatLon latLon, PointDescription pointDescription, Object object) { MapContextMenuFragment fragment = findMenuFragment(); if (fragment != null) { init(latLon, pointDescription, object, true); @@ -153,6 +154,14 @@ public class MapContextMenu extends MenuTitleController { } } + public void showOrUpdate(LatLon latLon, PointDescription pointDescription, Object object) { + if (isVisible() && this.object != null && this.object.equals(object)) { + update(latLon, pointDescription, object); + } else { + show(latLon, pointDescription, object); + } + } + public void close() { active = false; hide(); @@ -171,6 +180,21 @@ public class MapContextMenu extends MenuTitleController { } private void clearSelectedObject(Object object) { + if (object != null) { + for (OsmandMapLayer l : mapActivity.getMapView().getLayers()) { + if (l instanceof ContextMenuLayer.IContextMenuProvider) { + PointDescription pointDescription = ((ContextMenuLayer.IContextMenuProvider) l).getObjectName(object); + if (pointDescription != null) { + if (l instanceof ContextMenuLayer.IContextMenuProviderSelection) { + ((ContextMenuLayer.IContextMenuProviderSelection) l).clearSelectedObject(); + } + } + } + } + } + } + + private void setSelectedObject(Object object) { if (object != null) { for (OsmandMapLayer l : mapActivity.getMapView().getLayers()) { if (l instanceof ContextMenuLayer.IContextMenuProvider) { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/details/OsMoMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/details/OsMoMenuController.java index 4a7f29488d..67896c93f3 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/details/OsMoMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/details/OsMoMenuController.java @@ -2,6 +2,8 @@ package net.osmand.plus.mapcontextmenu.details; import android.graphics.drawable.Drawable; +import net.osmand.Location; +import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; @@ -49,8 +51,19 @@ public class OsMoMenuController extends MenuController { @Override public String getTypeStr() { - // todo - return ""; + OsmandApplication app = getMapActivity().getMyApplication(); + StringBuilder sb = new StringBuilder(); + final Location l = device.getLastLocation(); + if(l != null && l.hasSpeed()) { + sb.append(OsmAndFormatter.getFormattedSpeed(l.getSpeed(), app)); + sb.append(" — "); + } + Location myLocation = app.getLocationProvider().getLastKnownLocation(); + if (myLocation != null) { + float dist = myLocation.distanceTo(l); + sb.append(OsmAndFormatter.getFormattedDistance(dist, app)); + } + return sb.toString(); } @Override diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/details/TargetPointMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/details/TargetPointMenuController.java index 8c16d24657..289df7cb65 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/details/TargetPointMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/details/TargetPointMenuController.java @@ -53,7 +53,11 @@ public class TargetPointMenuController extends MenuController { @Override public String getNameStr() { - return targetPoint.getOriginalPointDescription().getSimpleName(getMapActivity(), false); + if (targetPoint.getOriginalPointDescription() != null) { + return targetPoint.getOriginalPointDescription().getSimpleName(getMapActivity(), false); + } else { + return targetPoint.getOnlyName(); + } } @Override diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditorFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditorFragment.java index ca39232110..e01a695bfd 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditorFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/editors/FavoritePointEditorFragment.java @@ -6,7 +6,6 @@ import android.content.DialogInterface; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.os.Bundle; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -156,7 +155,7 @@ public class FavoritePointEditorFragment extends PointEditorFragment { PointDescription pointDescription = favorite.getPointDescription(); pointDescription.setLat(favorite.getLatitude()); pointDescription.setLon(favorite.getLongitude()); - menu.refreshMenu(latLon, pointDescription, favorite); + menu.update(latLon, pointDescription, favorite); } } diff --git a/OsmAnd/src/net/osmand/plus/osmo/OsMoPositionLayer.java b/OsmAnd/src/net/osmand/plus/osmo/OsMoPositionLayer.java index be335c8722..1840239d0b 100644 --- a/OsmAnd/src/net/osmand/plus/osmo/OsMoPositionLayer.java +++ b/OsmAnd/src/net/osmand/plus/osmo/OsMoPositionLayer.java @@ -349,7 +349,8 @@ public class OsMoPositionLayer extends OsmandMapLayer implements ContextMenuLaye ContextMenuLayer cl = map.getMapLayers().getContextMenuLayer(); final boolean sameObject; if (map.getContextMenu().getObject() instanceof OsMoDevice && cl.isVisible()) { - sameObject = Algorithms.objectEquals(device.trackerId, ((OsMoDevice) map.getContextMenu().getObject()).trackerId); + sameObject = Algorithms.objectEquals(device.trackerId, + ((OsMoDevice) map.getContextMenu().getObject()).trackerId); } else { sameObject = false; } @@ -374,13 +375,14 @@ public class OsMoPositionLayer extends OsmandMapLayer implements ContextMenuLaye schedule = false; if (sameObject) { Location l = device.getLastLocation(); - map.getContextMenu().show(new LatLon(l.getLatitude(), l.getLongitude()), getObjectName(device), device); - //cl.setLocation(new LatLon(l.getLatitude(), l.getLongitude()), getObjectDescription(device)); - //cl.setSelectedObject(device); + if (centered) { + map.getContextMenu().setMapCenter(loc); + } + map.getContextMenu().showOrUpdate(new LatLon(l.getLatitude(), l.getLongitude()), + getObjectName(device), device); } if (centered) { - map.getMapView().setLatLon(loc.getLatitude(), - loc.getLongitude()); + map.getMapView().setLatLon(loc.getLatitude(), loc.getLongitude()); } map.getMapView().refreshMap(); }