cache added
This commit is contained in:
parent
6c86997d2c
commit
d0c6270a00
5 changed files with 110 additions and 21 deletions
|
@ -4,6 +4,7 @@
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:padding="32dp"
|
android:padding="32dp"
|
||||||
|
android:gravity="center"
|
||||||
android:background="@color/color_black">
|
android:background="@color/color_black">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
|
|
@ -71,7 +71,6 @@ public class AsyncLoadingThread extends Thread {
|
||||||
|
|
||||||
public boolean areResourcesLoading() {
|
public boolean areResourcesLoading() {
|
||||||
return !requests.isEmpty();
|
return !requests.isEmpty();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void requestToLoadTile(TileLoadDownloadRequest req) {
|
public void requestToLoadTile(TileLoadDownloadRequest req) {
|
||||||
|
|
|
@ -14,11 +14,11 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
public class MetaTileFileSystemCache {
|
public class MetaTileFileSystemCache {
|
||||||
private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class);
|
private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class);
|
||||||
private static final Object TILES_FOLDER = "tiles";
|
private static final Object TILES_FOLDER = "tiles";
|
||||||
private static final int MAX_IN_MEMORY_CACHE_SIZE = 4;
|
private static final int MAX_IN_MEMORY_CACHE_SIZE = 128;
|
||||||
private static final int MAX_CACHE_SIZE = 4;
|
private static final int MAX_CACHE_SIZE = 64;
|
||||||
private final ConcurrentLinkedQueue<TileEndpoint.MetaTileCache> inMemoryCache = new ConcurrentLinkedQueue<>();
|
private final ConcurrentLinkedQueue<TileEndpoint.MetaTileCache> inMemoryCache = new ConcurrentLinkedQueue<>();
|
||||||
private final File externalCacheDir;
|
private final File externalCacheDir;
|
||||||
public boolean inMemoryCacheEnabled = false;
|
public boolean inMemoryCacheEnabled = true;
|
||||||
|
|
||||||
public MetaTileFileSystemCache(OsmandApplication application) {
|
public MetaTileFileSystemCache(OsmandApplication application) {
|
||||||
externalCacheDir = new File(
|
externalCacheDir = new File(
|
||||||
|
@ -57,21 +57,40 @@ public class MetaTileFileSystemCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
public TileEndpoint.MetaTileCache get(int zoom, int METATILE_SIZE, int x, int y) {
|
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++) {
|
int mx = (x / METATILE_SIZE) * METATILE_SIZE;
|
||||||
for (int ty = y - METATILE_SIZE + 1; ty < METATILE_SIZE + y - 1; ty++) {
|
int my = (y / METATILE_SIZE) * METATILE_SIZE;
|
||||||
File file = new File(externalCacheDir, zoom + "_" + METATILE_SIZE + "_" + tx + "_" + ty);
|
if (inMemoryCacheEnabled) {
|
||||||
if (file.exists()) {
|
for (TileEndpoint.MetaTileCache r : inMemoryCache) {
|
||||||
TileEndpoint.MetaTileCache tile = new TileEndpoint.MetaTileCache(
|
if (r.getZoom() == zoom && r.getEx() >= x && r.getEy() >= y && r.getSx() <= x && r.getSy() <= y) {
|
||||||
BitmapFactory.decodeFile(file.getAbsolutePath()),
|
return r;
|
||||||
tx, ty, tx + METATILE_SIZE, ty + METATILE_SIZE, zoom
|
|
||||||
);
|
|
||||||
if (inMemoryCacheEnabled) {
|
|
||||||
inMemoryCache.add(tile);
|
|
||||||
}
|
|
||||||
return tile;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package net.osmand.plus.server;
|
||||||
import android.webkit.MimeTypeMap;
|
import android.webkit.MimeTypeMap;
|
||||||
import fi.iki.elonen.NanoHTTPD;
|
import fi.iki.elonen.NanoHTTPD;
|
||||||
import net.osmand.PlatformUtil;
|
import net.osmand.PlatformUtil;
|
||||||
|
import net.osmand.data.RotatedTileBox;
|
||||||
import net.osmand.plus.OsmandApplication;
|
import net.osmand.plus.OsmandApplication;
|
||||||
import net.osmand.plus.activities.MapActivity;
|
import net.osmand.plus.activities.MapActivity;
|
||||||
import net.osmand.plus.server.endpoints.TileEndpoint;
|
import net.osmand.plus.server.endpoints.TileEndpoint;
|
||||||
|
@ -49,7 +50,12 @@ public class OsmAndHttpServer extends NanoHTTPD {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUrl() {
|
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) {
|
private NanoHTTPD.Response routeApi(NanoHTTPD.IHTTPSession session) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package net.osmand.plus.server.endpoints;
|
package net.osmand.plus.server.endpoints;
|
||||||
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.*;
|
||||||
import fi.iki.elonen.NanoHTTPD;
|
import fi.iki.elonen.NanoHTTPD;
|
||||||
import net.osmand.PlatformUtil;
|
import net.osmand.PlatformUtil;
|
||||||
import net.osmand.data.RotatedTileBox;
|
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_SIZE_PX = 256;
|
||||||
private static final int TILE_DENSITY = 2;
|
private static final int TILE_DENSITY = 2;
|
||||||
private static final int TIMEOUT_STEP = 500;
|
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 int METATILE_SIZE = 2;
|
||||||
|
|
||||||
private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class);
|
private static final Log LOG = PlatformUtil.getLog(TileEndpoint.class);
|
||||||
private final MapActivity mapActivity;
|
private final MapActivity mapActivity;
|
||||||
private final MetaTileFileSystemCache cache;
|
private final MetaTileFileSystemCache cache;
|
||||||
|
private final RotatedTileBox mapTileBoxCopy;
|
||||||
|
|
||||||
public static class MetaTileCache {
|
public static class MetaTileCache {
|
||||||
Bitmap bmp;
|
Bitmap bmp;
|
||||||
int sx;
|
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 sy;
|
||||||
int ex;
|
int ex;
|
||||||
int ey;
|
int ey;
|
||||||
|
@ -68,6 +110,9 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
|
||||||
public TileEndpoint(MapActivity mapActivity) {
|
public TileEndpoint(MapActivity mapActivity) {
|
||||||
this.mapActivity = mapActivity;
|
this.mapActivity = mapActivity;
|
||||||
this.cache = new MetaTileFileSystemCache(mapActivity.getMyApplication());
|
this.cache = new MetaTileFileSystemCache(mapActivity.getMyApplication());
|
||||||
|
this.mapTileBoxCopy = mapActivity.getMapView().getCurrentRotatedTileBox().copy();
|
||||||
|
//for debug
|
||||||
|
//this.cache.clearCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -87,7 +132,10 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
|
||||||
int zoom = Integer.parseInt(prms[1]);
|
int zoom = Integer.parseInt(prms[1]);
|
||||||
int x = Integer.parseInt(prms[2]);
|
int x = Integer.parseInt(prms[2]);
|
||||||
int y = Integer.parseInt(prms[3]);
|
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) {
|
if (res == null) {
|
||||||
res = requestMetatile(x, y, zoom);
|
res = requestMetatile(x, y, zoom);
|
||||||
if (res == null) {
|
if (res == null) {
|
||||||
|
@ -114,7 +162,6 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
|
||||||
int my = (y / METATILE_SIZE) * METATILE_SIZE;
|
int my = (y / METATILE_SIZE) * METATILE_SIZE;
|
||||||
double lat = MapUtils.getLatitudeFromTile(zoom, my + 0.5 * METATILE_SIZE);
|
double lat = MapUtils.getLatitudeFromTile(zoom, my + 0.5 * METATILE_SIZE);
|
||||||
double lon = MapUtils.getLongitudeFromTile(zoom, mx + 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()
|
final RotatedTileBox rotatedTileBox = new RotatedTileBox.RotatedTileBoxBuilder()
|
||||||
.setLocation(lat, lon)
|
.setLocation(lat, lon)
|
||||||
.setMapDensity(TILE_DENSITY).density(TILE_DENSITY)
|
.setMapDensity(TILE_DENSITY).density(TILE_DENSITY)
|
||||||
|
@ -131,6 +178,23 @@ public class TileEndpoint implements OsmAndHttpServer.ApiEndpoint {
|
||||||
Thread.sleep(TIMEOUT_STEP);
|
Thread.sleep(TIMEOUT_STEP);
|
||||||
timeout += 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()) {
|
if (!athread.areResourcesLoading()) {
|
||||||
res = new MetaTileCache();
|
res = new MetaTileCache();
|
||||||
res.sx = mx;
|
res.sx = mx;
|
||||||
|
|
Loading…
Reference in a new issue