diff --git a/OsmAnd/res/layout/server_fragment.xml b/OsmAnd/res/layout/server_fragment.xml index 8b94edbfa5..35f9a16fe9 100644 --- a/OsmAnd/res/layout/server_fragment.xml +++ b/OsmAnd/res/layout/server_fragment.xml @@ -4,6 +4,7 @@ android:layout_height="match_parent" android:orientation="vertical" android:padding="32dp" + android:gravity="center" android:background="@color/color_black"> inMemoryCache = new ConcurrentLinkedQueue<>(); private final File externalCacheDir; - public boolean inMemoryCacheEnabled = false; + public boolean inMemoryCacheEnabled = true; public MetaTileFileSystemCache(OsmandApplication application) { externalCacheDir = new File( @@ -57,21 +57,40 @@ public class MetaTileFileSystemCache { } public TileEndpoint.MetaTileCache get(int zoom, int METATILE_SIZE, int x, int y) { - for (int tx = x - METATILE_SIZE + 1; tx < METATILE_SIZE + x - 1; tx++) { - for (int ty = y - METATILE_SIZE + 1; ty < METATILE_SIZE + y - 1; ty++) { - File file = new File(externalCacheDir, zoom + "_" + METATILE_SIZE + "_" + tx + "_" + ty); - if (file.exists()) { - TileEndpoint.MetaTileCache tile = new TileEndpoint.MetaTileCache( - BitmapFactory.decodeFile(file.getAbsolutePath()), - tx, ty, tx + METATILE_SIZE, ty + METATILE_SIZE, zoom - ); - if (inMemoryCacheEnabled) { - inMemoryCache.add(tile); - } - return tile; + int mx = (x / METATILE_SIZE) * METATILE_SIZE; + int my = (y / METATILE_SIZE) * METATILE_SIZE; + if (inMemoryCacheEnabled) { + for (TileEndpoint.MetaTileCache r : inMemoryCache) { + if (r.getZoom() == zoom && r.getEx() >= x && r.getEy() >= y && r.getSx() <= x && r.getSy() <= y) { + return r; } } } + File file = new File(externalCacheDir, zoom + "_" + METATILE_SIZE + "_" + mx + "_" + my); + if (file.exists()) { + TileEndpoint.MetaTileCache tile = new TileEndpoint.MetaTileCache( + BitmapFactory.decodeFile(file.getAbsolutePath()), + mx, my, mx + METATILE_SIZE - 1, my + METATILE_SIZE - 1, zoom); + if (inMemoryCacheEnabled) { + inMemoryCache.add(tile); + } + return tile; + } return null; } + + public void clearCache() { + clearInMemoryCache(); + clearFileCache(); + } + + private void clearFileCache() { + for (int i = 0; i < externalCacheDir.listFiles().length; i++) { + externalCacheDir.listFiles()[i].delete(); + } + } + + private void clearInMemoryCache() { + inMemoryCache.clear(); + } } diff --git a/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java b/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java index a20472d589..adbda78898 100644 --- a/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java +++ b/OsmAnd/src/net/osmand/plus/server/OsmAndHttpServer.java @@ -3,6 +3,7 @@ package net.osmand.plus.server; import android.webkit.MimeTypeMap; import fi.iki.elonen.NanoHTTPD; import net.osmand.PlatformUtil; +import net.osmand.data.RotatedTileBox; import net.osmand.plus.OsmandApplication; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.server.endpoints.TileEndpoint; @@ -49,7 +50,12 @@ public class OsmAndHttpServer extends NanoHTTPD { } public String getUrl() { - return "http://" + getHostname() + ":" + getListeningPort(); + RotatedTileBox rtb = mapActivity.getMapView().getCurrentRotatedTileBox(); + double lat = rtb.getLatitude(); + double lon = rtb.getLongitude(); + double z = rtb.getZoom(); + return "http://" + getHostname() + ":" + getListeningPort() + + "/?lat=" + lat + "&lon=" + lon + "&zoom=" + z; } private NanoHTTPD.Response routeApi(NanoHTTPD.IHTTPSession session) { diff --git a/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java b/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java index e081538d68..3752fcdafd 100644 --- a/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java +++ b/OsmAnd/src/net/osmand/plus/server/endpoints/TileEndpoint.java @@ -1,6 +1,6 @@ package net.osmand.plus.server.endpoints; -import android.graphics.Bitmap; +import android.graphics.*; import fi.iki.elonen.NanoHTTPD; import net.osmand.PlatformUtil; import net.osmand.data.RotatedTileBox; @@ -20,16 +20,58 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { private static final int TILE_SIZE_PX = 256; private static final int TILE_DENSITY = 2; private static final int TIMEOUT_STEP = 500; - private static final int TIMEOUT = 5000; + private static final int TIMEOUT = 10000; private static final int METATILE_SIZE = 2; private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class); private final MapActivity mapActivity; private final MetaTileFileSystemCache cache; + private final RotatedTileBox mapTileBoxCopy; public static class MetaTileCache { Bitmap bmp; int sx; + + public int getSx() { + return sx; + } + + public void setSx(int sx) { + this.sx = sx; + } + + public int getSy() { + return sy; + } + + public void setSy(int sy) { + this.sy = sy; + } + + public int getEx() { + return ex; + } + + public void setEx(int ex) { + this.ex = ex; + } + + public int getEy() { + return ey; + } + + public void setEy(int ey) { + this.ey = ey; + } + + public int getZoom() { + return zoom; + } + + public void setZoom(int zoom) { + this.zoom = zoom; + } + int sy; int ex; int ey; @@ -68,6 +110,9 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { public TileEndpoint(MapActivity mapActivity) { this.mapActivity = mapActivity; this.cache = new MetaTileFileSystemCache(mapActivity.getMyApplication()); + this.mapTileBoxCopy = mapActivity.getMapView().getCurrentRotatedTileBox().copy(); + //for debug + //this.cache.clearCache(); } @Override @@ -87,7 +132,10 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { int zoom = Integer.parseInt(prms[1]); int x = Integer.parseInt(prms[2]); int y = Integer.parseInt(prms[3]); - MetaTileCache res = cache.get(zoom, METATILE_SIZE, x, y); + MetaTileCache res; + synchronized (this) { + res = cache.get(zoom, METATILE_SIZE, x, y); + } if (res == null) { res = requestMetatile(x, y, zoom); if (res == null) { @@ -114,7 +162,6 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { int my = (y / METATILE_SIZE) * METATILE_SIZE; double lat = MapUtils.getLatitudeFromTile(zoom, my + 0.5 * METATILE_SIZE); double lon = MapUtils.getLongitudeFromTile(zoom, mx + 0.5 * METATILE_SIZE); - final RotatedTileBox cp = mapActivity.getMapView().getCurrentRotatedTileBox(); final RotatedTileBox rotatedTileBox = new RotatedTileBox.RotatedTileBoxBuilder() .setLocation(lat, lon) .setMapDensity(TILE_DENSITY).density(TILE_DENSITY) @@ -131,6 +178,23 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint { Thread.sleep(TIMEOUT_STEP); timeout += TIMEOUT_STEP; } + if (timeout >= TIMEOUT) { + res = new MetaTileCache(); + res.sx = mx; + res.ex = mx + METATILE_SIZE - 1; + res.sy = my; + res.ey = my + METATILE_SIZE - 1; + res.zoom = zoom; + Bitmap tempBmp = mapActivity.getMapView().getBufferBitmap(); + Canvas canvas = new Canvas(tempBmp); + Paint paint = new Paint(); + paint.setColor(Color.RED); + paint.setTextSize(12); + paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); + canvas.drawText("TIMEOUT", tempBmp.getWidth() / 2, tempBmp.getHeight() / 2, paint); + res.bmp = tempBmp.copy(tempBmp.getConfig(), true); + return res; + } if (!athread.areResourcesLoading()) { res = new MetaTileCache(); res.sx = mx;