From 287e840745691db3b08b37322bc2d4e4c357a7d0 Mon Sep 17 00:00:00 2001 From: leon087 Date: Thu, 22 Jun 2017 15:00:16 +0800 Subject: [PATCH 1/2] leon087-branch --- .../modules/network/OkHttpClientProvider.java | 65 +++++++++++++------ 1 file changed, 46 insertions(+), 19 deletions(-) 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 5b82aa027a3fad..10189d3c5cde16 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 @@ -29,31 +29,58 @@ */ public class OkHttpClientProvider { - // Centralized OkHttpClient for all networking requests. - private static @Nullable OkHttpClient sClient; + public interface IProvider { + OkHttpClient create(); - public static OkHttpClient getOkHttpClient() { - if (sClient == null) { - sClient = createClient(); + OkHttpClient get(); + } + + public static class DefaultProvider implements OkHttpClientProvider.IProvider { + // Centralized OkHttpClient for all networking requests. + private static @Nullable OkHttpClient sClient; + + @Override + public OkHttpClient create() { + // No timeouts by default + OkHttpClient.Builder client = new OkHttpClient.Builder() + .connectTimeout(0, TimeUnit.MILLISECONDS) + .readTimeout(0, TimeUnit.MILLISECONDS) + .writeTimeout(0, TimeUnit.MILLISECONDS) + .cookieJar(new ReactCookieJarContainer()); + + return enableTls12OnPreLollipop(client).build(); + } + + @Override + public OkHttpClient get() { + if (sClient == null) { + sClient = create(); + } + return sClient; } - return sClient; } - - // okhttp3 OkHttpClient is immutable - // This allows app to init an OkHttpClient with custom settings. - public static void replaceOkHttpClient(OkHttpClient client) { - sClient = client; + + private static @Nullable OkHttpClientProvider.IProvider sPprovider; + + private static OkHttpClientProvider.IProvider getProvider() { + if (sPprovider == null) { + sPprovider = new DefaultProvider(); + } + return sPprovider; + } + + // okhttp3 OkHttpClientProvider.IProvider is immutable + // This allows app to init an OkHttpClientProvider.IProvider with custom settings. + public static void replaceProvider(OkHttpClientProvider.IProvider provider) { + sPprovider = provider; + } + + public static OkHttpClient getOkHttpClient() { + return getProvider().get(); } public static OkHttpClient createClient() { - // No timeouts by default - OkHttpClient.Builder client = new OkHttpClient.Builder() - .connectTimeout(0, TimeUnit.MILLISECONDS) - .readTimeout(0, TimeUnit.MILLISECONDS) - .writeTimeout(0, TimeUnit.MILLISECONDS) - .cookieJar(new ReactCookieJarContainer()); - - return enableTls12OnPreLollipop(client).build(); + return getProvider().create(); } /* From 3c492a9de9bef67c111ff04aa6e802e3b218538e Mon Sep 17 00:00:00 2001 From: leon087 Date: Thu, 22 Jun 2017 18:50:48 +0800 Subject: [PATCH 2/2] allows app to init an OkHttpClientProvider.IProvider with custom settings(FrescoModule and NetworkingModule) --- .../modules/network/OkHttpClientProvider.java | 65 +++++++++++++------ 1 file changed, 46 insertions(+), 19 deletions(-) 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 5b82aa027a3fad..26f8ca3da9c083 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 @@ -29,31 +29,58 @@ */ public class OkHttpClientProvider { - // Centralized OkHttpClient for all networking requests. - private static @Nullable OkHttpClient sClient; + public interface IProvider { + OkHttpClient create(); - public static OkHttpClient getOkHttpClient() { - if (sClient == null) { - sClient = createClient(); + OkHttpClient get(); + } + + public static class DefaultProvider implements OkHttpClientProvider.IProvider { + // Centralized OkHttpClient for all networking requests. + private @Nullable OkHttpClient client; + + @Override + public OkHttpClient create() { + // No timeouts by default + OkHttpClient.Builder client = new OkHttpClient.Builder() + .connectTimeout(0, TimeUnit.MILLISECONDS) + .readTimeout(0, TimeUnit.MILLISECONDS) + .writeTimeout(0, TimeUnit.MILLISECONDS) + .cookieJar(new ReactCookieJarContainer()); + + return enableTls12OnPreLollipop(client).build(); + } + + @Override + public OkHttpClient get() { + if (client == null) { + client = create(); + } + return client; } - return sClient; } - - // okhttp3 OkHttpClient is immutable - // This allows app to init an OkHttpClient with custom settings. - public static void replaceOkHttpClient(OkHttpClient client) { - sClient = client; + + private static @Nullable OkHttpClientProvider.IProvider sPprovider; + + private static OkHttpClientProvider.IProvider getProvider() { + if (sPprovider == null) { + sPprovider = new DefaultProvider(); + } + return sPprovider; + } + + // okhttp3 OkHttpClientProvider.IProvider is immutable + // This allows app to init an OkHttpClientProvider.IProvider with custom settings. + public static void replaceProvider(OkHttpClientProvider.IProvider provider) { + sPprovider = provider; + } + + public static OkHttpClient getOkHttpClient() { + return getProvider().get(); } public static OkHttpClient createClient() { - // No timeouts by default - OkHttpClient.Builder client = new OkHttpClient.Builder() - .connectTimeout(0, TimeUnit.MILLISECONDS) - .readTimeout(0, TimeUnit.MILLISECONDS) - .writeTimeout(0, TimeUnit.MILLISECONDS) - .cookieJar(new ReactCookieJarContainer()); - - return enableTls12OnPreLollipop(client).build(); + return getProvider().create(); } /*