Fix issue 413. Freeze UI for downloading with sqlite
This commit is contained in:
parent
6df0671597
commit
96dccf0440
3 changed files with 36 additions and 82 deletions
|
@ -30,7 +30,6 @@ import net.osmand.osm.MapUtils;
|
|||
import net.osmand.plus.activities.OsmandApplication;
|
||||
import net.osmand.plus.render.BaseOsmandRender;
|
||||
import net.osmand.plus.render.MapRenderRepositories;
|
||||
import net.osmand.plus.render.RendererRegistry;
|
||||
import net.osmand.plus.views.POIMapLayer;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
@ -174,6 +173,9 @@ public class ResourceManager {
|
|||
if(!imagesOnFS.containsKey(file)){
|
||||
boolean ex = false;
|
||||
if(map instanceof SQLiteTileSource){
|
||||
if(((SQLiteTileSource) map).isLocked()){
|
||||
return false;
|
||||
}
|
||||
ex = ((SQLiteTileSource) map).exists(x, y, zoom);
|
||||
} else {
|
||||
if(file == null){
|
||||
|
@ -308,6 +310,9 @@ public class ResourceManager {
|
|||
}
|
||||
if (req.dirWithTiles.canRead() && !downloader.isFileCurrentlyDownloaded(req.fileToSave)) {
|
||||
long time = System.currentTimeMillis();
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Start loaded file : " + req.tileId + " " + Thread.currentThread().getName()); //$NON-NLS-1$ //$NON-NLS-2$
|
||||
}
|
||||
Bitmap bmp = null;
|
||||
if (req.tileSource instanceof SQLiteTileSource) {
|
||||
bmp = ((SQLiteTileSource) req.tileSource).getImage(req.xTile, req.yTile, req.zoom);
|
||||
|
|
|
@ -34,6 +34,7 @@ public class SQLiteTileSource implements ITileSource {
|
|||
private final File file;
|
||||
private int minZoom = 1;
|
||||
private int maxZoom = 17;
|
||||
private boolean locked = false;
|
||||
|
||||
public SQLiteTileSource(File f){
|
||||
this.file = f;
|
||||
|
@ -167,6 +168,10 @@ public class SQLiteTileSource implements ITileSource {
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isLocked() {
|
||||
return locked;
|
||||
}
|
||||
|
||||
public Bitmap getImage(int x, int y, int zoom) {
|
||||
SQLiteDatabase db = getDatabase();
|
||||
|
@ -207,23 +212,28 @@ public class SQLiteTileSource implements ITileSource {
|
|||
if(exists(x, y, zoom)){
|
||||
return;
|
||||
}
|
||||
ByteBuffer buf = ByteBuffer.allocate((int) fileToSave.length());
|
||||
FileInputStream is = new FileInputStream(fileToSave);
|
||||
int i = 0;
|
||||
byte[] b = new byte[BUF_SIZE];
|
||||
while((i=is.read(b, 0, BUF_SIZE)) > - 1){
|
||||
buf.put(b, 0, i);
|
||||
try {
|
||||
locked = true;
|
||||
ByteBuffer buf = ByteBuffer.allocate((int) fileToSave.length());
|
||||
FileInputStream is = new FileInputStream(fileToSave);
|
||||
int i = 0;
|
||||
byte[] b = new byte[BUF_SIZE];
|
||||
while ((i = is.read(b, 0, BUF_SIZE)) > -1) {
|
||||
buf.put(b, 0, i);
|
||||
}
|
||||
|
||||
SQLiteStatement statement = db.compileStatement("INSERT INTO tiles VALUES(?, ?, ?, ?, ?)"); //$NON-NLS-1$
|
||||
statement.bindLong(1, x);
|
||||
statement.bindLong(2, y);
|
||||
statement.bindLong(3, 17 - zoom);
|
||||
statement.bindLong(4, 0);
|
||||
statement.bindBlob(5, buf.array());
|
||||
statement.execute();
|
||||
statement.close();
|
||||
} finally {
|
||||
locked = false;
|
||||
}
|
||||
|
||||
SQLiteStatement statement = db.compileStatement("INSERT INTO tiles VALUES(?, ?, ?, ?, ?)"); //$NON-NLS-1$
|
||||
statement.bindLong(1, x);
|
||||
statement.bindLong(2, y);
|
||||
statement.bindLong(3, 17 - zoom);
|
||||
statement.bindLong(4, 0);
|
||||
statement.bindBlob(5, buf.array());
|
||||
statement.execute();
|
||||
statement.close();
|
||||
|
||||
}
|
||||
|
||||
public void closeDB(){
|
||||
|
|
|
@ -100,7 +100,7 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
|
|||
private Map<OsmandMapLayer, Float> zOrders = new HashMap<OsmandMapLayer, Float>();
|
||||
|
||||
// UI Part
|
||||
// handler to refresh map (in ui thread - not necessary in ui thread, but msg queue is desirable).
|
||||
// handler to refresh map (in ui thread - ui thread is not necessary, but msg queue is required).
|
||||
protected Handler handler = new Handler();
|
||||
|
||||
private AnimateDraggingMapThread animatedDraggingThread;
|
||||
|
@ -614,71 +614,10 @@ public class OsmandMapTileView extends SurfaceView implements IMapDownloaderCall
|
|||
}
|
||||
|
||||
public void tileDownloaded(DownloadRequest request) {
|
||||
if (request == null || rotate != 0) {
|
||||
// if image is rotated call refresh the whole canvas
|
||||
// because we can't find dirty rectangular region
|
||||
|
||||
// if request null then we don't know exact images were changed
|
||||
refreshMap();
|
||||
return;
|
||||
}
|
||||
if (request.error) {
|
||||
return;
|
||||
}
|
||||
if (request.zoom != getZoom()) {
|
||||
refreshMap();
|
||||
return;
|
||||
}
|
||||
float w = getCenterPointX();
|
||||
float h = getCenterPointY();
|
||||
float tileX = getXTile();
|
||||
float tileY = getYTile();
|
||||
|
||||
SurfaceHolder holder = getHolder();
|
||||
synchronized (holder) {
|
||||
tilesRect.set(request.xTile, request.yTile, request.xTile + 1, request.yTile + 1);
|
||||
calculatePixelRectangle(boundsRect, w, h, tileX, tileY, tilesRect);
|
||||
|
||||
if (boundsRect.left > getWidth() || boundsRect.right < 0 || boundsRect.bottom < 0 || boundsRect.top > getHeight()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Canvas canvas = holder.lockCanvas(boundsRect);
|
||||
if (canvas != null) {
|
||||
boolean nightMode = false;
|
||||
if(application != null){
|
||||
Boolean dayNightRenderer = application.getDaynightHelper().getDayNightRenderer();
|
||||
if(dayNightRenderer != null){
|
||||
nightMode = !dayNightRenderer.booleanValue();
|
||||
}
|
||||
}
|
||||
canvas.save();
|
||||
canvas.rotate(rotate, w, h);
|
||||
|
||||
try {
|
||||
Bitmap bmp = null;
|
||||
if (map != null) {
|
||||
ResourceManager mgr = getApplication().getResourceManager();
|
||||
bmp = mgr.getTileImageForMapSync(null, map, request.xTile, request.yTile, request.zoom, false);
|
||||
}
|
||||
|
||||
float x = (request.xTile - tileX) * getTileSize() + w;
|
||||
float y = (request.yTile - tileY) * getTileSize() + h;
|
||||
float tileSize = getTileSize();
|
||||
if (bmp == null) {
|
||||
drawEmptyTile(canvas, x, y, tileSize, nightMode);
|
||||
} else {
|
||||
bitmapToZoom.set(0, 0, getSourceTileSize(), getSourceTileSize());
|
||||
bitmapToDraw.set(x, y, x + tileSize, y + tileSize);
|
||||
canvas.drawBitmap(bmp, bitmapToZoom, bitmapToDraw, paintBitmap);
|
||||
}
|
||||
drawOverMap(canvas, latlonRect, nightMode);
|
||||
} finally {
|
||||
holder.unlockCanvasAndPost(canvas);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// force to refresh map because image can be loaded from different threads
|
||||
// and threads can block each other especially for sqlite images when they
|
||||
// are inserting into db they block main thread
|
||||
refreshMap();
|
||||
}
|
||||
|
||||
// ///////////////////////////////// DRAGGING PART ///////////////////////////////////////
|
||||
|
|
Loading…
Reference in a new issue