From 6695f4f598c2d2dfa0451879e4a02e48e171ad85 Mon Sep 17 00:00:00 2001 From: Louis Lagrange Date: Tue, 13 Mar 2018 11:38:18 +0100 Subject: [PATCH 1/3] [Android] Add HTTP cache by default (like iOS) --- .../modules/network/NetworkingModule.java | 6 ++-- .../modules/network/OkHttpClientProvider.java | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java b/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java index 5534eb6449cf55..39c0c22f04fd4a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java @@ -161,7 +161,7 @@ public interface ResponseHandler { * @param context the ReactContext of the application */ public NetworkingModule(final ReactApplicationContext context) { - this(context, null, OkHttpClientProvider.createClient(), null); + this(context, null, OkHttpClientProvider.createClient(context), null); } /** @@ -172,7 +172,7 @@ public NetworkingModule(final ReactApplicationContext context) { public NetworkingModule( ReactApplicationContext context, List networkInterceptorCreators) { - this(context, null, OkHttpClientProvider.createClient(), networkInterceptorCreators); + this(context, null, OkHttpClientProvider.createClient(context), networkInterceptorCreators); } /** @@ -181,7 +181,7 @@ public NetworkingModule( * caller does not provide one explicitly */ public NetworkingModule(ReactApplicationContext context, String defaultUserAgent) { - this(context, defaultUserAgent, OkHttpClientProvider.createClient(), null); + this(context, defaultUserAgent, OkHttpClientProvider.createClient(context), null); } @Override diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java b/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java index 766290c40e067b..fd7e811eae9d0f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java @@ -7,16 +7,19 @@ package com.facebook.react.modules.network; +import android.content.Context; import android.os.Build; import com.facebook.common.logging.FLog; +import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; +import okhttp3.Cache; import okhttp3.ConnectionSpec; import okhttp3.OkHttpClient; import okhttp3.TlsVersion; @@ -57,6 +60,13 @@ public static OkHttpClient createClient() { return createClientBuilder().build(); } + public static OkHttpClient createClient(Context context) { + if (sFactory != null) { + return sFactory.createNewNetworkModuleClient(); + } + return createClientBuilder(context).build(); + } + public static OkHttpClient.Builder createClientBuilder() { // No timeouts by default OkHttpClient.Builder client = new OkHttpClient.Builder() @@ -68,6 +78,24 @@ public static OkHttpClient.Builder createClientBuilder() { return enableTls12OnPreLollipop(client); } + public static OkHttpClient.Builder createClientBuilder(Context context) { + int cacheSize = 10 * 1024 * 1024; // 10 Mo + return createClientBuilder(context, cacheSize); + } + + public static OkHttpClient.Builder createClientBuilder(Context context, int cacheSize) { + OkHttpClient.Builder client = createClientBuilder(); + + if (cacheSize == 0) { + return client; + } + + File cacheDirectory = new File(context.getCacheDir(), "http-cache"); + Cache cache = new Cache(cacheDirectory, cacheSize); + + return client.cache(cache); + } + /* On Android 4.1-4.4 (API level 16 to 19) TLS 1.1 and 1.2 are available but not enabled by default. The following method From d9dc5fb0b91664ae358da1a6de194786be5795f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= <165856+hramos@users.noreply.github.com> Date: Tue, 15 Jan 2019 13:53:23 -0800 Subject: [PATCH 2/3] Update OkHttpClientProvider.java --- .../facebook/react/modules/network/OkHttpClientProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java b/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java index fd7e811eae9d0f..fc68c607140cd4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java @@ -79,7 +79,7 @@ public static OkHttpClient.Builder createClientBuilder() { } public static OkHttpClient.Builder createClientBuilder(Context context) { - int cacheSize = 10 * 1024 * 1024; // 10 Mo + int cacheSize = 10 * 1024 * 1024; // 10 Mb return createClientBuilder(context, cacheSize); } From 74a7770c9d2531ef7b2a7948febd0602165a00d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ramos?= <165856+hramos@users.noreply.github.com> Date: Wed, 16 Jan 2019 15:13:47 -0800 Subject: [PATCH 3/3] Use Megaoctets --- .../facebook/react/modules/network/OkHttpClientProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java b/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java index fc68c607140cd4..fd7e811eae9d0f 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java @@ -79,7 +79,7 @@ public static OkHttpClient.Builder createClientBuilder() { } public static OkHttpClient.Builder createClientBuilder(Context context) { - int cacheSize = 10 * 1024 * 1024; // 10 Mb + int cacheSize = 10 * 1024 * 1024; // 10 Mo return createClientBuilder(context, cacheSize); }