diff --git a/android/demo/src/com/mapzen/tangram/android/MainActivity.java b/android/demo/src/com/mapzen/tangram/android/MainActivity.java index ac80584206..cf194d9d18 100644 --- a/android/demo/src/com/mapzen/tangram/android/MainActivity.java +++ b/android/demo/src/com/mapzen/tangram/android/MainActivity.java @@ -102,10 +102,12 @@ public void run() { } HttpHandler getHttpHandler() { - HttpHandler handler = new HttpHandler(); File cacheDir = getExternalCacheDir(); + HttpHandler handler; if (cacheDir != null && cacheDir.exists()) { - handler.setCache(new File(cacheDir, "tile_cache"), 30 * 1024 * 1024); + handler = new HttpHandler(new File(cacheDir, "tile_cache"), 30 * 1024 * 1024); + } else { + handler = new HttpHandler(); } return handler; diff --git a/android/tangram/build.gradle b/android/tangram/build.gradle index aa5eaa88c9..903c00c9e5 100644 --- a/android/tangram/build.gradle +++ b/android/tangram/build.gradle @@ -49,7 +49,7 @@ afterEvaluate { } dependencies { - compile 'com.squareup.okhttp:okhttp:3.5.0' + compile 'com.squareup.okhttp3:okhttp:3.5.0' compile 'xmlpull:xmlpull:1.1.3.1' compile 'com.android.support:support-annotations:24.1.1' } diff --git a/android/tangram/src/com/mapzen/tangram/HttpHandler.java b/android/tangram/src/com/mapzen/tangram/HttpHandler.java index 276dad0211..678d7e7bf3 100644 --- a/android/tangram/src/com/mapzen/tangram/HttpHandler.java +++ b/android/tangram/src/com/mapzen/tangram/HttpHandler.java @@ -1,9 +1,10 @@ package com.mapzen.tangram; -import com.squareup.okhttp3.Cache; -import com.squareup.okhttp3.Callback; -import com.squareup.okhttp3.OkHttpClient; -import com.squareup.okhttp3.Request; +import okhttp3.Cache; +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.OkHttpClient; +import okhttp3.Request; import java.io.File; import java.io.IOException; @@ -16,16 +17,32 @@ public class HttpHandler { private OkHttpClient okClient; - protected Request.Builder okRequestBuilder; /** * Construct an {@code HttpHandler} with default options. */ public HttpHandler() { - okRequestBuilder = new Request.Builder(); - okClient = new OkHttpClient(); - okClient.setConnectTimeout(10, TimeUnit.SECONDS); - okClient.setReadTimeout(30, TimeUnit.SECONDS); + okClient = new OkHttpClient.Builder() + .connectTimeout(10, TimeUnit.SECONDS) + .writeTimeout(10, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .build(); + } + + /** + * Construct an {@code HttpHandler} with cache. + * Cache map data in a directory with a specified size limit + * @param directory Directory in which map data will be cached + * @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) + .connectTimeout(10, TimeUnit.SECONDS) + .writeTimeout(10, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .build(); } /** @@ -35,7 +52,10 @@ public HttpHandler() { * @return true if request was successfully started */ public boolean onRequest(String url, Callback cb) { - Request request = okRequestBuilder.tag(url).url(url).build(); + Request request = new Request.Builder() + .tag(url) + .url(url) + .build(); okClient.newCall(request).enqueue(cb); return true; } @@ -45,20 +65,22 @@ public boolean onRequest(String url, Callback cb) { * @param url URL of the request to be cancelled */ public void onCancel(String url) { - okClient.cancel(url); - } - /** - * Cache map data in a directory with a specified size limit - * @param directory Directory in which map data will be cached - * @param maxSize Maximum size of data to cache, in bytes - * @return true if cache was successfully created - */ - public boolean setCache(File directory, long maxSize) { - Cache okTileCache = new Cache(directory, maxSize); - okClient.setCache(okTileCache); + // check and cancel running call + for (Call runningCall : okClient.dispatcher().runningCalls()) { + if (runningCall.request().tag().equals(url)) { + runningCall.cancel(); + return; + } + } - return true; + // check and cancel queued call + for (Call queuedCall : okClient.dispatcher().queuedCalls()) { + if (queuedCall.request().tag().equals(url)) { + queuedCall.cancel(); + return; + } + } } } diff --git a/android/tangram/src/com/mapzen/tangram/MapController.java b/android/tangram/src/com/mapzen/tangram/MapController.java index f3eb8255c1..9f7af21806 100644 --- a/android/tangram/src/com/mapzen/tangram/MapController.java +++ b/android/tangram/src/com/mapzen/tangram/MapController.java @@ -8,9 +8,10 @@ import android.util.DisplayMetrics; import com.mapzen.tangram.TouchInput.Gestures; -import com.squareup.okhttp3.Callback; -import com.squareup.okhttp3.Request; -import com.squareup.okhttp3.Response; +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.Request; +import okhttp3.Response; import java.io.IOException; import java.util.HashMap; @@ -993,14 +994,15 @@ boolean startUrlRequest(String url, final long callbackPtr) throws Exception { if (httpHandler == null) { return false; } + httpHandler.onRequest(url, new Callback() { @Override - public void onFailure(Request request, IOException e) { + public void onFailure(Call call, IOException e) { nativeOnUrlFailure(callbackPtr); } @Override - public void onResponse(Response response) throws IOException { + public void onResponse(Call call, Response response) throws IOException { if (!response.isSuccessful()) { nativeOnUrlFailure(callbackPtr); throw new IOException("Unexpected response code: " + response);