From 1a92be876ef100bfca6bb0611ef8e62582e9df36 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 20 Mar 2015 16:27:31 +0200 Subject: [PATCH] Added layer for local changes. --- OsmAnd/res/layout/dash_osmo_item.xml | 5 +- .../plus/audionotes/AudioNotesLayer.java | 27 +-- ...ity.java => EditingPOIDialogProvider.java} | 8 +- .../net/osmand/plus/osmedit/OsmBugsLayer.java | 52 ++--- .../osmand/plus/osmedit/OsmEditingPlugin.java | 98 +++++++--- .../osmand/plus/osmedit/OsmEditsFragment.java | 15 +- .../osmand/plus/osmedit/OsmEditsLayer.java | 177 ++++++++++++++++++ .../UploadOpenstreetmapPointAsyncTask.java | 7 +- 8 files changed, 308 insertions(+), 81 deletions(-) rename OsmAnd/src/net/osmand/plus/osmedit/{EditingPOIActivity.java => EditingPOIDialogProvider.java} (99%) create mode 100644 OsmAnd/src/net/osmand/plus/osmedit/OsmEditsLayer.java diff --git a/OsmAnd/res/layout/dash_osmo_item.xml b/OsmAnd/res/layout/dash_osmo_item.xml index aa2b8b77e5..5905251d3f 100644 --- a/OsmAnd/res/layout/dash_osmo_item.xml +++ b/OsmAnd/res/layout/dash_osmo_item.xml @@ -58,8 +58,9 @@ allTranslatedSubTypes; - public EditingPOIActivity(MapActivity uiContext){ + public EditingPOIDialogProvider(MapActivity uiContext, OsmEditingPlugin plugin){ this.activity = uiContext; + this.plugin = plugin; poiTypes = uiContext.getMyApplication().getPoiTypes(); allTranslatedSubTypes = poiTypes.getAllTranslatedNames(false); @@ -637,6 +638,7 @@ public class EditingPOIActivity implements DialogProvider { @Override protected void onPostExecute(Node result) { progress.dismiss(); + plugin.collectLocalOsmEdits(); if (result != null) { successAction.run(); } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java index 1224bdab35..14f0aaa9e4 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java @@ -1,14 +1,20 @@ package net.osmand.plus.osmedit; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Serializable; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.List; +import android.app.AlertDialog; +import android.app.AlertDialog.Builder; +import android.app.Dialog; +import android.content.DialogInterface; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.PointF; +import android.os.AsyncTask; +import android.os.Bundle; +import android.util.Xml; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.EditText; +import android.widget.Toast; import net.osmand.AndroidUtils; import net.osmand.PlatformUtil; @@ -34,26 +40,20 @@ import org.apache.commons.logging.Log; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; -import android.app.AlertDialog; -import android.app.AlertDialog.Builder; -import android.app.Dialog; -import android.content.DialogInterface; -import android.graphics.Canvas; -import android.graphics.Paint; -import android.graphics.PointF; -import android.os.AsyncTask; -import android.os.Bundle; -import android.util.Xml; -import android.view.View; -import android.widget.ArrayAdapter; -import android.widget.EditText; -import android.widget.Toast; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Serializable; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.List; public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider, DialogProvider { private static final Log log = PlatformUtil.getLog(OsmBugsLayer.class); private final static int startZoom = 8; - + private final OsmEditingPlugin plugin; + private OsmandMapTileView view; private Paint pointClosedUI; @@ -74,8 +74,9 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider private OsmBugsRemoteUtil remote; private MapLayerData> data; - public OsmBugsLayer(MapActivity activity){ + public OsmBugsLayer(MapActivity activity, OsmEditingPlugin plugin){ this.activity = activity; + this.plugin = plugin; local = new OsmBugsLocalUtil(activity); remote = new OsmBugsRemoteUtil(activity.getMyApplication()); } @@ -219,6 +220,9 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider if(data != null) { data.clearCache(); } + if (plugin != null){ + plugin.collectLocalOsmEdits(); + } } private static String readText(XmlPullParser parser, String key) throws XmlPullParserException, IOException { diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingPlugin.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingPlugin.java index 16ba1a5d03..4820ca87c2 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingPlugin.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditingPlugin.java @@ -1,24 +1,5 @@ package net.osmand.plus.osmedit; -import java.util.List; - -import net.osmand.access.AccessibleToast; -import net.osmand.data.Amenity; -import net.osmand.plus.ContextMenuAdapter; -import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandPlugin; -import net.osmand.plus.OsmandSettings; -import net.osmand.plus.R; -import net.osmand.plus.activities.EnumAdapter; -import net.osmand.plus.activities.EnumAdapter.IEnumWithResource; -import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.activities.TabActivity; -import net.osmand.plus.myplaces.AvailableGPXFragment; -import net.osmand.plus.myplaces.AvailableGPXFragment.GpxInfo; -import net.osmand.plus.myplaces.FavoritesActivity; -import net.osmand.plus.views.OsmandMapTileView; -import net.osmand.util.Algorithms; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; @@ -34,12 +15,34 @@ import android.widget.EditText; import android.widget.Spinner; import android.widget.Toast; +import net.osmand.access.AccessibleToast; +import net.osmand.data.Amenity; +import net.osmand.data.DataTileManager; +import net.osmand.plus.ContextMenuAdapter; +import net.osmand.plus.ContextMenuAdapter.OnContextMenuClick; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; +import net.osmand.plus.OsmandSettings; +import net.osmand.plus.R; +import net.osmand.plus.activities.EnumAdapter; +import net.osmand.plus.activities.EnumAdapter.IEnumWithResource; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.activities.TabActivity; +import net.osmand.plus.myplaces.AvailableGPXFragment; +import net.osmand.plus.myplaces.AvailableGPXFragment.GpxInfo; +import net.osmand.plus.myplaces.FavoritesActivity; +import net.osmand.plus.views.OsmandMapTileView; +import net.osmand.util.Algorithms; + +import java.util.List; + public class OsmEditingPlugin extends OsmandPlugin { private static final String ID = "osm.editing"; private OsmandSettings settings; private OsmandApplication app; - + DataTileManager localOsmEdits = new DataTileManager<>(); + @Override public String getId() { return ID; @@ -47,6 +50,7 @@ public class OsmEditingPlugin extends OsmandPlugin { public OsmEditingPlugin(OsmandApplication app) { this.app = app; + collectLocalOsmEdits(); } @Override @@ -56,7 +60,8 @@ public class OsmEditingPlugin extends OsmandPlugin { } private OsmBugsLayer osmBugsLayer; - private EditingPOIActivity poiActions; + private OsmEditsLayer osmEditsLayer; + private EditingPOIDialogProvider poiActions; @Override public void updateLayers(OsmandMapTileView mapView, MapActivity activity){ @@ -74,7 +79,16 @@ public class OsmEditingPlugin extends OsmandPlugin { @Override public void registerLayers(MapActivity activity){ - osmBugsLayer = new OsmBugsLayer(activity); + osmBugsLayer = new OsmBugsLayer(activity, this); + osmEditsLayer = new OsmEditsLayer(activity, this); + activity.getMapView().addLayer(osmEditsLayer, 3.5f); + } + + public OsmEditsLayer getOsmEditsLayer(MapActivity activity){ + if(osmEditsLayer == null) { + registerLayers(activity); + } + return osmEditsLayer; } public OsmBugsLayer getBugsLayer(MapActivity activity) { @@ -87,7 +101,7 @@ public class OsmEditingPlugin extends OsmandPlugin { @Override public void mapActivityCreate(MapActivity activity) { // Always create new actions ! - poiActions = new EditingPOIActivity(activity); + poiActions = new EditingPOIDialogProvider(activity, this); activity.addDialogProvider(getPoiActions(activity)); activity.addDialogProvider(getBugsLayer(activity)); } @@ -98,9 +112,9 @@ public class OsmEditingPlugin extends OsmandPlugin { return SettingsOsmEditingActivity.class; } - public EditingPOIActivity getPoiActions(MapActivity activity) { + public EditingPOIDialogProvider getPoiActions(MapActivity activity) { if(poiActions == null) { - poiActions = new EditingPOIActivity(activity); + poiActions = new EditingPOIDialogProvider(activity, this); } return poiActions; } @@ -208,7 +222,11 @@ public class OsmEditingPlugin extends OsmandPlugin { }).position(5).reg(); } } - + + public void onLocalItemDeleted(OsmPoint point) { + localOsmEdits.unregisterObject(point.getLatitude(), point.getLongitude(), point); + } + public enum UploadVisibility implements IEnumWithResource { Public(R.string.gpxup_public), Identifiable(R.string.gpxup_identifiable), @@ -264,7 +282,33 @@ public class OsmEditingPlugin extends OsmandPlugin { bldr.show(); return true; } - + + public void onLocalOsmEditAdded(OsmPoint point){ + localOsmEdits.registerObject(point.getLatitude(), point.getLongitude(), point); + } + + public void collectLocalOsmEdits(){ + localOsmEdits.clear(); + OpenstreetmapsDbHelper dbpoi = new OpenstreetmapsDbHelper(app); + OsmBugsDbHelper dbbug = new OsmBugsDbHelper(app); + + List l1 = dbpoi.getOpenstreetmapPoints(); + List l2 = dbbug.getOsmbugsPoints(); + for (OsmPoint point : l1){ + localOsmEdits.registerObject(point.getLatitude(), point.getLongitude(), point); + } + for (OsmPoint point : l2){ + localOsmEdits.registerObject(point.getLatitude(), point.getLongitude(), point); + } + } + + public DataTileManager getLocalOsmEdits(){ + return localOsmEdits; + } + + public List getAllEdits(){ + return localOsmEdits.getAllObjects(); + } @Override public String getName() { diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java index 87757e5acb..b0df833a35 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsFragment.java @@ -306,16 +306,18 @@ public class OsmEditsFragment extends ListFragment implements OsmEditsUploadList public void onClick(DialogInterface dialog, int which) { Iterator it = points.iterator(); while (it.hasNext()) { - OsmPoint info = it.next(); - if (info.getGroup() == OsmPoint.Group.POI) { - dbpoi.deletePOI((OpenstreetmapPoint) info); - } else if (info.getGroup() == OsmPoint.Group.BUG) { - dbbug.deleteAllBugModifications((OsmNotesPoint) info); + OsmPoint omsPoint = it.next(); + if (omsPoint.getGroup() == OsmPoint.Group.POI) { + dbpoi.deletePOI((OpenstreetmapPoint) omsPoint); + } else if (omsPoint.getGroup() == OsmPoint.Group.BUG) { + dbbug.deleteAllBugModifications((OsmNotesPoint) omsPoint); } it.remove(); - listAdapter.delete(info); + listAdapter.delete(omsPoint); + plugin.onLocalItemDeleted(omsPoint); } listAdapter.notifyDataSetChanged(); + } }); b.setNegativeButton(R.string.shared_string_cancel, null); @@ -620,6 +622,7 @@ public class OsmEditsFragment extends ListFragment implements OsmEditsUploadList @Override public void uploadUpdated(OsmPoint point) { + plugin.onLocalItemDeleted(point); listAdapter.delete(point); } diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsLayer.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsLayer.java new file mode 100644 index 0000000000..a2bb4e7c80 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmEditsLayer.java @@ -0,0 +1,177 @@ +package net.osmand.plus.osmedit; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.graphics.PointF; + +import net.osmand.data.DataTileManager; +import net.osmand.data.LatLon; +import net.osmand.data.PointDescription; +import net.osmand.data.QuadRect; +import net.osmand.data.RotatedTileBox; +import net.osmand.plus.R; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.views.ContextMenuLayer; +import net.osmand.plus.views.OsmandMapLayer; +import net.osmand.plus.views.OsmandMapTileView; + +import java.util.List; + +/** + * Created by Denis on + * 20.03.2015. + */ +public class OsmEditsLayer extends OsmandMapLayer implements ContextMenuLayer.IContextMenuProvider { + + private static final int startZoom = 10; + private final OsmEditingPlugin plugin; + private final MapActivity activity; + private Bitmap poi; + private Bitmap bug; + private OsmandMapTileView view; + private Paint pointAtUI; + private Paint paintIcon; + private Paint point; + + + + public OsmEditsLayer(MapActivity activity, OsmEditingPlugin plugin) { + this.activity = activity; + this.plugin = plugin; + } + + @Override + public void initLayer(OsmandMapTileView view) { + this.view = view; + + + pointAtUI = new Paint(); + pointAtUI.setColor(0xa0FF3344); + pointAtUI.setStyle(Paint.Style.FILL); + + poi = BitmapFactory.decodeResource(view.getResources(), R.drawable.map_pin_origin); + bug = BitmapFactory.decodeResource(view.getResources(), R.drawable.map_pin_destination); + + paintIcon = new Paint(); + + point = new Paint(); + point.setColor(Color.RED); + point.setAntiAlias(true); + point.setStyle(Paint.Style.STROKE); + } + + @Override + public void onDraw(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { + + } + + @Override + public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { + if (tileBox.getZoom() >= startZoom) { + DataTileManager points = plugin.getLocalOsmEdits(); + final QuadRect latlon = tileBox.getLatLonBounds(); + List objects = points.getObjects(latlon. top, latlon.left, latlon.bottom, latlon.right); + for (OsmPoint o : objects) { + int x = (int) tileBox.getPixXFromLatLon(o.getLatitude(), o.getLongitude()); + int y = (int) tileBox.getPixYFromLatLon(o.getLatitude(), o.getLongitude()); + Bitmap b; + if (o.getGroup() == OsmPoint.Group.POI) { + b = poi; + } else if (o.getGroup() == OsmPoint.Group.BUG) { + b = bug; + } else { + b = poi; + + } + canvas.drawBitmap(b, x - b.getWidth() / 2, y - b.getHeight(), paintIcon); + } + } + } + + @Override + public void destroyLayer() { + + } + + @Override + public boolean drawInScreenPixels() { + return false; + } + + + public void getOsmEditsFromPoint(PointF point, RotatedTileBox tileBox, List am) { + int ex = (int) point.x; + int ey = (int) point.y; + int compare = getRadiusPoi(tileBox); + int radius = compare * 3 / 2; + for (OsmPoint n : plugin.getAllEdits()) { + int x = (int) tileBox.getPixXFromLatLon(n.getLatitude(), n.getLongitude()); + int y = (int) tileBox.getPixYFromLatLon(n.getLatitude(), n.getLongitude()); + if (calculateBelongs(ex, ey, x, y, compare)) { + compare = radius; + am.add(n); + } + } + } + + private boolean calculateBelongs(int ex, int ey, int objx, int objy, int radius) { + return Math.abs(objx - ex) <= radius && (ey - objy) <= radius / 2 && (objy - ey) <= 3 * radius ; + } + + public int getRadiusPoi(RotatedTileBox tb){ + int r = 0; + if(tb.getZoom() < startZoom){ + r = 0; + } else { + r = 15; + } + return (int) (r * tb.getDensity()); + } + + @Override + public void collectObjectsFromPoint(PointF point, RotatedTileBox tileBox, List o) { + getOsmEditsFromPoint(point, tileBox, o); + } + + @Override + public LatLon getObjectLocation(Object o) { + if (o instanceof OsmPoint) { + return new LatLon(((OsmPoint)o).getLatitude(),((OsmPoint)o).getLongitude()); + } + return null; + } + + @Override + public String getObjectDescription(Object o) { + if(o instanceof OsmPoint) { + OsmPoint point = (OsmPoint) o; + if (point.getGroup() == OsmPoint.Group.POI){ + return ((OpenstreetmapPoint) point).getName(); + } else if (point.getGroup() == OsmPoint.Group.BUG) { + return ((OsmNotesPoint) point).getText(); + } + } + return null; + } + + @Override + public PointDescription getObjectName(Object o) { + if(o instanceof OsmPoint) { + OsmPoint point = (OsmPoint) o; + String name = ""; + String type = ""; + if (point.getGroup() == OsmPoint.Group.POI){ + name = ((OpenstreetmapPoint) point).getName(); + type = PointDescription.POINT_TYPE_OSM_NOTE; + } else if (point.getGroup() == OsmPoint.Group.BUG) { + name = ((OsmNotesPoint) point).getText(); + type = PointDescription.POINT_TYPE_OSM_BUG; + } + return new PointDescription(type, name); + } + return null; + } +} diff --git a/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java b/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java index cdd2f2b035..07000adf5c 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/UploadOpenstreetmapPointAsyncTask.java @@ -1,18 +1,12 @@ package net.osmand.plus.osmedit; -import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.support.v4.app.Fragment; -import android.widget.Toast; -import net.osmand.access.AccessibleToast; import net.osmand.osm.edit.EntityInfo; import net.osmand.osm.edit.Node; -import net.osmand.plus.R; - -import java.text.MessageFormat; /** * Created by Denis @@ -41,6 +35,7 @@ public class UploadOpenstreetmapPointAsyncTask extends AsyncTask