1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/com/squareup/picasso/UrlConnectionDownloader.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,100 @@ 1.4 +/* 1.5 + * Copyright (C) 2013 Square, Inc. 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 +package com.squareup.picasso; 1.20 + 1.21 +import android.content.Context; 1.22 +import android.net.Uri; 1.23 +import android.net.http.HttpResponseCache; 1.24 +import android.os.Build; 1.25 +import java.io.File; 1.26 +import java.io.IOException; 1.27 +import java.net.HttpURLConnection; 1.28 +import java.net.URL; 1.29 + 1.30 +import static com.squareup.picasso.Utils.parseResponseSourceHeader; 1.31 + 1.32 +/** 1.33 + * A {@link Downloader} which uses {@link HttpURLConnection} to download images. A disk cache of 2% 1.34 + * of the total available space will be used (capped at 50MB) will automatically be installed in the 1.35 + * application's cache directory, when available. 1.36 + */ 1.37 +public class UrlConnectionDownloader implements Downloader { 1.38 + static final String RESPONSE_SOURCE = "X-Android-Response-Source"; 1.39 + 1.40 + private static final Object lock = new Object(); 1.41 + static volatile Object cache; 1.42 + 1.43 + private final Context context; 1.44 + 1.45 + public UrlConnectionDownloader(Context context) { 1.46 + this.context = context.getApplicationContext(); 1.47 + } 1.48 + 1.49 + protected HttpURLConnection openConnection(Uri path) throws IOException { 1.50 + HttpURLConnection connection = (HttpURLConnection) new URL(path.toString()).openConnection(); 1.51 + connection.setConnectTimeout(Utils.DEFAULT_CONNECT_TIMEOUT); 1.52 + connection.setReadTimeout(Utils.DEFAULT_READ_TIMEOUT); 1.53 + return connection; 1.54 + } 1.55 + 1.56 + @Override public Response load(Uri uri, boolean localCacheOnly) throws IOException { 1.57 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 1.58 + installCacheIfNeeded(context); 1.59 + } 1.60 + 1.61 + HttpURLConnection connection = openConnection(uri); 1.62 + connection.setUseCaches(true); 1.63 + if (localCacheOnly) { 1.64 + connection.setRequestProperty("Cache-Control", "only-if-cached,max-age=" + Integer.MAX_VALUE); 1.65 + } 1.66 + 1.67 + int responseCode = connection.getResponseCode(); 1.68 + if (responseCode >= 300) { 1.69 + connection.disconnect(); 1.70 + throw new ResponseException(responseCode + " " + connection.getResponseMessage()); 1.71 + } 1.72 + 1.73 + boolean fromCache = parseResponseSourceHeader(connection.getHeaderField(RESPONSE_SOURCE)); 1.74 + 1.75 + return new Response(connection.getInputStream(), fromCache); 1.76 + } 1.77 + 1.78 + private static void installCacheIfNeeded(Context context) { 1.79 + // DCL + volatile should be safe after Java 5. 1.80 + if (cache == null) { 1.81 + try { 1.82 + synchronized (lock) { 1.83 + if (cache == null) { 1.84 + cache = ResponseCacheIcs.install(context); 1.85 + } 1.86 + } 1.87 + } catch (IOException ignored) { 1.88 + } 1.89 + } 1.90 + } 1.91 + 1.92 + private static class ResponseCacheIcs { 1.93 + static Object install(Context context) throws IOException { 1.94 + File cacheDir = Utils.createDefaultCacheDir(context); 1.95 + HttpResponseCache cache = HttpResponseCache.getInstalled(); 1.96 + if (cache == null) { 1.97 + long maxSize = Utils.calculateDiskCacheSize(cacheDir); 1.98 + cache = HttpResponseCache.install(cacheDir, maxSize); 1.99 + } 1.100 + return cache; 1.101 + } 1.102 + } 1.103 +}