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:
parent
1010e77a3f
commit
8ca0656412
6 changed files with 44 additions and 1 deletions
|
@ -28,4 +28,6 @@ public interface ITileSource {
|
||||||
|
|
||||||
public int getExpirationTimeMinutes();
|
public int getExpirationTimeMinutes();
|
||||||
|
|
||||||
|
public String getReferer();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,6 +91,7 @@ public class MapTileDownloader {
|
||||||
public final int xTile;
|
public final int xTile;
|
||||||
public final int yTile;
|
public final int yTile;
|
||||||
public final String url;
|
public final String url;
|
||||||
|
public String referer = null;
|
||||||
public boolean error;
|
public boolean error;
|
||||||
|
|
||||||
public DownloadRequest(String url, File fileToSave, int xTile, int yTile, int zoom) {
|
public DownloadRequest(String url, File fileToSave, int xTile, int yTile, int zoom) {
|
||||||
|
@ -262,6 +263,8 @@ public class MapTileDownloader {
|
||||||
try {
|
try {
|
||||||
URLConnection connection = NetworkUtils.getHttpURLConnection(request.url);
|
URLConnection connection = NetworkUtils.getHttpURLConnection(request.url);
|
||||||
connection.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$
|
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.setConnectTimeout(CONNECTION_TIMEOUT);
|
||||||
connection.setReadTimeout(CONNECTION_TIMEOUT);
|
connection.setReadTimeout(CONNECTION_TIMEOUT);
|
||||||
BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream(), 8 * 1024);
|
BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream(), 8 * 1024);
|
||||||
|
|
|
@ -140,6 +140,9 @@ public class TileSourceManager {
|
||||||
return expirationTimeMillis;
|
return expirationTimeMillis;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getReferer() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getTileSize() {
|
public int getTileSize() {
|
||||||
|
|
|
@ -10,6 +10,7 @@ import java.util.List;
|
||||||
|
|
||||||
import net.osmand.IndexConstants;
|
import net.osmand.IndexConstants;
|
||||||
import net.osmand.PlatformUtil;
|
import net.osmand.PlatformUtil;
|
||||||
|
import net.osmand.access.AccessibleToast;
|
||||||
import net.osmand.data.QuadRect;
|
import net.osmand.data.QuadRect;
|
||||||
import net.osmand.map.ITileSource;
|
import net.osmand.map.ITileSource;
|
||||||
import net.osmand.map.TileSourceManager;
|
import net.osmand.map.TileSourceManager;
|
||||||
|
@ -24,6 +25,7 @@ import bsh.Interpreter;
|
||||||
import android.database.sqlite.SQLiteDiskIOException;
|
import android.database.sqlite.SQLiteDiskIOException;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
|
||||||
public class SQLiteTileSource implements ITileSource {
|
public class SQLiteTileSource implements ITileSource {
|
||||||
|
@ -44,6 +46,7 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
private int expirationTimeMillis = -1; // never
|
private int expirationTimeMillis = -1; // never
|
||||||
private boolean isEllipsoid = false;
|
private boolean isEllipsoid = false;
|
||||||
private String rule = null;
|
private String rule = null;
|
||||||
|
private String referer = null;
|
||||||
|
|
||||||
static final int tileSize = 256;
|
static final int tileSize = 256;
|
||||||
private OsmandApplication ctx;
|
private OsmandApplication ctx;
|
||||||
|
@ -122,6 +125,8 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
}
|
}
|
||||||
return (String) bshInterpreter.eval("getTileUrl("+zoom+","+x+","+y+");");
|
return (String) bshInterpreter.eval("getTileUrl("+zoom+","+x+","+y+");");
|
||||||
} catch (bsh.EvalError e) {
|
} catch (bsh.EvalError e) {
|
||||||
|
WDebug.log("getUrlToLoad Error"+e.getMessage());
|
||||||
|
AccessibleToast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -164,6 +169,7 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
|
|
||||||
protected SQLiteConnection getDatabase(){
|
protected SQLiteConnection getDatabase(){
|
||||||
if((db == null || db.isClosed()) && file.exists() ){
|
if((db == null || db.isClosed()) && file.exists() ){
|
||||||
|
WDebug.log("Open "+file.getAbsolutePath());
|
||||||
try {
|
try {
|
||||||
onlyReadonlyAvailable = false;
|
onlyReadonlyAvailable = false;
|
||||||
db = ctx.getSQLiteAPI().openByAbsolutePath(file.getAbsolutePath(), false);
|
db = ctx.getSQLiteAPI().openByAbsolutePath(file.getAbsolutePath(), false);
|
||||||
|
@ -188,6 +194,10 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
if(ruleId != -1) {
|
if(ruleId != -1) {
|
||||||
rule = cursor.getString(ruleId);
|
rule = cursor.getString(ruleId);
|
||||||
}
|
}
|
||||||
|
int refererId = list.indexOf("referer");
|
||||||
|
if(refererId != -1) {
|
||||||
|
referer = cursor.getString(refererId);
|
||||||
|
}
|
||||||
int tnumbering = list.indexOf("tilenumbering");
|
int tnumbering = list.indexOf("tilenumbering");
|
||||||
if(tnumbering != -1) {
|
if(tnumbering != -1) {
|
||||||
inversiveZoom = "BigPlanet".equalsIgnoreCase(cursor.getString(tnumbering));
|
inversiveZoom = "BigPlanet".equalsIgnoreCase(cursor.getString(tnumbering));
|
||||||
|
@ -442,13 +452,26 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void closeDB(){
|
public void closeDB(){
|
||||||
|
WDebug.log("closeDB");
|
||||||
bshInterpreter = null;
|
bshInterpreter = null;
|
||||||
|
if(timeSupported)
|
||||||
|
clearOld();
|
||||||
if(db != null){
|
if(db != null){
|
||||||
db.close();
|
db.close();
|
||||||
db = null;
|
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
|
@Override
|
||||||
public boolean couldBeDownloadedFromInternet() {
|
public boolean couldBeDownloadedFromInternet() {
|
||||||
if(getDatabase() == null || getDatabase().isReadOnly() || onlyReadonlyAvailable){
|
if(getDatabase() == null || getDatabase().isReadOnly() || onlyReadonlyAvailable){
|
||||||
|
@ -474,6 +497,9 @@ public class SQLiteTileSource implements ITileSource {
|
||||||
return expirationTimeMillis;
|
return expirationTimeMillis;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getReferer() {
|
||||||
|
return referer;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -170,6 +170,15 @@ public class AsyncLoadingThread extends Thread {
|
||||||
this.tileId = tileId;
|
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 {
|
public void saveTile(InputStream inputStream) throws IOException {
|
||||||
if(tileSource instanceof SQLiteTileSource){
|
if(tileSource instanceof SQLiteTileSource){
|
||||||
ByteArrayOutputStream stream = null;
|
ByteArrayOutputStream stream = null;
|
||||||
|
|
|
@ -323,7 +323,7 @@ public class ResourceManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TileLoadDownloadRequest req = new TileLoadDownloadRequest(dirWithTiles, url, toSave,
|
TileLoadDownloadRequest req = new TileLoadDownloadRequest(dirWithTiles, url, toSave,
|
||||||
tileId, map, x, y, zoom);
|
tileId, map, x, y, zoom, map.getReferer());
|
||||||
if(sync){
|
if(sync){
|
||||||
return getRequestedImageTile(req);
|
return getRequestedImageTile(req);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue