From 8ca0656412fd2717828d0af93434b49c2e6811d1 Mon Sep 17 00:00:00 2001 From: ilasica Date: Fri, 9 Oct 2015 11:35:21 +0300 Subject: [PATCH] 1. Means to set Referer field in connection to a tile server. Support of new optional field Referer in table INFO of sqlitedb 2. Delete old tiles from sqlitedb in case of using field time. Usefull for unstable tiles, for example traffic map. --- .../src/net/osmand/map/ITileSource.java | 2 ++ .../src/net/osmand/map/MapTileDownloader.java | 3 +++ .../src/net/osmand/map/TileSourceManager.java | 3 +++ .../src/net/osmand/plus/SQLiteTileSource.java | 26 +++++++++++++++++++ .../plus/resources/AsyncLoadingThread.java | 9 +++++++ .../plus/resources/ResourceManager.java | 2 +- 6 files changed, 44 insertions(+), 1 deletion(-) diff --git a/OsmAnd-java/src/net/osmand/map/ITileSource.java b/OsmAnd-java/src/net/osmand/map/ITileSource.java index 67e55c9db0..0e06aeeb32 100644 --- a/OsmAnd-java/src/net/osmand/map/ITileSource.java +++ b/OsmAnd-java/src/net/osmand/map/ITileSource.java @@ -27,5 +27,7 @@ public interface ITileSource { public int getExpirationTimeMillis(); public int getExpirationTimeMinutes(); + + public String getReferer(); } diff --git a/OsmAnd-java/src/net/osmand/map/MapTileDownloader.java b/OsmAnd-java/src/net/osmand/map/MapTileDownloader.java index 817dbf2cbb..a212843714 100644 --- a/OsmAnd-java/src/net/osmand/map/MapTileDownloader.java +++ b/OsmAnd-java/src/net/osmand/map/MapTileDownloader.java @@ -91,6 +91,7 @@ public class MapTileDownloader { public final int xTile; public final int yTile; public final String url; + public String referer = null; public boolean error; public DownloadRequest(String url, File fileToSave, int xTile, int yTile, int zoom) { @@ -262,6 +263,8 @@ public class MapTileDownloader { try { URLConnection connection = NetworkUtils.getHttpURLConnection(request.url); connection.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$ + if(request.referer != null) + connection.setRequestProperty("Referer", request.referer); //$NON-NLS-1$ connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(CONNECTION_TIMEOUT); BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream(), 8 * 1024); diff --git a/OsmAnd-java/src/net/osmand/map/TileSourceManager.java b/OsmAnd-java/src/net/osmand/map/TileSourceManager.java index 5c25fa60e9..337de4671b 100644 --- a/OsmAnd-java/src/net/osmand/map/TileSourceManager.java +++ b/OsmAnd-java/src/net/osmand/map/TileSourceManager.java @@ -140,6 +140,9 @@ public class TileSourceManager { return expirationTimeMillis; } + public String getReferer() { + return null; + } @Override public int getTileSize() { diff --git a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java index 6fd4494cb2..88b9aedd4a 100644 --- a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java +++ b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java @@ -10,6 +10,7 @@ import java.util.List; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; +import net.osmand.access.AccessibleToast; import net.osmand.data.QuadRect; import net.osmand.map.ITileSource; import net.osmand.map.TileSourceManager; @@ -24,6 +25,7 @@ import bsh.Interpreter; import android.database.sqlite.SQLiteDiskIOException; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import android.widget.Toast; public class SQLiteTileSource implements ITileSource { @@ -44,6 +46,7 @@ public class SQLiteTileSource implements ITileSource { private int expirationTimeMillis = -1; // never private boolean isEllipsoid = false; private String rule = null; + private String referer = null; static final int tileSize = 256; private OsmandApplication ctx; @@ -122,6 +125,8 @@ public class SQLiteTileSource implements ITileSource { } return (String) bshInterpreter.eval("getTileUrl("+zoom+","+x+","+y+");"); } catch (bsh.EvalError e) { + WDebug.log("getUrlToLoad Error"+e.getMessage()); + AccessibleToast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show(); log.error(e.getMessage(), e); return null; } @@ -164,6 +169,7 @@ public class SQLiteTileSource implements ITileSource { protected SQLiteConnection getDatabase(){ if((db == null || db.isClosed()) && file.exists() ){ + WDebug.log("Open "+file.getAbsolutePath()); try { onlyReadonlyAvailable = false; db = ctx.getSQLiteAPI().openByAbsolutePath(file.getAbsolutePath(), false); @@ -188,6 +194,10 @@ public class SQLiteTileSource implements ITileSource { if(ruleId != -1) { rule = cursor.getString(ruleId); } + int refererId = list.indexOf("referer"); + if(refererId != -1) { + referer = cursor.getString(refererId); + } int tnumbering = list.indexOf("tilenumbering"); if(tnumbering != -1) { inversiveZoom = "BigPlanet".equalsIgnoreCase(cursor.getString(tnumbering)); @@ -442,13 +452,26 @@ public class SQLiteTileSource implements ITileSource { } public void closeDB(){ + WDebug.log("closeDB"); bshInterpreter = null; + if(timeSupported) + clearOld(); if(db != null){ db.close(); db = null; } } + public void clearOld() { + SQLiteConnection db = getDatabase(); + if(db == null || db.isReadOnly()){ + return; + } + WDebug.log("DELETE FROM tiles WHERE time<"+(System.currentTimeMillis()-getExpirationTimeMillis())); + db.execSQL("DELETE FROM tiles WHERE time<"+(System.currentTimeMillis()-getExpirationTimeMillis())); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ + db.execSQL("VACUUM"); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ + } + @Override public boolean couldBeDownloadedFromInternet() { if(getDatabase() == null || getDatabase().isReadOnly() || onlyReadonlyAvailable){ @@ -474,6 +497,9 @@ public class SQLiteTileSource implements ITileSource { return expirationTimeMillis; } + public String getReferer() { + return referer; + } } diff --git a/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java b/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java index 1b12ca33de..445f4c403e 100644 --- a/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java +++ b/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java @@ -170,6 +170,15 @@ public class AsyncLoadingThread extends Thread { this.tileId = tileId; } + public TileLoadDownloadRequest(File dirWithTiles, String url, File fileToSave, String tileId, ITileSource source, int tileX, + int tileY, int zoom, String referer) { + super(url, fileToSave, tileX, tileY, zoom); + this.dirWithTiles = dirWithTiles; + this.tileSource = source; + this.tileId = tileId; + this.referer = referer; + } + public void saveTile(InputStream inputStream) throws IOException { if(tileSource instanceof SQLiteTileSource){ ByteArrayOutputStream stream = null; diff --git a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java index 6a96641565..c0c69d2ad3 100644 --- a/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/resources/ResourceManager.java @@ -323,7 +323,7 @@ public class ResourceManager { } } TileLoadDownloadRequest req = new TileLoadDownloadRequest(dirWithTiles, url, toSave, - tileId, map, x, y, zoom); + tileId, map, x, y, zoom, map.getReferer()); if(sync){ return getRequestedImageTile(req); } else {