From ef5807ba3c5b97b2c730a3a851dc7addc4535e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jind=C5=99ich=20Makovi=C4=8Dka?= Date: Mon, 3 Dec 2012 23:17:03 +0100 Subject: [PATCH] allow exists() to check either for an exact tile or a parent tile --- .../src/net/osmand/plus/ResourceManager.java | 6 +- .../src/net/osmand/plus/SQLiteTileSource.java | 61 +++++++++---------- .../plus/activities/DownloadTilesDialog.java | 2 +- .../net/osmand/plus/views/MapTileLayer.java | 6 +- 4 files changed, 36 insertions(+), 39 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/ResourceManager.java b/OsmAnd/src/net/osmand/plus/ResourceManager.java index 2c83c6c0f1..863d34b0ff 100644 --- a/OsmAnd/src/net/osmand/plus/ResourceManager.java +++ b/OsmAnd/src/net/osmand/plus/ResourceManager.java @@ -222,14 +222,14 @@ public class ResourceManager { } - public synchronized boolean tileExistOnFileSystem(String file, ITileSource map, int x, int y, int zoom){ + public synchronized boolean tileExistOnFileSystem(String file, ITileSource map, int x, int y, int zoom, boolean exact){ if(!imagesOnFS.containsKey(file)){ boolean ex = false; if(map instanceof SQLiteTileSource){ if(((SQLiteTileSource) map).isLocked()){ return false; } - ex = ((SQLiteTileSource) map).exists(x, y, zoom); + ex = ((SQLiteTileSource) map).exists(x, y, zoom, exact); } else { if(file == null){ file = calculateTileId(map, x, y, zoom); @@ -306,7 +306,7 @@ public class ResourceManager { if (loadFromFs && cacheOfImages.get(tileId) == null && map != null) { boolean locked = map instanceof SQLiteTileSource && ((SQLiteTileSource) map).isLocked(); - if(!loadFromInternetIfNeeded && !locked && !tileExistOnFileSystem(tileId, map, x, y, zoom)){ + if(!loadFromInternetIfNeeded && !locked && !tileExistOnFileSystem(tileId, map, x, y, zoom, false)){ return null; } String url = loadFromInternetIfNeeded ? map.getUrlToLoad(x, y, zoom) : null; diff --git a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java index 4a68f7b4fa..ee9da62dd9 100644 --- a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java +++ b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java @@ -158,42 +158,39 @@ public class SQLiteTileSource implements ITileSource { return db; } - public boolean exists(int zoom) { + public boolean exists(int x, int y, int zoom, boolean exact) { SQLiteDatabase db = getDatabase(); if(db == null){ return false; } - return true; // Always true in resampling mode -// Cursor cursor = db.rawQuery("SELECT 1 FROM tiles WHERE z = ? LIMIT 1", new String[] {(17 - zoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ -// try { -// boolean e = cursor.moveToFirst(); -// cursor.close(); -// return e; -// } catch (SQLiteDiskIOException e) { -// return false; -// } - } - - public boolean exists(int x, int y, int zoom) { - SQLiteDatabase db = getDatabase(); - if(db == null){ - return false; - } - //return true; // Cheat to test resampling /o modifying ressourceManager long time = System.currentTimeMillis(); - int n = zoom - baseZoom; - int base_xtile = x >> n; - int base_ytile = y >> n; - Cursor cursor = db.rawQuery("SELECT 1 FROM tiles WHERE x = ? AND y = ? AND z = ?", new String[] {base_xtile+"", base_ytile+"",(17 - baseZoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ - try { - boolean e = cursor.moveToFirst(); - cursor.close(); - if (log.isDebugEnabled()) { - log.debug("Checking parent tile existance x = " + base_xtile + " y = " + base_ytile + " z = " + baseZoom + " for " + (System.currentTimeMillis() - time)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + if (exact || zoom <= baseZoom) { + Cursor cursor = db.rawQuery("SELECT 1 FROM tiles WHERE x = ? AND y = ? AND z = ?", new String[] {x+"", y+"",(17 - zoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ + try { + boolean e = cursor.moveToFirst(); + cursor.close(); + if (log.isDebugEnabled()) { + log.debug("Checking tile existance x = " + x + " y = " + y + " z = " + zoom + " for " + (System.currentTimeMillis() - time)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + } + return e; + } catch (SQLiteDiskIOException e) { + return false; + } + } else { + int n = zoom - baseZoom; + int base_xtile = x >> n; + int base_ytile = y >> n; + Cursor cursor = db.rawQuery("SELECT 1 FROM tiles WHERE x = ? AND y = ? AND z = ?", new String[] {base_xtile+"", base_ytile+"",(17 - baseZoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ + try { + boolean e = cursor.moveToFirst(); + cursor.close(); + if (log.isDebugEnabled()) { + log.debug("Checking parent tile existance x = " + base_xtile + " y = " + base_ytile + " z = " + baseZoom + " for " + (System.currentTimeMillis() - time)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + } + return e; + } catch (SQLiteDiskIOException e) { + return false; } - return e; - } catch (SQLiteDiskIOException e) { - return false; } } @@ -260,7 +257,7 @@ public class SQLiteTileSource implements ITileSource { if(db == null){ return null; } - if ( zoom <= baseZoom) { + if (zoom <= baseZoom) { // return the normal tile if exists Cursor cursor = db.rawQuery("SELECT image FROM tiles WHERE x = ? AND y = ? AND z = ?", new String[] {x+"", y+"",(17 - zoom)+""}); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$ byte[] blob = null; @@ -340,7 +337,7 @@ public class SQLiteTileSource implements ITileSource { if (db == null || db.isReadOnly()) { return; } - if (exists(x, y, zoom)) { + if (exists(x, y, zoom, true)) { return; } ByteBuffer buf = ByteBuffer.allocate((int) fileToSave.length()); diff --git a/OsmAnd/src/net/osmand/plus/activities/DownloadTilesDialog.java b/OsmAnd/src/net/osmand/plus/activities/DownloadTilesDialog.java index 0619504bbd..29895cfc25 100644 --- a/OsmAnd/src/net/osmand/plus/activities/DownloadTilesDialog.java +++ b/OsmAnd/src/net/osmand/plus/activities/DownloadTilesDialog.java @@ -167,7 +167,7 @@ public class DownloadTilesDialog { for (int x = x1; x <= x2 && !cancel; x++) { for (int y = y1; y <= y2 && !cancel; y++) { String tileId = rm.calculateTileId(map, x, y, z); - if (rm.tileExistOnFileSystem(tileId, map, x, y, z)) { + if (rm.tileExistOnFileSystem(tileId, map, x, y, z, true)) { progressDlg.setProgress(progressDlg.getProgress() + 1); } else { rm.getTileImageForMapSync(tileId, map, x, y, z, true); diff --git a/OsmAnd/src/net/osmand/plus/views/MapTileLayer.java b/OsmAnd/src/net/osmand/plus/views/MapTileLayer.java index a3ecc5a8b2..222efd43c6 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapTileLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapTileLayer.java @@ -158,7 +158,7 @@ public class MapTileLayer extends BaseMapLayer { float y1 = (top + j - tileY) * ftileSize + h; String ordImgTile = mgr.calculateTileId(map, leftPlusI, topPlusJ, nzoom); // asking tile image async - boolean imgExist = mgr.tileExistOnFileSystem(ordImgTile, map, leftPlusI, topPlusJ, nzoom); + boolean imgExist = mgr.tileExistOnFileSystem(ordImgTile, map, leftPlusI, topPlusJ, nzoom, false); Bitmap bmp = null; boolean originalBeLoaded = useInternet && nzoom <= maxLevel; if (imgExist || originalBeLoaded) { @@ -178,11 +178,11 @@ public class MapTileLayer extends BaseMapLayer { } } if (!originalBeLoaded && !imgExist) { - if (mgr.tileExistOnFileSystem(imgTile2, map, leftPlusI / 2, topPlusJ / 2, nzoom - 1) + if (mgr.tileExistOnFileSystem(imgTile2, map, leftPlusI / 2, topPlusJ / 2, nzoom - 1, false) || (useInternet && nzoom - 1 <= maxLevel)) { bmp = mgr.getTileImageForMapAsync(imgTile2, map, leftPlusI / 2, topPlusJ / 2, nzoom - 1, useInternet); div = 2; - } else if (mgr.tileExistOnFileSystem(imgTile4, map, leftPlusI / 4, topPlusJ / 4, nzoom - 2) + } else if (mgr.tileExistOnFileSystem(imgTile4, map, leftPlusI / 4, topPlusJ / 4, nzoom - 2, false) || (useInternet && nzoom - 2 <= maxLevel)) { bmp = mgr.getTileImageForMapAsync(imgTile4, map, leftPlusI / 4, topPlusJ / 4, nzoom - 2, useInternet); div = 4;