|
@@ -1,34 +1,47 @@
|
|
|
package eu.kanade.tachiyomi.data.network;
|
|
|
|
|
|
|
|
|
-import com.squareup.okhttp.CacheControl;
|
|
|
-import com.squareup.okhttp.FormEncodingBuilder;
|
|
|
-import com.squareup.okhttp.Headers;
|
|
|
-import com.squareup.okhttp.OkHttpClient;
|
|
|
-import com.squareup.okhttp.Request;
|
|
|
-import com.squareup.okhttp.RequestBody;
|
|
|
-import com.squareup.okhttp.Response;
|
|
|
+import android.content.Context;
|
|
|
|
|
|
+import java.io.File;
|
|
|
import java.net.CookieManager;
|
|
|
import java.net.CookiePolicy;
|
|
|
import java.net.CookieStore;
|
|
|
|
|
|
+import okhttp3.Cache;
|
|
|
+import okhttp3.CacheControl;
|
|
|
+import okhttp3.FormBody;
|
|
|
+import okhttp3.Headers;
|
|
|
+import okhttp3.JavaNetCookieJar;
|
|
|
+import okhttp3.OkHttpClient;
|
|
|
+import okhttp3.Request;
|
|
|
+import okhttp3.RequestBody;
|
|
|
+import okhttp3.Response;
|
|
|
import rx.Observable;
|
|
|
|
|
|
public final class NetworkHelper {
|
|
|
|
|
|
private OkHttpClient client;
|
|
|
+
|
|
|
private CookieManager cookieManager;
|
|
|
|
|
|
public final CacheControl NULL_CACHE_CONTROL = new CacheControl.Builder().noCache().build();
|
|
|
public final Headers NULL_HEADERS = new Headers.Builder().build();
|
|
|
- public final RequestBody NULL_REQUEST_BODY = new FormEncodingBuilder().build();
|
|
|
+ public final RequestBody NULL_REQUEST_BODY = new FormBody.Builder().build();
|
|
|
+
|
|
|
+ private static final int CACHE_SIZE = 5 * 1024 * 1024; // 5 MiB
|
|
|
+ private static final String CACHE_DIR_NAME = "network_cache";
|
|
|
+
|
|
|
+ public NetworkHelper(Context context) {
|
|
|
+ File cacheDir = new File(context.getCacheDir(), CACHE_DIR_NAME);
|
|
|
|
|
|
- public NetworkHelper() {
|
|
|
- client = new OkHttpClient();
|
|
|
cookieManager = new CookieManager();
|
|
|
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
|
|
|
- client.setCookieHandler(cookieManager);
|
|
|
+
|
|
|
+ client = new OkHttpClient.Builder()
|
|
|
+ .cookieJar(new JavaNetCookieJar(cookieManager))
|
|
|
+ .cache(new Cache(cacheDir, CACHE_SIZE))
|
|
|
+ .build();
|
|
|
}
|
|
|
|
|
|
public Observable<Response> getResponse(final String url, final Headers headers, final CacheControl cacheControl) {
|
|
@@ -86,19 +99,20 @@ public final class NetworkHelper {
|
|
|
.headers(headers != null ? headers : NULL_HEADERS)
|
|
|
.build();
|
|
|
|
|
|
- OkHttpClient progressClient = client.clone();
|
|
|
+ OkHttpClient progressClient = client.newBuilder()
|
|
|
+ .cache(null)
|
|
|
+ .addNetworkInterceptor(chain -> {
|
|
|
+ Response originalResponse = chain.proceed(chain.request());
|
|
|
+ return originalResponse.newBuilder()
|
|
|
+ .body(new ProgressResponseBody(originalResponse.body(), listener))
|
|
|
+ .build();
|
|
|
+ }).build();
|
|
|
|
|
|
- progressClient.networkInterceptors().add(chain -> {
|
|
|
- Response originalResponse = chain.proceed(chain.request());
|
|
|
- return originalResponse.newBuilder()
|
|
|
- .body(new ProgressResponseBody(originalResponse.body(), listener))
|
|
|
- .build();
|
|
|
- });
|
|
|
return Observable.just(progressClient.newCall(request).execute());
|
|
|
} catch (Throwable e) {
|
|
|
return Observable.error(e);
|
|
|
}
|
|
|
- }).retry(2);
|
|
|
+ }).retry(1);
|
|
|
}
|
|
|
|
|
|
public CookieStore getCookies() {
|