From 04046a89460bc240510c3d670cae6b1a058f6344 Mon Sep 17 00:00:00 2001 From: vshcherb Date: Wed, 16 Oct 2013 23:17:56 +0200 Subject: [PATCH] Add new download handler --- .../net/osmand/plus/osmedit/OsmBugsLayer.java | 107 +++++------------- .../plus/views/DownloadedRegionsLayer.java | 1 - .../net/osmand/plus/views/OsmandMapLayer.java | 12 +- 3 files changed, 38 insertions(+), 82 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java index 2fa79edb9e..4acee4463c 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/OsmBugsLayer.java @@ -40,9 +40,6 @@ import android.graphics.Paint; import android.graphics.PointF; import android.os.AsyncTask; import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; -import android.os.Message; import android.util.Xml; import android.view.View; import android.widget.EditText; @@ -55,18 +52,11 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider private static final int SEARCH_LIMIT = 100; private OsmandMapTileView view; - private Handler handlerToLoop; - private List objects = new ArrayList(); private Paint pointClosedUI; private Paint pointOpenedUI; private Paint pointNotSubmitedUI; - private double cTopLatitude; - private double cBottomLatitude; - private double cLeftLongitude; - private double cRightLongitude; - private int czoom; private final MapActivity activity; private static final String KEY_AUTHOR = "author"; @@ -80,6 +70,7 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider private static Bundle dialogBundle = new Bundle(); private OsmBugsLocalUtil local; private OsmBugsRemoteUtil remote; + private MapLayerData> data; public OsmBugsLayer(MapActivity activity){ this.activity = activity; @@ -99,19 +90,6 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider @Override public void initLayer(OsmandMapTileView view) { this.view = view; - synchronized (this) { - if (handlerToLoop == null) { - new Thread("Open street bugs layer") { //$NON-NLS-1$ - @Override - public void run() { - Looper.prepare(); - handlerToLoop = new Handler(); - Looper.loop(); - } - }.start(); - } - - } pointOpenedUI = new Paint(); pointOpenedUI.setColor(activity.getResources().getColor(R.color.osmbug_opened)); pointOpenedUI.setAntiAlias(true); @@ -121,21 +99,23 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider pointClosedUI = new Paint(); pointClosedUI.setColor(activity.getResources().getColor(R.color.osmbug_closed)); pointClosedUI.setAntiAlias(true); + + data = new OsmandMapLayer.MapLayerData>() { + + { + ZOOM_THRESHOLD = 1; + } + + @Override + protected List calculateResult(RotatedTileBox tileBox) { + QuadRect bounds = tileBox.getLatLonBounds(); + return loadingBugs(bounds.top, bounds.left, bounds.bottom, bounds.right); + } + }; } @Override public void destroyLayer() { - synchronized (this) { - if(handlerToLoop != null){ - handlerToLoop.post(new Runnable(){ - @Override - public void run() { - Looper.myLooper().quit(); - } - }); - handlerToLoop = null; - } - } } @Override @@ -147,13 +127,15 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { if (tileBox.getZoom() >= startZoom) { // request to load - final QuadRect latLonBounds = tileBox.getLatLonBounds(); - requestToLoad(latLonBounds.top, latLonBounds.left, latLonBounds.bottom, latLonBounds.right, tileBox.getZoom()); - for (OpenStreetNote o : objects) { - int x = tileBox.getPixXFromLonNoRot(o.getLongitude()); - int y = tileBox.getPixYFromLatNoRot(o.getLatitude()); - canvas.drawCircle(x, y, getRadiusBug(tileBox), o.isLocal() ? pointNotSubmitedUI : (o.isOpened() ? pointOpenedUI - : pointClosedUI)); + data.queryNewData(tileBox); + List objects = data.getResults(); + if (objects != null) { + for (OpenStreetNote o : objects) { + int x = tileBox.getPixXFromLonNoRot(o.getLongitude()); + int y = tileBox.getPixYFromLatNoRot(o.getLatitude()); + canvas.drawCircle(x, y, getRadiusBug(tileBox), o.isLocal() ? pointNotSubmitedUI + : (o.isOpened() ? pointOpenedUI : pointClosedUI)); + } } } } @@ -181,37 +163,7 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider return (int) (z * tb.getDensity()); } - public void requestToLoad(double topLatitude, double leftLongitude, double bottomLatitude,double rightLongitude, final int zoom){ - boolean inside = cTopLatitude >= topLatitude && cLeftLongitude <= leftLongitude && cRightLongitude >= rightLongitude - && cBottomLatitude <= bottomLatitude; - if((!inside || (czoom != zoom && objects.size() >= SEARCH_LIMIT)) && handlerToLoop != null){ - handlerToLoop.removeMessages(1); - final double nTopLatitude = topLatitude + (topLatitude -bottomLatitude); - final double nBottomLatitude = bottomLatitude - (topLatitude -bottomLatitude); - final double nLeftLongitude = leftLongitude - (rightLongitude - leftLongitude); - final double nRightLongitude = rightLongitude + (rightLongitude - leftLongitude); - Message msg = Message.obtain(handlerToLoop, new Runnable() { - @Override - public void run() { - if(handlerToLoop != null && !handlerToLoop.hasMessages(1)){ - boolean inside = cTopLatitude >= nTopLatitude && cLeftLongitude <= nLeftLongitude && cRightLongitude >= nRightLongitude - && cBottomLatitude <= nBottomLatitude; - if (!inside || czoom != zoom) { - objects = loadingBugs(nTopLatitude, nLeftLongitude, nBottomLatitude, nRightLongitude); - cTopLatitude = nTopLatitude; - cLeftLongitude = nLeftLongitude; - cRightLongitude = nRightLongitude; - cBottomLatitude = nBottomLatitude; - czoom = zoom; - refreshMap(); - } - } - } - }); - msg.what = 1; - handlerToLoop.sendMessage(msg); - } - } + @Override public boolean onLongPressEvent(PointF point, RotatedTileBox tileBox) { @@ -219,6 +171,7 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider } public void getBugFromPoint(RotatedTileBox tb, PointF point, List res){ + List objects = data.getResults(); if (objects != null && view != null) { int ex = (int) point.x; int ey = (int) point.y; @@ -259,17 +212,11 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider } return false; } - - - public void clearCache() { - objects.clear(); - cTopLatitude = 0; - cBottomLatitude = 0; - cLeftLongitude = 0; - cRightLongitude = 0; + data.clearCache(); } + private static String readText(XmlPullParser parser, String key) throws XmlPullParserException, IOException { int tok; String text = ""; diff --git a/OsmAnd/src/net/osmand/plus/views/DownloadedRegionsLayer.java b/OsmAnd/src/net/osmand/plus/views/DownloadedRegionsLayer.java index 8f67e79241..fbceb220ba 100644 --- a/OsmAnd/src/net/osmand/plus/views/DownloadedRegionsLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/DownloadedRegionsLayer.java @@ -183,7 +183,6 @@ public class DownloadedRegionsLayer extends OsmandMapLayer { } } } - System.out.println("Queried regions " + result.size()); return result; } diff --git a/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java b/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java index e73dcf72e0..22a1744fb3 100644 --- a/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/OsmandMapLayer.java @@ -79,8 +79,12 @@ public abstract class OsmandMapLayer { return queriedBox; } + public T getResults() { + return results; + } + public boolean queriedBoxContains(final RotatedTileBox queriedData, final RotatedTileBox newBox) { - return queriedData != null && !queriedData.containsTileBox(newBox) && Math.abs(queriedData.getZoom() - newBox.getZoom()) <= ZOOM_THRESHOLD; + return queriedData != null && queriedData.containsTileBox(newBox) && Math.abs(queriedData.getZoom() - newBox.getZoom()) <= ZOOM_THRESHOLD; } public void queryNewData(RotatedTileBox tileBox) { @@ -150,6 +154,12 @@ public abstract class OsmandMapLayer { } } } + + public void clearCache() { + results = null; + queriedBox = null; + + } }