Made OKHttpEngine compliant with TileCache (#299) #135

This commit is contained in:
Mathieu De Brito 2017-02-22 17:12:26 +01:00 committed by Emux
parent 4f687e6fec
commit edf89ce36b
2 changed files with 60 additions and 6 deletions

View File

@ -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();
}
}

View File

@ -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