From edf89ce36b318e6152b745049f75ddfdf789d2d9 Mon Sep 17 00:00:00 2001 From: Mathieu De Brito Date: Wed, 22 Feb 2017 17:12:26 +0100 Subject: [PATCH] Made OKHttpEngine compliant with TileCache (#299) #135 --- .../oscim/android/test/MapboxMapActivity.java | 19 +++++++- .../org/oscim/tiling/source/OkHttpEngine.java | 47 +++++++++++++++++-- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/vtm-android-example/src/org/oscim/android/test/MapboxMapActivity.java b/vtm-android-example/src/org/oscim/android/test/MapboxMapActivity.java index 2fbd02d9..02fcb189 100644 --- a/vtm-android-example/src/org/oscim/android/test/MapboxMapActivity.java +++ b/vtm-android-example/src/org/oscim/android/test/MapboxMapActivity.java @@ -1,5 +1,6 @@ /* * Copyright 2016-2017 devemux86 + * Copyright 2017 Mathieu De Brito * * This program is free software: you can redistribute it and/or modify it under the * terms of the GNU Lesser General Public License as published by the Free Software @@ -16,6 +17,7 @@ package org.oscim.android.test; import android.os.Bundle; +import org.oscim.android.cache.TileCache; import org.oscim.layers.tile.buildings.BuildingLayer; import org.oscim.layers.tile.vector.VectorTileLayer; import org.oscim.layers.tile.vector.labeling.LabelLayer; @@ -26,20 +28,35 @@ import org.oscim.tiling.source.mvt.MapboxTileSource; public class MapboxMapActivity extends MapActivity { + private TileCache tileCache; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); UrlTileSource tileSource = MapboxTileSource.builder() .apiKey("mapzen-xxxxxxx") // Put a proper API key - .httpFactory(new OkHttpEngine.OkHttpFactory()) + .httpFactory(new OkHttpEngine.OkHttpFactory(true)) // Use TileCache or provide a Cache for OkHttp //.locale("en") .build(); + // Cache the tiles into a local sqlite database + tileCache = new TileCache(this, null, "tile_cache.db"); + tileCache.setCacheSize(512 * (1 << 10)); + tileSource.setCache(tileCache); + VectorTileLayer l = mMap.setBaseMap(tileSource); mMap.setTheme(VtmThemes.MAPZEN); mMap.layers().add(new BuildingLayer(mMap, l)); mMap.layers().add(new LabelLayer(mMap, l)); } + + @Override + protected void onDestroy() { + super.onDestroy(); + + if (tileCache != null) + tileCache.dispose(); + } } diff --git a/vtm-http/src/org/oscim/tiling/source/OkHttpEngine.java b/vtm-http/src/org/oscim/tiling/source/OkHttpEngine.java index 7c28d489..9c2d610a 100644 --- a/vtm-http/src/org/oscim/tiling/source/OkHttpEngine.java +++ b/vtm-http/src/org/oscim/tiling/source/OkHttpEngine.java @@ -2,6 +2,7 @@ * Copyright 2014 Charles Greb * Copyright 2014 Hannes Janetzek * Copyright 2017 devemux86 + * Copyright 2017 Mathieu De Brito * * This file is part of the OpenScienceMap project (http://www.opensciencemap.org). * @@ -23,6 +24,8 @@ import org.oscim.utils.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -40,14 +43,27 @@ public class OkHttpEngine implements HttpEngine { private final OkHttpClient mClient; private final UrlTileSource mTileSource; + private final boolean mUseTileCache; public static class OkHttpFactory implements HttpEngine.Factory { private final OkHttpClient mClient; + private boolean mUseTileCache = false; public OkHttpFactory() { mClient = new OkHttpClient(); } + /** + * Cache using ITileCache + */ + public OkHttpFactory(boolean useTileCache) { + this(); + mUseTileCache = useTileCache; + } + + /** + * OkHttp cache implemented through {@link OkHttpClient.Builder#cache(Cache)}. + */ public OkHttpFactory(Cache cache) { mClient = new OkHttpClient.Builder() .cache(cache) @@ -56,15 +72,17 @@ public class OkHttpEngine implements HttpEngine { @Override public HttpEngine create(UrlTileSource tileSource) { - return new OkHttpEngine(mClient, tileSource); + return new OkHttpEngine(mClient, tileSource, mUseTileCache); } } private InputStream inputStream; + private byte[] cachedData; - public OkHttpEngine(OkHttpClient client, UrlTileSource tileSource) { + public OkHttpEngine(OkHttpClient client, UrlTileSource tileSource, boolean useTileCache) { mClient = client; mTileSource = tileSource; + mUseTileCache = useTileCache; } @Override @@ -86,6 +104,21 @@ public class OkHttpEngine implements HttpEngine { Request request = builder.build(); Response response = mClient.newCall(request).execute(); inputStream = response.body().byteStream(); + + if (mUseTileCache) { + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + int nRead; + byte[] data = new byte[16384]; + while ((nRead = inputStream.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); + } + + buffer.flush(); + + cachedData = buffer.toByteArray(); + inputStream = new ByteArrayInputStream(cachedData); + } + } catch (Exception e) { log.error(e.getMessage(), e); } @@ -106,11 +139,15 @@ public class OkHttpEngine implements HttpEngine { }).start(); } - /** - * OkHttp cache implemented through {@link OkHttpClient.Builder#cache(Cache)}. - */ @Override public void setCache(OutputStream os) { + if (mUseTileCache) { + try { + os.write(cachedData); + } catch (IOException e) { + log.error(e.getMessage(), e); + } + } } @Override