Create synced context menu

This commit is contained in:
PavelRatushny 2017-11-16 18:01:18 +02:00
parent 8ef2ce4f8d
commit c819c7f8de
6 changed files with 244 additions and 111 deletions

View file

@ -574,13 +574,15 @@ public class MapMarkersHelper {
for (MapMarker marker : markers) {
if (marker.id.equals(group.getId() + name)) {
exists = true;
if (!marker.history && (!marker.point.equals(latLon))) {
for (MapMarker m : mapMarkers) {
if (m.id.equals(marker.id)) {
for (MapMarker m : mapMarkers) {
if (m.id.equals(marker.id)) {
m.favouritePoint = favouritePoint;
m.wptPt = wptPt;
if (!marker.history && !marker.point.equals(latLon)) {
m.point = latLon;
updateMapMarker(m, true);
break;
}
break;
}
}
markers.remove(marker);

View file

@ -2,49 +2,18 @@ package net.osmand.plus.mapcontextmenu.builders;
import android.view.View;
import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.data.Amenity;
import net.osmand.data.FavouritePoint;
import net.osmand.data.QuadRect;
import net.osmand.data.TransportStop;
import net.osmand.osm.PoiCategory;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapillary.MapillaryPlugin;
import net.osmand.util.MapUtils;
import java.util.List;
public class FavouritePointMenuBuilder extends MenuBuilder {
private final FavouritePoint fav;
private Object originObject;
public class FavouritePointMenuBuilder extends SyncedItemMenuBuilder {
public FavouritePointMenuBuilder(MapActivity mapActivity, final FavouritePoint fav) {
super(mapActivity);
this.fav = fav;
setShowNearestWiki(true);
this.favouritePoint = fav;
acquireOriginObject();
}
public void acquireOriginObject()
{
String originObjectName = fav.getOriginObjectName();
if (originObjectName.length() > 0) {
if (originObjectName.startsWith(Amenity.class.getSimpleName())) {
originObject = findAmenity(originObjectName, fav.getLatitude(), fav.getLongitude());
} else if (originObjectName.startsWith(TransportStop.class.getSimpleName())) {
originObject = findTransportStop(originObjectName, fav.getLatitude(), fav.getLongitude());
}
}
}
public Object getOriginObject() {
return originObject;
}
@Override
protected void buildNearestWikiRow(View view) {
if (originObject == null || !(originObject instanceof Amenity)) {
@ -61,54 +30,4 @@ public class FavouritePointMenuBuilder extends MenuBuilder {
builder.buildInternal(view);
}
}
private Amenity findAmenity(String nameStringEn, double lat, double lon) {
QuadRect rect = MapUtils.calculateLatLonBbox(lat, lon, 15);
List<Amenity> amenities = app.getResourceManager().searchAmenities(
new BinaryMapIndexReader.SearchPoiTypeFilter() {
@Override
public boolean accept(PoiCategory type, String subcategory) {
return true;
}
@Override
public boolean isEmpty() {
return false;
}
}, rect.top, rect.left, rect.bottom, rect.right, -1, null);
for (Amenity amenity : amenities) {
String stringEn = amenity.toStringEn();
if (stringEn.equals(nameStringEn)) {
return amenity;
}
}
return null;
}
private TransportStop findTransportStop(String nameStringEn, double lat, double lon) {
QuadRect rect = MapUtils.calculateLatLonBbox(lat, lon, 15);
List<TransportStop> res = app.getResourceManager().searchTransportSync(rect.top, rect.left,
rect.bottom, rect.right, new ResultMatcher<TransportStop>() {
@Override
public boolean publish(TransportStop object) {
return true;
}
@Override
public boolean isCancelled() {
return false;
}
});
for (TransportStop stop : res) {
String stringEn = stop.toStringEn();
if (stringEn.equals(nameStringEn)) {
return stop;
}
}
return null;
}
}

View file

@ -0,0 +1,95 @@
package net.osmand.plus.mapcontextmenu.builders;
import android.view.View;
import net.osmand.data.Amenity;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.views.POIMapLayer;
import net.osmand.util.Algorithms;
import java.text.DateFormat;
import java.util.Date;
public class MapMarkerMenuBuilder extends SyncedItemMenuBuilder {
private GPXUtilities.WptPt wptPt;
public MapMarkerMenuBuilder(MapActivity mapActivity, final MapMarkersHelper.MapMarker marker) {
super(mapActivity);
this.favouritePoint = marker.favouritePoint;
this.wptPt = marker.wptPt;
if (favouritePoint != null) {
acquireOriginObject();
}
}
@Override
protected boolean needBuildPlainMenuItems() {
return false;
}
@Override
protected void buildNearestWikiRow(View view) {
if (originObject == null || !(originObject instanceof Amenity)) {
super.buildNearestWikiRow(view);
}
}
@Override
protected void buildInternal(View view) {
if (originObject != null && originObject instanceof Amenity) {
AmenityMenuBuilder builder = new AmenityMenuBuilder(mapActivity, (Amenity) originObject);
builder.setLatLon(getLatLon());
builder.setLight(light);
builder.buildInternal(view);
}
if (wptPt != null) {
if (wptPt.time > 0) {
DateFormat dateFormat = android.text.format.DateFormat.getMediumDateFormat(view.getContext());
DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(view.getContext());
Date date = new Date(wptPt.time);
buildRow(view, R.drawable.ic_action_data,
dateFormat.format(date) + "" + timeFormat.format(date), 0, false, null, false, 0, false, null);
}
if (wptPt.speed > 0) {
buildRow(view, R.drawable.ic_action_speed,
OsmAndFormatter.getFormattedSpeed((float) wptPt.speed, app), 0, false, null, false, 0, false, null);
}
if (!Double.isNaN(wptPt.ele)) {
buildRow(view, R.drawable.ic_action_altitude,
OsmAndFormatter.getFormattedDistance((float) wptPt.ele, app), 0, false, null, false, 0, false, null);
}
if (!Double.isNaN(wptPt.hdop)) {
buildRow(view, R.drawable.ic_action_gps_info,
Algorithms.capitalizeFirstLetterAndLowercase(app.getString(R.string.plugin_distance_point_hdop)) + ": " + (int) wptPt.hdop, 0,
false, null, false, 0, false, null);
}
if (!Algorithms.isEmpty(wptPt.desc)) {
final View row = buildRow(view, R.drawable.ic_action_note_dark, wptPt.desc, 0, false, null, true, 10, false, null);
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
POIMapLayer.showDescriptionDialog(row.getContext(), app, wptPt.desc,
row.getResources().getString(R.string.description));
}
});
}
if (!Algorithms.isEmpty(wptPt.comment)) {
final View rowc = buildRow(view, R.drawable.ic_action_note_dark, wptPt.comment, 0,
false, null, true, 10, false, null);
rowc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
POIMapLayer.showDescriptionDialog(rowc.getContext(), app, wptPt.comment,
rowc.getResources().getString(R.string.poi_dialog_comment));
}
});
}
}
buildPlainMenuItems(view);
}
}

View file

@ -0,0 +1,92 @@
package net.osmand.plus.mapcontextmenu.builders;
import net.osmand.ResultMatcher;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.data.Amenity;
import net.osmand.data.FavouritePoint;
import net.osmand.data.QuadRect;
import net.osmand.data.TransportStop;
import net.osmand.osm.PoiCategory;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.util.MapUtils;
import java.util.List;
public class SyncedItemMenuBuilder extends MenuBuilder {
protected FavouritePoint favouritePoint;
protected Object originObject;
protected GPXUtilities.WptPt wptPt;
public SyncedItemMenuBuilder(MapActivity mapActivity) {
super(mapActivity);
setShowNearestWiki(true);
}
protected void acquireOriginObject() {
String originObjectName = favouritePoint.getOriginObjectName();
if (originObjectName.length() > 0) {
if (originObjectName.startsWith(Amenity.class.getSimpleName())) {
originObject = findAmenity(originObjectName, favouritePoint.getLatitude(), favouritePoint.getLongitude());
} else if (originObjectName.startsWith(TransportStop.class.getSimpleName())) {
originObject = findTransportStop(originObjectName, favouritePoint.getLatitude(), favouritePoint.getLongitude());
}
}
}
public Object getOriginObject() {
return originObject;
}
protected Amenity findAmenity(String nameStringEn, double lat, double lon) {
QuadRect rect = MapUtils.calculateLatLonBbox(lat, lon, 15);
List<Amenity> amenities = app.getResourceManager().searchAmenities(
new BinaryMapIndexReader.SearchPoiTypeFilter() {
@Override
public boolean accept(PoiCategory type, String subcategory) {
return true;
}
@Override
public boolean isEmpty() {
return false;
}
}, rect.top, rect.left, rect.bottom, rect.right, -1, null);
for (Amenity amenity : amenities) {
String stringEn = amenity.toStringEn();
if (stringEn.equals(nameStringEn)) {
return amenity;
}
}
return null;
}
protected TransportStop findTransportStop(String nameStringEn, double lat, double lon) {
QuadRect rect = MapUtils.calculateLatLonBbox(lat, lon, 15);
List<TransportStop> res = app.getResourceManager().searchTransportSync(rect.top, rect.left,
rect.bottom, rect.right, new ResultMatcher<TransportStop>() {
@Override
public boolean publish(TransportStop object) {
return true;
}
@Override
public boolean isCancelled() {
return false;
}
});
for (TransportStop stop : res) {
String stringEn = stop.toStringEn();
if (stringEn.equals(nameStringEn)) {
return stop;
}
}
return null;
}
}

View file

@ -6,22 +6,17 @@ import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapillary.MapillaryPlugin;
import net.osmand.plus.views.POIMapLayer;
import net.osmand.util.Algorithms;
import java.text.DateFormat;
import java.util.Date;
public class WptPtMenuBuilder extends MenuBuilder {
private final WptPt wpt;
public class WptPtMenuBuilder extends SyncedItemMenuBuilder {
public WptPtMenuBuilder(MapActivity mapActivity, final WptPt wpt) {
super(mapActivity);
this.wpt = wpt;
setShowNearestWiki(true);
this.wptPt = wpt;
}
@Override
@ -31,43 +26,43 @@ public class WptPtMenuBuilder extends MenuBuilder {
@Override
public void buildInternal(View view) {
if (wpt.time > 0) {
if (wptPt.time > 0) {
DateFormat dateFormat = android.text.format.DateFormat.getMediumDateFormat(view.getContext());
DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(view.getContext());
Date date = new Date(wpt.time);
Date date = new Date(wptPt.time);
buildRow(view, R.drawable.ic_action_data,
dateFormat.format(date) + "" + timeFormat.format(date), 0, false, null, false, 0, false, null);
}
if (wpt.speed > 0) {
if (wptPt.speed > 0) {
buildRow(view, R.drawable.ic_action_speed,
OsmAndFormatter.getFormattedSpeed((float)wpt.speed, app), 0, false, null, false, 0, false, null);
OsmAndFormatter.getFormattedSpeed((float)wptPt.speed, app), 0, false, null, false, 0, false, null);
}
if (!Double.isNaN(wpt.ele)) {
if (!Double.isNaN(wptPt.ele)) {
buildRow(view, R.drawable.ic_action_altitude,
OsmAndFormatter.getFormattedDistance((float) wpt.ele, app), 0, false, null, false, 0, false, null);
OsmAndFormatter.getFormattedDistance((float) wptPt.ele, app), 0, false, null, false, 0, false, null);
}
if (!Double.isNaN(wpt.hdop)) {
if (!Double.isNaN(wptPt.hdop)) {
buildRow(view, R.drawable.ic_action_gps_info,
Algorithms.capitalizeFirstLetterAndLowercase(app.getString(R.string.plugin_distance_point_hdop)) + ": " + (int)wpt.hdop, 0,
Algorithms.capitalizeFirstLetterAndLowercase(app.getString(R.string.plugin_distance_point_hdop)) + ": " + (int)wptPt.hdop, 0,
false, null, false, 0, false, null);
}
if (!Algorithms.isEmpty(wpt.desc)) {
final View row = buildRow(view, R.drawable.ic_action_note_dark, wpt.desc, 0, false, null, true, 10, false, null);
if (!Algorithms.isEmpty(wptPt.desc)) {
final View row = buildRow(view, R.drawable.ic_action_note_dark, wptPt.desc, 0, false, null, true, 10, false, null);
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
POIMapLayer.showDescriptionDialog(row.getContext(), app, wpt.desc,
POIMapLayer.showDescriptionDialog(row.getContext(), app, wptPt.desc,
row.getResources().getString(R.string.description));
}
});
}
if (!Algorithms.isEmpty(wpt.comment)) {
final View rowc = buildRow(view, R.drawable.ic_action_note_dark, wpt.comment, 0,
if (!Algorithms.isEmpty(wptPt.comment)) {
final View rowc = buildRow(view, R.drawable.ic_action_note_dark, wptPt.comment, 0,
false, null, true, 10, false, null);
rowc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
POIMapLayer.showDescriptionDialog(rowc.getContext(), app, wpt.comment,
POIMapLayer.showDescriptionDialog(rowc.getContext(), app, wptPt.comment,
rowc.getResources().getString(R.string.poi_dialog_comment));
}
});

View file

@ -3,15 +3,19 @@ package net.osmand.plus.mapcontextmenu.controllers;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import net.osmand.data.Amenity;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.data.PointDescription;
import net.osmand.data.TransportStop;
import net.osmand.plus.IconsCache;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.MapMarkerDialogHelper;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.mapcontextmenu.builders.MapMarkerMenuBuilder;
import net.osmand.util.Algorithms;
public class MapMarkerMenuController extends MenuController {
@ -19,9 +23,8 @@ public class MapMarkerMenuController extends MenuController {
private MapMarker mapMarker;
public MapMarkerMenuController(MapActivity mapActivity, PointDescription pointDescription, MapMarker mapMarker) {
super(new MenuBuilder(mapActivity), pointDescription, mapActivity);
super(new MapMarkerMenuBuilder(mapActivity, mapMarker), pointDescription, mapActivity);
this.mapMarker = mapMarker;
builder.setShowNearestWiki(true);
final MapMarkersHelper markersHelper = mapActivity.getMyApplication().getMapMarkersHelper();
leftTitleButtonController = new TitleButtonController() {
@ -54,6 +57,10 @@ public class MapMarkerMenuController extends MenuController {
return new LayerDrawable(new Drawable[]{background, topbar});
}
private MapMarkerMenuBuilder getBuilder() {
return (MapMarkerMenuBuilder) builder;
}
@Override
protected void setObject(Object object) {
if (object instanceof MapMarker) {
@ -104,4 +111,27 @@ public class MapMarkerMenuController extends MenuController {
public int getWaypointActionStringId() {
return R.string.rename_marker;
}
@Override
public void addPlainMenuItems(String typeStr, PointDescription pointDescription, LatLon latLon) {
FavouritePoint fav = mapMarker.favouritePoint;
if (fav != null && !Algorithms.isEmpty(fav.getDescription())) {
addPlainMenuItem(R.drawable.ic_action_note_dark, fav.getDescription(), true, false, null);
}
Object originObject = getBuilder().getOriginObject();
if (originObject != null) {
if (originObject instanceof Amenity) {
Amenity amenity = (Amenity) originObject;
AmenityMenuController.addPlainMenuItems(amenity, AmenityMenuController.getTypeStr(amenity), builder);
} else if (originObject instanceof TransportStop) {
TransportStop stop = (TransportStop) originObject;
TransportStopController transportStopController =
new TransportStopController(getMapActivity(), pointDescription, stop);
transportStopController.addPlainMenuItems(builder, latLon);
addMyLocationToPlainItems(latLon);
}
} else {
addMyLocationToPlainItems(latLon);
}
}
}