mobile/android/thirdparty/com/squareup/picasso/UrlConnectionDownloader.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * Copyright (C) 2013 Square, Inc.
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16 package com.squareup.picasso;
michael@0 17
michael@0 18 import android.content.Context;
michael@0 19 import android.net.Uri;
michael@0 20 import android.net.http.HttpResponseCache;
michael@0 21 import android.os.Build;
michael@0 22 import java.io.File;
michael@0 23 import java.io.IOException;
michael@0 24 import java.net.HttpURLConnection;
michael@0 25 import java.net.URL;
michael@0 26
michael@0 27 import static com.squareup.picasso.Utils.parseResponseSourceHeader;
michael@0 28
michael@0 29 /**
michael@0 30 * A {@link Downloader} which uses {@link HttpURLConnection} to download images. A disk cache of 2%
michael@0 31 * of the total available space will be used (capped at 50MB) will automatically be installed in the
michael@0 32 * application's cache directory, when available.
michael@0 33 */
michael@0 34 public class UrlConnectionDownloader implements Downloader {
michael@0 35 static final String RESPONSE_SOURCE = "X-Android-Response-Source";
michael@0 36
michael@0 37 private static final Object lock = new Object();
michael@0 38 static volatile Object cache;
michael@0 39
michael@0 40 private final Context context;
michael@0 41
michael@0 42 public UrlConnectionDownloader(Context context) {
michael@0 43 this.context = context.getApplicationContext();
michael@0 44 }
michael@0 45
michael@0 46 protected HttpURLConnection openConnection(Uri path) throws IOException {
michael@0 47 HttpURLConnection connection = (HttpURLConnection) new URL(path.toString()).openConnection();
michael@0 48 connection.setConnectTimeout(Utils.DEFAULT_CONNECT_TIMEOUT);
michael@0 49 connection.setReadTimeout(Utils.DEFAULT_READ_TIMEOUT);
michael@0 50 return connection;
michael@0 51 }
michael@0 52
michael@0 53 @Override public Response load(Uri uri, boolean localCacheOnly) throws IOException {
michael@0 54 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
michael@0 55 installCacheIfNeeded(context);
michael@0 56 }
michael@0 57
michael@0 58 HttpURLConnection connection = openConnection(uri);
michael@0 59 connection.setUseCaches(true);
michael@0 60 if (localCacheOnly) {
michael@0 61 connection.setRequestProperty("Cache-Control", "only-if-cached,max-age=" + Integer.MAX_VALUE);
michael@0 62 }
michael@0 63
michael@0 64 int responseCode = connection.getResponseCode();
michael@0 65 if (responseCode >= 300) {
michael@0 66 connection.disconnect();
michael@0 67 throw new ResponseException(responseCode + " " + connection.getResponseMessage());
michael@0 68 }
michael@0 69
michael@0 70 boolean fromCache = parseResponseSourceHeader(connection.getHeaderField(RESPONSE_SOURCE));
michael@0 71
michael@0 72 return new Response(connection.getInputStream(), fromCache);
michael@0 73 }
michael@0 74
michael@0 75 private static void installCacheIfNeeded(Context context) {
michael@0 76 // DCL + volatile should be safe after Java 5.
michael@0 77 if (cache == null) {
michael@0 78 try {
michael@0 79 synchronized (lock) {
michael@0 80 if (cache == null) {
michael@0 81 cache = ResponseCacheIcs.install(context);
michael@0 82 }
michael@0 83 }
michael@0 84 } catch (IOException ignored) {
michael@0 85 }
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89 private static class ResponseCacheIcs {
michael@0 90 static Object install(Context context) throws IOException {
michael@0 91 File cacheDir = Utils.createDefaultCacheDir(context);
michael@0 92 HttpResponseCache cache = HttpResponseCache.getInstalled();
michael@0 93 if (cache == null) {
michael@0 94 long maxSize = Utils.calculateDiskCacheSize(cacheDir);
michael@0 95 cache = HttpResponseCache.install(cacheDir, maxSize);
michael@0 96 }
michael@0 97 return cache;
michael@0 98 }
michael@0 99 }
michael@0 100 }

mercurial