From 9910e07a68ba897cbe40217f24966b2dc39e1e78 Mon Sep 17 00:00:00 2001 From: Alexey Pelykh Date: Tue, 9 Dec 2014 18:39:44 +0200 Subject: [PATCH] Blah Blah --- .../src/net/osmand/map/ITileSource.java | 27 +++++---- .../CoreResourcesFromAndroidAssetsCustom.java | 8 +-- .../core/android/TileSourceProxyProvider.java | 57 +++++++++++++++++++ .../src/net/osmand/plus/SQLiteTileSource.java | 40 +++++++++---- .../net/osmand/plus/views/MapTileLayer.java | 2 + 5 files changed, 108 insertions(+), 26 deletions(-) create mode 100755 OsmAnd/src/net/osmand/core/android/TileSourceProxyProvider.java diff --git a/OsmAnd-java/src/net/osmand/map/ITileSource.java b/OsmAnd-java/src/net/osmand/map/ITileSource.java index 0430afffda..61ffd3a0bb 100644 --- a/OsmAnd-java/src/net/osmand/map/ITileSource.java +++ b/OsmAnd-java/src/net/osmand/map/ITileSource.java @@ -1,28 +1,31 @@ package net.osmand.map; +import java.io.IOException; public interface ITileSource { - + public int getMaximumZoomSupported(); - + public String getName(); - + public int getTileSize(); - + public String getUrlToLoad(int x, int y, int zoom); - + + public byte[] getBytes(int x, int y, int zoom) throws IOException; + public int getMinimumZoomSupported(); - + public String getTileFormat(); - + public int getBitDensity(); - + public boolean isEllipticYTile(); - + public boolean couldBeDownloadedFromInternet(); - + public int getExpirationTimeMillis(); - + public int getExpirationTimeMinutes(); - + } diff --git a/OsmAnd/src/net/osmand/core/android/CoreResourcesFromAndroidAssetsCustom.java b/OsmAnd/src/net/osmand/core/android/CoreResourcesFromAndroidAssetsCustom.java index 427262e4ce..aff480433c 100644 --- a/OsmAnd/src/net/osmand/core/android/CoreResourcesFromAndroidAssetsCustom.java +++ b/OsmAnd/src/net/osmand/core/android/CoreResourcesFromAndroidAssetsCustom.java @@ -105,7 +105,7 @@ public class CoreResourcesFromAndroidAssetsCustom extends interface_ICoreResourc if (containgDir != null && !containgDir.exists()) containgDir.mkdirs(); extractedPath.createNewFile(); - + final InputStream resourceStream = assetManager.open(path, AssetManager.ACCESS_STREAMING); final FileOutputStream fileStream = new FileOutputStream(extractedPath); Algorithms.streamCopy(resourceStream, fileStream); @@ -114,7 +114,7 @@ public class CoreResourcesFromAndroidAssetsCustom extends interface_ICoreResourc } catch (IOException e2) { if (extractedPath.exists()) extractedPath.delete(); - + Log.e(NATIVE_TAG, "Failed to extract '" + resourceInBundle + "'", e2); continue; } @@ -239,8 +239,8 @@ public class CoreResourcesFromAndroidAssetsCustom extends interface_ICoreResourc } System.out.println(resourceEntry.defaultVariant.path.getAbsolutePath()); final SWIGTYPE_p_QByteArray bt; - if (resourceEntry.defaultVariant.offset == 0 && - resourceEntry.defaultVariant.size == resourceEntry.defaultVariant.path.length()) { + if (resourceEntry.defaultVariant.offset == 0 + && resourceEntry.defaultVariant.size == resourceEntry.defaultVariant.path.length()) { bt = SwigUtilities.readEntireFile(resourceEntry.defaultVariant.path.getAbsolutePath()); } else { bt = SwigUtilities.readPartOfFile(resourceEntry.defaultVariant.path.getAbsolutePath(), diff --git a/OsmAnd/src/net/osmand/core/android/TileSourceProxyProvider.java b/OsmAnd/src/net/osmand/core/android/TileSourceProxyProvider.java new file mode 100755 index 0000000000..877ce3cac5 --- /dev/null +++ b/OsmAnd/src/net/osmand/core/android/TileSourceProxyProvider.java @@ -0,0 +1,57 @@ +package net.osmand.core.android; + +import java.io.IOException; + +import net.osmand.core.jni.AlphaChannelPresence; +import net.osmand.core.jni.SWIGTYPE_p_QByteArray; +import net.osmand.core.jni.SwigUtilities; +import net.osmand.core.jni.TileId; +import net.osmand.core.jni.ZoomLevel; +import net.osmand.core.jni.interface_ImageMapLayerProvider; +import net.osmand.map.ITileSource; + +public class TileSourceProxyProvider extends interface_ImageMapLayerProvider { + + private final ITileSource tileSource; + + public TileSourceProxyProvider(ITileSource tileSource) { + this.tileSource = tileSource; + } + + @Override + public ZoomLevel getMinZoom() { + return ZoomLevel.swigToEnum(tileSource.getMinimumZoomSupported()); + } + + @Override + public ZoomLevel getMaxZoom() { + return ZoomLevel.swigToEnum(tileSource.getMaximumZoomSupported()); + } + + @Override + public SWIGTYPE_p_QByteArray obtainImage(TileId tileId, ZoomLevel zoom) { + byte[] image; + try { + image = tileSource.getBytes(tileId.getX(), tileId.getY(), zoom.swigValue()); + } catch(IOException e) { + return SwigUtilities.emptyQByteArray(); + } + + return SwigUtilities.createQByteArrayAsCopyOf(image); + } + + @Override + public long getTileSize() { + return tileSource.getTileSize(); + } + + @Override + public float getTileDensityFactor() { + return 1.0f; + } + + @Override + public AlphaChannelPresence getAlphaChannelPresence() { + return AlphaChannelPresence.Unknown; + } +} diff --git a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java index 5d603235f7..b7963616c9 100644 --- a/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java +++ b/OsmAnd/src/net/osmand/plus/SQLiteTileSource.java @@ -262,7 +262,7 @@ public class SQLiteTileSource implements ITileSource { return db.isDbLockedByOtherThreads(); } - public Bitmap getImage(int x, int y, int zoom, long[] timeHolder) { + public byte[] getBytes(int x, int y, int zoom, long[] timeHolder) throws IOException { SQLiteConnection db = getDatabase(); if(db == null){ return null; @@ -283,15 +283,7 @@ public class SQLiteTileSource implements ITileSource { } } cursor.close(); - if (blob != null) { - Bitmap bmp = null; - bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length); - if(bmp == null) { - // broken image delete it - db.execSQL("DELETE FROM tiles WHERE x = ? AND y = ? AND z = ?", params); - } - return bmp; - } + return blob; } return null; } finally { @@ -301,6 +293,34 @@ public class SQLiteTileSource implements ITileSource { } } } + + public byte[] getBytes(int x, int y, int zoom) throws IOException { + return getBytes(x, y, zoom, null); + } + + public Bitmap getImage(int x, int y, int zoom, long[] timeHolder) { + SQLiteConnection db = getDatabase(); + if(db == null){ + return null; + } + String[] params = new String[] { x + "", y + "", getFileZoom(zoom) + "" }; + byte[] blob; + try { + blob = getBytes(x, y, zoom, timeHolder); + } catch (IOException e) { + return null; + } + if (blob != null) { + Bitmap bmp = null; + bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length); + if(bmp == null) { + // broken image delete it + db.execSQL("DELETE FROM tiles WHERE x = ? AND y = ? AND z = ?", params); + } + return bmp; + } + return null; + } public ITileSource getBase() { return base; diff --git a/OsmAnd/src/net/osmand/plus/views/MapTileLayer.java b/OsmAnd/src/net/osmand/plus/views/MapTileLayer.java index 22d4e40848..ab4ba19ba0 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapTileLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapTileLayer.java @@ -12,6 +12,7 @@ import net.osmand.plus.R; import net.osmand.plus.rastermaps.OsmandRasterMapsPlugin; import net.osmand.plus.resources.ResourceManager; import net.osmand.util.MapUtils; +import android.annotation.SuppressLint; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; @@ -110,6 +111,7 @@ public class MapTileLayer extends BaseMapLayer { return mapTileAdapter; } + @SuppressLint("WrongCall") @Override public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSettings drawSettings) {