Android samples: use OkHttp cache

This commit is contained in:
Emux 2020-09-02 16:56:25 +03:00
parent 6616c22a32
commit 074320d6d2
No known key found for this signature in database
GPG Key ID: 64ED9980896038C3
8 changed files with 113 additions and 170 deletions

View File

@ -1,6 +1,6 @@
/*
* Copyright 2013 Hannes Janetzek
* Copyright 2016-2018 devemux86
* Copyright 2016-2020 devemux86
*
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
*
@ -20,8 +20,8 @@ package org.oscim.android.test;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import org.oscim.android.cache.TileCache;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import org.oscim.core.MapPosition;
import org.oscim.layers.TileGridLayer;
import org.oscim.layers.tile.vector.VectorTileLayer;
@ -29,11 +29,10 @@ import org.oscim.theme.VtmThemes;
import org.oscim.tiling.TileSource;
import org.oscim.tiling.source.OkHttpEngine;
import org.oscim.tiling.source.oscimap4.OSciMap4TileSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
public class BaseMapActivity extends MapActivity {
static final Logger log = LoggerFactory.getLogger(BaseMapActivity.class);
static final boolean USE_CACHE = false;
@ -41,8 +40,6 @@ public class BaseMapActivity extends MapActivity {
TileSource mTileSource;
TileGridLayer mGridLayer;
private TileCache mCache;
public BaseMapActivity(int contentView) {
super(contentView);
}
@ -54,15 +51,19 @@ public class BaseMapActivity extends MapActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (USE_CACHE) {
// Cache the tiles into file system
File cacheDirectory = new File(getExternalCacheDir(), "tiles");
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(cacheDirectory, cacheSize);
builder.cache(cache);
}
mTileSource = OSciMap4TileSource.builder()
.httpFactory(new OkHttpEngine.OkHttpFactory())
.httpFactory(new OkHttpEngine.OkHttpFactory(builder))
.build();
if (USE_CACHE) {
mCache = new TileCache(this, null, "tile.db");
mCache.setCacheSize(512 * (1 << 10));
mTileSource.setCache(mCache);
}
mBaseLayer = mMap.setBaseMap(mTileSource);
/* set initial position on first run */
@ -72,14 +73,6 @@ public class BaseMapActivity extends MapActivity {
mMap.setMapPosition(53.08, 8.83, Math.pow(2, 16));
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mCache != null)
mCache.dispose();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

View File

