Skip to content

Commit

Permalink
[Android] Add HTTP cache by default (like iOS)
Browse files Browse the repository at this point in the history
  • Loading branch information
Minishlink committed Mar 13, 2018
1 parent 7a1c618 commit 55503a0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -172,7 +172,7 @@ public NetworkingModule(final ReactApplicationContext context) {
public NetworkingModule(
ReactApplicationContext context,
List<NetworkInterceptorCreator> networkInterceptorCreators) {
this(context, null, OkHttpClientProvider.createClient(), networkInterceptorCreators);
this(context, null, OkHttpClientProvider.createClient(context), networkInterceptorCreators);
}

/**
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 55503a0

Please sign in to comment.