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