@ -16,22 +16,21 @@
package org.oscim.android.test;
import android.os.Bundle;
import org.oscim.android.cache.TileCache;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import org.oscim.core.MapPosition;
import org.oscim.core.MercatorProjection;
import org.oscim.layers.tile.bitmap.BitmapTileLayer;
import org.oscim.renderer.MapRenderer;
import org.oscim.tiling.source.OkHttpEngine;
import org.oscim.tiling.source.bitmap.BitmapTileSource;
import org.oscim.tiling.source.bitmap.DefaultSources;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Collections;
public class BitmapTileActivity extends MapActivity {
static final Logger log = LoggerFactory.getLogger(BitmapTileActivity.class);
private static final boolean USE_CACHE = false;
private final BitmapTileSource mTileSource;
@ -43,12 +42,9 @@ public class BitmapTileActivity extends MapActivity {
public BitmapTileActivity(BitmapTileSource tileSource) {
super(R.layout.activity_map);
tileSource.setHttpRequestHeaders(Collections.singletonMap("User-Agent", "vtm-android-example"));
mTileSource = tileSource;
}
private TileCache mCache;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -58,31 +54,24 @@ public class BitmapTileActivity extends MapActivity {
if (mTileSource == null)
return;
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (USE_CACHE) {
String cacheFile = mTileSource.getUrl()
.toString()
.replaceFirst("https?://", "")
.replaceAll("/", "-");
log.debug("use bitmap cache {}", cacheFile);
mCache = new TileCache(this, null, cacheFile);
mCache.setCacheSize(512 * (1 << 10));
mTileSource.setCache(mCache);
// Cache the tiles into file system
File cacheDirectory = new File(getExternalCacheDir(), "tiles");
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(cacheDirectory, cacheSize);
builder.cache(cache);
}
mTileSource.setHttpEngine(new OkHttpEngine.OkHttpFactory(builder));
mTileSource.setHttpRequestHeaders(Collections.singletonMap("User-Agent", "vtm-android-example"));
mBitmapLayer = new BitmapTileLayer(mMap, mTileSource);
mMap.layers().add(mBitmapLayer);
//loooop(1);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mCache != null)
mCache.dispose();
}
// Stress testing
void loooop(final int i) {
final long time = (long) (500 + Math.random() * 1000);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 devemux86
* Copyright 2018-2020 devemux86
*
* 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,10 +16,10 @@ package org.oscim.android.test;
import android.os.Build;
import android.os.Bundle;
import okhttp3.Cache;
import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import org.oscim.android.cache.TileCache;
import org.oscim.layers.tile.bitmap.BitmapTileLayer;
import org.oscim.layers.tile.buildings.BuildingLayer;
import org.oscim.layers.tile.vector.VectorTileLayer;
@ -30,6 +30,7 @@ import org.oscim.tiling.source.UrlTileSource;
import org.oscim.tiling.source.bitmap.DefaultSources;
import org.oscim.tiling.source.mvt.MapilionMvtTileSource;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -41,13 +42,18 @@ public class MapilionMvtActivity extends MapActivity {
private static final boolean USE_CACHE = false;
private TileCache mCache;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (USE_CACHE) {
// Cache the tiles into file system
File cacheDirectory = new File(getExternalCacheDir(), "tiles");
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(cacheDirectory, cacheSize);
builder.cache(cache);
}
// https://github.com/square/okhttp/issues/4053
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
@ -71,13 +77,6 @@ public class MapilionMvtActivity extends MapActivity {
//.locale("en")
.build();
if (USE_CACHE) {
// Cache the tiles into a local SQLite database
mCache = new TileCache(this, null, "tile.db");
mCache.setCacheSize(512 * (1 << 10));
tileSource.setCache(mCache);
}
VectorTileLayer l = mMap.setBaseMap(tileSource);
mMap.setTheme(VtmThemes.OPENMAPTILES);
@ -91,12 +90,4 @@ public class MapilionMvtActivity extends MapActivity {
mMap.layers().add(new BuildingLayer(mMap, l));
mMap.layers().add(new LabelLayer(mMap, l));
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mCache != null)
mCache.dispose();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 devemux86
* Copyright 2017-2020 devemux86
*
* 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
@ -15,8 +15,8 @@
package org.oscim.android.test;
import android.os.Bundle;
import org.oscim.android.cache.TileCache;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import org.oscim.layers.tile.buildings.BuildingLayer;
import org.oscim.layers.tile.vector.VectorTileLayer;
import org.oscim.layers.tile.vector.labeling.LabelLayer;
@ -25,41 +25,35 @@ import org.oscim.tiling.source.OkHttpEngine;
import org.oscim.tiling.source.UrlTileSource;
import org.oscim.tiling.source.geojson.MapzenGeojsonTileSource;
import java.io.File;
public class MapzenGeojsonActivity extends MapActivity {
private static final boolean USE_CACHE = false;
private TileCache mCache;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (USE_CACHE) {
// Cache the tiles into file system
File cacheDirectory = new File(getExternalCacheDir(), "tiles");
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(cacheDirectory, cacheSize);
builder.cache(cache);
}
UrlTileSource tileSource = MapzenGeojsonTileSource.builder()
.apiKey("xxxxxxx") // Put a proper API key
.httpFactory(new OkHttpEngine.OkHttpFactory())
.httpFactory(new OkHttpEngine.OkHttpFactory(builder))
//.locale("en")
.build();
if (USE_CACHE) {
// Cache the tiles into a local SQLite database
mCache = new TileCache(this, null, "tile.db");
mCache.setCacheSize(512 * (1 << 10));
tileSource.setCache(mCache);
}
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 (mCache != null)
mCache.dispose();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 devemux86
* Copyright 2016-2020 devemux86
* Copyright 2017 Mathieu De Brito
*
* This program is free software: you can redistribute it and/or modify it under the
@ -16,8 +16,8 @@
package org.oscim.android.test;
import android.os.Bundle;
import org.oscim.android.cache.TileCache;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import org.oscim.layers.tile.buildings.BuildingLayer;
import org.oscim.layers.tile.vector.VectorTileLayer;
import org.oscim.layers.tile.vector.labeling.LabelLayer;
@ -26,41 +26,35 @@ import org.oscim.tiling.source.OkHttpEngine;
import org.oscim.tiling.source.UrlTileSource;
import org.oscim.tiling.source.mvt.MapzenMvtTileSource;
import java.io.File;
public class MapzenMvtActivity extends MapActivity {
private static final boolean USE_CACHE = false;
private TileCache mCache;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (USE_CACHE) {
// Cache the tiles into file system
File cacheDirectory = new File(getExternalCacheDir(), "tiles");
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(cacheDirectory, cacheSize);
builder.cache(cache);
}
UrlTileSource tileSource = MapzenMvtTileSource.builder()
.apiKey("xxxxxxx") // Put a proper API key
.httpFactory(new OkHttpEngine.OkHttpFactory())
.httpFactory(new OkHttpEngine.OkHttpFactory(builder))
//.locale("en")
.build();
if (USE_CACHE) {
// Cache the tiles into a local SQLite database
mCache = new TileCache(this, null, "tile.db");
mCache.setCacheSize(512 * (1 << 10));
tileSource.setCache(mCache);
}
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 (mCache != null)
mCache.dispose();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2018 devemux86
* Copyright 2018-2020 devemux86
*
* 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
@ -15,8 +15,8 @@
package org.oscim.android.test;
import android.os.Bundle;
import org.oscim.android.cache.TileCache;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import org.oscim.layers.tile.buildings.BuildingLayer;
import org.oscim.layers.tile.vector.VectorTileLayer;
import org.oscim.layers.tile.vector.labeling.LabelLayer;
@ -25,41 +25,35 @@ import org.oscim.tiling.source.OkHttpEngine;
import org.oscim.tiling.source.UrlTileSource;
import org.oscim.tiling.source.geojson.NextzenGeojsonTileSource;
import java.io.File;
public class NextzenGeojsonActivity extends MapActivity {
private static final boolean USE_CACHE = false;
private TileCache mCache;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (USE_CACHE) {
// Cache the tiles into file system
File cacheDirectory = new File(getExternalCacheDir(), "tiles");
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(cacheDirectory, cacheSize);
builder.cache(cache);
}
UrlTileSource tileSource = NextzenGeojsonTileSource.builder()
.apiKey("xxxxxxx") // Put a proper API key
.httpFactory(new OkHttpEngine.OkHttpFactory())
.httpFactory(new OkHttpEngine.OkHttpFactory(builder))
//.locale("en")
.build();
if (USE_CACHE) {
// Cache the tiles into a local SQLite database
mCache = new TileCache(this, null, "tile.db");
mCache.setCacheSize(512 * (1 << 10));
tileSource.setCache(mCache);
}
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 (mCache != null)
mCache.dispose();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2018 devemux86
* Copyright 2018-2020 devemux86
*
* 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
@ -15,8 +15,8 @@
package org.oscim.android.test;
import android.os.Bundle;
import org.oscim.android.cache.TileCache;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import org.oscim.layers.tile.buildings.BuildingLayer;
import org.oscim.layers.tile.vector.VectorTileLayer;
import org.oscim.layers.tile.vector.labeling.LabelLayer;
@ -25,41 +25,35 @@ import org.oscim.tiling.source.OkHttpEngine;
import org.oscim.tiling.source.UrlTileSource;
import org.oscim.tiling.source.mvt.NextzenMvtTileSource;
import java.io.File;
public class NextzenMvtActivity extends MapActivity {
private static final boolean USE_CACHE = false;
private TileCache mCache;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (USE_CACHE) {
// Cache the tiles into file system
File cacheDirectory = new File(getExternalCacheDir(), "tiles");
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(cacheDirectory, cacheSize);
builder.cache(cache);
}
UrlTileSource tileSource = NextzenMvtTileSource.builder()
.apiKey("xxxxxxx") // Put a proper API key
.httpFactory(new OkHttpEngine.OkHttpFactory())
.httpFactory(new OkHttpEngine.OkHttpFactory(builder))
//.locale("en")
.build();
if (USE_CACHE) {
// Cache the tiles into a local SQLite database
mCache = new TileCache(this, null, "tile.db");
mCache.setCacheSize(512 * (1 << 10));
tileSource.setCache(mCache);
}
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 (mCache != null)
mCache.dispose();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 devemux86
* Copyright 2016-2020 devemux86
*
* 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
@ -15,8 +15,8 @@
package org.oscim.android.test;
import android.os.Bundle;
import org.oscim.android.cache.TileCache;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import org.oscim.layers.tile.buildings.BuildingLayer;
import org.oscim.layers.tile.vector.VectorTileLayer;
import org.oscim.layers.tile.vector.labeling.LabelLayer;
@ -25,41 +25,35 @@ import org.oscim.tiling.source.OkHttpEngine;
import org.oscim.tiling.source.UrlTileSource;
import org.oscim.tiling.source.mvt.OpenMapTilesMvtTileSource;
import java.io.File;
public class OpenMapTilesMvtActivity extends MapActivity {
private static final boolean USE_CACHE = false;
private TileCache mCache;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (USE_CACHE) {
// Cache the tiles into file system
File cacheDirectory = new File(getExternalCacheDir(), "tiles");
int cacheSize = 10 * 1024 * 1024; // 10 MB
Cache cache = new Cache(cacheDirectory, cacheSize);
builder.cache(cache);
}
UrlTileSource tileSource = OpenMapTilesMvtTileSource.builder()
.apiKey("xxxxxxx") // Put a proper API key
.httpFactory(new OkHttpEngine.OkHttpFactory())
.httpFactory(new OkHttpEngine.OkHttpFactory(builder))
//.locale("en")
.build();
if (USE_CACHE) {
// Cache the tiles into a local SQLite database
mCache = new TileCache(this, null, "tile.db");
mCache.setCacheSize(512 * (1 << 10));
tileSource.setCache(mCache);
}
VectorTileLayer l = mMap.setBaseMap(tileSource);
mMap.setTheme(VtmThemes.OPENMAPTILES);
mMap.layers().add(new BuildingLayer(mMap, l));
mMap.layers().add(new LabelLayer(mMap, l));
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mCache != null)
mCache.dispose();
}
}