From 1b9455e8642903a38c26a048eea8bd39a4890b52 Mon Sep 17 00:00:00 2001 From: Alexey Pelykh Date: Thu, 26 Feb 2015 16:12:54 +0200 Subject: [PATCH] Underlay/Overlay threading --- .../core/android/TileSourceProxyProvider.java | 73 +++++++++++++++++-- .../plus/resources/AsyncLoadingThread.java | 2 +- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/OsmAnd/src/net/osmand/core/android/TileSourceProxyProvider.java b/OsmAnd/src/net/osmand/core/android/TileSourceProxyProvider.java index 32dd427809..d9bba6a567 100644 --- a/OsmAnd/src/net/osmand/core/android/TileSourceProxyProvider.java +++ b/OsmAnd/src/net/osmand/core/android/TileSourceProxyProvider.java @@ -6,6 +6,7 @@ import java.io.IOException; import net.osmand.IndexConstants; import net.osmand.core.jni.AlphaChannelPresence; +import net.osmand.core.jni.IMapDataProvider; import net.osmand.core.jni.MapStubStyle; import net.osmand.core.jni.SWIGTYPE_p_QByteArray; import net.osmand.core.jni.SwigUtilities; @@ -13,7 +14,9 @@ import net.osmand.core.jni.TileId; import net.osmand.core.jni.ZoomLevel; import net.osmand.core.jni.interface_ImageMapLayerProvider; import net.osmand.map.ITileSource; +import net.osmand.map.MapTileDownloader; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.resources.AsyncLoadingThread; import net.osmand.plus.resources.ResourceManager; public class TileSourceProxyProvider extends interface_ImageMapLayerProvider { @@ -49,14 +52,22 @@ public class TileSourceProxyProvider extends interface_ImageMapLayerProvider { String tileFilename = rm.calculateTileId(tileSource, tileId.getX(), tileId.getY(), zoom.swigValue()); - if (!rm.tileExistOnFileSystem(tileFilename, tileSource, tileId.getX(), tileId.getY(), - zoom.swigValue())) { - Bitmap loadedBitmap = null; - while (loadedBitmap == null) { - loadedBitmap = rm.getTileImageForMapSync(tileFilename, tileSource, - tileId.getX(), tileId.getY(), zoom.swigValue(), true); + final TileReadyCallback tileReadyCallback = new TileReadyCallback(tileSource, + tileId.getX(), tileId.getY(), zoom.swigValue()); + rm.getMapTileDownloader().addDownloaderCallback(tileReadyCallback); + while (rm.getTileImageForMapAsync(tileFilename, tileSource, tileId.getX(), tileId.getY(), + zoom.swigValue(), true) == null) { + synchronized (tileReadyCallback.getSync()) { + if (tileReadyCallback.isReady()) { + break; + } + try { + tileReadyCallback.getSync().wait(250); + } catch (InterruptedException e) { + } } } + rm.getMapTileDownloader().removeDownloaderCallback(tileReadyCallback); image = tileSource.getBytes(tileId.getX(), tileId.getY(), zoom.swigValue(), app.getAppPath(IndexConstants.TILES_INDEX_DIR).getAbsolutePath()); @@ -83,4 +94,54 @@ public class TileSourceProxyProvider extends interface_ImageMapLayerProvider { public AlphaChannelPresence getAlphaChannelPresence() { return AlphaChannelPresence.Unknown; } + + @Override + public IMapDataProvider.SourceType getSourceType() { + return IMapDataProvider.SourceType.NetworkDirect; + } + + private static class TileReadyCallback implements MapTileDownloader.IMapDownloaderCallback { + private final ITileSource tileSource; + private final int x; + private final int y; + private final int zoom; + private boolean ready = false; + private final Object sync = new Object(); + + public TileReadyCallback(ITileSource tileSource, int x, int y, int zoom) { + this.tileSource = tileSource; + this.x = x; + this.y = y; + this.zoom = zoom; + } + + public boolean isReady() { + return ready; + } + + public Object getSync() { + return sync; + } + + @Override + public void tileDownloaded(MapTileDownloader.DownloadRequest request) { + if (!(request instanceof AsyncLoadingThread.TileLoadDownloadRequest)) { + return; + } + AsyncLoadingThread.TileLoadDownloadRequest tileLoadRequest = + (AsyncLoadingThread.TileLoadDownloadRequest)request; + + if (tileSource != tileLoadRequest.tileSource || + x != tileLoadRequest.xTile || + y != tileLoadRequest.yTile || + zoom != tileLoadRequest.zoom) { + return; + } + + synchronized (sync) { + ready = true; + sync.notifyAll(); + } + } + } } diff --git a/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java b/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java index e680f7461f..d5d9ccd52f 100644 --- a/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java +++ b/OsmAnd/src/net/osmand/plus/resources/AsyncLoadingThread.java @@ -162,7 +162,7 @@ public class AsyncLoadingThread extends Thread { resourceManger.getMapTileDownloader().requestToDownload(req); } - protected static class TileLoadDownloadRequest extends DownloadRequest { + public static class TileLoadDownloadRequest extends DownloadRequest { public final String tileId; public final File dirWithTiles;