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