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.
This commit is contained in:
ilasica 2015-10-09 11:35:21 +03:00
parent 1010e77a3f
commit 8ca0656412
6 changed files with 44 additions and 1 deletions

View file

@ -27,5 +27,7 @@ public interface ITileSource {
public int getExpirationTimeMillis();
public int getExpirationTimeMinutes();
public String getReferer();
}

View file

@ -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);

View file

@ -140,6 +140,9 @@ public class TileSourceManager {
return expirationTimeMillis;
}
public String getReferer() {
return null;
}
@Override
public int getTileSize() {

View file

@ -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;
}
}

View file

@ -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;

View file

@ -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 {