|
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.graphics.Bitmap; |
|
19 import android.net.Uri; |
|
20 import java.io.IOException; |
|
21 import java.io.InputStream; |
|
22 |
|
23 /** A mechanism to load images from external resources such as a disk cache and/or the internet. */ |
|
24 public interface Downloader { |
|
25 /** |
|
26 * Download the specified image {@code url} from the internet. |
|
27 * |
|
28 * @param uri Remote image URL. |
|
29 * @param localCacheOnly If {@code true} the URL should only be loaded if available in a local |
|
30 * disk cache. |
|
31 * @return {@link Response} containing either a {@link Bitmap} representation of the request or an |
|
32 * {@link InputStream} for the image data. {@code null} can be returned to indicate a problem |
|
33 * loading the bitmap. |
|
34 * @throws IOException if the requested URL cannot successfully be loaded. |
|
35 */ |
|
36 Response load(Uri uri, boolean localCacheOnly) throws IOException; |
|
37 |
|
38 /** Thrown for non-2XX responses. */ |
|
39 class ResponseException extends IOException { |
|
40 public ResponseException(String message) { |
|
41 super(message); |
|
42 } |
|
43 } |
|
44 |
|
45 /** Response stream or bitmap and info. */ |
|
46 class Response { |
|
47 final InputStream stream; |
|
48 final Bitmap bitmap; |
|
49 final boolean cached; |
|
50 |
|
51 /** |
|
52 * Response image and info. |
|
53 * |
|
54 * @param bitmap Image. |
|
55 * @param loadedFromCache {@code true} if the source of the image is from a local disk cache. |
|
56 */ |
|
57 public Response(Bitmap bitmap, boolean loadedFromCache) { |
|
58 if (bitmap == null) { |
|
59 throw new IllegalArgumentException("Bitmap may not be null."); |
|
60 } |
|
61 this.stream = null; |
|
62 this.bitmap = bitmap; |
|
63 this.cached = loadedFromCache; |
|
64 } |
|
65 |
|
66 /** |
|
67 * Response stream and info. |
|
68 * |
|
69 * @param stream Image data stream. |
|
70 * @param loadedFromCache {@code true} if the source of the stream is from a local disk cache. |
|
71 */ |
|
72 public Response(InputStream stream, boolean loadedFromCache) { |
|
73 if (stream == null) { |
|
74 throw new IllegalArgumentException("Stream may not be null."); |
|
75 } |
|
76 this.stream = stream; |
|
77 this.bitmap = null; |
|
78 this.cached = loadedFromCache; |
|
79 } |
|
80 |
|
81 /** |
|
82 * Input stream containing image data. |
|
83 * <p> |
|
84 * If this returns {@code null}, image data will be available via {@link #getBitmap()}. |
|
85 */ |
|
86 public InputStream getInputStream() { |
|
87 return stream; |
|
88 } |
|
89 |
|
90 /** |
|
91 * Bitmap representing the image. |
|
92 * <p> |
|
93 * If this returns {@code null}, image data will be available via {@link #getInputStream()}. |
|
94 */ |
|
95 public Bitmap getBitmap() { |
|
96 return bitmap; |
|
97 } |
|
98 } |
|
99 } |