diff --git a/android/demo/src/main/java/com/mapzen/tangram/android/MainActivity.java b/android/demo/src/main/java/com/mapzen/tangram/android/MainActivity.java index cf194d9d18..6c25a71e93 100644 --- a/android/demo/src/main/java/com/mapzen/tangram/android/MainActivity.java +++ b/android/demo/src/main/java/com/mapzen/tangram/android/MainActivity.java @@ -103,14 +103,10 @@ public void run() { HttpHandler getHttpHandler() { File cacheDir = getExternalCacheDir(); - HttpHandler handler; if (cacheDir != null && cacheDir.exists()) { - handler = new HttpHandler(new File(cacheDir, "tile_cache"), 30 * 1024 * 1024); - } else { - handler = new HttpHandler(); + return new HttpHandler(new File(cacheDir, "tile_cache"), 30 * 1024 * 1024); } - - return handler; + return new HttpHandler(); } @Override diff --git a/android/tangram/src/main/java/com/mapzen/tangram/HttpHandler.java b/android/tangram/src/main/java/com/mapzen/tangram/HttpHandler.java index 678d7e7bf3..db91b71a8b 100644 --- a/android/tangram/src/main/java/com/mapzen/tangram/HttpHandler.java +++ b/android/tangram/src/main/java/com/mapzen/tangram/HttpHandler.java @@ -22,11 +22,7 @@ public class HttpHandler { * Construct an {@code HttpHandler} with default options. */ public HttpHandler() { - okClient = new OkHttpClient.Builder() - .connectTimeout(10, TimeUnit.SECONDS) - .writeTimeout(10, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .build(); + this(null, 0); } /** @@ -36,13 +32,16 @@ public HttpHandler() { * @param maxSize Maximum size of data to cache, in bytes */ public HttpHandler(File directory, long maxSize) { - Cache okTileCache = new Cache(directory, maxSize); - okClient = new OkHttpClient.Builder() - .cache(okTileCache) + OkHttpClient.Builder builder = new OkHttpClient.Builder() .connectTimeout(10, TimeUnit.SECONDS) .writeTimeout(10, TimeUnit.SECONDS) - .readTimeout(30, TimeUnit.SECONDS) - .build(); + .readTimeout(30, TimeUnit.SECONDS); + + if (directory != null && maxSize > 0) { + builder.cache(new Cache(directory, maxSize)); + } + + okClient = builder.build(); } /** @@ -53,7 +52,6 @@ public HttpHandler(File directory, long maxSize) { */ public boolean onRequest(String url, Callback cb) { Request request = new Request.Builder() - .tag(url) .url(url) .build(); okClient.newCall(request).enqueue(cb); @@ -68,17 +66,15 @@ public void onCancel(String url) { // check and cancel running call for (Call runningCall : okClient.dispatcher().runningCalls()) { - if (runningCall.request().tag().equals(url)) { + if (runningCall.request().url().toString().equals(url)) { runningCall.cancel(); - return; } } // check and cancel queued call for (Call queuedCall : okClient.dispatcher().queuedCalls()) { - if (queuedCall.request().tag().equals(url)) { + if (queuedCall.request().url().toString().equals(url)) { queuedCall.cancel(); - return; } } }