Skip to content

Commit

Permalink
Upgrade to using OKHTTP3.5 APIs
Browse files Browse the repository at this point in the history
This is an API breaking change.

- Removed HttpHandler::setCache
- Adds a parameterized constructor to set HttpHandler cache
  • Loading branch information
tallytalwar committed Dec 18, 2016
1 parent f2b8d31 commit 78055af
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 30 deletions.
6 changes: 4 additions & 2 deletions android/demo/src/com/mapzen/tangram/android/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion android/tangram/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
66 changes: 44 additions & 22 deletions android/tangram/src/com/mapzen/tangram/HttpHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}

/**
Expand All @@ -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;
}
Expand All @@ -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;
}
}
}

}
12 changes: 7 additions & 5 deletions android/tangram/src/com/mapzen/tangram/MapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 78055af

Please sign in to comment.