OkHttp external cache improvements, closes #135
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
- Symbol rotation [#294](https://github.com/mapsforge/vtm/issues/294)
|
- Symbol rotation [#294](https://github.com/mapsforge/vtm/issues/294)
|
||||||
- Osmagray theme [#300](https://github.com/mapsforge/vtm/issues/300)
|
- Osmagray theme [#300](https://github.com/mapsforge/vtm/issues/300)
|
||||||
|
- OkHttp external cache [#135](https://github.com/mapsforge/vtm/issues/135)
|
||||||
- Many other minor improvements and bug fixes
|
- Many other minor improvements and bug fixes
|
||||||
- [Solved issues](https://github.com/mapsforge/vtm/issues?q=is%3Aclosed+milestone%3A0.8.0)
|
- [Solved issues](https://github.com/mapsforge/vtm/issues?q=is%3Aclosed+milestone%3A0.8.0)
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,9 @@ import org.oscim.tiling.source.mvt.MapboxTileSource;
|
|||||||
|
|
||||||
public class MapboxMapActivity extends MapActivity {
|
public class MapboxMapActivity extends MapActivity {
|
||||||
|
|
||||||
private TileCache tileCache;
|
private static final boolean USE_CACHE = true;
|
||||||
|
|
||||||
|
private TileCache mCache;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
@@ -36,14 +38,16 @@ public class MapboxMapActivity extends MapActivity {
|
|||||||
|
|
||||||
UrlTileSource tileSource = MapboxTileSource.builder()
|
UrlTileSource tileSource = MapboxTileSource.builder()
|
||||||
.apiKey("mapzen-xxxxxxx") // Put a proper API key
|
.apiKey("mapzen-xxxxxxx") // Put a proper API key
|
||||||
.httpFactory(new OkHttpEngine.OkHttpFactory(true)) // Use TileCache or provide a Cache for OkHttp
|
.httpFactory(new OkHttpEngine.OkHttpFactory())
|
||||||
//.locale("en")
|
//.locale("en")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
// Cache the tiles into a local sqlite database
|
if (USE_CACHE) {
|
||||||
tileCache = new TileCache(this, null, "tile_cache.db");
|
// Cache the tiles into a local SQLite database
|
||||||
tileCache.setCacheSize(512 * (1 << 10));
|
mCache = new TileCache(this, null, "tile.db");
|
||||||
tileSource.setCache(tileCache);
|
mCache.setCacheSize(512 * (1 << 10));
|
||||||
|
tileSource.setCache(mCache);
|
||||||
|
}
|
||||||
|
|
||||||
VectorTileLayer l = mMap.setBaseMap(tileSource);
|
VectorTileLayer l = mMap.setBaseMap(tileSource);
|
||||||
mMap.setTheme(VtmThemes.MAPZEN);
|
mMap.setTheme(VtmThemes.MAPZEN);
|
||||||
@@ -56,7 +60,7 @@ public class MapboxMapActivity extends MapActivity {
|
|||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|
||||||
if (tileCache != null)
|
if (mCache != null)
|
||||||
tileCache.dispose();
|
mCache.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@@ -43,24 +42,17 @@ public class OkHttpEngine implements HttpEngine {
|
|||||||
|
|
||||||
private final OkHttpClient mClient;
|
private final OkHttpClient mClient;
|
||||||
private final UrlTileSource mTileSource;
|
private final UrlTileSource mTileSource;
|
||||||
private final boolean mUseTileCache;
|
|
||||||
|
private InputStream mInputStream;
|
||||||
|
private byte[] mCachedData;
|
||||||
|
|
||||||
public static class OkHttpFactory implements HttpEngine.Factory {
|
public static class OkHttpFactory implements HttpEngine.Factory {
|
||||||
private final OkHttpClient mClient;
|
private final OkHttpClient mClient;
|
||||||
private boolean mUseTileCache = false;
|
|
||||||
|
|
||||||
public OkHttpFactory() {
|
public OkHttpFactory() {
|
||||||
mClient = new OkHttpClient();
|
mClient = new OkHttpClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Cache using ITileCache
|
|
||||||
*/
|
|
||||||
public OkHttpFactory(boolean useTileCache) {
|
|
||||||
this();
|
|
||||||
mUseTileCache = useTileCache;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* OkHttp cache implemented through {@link OkHttpClient.Builder#cache(Cache)}.
|
* OkHttp cache implemented through {@link OkHttpClient.Builder#cache(Cache)}.
|
||||||
*/
|
*/
|
||||||
@@ -72,22 +64,18 @@ public class OkHttpEngine implements HttpEngine {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public HttpEngine create(UrlTileSource tileSource) {
|
public HttpEngine create(UrlTileSource tileSource) {
|
||||||
return new OkHttpEngine(mClient, tileSource, mUseTileCache);
|
return new OkHttpEngine(mClient, tileSource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputStream inputStream;
|
public OkHttpEngine(OkHttpClient client, UrlTileSource tileSource) {
|
||||||
private byte[] cachedData;
|
|
||||||
|
|
||||||
public OkHttpEngine(OkHttpClient client, UrlTileSource tileSource, boolean useTileCache) {
|
|
||||||
mClient = client;
|
mClient = client;
|
||||||
mTileSource = tileSource;
|
mTileSource = tileSource;
|
||||||
mUseTileCache = useTileCache;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream read() throws IOException {
|
public InputStream read() throws IOException {
|
||||||
return inputStream;
|
return mInputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -103,22 +91,11 @@ public class OkHttpEngine implements HttpEngine {
|
|||||||
builder.addHeader(opt.getKey(), opt.getValue());
|
builder.addHeader(opt.getKey(), opt.getValue());
|
||||||
Request request = builder.build();
|
Request request = builder.build();
|
||||||
Response response = mClient.newCall(request).execute();
|
Response response = mClient.newCall(request).execute();
|
||||||
inputStream = response.body().byteStream();
|
if (mTileSource.tileCache != null) {
|
||||||
|
mCachedData = response.body().bytes();
|
||||||
if (mUseTileCache) {
|
mInputStream = new ByteArrayInputStream(mCachedData);
|
||||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
} else
|
||||||
int nRead;
|
mInputStream = response.body().byteStream();
|
||||||
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) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@@ -126,11 +103,11 @@ public class OkHttpEngine implements HttpEngine {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
if (inputStream == null)
|
if (mInputStream == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
final InputStream is = inputStream;
|
final InputStream is = mInputStream;
|
||||||
inputStream = null;
|
mInputStream = null;
|
||||||
new Thread(new Runnable() {
|
new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -141,9 +118,9 @@ public class OkHttpEngine implements HttpEngine {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setCache(OutputStream os) {
|
public void setCache(OutputStream os) {
|
||||||
if (mUseTileCache) {
|
if (mTileSource.tileCache != null) {
|
||||||
try {
|
try {
|
||||||
os.write(cachedData);
|
os.write(mCachedData);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@@ -152,8 +129,8 @@ public class OkHttpEngine implements HttpEngine {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean requestCompleted(boolean success) {
|
public boolean requestCompleted(boolean success) {
|
||||||
IOUtils.closeQuietly(inputStream);
|
IOUtils.closeQuietly(mInputStream);
|
||||||
inputStream = null;
|
mInputStream = null;
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user