From db871d4a22536a63ada4d157f0bacf48f9fac066 Mon Sep 17 00:00:00 2001
From: Emux <devemux86@gmail.com>
Date: Thu, 8 Jun 2017 19:30:22 +0300
Subject: [PATCH] OkHttpEngine improvements #138

---
 .../org/oscim/tiling/source/OkHttpEngine.java | 38 +------------------
 .../src/org/oscim/test/MapboxTest.java        | 18 +++++----
 .../oscim/tiling/source/OkHttpEngineTest.java |  4 +-
 3 files changed, 15 insertions(+), 45 deletions(-)

diff --git a/vtm-http/src/org/oscim/tiling/source/OkHttpEngine.java b/vtm-http/src/org/oscim/tiling/source/OkHttpEngine.java
index e19f0f95..8a45e640 100644
--- a/vtm-http/src/org/oscim/tiling/source/OkHttpEngine.java
+++ b/vtm-http/src/org/oscim/tiling/source/OkHttpEngine.java
@@ -30,9 +30,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
 import java.util.Map.Entry;
-import java.util.concurrent.TimeUnit;
 
-import okhttp3.Cache;
 import okhttp3.OkHttpClient;
 import okhttp3.Request;
 import okhttp3.Response;
@@ -54,40 +52,8 @@ public class OkHttpEngine implements HttpEngine {
             mClientBuilder = new OkHttpClient.Builder();
         }
 
-        /**
-         * Sets the response cache to be used to read and write cached responses.
-         */
-        public OkHttpFactory cache(Cache cache) {
-            mClientBuilder.cache(cache);
-            return this;
-        }
-
-        /**
-         * Sets the default connect timeout for new connections. A value of 0 means no timeout,
-         * otherwise values must be between 1 and {@link Integer#MAX_VALUE} when converted to
-         * milliseconds.
-         */
-        public OkHttpFactory connectTimeout(long timeout, TimeUnit unit) {
-            mClientBuilder.connectTimeout(timeout, unit);
-            return this;
-        }
-
-        /**
-         * Sets the default read timeout for new connections. A value of 0 means no timeout, otherwise
-         * values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds.
-         */
-        public OkHttpFactory readTimeout(long timeout, TimeUnit unit) {
-            mClientBuilder.readTimeout(timeout, unit);
-            return this;
-        }
-
-        /**
-         * Sets the default write timeout for new connections. A value of 0 means no timeout, otherwise
-         * values must be between 1 and {@link Integer#MAX_VALUE} when converted to milliseconds.
-         */
-        public OkHttpFactory writeTimeout(long timeout, TimeUnit unit) {
-            mClientBuilder.writeTimeout(timeout, unit);
-            return this;
+        public OkHttpFactory(OkHttpClient.Builder clientBuilder) {
+            mClientBuilder = clientBuilder;
         }
 
         @Override
diff --git a/vtm-playground/src/org/oscim/test/MapboxTest.java b/vtm-playground/src/org/oscim/test/MapboxTest.java
index 6773b5d9..94f2cf5b 100644
--- a/vtm-playground/src/org/oscim/test/MapboxTest.java
+++ b/vtm-playground/src/org/oscim/test/MapboxTest.java
@@ -27,6 +27,7 @@ import java.io.File;
 import java.util.UUID;
 
 import okhttp3.Cache;
+import okhttp3.OkHttpClient;
 
 public class MapboxTest extends GdxMapApp {
 
@@ -34,14 +35,15 @@ public class MapboxTest extends GdxMapApp {
 
     @Override
     public void createLayers() {
-        // Cache the tiles into file system
-        File cacheDirectory = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
-        int cacheSize = 10 * 1024 * 1024; // 10 MB
-        Cache cache = new Cache(cacheDirectory, cacheSize);
-
-        OkHttpEngine.OkHttpFactory factory = new OkHttpEngine.OkHttpFactory();
-        if (USE_CACHE)
-            factory.cache(cache);
+        OkHttpClient.Builder builder = new OkHttpClient.Builder();
+        if (USE_CACHE) {
+            // Cache the tiles into file system
+            File cacheDirectory = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
+            int cacheSize = 10 * 1024 * 1024; // 10 MB
+            Cache cache = new Cache(cacheDirectory, cacheSize);
+            builder.cache(cache);
+        }
+        OkHttpEngine.OkHttpFactory factory = new OkHttpEngine.OkHttpFactory(builder);
 
         UrlTileSource tileSource = MapboxTileSource.builder()
                 .apiKey("mapzen-xxxxxxx") // Put a proper API key
diff --git a/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java b/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java
index d94ddd73..80b3e5fb 100644
--- a/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java
+++ b/vtm-tests/test/org/oscim/tiling/source/OkHttpEngineTest.java
@@ -12,6 +12,7 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 
 import okhttp3.Cache;
+import okhttp3.OkHttpClient;
 import okhttp3.mockwebserver.MockResponse;
 import okhttp3.mockwebserver.MockWebServer;
 import okhttp3.mockwebserver.RecordedRequest;
@@ -94,7 +95,8 @@ public class OkHttpEngineTest {
         Cache cache = new Cache(new File("tmp"), 1024);
         OSciMap4TileSource tileSource =
                 new OSciMap4TileSource(server.url("/tiles/vtm").toString());
-        engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory().cache(cache).create(tileSource);
+        OkHttpClient.Builder builder = new OkHttpClient.Builder().cache(cache);
+        engine = (OkHttpEngine) new OkHttpEngine.OkHttpFactory(builder).create(tileSource);
         engine.sendRequest(new Tile(1, 2, (byte) 3));
         engine.requestCompleted(true);
         assertThat(cache.requestCount()).isEqualTo(1);