79 lines
2.6 KiB
Java
79 lines
2.6 KiB
Java
package com.squareup.picasso;
|
|
|
|
import android.content.Context;
|
|
import android.net.Uri;
|
|
import com.squareup.okhttp.CacheControl;
|
|
import com.squareup.okhttp.OkHttpClient;
|
|
import com.squareup.okhttp.Request;
|
|
import com.squareup.okhttp.Response;
|
|
import com.squareup.okhttp.ResponseBody;
|
|
import com.squareup.picasso.Downloader;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class OkHttpDownloader implements Downloader {
|
|
private final OkHttpClient a;
|
|
|
|
public OkHttpDownloader(Context context) {
|
|
this(Utils.b(context));
|
|
}
|
|
|
|
private static OkHttpClient a() {
|
|
OkHttpClient okHttpClient = new OkHttpClient();
|
|
okHttpClient.setConnectTimeout(15000L, TimeUnit.MILLISECONDS);
|
|
okHttpClient.setReadTimeout(20000L, TimeUnit.MILLISECONDS);
|
|
okHttpClient.setWriteTimeout(20000L, TimeUnit.MILLISECONDS);
|
|
return okHttpClient;
|
|
}
|
|
|
|
public OkHttpDownloader(File file) {
|
|
this(file, Utils.a(file));
|
|
}
|
|
|
|
public OkHttpDownloader(File file, long j) {
|
|
this(a());
|
|
try {
|
|
this.a.setCache(new com.squareup.okhttp.Cache(file, j));
|
|
} catch (IOException unused) {
|
|
}
|
|
}
|
|
|
|
public OkHttpDownloader(OkHttpClient okHttpClient) {
|
|
this.a = okHttpClient;
|
|
}
|
|
|
|
@Override // com.squareup.picasso.Downloader
|
|
public Downloader.Response a(Uri uri, int i) throws IOException {
|
|
CacheControl cacheControl;
|
|
if (i == 0) {
|
|
cacheControl = null;
|
|
} else if (NetworkPolicy.isOfflineOnly(i)) {
|
|
cacheControl = CacheControl.FORCE_CACHE;
|
|
} else {
|
|
CacheControl.Builder builder = new CacheControl.Builder();
|
|
if (!NetworkPolicy.shouldReadFromDiskCache(i)) {
|
|
builder.noCache();
|
|
}
|
|
if (!NetworkPolicy.shouldWriteToDiskCache(i)) {
|
|
builder.noStore();
|
|
}
|
|
cacheControl = builder.build();
|
|
}
|
|
Request.Builder url = new Request.Builder().url(uri.toString());
|
|
if (cacheControl != null) {
|
|
url.cacheControl(cacheControl);
|
|
}
|
|
Response execute = this.a.newCall(url.build()).execute();
|
|
int code = execute.code();
|
|
if (code < 300) {
|
|
boolean z = execute.cacheResponse() != null;
|
|
ResponseBody body = execute.body();
|
|
return new Downloader.Response(body.byteStream(), z, body.contentLength());
|
|
}
|
|
execute.body().close();
|
|
throw new Downloader.ResponseException(code + " " + execute.message(), i, code);
|
|
}
|
|
}
|