93 lines
3.3 KiB
Java
93 lines
3.3 KiB
Java
package com.squareup.picasso;
|
|
|
|
import android.content.Context;
|
|
import android.net.Uri;
|
|
import android.net.http.HttpResponseCache;
|
|
import android.os.Build;
|
|
import com.squareup.picasso.Downloader;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class UrlConnectionDownloader implements Downloader {
|
|
static volatile Object b;
|
|
private static final Object c = new Object();
|
|
private static final ThreadLocal<StringBuilder> d = new ThreadLocal<StringBuilder>() { // from class: com.squareup.picasso.UrlConnectionDownloader.1
|
|
/* JADX INFO: Access modifiers changed from: protected */
|
|
@Override // java.lang.ThreadLocal
|
|
public StringBuilder initialValue() {
|
|
return new StringBuilder();
|
|
}
|
|
};
|
|
private final Context a;
|
|
|
|
private static class ResponseCacheIcs {
|
|
static Object a(Context context) throws IOException {
|
|
File b = Utils.b(context);
|
|
HttpResponseCache installed = HttpResponseCache.getInstalled();
|
|
return installed == null ? HttpResponseCache.install(b, Utils.a(b)) : installed;
|
|
}
|
|
}
|
|
|
|
public UrlConnectionDownloader(Context context) {
|
|
this.a = context.getApplicationContext();
|
|
}
|
|
|
|
protected HttpURLConnection a(Uri uri) throws IOException {
|
|
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(uri.toString()).openConnection();
|
|
httpURLConnection.setConnectTimeout(15000);
|
|
httpURLConnection.setReadTimeout(20000);
|
|
return httpURLConnection;
|
|
}
|
|
|
|
@Override // com.squareup.picasso.Downloader
|
|
public Downloader.Response a(Uri uri, int i) throws IOException {
|
|
String sb;
|
|
if (Build.VERSION.SDK_INT >= 14) {
|
|
a(this.a);
|
|
}
|
|
HttpURLConnection a = a(uri);
|
|
a.setUseCaches(true);
|
|
if (i != 0) {
|
|
if (NetworkPolicy.isOfflineOnly(i)) {
|
|
sb = "only-if-cached,max-age=2147483647";
|
|
} else {
|
|
StringBuilder sb2 = d.get();
|
|
sb2.setLength(0);
|
|
if (!NetworkPolicy.shouldReadFromDiskCache(i)) {
|
|
sb2.append("no-cache");
|
|
}
|
|
if (!NetworkPolicy.shouldWriteToDiskCache(i)) {
|
|
if (sb2.length() > 0) {
|
|
sb2.append(',');
|
|
}
|
|
sb2.append("no-store");
|
|
}
|
|
sb = sb2.toString();
|
|
}
|
|
a.setRequestProperty("Cache-Control", sb);
|
|
}
|
|
int responseCode = a.getResponseCode();
|
|
if (responseCode < 300) {
|
|
return new Downloader.Response(a.getInputStream(), Utils.a(a.getHeaderField("X-Android-Response-Source")), a.getHeaderFieldInt("Content-Length", -1));
|
|
}
|
|
a.disconnect();
|
|
throw new Downloader.ResponseException(responseCode + " " + a.getResponseMessage(), i, responseCode);
|
|
}
|
|
|
|
private static void a(Context context) {
|
|
if (b == null) {
|
|
try {
|
|
synchronized (c) {
|
|
if (b == null) {
|
|
b = ResponseCacheIcs.a(context);
|
|
}
|
|
}
|
|
} catch (IOException unused) {
|
|
}
|
|
}
|
|
}
|
|
}
|